ILIAS  release_7 Revision v7.30-3-g800a261c036
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
12{
13 protected $obj_id = 0;
14 protected $draft_id = 0;
15 protected $drafts_path = '';
16 private $lng;
17 private $error;
18
19 public function __construct($obj_id = 0, $draft_id)
20 {
21 global $DIC;
22 $this->lng = $DIC->language();
23 $this->error = $DIC['ilErr'];
24
25 $this->obj_id = $obj_id;
26 $this->draft_id = $draft_id;
27
28 define('FORUM_DRAFTS_PATH', 'forum/drafts');
30 $this->drafts_path = parent::getPath() . "/" . FORUM_DRAFTS_PATH;
31
32 // IF DIRECTORY ISN'T CREATED CREATE IT
33 if (!$this->__checkPath()) {
34 $this->__initDirectory();
35 }
36 }
37
41 public function getObjId()
42 {
43 return $this->obj_id;
44 }
45
49 public function setObjId($obj_id)
50 {
51 $this->obj_id = $obj_id;
52 }
53
57 public function getDraftId()
58 {
59 return $this->draft_id;
60 }
61
65 public function setDraftId($draft_id)
66 {
67 $this->draft_id = $draft_id;
68 }
69
73 public function getDraftsPath()
74 {
75 return $this->drafts_path;
76 }
77
78
82 public function getFiles()
83 {
84 $files = array();
85
86 foreach (new DirectoryIterator($this->getDraftsPath() . '/' . $this->getDraftId()) as $file) {
91 if ($file->isDir()) {
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) {
119 if ($file->isDir()) {
120 continue;
121 }
122
123 $files[$file->getFilename()] = array(
124 'path' => $file->getPathname(),
125 'md5' => md5($file->getFilename()),
126 'name' => $file->getFilename(),
127 'size' => $file->getSize(),
128 'ctime' => date('Y-m-d H:i:s', $file->getCTime())
129 );
130 }
131
132 return $files;
133 }
134
135 public function moveFilesOfDraft($forum_path, $new_post_id)
136 {
137 foreach ($this->getFilesOfPost() as $file) {
138 @copy(
139 $file['path'],
140 $forum_path . '/' . $this->obj_id . '_' . $new_post_id . '_' . $file['name']
141 );
142 }
143 return true;
144 }
145
149 public function delete()
150 {
151 ilUtil::delDir($this->getDraftsPath() . '/' . $this->getDraftId());
152 return true;
153 }
154
164 public function storeUploadedFile($files)
165 {
166 if (isset($files['name']) && is_array($files['name'])) {
167 foreach ($files['name'] as $index => $name) {
168 // remove trailing '/'
169 while (substr($name, -1) == '/') {
170 $name = substr($name, 0, -1);
171 }
173 $temp_name = $files['tmp_name'][$index];
174 $error = $files['error'][$index];
175
176 if (strlen($filename) && strlen($temp_name) && $error == 0) {
177 $path = $this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $filename;
178
179 $this->__rotateFiles($path);
181 }
182 }
183
184 return true;
185 } elseif (isset($files['name']) && is_string($files['name'])) {
186 // remove trailing '/'
187 while (substr($files['name'], -1) == '/') {
188 $files['name'] = substr($files['name'], 0, -1);
189 }
190 $filename = ilUtil::_sanitizeFilemame($files['name']);
191 $temp_name = $files['tmp_name'];
192
193 $path = $this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $filename;
194
195 $this->__rotateFiles($path);
197
198 return true;
199 }
200
201 return false;
202 }
209 public function unlinkFiles($a_filenames)
210 {
211 if (is_array($a_filenames)) {
212 foreach ($a_filenames as $file) {
213 if (!$this->unlinkFile($file)) {
214 return $file;
215 }
216 }
217 }
218 return '';
219 }
226 public function unlinkFile($a_filename)
227 {
228 if (file_exists($this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $a_filename)) {
229 return unlink($this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $a_filename);
230 }
231 }
238 public function getAbsolutePath($a_path)
239 {
240 return $this->getDraftsPath() . '/' . $this->getDraftId();
241 }
242
249 public function getFileDataByMD5Filename($a_md5_filename)
250 {
251 $files = ilUtil::getDir($this->getDraftsPath() . '/' . $this->getDraftId());
252 foreach ((array) $files as $file) {
253 if ($file['type'] == 'file' && md5($file['entry']) == $a_md5_filename) {
254 return array(
255 'path' => $this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $file['entry'],
256 'filename' => $file['entry'],
257 'clean_filename' => $file['entry']
258 );
259 }
260 }
261
262 return false;
263 }
264
271 public function unlinkFilesByMD5Filenames($a_md5_filename)
272 {
273 $files = ilUtil::getDir($this->getDraftsPath() . '/' . $this->getDraftId());
274 if (is_array($a_md5_filename)) {
275 foreach ((array) $files as $file) {
276 if ($file['type'] == 'file' && in_array(md5($file['entry']), $a_md5_filename)) {
277 unlink($this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $file['entry']);
278 }
279 }
280
281 return true;
282 } else {
283 foreach ((array) $files as $file) {
284 if ($file['type'] == 'file' && md5($file['entry']) == $a_md5_filename) {
285 return unlink($this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $file['entry']);
286 }
287 }
288 }
289
290 return false;
291 }
292
299 public function checkFilesExist($a_files)
300 {
301 if ($a_files) {
302 foreach ($a_files as $file) {
303 if (!file_exists($this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $file)) {
304 return false;
305 }
306 }
307 return true;
308 }
309 return true;
310 }
311
312 // PRIVATE METHODS
313 public function __checkPath()
314 {
315 if (!@file_exists($this->getDraftsPath() . '/' . $this->getDraftId())) {
316 return false;
317 }
318 $this->__checkReadWrite();
319
320 return true;
321 }
328 public function __checkReadWrite()
329 {
330 if (is_writable($this->getDraftsPath() . '/' . $this->getDraftId()) && is_readable($this->getDraftsPath() . '/' . $this->getDraftId())) {
331 return true;
332 } else {
333 $this->error->raiseError("Forum directory is not readable/writable by webserver", $this->error->FATAL);
334 }
335 }
342 public function __initDirectory()
343 {
344 if (is_writable($this->getPath())) {
345 if (ilUtil::makeDirParents($this->getDraftsPath() . "/" . $this->getDraftId())) {
346 if (chmod($this->getDraftsPath() . "/" . $this->getDraftId(), 0755)) {
347 return true;
348 }
349 }
350 }
351 return false;
352 }
360 public function __rotateFiles($a_path)
361 {
362 if (file_exists($a_path)) {
363 $this->__rotateFiles($a_path . ".old");
364 return \ilFileUtils::rename($a_path, $a_path . '.old');
365 }
366 return true;
367 }
368
373 public function deliverFile($file)
374 {
375 if (!$path = $this->getFileDataByMD5Filename($file)) {
376 return ilUtil::sendFailure($this->lng->txt('error_reading_file'), true);
377 } else {
378 return ilUtil::deliverFile($path['path'], $path['clean_filename']);
379 }
380 }
381
382 public function deliverZipFile()
383 {
384 $zip_file = $this->createZipFile();
385 if (!$zip_file) {
386 ilUtil::sendFailure($this->lng->txt('error_reading_file'), true);
387 return false;
388 } else {
390 ilUtil::deliverFile($zip_file, $post->getPostSubject() . '.zip', '', false, true, false);
391 ilUtil::delDir($this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId());
392 exit();
393 }
394 }
395
399 public function createZipFile()
400 {
401 $filesOfDraft = $this->getFilesOfPost();
402 if (count($filesOfDraft)) {
403 ksort($filesOfDraft);
404
405 ilUtil::makeDirParents($this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId());
406 $tmp_dir = $this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId();
407 foreach ($filesOfDraft as $file) {
408 @copy($file['path'], $tmp_dir . '/' . $file['name']);
409 }
410 }
411
412 $zip_file = null;
413 if (ilUtil::zip($tmp_dir, $this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId() . '.zip')) {
414 $zip_file = $this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId() . '.zip';
415 }
416
417 return $zip_file;
418 }
419}
$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 drafts of a forum object.
checkFilesExist($a_files)
check if files exist
__checkReadWrite()
check if directory is writable overwritten method from base class @access private
unlinkFile($a_filename)
unlink one uploaded file expects a filename e.g 'foo'
getAbsolutePath($a_path)
get absolute path of filename
storeUploadedFile($files)
Store uploaded files in filesystem.
unlinkFilesByMD5Filenames($a_md5_filename)
get file data of a specific attachment
getFileDataByMD5Filename($a_md5_filename)
get file data of a specific attachment
__rotateFiles($a_path)
rotate files with same name recursive method
unlinkFiles($a_filenames)
unlink files: expects an array of filenames e.g.
__initDirectory()
init directory overwritten method @access public
moveFilesOfDraft($forum_path, $new_post_id)
__construct($obj_id=0, $draft_id)
This class handles all operations on files in directory /ilias_data/.
getPath()
get Path @access public
static newInstanceByDraftId($draft_id)
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded 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.
global $DIC
Definition: goto.php:24
exit
Definition: login.php:29
if($format !==null) $name
Definition: metadata.php:230
$index
Definition: metadata.php:128
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc