00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 require_once("classes/class.ilFileData.php");
00025
00034 class ilFileDataExercise extends ilFileData
00035 {
00041 var $obj_id;
00042
00048 var $exercise_path;
00049
00057 function ilFileDataExercise($a_obj_id = 0)
00058 {
00059 define('EXERCISE_PATH','exercise');
00060 parent::ilFileData();
00061 $this->exercise_path = parent::getPath()."/".EXERCISE_PATH;
00062
00063
00064 if(!$this->__checkPath())
00065 {
00066 $this->__initDirectory();
00067 }
00068 $this->obj_id = $a_obj_id;
00069 }
00070
00071 function getObjId()
00072 {
00073 return $this->obj_id;
00074 }
00075
00081 function getExercisePath()
00082 {
00083 return $this->exercise_path;
00084 }
00085
00086 function getFiles()
00087 {
00088 $files = array();
00089 $dp = opendir($this->exercise_path);
00090
00091 while($file = readdir($dp))
00092 {
00093 if(is_dir($file))
00094 {
00095 continue;
00096 }
00097 list($obj_id,$rest) = split('_',$file,2);
00098 if($obj_id == $this->obj_id)
00099 {
00100 if(!is_dir($this->exercise_path.'/'.$file))
00101 {
00102 $files[] = array(
00103 'name' => $rest,
00104 'size' => filesize($this->exercise_path.'/'.$file),
00105 'ctime' => ilFormat::formatDate(date('Y-m-d H:i:s',filectime($this->exercise_path.'/'.$file))),
00106 'fullpath' => $this->exercise_path.'/'.$file);
00107 }
00108 }
00109 }
00110 closedir($dp);
00111 return $files;
00112 }
00113
00114 function ilClone($a_new_obj_id)
00115 {
00116 foreach($this->getFiles() as $file)
00117 {
00118 @copy($this->getExercisePath()."/".$this->obj_id.'_'.$file["name"],
00119 $this->getExercisePath()."/".$a_new_obj_id.'_'.$file["name"]);
00120 }
00121 return true;
00122 }
00123 function delete()
00124 {
00125 foreach($this->getFiles() as $file)
00126 {
00127 $this->unlinkFile($file["name"]);
00128 }
00129
00130 $delivered_file_path = $this->getExercisePath() . "/" . $this->obj_id . "/";
00131 if (is_dir($delivered_file_path))
00132 {
00133 include_once "./Services/Utilities/classes/class.ilUtil.php";
00134 ilUtil::delDir($delivered_file_path);
00135 }
00136
00137 return true;
00138 }
00139
00147 function storeUploadedFile($a_http_post_file, $secure_filename = false, $is_unziped = false)
00148 {
00149
00150
00151
00152 $filename = $a_http_post_file['name'];
00153 if ($secure_filename)
00154 {
00155
00156 $filename = preg_replace("/\s/", "_", $filename);
00157
00158 $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
00159 }
00160 if(isset($a_http_post_file) && $a_http_post_file['size'])
00161 {
00162
00163 $this->__rotateFiles($this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
00164
00165
00166
00167 if (!$is_unziped)
00168 {
00169 ilUtil::moveUploadedFile($a_http_post_file['tmp_name'], $a_http_post_file['name'],
00170 $this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
00171 }
00172 else
00173 {
00174
00175 rename($a_http_post_file['tmp_name'],
00176 $this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
00177 }
00178
00179 }
00180 return true;
00181 }
00182
00183
00191 function storeContentAsFile($filename, $content, $secure_filename = false)
00192 {
00193
00194
00195
00196 if ($secure_filename)
00197 {
00198
00199 $filename = preg_replace("/\s/", "_", $filename);
00200
00201 $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
00202 }
00203 if(count($content) > 0 )
00204 {
00205
00206 $filename = $this->getAbsolutePath($filename);
00207 $this->__rotateFiles($filename);
00208 file_put_contents($filename, $content);
00209
00210
00211 $vir = ilUtil::virusHandling($filename);
00212 if (!$vir[0] ||$vir[1] != "")
00213 {
00214 unlink($filename);
00215 return false;
00216 }
00217 return true;
00218 }
00219 return false;
00220 }
00221
00229 function deliverFile($a_http_post_file, $user_id, $is_unziped = false)
00230 {
00231
00232
00233
00234 $result = false;
00235 if(isset($a_http_post_file) && $a_http_post_file['size'])
00236 {
00237 $filename = $a_http_post_file['name'];
00238
00239 $filename = preg_replace("/\s/", "_", $filename);
00240
00241 $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
00242
00243 if(!is_dir($savepath = $this->getExercisePath()."/".$this->obj_id))
00244 {
00245 ilUtil::makeDir($savepath);
00246 }
00247 $savepath .= '/' .$user_id;
00248 if(!is_dir($savepath))
00249 {
00250 ilUtil::makeDir($savepath);
00251 }
00252
00253
00254 if (!is_dir($savepath))
00255 {
00256 require_once "./Services/Utilities/classes/class.ilUtil.php";
00257 #ilUtil::makeDirParents($savepath);
00258 ilUtil::makeDir($savepath);
00259 }
00260 $now = getdate();
00261 $prefix = sprintf("%04d%02d%02d%02d%02d%02d", $now["year"], $now["mon"], $now["mday"], $now["hours"],
00262 $now["minutes"], $now["seconds"]);
00263
00264 if (!$is_unziped)
00265 {
00266
00267 ilUtil::moveUploadedFile($a_http_post_file["tmp_name"], $a_http_post_file["name"],
00268 $savepath . "/" . $prefix . "_" . $filename);
00269 }
00270 else
00271 {
00272
00273 rename($a_http_post_file['tmp_name'],
00274 $savepath . "/" . $prefix . "_" . $filename);
00275 }
00276
00277 require_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
00278
00279 if (is_file($savepath . "/" . $prefix . "_" . $filename))
00280 {
00281 $result = array(
00282 "filename" => $prefix . "_" . $filename,
00283 "fullname" => $savepath . "/" . $prefix . "_" . $filename,
00284 "mimetype" => ilObjMediaObject::getMimeType($savepath . "/" . $prefix . "_" . $filename)
00285 );
00286 }
00287 }
00288 return $result;
00289 }
00290
00296 function downloadAllDeliveredFiles($members)
00297 {
00298 require_once "./Services/Utilities/classes/class.ilUtil.php";
00299 global $lng, $ilObjDataCache;
00300
00301 ksort($members);
00302 $savepath = $this->getExercisePath() . "/" . $this->obj_id . "/";
00303 $cdir = getcwd();
00304
00305
00306
00307
00308
00309
00310 if (!is_dir($savepath))
00311 {
00312 return;
00313 }
00314
00315 chdir($this->getExercisePath());
00316 $zip = PATH_TO_ZIP;
00317
00318
00319 $tmpdir = ilUtil::ilTempnam();
00320 ilUtil::makeDir($tmpdir);
00321 chdir($tmpdir);
00322
00323
00324 $dirsize = 0;
00325 foreach ($members as $id => $object) {
00326 $directory = $savepath.DIRECTORY_SEPARATOR.$id;
00327 $dirsize += ilUtil::dirsize($directory);
00328 }
00329 if ($dirsize > disk_free_space($tmpdir)) {
00330 return -1;
00331 }
00332
00333
00334
00335
00336
00337 $cache = array();
00338 foreach ($members as $id => $user)
00339 {
00340 $sourcedir = $savepath.DIRECTORY_SEPARATOR.$id;
00341 if (!is_dir($sourcedir))
00342 continue;
00343 $userName = ilObjUser::_lookupName($id);
00344 $directory = ilUtil::getASCIIFilename($userName["lastname"]."_".$userName["firstname"]);
00345 if (array_key_exists($directory, $cache))
00346 {
00347
00348 $directory = ilUtil::getASCIIFilename($directory."_". ilObjUser::_lookupLogin($id));
00349 if (array_key_exists($directory, $cache)) {
00350
00351 $directory .= "_".$id;
00352 }
00353 }
00354 $cache[$directory] = $directory;
00355 ilUtil::makeDir ($directory);
00356 $sourcefiles = scandir($sourcedir);
00357 foreach ($sourcefiles as $sourcefile) {
00358 if ($sourcefile == "." || $sourcefile == "..")
00359 continue;
00360 $targetfile = trim(basename($sourcefile));
00361 $pos = strpos($targetfile, "_");
00362 if ($pos === false)
00363 {
00364 } else
00365 {
00366 $targetfile= substr($targetfile, $pos + 1);
00367 }
00368 $targetfile = $directory.DIRECTORY_SEPARATOR.$targetfile;
00369 $sourcefile = $sourcedir.DIRECTORY_SEPARATOR.$sourcefile;
00370
00371 if (!copy ($sourcefile, $targetfile))
00372 {
00373 echo 'Could not copy '.$sourcefile.' to '.$targetfile;
00374 } else
00375 {
00376
00377 touch($targetfile, filectime($sourcefile));
00378 }
00379
00380 }
00381 }
00382
00383 $tmpfile = ilUtil::ilTempnam();
00384 $tmpzipfile = $tmpfile . ".zip";
00385
00386 $zipcmd = $zip." -r ".ilUtil::escapeShellArg($tmpzipfile)." .";
00387 exec($zipcmd);
00388 ilUtil::delDir($tmpdir);
00389
00390 $exerciseTitle = $ilObjDataCache->lookupTitle($this->getObjId());
00391 ilUtil::deliverFile($tmpzipfile, (strlen($exerciseTitle) == 0? strtolower($lng->txt("excs")) : $exerciseTitle). ".zip");
00392 chdir($cdir);
00393 unlink($tmpfile);
00394 unlink($tmpzipfile);
00395 }
00396
00397
00404 function unlinkFiles($a_filenames)
00405 {
00406 if(is_array($a_filenames))
00407 {
00408 foreach($a_filenames as $file)
00409 {
00410 if(!$this->unlinkFile($file))
00411 {
00412 return $file;
00413 }
00414 }
00415 }
00416 return '';
00417 }
00424 function unlinkFile($a_filename)
00425 {
00426 if(file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename))
00427 {
00428 return unlink($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename);
00429 }
00430 }
00437 function getAbsolutePath($a_path)
00438 {
00439 return $this->exercise_path.'/'.$this->obj_id.'_'.$a_path;
00440 }
00441
00448 function checkFilesExist($a_files)
00449 {
00450 if($a_files)
00451 {
00452 foreach($a_files as $file)
00453 {
00454 if(!file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$file))
00455 {
00456 return false;
00457 }
00458 }
00459 return true;
00460 }
00461 return true;
00462 }
00463
00464
00465 function __checkPath()
00466 {
00467 if(!@file_exists($this->getExercisePath()))
00468 {
00469 return false;
00470 }
00471 $this->__checkReadWrite();
00472
00473 return true;
00474 }
00481 function __checkReadWrite()
00482 {
00483 if(is_writable($this->exercise_path) && is_readable($this->exercise_path))
00484 {
00485 return true;
00486 }
00487 else
00488 {
00489 $this->ilias->raiseError("Exercise directory is not readable/writable by webserver",$this->ilias->error_obj->FATAL);
00490 }
00491 }
00498 function __initDirectory()
00499 {
00500 if(is_writable($this->getPath()))
00501 {
00502 return ilUtil::makeDir($this->excercise_path = $this->getPath().'/'.EXERCISE_PATH);
00503 }
00504 return false;
00505 }
00513 function __rotateFiles($a_path)
00514 {
00515 if(file_exists($a_path))
00516 {
00517 $this->__rotateFiles($a_path.".old");
00518 return rename($a_path,$a_path.'.old');
00519 }
00520 return true;
00521 }
00522 }