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.ilObject.php";
00025 include_once('Modules/File/classes/class.ilFSStorageFile.php');
00026
00038 class ilObjFile extends ilObject
00039 {
00040 var $filename;
00041 var $filetype;
00042 var $filemaxsize = "20000000";
00043 var $raise_upload_error;
00044 var $mode = "object";
00045
00046 private $file_storage = null;
00047
00054 function ilObjFile($a_id = 0,$a_call_by_reference = true)
00055 {
00056 $this->version = 0;
00057 $this->type = "file";
00058 $this->raise_upload_error = true;
00059 $this->ilObject($a_id,$a_call_by_reference);
00060
00061 if($this->getId())
00062 {
00063 $this->initFileStorage();
00064 }
00065 }
00066
00072 function create($a_upload = false)
00073 {
00074 global $ilDB;
00075
00076 $new_id = parent::create();
00077
00078 $this->initFileStorage();
00079 $this->file_storage->create();
00080
00081 if($a_upload)
00082 {
00083 return $new_id;
00084 }
00085
00086
00087 require_once("classes/class.ilHistory.php");
00088 ilHistory::_createEntry($this->getId(), "create", $this->getFileName().",1");
00089 $this->addNewsNotification("file_created");
00090 $default_visibility =
00091 ilNewsItem::_getDefaultVisibilityForRefId($_GET["ref_id"]);
00092 if ($default_visibility == "public")
00093 {
00094 ilBlockSetting::_write("news", "public_notifications",
00095 1, 0, $this->getId());
00096 }
00097
00098 $q = "INSERT INTO file_data (file_id,file_name,file_type,file_size,version,mode) "
00099 ."VALUES (".$ilDB->quote($this->getId()).","
00100 .$ilDB->quote($this->getFileName()).","
00101 .$ilDB->quote($this->getFileType()).","
00102 .$ilDB->quote($this->getFileSize()).","
00103 .$ilDB->quote("1").",".$ilDB->quote($this->getMode()).")";
00104 $this->ilias->db->query($q);
00105
00106
00107 if ($this->getMode() != "filelist")
00108 {
00109 $this->createMetaData();
00110 }
00111 return $new_id;
00112 }
00113
00117 function createMetaData()
00118 {
00119 parent::createMetaData();
00120
00121
00122 $md_obj =& new ilMD($this->getId(),0,$this->getType());
00123 $technical = $md_obj->addTechnical();
00124 $technical->setSize($this->getFileSize());
00125 $technical->save();
00126 $format = $technical->addFormat();
00127 $format->setFormat($this->getFileType());
00128 $format->save();
00129 $technical->update();
00130 }
00131
00143 function MDUpdateListener($a_element)
00144 {
00145
00146 parent::MDUpdateListener($a_element);
00147
00148
00149 include_once 'Services/MetaData/classes/class.ilMD.php';
00150
00151 switch($a_element)
00152 {
00153 case 'Technical':
00154
00155
00156 $md = new ilMD($this->getId(),0, $this->getType());
00157 if(!is_object($md_technical = $md->getTechnical()))
00158 {
00159 return false;
00160 }
00161
00162 foreach($md_technical->getFormatIds() as $id)
00163 {
00164 $md_format = $md_technical->getFormat($id);
00165 ilObjFile::_writeFileType($this->getId(),$md_format->getFormat());
00166 $this->setFileType($md_format->getFormat());
00167 break;
00168 }
00169
00170 break;
00171
00172 default:
00173 }
00174 return true;
00175 }
00176
00177
00178 function getDirectory($a_version = 0)
00179 {
00180 $version_subdir = "";
00181
00182 if ($a_version)
00183 {
00184 $version_subdir = "/".sprintf("%03d", $a_version);
00185 }
00186
00187 if(!is_object($this->file_storage))
00188 {
00189 $this->initFileStorage();
00190 }
00191
00192 return $this->file_storage->getAbsolutePath().'/'.$version_subdir;
00193 }
00194
00195 function createDirectory()
00196 {
00197 ilUtil::makeDirParents($this->getDirectory());
00198 }
00199
00200 function raiseUploadError($a_raise = true)
00201 {
00202 $this->raise_upload_error = $a_raise;
00203 }
00204
00205 function getUploadFile($a_upload_file, $a_filename)
00206 {
00207 $this->setVersion($this->getVersion() + 1);
00208
00209 if (@!is_dir($this->getDirectory($this->getVersion())))
00210 {
00211 ilUtil::makeDirParents($this->getDirectory($this->getVersion()));
00212 }
00213
00214 $file = $this->getDirectory($this->getVersion())."/".$a_filename;
00215
00216 ilUtil::moveUploadedFile($a_upload_file, $a_filename, $file, $this->raise_upload_error);
00217 }
00218
00222 function replaceFile($a_upload_file, $a_filename)
00223 {
00224
00225 $this->getUploadFile($a_upload_file, $a_filename);
00226
00227 require_once("classes/class.ilHistory.php");
00228 ilHistory::_createEntry($this->getId(), "replace",
00229 $a_filename.",".$this->getVersion());
00230 $this->setFilename($a_filename);
00231 $this->addNewsNotification("file_updated");
00232 }
00233
00234
00238 function copy($a_source,$a_destination)
00239 {
00240 return copy($a_source,$this->getDirectory()."/".$a_destination);
00241 }
00242
00246 function clearDataDirectory()
00247 {
00248 ilUtil::delDir($this->getDirectory());
00249 $this->createDirectory();
00250 }
00251
00255 function read()
00256 {
00257 global $ilDB;
00258
00259 parent::read();
00260
00261 $q = "SELECT * FROM file_data WHERE file_id = ".$ilDB->quote($this->getId());
00262 $r = $this->ilias->db->query($q);
00263 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
00264
00265 $this->setFileName($row->file_name);
00266 $this->setFileType($row->file_type);
00267 $this->setFileSize($row->file_size);
00268 $this->setVersion($row->version);
00269 $this->setMode($row->mode);
00270
00271 $this->initFileStorage();
00272 }
00273
00277 function update()
00278 {
00279 global $ilDB;
00280
00281
00282 if ($this->getMode() != "filelist")
00283 {
00284 $this->updateMetaData();
00285 }
00286 parent::update();
00287
00288 $q = "UPDATE file_data SET file_name = ".$ilDB->quote($this->getFileName()).
00289 ", file_type = ".$ilDB->quote($this->getFiletype())." ".
00290 ", file_size = ".$ilDB->quote($this->getFileSize())." ".
00291 ", version = ".$ilDB->quote($this->getVersion())." ".
00292 ", mode = ".$ilDB->quote($this->getMode())." ".
00293 "WHERE file_id = ".$ilDB->quote($this->getId());
00294 $this->ilias->db->query($q);
00295
00296 return true;
00297 }
00298
00302 function updateMetaData()
00303 {
00304 parent::updateMetaData();
00305
00306
00307 $md_obj =& new ilMD($this->getId(),0,$this->getType());
00308 if(!is_object($technical = $md_obj->getTechnical()))
00309 {
00310 $technical = $md_obj->addTechnical();
00311 $technical->save();
00312 }
00313 $technical->setSize($this->getFileSize());
00314
00315 $format_ids = $technical->getFormatIds();
00316 if (count($format_ids) > 0)
00317 {
00318 $format = $technical->getFormat($format_ids[0]);
00319 $format->setFormat($this->getFileType());
00320 $format->update();
00321 }
00322 else
00323 {
00324 $format = $technical->addFormat();
00325 $format->setFormat($this->getFileType());
00326 $format->save();
00327 }
00328 $technical->update();
00329 }
00330
00334 function setFileName($a_name)
00335 {
00336 $this->filename = $a_name;
00337 }
00338
00339 function getFileName()
00340 {
00341 return $this->filename;
00342 }
00343
00344 function setFileType($a_type)
00345 {
00346 $this->filetype = $a_type;
00347 }
00348
00349 function getFileType()
00350 {
00351 return $this->filetype;
00352 }
00353
00354 function setFileSize($a_size)
00355 {
00356 $this->filesize = $a_size;
00357 }
00358
00359 function getFileSize()
00360 {
00361 return $this->filesize;
00362 }
00363
00364 function setVersion($a_version)
00365 {
00366 $this->version = $a_version;
00367 }
00368
00369 function getVersion()
00370 {
00371 return $this->version;
00372 }
00373
00379 function setMode($a_mode)
00380 {
00381 $this->mode = $a_mode;
00382 }
00383
00389 function getMode()
00390 {
00391 return $this->mode;
00392 }
00393
00394 function _writeFileType($a_id ,$a_format)
00395 {
00396 global $ilDB;
00397
00398 $q = "UPDATE file_data SET ".
00399 " file_type = ".$ilDB->quote($a_format).
00400 " WHERE file_id = ".$ilDB->quote($a_id);
00401 $ilDB->query($q);
00402
00403 }
00404
00405 function _lookupFileName($a_id)
00406 {
00407 global $ilDB;
00408
00409 $q = "SELECT * FROM file_data WHERE file_id = ".$ilDB->quote($a_id);
00410 $r = $ilDB->query($q);
00411 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
00412
00413 return ilUtil::stripSlashes($row->file_name);
00414 }
00415
00416
00417 function _lookupFileSize($a_id, $a_as_string = false)
00418 {
00419 global $ilDB;
00420
00421 $q = "SELECT * FROM file_data WHERE file_id = ".$ilDB->quote($a_id);
00422 $r = $ilDB->query($q);
00423 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
00424
00425 $size = $row->file_size;
00426 if ($a_as_string)
00427 {
00428 if ($size > 1000000)
00429 {
00430 return round($size/1000000,1)." MB";
00431 }
00432 else if ($size > 1000)
00433 {
00434 return round($size/1000,1)." KB";
00435 }
00436 else
00437 {
00438 return $size." Bytes";
00439 }
00440
00441 }
00442 return $size;
00443 }
00444
00448 function _lookupVersion($a_id)
00449 {
00450 include_once("./Modules/File/classes/class.ilObjFileAccess.php");
00451 return ilObjFileAccess::_lookupVersion($a_id);
00452 }
00453
00457 function determineFileSize()
00458 {
00459 $file = $this->getDirectory($this->getVersion())."/".$this->getFileName();
00460
00461
00462 if (@!is_file($file))
00463 {
00464 $file = $this->getDirectory()."/".$this->getFileName();
00465 }
00466
00467 $size = @filesize($file);
00468 if ($size > 0)
00469 {
00470 $this->setFilesize($size);
00471 }
00472
00473 return $size;
00474 }
00475
00476 function sendFile($a_hist_entry_id = null)
00477 {
00478 if (is_null($a_hist_entry_id))
00479 {
00480 $file = $this->getDirectory($this->getVersion())."/".$this->getFileName();
00481
00482
00483 if (@!is_file($file))
00484 {
00485 $file = $this->getDirectory()."/".$this->getFileName();
00486 }
00487 }
00488 else
00489 {
00490 require_once("classes/class.ilHistory.php");
00491 $entry = ilHistory::_getEntryByHistoryID($a_hist_entry_id);
00492
00493 if ($entry === false)
00494 {
00495 echo "3";return false;
00496 }
00497
00498 $data = explode(",",$entry["info_params"]);
00499
00500
00501
00502 if (empty($data[1]))
00503 {
00504 $data[1] = "1";
00505 }
00506
00507 $file = $this->getDirectory($data[1])."/".$data[0];
00508
00509
00510 if (@!is_file($file))
00511 {
00512 $file = $this->getDirectory()."/".$data[0];
00513 }
00514
00515 if (@is_file($file))
00516 {
00517 ilUtil::deliverFile($file, $data[0]);
00518 return true;
00519 }
00520 }
00521
00522 if (@is_file($file))
00523 {
00524 ilUtil::deliverFile($file, $this->getFileName());
00525 return true;
00526 }
00527
00528 return false;
00529 }
00530
00531
00540 public function cloneObject($a_target_id,$a_copy_id = 0)
00541 {
00542 global $ilDB;
00543
00544 $new_obj = parent::cloneObject($a_target_id,$a_copy_id);
00545 $new_obj->createDirectory();
00546 $this->cloneMetaData($new_obj);
00547
00548
00549 ilUtil::rCopy($this->getDirectory(),$new_obj->getDirectory());
00550
00551
00552 $query = "INSERT INTO file_data (file_id,file_name,file_type,file_size,version,mode) VALUES (".
00553 $ilDB->quote($new_obj->getId()).",".
00554 $ilDB->quote($this->getFileName()).",".
00555 $ilDB->quote($this->getFileType()).",".
00556 $ilDB->quote($this->getFileSize()).", ".
00557 $ilDB->quote($this->getVersion()).", ".
00558 $ilDB->quote($this->getMode()).")";
00559 $ilDB->query($query);
00560
00561
00562 require_once("classes/class.ilHistory.php");
00563 ilHistory::_copyEntriesForObject($this->getId(),$new_obj->getId());
00564
00565
00566 $new_obj->addNewsNotification("file_created");
00567
00568 return $new_obj;
00569 }
00570
00571
00578 function delete()
00579 {
00580 global $ilDB;
00581
00582
00583 $usages = $this->getUsages();
00584
00585 if (count($usages) == 0)
00586 {
00587
00588 if (!parent::delete())
00589 {
00590 return false;
00591 }
00592
00593
00594 $q = "DELETE FROM file_data WHERE file_id = ".$ilDB->quote($this->getId());
00595 $this->ilias->db->query($q);
00596
00597
00598 require_once("classes/class.ilHistory.php");
00599 ilHistory::_removeEntriesForObject($this->getId());
00600
00601
00602 if (@is_dir($this->getDirectory()))
00603 {
00604 ilUtil::delDir($this->getDirectory());
00605 }
00606
00607
00608 if ($this->getMode() != "filelist")
00609 {
00610 $this->deleteMetaData();
00611 }
00612
00613 return true;
00614 }
00615
00616 return false;
00617 }
00618
00626 function export($a_target_dir)
00627 {
00628 $subdir = "il_".IL_INST_ID."_file_".$this->getId();
00629 ilUtil::makeDir($a_target_dir."/objects/".$subdir);
00630
00631 $filedir = $this->getDirectory($this->getVersion());
00632
00633 if (@!is_dir($filedir))
00634 {
00635 $filedir = $this->getDirectory();
00636 }
00637
00638 ilUtil::rCopy($filedir, $a_target_dir."/objects/".$subdir);
00639 }
00640
00644 function _deleteAllUsages($a_type, $a_id)
00645 {
00646 global $ilDB;
00647
00648 $q = "DELETE FROM file_usage WHERE usage_type=".$ilDB->quote($a_type)." AND usage_id=".$ilDB->quote($a_id);
00649 $this->ilias->db->query($q);
00650 }
00651
00655 function _saveUsage($a_mob_id, $a_type, $a_id)
00656 {
00657 global $ilDB;
00658
00659 $q = "REPLACE INTO file_usage (id, usage_type, usage_id) VALUES".
00660 " (".$ilDB->quote($a_mob_id).",".$ilDB->quote($a_type).",".$ilDB->quote($a_id).")";
00661 $this->ilias->db->query($q);
00662 }
00663
00667 function getUsages()
00668 {
00669 global $ilDB;
00670
00671
00672 $q = "SELECT * FROM file_usage WHERE id = ".$ilDB->quote($this->getId());
00673 $us_set = $ilDB->query($q);
00674 $ret = array();
00675 while($us_rec = $us_set->fetchRow(DB_FETCHMODE_ASSOC))
00676 {
00677 $ret[] = array("type" => $us_rec["usage_type"],
00678 "id" => $us_rec["usage_id"]);
00679 }
00680
00681 return $ret;
00682 }
00683
00692 function _getFilesOfObject($a_type, $a_id)
00693 {
00694 global $ilDB;
00695
00696
00697 $q = "SELECT * FROM file_usage WHERE usage_id = ".$ilDB->quote($a_id).
00698 " AND usage_type = ".$ilDB->quote($a_type);
00699 $file_set = $ilDB->query($q);
00700 $ret = array();
00701 while($file_rec = $file_set->fetchRow(DB_FETCHMODE_ASSOC))
00702 {
00703 $ret[$file_rec["id"]] = $file_rec["id"];
00704 }
00705
00706 return $ret;
00707 }
00708
00709
00710 function getXMLZip()
00711 {
00712 global $ilias;
00713
00714 $zip = PATH_TO_ZIP;
00715
00716 exec($zip.' '.ilUtil::escapeShellArg($this->getDirectory().'/'.$this->getFileName())." ".
00717 ilUtil::escapeShellArg($this->getDirectory().'/'.'1.zip'));
00718
00719 return $this->getDirectory().'/1.zip';
00720 }
00721
00722 function addNewsNotification($a_lang_var)
00723 {
00724 global $ilUser;
00725
00726
00727 include_once("./Services/News/classes/class.ilNewsItem.php");
00728 include_once("./Modules/File/classes/class.ilObjFileAccess.php");
00729 $news_item = new ilNewsItem();
00730 $news_item->setContext($this->getId(), $this->getType());
00731 $news_item->setPriority(NEWS_NOTICE);
00732 $news_item->setTitle($a_lang_var);
00733 $news_item->setContentIsLangVar(true);
00734 if ($this->getDescription() != "")
00735 {
00736 $news_item->setContent(
00737 "<p>".
00738 $this->getDescription()."</p>");
00739 }
00740 $news_item->setUserId($ilUser->getId());
00741 $news_item->setVisibility(NEWS_USERS);
00742 $news_item->create();
00743 }
00744
00751 public function initFileStorage()
00752 {
00753 $this->file_storage = new ilFSStorageFile($this->getId());
00754 return true;
00755 }
00765 function storeUnzipedFile($a_upload_file, $a_filename)
00766 {
00767 $this->setVersion($this->getVersion() + 1);
00768
00769 if (@!is_dir($this->getDirectory($this->getVersion())))
00770 {
00771 ilUtil::makeDir($this->getDirectory($this->getVersion()));
00772 }
00773
00774 $file = $this->getDirectory($this->getVersion())."/".$a_filename;
00775
00776 rename($a_upload_file, $file);
00777 }
00778
00779 }
00780 ?>