• 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         var $mode = "object";
00044 
00051         function ilObjFile($a_id = 0,$a_call_by_reference = true)
00052         {
00053                 $this->version = 0;
00054                 $this->type = "file";
00055                 $this->raise_upload_error = true;
00056                 $this->ilObject($a_id,$a_call_by_reference);
00057         }
00058 
00062         function create()
00063         {
00064                 global $ilDB;
00065                 
00066                 parent::create();
00067 
00068                 require_once("classes/class.ilHistory.php");
00069                 ilHistory::_createEntry($this->getId(), "create", $this->getFileName().",1");
00070 
00071                 $q = "INSERT INTO file_data (file_id,file_name,file_type,version,mode) "
00072                         ."VALUES ('".$this->getId()."','"
00073                         .ilUtil::prepareDBString($this->getFileName())."','"
00074                         .$this->getFileType()."','"
00075                         ."1"."',".$ilDB->quote($this->getMode()).")";
00076                 $this->ilias->db->query($q);
00077                 
00078                 // no meta data handling for file list files
00079                 if ($this->getMode() != "filelist")
00080                 {
00081                         $this->createMetaData();
00082                 }
00083         }
00084         
00088         function createMetaData()
00089         {
00090                 parent::createMetaData();
00091                 
00092                 // add technical section with file size and format
00093                 $md_obj =& new ilMD($this->getId(),0,$this->getType());
00094                 $technical = $md_obj->addTechnical();
00095                 $technical->setSize($this->getFileSize());
00096                 $technical->save();
00097                 $format = $technical->addFormat();
00098                 $format->setFormat($this->getFileType());
00099                 $format->save();
00100                 $technical->update();
00101         }
00102         
00114         function MDUpdateListener($a_element)
00115         {
00116                 // handling for general section
00117                 parent::MDUpdateListener($a_element);
00118                 
00119                 // handling for technical section 
00120                 include_once 'Services/MetaData/classes/class.ilMD.php';
00121 //echo "-".$a_element."-";
00122                 switch($a_element)
00123                 {
00124                         case 'Technical':
00125 
00126                                 // Update Format (size is not stored in db)
00127                                 $md = new ilMD($this->getId(),0, $this->getType());
00128                                 if(!is_object($md_technical = $md->getTechnical()))
00129                                 {
00130                                         return false;
00131                                 }
00132 
00133                                 foreach($md_technical->getFormatIds() as $id)
00134                                 {
00135                                         $md_format = $md_technical->getFormat($id);
00136                                         ilObjFile::_writeFileType($this->getId(),$md_format->getFormat());
00137                                         $this->setFileType($md_format->getFormat());
00138                                         break;
00139                                 }
00140 
00141                                 break;
00142 
00143                         default:
00144                 }
00145                 return true;
00146         }
00147 
00148 
00149         function getDirectory($a_version = 0)
00150         {
00151                 $version_subdir = "";
00152 
00153                 if ($a_version)
00154                 {
00155                         $version_subdir = "/".sprintf("%03d", $a_version);
00156                 }
00157                 
00158                 return ilUtil::getDataDir()."/files/file_".$this->getId().$version_subdir;
00159         }
00160 
00161         function createDirectory()
00162         {
00163                 ilUtil::makeDir($this->getDirectory());
00164         }
00165         
00166         function raiseUploadError($a_raise = true)
00167         {
00168                 $this->raise_upload_error = $a_raise;
00169         }
00170 
00171         function getUploadFile($a_upload_file, $a_filename)
00172         {
00173                 $this->setVersion($this->getVersion() + 1);
00174 
00175                 if (@!is_dir($this->getDirectory($this->getVersion())))
00176                 {
00177                         ilUtil::makeDir($this->getDirectory($this->getVersion()));
00178                 }
00179 
00180                 $file = $this->getDirectory($this->getVersion())."/".$a_filename;
00181                 //move_uploaded_file($a_upload_file, $file);
00182                 ilUtil::moveUploadedFile($a_upload_file, $a_filename, $file, $this->raise_upload_error);
00183         }
00184 
00188         function replaceFile($a_upload_file, $a_filename)
00189         {
00190                 //$this->clearDataDirectory();          // ! This has to be changed, if multiple versions should be supported
00191                 $this->getUploadFile($a_upload_file, $a_filename);
00192                 
00193                 require_once("classes/class.ilHistory.php");
00194                 ilHistory::_createEntry($this->getId(), "replace",
00195                         $a_filename.",".$this->getVersion());
00196         }
00197 
00198 
00202         function copy($a_source,$a_destination)
00203         {
00204                 return copy($a_source,$this->getDirectory()."/".$a_destination);
00205         }
00206         
00210         function clearDataDirectory()
00211         {
00212                 ilUtil::delDir($this->getDirectory());
00213                 $this->createDirectory();
00214         }
00215 
00219         function read()
00220         {
00221                 parent::read();
00222 
00223                 $q = "SELECT * FROM file_data WHERE file_id = '".$this->getId()."'";
00224                 $r = $this->ilias->db->query($q);
00225                 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
00226 
00227                 $this->setFileName($row->file_name);
00228                 $this->setFileType($row->file_type);
00229                 $this->setVersion($row->version);
00230                 $this->setMode($row->mode);
00231         }
00232 
00236         function update()
00237         {
00238                 global $ilDB;
00239                 
00240                 // no meta data handling for file list files
00241                 if ($this->getMode() != "filelist")
00242                 {
00243                         $this->updateMetaData();
00244                 }
00245                 parent::update();
00246                 
00247                 $q = "UPDATE file_data SET file_name = '".ilUtil::prepareDBString($this->getFileName()).
00248                         "', file_type = '".$this->getFiletype()."' ".
00249                         ", version = '".$this->getVersion()."' ".
00250                         ", mode = ".$ilDB->quote($this->getMode())." ".
00251                         "WHERE file_id = '".$this->getId()."'";
00252                 $this->ilias->db->query($q);
00253                 
00254                 return true;
00255         }
00256         
00260         function updateMetaData()
00261         {
00262                 parent::updateMetaData();
00263                 
00264                 // add technical section with file size and format
00265                 $md_obj =& new ilMD($this->getId(),0,$this->getType());
00266                 if(!is_object($technical = $md_obj->getTechnical()))
00267                 {
00268                         $technical = $md_obj->addTechnical();
00269                         $technical->save();
00270                 }
00271                 $technical->setSize($this->getFileSize());
00272                 
00273                 $format_ids = $technical->getFormatIds();
00274                 if (count($format_ids) > 0)
00275                 {
00276                         $format = $technical->getFormat($format_ids[0]);
00277                         $format->setFormat($this->getFileType());
00278                         $format->update();
00279                 }
00280                 else
00281                 {
00282                         $format = $technical->addFormat();
00283                         $format->setFormat($this->getFileType());
00284                         $format->save();
00285                 }
00286                 $technical->update();
00287         }
00288 
00292         function setFileName($a_name)
00293         {
00294                 $this->filename = $a_name;
00295         }
00296 
00297         function getFileName()
00298         {
00299                 return $this->filename;
00300         }
00301 
00302         function setFileType($a_type)
00303         {
00304                 $this->filetype = $a_type;
00305         }
00306 
00307         function getFileType()
00308         {
00309                 return $this->filetype;
00310         }
00311 
00312         function setFileSize($a_size)
00313         {
00314                 $this->filesize = $a_size;
00315         }
00316 
00317         function getFileSize()
00318         {
00319                 return $this->filesize;
00320         }
00321         
00322         function setVersion($a_version)
00323         {
00324                 $this->version = $a_version;
00325         }
00326 
00327         function getVersion()
00328         {
00329                 return $this->version;
00330         }
00331         
00337         function setMode($a_mode)
00338         {
00339                 $this->mode = $a_mode;
00340         }
00341 
00347         function getMode()
00348         {
00349                 return $this->mode;
00350         }
00351         
00352         function _writeFileType($a_id ,$a_format)
00353         {
00354                 global $ilDB;
00355                 
00356                 $q = "UPDATE file_data SET ".
00357                         " file_type = ".$ilDB->quote($a_format).
00358                         " WHERE file_id = ".$ilDB->quote($a_id);
00359                 $ilDB->query($q);
00360                 
00361         }
00362 
00363         function _lookupFileName($a_id)
00364         {
00365                 global $ilDB;
00366 
00367                 $q = "SELECT * FROM file_data WHERE file_id = '".$a_id."'";
00368                 $r = $ilDB->query($q);
00369                 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
00370 
00371                 return ilUtil::stripSlashes($row->file_name);
00372         }
00373 
00374 
00375         function _lookupFileSize($a_id, $a_as_string = false)
00376         {
00377                 global $ilDB;
00378 
00379                 $q = "SELECT * FROM file_data WHERE file_id = '".$a_id."'";
00380                 $r = $ilDB->query($q);
00381                 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
00382 
00383                 $file = ilUtil::getDataDir()."/files/file_".$a_id."/".$row->file_name;
00384 
00385                 if (@!is_file($file))
00386                 {
00387                         $version_subdir = "/".sprintf("%03d", ilObjFile::_lookupVersion($a_id));
00388                         $file = ilUtil::getDataDir()."/files/file_".$a_id.$version_subdir."/".$row->file_name;
00389                 }
00390 
00391                 if (is_file($file))
00392                 {
00393                         $size = filesize($file);
00394                 }
00395                 else
00396                 {
00397                         $size = 0;
00398                 }
00399                 
00400                 if ($a_as_string)
00401                 {
00402                         if ($size > 1000000)
00403                         {
00404                                 return round($size/1000000,2)." MB";
00405                         }
00406                         else if ($size > 1000)
00407                         {
00408                                 return round($size/1000,2)." KBytes";
00409                         }
00410                         else
00411                         {
00412                                 return $size." Bytes";
00413                         }
00414                         
00415                 }
00416                 
00417                 return $size;
00418         }
00419         
00423         function _lookupVersion($a_id)
00424         {
00425                 global $ilDB;
00426 
00427                 $q = "SELECT * FROM file_data WHERE file_id = '".$a_id."'";
00428                 $r = $ilDB->query($q);
00429                 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
00430 
00431                 return ilUtil::stripSlashes($row->version);
00432         }
00433 
00434         function sendFile($a_hist_entry_id = null)
00435         {       
00436                 if (is_null($a_hist_entry_id))
00437                 {
00438                         $file = $this->getDirectory($this->getVersion())."/".$this->getFileName();
00439 
00440                         // if not found lookup for file in file object's main directory for downward c  ompability
00441                         if (@!is_file($file))
00442                         {
00443                                 $file = $this->getDirectory()."/".$this->getFileName();
00444                         }
00445                 }
00446                 else
00447                 {
00448                         require_once("classes/class.ilHistory.php");
00449                         $entry = ilHistory::_getEntryByHistoryID($a_hist_entry_id);
00450                         
00451                         if ($entry === false)
00452                         {
00453                                 echo "3";return false;
00454                         }
00455 
00456                         $data = explode(",",$entry["info_params"]);
00457                         
00458                         // bugfix: first created file had no version number
00459                         // this is a workaround for all files created before the bug was fixed
00460                         if (empty($data[1]))
00461                         {
00462                                 $data[1] = "1";
00463                         }
00464 
00465                         $file = $this->getDirectory($data[1])."/".$data[0];
00466                         
00467                         // if not found lookup for file in file object's main directory for downward compability
00468                         if (@!is_file($file))
00469                         {
00470                                 $file = $this->getDirectory()."/".$data[0];
00471                         }
00472 
00473                         if (@is_file($file))
00474                         {
00475                                 ilUtil::deliverFile($file, $data[0]);
00476                                 return true;
00477                         }
00478                 }
00479 
00480                 if (@is_file($file))
00481                 {
00482                         ilUtil::deliverFile($file, $this->getFileName());
00483                         return true;
00484                 }
00485 
00486                 return false;
00487         }
00488 
00489         function ilClone($a_parent_ref)
00490         {
00491                 global $ilDB;
00492                 
00493                 // always call parent clone function first!!
00494                 $new_ref_id = parent::ilClone($a_parent_ref);
00495 
00496                 $fileObj =& $this->ilias->obj_factory->getInstanceByRefId($new_ref_id);
00497                 $fileObj->createDirectory();
00498                 
00499                 // copy all versions of file
00500                 ilUtil::rCopy($this->getDirectory(),$fileObj->getDirectory());
00501                 //copy($this->getDirectory()."/".$this->getFileName(),$fileObj->getDirectory()."/".$this->getFileName());
00502 
00503                 $q = "INSERT INTO file_data (file_id,file_name,file_type,version,mode) VALUES ('"
00504                         .$fileObj->getId()."','"
00505                         .ilUtil::prepareDBString($this->getFileName())."','"
00506                         .$this->getFileType()."','".$this->getVersion()
00507                         ."',".$ilDB->quote($this->getMode()).")";
00508 
00509                 $this->ilias->db->query($q);
00510 
00511                 // copy history entries
00512                 require_once("classes/class.ilHistory.php");
00513                 ilHistory::_copyEntriesForObject($this->getId(),$fileObj->getId());
00514 
00515                 // dump object
00516                 unset($fileObj);
00517 
00518                 // ... and finally always return new reference ID!!
00519                 return $new_ref_id;
00520         }
00521 
00528         function delete()
00529         {
00530                 // check, if file is used somewhere
00531                 $usages = $this->getUsages();
00532 
00533                 if (count($usages) == 0)
00534                 {
00535                         // always call parent delete function first!!
00536                         if (!parent::delete())
00537                         {
00538                                 return false;
00539                         }
00540 
00541                         // delete file data entry
00542                         $q = "DELETE FROM file_data WHERE file_id = '".$this->getId()."'";
00543                         $this->ilias->db->query($q);
00544                         
00545                         // delete history entries
00546                         require_once("classes/class.ilHistory.php");
00547                         ilHistory::_removeEntriesForObject($this->getId());
00548                         
00549                         // delete entire directory and its content
00550                         if (@is_dir($this->getDirectory()))
00551                         {
00552                                 ilUtil::delDir($this->getDirectory());
00553                         }
00554                         
00555                         // delete meta data
00556                         if ($this->getMode() != "filelist")
00557                         {
00558                                 $this->deleteMetaData();
00559                         }
00560 
00561                         return true;
00562                 }
00563 
00564                 return false;
00565         }
00566 
00574         function export($a_target_dir)
00575         {
00576                 $subdir = "il_".IL_INST_ID."_file_".$this->getId();
00577                 ilUtil::makeDir($a_target_dir."/objects/".$subdir);
00578 
00579                 $filedir = $this->getDirectory($this->getVersion());
00580                 
00581                 if (@!is_dir($filedir))
00582                 {
00583                         $filedir = $this->getDirectory();
00584                 }
00585                 
00586                 ilUtil::rCopy($filedir, $a_target_dir."/objects/".$subdir);
00587         }
00588 
00592         function _deleteAllUsages($a_type, $a_id)
00593         {
00594                 $q = "DELETE FROM file_usage WHERE usage_type='$a_type' AND usage_id='$a_id'";
00595                 $this->ilias->db->query($q);
00596         }
00597 
00601         function _saveUsage($a_mob_id, $a_type, $a_id)
00602         {
00603                 $q = "REPLACE INTO file_usage (id, usage_type, usage_id) VALUES".
00604                         " ('$a_mob_id', '$a_type', '$a_id')";
00605                 $this->ilias->db->query($q);
00606         }
00607 
00611         function getUsages()
00612         {
00613                 global $ilDB;
00614 
00615                 // get usages in learning modules
00616                 $q = "SELECT * FROM file_usage WHERE id = '".$this->getId()."'";
00617                 $us_set = $ilDB->query($q);
00618                 $ret = array();
00619                 while($us_rec = $us_set->fetchRow(DB_FETCHMODE_ASSOC))
00620                 {
00621                         $ret[] = array("type" => $us_rec["usage_type"],
00622                                 "id" => $us_rec["usage_id"]);
00623                 }
00624 
00625                 return $ret;
00626         }
00627 
00636         function _getFilesOfObject($a_type, $a_id)
00637         {
00638                 global $ilDB;
00639 
00640                 // get usages in learning modules
00641                 $q = "SELECT * FROM file_usage WHERE usage_id = ".$ilDB->quote($a_id).
00642                         " AND usage_type = ".$ilDB->quote($a_type);
00643                 $file_set = $ilDB->query($q);
00644                 $ret = array();
00645                 while($file_rec = $file_set->fetchRow(DB_FETCHMODE_ASSOC))
00646                 {
00647                         $ret[$file_rec["id"]] = $file_rec["id"];
00648                 }
00649 
00650                 return $ret;
00651         }
00652 
00653         // TODO: What is this function good for??
00654         function getXMLZip()
00655         {
00656                 global $ilias;
00657 
00658                 $zip = PATH_TO_ZIP;
00659 
00660                 exec($zip.' '.ilUtil::escapeShellArg($this->getDirectory().'/'.$this->getFileName())." ".
00661                          ilUtil::escapeShellArg($this->getDirectory().'/'.'1.zip'));
00662 
00663                 return $this->getDirectory().'/1.zip';
00664         }
00665         
00666         function _goto($a_target)
00667         {
00668                 global $ilAccess, $ilErr, $lng;
00669 
00670                 if ($ilAccess->checkAccess("read", "", $a_target))
00671                 {
00672                         $_GET["cmd"] = "frameset";
00673                         $_GET["ref_id"] = $a_target;
00674                 }
00675                 else
00676                 {
00677                         $ilErr->raiseError($lng->txt("msg_no_perm_read"), $ilErr->FATAL);
00678                 }
00679         }
00680 
00681 } // END class.ilObjFile
00682 ?>

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