ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
4require_once("./Services/FileSystem/classes/class.ilFileData.php");
5require_once("./Services/Utilities/classes/class.ilFileUtils.php");
6
16{
22 public $obj_id;
23 public $pos_id;
24
31
32 private $error;
33
41 public function __construct($a_obj_id = 0, $a_pos_id = 0)
42 {
43 global $DIC;
44 $this->error = $DIC['ilErr'];
45
46 define('FORUM_PATH', 'forum');
47 parent::__construct();
48 $this->forum_path = parent::getPath() . "/" . FORUM_PATH;
49
50 // IF DIRECTORY ISN'T CREATED CREATE IT
51 if (!$this->__checkPath()) {
52 $this->__initDirectory();
53 }
54 $this->obj_id = $a_obj_id;
55 $this->pos_id = $a_pos_id;
56 }
57
58 public function getObjId()
59 {
60 return $this->obj_id;
61 }
62 public function getPosId()
63 {
64 return $this->pos_id;
65 }
66 public function setPosId($a_id)
67 {
68 $this->pos_id = $a_id;
69 }
75 public function getForumPath()
76 {
77 return $this->forum_path;
78 }
79
83 public function getFiles()
84 {
85 $files = array();
86
87 foreach (new DirectoryIterator($this->forum_path) as $file) {
92 if ($file->isDir()) {
93 continue;
94 }
95
96 list($obj_id, $rest) = explode('_', $file->getFilename(), 2);
97 if ($obj_id == $this->obj_id) {
98 $files[] = array(
99 'path' => $file->getPathname(),
100 'md5' => md5($this->obj_id . '_' . $this->pos_id . '_' . $rest),
101 'name' => $rest,
102 'size' => $file->getSize(),
103 'ctime' => date('Y-m-d H:i:s', $file->getCTime())
104 );
105 }
106 }
107
108 return $files;
109 }
110
114 public function getFilesOfPost()
115 {
116 $files = array();
117
118 foreach (new DirectoryIterator($this->forum_path) as $file) {
123 if ($file->isDir()) {
124 continue;
125 }
126
127 list($obj_id, $rest) = explode('_', $file->getFilename(), 2);
128 if ($obj_id == $this->obj_id) {
129 list($pos_id, $rest) = explode('_', $rest, 2);
130 if ($pos_id == $this->getPosId()) {
131 $files[$rest] = array(
132 'path' => $file->getPathname(),
133 'md5' => md5($this->obj_id . '_' . $this->pos_id . '_' . $rest),
134 'name' => $rest,
135 'size' => $file->getSize(),
136 'ctime' => date('Y-m-d H:i:s', $file->getCTime())
137 );
138 }
139 }
140 }
141
142 return $files;
143 }
144
149 public function moveFilesOfPost($a_new_frm_id = 0)
150 {
151 if ((int) $a_new_frm_id) {
152 foreach (new DirectoryIterator($this->forum_path) as $file) {
157 if ($file->isDir()) {
158 continue;
159 }
160
161 list($obj_id, $rest) = explode('_', $file->getFilename(), 2);
162 if ($obj_id == $this->obj_id) {
163 list($pos_id, $rest) = explode('_', $rest, 2);
164 if ($pos_id == $this->getPosId()) {
166 $file->getPathname(),
167 $this->forum_path . '/' . $a_new_frm_id . '_' . $this->pos_id . '_' . $rest
168 );
169 }
170 }
171 }
172
173 return true;
174 }
175
176 return false;
177 }
178
179 public function ilClone($a_new_obj_id, $a_new_pos_id)
180 {
181 foreach ($this->getFilesOfPost() as $file) {
182 @copy(
183 $this->getForumPath() . "/" . $this->obj_id . "_" . $this->pos_id . "_" . $file["name"],
184 $this->getForumPath() . "/" . $a_new_obj_id . "_" . $a_new_pos_id . "_" . $file["name"]
185 );
186 }
187 return true;
188 }
189 public function delete()
190 {
191 foreach ($this->getFiles() as $file) {
192 if (file_exists($this->getForumPath() . "/" . $this->getObjId() . "_" . $file["name"])) {
193 unlink($this->getForumPath() . "/" . $this->getObjId() . "_" . $file["name"]);
194 }
195 }
196 return true;
197 }
198
208 public function storeUploadedFile($files)
209 {
210 if (isset($files['name']) && is_array($files['name'])) {
211 foreach ($files['name'] as $index => $name) {
212 // remove trailing '/'
213 $name = rtrim($name, '/');
214
216 $temp_name = $files['tmp_name'][$index];
217 $error = $files['error'][$index];
218
219 if (strlen($filename) && strlen($temp_name) && $error == 0) {
220 $path = $this->getForumPath() . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $filename;
221
222 $this->__rotateFiles($path);
223 ilUtil::moveUploadedFile($temp_name, $filename, $path);
224 }
225 }
226
227 return true;
228 } elseif (isset($files['name']) && is_string($files['name'])) {
229 // remove trailing '/'
230 $files['name'] = rtrim($files['name'], '/');
231
233 $temp_name = $files['tmp_name'];
234
235 $path = $this->getForumPath() . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $filename;
236
237 $this->__rotateFiles($path);
238 ilUtil::moveUploadedFile($temp_name, $filename, $path);
239
240 return true;
241 }
242
243 return false;
244 }
251 public function unlinkFiles($a_filenames)
252 {
253 if (is_array($a_filenames)) {
254 foreach ($a_filenames as $file) {
255 if (!$this->unlinkFile($file)) {
256 return $file;
257 }
258 }
259 }
260 return '';
261 }
268 public function unlinkFile($a_filename)
269 {
270 if (file_exists($this->forum_path . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $a_filename)) {
271 return unlink($this->forum_path . '/' . $this->obj_id . '_' . $this->pos_id . "_" . $a_filename);
272 }
273 }
280 public function getAbsolutePath($a_path)
281 {
282 return $this->forum_path . '/' . $this->obj_id . '_' . $this->pos_id . "_" . $a_path;
283 }
284
291 public function getFileDataByMD5Filename($a_md5_filename)
292 {
293 $files = ilUtil::getDir($this->forum_path);
294 foreach ((array) $files as $file) {
295 if ($file['type'] == 'file' && md5($file['entry']) == $a_md5_filename) {
296 return array(
297 'path' => $this->forum_path . '/' . $file['entry'],
298 'filename' => $file['entry'],
299 'clean_filename' => str_replace($this->obj_id . '_' . $this->pos_id . '_', '', $file['entry'])
300 );
301 }
302 }
303
304 return false;
305 }
306
313 public function unlinkFilesByMD5Filenames($a_md5_filename)
314 {
315 $files = ilUtil::getDir($this->forum_path);
316 if (is_array($a_md5_filename)) {
317 foreach ((array) $files as $file) {
318 if ($file['type'] == 'file' && in_array(md5($file['entry']), $a_md5_filename)) {
319 unlink($this->forum_path . '/' . $file['entry']);
320 }
321 }
322
323 return true;
324 } else {
325 foreach ((array) $files as $file) {
326 if ($file['type'] == 'file' && md5($file['entry']) == $a_md5_filename) {
327 return unlink($this->forum_path . '/' . $file['entry']);
328 }
329 }
330 }
331
332 return false;
333 }
334
341 public function checkFilesExist($a_files)
342 {
343 if ($a_files) {
344 foreach ($a_files as $file) {
345 if (!file_exists($this->forum_path . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $file)) {
346 return false;
347 }
348 }
349 return true;
350 }
351 return true;
352 }
353
354 // PRIVATE METHODS
355 public function __checkPath()
356 {
357 if (!@file_exists($this->getForumPath())) {
358 return false;
359 }
360 $this->__checkReadWrite();
361
362 return true;
363 }
370 public function __checkReadWrite()
371 {
372 if (is_writable($this->forum_path) && is_readable($this->forum_path)) {
373 return true;
374 } else {
375 $this->error->raiseError("Forum directory is not readable/writable by webserver", $this->error->FATAL);
376 }
377 }
384 public function __initDirectory()
385 {
386 if (is_writable($this->getPath())) {
387 if (mkdir($this->getPath() . '/' . FORUM_PATH)) {
388 if (chmod($this->getPath() . '/' . FORUM_PATH, 0755)) {
389 $this->forum_path = $this->getPath() . '/' . FORUM_PATH;
390 return true;
391 }
392 }
393 }
394 return false;
395 }
403 public function __rotateFiles($a_path)
404 {
405 if (file_exists($a_path)) {
406 $this->__rotateFiles($a_path . ".old");
407 return \ilFileUtils::rename($a_path, $a_path . '.old');
408 }
409 return true;
410 }
411
416 public function deliverFile($file)
417 {
418 if (!$path = $this->getFileDataByMD5Filename($file)) {
419 return ilUtil::sendFailure($this->lng->txt('error_reading_file'), true);
420 } else {
421 return ilUtil::deliverFile($path['path'], $path['clean_filename']);
422 }
423 }
424
428 public function deliverZipFile()
429 {
430 global $DIC;
431
432 $zip_file = $this->createZipFile();
433 if (!$zip_file) {
434 ilUtil::sendFailure($DIC->language()->txt('error_reading_file'), true);
435 return false;
436 } else {
437 $post = new ilForumPost($this->getPosId());
438 ilUtil::deliverFile($zip_file, $post->getSubject() . '.zip', '', false, true, false);
439 ilUtil::delDir($this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId());
440 exit();
441 }
442 }
443
447 protected function createZipFile()
448 {
449 $filesOfPost = $this->getFilesOfPost();
450 ksort($filesOfPost);
451
452 ilUtil::makeDirParents($this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId());
453 $tmp_dir = $this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId();
454 foreach ($filesOfPost as $file) {
455 @copy($file['path'], $tmp_dir . '/' . $file['name']);
456 }
457
458 $zip_file = null;
459 if (ilUtil::zip($tmp_dir, $this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId() . '.zip')) {
460 $zip_file = $this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId() . '.zip';
461 }
462
463 return $zip_file;
464 }
465}
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
$files
Definition: add-vimline.php:18
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
if($format !==null) $name
Definition: metadata.php:146
$index
Definition: metadata.php:60
$post
Definition: post.php:34
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file
global $DIC
Definition: saml.php:7