ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilFileDataExercise.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once("./Services/FileSystem/classes/class.ilFileData.php");
5 
15 {
21  var $obj_id;
22 
29 
37  function ilFileDataExercise($a_obj_id = 0)
38  {
39 die ("ilFileDataExercise is deprecated.");
40  define('EXERCISE_PATH','exercise');
42  $this->exercise_path = parent::getPath()."/".EXERCISE_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  }
51 
52  function getObjId()
53  {
54  return $this->obj_id;
55  }
56 
62  function getExercisePath()
63  {
64  return $this->exercise_path;
65  }
66 
67  function getFiles()
68  {
69  $files = array();
70  $dp = opendir($this->exercise_path);
71 
72  while($file = readdir($dp))
73  {
74  if(is_dir($file))
75  {
76  continue;
77  }
78  list($obj_id,$rest) = split('_',$file,2);
79  if($obj_id == $this->obj_id)
80  {
81  if(!is_dir($this->exercise_path.'/'.$file))
82  {
83  $files[] = array(
84  'name' => $rest,
85  'size' => filesize($this->exercise_path.'/'.$file),
86  'ctime' => ilFormat::formatDate(date('Y-m-d H:i:s',filectime($this->exercise_path.'/'.$file))),
87  'fullpath' => $this->exercise_path.'/'.$file);
88  }
89  }
90  }
91  closedir($dp);
92  return $files;
93  }
94 
95  function ilClone($a_new_obj_id)
96  {
97  foreach($this->getFiles() as $file)
98  {
99  @copy($this->getExercisePath()."/".$this->obj_id.'_'.$file["name"],
100  $this->getExercisePath()."/".$a_new_obj_id.'_'.$file["name"]);
101  }
102  return true;
103  }
104  function delete()
105  {
106  foreach($this->getFiles() as $file)
107  {
108  $this->unlinkFile($file["name"]);
109  }
110 
111  $delivered_file_path = $this->getExercisePath() . "/" . $this->obj_id . "/";
112  if (is_dir($delivered_file_path))
113  {
114  include_once "./Services/Utilities/classes/class.ilUtil.php";
115  ilUtil::delDir($delivered_file_path);
116  }
117 
118  return true;
119  }
120 
128  function storeUploadedFile($a_http_post_file, $secure_filename = false, $is_unziped = false)
129  {
130  // TODO:
131  // CHECK UPLOAD LIMIT
132  //
133  $filename = $a_http_post_file['name'];
134  if ($secure_filename)
135  {
136  // replace whitespaces with underscores
137  $filename = preg_replace("/\s/", "_", $filename);
138  // remove all special characters
139  $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
140  }
141  if(isset($a_http_post_file) && $a_http_post_file['size'])
142  {
143  // CHECK IF FILE WITH SAME NAME EXISTS
144  $this->__rotateFiles($this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
145  //move_uploaded_file($a_http_post_file['tmp_name'],$this->getExercisePath().'/'.$this->obj_id.'_'.
146  // $filename);
147 
148  if (!$is_unziped)
149  {
150  ilUtil::moveUploadedFile($a_http_post_file['tmp_name'], $a_http_post_file['name'],
151  $this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
152  }
153  else
154  {
155  // ######## Warning, there is no check whether the unziped files are virus free or not
156  rename($a_http_post_file['tmp_name'],
157  $this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
158  }
159 
160  }
161  return true;
162  }
163 
164 
172  function storeContentAsFile($filename, $content, $secure_filename = false)
173  {
174  // TODO:
175  // CHECK UPLOAD LIMIT
176  //
177  if ($secure_filename)
178  {
179  // replace whitespaces with underscores
180  $filename = preg_replace("/\s/", "_", $filename);
181  // remove all special characters
182  $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
183  }
184  if(count($content) > 0 )
185  {
186  // CHECK IF FILE WITH SAME NAME EXISTS
188  $this->__rotateFiles($filename);
189  file_put_contents($filename, $content);
190 
191  // check for virus
193  if (!$vir[0] ||$vir[1] != "")
194  {
195  unlink($filename);
196  return false;
197  }
198  return true;
199  }
200  return false;
201  }
202 
203 
209  function downloadAllDeliveredFiles($members)
210  {
211  require_once "./Services/Utilities/classes/class.ilUtil.php";
212  global $lng, $ilObjDataCache;
213 
214  ksort($members);
215  $savepath = $this->getExercisePath() . "/" . $this->obj_id . "/";
216  $cdir = getcwd();
217 
218 
219  // important check: if the directory does not exist
220  // ILIAS stays in the current directory (echoing only a warning)
221  // and the zip command below archives the whole ILIAS directory
222  // (including the data directory) and sends a mega file to the user :-o
223  if (!is_dir($savepath))
224  {
225  return;
226  }
227  // Safe mode fix
228  chdir($this->getExercisePath());
229  $zip = PATH_TO_ZIP;
230 
231  // check first, if we have enough free disk space to copy all files to temporary directory
232  $tmpdir = ilUtil::ilTempnam();
233  ilUtil::makeDir($tmpdir);
234  chdir($tmpdir);
235 
236 
237  $dirsize = 0;
238  foreach ($members as $id => $object) {
239  $directory = $savepath.DIRECTORY_SEPARATOR.$id;
240  $dirsize += ilUtil::dirsize($directory);
241  }
242  if ($dirsize > disk_free_space($tmpdir)) {
243  return -1;
244  }
245 
246  // copy all member directories to the temporary folder
247  // switch from id to member name and append the login if the member name is double
248  // ensure that no illegal filenames will be created
249  // remove timestamp from filename
250  $cache = array();
251  foreach ($members as $id => $user)
252  {
253  $sourcedir = $savepath.DIRECTORY_SEPARATOR.$id;
254  if (!is_dir($sourcedir))
255  continue;
256  $userName = ilObjUser::_lookupName($id);
257  $directory = ilUtil::getASCIIFilename(trim($userName["lastname"])."_".
258  trim($userName["firstname"])."_".trim($userName["login"])."_".$userName["user_id"]);
259  /*if (array_key_exists($directory, $cache))
260  {
261  // first try is to append the login;
262  $directory = ilUtil::getASCIIFilename($directory."_".trim(ilObjUser::_lookupLogin($id)));
263  if (array_key_exists($directory, $cache)) {
264  // second and secure: append the user id as well.
265  $directory .= "_".$id;
266  }
267  }*/
268 
269  $cache[$directory] = $directory;
270  ilUtil::makeDir ($directory);
271  $sourcefiles = scandir($sourcedir);
272  foreach ($sourcefiles as $sourcefile) {
273  if ($sourcefile == "." || $sourcefile == "..")
274  continue;
275  $targetfile = trim(basename($sourcefile));
276  $pos = strpos($targetfile, "_");
277  if ($pos === false)
278  {
279  } else
280  {
281  $targetfile= substr($targetfile, $pos + 1);
282  }
283  $targetfile = $directory.DIRECTORY_SEPARATOR.$targetfile;
284  $sourcefile = $sourcedir.DIRECTORY_SEPARATOR.$sourcefile;
285 
286  if (!copy ($sourcefile, $targetfile))
287  {
288  //echo 'Could not copy '.$sourcefile.' to '.$targetfile;
289  $this->ilias->raiseError('Could not copy '.basename($sourcefile)." to '".$targetfile."'.",
290  $this->ilias->error_obj->MESSAGE);
291  }
292  else
293  {
294  // preserve time stamp
295  touch($targetfile, filectime($sourcefile));
296  }
297 
298  }
299  }
300 
301  $tmpfile = ilUtil::ilTempnam();
302  $tmpzipfile = $tmpfile . ".zip";
303  // Safe mode fix
304  $zipcmd = $zip." -r ".ilUtil::escapeShellArg($tmpzipfile)." .";
305  exec($zipcmd);
306  ilUtil::delDir($tmpdir);
307 
308  $exerciseTitle = $ilObjDataCache->lookupTitle($this->getObjId());
309  ilUtil::deliverFile($tmpzipfile, (strlen($exerciseTitle) == 0? strtolower($lng->txt("excs")) : $exerciseTitle). ".zip");
310  chdir($cdir);
311  unlink($tmpfile);
312  unlink($tmpzipfile);
313  }
314 
315 
322  function unlinkFiles($a_filenames)
323  {
324  if(is_array($a_filenames))
325  {
326  foreach($a_filenames as $file)
327  {
328  if(!$this->unlinkFile($file))
329  {
330  return $file;
331  }
332  }
333  }
334  return '';
335  }
342  function unlinkFile($a_filename)
343  {
344  if(file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename))
345  {
346  return unlink($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename);
347  }
348  }
355  function getAbsolutePath($a_path)
356  {
357  return $this->exercise_path.'/'.$this->obj_id.'_'.$a_path;
358  }
359 
366  function checkFilesExist($a_files)
367  {
368  if($a_files)
369  {
370  foreach($a_files as $file)
371  {
372  if(!file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$file))
373  {
374  return false;
375  }
376  }
377  return true;
378  }
379  return true;
380  }
381 
382  // PRIVATE METHODS
383  function __checkPath()
384  {
385  if(!@file_exists($this->getExercisePath()))
386  {
387  return false;
388  }
389  $this->__checkReadWrite();
390 
391  return true;
392  }
399  function __checkReadWrite()
400  {
401  if(is_writable($this->exercise_path) && is_readable($this->exercise_path))
402  {
403  return true;
404  }
405  else
406  {
407  $this->ilias->raiseError("Exercise directory is not readable/writable by webserver",$this->ilias->error_obj->FATAL);
408  }
409  }
416  function __initDirectory()
417  {
418  if(is_writable($this->getPath()))
419  {
420  return ilUtil::makeDir($this->excercise_path = $this->getPath().'/'.EXERCISE_PATH);
421  }
422  return false;
423  }
431  function __rotateFiles($a_path)
432  {
433  if(file_exists($a_path))
434  {
435  $this->__rotateFiles($a_path.".old");
436  return rename($a_path,$a_path.'.old');
437  }
438  return true;
439  }
440 }