ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilFileDataExercise.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once("classes/class.ilFileData.php");
25 
35 {
41  var $obj_id;
42 
49 
57  function ilFileDataExercise($a_obj_id = 0)
58  {
59  define('EXERCISE_PATH','exercise');
61  $this->exercise_path = parent::getPath()."/".EXERCISE_PATH;
62 
63  // IF DIRECTORY ISN'T CREATED CREATE IT
64  if(!$this->__checkPath())
65  {
66  $this->__initDirectory();
67  }
68  $this->obj_id = $a_obj_id;
69  }
70 
71  function getObjId()
72  {
73  return $this->obj_id;
74  }
75 
81  function getExercisePath()
82  {
83  return $this->exercise_path;
84  }
85 
86  function getFiles()
87  {
88  $files = array();
89  $dp = opendir($this->exercise_path);
90 
91  while($file = readdir($dp))
92  {
93  if(is_dir($file))
94  {
95  continue;
96  }
97  list($obj_id,$rest) = split('_',$file,2);
98  if($obj_id == $this->obj_id)
99  {
100  if(!is_dir($this->exercise_path.'/'.$file))
101  {
102  $files[] = array(
103  'name' => $rest,
104  'size' => filesize($this->exercise_path.'/'.$file),
105  'ctime' => ilFormat::formatDate(date('Y-m-d H:i:s',filectime($this->exercise_path.'/'.$file))),
106  'fullpath' => $this->exercise_path.'/'.$file);
107  }
108  }
109  }
110  closedir($dp);
111  return $files;
112  }
113 
114  function ilClone($a_new_obj_id)
115  {
116  foreach($this->getFiles() as $file)
117  {
118  @copy($this->getExercisePath()."/".$this->obj_id.'_'.$file["name"],
119  $this->getExercisePath()."/".$a_new_obj_id.'_'.$file["name"]);
120  }
121  return true;
122  }
123  function delete()
124  {
125  foreach($this->getFiles() as $file)
126  {
127  $this->unlinkFile($file["name"]);
128  }
129 
130  $delivered_file_path = $this->getExercisePath() . "/" . $this->obj_id . "/";
131  if (is_dir($delivered_file_path))
132  {
133  include_once "./Services/Utilities/classes/class.ilUtil.php";
134  ilUtil::delDir($delivered_file_path);
135  }
136 
137  return true;
138  }
139 
147  function storeUploadedFile($a_http_post_file, $secure_filename = false, $is_unziped = false)
148  {
149  // TODO:
150  // CHECK UPLOAD LIMIT
151  //
152  $filename = $a_http_post_file['name'];
153  if ($secure_filename)
154  {
155  // replace whitespaces with underscores
156  $filename = preg_replace("/\s/", "_", $filename);
157  // remove all special characters
158  $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
159  }
160  if(isset($a_http_post_file) && $a_http_post_file['size'])
161  {
162  // CHECK IF FILE WITH SAME NAME EXISTS
163  $this->__rotateFiles($this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
164  //move_uploaded_file($a_http_post_file['tmp_name'],$this->getExercisePath().'/'.$this->obj_id.'_'.
165  // $filename);
166 
167  if (!$is_unziped)
168  {
169  ilUtil::moveUploadedFile($a_http_post_file['tmp_name'], $a_http_post_file['name'],
170  $this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
171  }
172  else
173  {
174  // ######## Warning, there is no check whether the unziped files are virus free or not
175  rename($a_http_post_file['tmp_name'],
176  $this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
177  }
178 
179  }
180  return true;
181  }
182 
183 
191  function storeContentAsFile($filename, $content, $secure_filename = false)
192  {
193  // TODO:
194  // CHECK UPLOAD LIMIT
195  //
196  if ($secure_filename)
197  {
198  // replace whitespaces with underscores
199  $filename = preg_replace("/\s/", "_", $filename);
200  // remove all special characters
201  $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
202  }
203  if(count($content) > 0 )
204  {
205  // CHECK IF FILE WITH SAME NAME EXISTS
207  $this->__rotateFiles($filename);
208  file_put_contents($filename, $content);
209 
210  // check for virus
212  if (!$vir[0] ||$vir[1] != "")
213  {
214  unlink($filename);
215  return false;
216  }
217  return true;
218  }
219  return false;
220  }
221 
229  function deliverFile($a_http_post_file, $user_id, $is_unziped = false)
230  {
231  // TODO:
232  // CHECK UPLOAD LIMIT
233  //
234  $result = false;
235  if(isset($a_http_post_file) && $a_http_post_file['size'])
236  {
237  $filename = $a_http_post_file['name'];
238  // replace whitespaces with underscores
239  $filename = preg_replace("/\s/", "_", $filename);
240  // remove all special characters
241  $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
242 
243  if(!is_dir($savepath = $this->getExercisePath()."/".$this->obj_id))
244  {
245  ilUtil::makeDir($savepath);
246  }
247  $savepath .= '/' .$user_id;
248  if(!is_dir($savepath))
249  {
250  ilUtil::makeDir($savepath);
251  }
252 
253  // CHECK IF FILE PATH EXISTS
254  if (!is_dir($savepath))
255  {
256  require_once "./Services/Utilities/classes/class.ilUtil.php";
257  #ilUtil::makeDirParents($savepath);
258  ilUtil::makeDir($savepath);
259  }
260  $now = getdate();
261  $prefix = sprintf("%04d%02d%02d%02d%02d%02d", $now["year"], $now["mon"], $now["mday"], $now["hours"],
262  $now["minutes"], $now["seconds"]);
263 
264  if (!$is_unziped)
265  {
266  //move_uploaded_file($a_http_post_file["tmp_name"], $savepath . $prefix . "_" . $filename);
267  ilUtil::moveUploadedFile($a_http_post_file["tmp_name"], $a_http_post_file["name"],
268  $savepath . "/" . $prefix . "_" . $filename);
269  }
270  else
271  {
272 
273  rename($a_http_post_file['tmp_name'],
274  $savepath . "/" . $prefix . "_" . $filename);
275  }
276 
277  require_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
278 
279  if (is_file($savepath . "/" . $prefix . "_" . $filename))
280  {
281  $result = array(
282  "filename" => $prefix . "_" . $filename,
283  "fullname" => $savepath . "/" . $prefix . "_" . $filename,
284  "mimetype" => ilObjMediaObject::getMimeType($savepath . "/" . $prefix . "_" . $filename)
285  );
286  }
287  }
288  return $result;
289  }
290 
296  function downloadAllDeliveredFiles($members)
297  {
298  require_once "./Services/Utilities/classes/class.ilUtil.php";
299  global $lng, $ilObjDataCache;
300 
301  ksort($members);
302  $savepath = $this->getExercisePath() . "/" . $this->obj_id . "/";
303  $cdir = getcwd();
304 
305 
306  // important check: if the directory does not exist
307  // ILIAS stays in the current directory (echoing only a warning)
308  // and the zip command below archives the whole ILIAS directory
309  // (including the data directory) and sends a mega file to the user :-o
310  if (!is_dir($savepath))
311  {
312  return;
313  }
314  // Safe mode fix
315  chdir($this->getExercisePath());
316  $zip = PATH_TO_ZIP;
317 
318  // check first, if we have enough free disk space to copy all files to temporary directory
319  $tmpdir = ilUtil::ilTempnam();
320  ilUtil::makeDir($tmpdir);
321  chdir($tmpdir);
322 
323 
324  $dirsize = 0;
325  foreach ($members as $id => $object) {
326  $directory = $savepath.DIRECTORY_SEPARATOR.$id;
327  $dirsize += ilUtil::dirsize($directory);
328  }
329  if ($dirsize > disk_free_space($tmpdir)) {
330  return -1;
331  }
332 
333  // copy all member directories to the temporary folder
334  // switch from id to member name and append the login if the member name is double
335  // ensure that no illegal filenames will be created
336  // remove timestamp from filename
337  $cache = array();
338  foreach ($members as $id => $user)
339  {
340  $sourcedir = $savepath.DIRECTORY_SEPARATOR.$id;
341  if (!is_dir($sourcedir))
342  continue;
343  $userName = ilObjUser::_lookupName($id);
344  $directory = ilUtil::getASCIIFilename(trim($userName["lastname"])."_".trim($userName["firstname"]));
345  if (array_key_exists($directory, $cache))
346  {
347  // first try is to append the login;
348  $directory = ilUtil::getASCIIFilename($directory."_".trim(ilObjUser::_lookupLogin($id)));
349  if (array_key_exists($directory, $cache)) {
350  // second and secure: append the user id as well.
351  $directory .= "_".$id;
352  }
353  }
354 
355  $cache[$directory] = $directory;
356  ilUtil::makeDir ($directory);
357  $sourcefiles = scandir($sourcedir);
358  foreach ($sourcefiles as $sourcefile) {
359  if ($sourcefile == "." || $sourcefile == "..")
360  continue;
361  $targetfile = trim(basename($sourcefile));
362  $pos = strpos($targetfile, "_");
363  if ($pos === false)
364  {
365  } else
366  {
367  $targetfile= substr($targetfile, $pos + 1);
368  }
369  $targetfile = $directory.DIRECTORY_SEPARATOR.$targetfile;
370  $sourcefile = $sourcedir.DIRECTORY_SEPARATOR.$sourcefile;
371 
372  if (!copy ($sourcefile, $targetfile))
373  {
374  //echo 'Could not copy '.$sourcefile.' to '.$targetfile;
375  $this->ilias->raiseError('Could not copy '.basename($sourcefile)." to '".$targetfile."'.",
376  $this->ilias->error_obj->MESSAGE);
377  }
378  else
379  {
380  // preserve time stamp
381  touch($targetfile, filectime($sourcefile));
382  }
383 
384  }
385  }
386 
387  $tmpfile = ilUtil::ilTempnam();
388  $tmpzipfile = $tmpfile . ".zip";
389  // Safe mode fix
390  $zipcmd = $zip." -r ".ilUtil::escapeShellArg($tmpzipfile)." .";
391  exec($zipcmd);
392  ilUtil::delDir($tmpdir);
393 
394  $exerciseTitle = $ilObjDataCache->lookupTitle($this->getObjId());
395  ilUtil::deliverFile($tmpzipfile, (strlen($exerciseTitle) == 0? strtolower($lng->txt("excs")) : $exerciseTitle). ".zip");
396  chdir($cdir);
397  unlink($tmpfile);
398  unlink($tmpzipfile);
399  }
400 
401 
408  function unlinkFiles($a_filenames)
409  {
410  if(is_array($a_filenames))
411  {
412  foreach($a_filenames as $file)
413  {
414  if(!$this->unlinkFile($file))
415  {
416  return $file;
417  }
418  }
419  }
420  return '';
421  }
428  function unlinkFile($a_filename)
429  {
430  if(file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename))
431  {
432  return unlink($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename);
433  }
434  }
441  function getAbsolutePath($a_path)
442  {
443  return $this->exercise_path.'/'.$this->obj_id.'_'.$a_path;
444  }
445 
452  function checkFilesExist($a_files)
453  {
454  if($a_files)
455  {
456  foreach($a_files as $file)
457  {
458  if(!file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$file))
459  {
460  return false;
461  }
462  }
463  return true;
464  }
465  return true;
466  }
467 
468  // PRIVATE METHODS
469  function __checkPath()
470  {
471  if(!@file_exists($this->getExercisePath()))
472  {
473  return false;
474  }
475  $this->__checkReadWrite();
476 
477  return true;
478  }
485  function __checkReadWrite()
486  {
487  if(is_writable($this->exercise_path) && is_readable($this->exercise_path))
488  {
489  return true;
490  }
491  else
492  {
493  $this->ilias->raiseError("Exercise directory is not readable/writable by webserver",$this->ilias->error_obj->FATAL);
494  }
495  }
502  function __initDirectory()
503  {
504  if(is_writable($this->getPath()))
505  {
506  return ilUtil::makeDir($this->excercise_path = $this->getPath().'/'.EXERCISE_PATH);
507  }
508  return false;
509  }
517  function __rotateFiles($a_path)
518  {
519  if(file_exists($a_path))
520  {
521  $this->__rotateFiles($a_path.".old");
522  return rename($a_path,$a_path.'.old');
523  }
524  return true;
525  }
526 }