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 "class.ilObject.php";
00025
00037 class ilObjFile extends ilObject
00038 {
00039 var $filename;
00040 var $filetype;
00041 var $filemaxsize = "20000000";
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
00079 if ($this->getMode() != "filelist")
00080 {
00081 $this->createMetaData();
00082 }
00083 }
00084
00088 function createMetaData()
00089 {
00090 parent::createMetaData();
00091
00092
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
00117 parent::MDUpdateListener($a_element);
00118
00119
00120 include_once 'Services/MetaData/classes/class.ilMD.php';
00121
00122 switch($a_element)
00123 {
00124 case 'Technical':
00125
00126
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
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
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
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
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 include_once("classes/class.ilObjFileAccess.php");
00378 return ilObjFileAccess::_lookupFileSize($a_id, $a_as_string);
00379 }
00380
00384 function _lookupVersion($a_id)
00385 {
00386 include_once("classes/class.ilObjFileAccess.php");
00387 return ilObjFileAccess::_lookupVersion($a_id);
00388 }
00389
00390 function sendFile($a_hist_entry_id = null)
00391 {
00392 if (is_null($a_hist_entry_id))
00393 {
00394 $file = $this->getDirectory($this->getVersion())."/".$this->getFileName();
00395
00396
00397 if (@!is_file($file))
00398 {
00399 $file = $this->getDirectory()."/".$this->getFileName();
00400 }
00401 }
00402 else
00403 {
00404 require_once("classes/class.ilHistory.php");
00405 $entry = ilHistory::_getEntryByHistoryID($a_hist_entry_id);
00406
00407 if ($entry === false)
00408 {
00409 echo "3";return false;
00410 }
00411
00412 $data = explode(",",$entry["info_params"]);
00413
00414
00415
00416 if (empty($data[1]))
00417 {
00418 $data[1] = "1";
00419 }
00420
00421 $file = $this->getDirectory($data[1])."/".$data[0];
00422
00423
00424 if (@!is_file($file))
00425 {
00426 $file = $this->getDirectory()."/".$data[0];
00427 }
00428
00429 if (@is_file($file))
00430 {
00431 ilUtil::deliverFile($file, $data[0]);
00432 return true;
00433 }
00434 }
00435
00436 if (@is_file($file))
00437 {
00438 ilUtil::deliverFile($file, $this->getFileName());
00439 return true;
00440 }
00441
00442 return false;
00443 }
00444
00445 function ilClone($a_parent_ref)
00446 {
00447 global $ilDB;
00448
00449
00450 $new_ref_id = parent::ilClone($a_parent_ref);
00451
00452 $fileObj =& $this->ilias->obj_factory->getInstanceByRefId($new_ref_id);
00453 $fileObj->createDirectory();
00454
00455
00456 ilUtil::rCopy($this->getDirectory(),$fileObj->getDirectory());
00457
00458
00459 $q = "INSERT INTO file_data (file_id,file_name,file_type,version,mode) VALUES ('"
00460 .$fileObj->getId()."','"
00461 .ilUtil::prepareDBString($this->getFileName())."','"
00462 .$this->getFileType()."','".$this->getVersion()
00463 ."',".$ilDB->quote($this->getMode()).")";
00464
00465 $this->ilias->db->query($q);
00466
00467
00468 require_once("classes/class.ilHistory.php");
00469 ilHistory::_copyEntriesForObject($this->getId(),$fileObj->getId());
00470
00471
00472 unset($fileObj);
00473
00474
00475 return $new_ref_id;
00476 }
00477
00484 function delete()
00485 {
00486
00487 $usages = $this->getUsages();
00488
00489 if (count($usages) == 0)
00490 {
00491
00492 if (!parent::delete())
00493 {
00494 return false;
00495 }
00496
00497
00498 $q = "DELETE FROM file_data WHERE file_id = '".$this->getId()."'";
00499 $this->ilias->db->query($q);
00500
00501
00502 require_once("classes/class.ilHistory.php");
00503 ilHistory::_removeEntriesForObject($this->getId());
00504
00505
00506 if (@is_dir($this->getDirectory()))
00507 {
00508 ilUtil::delDir($this->getDirectory());
00509 }
00510
00511
00512 if ($this->getMode() != "filelist")
00513 {
00514 $this->deleteMetaData();
00515 }
00516
00517 return true;
00518 }
00519
00520 return false;
00521 }
00522
00530 function export($a_target_dir)
00531 {
00532 $subdir = "il_".IL_INST_ID."_file_".$this->getId();
00533 ilUtil::makeDir($a_target_dir."/objects/".$subdir);
00534
00535 $filedir = $this->getDirectory($this->getVersion());
00536
00537 if (@!is_dir($filedir))
00538 {
00539 $filedir = $this->getDirectory();
00540 }
00541
00542 ilUtil::rCopy($filedir, $a_target_dir."/objects/".$subdir);
00543 }
00544
00548 function _deleteAllUsages($a_type, $a_id)
00549 {
00550 $q = "DELETE FROM file_usage WHERE usage_type='$a_type' AND usage_id='$a_id'";
00551 $this->ilias->db->query($q);
00552 }
00553
00557 function _saveUsage($a_mob_id, $a_type, $a_id)
00558 {
00559 $q = "REPLACE INTO file_usage (id, usage_type, usage_id) VALUES".
00560 " ('$a_mob_id', '$a_type', '$a_id')";
00561 $this->ilias->db->query($q);
00562 }
00563
00567 function getUsages()
00568 {
00569 global $ilDB;
00570
00571
00572 $q = "SELECT * FROM file_usage WHERE id = '".$this->getId()."'";
00573 $us_set = $ilDB->query($q);
00574 $ret = array();
00575 while($us_rec = $us_set->fetchRow(DB_FETCHMODE_ASSOC))
00576 {
00577 $ret[] = array("type" => $us_rec["usage_type"],
00578 "id" => $us_rec["usage_id"]);
00579 }
00580
00581 return $ret;
00582 }
00583
00592 function _getFilesOfObject($a_type, $a_id)
00593 {
00594 global $ilDB;
00595
00596
00597 $q = "SELECT * FROM file_usage WHERE usage_id = ".$ilDB->quote($a_id).
00598 " AND usage_type = ".$ilDB->quote($a_type);
00599 $file_set = $ilDB->query($q);
00600 $ret = array();
00601 while($file_rec = $file_set->fetchRow(DB_FETCHMODE_ASSOC))
00602 {
00603 $ret[$file_rec["id"]] = $file_rec["id"];
00604 }
00605
00606 return $ret;
00607 }
00608
00609
00610 function getXMLZip()
00611 {
00612 global $ilias;
00613
00614 $zip = PATH_TO_ZIP;
00615
00616 exec($zip.' '.ilUtil::escapeShellArg($this->getDirectory().'/'.$this->getFileName())." ".
00617 ilUtil::escapeShellArg($this->getDirectory().'/'.'1.zip'));
00618
00619 return $this->getDirectory().'/1.zip';
00620 }
00621
00622 }
00623 ?>