ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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');
44 parent::__construct();
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 $files = array();
83
84 foreach (new DirectoryIterator($this->forum_path) as $file) {
89 if ($file->isDir()) {
90 continue;
91 }
92
93 list($obj_id, $rest) = explode('_', $file->getFilename(), 2);
94 if ($obj_id == $this->obj_id) {
95 $files[] = array(
96 'path' => $file->getPathname(),
97 'md5' => md5($this->obj_id . '_' . $this->pos_id . '_' . $rest),
98 'name' => $rest,
99 'size' => $file->getSize(),
100 'ctime' => date('Y-m-d H:i:s', $file->getCTime())
101 );
102 }
103 }
104
105 return $files;
106 }
107
111 public function getFilesOfPost()
112 {
113 $files = array();
114
115 foreach (new DirectoryIterator($this->forum_path) as $file) {
120 if ($file->isDir()) {
121 continue;
122 }
123
124 list($obj_id, $rest) = explode('_', $file->getFilename(), 2);
125 if ($obj_id == $this->obj_id) {
126 list($pos_id, $rest) = explode('_', $rest, 2);
127 if ($pos_id == $this->getPosId()) {
128 $files[$rest] = array(
129 'path' => $file->getPathname(),
130 'md5' => md5($this->obj_id . '_' . $this->pos_id . '_' . $rest),
131 'name' => $rest,
132 'size' => $file->getSize(),
133 'ctime' => date('Y-m-d H:i:s', $file->getCTime())
134 );
135 }
136 }
137 }
138
139 return $files;
140 }
141
146 public function moveFilesOfPost($a_new_frm_id = 0)
147 {
148 if ((int) $a_new_frm_id) {
149 foreach (new DirectoryIterator($this->forum_path) as $file) {
154 if ($file->isDir()) {
155 continue;
156 }
157
158 list($obj_id, $rest) = explode('_', $file->getFilename(), 2);
159 if ($obj_id == $this->obj_id) {
160 list($pos_id, $rest) = explode('_', $rest, 2);
161 if ($pos_id == $this->getPosId()) {
163 $file->getPathname(),
164 $this->forum_path . '/' . $a_new_frm_id . '_' . $this->pos_id . '_' . $rest
165 );
166 }
167 }
168 }
169
170 return true;
171 }
172
173 return false;
174 }
175
176 public function ilClone($a_new_obj_id, $a_new_pos_id)
177 {
178 foreach ($this->getFilesOfPost() as $file) {
179 @copy(
180 $this->getForumPath() . "/" . $this->obj_id . "_" . $this->pos_id . "_" . $file["name"],
181 $this->getForumPath() . "/" . $a_new_obj_id . "_" . $a_new_pos_id . "_" . $file["name"]
182 );
183 }
184 return true;
185 }
186 public function delete()
187 {
188 foreach ($this->getFiles() as $file) {
189 if (file_exists($this->getForumPath() . "/" . $this->getObjId() . "_" . $file["name"])) {
190 unlink($this->getForumPath() . "/" . $this->getObjId() . "_" . $file["name"]);
191 }
192 }
193 return true;
194 }
195
205 public function storeUploadedFile($files)
206 {
207 if (isset($files['name']) && is_array($files['name'])) {
208 foreach ($files['name'] as $index => $name) {
209 // remove trailing '/'
210 $name = rtrim($name, '/');
211
213 $temp_name = $files['tmp_name'][$index];
214 $error = $files['error'][$index];
215
216 if (strlen($filename) && strlen($temp_name) && $error == 0) {
217 $path = $this->getForumPath() . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $filename;
218
219 $this->__rotateFiles($path);
220 ilUtil::moveUploadedFile($temp_name, $filename, $path);
221 }
222 }
223
224 return true;
225 } elseif (isset($files['name']) && is_string($files['name'])) {
226 // remove trailing '/'
227 $files['name'] = rtrim($files['name'], '/');
228
230 $temp_name = $files['tmp_name'];
231
232 $path = $this->getForumPath() . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $filename;
233
234 $this->__rotateFiles($path);
235 ilUtil::moveUploadedFile($temp_name, $filename, $path);
236
237 return true;
238 }
239
240 return false;
241 }
248 public function unlinkFiles($a_filenames)
249 {
250 if (is_array($a_filenames)) {
251 foreach ($a_filenames as $file) {
252 if (!$this->unlinkFile($file)) {
253 return $file;
254 }
255 }
256 }
257 return '';
258 }
265 public function unlinkFile($a_filename)
266 {
267 if (file_exists($this->forum_path . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $a_filename)) {
268 return unlink($this->forum_path . '/' . $this->obj_id . '_' . $this->pos_id . "_" . $a_filename);
269 }
270 }
277 public function getAbsolutePath($a_path)
278 {
279 return $this->forum_path . '/' . $this->obj_id . '_' . $this->pos_id . "_" . $a_path;
280 }
281
288 public function getFileDataByMD5Filename($a_md5_filename)
289 {
290 $files = ilUtil::getDir($this->forum_path);
291 foreach ((array) $files as $file) {
292 if ($file['type'] == 'file' && md5($file['entry']) == $a_md5_filename) {
293 return array(
294 'path' => $this->forum_path . '/' . $file['entry'],
295 'filename' => $file['entry'],
296 'clean_filename' => str_replace($this->obj_id . '_' . $this->pos_id . '_', '', $file['entry'])
297 );
298 }
299 }
300
301 return false;
302 }
303
310 public function unlinkFilesByMD5Filenames($a_md5_filename)
311 {
312 $files = ilUtil::getDir($this->forum_path);
313 if (is_array($a_md5_filename)) {
314 foreach ((array) $files as $file) {
315 if ($file['type'] == 'file' && in_array(md5($file['entry']), $a_md5_filename)) {
316 unlink($this->forum_path . '/' . $file['entry']);
317 }
318 }
319
320 return true;
321 } else {
322 foreach ((array) $files as $file) {
323 if ($file['type'] == 'file' && md5($file['entry']) == $a_md5_filename) {
324 return unlink($this->forum_path . '/' . $file['entry']);
325 }
326 }
327 }
328
329 return false;
330 }
331
338 public function checkFilesExist($a_files)
339 {
340 if ($a_files) {
341 foreach ($a_files as $file) {
342 if (!file_exists($this->forum_path . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $file)) {
343 return false;
344 }
345 }
346 return true;
347 }
348 return true;
349 }
350
351 // PRIVATE METHODS
352 public function __checkPath()
353 {
354 if (!@file_exists($this->getForumPath())) {
355 return false;
356 }
357 $this->__checkReadWrite();
358
359 return true;
360 }
367 public function __checkReadWrite()
368 {
369 if (is_writable($this->forum_path) && is_readable($this->forum_path)) {
370 return true;
371 } else {
372 $this->error->raiseError("Forum directory is not readable/writable by webserver", $this->error->FATAL);
373 }
374 }
381 public function __initDirectory()
382 {
383 if (is_writable($this->getPath())) {
384 if (mkdir($this->getPath() . '/' . FORUM_PATH)) {
385 if (chmod($this->getPath() . '/' . FORUM_PATH, 0755)) {
386 $this->forum_path = $this->getPath() . '/' . FORUM_PATH;
387 return true;
388 }
389 }
390 }
391 return false;
392 }
400 public function __rotateFiles($a_path)
401 {
402 if (file_exists($a_path)) {
403 $this->__rotateFiles($a_path . ".old");
404 return \ilFileUtils::rename($a_path, $a_path . '.old');
405 }
406 return true;
407 }
408
413 public function deliverFile($file)
414 {
415 if (!$path = $this->getFileDataByMD5Filename($file)) {
416 return ilUtil::sendFailure($this->lng->txt('error_reading_file'), true);
417 } else {
418 return ilUtil::deliverFile($path['path'], $path['clean_filename']);
419 }
420 }
421
425 public function deliverZipFile()
426 {
427 global $DIC;
428
429 $zip_file = $this->createZipFile();
430 if (!$zip_file) {
431 ilUtil::sendFailure($DIC->language()->txt('error_reading_file'), true);
432 return false;
433 } else {
434 $post = new ilForumPost($this->getPosId());
435 ilUtil::deliverFile($zip_file, $post->getSubject() . '.zip', '', false, true, false);
436 ilUtil::delDir($this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId());
437 exit();
438 }
439 }
440
444 protected function createZipFile()
445 {
446 $filesOfPost = $this->getFilesOfPost();
447 ksort($filesOfPost);
448
449 ilUtil::makeDirParents($this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId());
450 $tmp_dir = $this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId();
451 foreach ($filesOfPost as $file) {
452 @copy($file['path'], $tmp_dir . '/' . $file['name']);
453 }
454
455 $zip_file = null;
456 if (ilUtil::zip($tmp_dir, $this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId() . '.zip')) {
457 $zip_file = $this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId() . '.zip';
458 }
459
460 return $zip_file;
461 }
462}
exit
Definition: backend.php:16
$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 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.
$rest
Definition: goto.php:46
$index
Definition: metadata.php:60
$files
Definition: metarefresh.php:49
$post
Definition: post.php:34
global $DIC
Definition: saml.php:7