| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | /********************************************************************\ |
|---|
| 5 | * This program is free software; you can redistribute it and/or * |
|---|
| 6 | * modify it under the terms of the GNU General Public License as * |
|---|
| 7 | * published by the Free Software Foundation; either version 2 of * |
|---|
| 8 | * the License, or (at your option) any later version. * |
|---|
| 9 | * * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, * |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 13 | * GNU General Public License for more details. * |
|---|
| 14 | * * |
|---|
| 15 | * You should have received a copy of the GNU General Public License* |
|---|
| 16 | * along with this program; if not, contact: * |
|---|
| 17 | * * |
|---|
| 18 | * Free Software Foundation Voice: +1-617-542-5942 * |
|---|
| 19 | * 59 Temple Place - Suite 330 Fax: +1-617-542-2652 * |
|---|
| 20 | * Boston, MA 02111-1307, USA gnu@gnu.org * |
|---|
| 21 | * * |
|---|
| 22 | \********************************************************************/ |
|---|
| 23 | /**@file File.php |
|---|
| 24 | * @author Copyright (C) 2005 François Proulx, Technologies Coeus inc. |
|---|
| 25 | */ |
|---|
| 26 | |
|---|
| 27 | require_once BASEPATH.'classes/Content.php'; |
|---|
| 28 | |
|---|
| 29 | error_reporting(E_ALL); |
|---|
| 30 | |
|---|
| 31 | /** Représente un Langstring en particulier, ne créez pas un objet langstrings si vous n'en avez pas spécifiquement besoin |
|---|
| 32 | */ |
|---|
| 33 | class File extends Content |
|---|
| 34 | { |
|---|
| 35 | /* File size units */ |
|---|
| 36 | const UNIT_BYTES = 1; |
|---|
| 37 | const UNIT_KILOBYTES = 1024; |
|---|
| 38 | const UNIT_MEGABYTES = 1048576; |
|---|
| 39 | const UNIT_GIGABYTES = 1073741824; |
|---|
| 40 | |
|---|
| 41 | /**Constructeur |
|---|
| 42 | @param $content_id Content id |
|---|
| 43 | */ |
|---|
| 44 | function __construct($content_id) |
|---|
| 45 | { |
|---|
| 46 | parent :: __construct($content_id); |
|---|
| 47 | $this->setIsPersistent(false); |
|---|
| 48 | $this->setIsTrivialContent(true); |
|---|
| 49 | global $db; |
|---|
| 50 | |
|---|
| 51 | $content_id = $db->EscapeString($content_id); |
|---|
| 52 | $sql = "SELECT files_id, filename, mime_type, url, octet_length(binary_data) AS local_binary_size, remote_size FROM files WHERE files_id='$content_id'"; |
|---|
| 53 | $db->ExecSqlUniqueRes($sql, $row, false); |
|---|
| 54 | if ($row == null) |
|---|
| 55 | { |
|---|
| 56 | /*Since the parent Content exists, the necessary data in content_group had not yet been created */ |
|---|
| 57 | $sql = "INSERT INTO files (files_id) VALUES ('$content_id')"; |
|---|
| 58 | $db->ExecSqlUpdate($sql, false); |
|---|
| 59 | |
|---|
| 60 | $sql = "SELECT files_id, filename, mime_type, url, octet_length(binary_data) AS local_binary_size, remote_size FROM files WHERE files_id='$content_id'"; |
|---|
| 61 | $db->ExecSqlUniqueRes($sql, $row, false); |
|---|
| 62 | if ($row == null) |
|---|
| 63 | { |
|---|
| 64 | throw new Exception(_("The content with the following id could not be found in the database: ").$content_id); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | } |
|---|
| 68 | $this->mBd = & $db; |
|---|
| 69 | $this->files_row = $row; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * Set Binary data from a POST form data field |
|---|
| 74 | * @param string $upload_field The form field that contains the data |
|---|
| 75 | * |
|---|
| 76 | */ |
|---|
| 77 | function setBinaryDataFromPostVar($upload_field) |
|---|
| 78 | { |
|---|
| 79 | if (!empty ($_FILES[$upload_field]) && $_FILES[$upload_field]['error'] == UPLOAD_ERR_OK) |
|---|
| 80 | { |
|---|
| 81 | // Getting binary data from file |
|---|
| 82 | $fp = fopen($_FILES[$upload_field]['tmp_name'], "rb"); |
|---|
| 83 | $buffer = fread($fp, filesize($_FILES[$upload_field]['tmp_name'])); |
|---|
| 84 | fclose($fp); |
|---|
| 85 | |
|---|
| 86 | // Updating database |
|---|
| 87 | $this->setBinaryData($buffer); |
|---|
| 88 | $this->setMimeType($_FILES[$upload_field]['type']); |
|---|
| 89 | $this->setFilename($_FILES[$upload_field]['name']); |
|---|
| 90 | $this->refresh(); |
|---|
| 91 | return true; |
|---|
| 92 | } |
|---|
| 93 | else |
|---|
| 94 | { |
|---|
| 95 | switch($_FILES[$upload_field]['error']) |
|---|
| 96 | { |
|---|
| 97 | case UPLOAD_ERR_INI_SIZE: |
|---|
| 98 | echo _("File size exceeds limit specified in PHP.ini"); |
|---|
| 99 | break; |
|---|
| 100 | case UPLOAD_ERR_FORM_SIZE: |
|---|
| 101 | echo _("File size exceeds limit specified HTML form"); |
|---|
| 102 | break; |
|---|
| 103 | case UPLOAD_ERR_PARTIAL: |
|---|
| 104 | echo _("File upload was interrupted"); |
|---|
| 105 | break; |
|---|
| 106 | /* |
|---|
| 107 | case UPLOAD_ERR_NO_FILE: |
|---|
| 108 | echo _("No file was uploaded"); |
|---|
| 109 | break;*/ |
|---|
| 110 | case UPLOAD_ERR_NO_TMP_DIR: |
|---|
| 111 | echo _("Missing temp folder"); |
|---|
| 112 | break; |
|---|
| 113 | } |
|---|
| 114 | return false; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /** Returns the DateTime object representing the date of the contribution */ |
|---|
| 119 | function getBinaryData() |
|---|
| 120 | { |
|---|
| 121 | $this->mBd->ExecSqlUniqueRes("SELECT binary_data FROM files WHERE files_id ='".$this->getId()."';", $row, false); |
|---|
| 122 | return $this->mBd->UnescapeBinaryString($row['binary_data']); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | function setBinaryData($data) |
|---|
| 126 | { |
|---|
| 127 | if($data == null) |
|---|
| 128 | $data = "NULL"; |
|---|
| 129 | else |
|---|
| 130 | $data = "'".$this->mBd->EscapeBinaryString($data)."'"; |
|---|
| 131 | $this->mBd->ExecSqlUpdate("UPDATE files SET binary_data = $data WHERE files_id='".$this->getId()."'", false); |
|---|
| 132 | $this->refresh(); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | function getMimeType() |
|---|
| 136 | { |
|---|
| 137 | return $this->files_row['mime_type']; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | function setMimeType($mime_type) |
|---|
| 141 | { |
|---|
| 142 | $mime_type = $this->mBd->EscapeString($mime_type); |
|---|
| 143 | $this->mBd->ExecSqlUpdate("UPDATE files SET mime_type ='".$mime_type."' WHERE files_id='".$this->getId()."'", false); |
|---|
| 144 | $this->refresh(); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | function getFilename() |
|---|
| 148 | { |
|---|
| 149 | return $this->files_row['filename']; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | function setFilename($file_name) |
|---|
| 153 | { |
|---|
| 154 | $file_name = $this->mBd->EscapeString($file_name); |
|---|
| 155 | $this->mBd->ExecSqlUpdate("UPDATE files SET filename ='".$file_name."' WHERE files_id='".$this->getId()."'", false); |
|---|
| 156 | $this->refresh(); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | function getFileSize($unit = self :: UNIT_BYTES) |
|---|
| 160 | { |
|---|
| 161 | if($this->isLocalFile()) |
|---|
| 162 | $size = $this->files_row['local_binary_size']; |
|---|
| 163 | else |
|---|
| 164 | $size = $this->files_row['remote_size']; |
|---|
| 165 | |
|---|
| 166 | switch ($unit) |
|---|
| 167 | { |
|---|
| 168 | case self :: UNIT_KILOBYTES; |
|---|
| 169 | case self :: UNIT_MEGABYTES : |
|---|
| 170 | case self :: UNIT_GIGABYTES : |
|---|
| 171 | case self :: UNIT_BYTES : |
|---|
| 172 | return round($size / $unit, 2); |
|---|
| 173 | default : |
|---|
| 174 | return $size; |
|---|
| 175 | break; |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | function setRemoteFileSize($size, $unit = self::UNIT_KILOBYTES) |
|---|
| 180 | { |
|---|
| 181 | if(is_numeric($size)) |
|---|
| 182 | { |
|---|
| 183 | $octet_size = $size * $unit; |
|---|
| 184 | $this->mBd->execSqlUpdate("UPDATE files SET remote_size = $octet_size WHERE files_id='".$this->getId()."'", false); |
|---|
| 185 | $this->refresh(); |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | function getFileUrl() |
|---|
| 190 | { |
|---|
| 191 | if (!$this->isLocalFile()) |
|---|
| 192 | return $this->files_row['url']; |
|---|
| 193 | else |
|---|
| 194 | return BASE_SSL_PATH."file_download.php?file_id=".$this->getId(); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | function setURL($url) |
|---|
| 198 | { |
|---|
| 199 | if($url == null) |
|---|
| 200 | $url = "NULL"; |
|---|
| 201 | else |
|---|
| 202 | $url = "'".$this->mBd->EscapeString($url)."'"; |
|---|
| 203 | $this->mBd->execSqlUpdate("UPDATE files SET url = $url WHERE files_id='".$this->getId()."'", false); |
|---|
| 204 | $this->refresh(); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | function isLocalFile() |
|---|
| 208 | { |
|---|
| 209 | return is_null($this->files_row['url']); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | /**Affiche l'interface d'administration de l'objet */ |
|---|
| 213 | function getAdminUI($subclass_admin_interface = null) |
|---|
| 214 | { |
|---|
| 215 | $html = ''; |
|---|
| 216 | $html .= "<div class='admin_class'>File (".get_class($this)." instance)</div>\n"; |
|---|
| 217 | |
|---|
| 218 | $html .= "<div class='admin_section_container'>\n"; |
|---|
| 219 | $html .= "<div class='admin_section_title'>"; |
|---|
| 220 | $html .= "<input type='radio' name='file_mode".$this->getId()."' value='by_upload' ". ($this->isLocalFile() ? "CHECKED" : "").">"; |
|---|
| 221 | $html .= _("Upload a new file (Uploading a new one will replace any existing file)")." : </div>\n"; |
|---|
| 222 | $html .= "<div class='admin_section_data'>\n"; |
|---|
| 223 | $html .= '<input type="hidden" name="MAX_FILE_SIZE" value="1073741824" />'; |
|---|
| 224 | $html .= '<input name="file_file_upload'.$this->getId().'" type="file" />'; |
|---|
| 225 | $html .= "</div>\n"; |
|---|
| 226 | $html .= "</div>\n"; |
|---|
| 227 | |
|---|
| 228 | $html .= "<div class='admin_section_container'>\n"; |
|---|
| 229 | $html .= "<div class='admin_section_title'>"; |
|---|
| 230 | $html .= "<input type='radio' name='file_mode".$this->getId()."' value='remote' ". (!$this->isLocalFile() ? "CHECKED" : "").">"; |
|---|
| 231 | $html .= _("Remote file via URL")." : </div>\n"; |
|---|
| 232 | $html .= "<div class='admin_section_data'>\n"; |
|---|
| 233 | if($this->isLocalFile()) |
|---|
| 234 | $html .= "<input name='file_url".$this->getId()."' type='text' size='50'/>"; |
|---|
| 235 | else |
|---|
| 236 | $html .= "<input name='file_url".$this->getId()."' type='text' size='50' value='".$this->getFileUrl()."'/>"; |
|---|
| 237 | $html .= "</div>\n"; |
|---|
| 238 | $html .= "</div>\n"; |
|---|
| 239 | |
|---|
| 240 | if (!$this->isLocalFile()) |
|---|
| 241 | { |
|---|
| 242 | $html .= "<div class='admin_section_container'>\n"; |
|---|
| 243 | $html .= "<div class='admin_section_title'>"._("File URL")." : </div>\n"; |
|---|
| 244 | $html .= "<div class='admin_section_data'>\n"; |
|---|
| 245 | $html .= $this->getFileUrl(); |
|---|
| 246 | $html .= "</div>\n"; |
|---|
| 247 | $html .= "</div>\n"; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | $html .= "<div class='admin_section_container'>\n"; |
|---|
| 251 | $html .= "<div class='admin_section_title'>"._("Filename to display")." : </div>\n"; |
|---|
| 252 | $html .= "<div class='admin_section_data'>\n"; |
|---|
| 253 | $html .= '<input type="text" name="file_file_name'.$this->getId().'" value="'.$this->getFilename().'" />'; |
|---|
| 254 | $html .= "</div>\n"; |
|---|
| 255 | $html .= "</div>\n"; |
|---|
| 256 | |
|---|
| 257 | if ($this->isLocalFile()) |
|---|
| 258 | { |
|---|
| 259 | $html .= "<div class='admin_section_container'>\n"; |
|---|
| 260 | $html .= "<div class='admin_section_title'>"._("MIME type")." : </div>\n"; |
|---|
| 261 | $html .= "<div class='admin_section_data'>\n"; |
|---|
| 262 | $html .= '<input type="text" name="file_mime_type'.$this->getId().'" value="'.$this->getMimeType().'" />'; |
|---|
| 263 | $html .= "</div>\n"; |
|---|
| 264 | $html .= "</div>\n"; |
|---|
| 265 | |
|---|
| 266 | $html .= "<div class='admin_section_container'>\n"; |
|---|
| 267 | $html .= "<div class='admin_section_title'>"._("Locally stored file size")." : </div>\n"; |
|---|
| 268 | $html .= "<div class='admin_section_data'>\n"; |
|---|
| 269 | $html .= $this->getFileSize(self :: UNIT_KILOBYTES)." "._("KB"); |
|---|
| 270 | $html .= "</div>\n"; |
|---|
| 271 | $html .= "</div>\n"; |
|---|
| 272 | } |
|---|
| 273 | else |
|---|
| 274 | { |
|---|
| 275 | $html .= "<div class='admin_section_container'>\n"; |
|---|
| 276 | $html .= "<div class='admin_section_title'>"._("Remote file size")." : </div>\n"; |
|---|
| 277 | $html .= "<div class='admin_section_data'>\n"; |
|---|
| 278 | // The hidden field contains old value to determine if we have to update ( this prevents unwanted successive floating point evaluation ) |
|---|
| 279 | $html .= '<input type="hidden" name="file_old_remote_size'.$this->getId().'" value="'.$this->getFileSize().'" />'; |
|---|
| 280 | $html .= '<input type="text" name="file_remote_size'.$this->getId().'" value="'.$this->getFileSize().'" />'; |
|---|
| 281 | $html .= "</div>\n"; |
|---|
| 282 | $html .= "</div>\n"; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | $html .= "<div class='admin_section_container'>\n"; |
|---|
| 286 | $html .= "<div class='admin_section_data'>\n"; |
|---|
| 287 | $html .= "<a href='".$this->getFileUrl()."'>"._("Download")." ".$this->getFilename()." (".$this->getFileSize(self::UNIT_KILOBYTES)." "._("KB").")</a>"; |
|---|
| 288 | $html .= "</div>\n"; |
|---|
| 289 | $html .= "</div>\n"; |
|---|
| 290 | |
|---|
| 291 | $html .= $subclass_admin_interface; |
|---|
| 292 | return parent :: getAdminUI($html); |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | function processAdminUI() |
|---|
| 296 | { |
|---|
| 297 | parent :: processAdminUI(); |
|---|
| 298 | // If no file was uploaded, update filename and mime type |
|---|
| 299 | if(!empty($_REQUEST["file_mode".$this->getId()])) |
|---|
| 300 | { |
|---|
| 301 | $file_mode = $_REQUEST["file_mode".$this->getId()]; |
|---|
| 302 | if ($file_mode == "by_upload") |
|---|
| 303 | { |
|---|
| 304 | $this->setBinaryDataFromPostVar("file_file_upload".$this->getId()); |
|---|
| 305 | $this->setURL(null); |
|---|
| 306 | // Reset the remote file size ( not used ) |
|---|
| 307 | $this->setRemoteFileSize(0); |
|---|
| 308 | } |
|---|
| 309 | else |
|---|
| 310 | { |
|---|
| 311 | if ($file_mode == "remote") |
|---|
| 312 | { |
|---|
| 313 | $this->setURL($_REQUEST["file_url".$this->getId()]); |
|---|
| 314 | $this->setBinaryData(null); |
|---|
| 315 | // When switching from local to remote, this field does not exist yet |
|---|
| 316 | if(!empty($_REQUEST["file_old_remote_size".$this->getId()])) |
|---|
| 317 | { |
|---|
| 318 | if($_REQUEST["file_remote_size".$this->getId()] != $_REQUEST["file_old_remote_size".$this->getId()]) |
|---|
| 319 | $this->setRemoteFileSize($_REQUEST["file_remote_size".$this->getId()]); |
|---|
| 320 | } |
|---|
| 321 | else |
|---|
| 322 | $this->setRemoteFileSize(0); |
|---|
| 323 | } |
|---|
| 324 | $this->setFilename($_REQUEST["file_file_name".$this->getId()]); |
|---|
| 325 | } |
|---|
| 326 | } |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | /** Retreives the user interface of this object. Anything that overrides this method should call the parent method with it's output at the END of processing. |
|---|
| 330 | * @param $subclass_admin_interface Html content of the interface element of a children |
|---|
| 331 | * @return The HTML fragment for this interface */ |
|---|
| 332 | public function getUserUI() |
|---|
| 333 | { |
|---|
| 334 | $html = ''; |
|---|
| 335 | $html .= "<div class='user_ui_container'>\n"; |
|---|
| 336 | $html .= "<div class='user_ui_object_class'>File (".get_class($this)." instance)</div>\n"; |
|---|
| 337 | $html .= "<a href='".htmlentities($this->getFileUrl())."'>"._("Download")." ".$this->getFilename()." (".$this->getFileSize(self::UNIT_KILOBYTES)." "._("KB").")</a>"; |
|---|
| 338 | $html .= "</div>\n"; |
|---|
| 339 | return parent :: getUserUI($html); |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | /** Delete this Content from the database */ |
|---|
| 343 | public function delete(& $errmsg) |
|---|
| 344 | { |
|---|
| 345 | if ($this->isPersistent() == false) |
|---|
| 346 | { |
|---|
| 347 | $this->mBd->ExecSqlUpdate("DELETE FROM files WHERE files_id = '".$this->getId()."'", false); |
|---|
| 348 | } |
|---|
| 349 | else |
|---|
| 350 | $errmsg = _("Could not delete this file, since it is persistent"); |
|---|
| 351 | return parent :: delete($errmsg); |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | } /* end class File */ |
|---|
| 355 | ?> |
|---|