ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilFileDataForumDrafts.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2016 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 
15 {
16  protected $obj_id = 0;
17  protected $draft_id = 0;
18  protected $drafts_path = '';
19 
20  public function __construct($obj_id = 0, $draft_id)
21  {
22  $this->obj_id = $obj_id;
23  $this->draft_id = $draft_id;
24 
25  define('FORUM_DRAFTS_PATH', 'forum/drafts');
26  parent::__construct();
27  $this->drafts_path = parent::getPath()."/".FORUM_DRAFTS_PATH;
28 
29  // IF DIRECTORY ISN'T CREATED CREATE IT
30  if(!$this->__checkPath())
31  {
32  $this->__initDirectory();
33  }
34  }
35 
39  public function getObjId()
40  {
41  return $this->obj_id;
42  }
43 
47  public function setObjId($obj_id)
48  {
49  $this->obj_id = $obj_id;
50  }
51 
55  public function getDraftId()
56  {
57  return $this->draft_id;
58  }
59 
63  public function setDraftId($draft_id)
64  {
65  $this->draft_id = $draft_id;
66  }
67 
71  public function getDraftsPath()
72  {
73  return $this->drafts_path;
74  }
75 
76 
80  public function getFiles()
81  {
82  $files = array();
83 
84  foreach(new DirectoryIterator($this->getDraftsPath().'/'.$this->getDraftId()) as $file)
85  {
90  if($file->isDir())
91  {
92  continue;
93  }
94 
95  $files[] = array(
96  'path' => $file->getPathname(),
97  'md5' => md5($file->getFilename()),
98  'name' => $file->getFilename(),
99  'size' => $file->getSize(),
100  'ctime' => date('Y-m-d H:i:s', $file->getCTime())
101  );
102  }
103 
104  return $files;
105  }
106 
110  public function getFilesOfPost()
111  {
112  $files = array();
113 
114  foreach(new DirectoryIterator($this->getDraftsPath().'/'.$this->getDraftId()) as $file)
115  {
120  if($file->isDir())
121  {
122  continue;
123  }
124 
125  $files[$file->getFilename()] = array(
126  'path' => $file->getPathname(),
127  'md5' => md5($file->getFilename()),
128  'name' => $file->getFilename(),
129  'size' => $file->getSize(),
130  'ctime' => date('Y-m-d H:i:s', $file->getCTime())
131  );
132  }
133 
134  return $files;
135  }
136 
137  public function moveFilesOfDraft($forum_path, $new_post_id)
138  {
139  foreach($this->getFilesOfPost() as $file)
140  {
141  @copy($file['path'],
142  $forum_path.'/'.$this->obj_id.'_'.$new_post_id.'_'.$file['name']);
143  }
144  return true;
145  }
146 
150  public function delete()
151  {
152  ilUtil::delDir($this->getDraftsPath().'/'.$this->getDraftId());
153  return true;
154  }
155 
166  {
167  if(isset($files['name']) && is_array($files['name']))
168  {
169  foreach($files['name'] as $index => $name)
170  {
171  // remove trailing '/'
172  while(substr($name, -1) == '/')
173  {
174  $name = substr($name, 0, -1);
175  }
177  $temp_name = $files['tmp_name'][$index];
178  $error = $files['error'][$index];
179 
180  if(strlen($filename) && strlen($temp_name) && $error == 0)
181  {
182  $path = $this->getDraftsPath().'/'.$this->getDraftId().'/'.$filename;
183 
184  $this->__rotateFiles($path);
186  }
187  }
188 
189  return true;
190  }
191  else if(isset($files['name']) && is_string($files['name']))
192  {
193  // remove trailing '/'
194  while(substr($files['name'], -1) == '/')
195  {
196  $files['name'] = substr($files['name'], 0, -1);
197  }
199  $temp_name = $files['tmp_name'];
200 
201  $path = $this->getDraftsPath().'/'.$this->getDraftId().'/'.$filename;
202 
203  $this->__rotateFiles($path);
205 
206  return true;
207  }
208 
209  return false;
210  }
217  function unlinkFiles($a_filenames)
218  {
219  if(is_array($a_filenames))
220  {
221  foreach($a_filenames as $file)
222  {
223  if(!$this->unlinkFile($file))
224  {
225  return $file;
226  }
227  }
228  }
229  return '';
230  }
237  function unlinkFile($a_filename)
238  {
239  if(file_exists($this->getDraftsPath().'/'.$this->getDraftId().'/'.$a_filename))
240  {
241  return unlink($this->getDraftsPath().'/'.$this->getDraftId().'/'.$a_filename);
242  }
243  }
250  function getAbsolutePath($a_path)
251  {
252  return $this->getDraftsPath().'/'.$this->getDraftId();
253  }
254 
261  public function getFileDataByMD5Filename($a_md5_filename)
262  {
263  $files = ilUtil::getDir( $this->getDraftsPath().'/'.$this->getDraftId() );
264  foreach((array)$files as $file)
265  {
266  if($file['type'] == 'file' && md5($file['entry']) == $a_md5_filename)
267  {
268  return array(
269  'path' => $this->getDraftsPath().'/'.$this->getDraftId().'/'.$file['entry'],
270  'filename' => $file['entry'],
271  'clean_filename' => $file['entry']
272  );
273  }
274  }
275 
276  return false;
277  }
278 
285  function unlinkFilesByMD5Filenames($a_md5_filename)
286  {
287  $files = ilUtil::getDir( $this->getDraftsPath().'/'.$this->getDraftId() );
288  if(is_array($a_md5_filename))
289  {
290  foreach((array)$files as $file)
291  {
292  if($file['type'] == 'file' && in_array(md5($file['entry']), $a_md5_filename))
293  {
294  unlink( $this->getDraftsPath().'/'.$this->getDraftId().'/'.$file['entry'] );
295  }
296  }
297 
298  return true;
299  }
300  else
301  {
302  foreach((array)$files as $file)
303  {
304  if($file['type'] == 'file' && md5($file['entry']) == $a_md5_filename)
305  {
306  return unlink( $this->getDraftsPath().'/'.$this->getDraftId().'/'.$file['entry'] );
307  }
308  }
309  }
310 
311  return false;
312  }
313 
320  function checkFilesExist($a_files)
321  {
322  if($a_files)
323  {
324  foreach($a_files as $file)
325  {
326  if(!file_exists($this->getDraftsPath().'/'.$this->getDraftId().'/'.$file))
327  {
328  return false;
329  }
330  }
331  return true;
332  }
333  return true;
334  }
335 
336  // PRIVATE METHODS
337  function __checkPath()
338  {
339  if(!@file_exists($this->getDraftsPath().'/'.$this->getDraftId()))
340  {
341  return false;
342  }
343  $this->__checkReadWrite();
344 
345  return true;
346  }
353  function __checkReadWrite()
354  {
355  if(is_writable($this->getDraftsPath().'/'.$this->getDraftId()) && is_readable($this->getDraftsPath().'/'.$this->getDraftId()))
356  {
357  return true;
358  }
359  else
360  {
361  $this->ilias->raiseError("Forum directory is not readable/writable by webserver",$this->ilias->error_obj->FATAL);
362  }
363  }
370  function __initDirectory()
371  {
372  if(is_writable($this->getPath()))
373  {
374  if(ilUtil::makeDirParents($this->getDraftsPath()."/".$this->getDraftId()))
375  {
376  if(chmod($this->getDraftsPath()."/".$this->getDraftId(),0755))
377  {
378  return true;
379  }
380  }
381  }
382  return false;
383  }
391  function __rotateFiles($a_path)
392  {
393  if(file_exists($a_path))
394  {
395  $this->__rotateFiles($a_path.".old");
396  return \ilFileUtils::rename($a_path, $a_path . '.old');
397  }
398  return true;
399  }
400 
405  public function deliverFile($file)
406  {
407  if(!$path = $this->getFileDataByMD5Filename($file))
408  {
409  global $lng;
410  return ilUtil::sendFailure($lng->txt('error_reading_file'), true);
411  }
412  else
413  {
414  return ilUtil::deliverFile($path['path'], $path['clean_filename']);
415  }
416  }
417 
418  public function deliverZipFile()
419  {
423  global $lng, $ilUser;
424 
425  $zip_file = $this->createZipFile();
426  if(!$zip_file)
427  {
428  ilUtil::sendFailure($lng->txt('error_reading_file'), true);
429  return false;
430  }
431  else
432  {
433  $post = ilForumPostDraft::newInstanceByDraftId($this->getDraftId());
434  ilUtil::deliverFile($zip_file, $post->getPostSubject() . '.zip', '', false, true, false);
435  ilUtil::delDir($this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId());
436  exit();
437  }
438  }
439 
443  public function createZipFile()
444  {
445  $filesOfDraft = $this->getFilesOfPost();
446  if(count($filesOfDraft))
447  {
448  ksort($filesOfDraft);
449 
450  ilUtil::makeDirParents($this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId());
451  $tmp_dir = $this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId();
452  foreach($filesOfDraft as $file)
453  {
454  @copy($file['path'], $tmp_dir . '/' . $file['name']);
455  }
456  }
457 
458  $zip_file = null;
459  if(ilUtil::zip($tmp_dir, $this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId() . '.zip'))
460  {
461  $zip_file = $this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId() . '.zip';
462  }
463 
464  return $zip_file;
465  }
466 }
$files
Definition: add-vimline.php:18
static makeDirParents($a_dir)
Create a new directory and all parent directories.
__checkReadWrite()
check if directory is writable overwritten method from base class private
$error
Definition: Error.php:17
__construct($obj_id=0, $draft_id)
storeUploadedFile($files)
Store uploaded files in filesystem.
getPath()
get Path public
moveFilesOfDraft($forum_path, $new_post_id)
static getDir($a_dir, $a_rec=false, $a_sub_dir="")
get directory
checkFilesExist($a_files)
check if files exist
This class handles all operations on files for the drafts of a forum object.
unlinkFiles($a_filenames)
unlink files: expects an array of filenames e.g.
unlinkFilesByMD5Filenames($a_md5_filename)
get file data of a specific attachment
static _sanitizeFilemame($a_filename)
getAbsolutePath($a_path)
get absolute path of 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())
unlinkFile($a_filename)
unlink one uploaded file expects a filename e.g &#39;foo&#39;
$ilUser
Definition: imgupload.php:18
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
getFileDataByMD5Filename($a_md5_filename)
get file data of a specific attachment
Create styles array
The data for the language used.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
global $lng
Definition: privfeed.php:17
__initDirectory()
init directory overwritten method public
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
__rotateFiles($a_path)
rotate files with same name recursive method