1<?php 2/* 3 +-----------------------------------------------------------------------------+ 4 | ILIAS open source | 5 +-----------------------------------------------------------------------------+ 6 | Copyright (c) 1998-2001 ILIAS open source, University of Cologne | 7 | | 8 | This program is free software; you can redistribute it and/or | 9 | modify it under the terms of the GNU General Public License | 10 | as published by the Free Software Foundation; either version 2 | 11 | of the License, or (at your option) any later version. | 12 | | 13 | This program is distributed in the hope that it will be useful, | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 16 | GNU General Public License for more details. | 17 | | 18 | You should have received a copy of the GNU General Public License | 19 | along with this program; if not, write to the Free Software | 20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 21 +-----------------------------------------------------------------------------+ 22*/ 23 24include_once('Modules/Course/classes/class.ilFSStorageCourse.php'); 25 26/** 27* 28* @author Stefan Meyer <meyer@leifos.com> 29* @version $Id$ 30* 31* @extends Object 32*/ 33 34 35class ilCourseFile 36{ 37 public $ilErr; 38 public $ilDB; 39 public $tree; 40 public $lng; 41 42 public $course_id = null; 43 public $file_id = null; 44 45 /** 46 * @var \ilFSStorageCourse|null 47 */ 48 private $fss_storage = null; 49 50 /** 51 * Constructor 52 * @param int $a_file_id 53 */ 54 public function __construct($a_file_id = null) 55 { 56 global $DIC; 57 58 $ilErr = $DIC['ilErr']; 59 $ilDB = $DIC['ilDB']; 60 $lng = $DIC['lng']; 61 62 $this->ilErr = $ilErr; 63 $this->db = $ilDB; 64 $this->lng = $lng; 65 66 $this->file_id = $a_file_id; 67 $this->__read(); 68 } 69 70 /** 71 * Clone course files 72 * 73 * @access public 74 * @static 75 * 76 * @param int source id 77 * @param int target_id 78 */ 79 public static function _cloneFiles($a_source_id, $a_target_id) 80 { 81 $source = new ilFSStorageCourse($a_source_id); 82 83 foreach (ilCourseFile::_readFilesByCourse($a_source_id) as $file_obj) { 84 $new_file = new ilCourseFile(); 85 $new_file->setCourseId($a_target_id); 86 $new_file->setFileName($file_obj->getFileName()); 87 $new_file->setFileSize($file_obj->getFileSize()); 88 $new_file->setFileType($file_obj->getFileType()); 89 $new_file->create(false); 90 91 $target = new ilFSStorageCourse($a_target_id); 92 $target->initInfoDirectory(); 93 $source->copyFile($file_obj->getAbsolutePath(), $new_file->fss_storage->getInfoDirectory() . '/' . $new_file->getFileId()); 94 } 95 } 96 97 public function setFileId($a_id) 98 { 99 $this->file_id = $a_id; 100 } 101 public function getFileId() 102 { 103 return $this->file_id; 104 } 105 106 public function getCourseId() 107 { 108 return $this->course_id; 109 } 110 public function setCourseId($a_course_id) 111 { 112 $this->course_id = $a_course_id; 113 } 114 115 public function setFileName($a_name) 116 { 117 $this->file_name = $a_name; 118 } 119 public function getFileName() 120 { 121 return $this->file_name; 122 } 123 public function setFileType($a_type) 124 { 125 $this->file_type = $a_type; 126 } 127 public function getFileType() 128 { 129 return $this->file_type; 130 } 131 public function setFileSize($a_size) 132 { 133 $this->file_size = $a_size; 134 } 135 public function getFileSize() 136 { 137 return $this->file_size; 138 } 139 public function setTemporaryName($a_name) 140 { 141 $this->tmp_name = $a_name; 142 } 143 public function getTemporaryName() 144 { 145 return $this->tmp_name; 146 } 147 public function setErrorCode($a_code) 148 { 149 $this->error_code = $a_code; 150 } 151 public function getErrorCode() 152 { 153 return $this->error_code; 154 } 155 156 /** 157 * @return bool|string 158 */ 159 public function getAbsolutePath() 160 { 161 // workaround for "secured" files. 162 if (!$this->fss_storage instanceof \ilFSStorageCourse) { 163 return false; 164 } 165 166 $file = $this->fss_storage->getInfoDirectory() . '/' . $this->getFileId(); 167 if (!file_exists($file)) { 168 $file = $this->fss_storage->getInfoDirectory() . '/' . $this->getFileId() . '.sec'; 169 } 170 if (file_exists($file)) { 171 return $file; 172 } 173 return false; 174 } 175 176 public function getInfoDirectory() 177 { 178 if (is_object($this->fss_storage)) { 179 return $this->fss_storage->getInfoDirectory(); 180 } 181 } 182 183 public function validate() 184 { 185 switch ($this->getErrorCode()) { 186 case UPLOAD_ERR_INI_SIZE: 187 $this->ilErr->appendMessage($this->lng->txt('file_upload_ini_size')); 188 break; 189 case UPLOAD_ERR_FORM_SIZE: 190 $this->ilErr->appendMessage($this->lng->txt('file_upload_form_size')); 191 break; 192 193 case UPLOAD_ERR_PARTIAL: 194 $this->ilErr->appendMessage($this->lng->txt('file_upload_only_partial')); 195 break; 196 197 case UPLOAD_ERR_NO_TMP_DIR: 198 $this->ilErr->appendMessage($this->lng->txt('file_upload_no_tmp_dir')); 199 break; 200 201 // not possible with php 4 202 #case UPLOAD_ERR_CANT_WRITE: 203 # $this->ilErr->appendMessage($this->lng->txt('file_upload_no_write')); 204 # break; 205 206 case UPLOAD_ERR_OK: 207 case UPLOAD_ERR_NO_FILE: 208 default: 209 return true; 210 } 211 } 212 213 public function create($a_upload = true) 214 { 215 global $DIC; 216 217 $ilDB = $DIC['ilDB']; 218 219 if ($this->getErrorCode() != 0) { 220 return false; 221 } 222 223 $next_id = $ilDB->nextId('crs_file'); 224 $query = "INSERT INTO crs_file (file_id,course_id,file_name,file_size,file_type) " . 225 "VALUES( " . 226 $ilDB->quote($next_id, 'integer') . ", " . 227 $ilDB->quote($this->getCourseId(), 'integer') . ", " . 228 $ilDB->quote($this->getFileName(), 'text') . ", " . 229 $ilDB->quote($this->getFileSize(), 'integer') . ", " . 230 $ilDB->quote($this->getFileType(), 'text') . " " . 231 ")"; 232 $res = $ilDB->manipulate($query); 233 $this->setFileId($next_id); 234 235 $this->fss_storage = new ilFSStorageCourse($this->getCourseId()); 236 $this->fss_storage->initInfoDirectory(); 237 238 if ($a_upload) { 239 // now create file 240 ilUtil::moveUploadedFile( 241 $this->getTemporaryName(), 242 $this->getFileName(), 243 $this->fss_storage->getInfoDirectory() . '/' . $this->getFileId() 244 ); 245 } 246 return true; 247 } 248 249 public function delete() 250 { 251 global $DIC; 252 253 $ilDB = $DIC['ilDB']; 254 255 // Delete db entry 256 $query = "DELETE FROM crs_file " . 257 "WHERE file_id = " . $ilDB->quote($this->getFileId(), 'integer') . ""; 258 $res = $ilDB->manipulate($query); 259 260 // Delete file 261 if (file_exists($this->getAbsolutePath())) { 262 unlink($this->getAbsolutePath()); 263 } 264 265 return true; 266 } 267 268 public static function _deleteByCourse($a_course_id) 269 { 270 global $DIC; 271 272 $ilDB = $DIC['ilDB']; 273 274 // delete all course ids and delete assigned files 275 $query = "DELETE FROM crs_file " . 276 "WHERE course_id = " . $ilDB->quote($a_course_id, 'integer') . ""; 277 $res = $ilDB->manipulate($query); 278 279 return true; 280 } 281 282 283 /** 284 * @param int $a_course_id obj_id of course 285 * @return ilCourseFile[] 286 */ 287 public static function _readFilesByCourse($a_course_id) 288 { 289 global $DIC; 290 291 $ilDB = $DIC['ilDB']; 292 293 $query = "SELECT * FROM crs_file " . 294 "WHERE course_id = " . $ilDB->quote($a_course_id, 'integer') . ""; 295 296 $res = $ilDB->query($query); 297 while ($row = $ilDB->fetchObject($res)) { 298 $files[] = new ilCourseFile($row->file_id); 299 } 300 return is_array($files) ? $files : array(); 301 } 302 303 public function __read() 304 { 305 global $DIC; 306 307 $ilDB = $DIC['ilDB']; 308 309 if (!$this->file_id) { 310 return true; 311 } 312 313 // read file data 314 $query = "SELECT * FROM crs_file WHERE file_id = " . $ilDB->quote($this->file_id, 'integer'); 315 $res = $this->db->query($query); 316 while ($row = $ilDB->fetchObject($res)) { 317 $this->setFileName($row->file_name); 318 $this->setFileSize($row->file_size); 319 $this->setFileType($row->file_type); 320 $this->setCourseId($row->course_id); 321 } 322 $this->fss_storage = new ilFSStorageCourse($this->getCourseId()); 323 return true; 324 } 325} 326