ILIAS  Release_4_1_x_branch Revision 61804
 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 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"])."_".trim($userName["firstname"]));
258  if (array_key_exists($directory, $cache))
259  {
260  // first try is to append the login;
261  $directory = ilUtil::getASCIIFilename($directory."_".trim(ilObjUser::_lookupLogin($id)));
262  if (array_key_exists($directory, $cache)) {
263  // second and secure: append the user id as well.
264  $directory .= "_".$id;
265  }
266  }
267 
268  $cache[$directory] = $directory;
269  ilUtil::makeDir ($directory);
270  $sourcefiles = scandir($sourcedir);
271  foreach ($sourcefiles as $sourcefile) {
272  if ($sourcefile == "." || $sourcefile == "..")
273  continue;
274  $targetfile = trim(basename($sourcefile));
275  $pos = strpos($targetfile, "_");
276  if ($pos === false)
277  {
278  } else
279  {
280  $targetfile= substr($targetfile, $pos + 1);
281  }
282  $targetfile = $directory.DIRECTORY_SEPARATOR.$targetfile;
283  $sourcefile = $sourcedir.DIRECTORY_SEPARATOR.$sourcefile;
284 
285  if (!copy ($sourcefile, $targetfile))
286  {
287  //echo 'Could not copy '.$sourcefile.' to '.$targetfile;
288  $this->ilias->raiseError('Could not copy '.basename($sourcefile)." to '".$targetfile."'.",
289  $this->ilias->error_obj->MESSAGE);
290  }
291  else
292  {
293  // preserve time stamp
294  touch($targetfile, filectime($sourcefile));
295  }
296 
297  }
298  }
299 
300  $tmpfile = ilUtil::ilTempnam();
301  $tmpzipfile = $tmpfile . ".zip";
302  // Safe mode fix
303  $zipcmd = $zip." -r ".ilUtil::escapeShellArg($tmpzipfile)." .";
304  exec($zipcmd);
305  ilUtil::delDir($tmpdir);
306 
307  $exerciseTitle = $ilObjDataCache->lookupTitle($this->getObjId());
308  ilUtil::deliverFile($tmpzipfile, (strlen($exerciseTitle) == 0? strtolower($lng->txt("excs")) : $exerciseTitle). ".zip");
309  chdir($cdir);
310  unlink($tmpfile);
311  unlink($tmpzipfile);
312  }
313 
314 
321  function unlinkFiles($a_filenames)
322  {
323  if(is_array($a_filenames))
324  {
325  foreach($a_filenames as $file)
326  {
327  if(!$this->unlinkFile($file))
328  {
329  return $file;
330  }
331  }
332  }
333  return '';
334  }
341  function unlinkFile($a_filename)
342  {
343  if(file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename))
344  {
345  return unlink($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename);
346  }
347  }
354  function getAbsolutePath($a_path)
355  {
356  return $this->exercise_path.'/'.$this->obj_id.'_'.$a_path;
357  }
358 
365  function checkFilesExist($a_files)
366  {
367  if($a_files)
368  {
369  foreach($a_files as $file)
370  {
371  if(!file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$file))
372  {
373  return false;
374  }
375  }
376  return true;
377  }
378  return true;
379  }
380 
381  // PRIVATE METHODS
382  function __checkPath()
383  {
384  if(!@file_exists($this->getExercisePath()))
385  {
386  return false;
387  }
388  $this->__checkReadWrite();
389 
390  return true;
391  }
398  function __checkReadWrite()
399  {
400  if(is_writable($this->exercise_path) && is_readable($this->exercise_path))
401  {
402  return true;
403  }
404  else
405  {
406  $this->ilias->raiseError("Exercise directory is not readable/writable by webserver",$this->ilias->error_obj->FATAL);
407  }
408  }
415  function __initDirectory()
416  {
417  if(is_writable($this->getPath()))
418  {
419  return ilUtil::makeDir($this->excercise_path = $this->getPath().'/'.EXERCISE_PATH);
420  }
421  return false;
422  }
430  function __rotateFiles($a_path)
431  {
432  if(file_exists($a_path))
433  {
434  $this->__rotateFiles($a_path.".old");
435  return rename($a_path,$a_path.'.old');
436  }
437  return true;
438  }
439 }