• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilFileDataExercise.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00024 
00033 require_once("classes/class.ilFileData.php");
00034                                 
00035 class ilFileDataExercise extends ilFileData
00036 {
00042         var $obj_id;
00043 
00049         var $exercise_path;
00050 
00058         function ilFileDataExercise($a_obj_id = 0)
00059         {
00060                 define('EXERCISE_PATH','exercise');
00061                 parent::ilFileData();
00062                 $this->exercise_path = parent::getPath()."/".EXERCISE_PATH;
00063                 
00064                 // IF DIRECTORY ISN'T CREATED CREATE IT
00065                 if(!$this->__checkPath())
00066                 {
00067                         $this->__initDirectory();
00068                 }
00069                 $this->obj_id = $a_obj_id;
00070         }
00071 
00072         function getObjId()
00073         {
00074                 return $this->obj_id;
00075         }
00076 
00082         function getExercisePath()
00083         {
00084                 return $this->exercise_path;
00085         }
00086 
00087         function getFiles()
00088         {
00089                 $files = array();
00090                 $dp = opendir($this->exercise_path);
00091 
00092                 while($file = readdir($dp))
00093                 {
00094                         if(is_dir($file))
00095                         {
00096                                 continue;
00097                         }
00098                         list($obj_id,$rest) = split('_',$file,2);
00099                         if($obj_id == $this->obj_id)
00100                         {
00101                                 if(!is_dir($this->exercise_path.'/'.$file))
00102                                 {
00103                                         $files[] = array(
00104                                                 'name'     => $rest,
00105                                                 'size'     => filesize($this->exercise_path.'/'.$file),
00106                                                 'ctime'    => ilFormat::formatDate(date('Y-m-d H:i:s',filectime($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 "./classes/class.ilUtil.php";
00134                         ilUtil::delDir($delivered_file_path);
00135                 }
00136                 
00137                 return true;
00138         }
00139 
00146         function storeUploadedFile($a_http_post_file, $secure_filename = false)
00147         {
00148                 // TODO: 
00149                 // CHECK UPLOAD LIMIT
00150                 // 
00151                 $filename = $a_http_post_file['name'];
00152                 if ($secure_filename)
00153                 {
00154                         // replace whitespaces with underscores
00155                         $filename = preg_replace("/\s/", "_", $filename);
00156                         // remove all special characters
00157                         $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
00158                 }
00159                 if(isset($a_http_post_file) && $a_http_post_file['size'])
00160                 {
00161                         // CHECK IF FILE WITH SAME NAME EXISTS
00162                         $this->__rotateFiles($this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
00163                         //move_uploaded_file($a_http_post_file['tmp_name'],$this->getExercisePath().'/'.$this->obj_id.'_'.
00164                         //                                 $filename);
00165                         ilUtil::moveUploadedFile($a_http_post_file['tmp_name'], $a_http_post_file['name'],
00166                                 $this->getExercisePath().'/'.$this->obj_id.'_'.$filename);
00167 
00168                 }
00169                 return true;
00170         }
00171 
00179         function deliverFile($a_http_post_file, $user_id)
00180         {
00181                 // TODO: 
00182                 // CHECK UPLOAD LIMIT
00183                 // 
00184                 $result = false;
00185                 if(isset($a_http_post_file) && $a_http_post_file['size'])
00186                 {
00187                         $filename = $a_http_post_file['name'];
00188                         // replace whitespaces with underscores
00189                         $filename = preg_replace("/\s/", "_", $filename);
00190                         // remove all special characters
00191                         $filename = preg_replace("/[^_a-zA-Z0-9\.]/", "", $filename);
00192 
00193                         if(!is_dir($savepath = $this->getExercisePath()."/".$this->obj_id))
00194                         {
00195                                 ilUtil::makeDir($savepath);
00196                         }
00197                         $savepath .= '/' .$user_id;
00198                         if(!is_dir($savepath))
00199                         {
00200                                 ilUtil::makeDir($savepath);
00201                         }                               
00202 
00203                         // CHECK IF FILE PATH EXISTS
00204                         if (!is_dir($savepath))
00205                         {
00206                                 require_once "./classes/class.ilUtil.php";
00207                                 #ilUtil::makeDirParents($savepath);
00208                                 ilUtil::makeDir($savepath);
00209                         }
00210                         $now = getdate();
00211                         $prefix = sprintf("%04d%02d%02d%02d%02d%02d", $now["year"], $now["mon"], $now["mday"], $now["hours"], 
00212                                                           $now["minutes"], $now["seconds"]);
00213                         //move_uploaded_file($a_http_post_file["tmp_name"], $savepath . $prefix . "_" . $filename);
00214                         ilUtil::moveUploadedFile($a_http_post_file["tmp_name"], $a_http_post_file["name"],
00215                                 $savepath . "/" . $prefix . "_" . $filename);
00216                         require_once "./content/classes/Media/class.ilObjMediaObject.php";
00217                         $result = array(
00218                                 "filename" => $prefix . "_" . $filename,
00219                                 "fullname" => $savepath . "/" . $prefix . "_" . $filename,
00220                                 "mimetype" =>   ilObjMediaObject::getMimeType($savepath . "/" . $prefix . "_" . $filename)
00221                         );
00222                 }
00223                 return $result;
00224         }
00225         
00226         function downloadAllDeliveredFiles($members)
00227         {
00228                 require_once "./classes/class.ilUtil.php";
00229                 global $lng;
00230                 
00231                 ksort($members);
00232                 $tmpfile = ilUtil::ilTempnam();
00233                 $fh = fopen($tmpfile, "w");
00234 
00235                 if ($fh)
00236                 {
00237                         foreach ($members as $id => $member)
00238                         {
00239                                 fwrite($fh, "$id\t$member\n");
00240                         }
00241                         fclose($fh);
00242                 }
00243 
00244                 $savepath = $this->getExercisePath() . "/" . $this->obj_id . "/";
00245                 copy($tmpfile, $savepath . "users.txt");
00246                 $cdir = getcwd();
00247 
00248                 // important check: if the directory does not exist
00249                 // ILIAS stays in the current directory (echoing only a warning)
00250                 // and the zip command below archives the whole ILIAS directory
00251                 // (including the data directory) and sends a mega file to the user :-o
00252                 if (!is_dir($savepath))
00253                 {
00254                         return;
00255                 }
00256                 // Safe mode fix
00257                 chdir($this->getExercisePath());
00258                 $zip = PATH_TO_ZIP;
00259                 $tmpfile = ilUtil::ilTempnam();
00260                 $tmpzipfile = $tmpfile . ".zip";
00261                 // Safe mode fix
00262                 $zipcmd = $zip." -r ".ilUtil::escapeShellArg($tmpzipfile)." ".$this->obj_id;
00263                 exec($zipcmd);
00264                 ilUtil::deliverFile($tmpzipfile, strtolower($lng->txt("excs")) . ".zip");
00265                 chdir($cdir);
00266                 unlink($savepath . "users.txt");
00267                 unlink($tmpfile);
00268                 unlink($tmpzipfile);
00269         }
00270         
00277         function unlinkFiles($a_filenames)
00278         {
00279                 if(is_array($a_filenames))
00280                 {
00281                         foreach($a_filenames as $file)
00282                         {
00283                                 if(!$this->unlinkFile($file))
00284                                 {
00285                                         return $file;
00286                                 }
00287                         }
00288                 }
00289                 return '';
00290         }
00297         function unlinkFile($a_filename)
00298         {
00299                 if(file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename))
00300                 {
00301                         return unlink($this->exercise_path.'/'.$this->obj_id.'_'.$a_filename);
00302                 }
00303         }
00310         function getAbsolutePath($a_path)
00311         {
00312                 return $this->exercise_path.'/'.$this->obj_id.'_'.$a_path;
00313         }
00314 
00321         function checkFilesExist($a_files)
00322         {
00323                 if($a_files)
00324                 {
00325                         foreach($a_files as $file)
00326                         {
00327                                 if(!file_exists($this->exercise_path.'/'.$this->obj_id.'_'.$file))
00328                                 {
00329                                         return false;
00330                                 }
00331                         }
00332                         return true;
00333                 }
00334                 return true;
00335         }
00336 
00337         // PRIVATE METHODS
00338         function __checkPath()
00339         {
00340                 if(!@file_exists($this->getExercisePath()))
00341                 {
00342                         return false;
00343                 }
00344                 $this->__checkReadWrite();
00345 
00346                 return true;
00347         }
00354         function __checkReadWrite()
00355         {
00356                 if(is_writable($this->exercise_path) && is_readable($this->exercise_path))
00357                 {
00358                         return true;
00359                 }
00360                 else
00361                 {
00362                         $this->ilias->raiseError("Exercise directory is not readable/writable by webserver",$this->ilias->error_obj->FATAL);
00363                 }
00364         }
00371         function __initDirectory()
00372         {
00373                 if(is_writable($this->getPath()))
00374                 {
00375                         return ilUtil::makeDir($this->excercise_path = $this->getPath().'/'.EXERCISE_PATH);
00376                 }
00377                 return false;
00378         }
00386         function __rotateFiles($a_path)
00387         {
00388                 if(file_exists($a_path))
00389                 {
00390                         $this->__rotateFiles($a_path.".old");
00391                         return rename($a_path,$a_path.'.old');
00392                 }
00393                 return true;
00394         }
00395 }

Generated on Fri Dec 13 2013 11:57:54 for ILIAS Release_3_6_x_branch .rev 46809 by  doxygen 1.7.1