| Total Complexity | 83 |
| Total Lines | 768 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Upload often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Upload, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Upload extends BaseClass { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Version |
||
| 44 | * |
||
| 45 | * @since 1.5 |
||
| 46 | * @version 1.0 |
||
| 47 | */ |
||
| 48 | const VERSION = '1.5'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Upload function name |
||
| 52 | * Remember: |
||
| 53 | * Default function: move_uploaded_file |
||
| 54 | * Native options: |
||
| 55 | * - move_uploaded_file (Default and best option) |
||
| 56 | * - copy |
||
| 57 | * |
||
| 58 | * @since 1.0 |
||
| 59 | * @version 1.0 |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private $upload_function = 'move_uploaded_file'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Array with the information obtained from the |
||
| 66 | * variable $_FILES or $HTTP_POST_FILES. |
||
| 67 | * |
||
| 68 | * @since 1.0 |
||
| 69 | * @version 1.0 |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | private $file_array = array(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * If the file you are trying to upload already exists it will |
||
| 76 | * be overwritten if you set the variable to true. |
||
| 77 | * |
||
| 78 | * @since 1.0 |
||
| 79 | * @version 1.0 |
||
| 80 | * @var boolean |
||
| 81 | */ |
||
| 82 | private $overwrite_file = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Input element |
||
| 86 | * Example: |
||
| 87 | * <input type="file" name="file" /> |
||
| 88 | * Result: |
||
| 89 | * FileUpload::$input = file |
||
| 90 | * |
||
| 91 | * @since 1.0 |
||
| 92 | * @version 1.0 |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | private $input; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Path output |
||
| 99 | * |
||
| 100 | * @since 1.0 |
||
| 101 | * @version 1.0 |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | private $destination_directory; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Output filename |
||
| 108 | * |
||
| 109 | * @since 1.0 |
||
| 110 | * @version 1.0 |
||
| 111 | * @var string |
||
| 112 | */ |
||
| 113 | private $filename; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Max file size |
||
| 117 | * |
||
| 118 | * @since 1.0 |
||
| 119 | * @version 1.0 |
||
| 120 | * @var float |
||
| 121 | */ |
||
| 122 | private $max_file_size = 0.0; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * List of allowed mime types |
||
| 126 | * |
||
| 127 | * @since 1.0 |
||
| 128 | * @version 1.0 |
||
| 129 | * @var array |
||
| 130 | */ |
||
| 131 | private $allowed_mime_types = array(); |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Callbacks |
||
| 135 | * |
||
| 136 | * @since 1.0 |
||
| 137 | * @version 1.0 |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | private $callbacks = array('before' => null, 'after' => null); |
||
| 141 | |||
| 142 | /** |
||
| 143 | * File object |
||
| 144 | * |
||
| 145 | * @since 1.0 |
||
| 146 | * @version 1.0 |
||
| 147 | * @var object |
||
| 148 | */ |
||
| 149 | private $file; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Helping mime types |
||
| 153 | * |
||
| 154 | * @since 1.0 |
||
| 155 | * @version 1.0 |
||
| 156 | * @var array |
||
| 157 | */ |
||
| 158 | private $mime_helping = array( |
||
| 159 | 'text' => array('text/plain',), |
||
| 160 | 'image' => array( |
||
| 161 | 'image/jpeg', |
||
| 162 | 'image/jpg', |
||
| 163 | 'image/pjpeg', |
||
| 164 | 'image/png', |
||
| 165 | 'image/gif', |
||
| 166 | ), |
||
| 167 | 'document' => array( |
||
| 168 | 'application/pdf', |
||
| 169 | 'application/msword', |
||
| 170 | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
||
| 171 | 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
||
| 172 | 'application/vnd.ms-powerpoint', |
||
| 173 | 'application/vnd.ms-excel', |
||
| 174 | 'application/vnd.oasis.opendocument.spreadsheet', |
||
| 175 | 'application/vnd.oasis.opendocument.presentation', |
||
| 176 | ), |
||
| 177 | 'video' => array( |
||
| 178 | 'video/3gpp', |
||
| 179 | 'video/3gpp', |
||
| 180 | 'video/x-msvideo', |
||
| 181 | 'video/avi', |
||
| 182 | 'video/mpeg4', |
||
| 183 | 'video/mp4', |
||
| 184 | 'video/mpeg', |
||
| 185 | 'video/mpg', |
||
| 186 | 'video/quicktime', |
||
| 187 | 'video/x-sgi-movie', |
||
| 188 | 'video/x-ms-wmv', |
||
| 189 | 'video/x-flv', |
||
| 190 | ), |
||
| 191 | ); |
||
| 192 | |||
| 193 | /** |
||
| 194 | * The upload error message |
||
| 195 | * @var array |
||
| 196 | */ |
||
| 197 | public $error_messages = array(); |
||
| 198 | |||
| 199 | /** |
||
| 200 | * The upload error message |
||
| 201 | * @var string |
||
| 202 | */ |
||
| 203 | protected $error = null; |
||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * Construct |
||
| 208 | * |
||
| 209 | * @since 0.1 |
||
| 210 | * @version 1.0.1 |
||
| 211 | * @return object |
||
| 212 | * @method object __construct |
||
| 213 | */ |
||
| 214 | public function __construct() { |
||
| 215 | parent::__construct(); |
||
| 216 | |||
| 217 | Loader::lang('file_upload'); |
||
| 218 | $obj = & get_instance(); |
||
| 219 | |||
| 220 | $this->error_messages = array( |
||
| 221 | 'upload_err_ini_size' => $obj->lang->get('fu_upload_err_ini_size'), |
||
| 222 | 'upload_err_form_size' => $obj->lang->get('fu_upload_err_form_size'), |
||
| 223 | 'upload_err_partial' => $obj->lang->get('fu_upload_err_partial'), |
||
| 224 | 'upload_err_no_file' => $obj->lang->get('fu_upload_err_no_file'), |
||
| 225 | 'upload_err_no_tmp_dir' => $obj->lang->get('fu_upload_err_no_tmp_dir'), |
||
| 226 | 'upload_err_cant_write' => $obj->lang->get('fu_upload_err_cant_write'), |
||
| 227 | 'upload_err_extension' => $obj->lang->get('fu_upload_err_extension'), |
||
| 228 | 'accept_file_types' => $obj->lang->get('fu_accept_file_types'), |
||
| 229 | 'file_uploads' => $obj->lang->get('fu_file_uploads_disabled'), |
||
| 230 | 'max_file_size' => $obj->lang->get('fu_max_file_size'), |
||
| 231 | 'overwritten_not_allowed' => $obj->lang->get('fu_overwritten_not_allowed'), |
||
| 232 | ); |
||
| 233 | |||
| 234 | $this->file = array( |
||
| 235 | 'status' => false, // True: success upload |
||
| 236 | 'mime' => '', // Empty string |
||
| 237 | 'filename' => '', // Empty string |
||
| 238 | 'original' => '', // Empty string |
||
| 239 | 'size' => 0, // 0 Bytes |
||
| 240 | 'sizeFormated' => '0B', // 0 Bytes |
||
| 241 | 'destination' => './', // Default: ./ |
||
| 242 | 'allowed_mime_types' => array(), // Allowed mime types |
||
| 243 | 'error' => null, // File error |
||
| 244 | ); |
||
| 245 | |||
| 246 | // Change dir to current dir |
||
| 247 | $this->destination_directory = dirname(__FILE__) . DIRECTORY_SEPARATOR; |
||
| 248 | |||
| 249 | // Set file array |
||
| 250 | if (isset($_FILES) && is_array($_FILES)) { |
||
| 251 | $this->file_array = $_FILES; |
||
| 252 | } |
||
| 253 | $this->logger->info('The upload file information are : ' . stringfy_vars($this->file_array)); |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Set input. |
||
| 258 | * If you have $_FILES["file"], you must use the key "file" |
||
| 259 | * Example: |
||
| 260 | * $object->setInput("file"); |
||
| 261 | * |
||
| 262 | * @since 1.0 |
||
| 263 | * @version 1.0 |
||
| 264 | * @param string $input |
||
| 265 | * @return object |
||
| 266 | * @method boolean setInput |
||
| 267 | */ |
||
| 268 | public function setInput($input) |
||
| 269 | { |
||
| 270 | if (!empty($input) && (is_string($input) || is_numeric($input))) { |
||
| 271 | $this->input = $input; |
||
| 272 | } |
||
| 273 | return $this; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Set the file array data generally in constructor this is already set using $_FILES |
||
| 278 | * @param array $fileArray the new value |
||
| 279 | */ |
||
| 280 | public function setFileArray($fileArray){ |
||
| 281 | $this->file_array = $fileArray; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Set new filename |
||
| 286 | * Example: |
||
| 287 | * FileUpload::setFilename("new file.txt") |
||
| 288 | * Remember: |
||
| 289 | * Use %s to retrive file extension |
||
| 290 | * |
||
| 291 | * @since 1.0 |
||
| 292 | * @version 1.0 |
||
| 293 | * @param string $filename |
||
| 294 | * @return object |
||
| 295 | * @method boolean setFilename |
||
| 296 | */ |
||
| 297 | public function setFilename($filename) |
||
| 298 | { |
||
| 299 | if ($this->isFilename($filename)) { |
||
| 300 | $this->filename = $filename; |
||
| 301 | } |
||
| 302 | return $this; |
||
| 303 | } |
||
| 304 | /** |
||
| 305 | * Set automatic filename |
||
| 306 | * |
||
| 307 | * @since 1.0 |
||
| 308 | * @version 1.5 |
||
| 309 | * @param string $extension |
||
| 310 | * @return object |
||
| 311 | * @method boolean setAutoFilename |
||
| 312 | */ |
||
| 313 | public function setAutoFilename() |
||
| 314 | { |
||
| 315 | $this->filename = sha1(mt_rand(1, 9999) . uniqid()); |
||
| 316 | $this->filename .= time(); |
||
| 317 | return $this; |
||
| 318 | } |
||
| 319 | /** |
||
| 320 | * Set file size limit |
||
| 321 | * |
||
| 322 | * @since 1.0 |
||
| 323 | * @version 1.0 |
||
| 324 | * @param double $file_size |
||
| 325 | * @return object |
||
| 326 | * @method boolean setMaxFileSize |
||
| 327 | */ |
||
| 328 | public function setMaxFileSize($file_size) |
||
| 329 | { |
||
| 330 | $file_size = $this->sizeInBytes($file_size); |
||
| 331 | if (is_numeric($file_size) && $file_size > -1) { |
||
| 332 | // Get php config |
||
| 333 | $php_size = $this->sizeInBytes((int) ini_get('upload_max_filesize')); |
||
| 334 | // Calculate difference |
||
| 335 | if ($php_size < $file_size) { |
||
| 336 | $this->logger->warning('The upload max file size you set [' . $file_size . '] is greather than the PHP configuration for upload max file size [' . $php_size . ']'); |
||
| 337 | } |
||
| 338 | $this->max_file_size = $file_size; |
||
| 339 | } |
||
| 340 | return $this; |
||
| 341 | } |
||
| 342 | /** |
||
| 343 | * Set array mime types |
||
| 344 | * |
||
| 345 | * @since 1.0 |
||
| 346 | * @version 1.0 |
||
| 347 | * @param array $mimes |
||
| 348 | * @return object |
||
| 349 | * @method boolean setAllowedMimeTypes |
||
| 350 | */ |
||
| 351 | public function setAllowedMimeTypes(array $mimes) |
||
| 352 | { |
||
| 353 | if (count($mimes) > 0) { |
||
| 354 | array_map(array($this, 'setAllowMimeType'), $mimes); |
||
| 355 | } |
||
| 356 | return $this; |
||
| 357 | } |
||
| 358 | /** |
||
| 359 | * Set input callback |
||
| 360 | * |
||
| 361 | * @since 1.0 |
||
| 362 | * @version 1.0 |
||
| 363 | * @param mixed $callback |
||
| 364 | * @return object |
||
| 365 | * @method boolean setCallbackInput |
||
| 366 | */ |
||
| 367 | public function setCallbackInput($callback) |
||
| 368 | { |
||
| 369 | if (is_callable($callback, false)) { |
||
| 370 | $this->callbacks['input'] = $callback; |
||
| 371 | } |
||
| 372 | return $this; |
||
| 373 | } |
||
| 374 | /** |
||
| 375 | * Set output callback |
||
| 376 | * |
||
| 377 | * @since 1.0 |
||
| 378 | * @version 1.0 |
||
| 379 | * @param mixed $callback |
||
| 380 | * @return object |
||
| 381 | * @method boolean setCallbackOutput |
||
| 382 | */ |
||
| 383 | public function setCallbackOutput($callback) |
||
| 384 | { |
||
| 385 | if (is_callable($callback, false)) { |
||
| 386 | $this->callbacks['output'] = $callback; |
||
| 387 | } |
||
| 388 | return $this; |
||
| 389 | } |
||
| 390 | /** |
||
| 391 | * Append a mime type to allowed mime types |
||
| 392 | * |
||
| 393 | * @since 1.0 |
||
| 394 | * @version 1.0.1 |
||
| 395 | * @param string $mime |
||
| 396 | * @return object |
||
| 397 | * @method boolean setAllowMimeType |
||
| 398 | */ |
||
| 399 | public function setAllowMimeType($mime) |
||
| 400 | { |
||
| 401 | if (!empty($mime) && is_string($mime)) { |
||
| 402 | $this->allowed_mime_types[] = strtolower($mime); |
||
| 403 | $this->file['allowed_mime_types'][] = strtolower($mime); |
||
| 404 | } |
||
| 405 | return $this; |
||
| 406 | } |
||
| 407 | /** |
||
| 408 | * Set allowed mime types from mime helping |
||
| 409 | * |
||
| 410 | * @since 1.0.1 |
||
| 411 | * @version 1.0.1 |
||
| 412 | * @return object |
||
| 413 | * @method boolean setMimeHelping |
||
| 414 | */ |
||
| 415 | public function setMimeHelping($name) |
||
| 416 | { |
||
| 417 | if (!empty($name) |
||
| 418 | && is_string($name) |
||
| 419 | && array_key_exists($name, $this->mime_helping)) { |
||
| 420 | return $this->setAllowedMimeTypes($this->mime_helping[$name]); |
||
| 421 | } |
||
| 422 | return $this; |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Set function to upload file |
||
| 427 | * Examples: |
||
| 428 | * 1.- FileUpload::setUploadFunction("move_uploaded_file"); |
||
| 429 | * 2.- FileUpload::setUploadFunction("copy"); |
||
| 430 | * |
||
| 431 | * @since 1.0 |
||
| 432 | * @version 1.0 |
||
| 433 | * @param string $function |
||
| 434 | * @return object |
||
| 435 | * @method boolean setUploadFunction |
||
| 436 | */ |
||
| 437 | public function setUploadFunction($function) |
||
| 438 | { |
||
| 439 | if (is_callable($function)) { |
||
| 440 | $this->upload_function = $function; |
||
| 441 | } |
||
| 442 | return $this; |
||
| 443 | } |
||
| 444 | /** |
||
| 445 | * Clear allowed mime types cache |
||
| 446 | * |
||
| 447 | * @since 1.0 |
||
| 448 | * @version 1.0 |
||
| 449 | * @return object |
||
| 450 | * @method boolean clearAllowedMimeTypes |
||
| 451 | */ |
||
| 452 | public function clearAllowedMimeTypes() |
||
| 457 | } |
||
| 458 | /** |
||
| 459 | * Set destination output |
||
| 460 | * |
||
| 461 | * @since 1.0 |
||
| 462 | * @version 1.0 |
||
| 463 | * @param string $destination_directory Destination path |
||
| 464 | * @param boolean $create_if_not_exist |
||
| 465 | * @return object |
||
| 466 | * @method boolean setDestinationDirectory |
||
| 467 | */ |
||
| 468 | public function setDestinationDirectory($destination_directory, $create_if_not_exist = false) { |
||
| 469 | $destination_directory = realpath($destination_directory); |
||
| 470 | if (substr($destination_directory, -1) != DIRECTORY_SEPARATOR) { |
||
| 471 | $destination_directory .= DIRECTORY_SEPARATOR; |
||
| 472 | } |
||
| 473 | |||
| 474 | if ($this->isDirpath($destination_directory)) { |
||
| 475 | if ($this->dirExists($destination_directory)) { |
||
| 476 | $this->destination_directory = $destination_directory; |
||
| 477 | chdir($destination_directory); |
||
| 478 | } else if ($create_if_not_exist === true) { |
||
| 479 | if (mkdir($destination_directory, 0775, true)) { |
||
| 480 | $this->destination_directory = $destination_directory; |
||
| 481 | chdir($destination_directory); |
||
| 482 | } else { |
||
| 483 | $this->logger->warning('Can not create the upload directory [' . $destination_directory . ']'); |
||
| 484 | } |
||
| 485 | } |
||
| 486 | } |
||
| 487 | return $this; |
||
| 488 | } |
||
| 489 | /** |
||
| 490 | * Check file exists |
||
| 491 | * |
||
| 492 | * @since 1.0 |
||
| 493 | * @version 1.0.1 |
||
| 494 | * @param string $file_destination |
||
| 495 | * @return boolean |
||
| 496 | * @method boolean fileExists |
||
| 497 | */ |
||
| 498 | public function fileExists($file_destination) |
||
| 499 | { |
||
| 500 | return $this->isFilename($file_destination) |
||
| 501 | && file_exists($file_destination) |
||
| 502 | && is_file($file_destination); |
||
| 503 | } |
||
| 504 | /** |
||
| 505 | * Check dir exists |
||
| 506 | * |
||
| 507 | * @since 1.0 |
||
| 508 | * @version 1.0.1 |
||
| 509 | * @param string $path |
||
| 510 | * @return boolean |
||
| 511 | * @method boolean dirExists |
||
| 512 | */ |
||
| 513 | public function dirExists($path) |
||
| 514 | { |
||
| 515 | return $this->isDirpath($path) |
||
| 516 | && file_exists($path) |
||
| 517 | && is_dir($path); |
||
| 518 | } |
||
| 519 | /** |
||
| 520 | * Check valid filename |
||
| 521 | * |
||
| 522 | * @since 1.0 |
||
| 523 | * @version 1.0.1 |
||
| 524 | * @param string $filename |
||
| 525 | * @return boolean |
||
| 526 | * @method boolean isFilename |
||
| 527 | */ |
||
| 528 | public function isFilename($filename) |
||
| 529 | { |
||
| 530 | $filename = basename($filename); |
||
| 531 | return (!empty($filename) && (is_string($filename) || is_numeric($filename))); |
||
| 532 | } |
||
| 533 | /** |
||
| 534 | * Validate mime type with allowed mime types, |
||
| 535 | * but if allowed mime types is empty, this method return true |
||
| 536 | * |
||
| 537 | * @since 1.0 |
||
| 538 | * @version 1.0 |
||
| 539 | * @param string $mime |
||
| 540 | * @return boolean |
||
| 541 | * @method boolean checkMimeType |
||
| 542 | */ |
||
| 543 | public function checkMimeType($mime) |
||
| 544 | { |
||
| 545 | if (count($this->allowed_mime_types) == 0) { |
||
| 546 | return true; |
||
| 547 | } |
||
| 548 | return in_array(strtolower($mime), $this->allowed_mime_types); |
||
| 549 | } |
||
| 550 | /** |
||
| 551 | * Retrive status of upload |
||
| 552 | * |
||
| 553 | * @since 1.0 |
||
| 554 | * @version 1.0 |
||
| 555 | * @return boolean |
||
| 556 | * @method boolean getStatus |
||
| 557 | */ |
||
| 558 | public function getStatus() |
||
| 559 | { |
||
| 560 | return $this->file['status']; |
||
| 561 | } |
||
| 562 | /** |
||
| 563 | * Check valid path |
||
| 564 | * |
||
| 565 | * @since 1.0 |
||
| 566 | * @version 1.0.1 |
||
| 567 | * @param string $filename |
||
| 568 | * @return boolean |
||
| 569 | * @method boolean isDirpath |
||
| 570 | */ |
||
| 571 | public function isDirpath($path) |
||
| 572 | { |
||
| 573 | if (!empty($path) && (is_string($path) || is_numeric($path))) { |
||
| 574 | if (DIRECTORY_SEPARATOR == '/') { |
||
| 575 | return (preg_match('/^[^*?"<>|:]*$/', $path) == 1); |
||
| 576 | } else { |
||
| 577 | return (preg_match("/^[^*?\"<>|:]*$/", substr($path, 2)) == 1); |
||
| 578 | } |
||
| 579 | } |
||
| 580 | return false; |
||
| 581 | } |
||
| 582 | /** |
||
| 583 | * Allow overwriting files |
||
| 584 | * |
||
| 585 | * @since 1.0 |
||
| 586 | * @version 1.0 |
||
| 587 | * @return object |
||
| 588 | * @method boolean allowOverwriting |
||
| 589 | */ |
||
| 590 | public function allowOverwriting() |
||
| 591 | { |
||
| 592 | $this->overwrite_file = true; |
||
| 593 | return $this; |
||
| 594 | } |
||
| 595 | /** |
||
| 596 | * File info |
||
| 597 | * |
||
| 598 | * @since 1.0 |
||
| 599 | * @version 1.0 |
||
| 600 | * @return object |
||
| 601 | * @method object getInfo |
||
| 602 | */ |
||
| 603 | public function getInfo() |
||
| 604 | { |
||
| 605 | return (object) $this->file; |
||
| 606 | } |
||
| 607 | |||
| 608 | |||
| 609 | /** |
||
| 610 | * Check if the file is uploaded |
||
| 611 | * @return boolean |
||
| 612 | */ |
||
| 613 | public function isUploaded() { |
||
| 614 | return isset($this->file_array[$this->input]) |
||
| 615 | && is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
||
| 616 | } |
||
| 617 | |||
| 618 | |||
| 619 | /** |
||
| 620 | * Upload file |
||
| 621 | * |
||
| 622 | * @since 1.0 |
||
| 623 | * @version 1.0.1 |
||
| 624 | * @return boolean |
||
| 625 | * @method boolean save |
||
| 626 | */ |
||
| 627 | public function save() { |
||
| 628 | if (count($this->file_array) > 0 && array_key_exists($this->input, $this->file_array)) { |
||
| 629 | // set original filename if not have a new name |
||
| 630 | if (empty($this->filename)) { |
||
| 631 | $this->filename = $this->file_array[$this->input]['name']; |
||
| 632 | } else { |
||
| 633 | // Replace %s for extension in filename |
||
| 634 | // Before: /[\w\d]*(.[\d\w]+)$/i |
||
| 635 | // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
||
| 636 | // Support unicode(utf-8) characters |
||
| 637 | // Example: "русские.jpeg" is valid; "Zhōngguó.jpeg" is valid; "Tønsberg.jpeg" is valid |
||
| 638 | $extension = preg_replace( |
||
| 639 | '/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu', |
||
| 640 | '$1', |
||
| 641 | $this->file_array[$this->input]['name'] |
||
| 642 | ); |
||
| 643 | $this->filename = $this->filename . '.' . $extension; |
||
| 644 | } |
||
| 645 | |||
| 646 | // set file info |
||
| 647 | $this->file['mime'] = $this->file_array[$this->input]['type']; |
||
| 648 | $this->file['tmp'] = $this->file_array[$this->input]['tmp_name']; |
||
| 649 | $this->file['original'] = $this->file_array[$this->input]['name']; |
||
| 650 | $this->file['size'] = $this->file_array[$this->input]['size']; |
||
| 651 | $this->file['sizeFormated'] = $this->sizeFormat($this->file['size']); |
||
| 652 | $this->file['destination'] = $this->destination_directory . $this->filename; |
||
| 653 | $this->file['filename'] = $this->filename; |
||
| 654 | $this->file['error'] = $this->file_array[$this->input]['error']; |
||
| 655 | |||
| 656 | $this->logger->info('The upload file information to process is : ' . stringfy_vars($this->file)); |
||
| 657 | |||
| 658 | $error = $this->uploadHasError(); |
||
| 659 | if ($error) { |
||
| 660 | return false; |
||
| 661 | } |
||
| 662 | // Execute input callback |
||
| 663 | $this->runCallback('input'); |
||
| 664 | |||
| 665 | $this->file['status'] = call_user_func_array( |
||
| 666 | $this->upload_function, array( |
||
| 667 | $this->file_array[$this->input]['tmp_name'], |
||
| 668 | $this->destination_directory . $this->filename |
||
| 669 | ) |
||
| 670 | ); |
||
| 671 | |||
| 672 | // Execute output callback |
||
| 673 | $this->runCallback('output'); |
||
| 674 | |||
| 675 | return $this->file['status']; |
||
| 676 | } |
||
| 677 | return false; |
||
| 678 | } |
||
| 679 | |||
| 680 | |||
| 681 | /** |
||
| 682 | * File size for humans. |
||
| 683 | * |
||
| 684 | * @since 1.0 |
||
| 685 | * @version 1.0 |
||
| 686 | * @param integer $bytes |
||
| 687 | * @param integer $precision |
||
| 688 | * @return string |
||
| 689 | * @method string sizeFormat |
||
| 690 | */ |
||
| 691 | public function sizeFormat($size, $precision = 2) |
||
| 692 | { |
||
| 693 | if ($size > 0) { |
||
| 694 | $base = log($size) / log(1024); |
||
| 695 | $suffixes = array('B', 'K', 'M', 'G', 'T'); |
||
| 696 | $suf = ''; |
||
| 697 | if (isset($suffixes[floor($base)])) { |
||
| 698 | $suf = $suffixes[floor($base)]; |
||
| 699 | } |
||
| 700 | return round(pow(1024, $base - floor($base)), $precision) . $suf; |
||
| 701 | } |
||
| 702 | return null; |
||
| 703 | } |
||
| 704 | |||
| 705 | |||
| 706 | /** |
||
| 707 | * Convert human file size to bytes |
||
| 708 | * |
||
| 709 | * @since 1.0 |
||
| 710 | * @version 1.0.1 |
||
| 711 | * @param integer|double $size |
||
| 712 | * @return integer|double |
||
| 713 | * @method string sizeInBytes |
||
| 714 | */ |
||
| 715 | public function sizeInBytes($size) |
||
| 725 | } |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Get the upload error message |
||
| 729 | * @return string |
||
| 730 | */ |
||
| 731 | public function getError() { |
||
| 732 | return $this->error; |
||
| 733 | } |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Set the upload error message |
||
| 737 | * @param string $message the upload error message to set |
||
| 738 | */ |
||
| 739 | public function setError($message) { |
||
| 740 | $this->logger->info('The file upload got error : ' . $message); |
||
| 741 | $this->error = $message; |
||
| 742 | } |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Run the callbacks in the file uploaded |
||
| 746 | * @param string $type the type of callback "input" or "output" |
||
| 747 | * @return void |
||
| 748 | */ |
||
| 749 | protected function runCallback($type) { |
||
| 750 | if (!empty($this->callbacks[$type])) { |
||
| 751 | call_user_func($this->callbacks[$type], (object) $this->file); |
||
| 752 | } |
||
| 753 | } |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Check if file upload has error |
||
| 757 | * @return boolean |
||
| 758 | */ |
||
| 759 | protected function uploadHasError() { |
||
| 760 | //check if file upload is allowed in the configuration |
||
| 761 | if (!ini_get('file_uploads')) { |
||
| 762 | $this->setError($this->error_messages['file_uploads']); |
||
| 763 | return true; |
||
| 764 | } |
||
| 765 | |||
| 766 | //check for php upload error |
||
| 767 | if (is_numeric($this->file['error']) && $this->file['error'] > 0) { |
||
| 768 | $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
||
| 769 | return true; |
||
| 770 | } |
||
| 771 | |||
| 772 | //check for mime type |
||
| 773 | if (!$this->checkMimeType($this->file['mime'])) { |
||
| 774 | $this->setError($this->error_messages['accept_file_types']); |
||
| 775 | return true; |
||
| 776 | } |
||
| 777 | |||
| 778 | // Check file size |
||
| 779 | if ($this->max_file_size > 0 && $this->max_file_size < $this->file['size']) { |
||
| 780 | $this->setError(sprintf($this->error_messages['max_file_size'], $this->sizeFormat($this->max_file_size))); |
||
| 781 | return true; |
||
| 782 | } |
||
| 783 | |||
| 784 | // Check if exists file |
||
| 785 | if ($this->fileExists($this->destination_directory . $this->filename) && $this->overwrite_file === false) { |
||
| 786 | $this->setError($this->error_messages['overwritten_not_allowed']); |
||
| 787 | return true; |
||
| 788 | } |
||
| 789 | return false; |
||
| 790 | } |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Get the PHP upload error message for the given code |
||
| 794 | * @param int $code the error code |
||
| 795 | * @return string the error message |
||
| 796 | */ |
||
| 797 | private function getPhpUploadErrorMessageByCode($code) { |
||
| 808 | } |
||
| 809 | } |
||
| 810 |