ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilFileDataForum.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3
13{
19 public $obj_id;
20 public $pos_id;
21
28
29 private $error;
30
38 public function __construct($a_obj_id = 0, $a_pos_id = 0)
39 {
40 global $DIC;
41 $this->error = $DIC['ilErr'];
42
43 define('FORUM_PATH', 'forum');
45 $this->forum_path = parent::getPath() . "/" . FORUM_PATH;
46
47 // IF DIRECTORY ISN'T CREATED CREATE IT
48 if (!$this->__checkPath()) {
49 $this->__initDirectory();
50 }
51 $this->obj_id = $a_obj_id;
52 $this->pos_id = $a_pos_id;
53 }
54
55 public function getObjId()
56 {
57 return $this->obj_id;
58 }
59 public function getPosId()
60 {
61 return $this->pos_id;
62 }
63 public function setPosId($a_id)
64 {
65 $this->pos_id = $a_id;
66 }
72 public function getForumPath()
73 {
74 return $this->forum_path;
75 }
76
80 public function getFiles()
81 {
82 $directory_iterator = new DirectoryIterator($this->forum_path);
83 $filter_iterator = new RegexIterator($directory_iterator, "/^{$this->obj_id}_(.+)$/");
84
85 $files = [];
86 foreach ($filter_iterator as $file) {
88 if (!$file->isFile()) {
89 continue;
90 }
91
92 list($obj_id, $rest) = explode('_', $file->getFilename(), 2);
93 if ($obj_id == $this->obj_id) {
94 $files[] = [
95 'path' => $file->getPathname(),
96 'md5' => md5($this->obj_id . '_' . $rest),
97 'name' => $rest,
98 'size' => $file->getSize(),
99 'ctime' => date('Y-m-d H:i:s', $file->getCTime())
100 ];
101 }
102 }
103
104 return $files;
105 }
106
110 public function getFilesOfPost()
111 {
112 $directoryIterator = new DirectoryIterator($this->forum_path);
113 $filterIterator = new RegexIterator($directoryIterator, "/^{$this->obj_id}_{$this->getPosId()}_(.+)$/");
114
115 $files = array();
116 foreach ($filterIterator as $file) {
117 if ($file->isFile()) {
118 list($obj_id, $pos_id, $rest) = explode('_', $file->getFilename(), 3);
119 $files[$rest] = array(
120 'path' => $file->getPathname(),
121 'md5' => md5($this->obj_id . '_' . $this->pos_id . '_' . $rest),
122 'name' => $rest,
123 'size' => $file->getSize(),
124 'ctime' => date('Y-m-d H:i:s', $file->getCTime())
125 );
126 }
127 }
128
129 return $files;
130 }
131
136 public function moveFilesOfPost($a_new_frm_id = 0)
137 {
138 if ($a_new_frm_id) {
139 $directory_iterator = new DirectoryIterator($this->forum_path);
140 $filter_iterator = new RegexIterator($directory_iterator, "/^{$this->obj_id}_(\d+)_(.+)$/");
141
142 foreach ($filter_iterator as $file) {
144 if (!$file->isFile()) {
145 continue;
146 }
147
148 [$obj_id, $pos_id, $rest] = explode('_', $file->getFilename(), 3);
149 if ((int) $obj_id !== (int) $this->obj_id || (int) $pos_id !== (int) $this->getPosId()) {
150 continue;
151 }
152
154 $file->getPathname(),
155 $this->forum_path . '/' . $a_new_frm_id . '_' . $this->pos_id . '_' . $rest
156 );
157 }
158
159 return true;
160 }
161
162 return false;
163 }
164
165 public function ilClone($a_new_obj_id, $a_new_pos_id)
166 {
167 foreach ($this->getFilesOfPost() as $file) {
168 @copy(
169 $this->getForumPath() . "/" . $this->obj_id . "_" . $this->pos_id . "_" . $file["name"],
170 $this->getForumPath() . "/" . $a_new_obj_id . "_" . $a_new_pos_id . "_" . $file["name"]
171 );
172 }
173 return true;
174 }
175 public function delete()
176 {
177 foreach ($this->getFiles() as $file) {
178 if (file_exists($this->getForumPath() . "/" . $this->getObjId() . "_" . $file["name"])) {
179 unlink($this->getForumPath() . "/" . $this->getObjId() . "_" . $file["name"]);
180 }
181 }
182 return true;
183 }
184
194 public function storeUploadedFile($files)
195 {
196 if (isset($files['name']) && is_array($files['name'])) {
197 foreach ($files['name'] as $index => $name) {
198 // remove trailing '/'
199 $name = rtrim($name, '/');
200
202 $temp_name = $files['tmp_name'][$index];
203 $error = $files['error'][$index];
204
205 if (strlen($filename) && strlen($temp_name) && $error == 0) {
206 $path = $this->getForumPath() . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $filename;
207
208 $this->__rotateFiles($path);
210 }
211 }
212
213 return true;
214 } elseif (isset($files['name']) && is_string($files['name'])) {
215 // remove trailing '/'
216 $files['name'] = rtrim($files['name'], '/');
217
218 $filename = ilUtil::_sanitizeFilemame($files['name']);
219 $temp_name = $files['tmp_name'];
220
221 $path = $this->getForumPath() . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $filename;
222
223 $this->__rotateFiles($path);
225
226 return true;
227 }
228
229 return false;
230 }
237 public function unlinkFiles($a_filenames)
238 {
239 if (is_array($a_filenames)) {
240 foreach ($a_filenames as $file) {
241 if (!$this->unlinkFile($file)) {
242 return $file;
243 }
244 }
245 }
246 return '';
247 }
254 public function unlinkFile($a_filename)
255 {
256 if (file_exists($this->forum_path . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $a_filename)) {
257 return unlink($this->forum_path . '/' . $this->obj_id . '_' . $this->pos_id . "_" . $a_filename);
258 }
259 }
266 public function getAbsolutePath($a_path)
267 {
268 return $this->forum_path . '/' . $this->obj_id . '_' . $this->pos_id . "_" . $a_path;
269 }
270
277 public function getFileDataByMD5Filename($a_md5_filename)
278 {
279 $files = ilUtil::getDir($this->forum_path);
280 foreach ((array) $files as $file) {
281 if ($file['type'] == 'file' && md5($file['entry']) == $a_md5_filename) {
282 return array(
283 'path' => $this->forum_path . '/' . $file['entry'],
284 'filename' => $file['entry'],
285 'clean_filename' => str_replace($this->obj_id . '_' . $this->pos_id . '_', '', $file['entry'])
286 );
287 }
288 }
289
290 return false;
291 }
292
299 public function unlinkFilesByMD5Filenames($a_md5_filename)
300 {
301 $files = ilUtil::getDir($this->forum_path);
302 if (is_array($a_md5_filename)) {
303 foreach ((array) $files as $file) {
304 if ($file['type'] == 'file' && in_array(md5($file['entry']), $a_md5_filename)) {
305 unlink($this->forum_path . '/' . $file['entry']);
306 }
307 }
308
309 return true;
310 } else {
311 foreach ((array) $files as $file) {
312 if ($file['type'] == 'file' && md5($file['entry']) == $a_md5_filename) {
313 return unlink($this->forum_path . '/' . $file['entry']);
314 }
315 }
316 }
317
318 return false;
319 }
320
327 public function checkFilesExist($a_files)
328 {
329 if ($a_files) {
330 foreach ($a_files as $file) {
331 if (!file_exists($this->forum_path . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $file)) {
332 return false;
333 }
334 }
335 return true;
336 }
337 return true;
338 }
339
340 // PRIVATE METHODS
341 public function __checkPath()
342 {
343 if (!@file_exists($this->getForumPath())) {
344 return false;
345 }
346 $this->__checkReadWrite();
347
348 return true;
349 }
356 public function __checkReadWrite()
357 {
358 if (is_writable($this->forum_path) && is_readable($this->forum_path)) {
359 return true;
360 } else {
361 $this->error->raiseError("Forum directory is not readable/writable by webserver", $this->error->FATAL);
362 }
363 }
370 public function __initDirectory()
371 {
372 if (is_writable($this->getPath())) {
373 if (mkdir($this->getPath() . '/' . FORUM_PATH)) {
374 if (chmod($this->getPath() . '/' . FORUM_PATH, 0755)) {
375 $this->forum_path = $this->getPath() . '/' . FORUM_PATH;
376 return true;
377 }
378 }
379 }
380 return false;
381 }
389 public function __rotateFiles($a_path)
390 {
391 if (file_exists($a_path)) {
392 $this->__rotateFiles($a_path . ".old");
393 return \ilFileUtils::rename($a_path, $a_path . '.old');
394 }
395 return true;
396 }
397
402 public function deliverFile($file)
403 {
404 if (!$path = $this->getFileDataByMD5Filename($file)) {
405 return ilUtil::sendFailure($this->lng->txt('error_reading_file'), true);
406 } else {
407 return ilUtil::deliverFile($path['path'], $path['clean_filename']);
408 }
409 }
410
414 public function deliverZipFile()
415 {
416 global $DIC;
417
418 $zip_file = $this->createZipFile();
419 if (!$zip_file) {
420 ilUtil::sendFailure($DIC->language()->txt('error_reading_file'), true);
421 return false;
422 } else {
423 $post = new ilForumPost($this->getPosId());
424 ilUtil::deliverFile($zip_file, $post->getSubject() . '.zip', '', false, true, false);
425 ilUtil::delDir($this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId());
426 $DIC->http()->close();
427 }
428 }
429
433 protected function createZipFile()
434 {
435 $filesOfPost = $this->getFilesOfPost();
436 ksort($filesOfPost);
437
438 ilUtil::makeDirParents($this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId());
439 $tmp_dir = $this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId();
440 foreach ($filesOfPost as $file) {
441 @copy($file['path'], $tmp_dir . '/' . $file['name']);
442 }
443
444 $zip_file = null;
445 if (ilUtil::zip($tmp_dir, $this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId() . '.zip')) {
446 $zip_file = $this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId() . '.zip';
447 }
448
449 return $zip_file;
450 }
451}
$filename
Definition: buildRTE.php:89
An exception for terminatinating execution or to throw for unit testing.
error($a_errmsg)
set error message @access public
This class handles all operations on files for the forum object.
__construct($a_obj_id=0, $a_pos_id=0)
Constructor call base constructors checks if directory is writable and sets the optional obj_id.
__rotateFiles($a_path)
rotate files with same name recursive method
getAbsolutePath($a_path)
get absolute path of filename
unlinkFilesByMD5Filenames($a_md5_filename)
get file data of a specific attachment
getFileDataByMD5Filename($a_md5_filename)
get file data of a specific attachment
__initDirectory()
init directory overwritten method @access public
getForumPath()
get forum path @access public
storeUploadedFile($files)
Store uploaded files in filesystem.
unlinkFile($a_filename)
unlink one uploaded file expects a filename e.g 'foo'
ilClone($a_new_obj_id, $a_new_pos_id)
__checkReadWrite()
check if directory is writable overwritten method from base class @access private
unlinkFiles($a_filenames)
unlink files: expects an array of filenames e.g.
checkFilesExist($a_files)
check if files exist
This class handles all operations on files in directory /ilias_data/.
getPath()
get Path @access public
static rename($a_source, $a_target)
Rename a file.
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static _sanitizeFilemame($a_filename)
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
static getDir($a_dir, $a_rec=false, $a_sub_dir="")
get directory
static deliverFile( $a_file, $a_filename, $a_mime='', $isInline=false, $removeAfterDelivery=false, $a_exit_after=true)
deliver file for download via browser.
static makeDirParents($a_dir)
Create a new directory and all parent directories.
global $DIC
Definition: goto.php:24
$rest
Definition: goto.php:48
if($format !==null) $name
Definition: metadata.php:230
$index
Definition: metadata.php:128
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc