ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
4 require_once("./Services/FileSystem/classes/class.ilFileData.php");
5 require_once("./Services/Utilities/classes/class.ilFileUtils.php");
6 
16 {
22  var $obj_id;
23  var $pos_id;
24 
31 
39  public function __construct($a_obj_id = 0,$a_pos_id = 0)
40  {
41  define('FORUM_PATH', 'forum');
42  parent::__construct();
43  $this->forum_path = parent::getPath()."/".FORUM_PATH;
44 
45  // IF DIRECTORY ISN'T CREATED CREATE IT
46  if(!$this->__checkPath())
47  {
48  $this->__initDirectory();
49  }
50  $this->obj_id = $a_obj_id;
51  $this->pos_id = $a_pos_id;
52  }
53 
54  function getObjId()
55  {
56  return $this->obj_id;
57  }
58  function getPosId()
59  {
60  return $this->pos_id;
61  }
62  function setPosId($a_id)
63  {
64  $this->pos_id = $a_id;
65  }
71  function getForumPath()
72  {
73  return $this->forum_path;
74  }
75 
79  public function getFiles()
80  {
81  $files = array();
82 
83  foreach(new DirectoryIterator($this->forum_path) as $file)
84  {
89  if($file->isDir())
90  {
91  continue;
92  }
93 
94  list($obj_id, $rest) = explode('_', $file->getFilename(), 2);
95  if($obj_id == $this->obj_id)
96  {
97  $files[] = array(
98  'path' => $file->getPathname(),
99  'md5' => md5($this->obj_id . '_' . $this->pos_id . '_' . $rest),
100  'name' => $rest,
101  'size' => $file->getSize(),
102  'ctime' => date('Y-m-d H:i:s', $file->getCTime())
103  );
104  }
105  }
106 
107  return $files;
108  }
109 
113  public function getFilesOfPost()
114  {
115  $files = array();
116 
117  foreach(new DirectoryIterator($this->forum_path) as $file)
118  {
123  if($file->isDir())
124  {
125  continue;
126  }
127 
128  list($obj_id, $rest) = explode('_', $file->getFilename(), 2);
129  if($obj_id == $this->obj_id)
130  {
131  list($pos_id, $rest) = explode('_', $rest, 2);
132  if($pos_id == $this->getPosId())
133  {
134  $files[$rest] = array(
135  'path' => $file->getPathname(),
136  'md5' => md5($this->obj_id . '_' . $this->pos_id . '_' . $rest),
137  'name' => $rest,
138  'size' => $file->getSize(),
139  'ctime' => date('Y-m-d H:i:s', $file->getCTime())
140  );
141  }
142  }
143  }
144 
145  return $files;
146  }
147 
152  public function moveFilesOfPost($a_new_frm_id = 0)
153  {
154  if((int)$a_new_frm_id)
155  {
156  foreach(new DirectoryIterator($this->forum_path) as $file)
157  {
162  if($file->isDir())
163  {
164  continue;
165  }
166 
167  list($obj_id, $rest) = explode('_', $file->getFilename(), 2);
168  if($obj_id == $this->obj_id)
169  {
170  list($pos_id, $rest) = explode('_', $rest, 2);
171  if($pos_id == $this->getPosId())
172  {
174  $file->getPathname(),
175  $this->forum_path . '/' . $a_new_frm_id . '_' . $this->pos_id . '_' . $rest
176  );
177  }
178  }
179  }
180 
181  return true;
182  }
183 
184  return false;
185  }
186 
187  function ilClone($a_new_obj_id,$a_new_pos_id)
188  {
189  foreach($this->getFilesOfPost() as $file)
190  {
191  @copy($this->getForumPath()."/".$this->obj_id."_".$this->pos_id."_".$file["name"],
192  $this->getForumPath()."/".$a_new_obj_id."_".$a_new_pos_id."_".$file["name"]);
193  }
194  return true;
195  }
196  function delete()
197  {
198  foreach($this->getFiles() as $file)
199  {
200  if(file_exists($this->getForumPath()."/".$this->getObjId()."_".$file["name"]))
201  {
202  unlink($this->getForumPath()."/".$this->getObjId()."_".$file["name"]);
203  }
204  }
205  return true;
206  }
207 
218  {
219  if(isset($files['name']) && is_array($files['name']))
220  {
221  foreach($files['name'] as $index => $name)
222  {
223  // remove trailing '/'
224  $name = rtrim($name, '/');
225 
227  $temp_name = $files['tmp_name'][$index];
228  $error = $files['error'][$index];
229 
230  if(strlen($filename) && strlen($temp_name) && $error == 0)
231  {
232  $path = $this->getForumPath().'/'.$this->obj_id.'_'.$this->pos_id.'_'.$filename;
233 
234  $this->__rotateFiles($path);
236  }
237  }
238 
239  return true;
240  }
241  else if(isset($files['name']) && is_string($files['name']))
242  {
243  // remove trailing '/'
244  $files['name'] = rtrim($files['name'], '/');
245 
247  $temp_name = $files['tmp_name'];
248 
249  $path = $this->getForumPath().'/'.$this->obj_id.'_'.$this->pos_id.'_'.$filename;
250 
251  $this->__rotateFiles($path);
253 
254  return true;
255  }
256 
257  return false;
258  }
265  function unlinkFiles($a_filenames)
266  {
267  if(is_array($a_filenames))
268  {
269  foreach($a_filenames as $file)
270  {
271  if(!$this->unlinkFile($file))
272  {
273  return $file;
274  }
275  }
276  }
277  return '';
278  }
285  function unlinkFile($a_filename)
286  {
287  if(file_exists($this->forum_path.'/'.$this->obj_id.'_'.$this->pos_id.'_'.$a_filename))
288  {
289  return unlink($this->forum_path.'/'.$this->obj_id.'_'.$this->pos_id."_".$a_filename);
290  }
291  }
298  function getAbsolutePath($a_path)
299  {
300  return $this->forum_path.'/'.$this->obj_id.'_'.$this->pos_id."_".$a_path;
301  }
302 
309  public function getFileDataByMD5Filename($a_md5_filename)
310  {
311  $files = ilUtil::getDir( $this->forum_path );
312  foreach((array)$files as $file)
313  {
314  if($file['type'] == 'file' && md5($file['entry']) == $a_md5_filename)
315  {
316  return array(
317  'path' => $this->forum_path.'/'.$file['entry'],
318  'filename' => $file['entry'],
319  'clean_filename' => str_replace($this->obj_id.'_'.$this->pos_id.'_', '', $file['entry'])
320  );
321  }
322  }
323 
324  return false;
325  }
326 
333  function unlinkFilesByMD5Filenames($a_md5_filename)
334  {
335  $files = ilUtil::getDir( $this->forum_path );
336  if(is_array($a_md5_filename))
337  {
338  foreach((array)$files as $file)
339  {
340  if($file['type'] == 'file' && in_array(md5($file['entry']), $a_md5_filename))
341  {
342  unlink( $this->forum_path.'/'.$file['entry'] );
343  }
344  }
345 
346  return true;
347  }
348  else
349  {
350  foreach((array)$files as $file)
351  {
352  if($file['type'] == 'file' && md5($file['entry']) == $a_md5_filename)
353  {
354  return unlink( $this->forum_path.'/'.$file['entry'] );
355  }
356  }
357  }
358 
359  return false;
360  }
361 
368  function checkFilesExist($a_files)
369  {
370  if($a_files)
371  {
372  foreach($a_files as $file)
373  {
374  if(!file_exists($this->forum_path.'/'.$this->obj_id.'_'.$this->pos_id.'_'.$file))
375  {
376  return false;
377  }
378  }
379  return true;
380  }
381  return true;
382  }
383 
384  // PRIVATE METHODS
385  function __checkPath()
386  {
387  if(!@file_exists($this->getForumPath()))
388  {
389  return false;
390  }
391  $this->__checkReadWrite();
392 
393  return true;
394  }
401  function __checkReadWrite()
402  {
403  if(is_writable($this->forum_path) && is_readable($this->forum_path))
404  {
405  return true;
406  }
407  else
408  {
409  $this->ilias->raiseError("Forum directory is not readable/writable by webserver",$this->ilias->error_obj->FATAL);
410  }
411  }
418  function __initDirectory()
419  {
420  if(is_writable($this->getPath()))
421  {
422  if(mkdir($this->getPath().'/'.FORUM_PATH))
423  {
424  if(chmod($this->getPath().'/'.FORUM_PATH,0755))
425  {
426  $this->forum_path = $this->getPath().'/'.FORUM_PATH;
427  return true;
428  }
429  }
430  }
431  return false;
432  }
440  function __rotateFiles($a_path)
441  {
442  if(file_exists($a_path))
443  {
444  $this->__rotateFiles($a_path.".old");
445  return \ilFileUtils::rename($a_path, $a_path . '.old');
446  }
447  return true;
448  }
449 
454  public function deliverFile($file)
455  {
456  if(!$path = $this->getFileDataByMD5Filename($file))
457  {
458  return ilUtil::sendFailure($this->lng->txt('error_reading_file'), true);
459  }
460  else
461  {
462  return ilUtil::deliverFile($path['path'], $path['clean_filename']);
463  }
464  }
465 
469  public function deliverZipFile()
470  {
474  global $lng;
475 
476  $zip_file = $this->createZipFile();
477  if(!$zip_file)
478  {
479  ilUtil::sendFailure($lng->txt('error_reading_file'), true);
480  return false;
481  }
482  else
483  {
484  $post = new ilForumPost($this->getPosId());
485  ilUtil::deliverFile($zip_file, $post->getSubject() . '.zip', '', false, true, false);
486  ilUtil::delDir($this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId());
487  exit();
488  }
489  }
490 
494  protected function createZipFile()
495  {
496  $filesOfPost = $this->getFilesOfPost();
497  ksort($filesOfPost);
498 
499  ilUtil::makeDirParents($this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId());
500  $tmp_dir = $this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId();
501  foreach($filesOfPost as $file)
502  {
503  @copy($file['path'], $tmp_dir . '/' . $file['name']);
504  }
505 
506  $zip_file = null;
507  if(ilUtil::zip($tmp_dir, $this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId() . '.zip'))
508  {
509  $zip_file = $this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId() . '.zip';
510  }
511 
512  return $zip_file;
513  }
514 }
$files
Definition: add-vimline.php:18
checkFilesExist($a_files)
check if files exist
unlinkFiles($a_filenames)
unlink files: expects an array of filenames e.g.
static makeDirParents($a_dir)
Create a new directory and all parent directories.
$error
Definition: Error.php:17
__checkReadWrite()
check if directory is writable overwritten method from base class private
$rest
Definition: goto.php:48
getAbsolutePath($a_path)
get absolute path of filename
getPath()
get Path public
static getDir($a_dir, $a_rec=false, $a_sub_dir="")
get directory
__construct($a_obj_id=0, $a_pos_id=0)
Constructor call base constructors checks if directory is writable and sets the optional obj_id...
static rename($a_source, $a_target)
Rename a file.
static _sanitizeFilemame($a_filename)
This class handles all operations on files in directory /ilias_data/.
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
redirection script todo: (a better solution should control the processing via a xml file) ...
static deliverFile($a_file, $a_filename, $a_mime='', $isInline=false, $removeAfterDelivery=false, $a_exit_after=true)
deliver file for download via browser.
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
Create styles array
The data for the language used.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
unlinkFilesByMD5Filenames($a_md5_filename)
get file data of a specific attachment
__rotateFiles($a_path)
rotate files with same name recursive method
unlinkFile($a_filename)
unlink one uploaded file expects a filename e.g &#39;foo&#39;
global $lng
Definition: privfeed.php:17
This class handles all operations on files for the forum object.
getFileDataByMD5Filename($a_md5_filename)
get file data of a specific attachment
ilClone($a_new_obj_id, $a_new_pos_id)
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
__initDirectory()
init directory overwritten method public
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
getForumPath()
get forum path public
storeUploadedFile($files)
Store uploaded files in filesystem.