| Total Complexity | 92 |
| Total Lines | 824 |
| 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(); |
||
| 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() { |
||
| 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 | if (!empty($input) && (is_string($input) || is_numeric($input))) { |
||
| 270 | $this->input = $input; |
||
| 271 | } |
||
| 272 | return $this; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Set the file array data generally in constructor this is already set using $_FILES |
||
| 277 | * @param array $fileArray the new value |
||
| 278 | */ |
||
| 279 | public function setFileArray($fileArray){ |
||
| 280 | $this->file_array = $fileArray; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Set new filename |
||
| 285 | * Example: |
||
| 286 | * FileUpload::setFilename("new file.txt") |
||
| 287 | * Remember: |
||
| 288 | * Use %s to retrive file extension |
||
| 289 | * |
||
| 290 | * @since 1.0 |
||
| 291 | * @version 1.0 |
||
| 292 | * @param string $filename |
||
| 293 | * @return object |
||
| 294 | * @method boolean setFilename |
||
| 295 | */ |
||
| 296 | public function setFilename($filename) { |
||
| 297 | if ($this->isFilename($filename)) { |
||
| 298 | $this->filename = $filename; |
||
| 299 | } |
||
| 300 | return $this; |
||
| 301 | } |
||
| 302 | /** |
||
| 303 | * Set automatic filename |
||
| 304 | * |
||
| 305 | * @since 1.0 |
||
| 306 | * @version 1.5 |
||
| 307 | * @param string $extension |
||
| 308 | * @return object |
||
| 309 | * @method boolean setAutoFilename |
||
| 310 | */ |
||
| 311 | public function setAutoFilename() { |
||
| 312 | $this->filename = sha1(mt_rand(1, 9999) . uniqid()); |
||
| 313 | $this->filename .= time(); |
||
| 314 | return $this; |
||
| 315 | } |
||
| 316 | /** |
||
| 317 | * Set file size limit |
||
| 318 | * |
||
| 319 | * @since 1.0 |
||
| 320 | * @version 1.0 |
||
| 321 | * @param double|string $file_size |
||
| 322 | * @return object |
||
| 323 | * @method boolean setMaxFileSize |
||
| 324 | */ |
||
| 325 | public function setMaxFileSize($file_size) { |
||
| 326 | $file_size = $this->sizeInBytes($file_size); |
||
|
|
|||
| 327 | if (is_numeric($file_size) && $file_size > -1) { |
||
| 328 | // Get php config |
||
| 329 | $php_size = $this->sizeInBytes((int) ini_get('upload_max_filesize')); |
||
| 330 | // Calculate difference |
||
| 331 | if ($php_size < $file_size) { |
||
| 332 | $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 . ']'); |
||
| 333 | } |
||
| 334 | $this->max_file_size = $file_size; |
||
| 335 | } |
||
| 336 | return $this; |
||
| 337 | } |
||
| 338 | /** |
||
| 339 | * Set array mime types |
||
| 340 | * |
||
| 341 | * @since 1.0 |
||
| 342 | * @version 1.0 |
||
| 343 | * @param array $mimes |
||
| 344 | * @return object |
||
| 345 | * @method boolean setAllowedMimeTypes |
||
| 346 | */ |
||
| 347 | public function setAllowedMimeTypes(array $mimes) { |
||
| 348 | if (count($mimes) > 0) { |
||
| 349 | array_map(array($this, 'setAllowMimeType'), $mimes); |
||
| 350 | } |
||
| 351 | return $this; |
||
| 352 | } |
||
| 353 | /** |
||
| 354 | * Set input callback |
||
| 355 | * |
||
| 356 | * @since 1.0 |
||
| 357 | * @version 1.0 |
||
| 358 | * @param mixed $callback |
||
| 359 | * @return object |
||
| 360 | * @method boolean setCallbackInput |
||
| 361 | */ |
||
| 362 | public function setCallbackInput($callback) { |
||
| 363 | if (is_callable($callback, false)) { |
||
| 364 | $this->callbacks['input'] = $callback; |
||
| 365 | } |
||
| 366 | return $this; |
||
| 367 | } |
||
| 368 | /** |
||
| 369 | * Set output callback |
||
| 370 | * |
||
| 371 | * @since 1.0 |
||
| 372 | * @version 1.0 |
||
| 373 | * @param mixed $callback |
||
| 374 | * @return object |
||
| 375 | * @method boolean setCallbackOutput |
||
| 376 | */ |
||
| 377 | public function setCallbackOutput($callback) { |
||
| 378 | if (is_callable($callback, false)) { |
||
| 379 | $this->callbacks['output'] = $callback; |
||
| 380 | } |
||
| 381 | return $this; |
||
| 382 | } |
||
| 383 | /** |
||
| 384 | * Append a mime type to allowed mime types |
||
| 385 | * |
||
| 386 | * @since 1.0 |
||
| 387 | * @version 1.0.1 |
||
| 388 | * @param string $mime |
||
| 389 | * @return object |
||
| 390 | * @method boolean setAllowMimeType |
||
| 391 | */ |
||
| 392 | public function setAllowMimeType($mime) { |
||
| 393 | if (!empty($mime) && is_string($mime)) { |
||
| 394 | $this->allowed_mime_types[] = strtolower($mime); |
||
| 395 | $this->file['allowed_mime_types'][] = strtolower($mime); |
||
| 396 | } |
||
| 397 | return $this; |
||
| 398 | } |
||
| 399 | /** |
||
| 400 | * Set allowed mime types from mime helping |
||
| 401 | * |
||
| 402 | * @since 1.0.1 |
||
| 403 | * @version 1.0.1 |
||
| 404 | * @return object |
||
| 405 | * @method boolean setMimeHelping |
||
| 406 | */ |
||
| 407 | public function setMimeHelping($name) { |
||
| 408 | if (!empty($name) |
||
| 409 | && is_string($name) |
||
| 410 | && array_key_exists($name, $this->mime_helping)) { |
||
| 411 | return $this->setAllowedMimeTypes($this->mime_helping[$name]); |
||
| 412 | } |
||
| 413 | return $this; |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Set function to upload file |
||
| 418 | * Examples: |
||
| 419 | * 1.- FileUpload::setUploadFunction("move_uploaded_file"); |
||
| 420 | * 2.- FileUpload::setUploadFunction("copy"); |
||
| 421 | * |
||
| 422 | * @since 1.0 |
||
| 423 | * @version 1.0 |
||
| 424 | * @param string $function |
||
| 425 | * @return object |
||
| 426 | * @method boolean setUploadFunction |
||
| 427 | */ |
||
| 428 | public function setUploadFunction($function) { |
||
| 429 | if (is_callable($function)) { |
||
| 430 | $this->upload_function = $function; |
||
| 431 | } |
||
| 432 | return $this; |
||
| 433 | } |
||
| 434 | /** |
||
| 435 | * Clear allowed mime types cache |
||
| 436 | * |
||
| 437 | * @since 1.0 |
||
| 438 | * @version 1.0 |
||
| 439 | * @return object |
||
| 440 | * @method boolean clearAllowedMimeTypes |
||
| 441 | */ |
||
| 442 | public function clearAllowedMimeTypes() { |
||
| 443 | $this->allowed_mime_types = array(); |
||
| 444 | $this->file['allowed_mime_types'] = array(); |
||
| 445 | return $this; |
||
| 446 | } |
||
| 447 | /** |
||
| 448 | * Set destination output |
||
| 449 | * |
||
| 450 | * @since 1.0 |
||
| 451 | * @version 1.0 |
||
| 452 | * @param string $destination_directory Destination path |
||
| 453 | * @param boolean $create_if_not_exist |
||
| 454 | * @return object |
||
| 455 | * @method boolean setDestinationDirectory |
||
| 456 | */ |
||
| 457 | public function setDestinationDirectory($destination_directory, $create_if_not_exist = false) { |
||
| 458 | $destination_directory = realpath($destination_directory); |
||
| 459 | if (substr($destination_directory, -1) != DIRECTORY_SEPARATOR) { |
||
| 460 | $destination_directory .= DIRECTORY_SEPARATOR; |
||
| 461 | } |
||
| 462 | |||
| 463 | if ($this->isDirpath($destination_directory)) { |
||
| 464 | if ($this->dirExists($destination_directory)) { |
||
| 465 | $this->destination_directory = $destination_directory; |
||
| 466 | chdir($destination_directory); |
||
| 467 | } else if ($create_if_not_exist === true) { |
||
| 468 | if (mkdir($destination_directory, 0775, true)) { |
||
| 469 | $this->destination_directory = $destination_directory; |
||
| 470 | chdir($destination_directory); |
||
| 471 | } else { |
||
| 472 | $this->logger->warning('Can not create the upload directory [' . $destination_directory . ']'); |
||
| 473 | } |
||
| 474 | } |
||
| 475 | } |
||
| 476 | return $this; |
||
| 477 | } |
||
| 478 | /** |
||
| 479 | * Check file exists |
||
| 480 | * |
||
| 481 | * @since 1.0 |
||
| 482 | * @version 1.0.1 |
||
| 483 | * @param string $file_destination |
||
| 484 | * @return boolean |
||
| 485 | * @method boolean fileExists |
||
| 486 | */ |
||
| 487 | public function fileExists($file_destination) { |
||
| 488 | return $this->isFilename($file_destination) |
||
| 489 | && file_exists($file_destination) |
||
| 490 | && is_file($file_destination); |
||
| 491 | } |
||
| 492 | /** |
||
| 493 | * Check dir exists |
||
| 494 | * |
||
| 495 | * @since 1.0 |
||
| 496 | * @version 1.0.1 |
||
| 497 | * @param string $path |
||
| 498 | * @return boolean |
||
| 499 | * @method boolean dirExists |
||
| 500 | */ |
||
| 501 | public function dirExists($path) { |
||
| 502 | return $this->isDirpath($path) |
||
| 503 | && file_exists($path) |
||
| 504 | && is_dir($path); |
||
| 505 | } |
||
| 506 | /** |
||
| 507 | * Check valid filename |
||
| 508 | * |
||
| 509 | * @since 1.0 |
||
| 510 | * @version 1.0.1 |
||
| 511 | * @param string $filename |
||
| 512 | * @return boolean |
||
| 513 | * @method boolean isFilename |
||
| 514 | */ |
||
| 515 | public function isFilename($filename) { |
||
| 516 | $filename = basename($filename); |
||
| 517 | return (!empty($filename) && (is_string($filename) || is_numeric($filename))); |
||
| 518 | } |
||
| 519 | /** |
||
| 520 | * Validate mime type with allowed mime types, |
||
| 521 | * but if allowed mime types is empty, this method return true |
||
| 522 | * |
||
| 523 | * @since 1.0 |
||
| 524 | * @version 1.0 |
||
| 525 | * @param string $mime |
||
| 526 | * @return boolean |
||
| 527 | * @method boolean checkMimeType |
||
| 528 | */ |
||
| 529 | public function checkMimeType($mime) { |
||
| 530 | if (count($this->allowed_mime_types) == 0) { |
||
| 531 | return true; |
||
| 532 | } |
||
| 533 | return in_array(strtolower($mime), $this->allowed_mime_types); |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Get the file input name |
||
| 538 | * @return string |
||
| 539 | */ |
||
| 540 | public function getInput() { |
||
| 541 | return $this->input; |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Get the file array |
||
| 546 | * @return array |
||
| 547 | */ |
||
| 548 | public function getFileArray() { |
||
| 549 | return $this->file_array; |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Get the filename |
||
| 554 | * @return string |
||
| 555 | */ |
||
| 556 | public function getFilename() { |
||
| 557 | return $this->filename; |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Get the max file size |
||
| 562 | * @return double|int |
||
| 563 | */ |
||
| 564 | public function getMaxFileSize() { |
||
| 565 | return $this->max_file_size; |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Get the list of mimes type allowed |
||
| 570 | * @return array |
||
| 571 | */ |
||
| 572 | public function getAllowMimeType() { |
||
| 573 | return $this->allowed_mime_types; |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Get the list of callbacks for "input" and "output" |
||
| 578 | * @return array |
||
| 579 | */ |
||
| 580 | public function getCallbacks() { |
||
| 581 | return $this->callbacks; |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Get the upload function name like "copy", "move_uploaded_file" |
||
| 586 | * @return string |
||
| 587 | */ |
||
| 588 | public function getUploadFunction() { |
||
| 589 | return $this->upload_function; |
||
| 590 | } |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Get the destination directory |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | public function getDestinationDirectory() { |
||
| 597 | return $this->destination_directory ; |
||
| 598 | } |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Get the allow overwriting |
||
| 602 | * @return boolean |
||
| 603 | */ |
||
| 604 | public function isAllowOverwriting() { |
||
| 605 | return $this->overwrite_file ; |
||
| 606 | } |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Retrive status of upload |
||
| 610 | * |
||
| 611 | * @since 1.0 |
||
| 612 | * @version 1.0 |
||
| 613 | * @return boolean |
||
| 614 | * @method boolean getStatus |
||
| 615 | */ |
||
| 616 | public function getStatus() { |
||
| 617 | return $this->file['status']; |
||
| 618 | } |
||
| 619 | |||
| 620 | |||
| 621 | /** |
||
| 622 | * Check valid path |
||
| 623 | * |
||
| 624 | * @since 1.0 |
||
| 625 | * @version 1.0.1 |
||
| 626 | * @param string $filename |
||
| 627 | * @return boolean |
||
| 628 | * @method boolean isDirpath |
||
| 629 | */ |
||
| 630 | public function isDirpath($path) { |
||
| 631 | if (!empty($path) && (is_string($path) || is_numeric($path))) { |
||
| 632 | if (DIRECTORY_SEPARATOR == '/') { |
||
| 633 | return (preg_match('/^[^*?"<>|:]*$/', $path) == 1); |
||
| 634 | } else { |
||
| 635 | return (preg_match("/^[^*?\"<>|:]*$/", substr($path, 2)) == 1); |
||
| 636 | } |
||
| 637 | } |
||
| 638 | return false; |
||
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Allow overwriting files |
||
| 643 | * |
||
| 644 | * @since 1.0 |
||
| 645 | * @version 1.0 |
||
| 646 | * @return object |
||
| 647 | * @method boolean allowOverwriting |
||
| 648 | */ |
||
| 649 | public function allowOverwriting() { |
||
| 650 | $this->overwrite_file = true; |
||
| 651 | return $this; |
||
| 652 | } |
||
| 653 | |||
| 654 | /** |
||
| 655 | * File info |
||
| 656 | * |
||
| 657 | * @since 1.0 |
||
| 658 | * @version 1.0 |
||
| 659 | * @return object |
||
| 660 | * @method object getInfo |
||
| 661 | */ |
||
| 662 | public function getInfo() { |
||
| 663 | return (object) $this->file; |
||
| 664 | } |
||
| 665 | |||
| 666 | |||
| 667 | /** |
||
| 668 | * Check if the file is uploaded |
||
| 669 | * @return boolean |
||
| 670 | */ |
||
| 671 | public function isUploaded() { |
||
| 672 | return isset($this->file_array[$this->input]) |
||
| 673 | && is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
||
| 674 | } |
||
| 675 | |||
| 676 | |||
| 677 | /** |
||
| 678 | * Upload file |
||
| 679 | * |
||
| 680 | * @since 1.0 |
||
| 681 | * @version 1.0.1 |
||
| 682 | * @return boolean |
||
| 683 | * @method boolean save |
||
| 684 | */ |
||
| 685 | public function save() { |
||
| 686 | if (count($this->file_array) > 0 && array_key_exists($this->input, $this->file_array)) { |
||
| 687 | // set original filename if not have a new name |
||
| 688 | if (empty($this->filename)) { |
||
| 689 | $this->filename = $this->file_array[$this->input]['name']; |
||
| 690 | } else { |
||
| 691 | // Replace %s for extension in filename |
||
| 692 | // Before: /[\w\d]*(.[\d\w]+)$/i |
||
| 693 | // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
||
| 694 | // Support unicode(utf-8) characters |
||
| 695 | // Example: "русские.jpeg" is valid; "Zhōngguó.jpeg" is valid; "Tønsberg.jpeg" is valid |
||
| 696 | $extension = preg_replace( |
||
| 697 | '/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu', |
||
| 698 | '$1', |
||
| 699 | $this->file_array[$this->input]['name'] |
||
| 700 | ); |
||
| 701 | $this->filename = $this->filename . '.' . $extension; |
||
| 702 | } |
||
| 703 | |||
| 704 | // set file info |
||
| 705 | $this->file['mime'] = $this->file_array[$this->input]['type']; |
||
| 706 | $this->file['tmp'] = $this->file_array[$this->input]['tmp_name']; |
||
| 707 | $this->file['original'] = $this->file_array[$this->input]['name']; |
||
| 708 | $this->file['size'] = $this->file_array[$this->input]['size']; |
||
| 709 | $this->file['sizeFormated'] = $this->sizeFormat($this->file['size']); |
||
| 710 | $this->file['destination'] = $this->destination_directory . $this->filename; |
||
| 711 | $this->file['filename'] = $this->filename; |
||
| 712 | $this->file['error'] = $this->file_array[$this->input]['error']; |
||
| 713 | |||
| 714 | $this->logger->info('The upload file information to process is : ' . stringfy_vars($this->file)); |
||
| 715 | |||
| 716 | $error = $this->uploadHasError(); |
||
| 717 | if ($error) { |
||
| 718 | return false; |
||
| 719 | } |
||
| 720 | // Execute input callback |
||
| 721 | $this->runCallback('input'); |
||
| 722 | |||
| 723 | $this->file['status'] = call_user_func_array( |
||
| 724 | $this->upload_function, array( |
||
| 725 | $this->file_array[$this->input]['tmp_name'], |
||
| 726 | $this->destination_directory . $this->filename |
||
| 727 | ) |
||
| 728 | ); |
||
| 729 | |||
| 730 | // Execute output callback |
||
| 731 | $this->runCallback('output'); |
||
| 732 | |||
| 733 | return $this->file['status']; |
||
| 734 | } |
||
| 735 | return false; |
||
| 736 | } |
||
| 737 | |||
| 738 | |||
| 739 | /** |
||
| 740 | * File size for humans. |
||
| 741 | * |
||
| 742 | * @since 1.0 |
||
| 743 | * @version 1.0 |
||
| 744 | * @param integer $bytes |
||
| 745 | * @param integer $precision |
||
| 746 | * @return string |
||
| 747 | * @method string sizeFormat |
||
| 748 | */ |
||
| 749 | public function sizeFormat($size, $precision = 2) { |
||
| 750 | if ($size > 0) { |
||
| 751 | $base = log($size) / log(1024); |
||
| 752 | $suffixes = array('B', 'K', 'M', 'G', 'T'); |
||
| 753 | $suf = ''; |
||
| 754 | if (isset($suffixes[floor($base)])) { |
||
| 755 | $suf = $suffixes[floor($base)]; |
||
| 756 | } |
||
| 757 | return round(pow(1024, $base - floor($base)), $precision) . $suf; |
||
| 758 | } |
||
| 759 | return null; |
||
| 760 | } |
||
| 761 | |||
| 762 | |||
| 763 | /** |
||
| 764 | * Convert human file size to bytes |
||
| 765 | * |
||
| 766 | * @since 1.0 |
||
| 767 | * @version 1.0.1 |
||
| 768 | * @param integer|double $size |
||
| 769 | * @return integer|double |
||
| 770 | * @method string sizeInBytes |
||
| 771 | */ |
||
| 772 | public function sizeInBytes($size) { |
||
| 781 | } |
||
| 782 | |||
| 783 | /** |
||
| 784 | * Get the upload error message |
||
| 785 | * @return string |
||
| 786 | */ |
||
| 787 | public function getError() { |
||
| 788 | return $this->error; |
||
| 789 | } |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Set the upload error message |
||
| 793 | * @param string $message the upload error message to set |
||
| 794 | */ |
||
| 795 | public function setError($message) { |
||
| 796 | $this->logger->info('The file upload got error : ' . $message); |
||
| 797 | $this->error = $message; |
||
| 798 | } |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Run the callbacks in the file uploaded |
||
| 802 | * @param string $type the type of callback "input" or "output" |
||
| 803 | * @return void |
||
| 804 | */ |
||
| 805 | protected function runCallback($type) { |
||
| 806 | if (!empty($this->callbacks[$type])) { |
||
| 807 | call_user_func($this->callbacks[$type], (object) $this->file); |
||
| 808 | } |
||
| 809 | } |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Check if file upload has error |
||
| 813 | * @return boolean |
||
| 814 | */ |
||
| 815 | protected function uploadHasError() { |
||
| 816 | //check if file upload is allowed in the configuration |
||
| 817 | if (!ini_get('file_uploads')) { |
||
| 818 | $this->setError($this->error_messages['file_uploads']); |
||
| 819 | return true; |
||
| 820 | } |
||
| 821 | |||
| 822 | //check for php upload error |
||
| 823 | if (is_numeric($this->file['error']) && $this->file['error'] > 0) { |
||
| 824 | $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
||
| 825 | return true; |
||
| 826 | } |
||
| 827 | |||
| 828 | //check for mime type |
||
| 829 | if (!$this->checkMimeType($this->file['mime'])) { |
||
| 830 | $this->setError($this->error_messages['accept_file_types']); |
||
| 831 | return true; |
||
| 832 | } |
||
| 833 | |||
| 834 | // Check file size |
||
| 835 | if ($this->max_file_size > 0 && $this->max_file_size < $this->file['size']) { |
||
| 836 | $this->setError(sprintf($this->error_messages['max_file_size'], $this->sizeFormat($this->max_file_size))); |
||
| 837 | return true; |
||
| 838 | } |
||
| 839 | |||
| 840 | // Check if exists file |
||
| 841 | if ($this->fileExists($this->destination_directory . $this->filename) && $this->overwrite_file === false) { |
||
| 842 | $this->setError($this->error_messages['overwritten_not_allowed']); |
||
| 843 | return true; |
||
| 844 | } |
||
| 845 | return false; |
||
| 846 | } |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Get the PHP upload error message for the given code |
||
| 850 | * @param int $code the error code |
||
| 851 | * @return string the error message |
||
| 852 | */ |
||
| 853 | private function getPhpUploadErrorMessageByCode($code) { |
||
| 864 | } |
||
| 865 | } |
||
| 866 |