ILIAS  Release_4_0_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("classes/class.ilFileData.php");
5 
15 {
21  var $obj_id;
22 
29 
37  function ilFileDataExercise($a_obj_id = 0)
38  {
39  define('EXERCISE_PATH','exercise');
41  $this->exercise_path = parent::getPath()."/".EXERCISE_PATH;
42 
43  // IF DIRECTORY ISN'T CREATED CREATE IT
44  if(!$this->__checkPath())
45  {
46  $this->__initDirectory();
47  }
48  $this->obj_id = $a_obj_id;
49  }
50 
51  function getObjId()
52  {
53  return $this->obj_id;
54  }
55 
61  function getExercisePath()
62  {
63  return $this->exercise_path;
64  }
65 
66  function getFiles()
67  {
68  $files = array();
69  $dp = opendir($this->exercise_path);
70 
71  while($file = readdir($dp))
72  {
73  if(is_dir($file))
74  {
75  continue;
76  }
77  list($obj_id,$rest) = split('_',$file,2);
78  if($obj_id == $this->obj_id)
79  {
80  if(!is_dir($this->exercise_path.'/'.$file))
81  {
82  $files[] = array(
83  'name' => $rest,
84  'size' => filesize($this->exercise_path.'/'.$file),
85  'ctime' => ilFormat::formatDate(date('Y-m-d H:i:s',filectime($this->exercise_path.'/'.$file))),
86  'fullpath' => $this->exercise_path.'/'.$file);
87  }
88  }
89  }
90  closedir($dp);
91  return $files;
92  }
93 
94  function ilClone($a_new_obj_id)
95  {
96  foreach($this->getFiles() as $file)
97  {
98  @copy($this->getExercisePath()."/".$this->obj_id.'_'.$file["name"],
99  $this->getExercisePath()."/".$a_new_obj_id.'_'.$file["name"]);
100  }
101  return true;
102  }
103  function delete()
104  {
105  foreach($this->getFiles() as $file)
106  {
107  $this->unlinkFile($file["name"]);
108  }
109 
110  $delivered_file_path = $this->getExercisePath() . "/" . $this->obj_id . "/";
111  if (is_dir($delivered_file_path))
112  {
113  include_once "./Services/Utilities/classes/class.ilUtil.php";
114  ilUtil::delDir($delivered_file_path);
115  }
116 
117  return true;
118  }
119 
127  function storeUploadedFile($a_http_post_file, $secure_filename = false, $is_unziped = false)
128  {
129  // TODO:
130  // CHECK UPLOAD LIMIT
131  //
132  $filename = $a_http_post_file['name'];
133  if ($secure_filename)
134  {
135  // replace whitespaces with underscores
136  $filename = preg_replace("/\s/", "_", $filename);
137  // remove all special characters
138  $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
139  }
140  if(isset($a_http_post_file) && $a_http_post_file['size'])
141  {
142  // CHECK IF FILE WITH SAME NAME EXISTS
143  $this->__rotateFiles($this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
144  //move_uploaded_file($a_http_post_file['tmp_name'],$this->getExercisePath().'/'.$this->obj_id.'_'.
145  // $filename);
146 
147  if (!$is_unziped)
148  {
149  ilUtil::moveUploadedFile($a_http_post_file['tmp_name'], $a_http_post_file['name'],
150  $this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
151  }
152  else
153  {
154  // ######## Warning, there is no check whether the unziped files are virus free or not
155  rename($a_http_post_file['tmp_name'],
156  $this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
157  }
158 
159  }
160  return true;
161  }
162 
163 
171  function storeContentAsFile($filename, $content, $secure_filename = false)
172  {
173  // TODO:
174  // CHECK UPLOAD LIMIT
175  //
176  if ($secure_filename)
177  {
178  // replace whitespaces with underscores
179  $filename = preg_replace("/\s/", "_", $filename);
180  // remove all special characters
181  $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
182  }
183  if(count($content) > 0 )
184  {
185  // CHECK IF FILE WITH SAME NAME EXISTS
187  $this->__rotateFiles($filename);
188  file_put_contents($filename, $content);
189 
190  // check for virus
192  if (!$vir[0] ||$vir[1] != "")
193  {
194  unlink($filename);
195  return false;
196  }
197  return true;
198  }
199  return false;
200  }
201 
209  function deliverFile($a_http_post_file, $user_id, $is_unziped = false)
210  {
211  // TODO:
212  // CHECK UPLOAD LIMIT
213  //
214  $result = false;
215  if(isset($a_http_post_file) && $a_http_post_file['size'])
216  {
217  $filename = $a_http_post_file['name'];
218  // replace whitespaces with underscores
219  $filename = preg_replace("/\s/", "_", $filename);
220  // remove all special characters
221  $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
222 
223  if(!is_dir($savepath = $this->getExercisePath()."/".$this->obj_id))
224  {
225  ilUtil::makeDir($savepath);
226  }
227  $savepath .= '/' .$user_id;
228  if(!is_dir($savepath))
229  {
230  ilUtil::makeDir($savepath);
231  }
232 
233  // CHECK IF FILE PATH EXISTS
234  if (!is_dir($savepath))
235  {
236  require_once "./Services/Utilities/classes/class.ilUtil.php";
237  #ilUtil::makeDirParents($savepath);
238  ilUtil::makeDir($savepath);
239  }
240  $now = getdate();
241  $prefix = sprintf("%04d%02d%02d%02d%02d%02d", $now["year"], $now["mon"], $now["mday"], $now["hours"],
242  $now["minutes"], $now["seconds"]);
243 
244  if (!$is_unziped)
245  {
246  //move_uploaded_file($a_http_post_file["tmp_name"], $savepath . $prefix . "_" . $filename);
247  ilUtil::moveUploadedFile($a_http_post_file["tmp_name"], $a_http_post_file["name"],
248  $savepath . "/" . $prefix . "_" . $filename);
249  }
250  else
251  {
252 
253  rename($a_http_post_file['tmp_name'],
254  $savepath . "/" . $prefix . "_" . $filename);
255  }
256 
257  require_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
258 
259  if (is_file($savepath . "/" . $prefix . "_" . $filename))
260  {
261  $result = array(
262  "filename" => $prefix . "_" . $filename,
263  "fullname" => $savepath . "/" . $prefix . "_" . $filename,
264  "mimetype" => ilObjMediaObject::getMimeType($savepath . "/" . $prefix . "_" . $filename)
265  );
266  }
267  }
268  return $result;
269  }
270 
276  function downloadAllDeliveredFiles($members)
277  {
278  require_once "./Services/Utilities/classes/class.ilUtil.php";
279  global $lng, $ilObjDataCache;
280 
281  ksort($members);
282  $savepath = $this->getExercisePath() . "/" . $this->obj_id . "/";
283  $cdir = getcwd();
284 
285 
286  // important check: if the directory does not exist
287  // ILIAS stays in the current directory (echoing only a warning)
288  // and the zip command below archives the whole ILIAS directory
289  // (including the data directory) and sends a mega file to the user :-o
290  if (!is_dir($savepath))
291  {
292  return;
293  }
294  // Safe mode fix
295  chdir($this->getExercisePath());
296  $zip = PATH_TO_ZIP;
297 
298  // check first, if we have enough free disk space to copy all files to temporary directory
299  $tmpdir = ilUtil::ilTempnam();
300  ilUtil::makeDir($tmpdir);
301  chdir($tmpdir);
302 
303 
304  $dirsize = 0;
305  foreach ($members as $id => $object) {
306  $directory = $savepath.DIRECTORY_SEPARATOR.$id;
307  $dirsize += ilUtil::dirsize($directory);
308  }
309  if ($dirsize > disk_free_space($tmpdir)) {
310  return -1;
311  }
312 
313  // copy all member directories to the temporary folder
314  // switch from id to member name and append the login if the member name is double
315  // ensure that no illegal filenames will be created
316  // remove timestamp from filename
317  $cache = array();
318  foreach ($members as $id => $user)
319  {
320  $sourcedir = $savepath.DIRECTORY_SEPARATOR.$id;
321  if (!is_dir($sourcedir))
322  continue;
323  $userName = ilObjUser::_lookupName($id);
324  $directory = ilUtil::getASCIIFilename(trim($userName["lastname"])."_".trim($userName["firstname"]));
325  if (array_key_exists($directory, $cache))
326  {
327  // first try is to append the login;
328  $directory = ilUtil::getASCIIFilename($directory."_".trim(ilObjUser::_lookupLogin($id)));
329  if (array_key_exists($directory, $cache)) {
330  // second and secure: append the user id as well.
331  $directory .= "_".$id;
332  }
333  }
334 
335  $cache[$directory] = $directory;
336  ilUtil::makeDir ($directory);
337  $sourcefiles = scandir($sourcedir);
338  foreach ($sourcefiles as $sourcefile) {
339  if ($sourcefile == "." || $sourcefile == "..")
340  continue;
341  $targetfile = trim(basename($sourcefile));
342  $pos = strpos($targetfile, "_");
343  if ($pos === false)
344  {
345  } else
346  {
347  $targetfile= substr($targetfile, $pos + 1);
348  }
349  $targetfile = $directory.DIRECTORY_SEPARATOR.$targetfile;
350  $sourcefile = $sourcedir.DIRECTORY_SEPARATOR.$sourcefile;
351 
352  if (!copy ($sourcefile, $targetfile))
353  {
354  //echo 'Could not copy '.$sourcefile.' to '.$targetfile;
355  $this->ilias->raiseError('Could not copy '.basename($sourcefile)." to '".$targetfile."'.",
356  $this->ilias->error_obj->MESSAGE);
357  }
358  else
359  {
360  // preserve time stamp
361  touch($targetfile, filectime($sourcefile));
362  }
363 
364  }
365  }
366 
367  $tmpfile = ilUtil::ilTempnam();
368  $tmpzipfile = $tmpfile . ".zip";
369  // Safe mode fix
370  $zipcmd = $zip." -r ".ilUtil::escapeShellArg($tmpzipfile)." .";
371  exec($zipcmd);
372  ilUtil::delDir($tmpdir);
373 
374  $exerciseTitle = $ilObjDataCache->lookupTitle($this->getObjId());
375  ilUtil::deliverFile($tmpzipfile, (strlen($exerciseTitle) == 0? strtolower($lng->txt("excs")) : $exerciseTitle). ".zip");
376  chdir($cdir);
377  unlink($tmpfile);
378  unlink($tmpzipfile);
379  }
380 
381 
388  function unlinkFiles($a_filenames)
389  {
390  if(is_array($a_filenames))
391  {
392  foreach($a_filenames as $file)
393  {
394  if(!$this->unlinkFile($file))
395  {
396  return $file;
397  }
398  }
399  }
400  return '';
401  }
408  function unlinkFile($a_filename)
409  {
410  if(file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename))
411  {
412  return unlink($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename);
413  }
414  }
421  function getAbsolutePath($a_path)
422  {
423  return $this->exercise_path.'/'.$this->obj_id.'_'.$a_path;
424  }
425 
432  function checkFilesExist($a_files)
433  {
434  if($a_files)
435  {
436  foreach($a_files as $file)
437  {
438  if(!file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$file))
439  {
440  return false;
441  }
442  }
443  return true;
444  }
445  return true;
446  }
447 
448  // PRIVATE METHODS
449  function __checkPath()
450  {
451  if(!@file_exists($this->getExercisePath()))
452  {
453  return false;
454  }
455  $this->__checkReadWrite();
456 
457  return true;
458  }
465  function __checkReadWrite()
466  {
467  if(is_writable($this->exercise_path) && is_readable($this->exercise_path))
468  {
469  return true;
470  }
471  else
472  {
473  $this->ilias->raiseError("Exercise directory is not readable/writable by webserver",$this->ilias->error_obj->FATAL);
474  }
475  }
482  function __initDirectory()
483  {
484  if(is_writable($this->getPath()))
485  {
486  return ilUtil::makeDir($this->excercise_path = $this->getPath().'/'.EXERCISE_PATH);
487  }
488  return false;
489  }
497  function __rotateFiles($a_path)
498  {
499  if(file_exists($a_path))
500  {
501  $this->__rotateFiles($a_path.".old");
502  return rename($a_path,$a_path.'.old');
503  }
504  return true;
505  }
506 }