ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
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");
5
15{
23
30
38 public function __construct($a_obj_id = 0,$a_pos_id = 0)
39 {
40 define('FORUM_PATH', 'forum');
41 parent::__construct();
42 $this->forum_path = parent::getPath()."/".FORUM_PATH;
43
44 // IF DIRECTORY ISN'T CREATED CREATE IT
45 if(!$this->__checkPath())
46 {
47 $this->__initDirectory();
48 }
49 $this->obj_id = $a_obj_id;
50 $this->pos_id = $a_pos_id;
51 }
52
53 function getObjId()
54 {
55 return $this->obj_id;
56 }
57 function getPosId()
58 {
59 return $this->pos_id;
60 }
61 function setPosId($a_id)
62 {
63 $this->pos_id = $a_id;
64 }
70 function getForumPath()
71 {
72 return $this->forum_path;
73 }
74
78 public function getFiles()
79 {
80 $files = array();
81
82 foreach(new DirectoryIterator($this->forum_path) as $file)
83 {
88 if($file->isDir())
89 {
90 continue;
91 }
92
93 list($obj_id, $rest) = explode('_', $file->getFilename(), 2);
94 if($obj_id == $this->obj_id)
95 {
96 $files[] = array(
97 'path' => $file->getPathname(),
98 'md5' => md5($this->obj_id . '_' . $this->pos_id . '_' . $rest),
99 'name' => $rest,
100 'size' => $file->getSize(),
101 'ctime' => ilFormat::formatDate(date('Y-m-d H:i:s', $file->getCTime()))
102 );
103 }
104 }
105
106 return $files;
107 }
108
112 public function getFilesOfPost()
113 {
114 $files = array();
115
116 foreach(new DirectoryIterator($this->forum_path) as $file)
117 {
122 if($file->isDir())
123 {
124 continue;
125 }
126
127 list($obj_id, $rest) = explode('_', $file->getFilename(), 2);
128 if($obj_id == $this->obj_id)
129 {
130 list($pos_id, $rest) = explode('_', $rest, 2);
131 if($pos_id == $this->getPosId())
132 {
133 $files[] = array(
134 'path' => $file->getPathname(),
135 'md5' => md5($this->obj_id . '_' . $this->pos_id . '_' . $rest),
136 'name' => $rest,
137 'size' => $file->getSize(),
138 'ctime' => ilFormat::formatDate(date('Y-m-d H:i:s', $file->getCTime()))
139 );
140 }
141 }
142 }
143
144 return $files;
145 }
146
151 public function moveFilesOfPost($a_new_frm_id = 0)
152 {
153 if((int)$a_new_frm_id)
154 {
155 foreach(new DirectoryIterator($this->forum_path) as $file)
156 {
161 if($file->isDir())
162 {
163 continue;
164 }
165
166 list($obj_id, $rest) = explode('_', $file->getFilename(), 2);
167 if($obj_id == $this->obj_id)
168 {
169 list($pos_id, $rest) = explode('_', $rest, 2);
170 if($pos_id == $this->getPosId())
171 {
172 @rename($file->getPathname(), $this->forum_path . '/' .$a_new_frm_id . '_' . $this->pos_id . '_' . $rest);
173 }
174 }
175 }
176
177 return true;
178 }
179
180 return false;
181 }
182
183 function ilClone($a_new_obj_id,$a_new_pos_id)
184 {
185 foreach($this->getFilesOfPost() as $file)
186 {
187 @copy($this->getForumPath()."/".$this->obj_id."_".$this->pos_id."_".$file["name"],
188 $this->getForumPath()."/".$a_new_obj_id."_".$a_new_pos_id."_".$file["name"]);
189 }
190 return true;
191 }
192 function delete()
193 {
194 foreach($this->getFiles() as $file)
195 {
196 if(file_exists($this->getForumPath()."/".$this->getObjId()."_".$file["name"]))
197 {
198 unlink($this->getForumPath()."/".$this->getObjId()."_".$file["name"]);
199 }
200 }
201 return true;
202 }
203
213 function storeUploadedFile($files)
214 {
215 if(isset($files['name']) && is_array($files['name']))
216 {
217 foreach($files['name'] as $index => $name)
218 {
219 // remove trailing '/'
220 while(substr($name, -1) == '/')
221 {
222 $name = substr($name, 0, -1);
223 }
225 $temp_name = $files['tmp_name'][$index];
226 $error = $files['error'][$index];
227
228 if(strlen($filename) && strlen($temp_name) && $error == 0)
229 {
230 $path = $this->getForumPath().'/'.$this->obj_id.'_'.$this->pos_id.'_'.$filename;
231
232 $this->__rotateFiles($path);
234 }
235 }
236
237 return true;
238 }
239 else if(isset($files['name']) && is_string($files['name']))
240 {
241 // remove trailing '/'
242 while(substr($files['name'], -1) == '/')
243 {
244 $files['name'] = substr($files['name'], 0, -1);
245 }
246 $filename = ilUtil::_sanitizeFilemame($files['name']);
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 }
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 }
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 rename($a_path,$a_path.'.old');
446 }
447 return true;
448 }
449
450}
print $file
$filename
Definition: buildRTE.php:89
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
formatDate($a_date, $a_mode="datetime", $a_omit_seconds=false, $a_relative=TRUE)
format a date according to the user language shortcut for Format::fmtDateTime @access public
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
static _sanitizeFilemame($a_filename)
static getDir($a_dir, $a_rec=false, $a_sub_dir="")
get directory
$rest
Definition: goto.php:85
redirection script todo: (a better solution should control the processing via a xml file)