ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  private $lng;
20  private $error;
21 
22  public function __construct($obj_id = 0, $draft_id)
23  {
24  global $DIC;
25  $this->lng = $DIC->language();
26  $this->error = $DIC['ilErr'];
27 
28  $this->obj_id = $obj_id;
29  $this->draft_id = $draft_id;
30 
31  define('FORUM_DRAFTS_PATH', 'forum/drafts');
32  parent::__construct();
33  $this->drafts_path = parent::getPath() . "/" . FORUM_DRAFTS_PATH;
34 
35  // IF DIRECTORY ISN'T CREATED CREATE IT
36  if (!$this->__checkPath()) {
37  $this->__initDirectory();
38  }
39  }
40 
44  public function getObjId()
45  {
46  return $this->obj_id;
47  }
48 
52  public function setObjId($obj_id)
53  {
54  $this->obj_id = $obj_id;
55  }
56 
60  public function getDraftId()
61  {
62  return $this->draft_id;
63  }
64 
68  public function setDraftId($draft_id)
69  {
70  $this->draft_id = $draft_id;
71  }
72 
76  public function getDraftsPath()
77  {
78  return $this->drafts_path;
79  }
80 
81 
85  public function getFiles()
86  {
87  $files = array();
88 
89  foreach (new DirectoryIterator($this->getDraftsPath() . '/' . $this->getDraftId()) as $file) {
94  if ($file->isDir()) {
95  continue;
96  }
97 
98  $files[] = array(
99  'path' => $file->getPathname(),
100  'md5' => md5($file->getFilename()),
101  'name' => $file->getFilename(),
102  'size' => $file->getSize(),
103  'ctime' => date('Y-m-d H:i:s', $file->getCTime())
104  );
105  }
106 
107  return $files;
108  }
109 
113  public function getFilesOfPost()
114  {
115  $files = array();
116 
117  foreach (new DirectoryIterator($this->getDraftsPath() . '/' . $this->getDraftId()) as $file) {
122  if ($file->isDir()) {
123  continue;
124  }
125 
126  $files[$file->getFilename()] = array(
127  'path' => $file->getPathname(),
128  'md5' => md5($file->getFilename()),
129  'name' => $file->getFilename(),
130  'size' => $file->getSize(),
131  'ctime' => date('Y-m-d H:i:s', $file->getCTime())
132  );
133  }
134 
135  return $files;
136  }
137 
138  public function moveFilesOfDraft($forum_path, $new_post_id)
139  {
140  foreach ($this->getFilesOfPost() as $file) {
141  @copy(
142  $file['path'],
143  $forum_path . '/' . $this->obj_id . '_' . $new_post_id . '_' . $file['name']
144  );
145  }
146  return true;
147  }
148 
152  public function delete()
153  {
154  ilUtil::delDir($this->getDraftsPath() . '/' . $this->getDraftId());
155  return true;
156  }
157 
167  public function storeUploadedFile($files)
168  {
169  if (isset($files['name']) && is_array($files['name'])) {
170  foreach ($files['name'] as $index => $name) {
171  // remove trailing '/'
172  while (substr($name, -1) == '/') {
173  $name = substr($name, 0, -1);
174  }
176  $temp_name = $files['tmp_name'][$index];
177  $error = $files['error'][$index];
178 
179  if (strlen($filename) && strlen($temp_name) && $error == 0) {
180  $path = $this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $filename;
181 
182  $this->__rotateFiles($path);
183  ilUtil::moveUploadedFile($temp_name, $filename, $path);
184  }
185  }
186 
187  return true;
188  } elseif (isset($files['name']) && is_string($files['name'])) {
189  // remove trailing '/'
190  while (substr($files['name'], -1) == '/') {
191  $files['name'] = substr($files['name'], 0, -1);
192  }
194  $temp_name = $files['tmp_name'];
195 
196  $path = $this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $filename;
197 
198  $this->__rotateFiles($path);
199  ilUtil::moveUploadedFile($temp_name, $filename, $path);
200 
201  return true;
202  }
203 
204  return false;
205  }
212  public function unlinkFiles($a_filenames)
213  {
214  if (is_array($a_filenames)) {
215  foreach ($a_filenames as $file) {
216  if (!$this->unlinkFile($file)) {
217  return $file;
218  }
219  }
220  }
221  return '';
222  }
229  public function unlinkFile($a_filename)
230  {
231  if (file_exists($this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $a_filename)) {
232  return unlink($this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $a_filename);
233  }
234  }
241  public function getAbsolutePath($a_path)
242  {
243  return $this->getDraftsPath() . '/' . $this->getDraftId();
244  }
245 
252  public function getFileDataByMD5Filename($a_md5_filename)
253  {
254  $files = ilUtil::getDir($this->getDraftsPath() . '/' . $this->getDraftId());
255  foreach ((array) $files as $file) {
256  if ($file['type'] == 'file' && md5($file['entry']) == $a_md5_filename) {
257  return array(
258  'path' => $this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $file['entry'],
259  'filename' => $file['entry'],
260  'clean_filename' => $file['entry']
261  );
262  }
263  }
264 
265  return false;
266  }
267 
274  public function unlinkFilesByMD5Filenames($a_md5_filename)
275  {
276  $files = ilUtil::getDir($this->getDraftsPath() . '/' . $this->getDraftId());
277  if (is_array($a_md5_filename)) {
278  foreach ((array) $files as $file) {
279  if ($file['type'] == 'file' && in_array(md5($file['entry']), $a_md5_filename)) {
280  unlink($this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $file['entry']);
281  }
282  }
283 
284  return true;
285  } else {
286  foreach ((array) $files as $file) {
287  if ($file['type'] == 'file' && md5($file['entry']) == $a_md5_filename) {
288  return unlink($this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $file['entry']);
289  }
290  }
291  }
292 
293  return false;
294  }
295 
302  public function checkFilesExist($a_files)
303  {
304  if ($a_files) {
305  foreach ($a_files as $file) {
306  if (!file_exists($this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $file)) {
307  return false;
308  }
309  }
310  return true;
311  }
312  return true;
313  }
314 
315  // PRIVATE METHODS
316  public function __checkPath()
317  {
318  if (!@file_exists($this->getDraftsPath() . '/' . $this->getDraftId())) {
319  return false;
320  }
321  $this->__checkReadWrite();
322 
323  return true;
324  }
331  public function __checkReadWrite()
332  {
333  if (is_writable($this->getDraftsPath() . '/' . $this->getDraftId()) && is_readable($this->getDraftsPath() . '/' . $this->getDraftId())) {
334  return true;
335  } else {
336  $this->error->raiseError("Forum directory is not readable/writable by webserver", $this->error->FATAL);
337  }
338  }
345  public function __initDirectory()
346  {
347  if (is_writable($this->getPath())) {
348  if (ilUtil::makeDirParents($this->getDraftsPath() . "/" . $this->getDraftId())) {
349  if (chmod($this->getDraftsPath() . "/" . $this->getDraftId(), 0755)) {
350  return true;
351  }
352  }
353  }
354  return false;
355  }
363  public function __rotateFiles($a_path)
364  {
365  if (file_exists($a_path)) {
366  $this->__rotateFiles($a_path . ".old");
367  return \ilFileUtils::rename($a_path, $a_path . '.old');
368  }
369  return true;
370  }
371 
376  public function deliverFile($file)
377  {
378  if (!$path = $this->getFileDataByMD5Filename($file)) {
379  return ilUtil::sendFailure($this->lng->txt('error_reading_file'), true);
380  } else {
381  return ilUtil::deliverFile($path['path'], $path['clean_filename']);
382  }
383  }
384 
385  public function deliverZipFile()
386  {
387  $zip_file = $this->createZipFile();
388  if (!$zip_file) {
389  ilUtil::sendFailure($this->lng->txt('error_reading_file'), true);
390  return false;
391  } else {
393  ilUtil::deliverFile($zip_file, $post->getPostSubject() . '.zip', '', false, true, false);
394  ilUtil::delDir($this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId());
395  exit();
396  }
397  }
398 
402  public function createZipFile()
403  {
404  $filesOfDraft = $this->getFilesOfPost();
405  if (count($filesOfDraft)) {
406  ksort($filesOfDraft);
407 
408  ilUtil::makeDirParents($this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId());
409  $tmp_dir = $this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId();
410  foreach ($filesOfDraft as $file) {
411  @copy($file['path'], $tmp_dir . '/' . $file['name']);
412  }
413  }
414 
415  $zip_file = null;
416  if (ilUtil::zip($tmp_dir, $this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId() . '.zip')) {
417  $zip_file = $this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId() . '.zip';
418  }
419 
420  return $zip_file;
421  }
422 }
$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
__construct($obj_id=0, $draft_id)
storeUploadedFile($files)
Store uploaded files in filesystem.
global $DIC
Definition: saml.php:7
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
$index
Definition: metadata.php:60
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)
if($format !==null) $name
Definition: metadata.php:146
getAbsolutePath($a_path)
get absolute path of filename
This class handles all operations on files in directory /ilias_data/.
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
unlinkFile($a_filename)
unlink one uploaded file expects a filename e.g &#39;foo&#39;
static newInstanceByDraftId($draft_id)
$post
Definition: post.php:34
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.
__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
static deliverFile( $a_file, $a_filename, $a_mime='', $isInline=false, $removeAfterDelivery=false, $a_exit_after=true)
deliver file for download via browser.