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

classes/class.ilObjFile.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 
00035 require_once "class.ilObject.php";
00036 
00037 class ilObjFile extends ilObject
00038 {
00039         var $filename;
00040         var $filetype;
00041         var $filemaxsize = "20000000";  // not used yet
00042         var $raise_upload_error;
00043 
00050         function ilObjFile($a_id = 0,$a_call_by_reference = true)
00051         {
00052                 $this->version = 0;
00053                 $this->type = "file";
00054                 $this->raise_upload_error = true;
00055                 $this->ilObject($a_id,$a_call_by_reference);
00056         }
00057 
00058         function create()
00059         {
00060                 parent::create();
00061 
00062                 require_once("classes/class.ilHistory.php");
00063                 ilHistory::_createEntry($this->getId(), "create", $this->getFileName().",1");
00064 
00065                 $q = "INSERT INTO file_data (file_id,file_name,file_type,version) "
00066                         ."VALUES ('".$this->getId()."','"
00067                         .ilUtil::prepareDBString($this->getFileName())."','"
00068                         .$this->getFileType()."','"
00069                         ."1"."')";
00070                 $this->ilias->db->query($q);
00071         }
00072 
00073         function getDirectory($a_version = 0)
00074         {
00075                 $version_subdir = "";
00076 
00077                 if ($a_version)
00078                 {
00079                         $version_subdir = "/".sprintf("%03d", $a_version);
00080                 }
00081                 
00082                 return ilUtil::getDataDir()."/files/file_".$this->getId().$version_subdir;
00083         }
00084 
00085         function createDirectory()
00086         {
00087                 ilUtil::makeDir($this->getDirectory());
00088         }
00089         
00090         function raiseUploadError($a_raise = true)
00091         {
00092                 $this->raise_upload_error = $a_raise;
00093         }
00094 
00095         function getUploadFile($a_upload_file, $a_filename)
00096         {
00097                 $this->setVersion($this->getVersion() + 1);
00098 
00099                 if (@!is_dir($this->getDirectory($this->getVersion())))
00100                 {
00101                         ilUtil::makeDir($this->getDirectory($this->getVersion()));
00102                 }
00103 
00104                 $file = $this->getDirectory($this->getVersion())."/".$a_filename;
00105                 //move_uploaded_file($a_upload_file, $file);
00106                 ilUtil::moveUploadedFile($a_upload_file, $a_filename, $file, $this->raise_upload_error);
00107         }
00108 
00112         function replaceFile($a_upload_file, $a_filename)
00113         {
00114                 //$this->clearDataDirectory();          // ! This has to be changed, if multiple versions should be supported
00115                 $this->getUploadFile($a_upload_file, $a_filename);
00116                 
00117                 require_once("classes/class.ilHistory.php");
00118                 ilHistory::_createEntry($this->getId(), "replace",
00119                         $a_filename.",".$this->getVersion());
00120         }
00121 
00122 
00126         function copy($a_source,$a_destination)
00127         {
00128                 return copy($a_source,$this->getDirectory()."/".$a_destination);
00129         }
00130         
00134         function clearDataDirectory()
00135         {
00136                 ilUtil::delDir($this->getDirectory());
00137                 $this->createDirectory();
00138         }
00139 
00143         function read()
00144         {
00145                 parent::read();
00146 
00147                 $q = "SELECT * FROM file_data WHERE file_id = '".$this->getId()."'";
00148                 $r = $this->ilias->db->query($q);
00149                 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
00150 
00151                 $this->setFileName($row->file_name);
00152                 $this->setFileType($row->file_type);
00153                 $this->setVersion($row->version);
00154         }
00155 
00159         function update()
00160         {
00161                 parent::update();
00162                 
00163                 $q = "UPDATE file_data SET file_name = '".ilUtil::prepareDBString($this->getFileName()).
00164                         "', file_type = '".$this->getFiletype()."' ".
00165                         ", version = '".$this->getVersion()."' ".
00166                         "WHERE file_id = '".$this->getId()."'";
00167                 $this->ilias->db->query($q);
00168                 
00169                 return true;
00170         }
00171 
00172         function setFileName($a_name)
00173         {
00174                 $this->filename = $a_name;
00175         }
00176 
00177         function getFileName()
00178         {
00179                 return $this->filename;
00180         }
00181 
00182         function setFileType($a_type)
00183         {
00184                 $this->filetype = $a_type;
00185         }
00186 
00187         function getFileType()
00188         {
00189                 return $this->filetype;
00190         }
00191 
00192         function setVersion($a_version)
00193         {
00194                 $this->version = $a_version;
00195         }
00196 
00197         function getVersion()
00198         {
00199                 return $this->version;
00200         }
00201 
00202         function _lookupFileName($a_id)
00203         {
00204                 global $ilDB;
00205 
00206                 $q = "SELECT * FROM file_data WHERE file_id = '".$a_id."'";
00207                 $r = $ilDB->query($q);
00208                 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
00209 
00210                 return ilUtil::stripSlashes($row->file_name);
00211         }
00212 
00213 
00214         function _lookupFileSize($a_id, $a_as_string = false)
00215         {
00216                 global $ilDB;
00217 
00218                 $q = "SELECT * FROM file_data WHERE file_id = '".$a_id."'";
00219                 $r = $ilDB->query($q);
00220                 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
00221 
00222                 $file = ilUtil::getDataDir()."/files/file_".$a_id."/".$row->file_name;
00223 
00224                 if (@!is_file($file))
00225                 {
00226                         $version_subdir = "/".sprintf("%03d", ilObjFile::_lookupVersion($a_id));
00227                         $file = ilUtil::getDataDir()."/files/file_".$a_id.$version_subdir."/".$row->file_name;
00228                 }
00229 
00230                 if (is_file($file))
00231                 {
00232                         $size = filesize($file);
00233                 }
00234                 else
00235                 {
00236                         $size = 0;
00237                 }
00238                 
00239                 if ($a_as_string)
00240                 {
00241                         if ($size > 1000000)
00242                         {
00243                                 return round($size/1000000,2)." MB";
00244                         }
00245                         else if ($size > 1000)
00246                         {
00247                                 return round($size/1000,2)." KBytes";
00248                         }
00249                         else
00250                         {
00251                                 return $size." Bytes";
00252                         }
00253                         
00254                 }
00255                 
00256                 return $size;
00257         }
00258         
00262         function _lookupVersion($a_id)
00263         {
00264                 global $ilDB;
00265 
00266                 $q = "SELECT * FROM file_data WHERE file_id = '".$a_id."'";
00267                 $r = $ilDB->query($q);
00268                 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
00269 
00270                 return ilUtil::stripSlashes($row->version);
00271         }
00272 
00273         function sendFile($a_hist_entry_id = null)
00274         {       
00275                 if (is_null($a_hist_entry_id))
00276                 {
00277                         $file = $this->getDirectory($this->getVersion())."/".$this->getFileName();
00278 
00279                         // if not found lookup for file in file object's main directory for downward c  ompability
00280                         if (@!is_file($file))
00281                         {
00282                                 $file = $this->getDirectory()."/".$this->getFileName();
00283                         }
00284                 }
00285                 else
00286                 {
00287                         require_once("classes/class.ilHistory.php");
00288                         $entry = ilHistory::_getEntryByHistoryID($a_hist_entry_id);
00289                         
00290                         if ($entry === false)
00291                         {
00292                                 echo "3";return false;
00293                         }
00294 
00295                         $data = explode(",",$entry["info_params"]);
00296                         
00297                         // bugfix: first created file had no version number
00298                         // this is a workaround for all files created before the bug was fixed
00299                         if (empty($data[1]))
00300                         {
00301                                 $data[1] = "1";
00302                         }
00303 
00304                         $file = $this->getDirectory($data[1])."/".$data[0];
00305                         
00306                         // if not found lookup for file in file object's main directory for downward compability
00307                         if (@!is_file($file))
00308                         {
00309                                 $file = $this->getDirectory()."/".$data[0];
00310                         }
00311 
00312                         if (@is_file($file))
00313                         {
00314                                 ilUtil::deliverFile($file, $data[0]);
00315                                 return true;
00316                         }
00317                 }
00318 
00319                 if (@is_file($file))
00320                 {
00321                         ilUtil::deliverFile($file, $this->getFileName());
00322                         return true;
00323                 }
00324 
00325                 return false;
00326         }
00327 
00328         function ilClone($a_parent_ref)
00329         {
00330                 // always call parent clone function first!!
00331                 $new_ref_id = parent::ilClone($a_parent_ref);
00332 
00333                 $fileObj =& $this->ilias->obj_factory->getInstanceByRefId($new_ref_id);
00334                 $fileObj->createDirectory();
00335                 
00336                 // copy all versions of file
00337                 ilUtil::rCopy($this->getDirectory(),$fileObj->getDirectory());
00338                 //copy($this->getDirectory()."/".$this->getFileName(),$fileObj->getDirectory()."/".$this->getFileName());
00339 
00340                 $q = "INSERT INTO file_data (file_id,file_name,file_type,version) VALUES ('"
00341                         .$fileObj->getId()."','"
00342                         .ilUtil::prepareDBString($this->getFileName())."','"
00343                         .$this->getFileType()."','".$this->getVersion()
00344                         ."')";
00345 
00346                 $this->ilias->db->query($q);
00347 
00348                 // copy history entries
00349                 require_once("classes/class.ilHistory.php");
00350                 ilHistory::_copyEntriesForObject($this->getId(),$fileObj->getId());
00351 
00352                 // dump object
00353                 unset($fileObj);
00354 
00355                 // ... and finally always return new reference ID!!
00356                 return $new_ref_id;
00357         }
00358 
00365         function delete()
00366         {
00367                 // check, if file is used somewhere
00368                 $usages = $this->getUsages();
00369 
00370                 if (count($usages) == 0)
00371                 {
00372                         // always call parent delete function first!!
00373                         if (!parent::delete())
00374                         {
00375                                 return false;
00376                         }
00377 
00378                         // delete file data entry
00379                         $q = "DELETE FROM file_data WHERE file_id = '".$this->getId()."'";
00380                         $this->ilias->db->query($q);
00381                         
00382                         // delete history entries
00383                         require_once("classes/class.ilHistory.php");
00384                         ilHistory::_removeEntriesForObject($this->getId());
00385                         
00386                         // delete entire directory and its content
00387                         if (@is_dir($this->getDirectory()))
00388                         {
00389                                 ilUtil::delDir($this->getDirectory());
00390                         }
00391 
00392                         return true;
00393                 }
00394 
00395                 return false;
00396         }
00397 
00405         function export($a_target_dir)
00406         {
00407                 $subdir = "il_".IL_INST_ID."_file_".$this->getId();
00408                 ilUtil::makeDir($a_target_dir."/objects/".$subdir);
00409 
00410                 $filedir = $this->getDirectory($this->getVersion());
00411                 
00412                 if (@!is_dir($filedir))
00413                 {
00414                         $filedir = $this->getDirectory();
00415                 }
00416                 
00417                 ilUtil::rCopy($filedir, $a_target_dir."/objects/".$subdir);
00418         }
00419 
00423         function _deleteAllUsages($a_type, $a_id)
00424         {
00425                 $q = "DELETE FROM file_usage WHERE usage_type='$a_type' AND usage_id='$a_id'";
00426                 $this->ilias->db->query($q);
00427         }
00428 
00432         function _saveUsage($a_mob_id, $a_type, $a_id)
00433         {
00434                 $q = "REPLACE INTO file_usage (id, usage_type, usage_id) VALUES".
00435                         " ('$a_mob_id', '$a_type', '$a_id')";
00436                 $this->ilias->db->query($q);
00437         }
00438 
00442         function getUsages()
00443         {
00444                 global $ilDB;
00445 
00446                 // get usages in learning modules
00447                 $q = "SELECT * FROM file_usage WHERE id = '".$this->getId()."'";
00448                 $us_set = $ilDB->query($q);
00449                 $ret = array();
00450                 while($us_rec = $us_set->fetchRow(DB_FETCHMODE_ASSOC))
00451                 {
00452                         $ret[] = array("type" => $us_rec["usage_type"],
00453                                 "id" => $us_rec["usage_id"]);
00454                 }
00455 
00456                 return $ret;
00457         }
00458 
00467         function _getFilesOfObject($a_type, $a_id)
00468         {
00469                 global $ilDB;
00470 
00471                 // get usages in learning modules
00472                 $q = "SELECT * FROM file_usage WHERE usage_id = ".$ilDB->quote($a_id).
00473                         " AND usage_type = ".$ilDB->quote($a_type);
00474                 $file_set = $ilDB->query($q);
00475                 $ret = array();
00476                 while($file_rec = $file_set->fetchRow(DB_FETCHMODE_ASSOC))
00477                 {
00478                         $ret[$file_rec["id"]] = $file_rec["id"];
00479                 }
00480 
00481                 return $ret;
00482         }
00483 
00484         // TODO: What is this function good for??
00485         function getXMLZip()
00486         {
00487                 global $ilias;
00488 
00489                 $zip = PATH_TO_ZIP;
00490 
00491                 exec($zip.' '.ilUtil::escapeShellArg($this->getDirectory().'/'.$this->getFileName())." ".
00492                          ilUtil::escapeShellArg($this->getDirectory().'/'.'1.zip'));
00493 
00494                 return $this->getDirectory().'/1.zip';
00495         }
00496 } // END class.ilObjFile
00497 ?>

Generated on Fri Dec 13 2013 09:06:34 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1