UploadedFile.class.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Checks that the data references an uploaded file.
  4. * Optional parameter will also check the MIME Type of the uploaded file.
  5. *
  6. * @author Ironpilot
  7. * @copyright Copywrite (c) 2011, STAPLE CODE
  8. *
  9. * This file is part of the STAPLE Framework.
  10. *
  11. * The STAPLE Framework is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by the
  13. * Free Software Foundation, either version 3 of the License, or (at your option)
  14. * any later version.
  15. *
  16. * The STAPLE Framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  18. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. class Staple_Form_Validate_UploadedFile extends Staple_Form_Validator
  25. {
  26. const DEFAULT_ERROR = 'File is not valid';
  27. protected $mimeCheck = NULL;
  28. public function __construct($mimetype = NULL, $usermsg = NULL)
  29. {
  30. if(isset($usermsg))
  31. {
  32. parent::__construct($usermsg);
  33. }
  34. if(isset($mimetype))
  35. {
  36. $this->setMimeCheck($mimetype);
  37. }
  38. }
  39. /**
  40. * @return the $mimeCheck
  41. */
  42. public function getMimeCheck()
  43. {
  44. return $this->mimeCheck;
  45. }
  46. /**
  47. * @param field_type $mimeCheck
  48. */
  49. public function setMimeCheck($mimeCheck)
  50. {
  51. $this->mimeCheck = $mimeCheck;
  52. return $this;
  53. }
  54. /**
  55. *
  56. * @param mixed $data
  57. * @return bool
  58. * @see Staple_Form_Validator::check()
  59. */
  60. public function check($data)
  61. {
  62. if(is_array($data))
  63. {
  64. if(array_key_exists('tmp_name', $data))
  65. {
  66. if($this->mimeCheck == null)
  67. {
  68. if(is_uploaded_file($data['tmp_name']))
  69. {
  70. return true;
  71. }
  72. }
  73. else
  74. {
  75. //Check that FileInfo Extention is enabled
  76. if(class_exists('finfo'))
  77. {
  78. $finfo = new finfo(FILEINFO_MIME_TYPE);
  79. if($finfo->file($data['tmp_name']) == $this->getMimeCheck())
  80. {
  81. return true;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. $this->addError(self::DEFAULT_ERROR);
  88. return false;
  89. }
  90. }
  91. ?>