PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Fire/Validators/File.php

https://github.com/igrekeichvi/timetable
PHP | 111 lines | 47 code | 18 blank | 46 comment | 13 complexity | fdec41e588a9a22bae4b67508321f54d MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * File upload validator.
  4. * @package Library
  5. * @subpackage Validators
  6. */
  7. /** Loads validator interface. */
  8. require_once(sprintf('%1$s%2$sValidators%2$sValidator.php', FIRE_LIBRARY_PATH, DIRECTORY_SEPARATOR));
  9. /**
  10. * File upload validator.
  11. * @package Library
  12. * @subpackage Validators
  13. */
  14. class FileValidator extends Fire_Validator {
  15. /**
  16. * @var mixed $types
  17. * @access public
  18. */
  19. var $mime_types;
  20. /**
  21. * @var mixed $extensions
  22. * @access public
  23. */
  24. var $extensions;
  25. /**
  26. * @var integer $max_size
  27. * @access public
  28. */
  29. var $max_size;
  30. /**
  31. * @var string $target_directory
  32. * @access public
  33. */
  34. var $target_directory;
  35. /**
  36. *
  37. * Constructor.
  38. * @param mixed $file_types
  39. * @param mixed $extensions
  40. * @param integer $max_size
  41. * @param string $target_directory
  42. * @return
  43. * @access public
  44. *
  45. */
  46. function __construct($mime_types = array(), $extensions = array(), $max_size = 2134016, $target_directory = '') {
  47. parent::__construct();
  48. $this->mime_types = $mime_types;
  49. $this->extensions = $extensions;
  50. $this->max_size = intval($max_size);
  51. $this->target_directory = $target_directory;
  52. }
  53. /**
  54. *
  55. * Validates a file upload.
  56. * @param string $key_of_files_array
  57. * @return boolean
  58. * @access public
  59. *
  60. */
  61. function validate($key_of_files_array) {
  62. if (!isset($_FILES[$key_of_files_array])) {
  63. return;
  64. }
  65. $_file_to_check = $_FILES[$key_of_files_array];
  66. if (empty($_file_to_check) || empty($_file_to_check['name'])) {
  67. $this->_error = 'error_message_for_missing_file';
  68. return false;
  69. }
  70. if ($_file_to_check['error'] != 0) {
  71. $this->_error = 'error_message_for_upload_file_failed';
  72. return false;
  73. }
  74. if (!empty($this->mime_types) && !in_array($_file_to_check['type'], $this->mime_types)) {
  75. $this->_error = 'error_message_for_disallowed_mime_type';
  76. return false;
  77. }
  78. if (!empty($this->extensions) && !in_array(strtolower(substr(strrchr($_file_to_check['name'], '.'), 1)), $this->extensions)) {
  79. $this->_error = 'error_message_for_disallowed_extension';
  80. return false;
  81. }
  82. if ($_file_to_check['size'] > $this->max_size) {
  83. $this->_error = 'error_message_for_file_size_limit_overload';
  84. return false;
  85. }
  86. if (!empty($this->target_directory) && (!is_dir($this->target_directory) || !is_writable($this->target_directory))) {
  87. $this->_error = 'error_message_for_target_directory_not_writeable';
  88. return false;
  89. }
  90. return true;
  91. }
  92. }
  93. ?>