PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/clonefish/validation.file.php

https://github.com/dodyrw/framework
PHP | 330 lines | 200 code | 74 blank | 56 comment | 34 complexity | c3ae42fa4fbaf3c069a12d2ee5b33ce1 MD5 | raw file
  1. <?php
  2. /**
  3. * Clonefish form generator class
  4. * (c) phpformclass.com, Dots Amazing
  5. * All rights reserved.
  6. *
  7. * @copyright 2010 Dots Amazing
  8. * @link http://phpformclass.com
  9. * @package clonefish
  10. * @subpackage validation
  11. */
  12. /*
  13. * Validation
  14. * @package clonefish
  15. * @subpackage validationTypes
  16. */
  17. class fileValidation extends validation {
  18. var $settings = Array();
  19. // settings coming from the settings array
  20. var $minimum; // minimum size in bytes
  21. var $maximum; // maximum size in bytes
  22. var $required = 1; // uploading a file is required
  23. var $types = Array(); // filetypes allowed
  24. var $extensions = Array(); // extensions allowed
  25. var $imagecreatecheck = 0; // check gif,jpeg,png,wbmp using imagecreate*()
  26. // to get a proper result if it can be
  27. // handled by gd2 later
  28. var $_channels = Array(); // allowed channels for a JPEG file
  29. var $_types = Array(); // filetypes - PHP constants rendered by the class
  30. var $_jpgchannels = Array();
  31. // only RGB channels: 'jpgrgb' setting,
  32. // only CMYK channels: 'jpgcmyk' setting,
  33. // both channels are set with 'jpg'
  34. // -------------------------------------------------------------------------
  35. function fileValidation( &$settings, &$element ) {
  36. // call parent constructor
  37. $parent_class_name = get_parent_class( $this );
  38. $this->$parent_class_name( $settings, $element );
  39. $inisize = ini_get('upload_max_filesize');
  40. if ( preg_match( '/^([0-9]+)M$/i', $inisize, $results ) )
  41. $inisize = $results[ 1 ] * 1024 * 1024;
  42. if (
  43. !is_numeric( $this->maximum ) ||
  44. ( $this->maximum > $inisize )
  45. )
  46. $maximum = $inisize;
  47. foreach ( $this->types as $type ) {
  48. switch ( $type ) {
  49. case 'gif': $this->_types[] = IMAGETYPE_GIF; break;
  50. case 'jpg':
  51. $this->_types[] = IMAGETYPE_JPEG;
  52. $this->_jpgchannels = Array( 3, 4 );
  53. // by default, both CMYK+RGB is allowed
  54. break;
  55. case 'jpgrgb':
  56. $this->_types[] = IMAGETYPE_JPEG;
  57. $this->_jpgchannels[] = 3;
  58. // adding RGB channel
  59. break;
  60. case 'jpgcmyk':
  61. $this->_types[] = IMAGETYPE_JPEG;
  62. $this->_jpgchannels[] = 4;
  63. // adding CMYK channel
  64. break;
  65. case 'png': $this->_types[] = IMAGETYPE_PNG; break;
  66. case 'bmp':
  67. $this->_types[] = IMAGETYPE_BMP;
  68. $this->_types[] = IMAGETYPE_WBMP;
  69. break;
  70. case 'swf':
  71. $this->_types[] = IMAGETYPE_SWF;
  72. $this->_types[] = IMAGETYPE_SWC;
  73. break;
  74. default:
  75. die( sprintf( CF_ERR_FILE_VALIDATION_UNSUPPORTED, $this->element->getName(), $type ) );
  76. break;
  77. }
  78. }
  79. }
  80. // -------------------------------------------------------------------------
  81. function validateExtension( $filename ) {
  82. if ( empty( $this->extensions ) )
  83. return true; // no extensions to validate, so we pass
  84. if ( ( $pos = strrpos( $filename, '.' ) ) !== false ) {
  85. $extension = strtolower( substr( $filename, $pos + 1 ) );
  86. foreach( $this->extensions as $v ) {
  87. $v = strtolower( $v );
  88. if ( $extension == $v )
  89. return true;
  90. }
  91. }
  92. return false;
  93. }
  94. // -------------------------------------------------------------------------
  95. function isValid() {
  96. $results = Array();
  97. if ( $this->checkDependencyPHP() ) {
  98. $name = $this->element->getName();
  99. if ( isset( $_FILES[ $name ] ) ) {
  100. if (
  101. !isset( $_FILES[ $name ] ) ||
  102. ( $_FILES[ $name ]['tmp_name'] == 'none' ) ||
  103. ( $_FILES[ $name ]['size'] == '0' )
  104. )
  105. $file['error'] = UPLOAD_ERR_NO_FILE;
  106. else
  107. $file = $_FILES[ $name ];
  108. switch ( $file['error'] ) {
  109. case UPLOAD_ERR_INI_SIZE:
  110. break;
  111. case UPLOAD_ERR_PARTIAL:
  112. break;
  113. case UPLOAD_ERR_NO_FILE:
  114. if ( $this->required && !$this->element->getValue( 0 ) ) {
  115. $message = sprintf(
  116. $this->selecthelp( $this->element, CF_STR_FILE_REQUIRED ),
  117. $this->element->getDisplayName()
  118. );
  119. $results[] = $message;
  120. $this->element->addMessage( $message );
  121. }
  122. break;
  123. case UPLOAD_ERR_OK:
  124. // EXTENSIONS
  125. if ( !$this->validateExtension( $file['name'] ) ) {
  126. $message =
  127. sprintf(
  128. $this->selecthelp( $this->element, CF_STR_FILE_EXTENSIONS_ALLOWED ),
  129. $this->element->getDisplayName(),
  130. implode( CF_STR_FILE_OR, $this->extensions )
  131. );
  132. $results[] = $message;
  133. $this->element->addMessage( $message );
  134. }
  135. // TYPES
  136. if ( count( $this->_types ) ) {
  137. $dimension = getimagesize( $file['tmp_name'] );
  138. $function = '';
  139. if ( $this->imagecreatecheck ) {
  140. switch ( $dimension[ 2 ] ) {
  141. case IMAGETYPE_GIF: $function = 'imagecreatefromgif'; break;
  142. case IMAGETYPE_JPEG: $function = 'imagecreatefromjpeg'; break;
  143. case IMAGETYPE_PNG: $function = 'imagecreatefrompng'; break;
  144. case IMAGETYPE_WBMP: $function = 'imagecreatefromwbmp'; break;
  145. }
  146. }
  147. if (
  148. !is_array( $dimension ) ||
  149. !in_array( $dimension[ 2 ], $this->_types ) ||
  150. (
  151. in_array( $dimension[ 2 ], $this->_types ) &&
  152. ( $dimension[ 2 ] == IMAGETYPE_JPEG ) &&
  153. !in_array( $dimension['channels'], $this->_jpgchannels )
  154. ) ||
  155. (
  156. $this->imagecreatecheck &&
  157. strlen( $function ) &&
  158. !@$function( $file['tmp_name'] )
  159. )
  160. ) {
  161. $message =
  162. sprintf(
  163. $this->selecthelp( $this->element, CF_STR_FILE_TYPES_ALLOWED ),
  164. $this->element->getDisplayName(),
  165. implode( CF_STR_FILE_OR, $this->types)
  166. );
  167. $results[] = $message;
  168. $this->element->addMessage( $message );
  169. }
  170. }
  171. // MINIMUM LENGTH
  172. if ( is_numeric( $this->minimum ) ) {
  173. if ( filesize( $file['tmp_name'] ) < $this->minimum ) {
  174. $message =
  175. sprintf(
  176. $this->selecthelp( $this->element, CF_STR_FILE_MINIMUM ),
  177. $this->element->getDisplayName(),
  178. $this->minimum
  179. );
  180. $results[] = $message;
  181. $this->element->addMessage( $message );
  182. }
  183. }
  184. // MAXIMUM LENGTH
  185. if ( is_numeric( $this->maximum ) ) {
  186. if ( filesize( $file['tmp_name'] ) > $this->maximum ) {
  187. $message =
  188. sprintf(
  189. $this->selecthelp( $this->element, CF_STR_FILE_MAXIMUM ),
  190. $this->element->getDisplayName(),
  191. $this->maximum
  192. );
  193. $results[] = $message;
  194. $this->element->addMessage( $message );
  195. }
  196. }
  197. break;
  198. }
  199. }
  200. else {
  201. // $_FILES[ $name ] was not set
  202. if ( $this->required && !$this->element->getValue( 0 ) ) {
  203. $message = sprintf(
  204. $this->selecthelp( $this->element, CF_STR_FILE_REQUIRED ),
  205. $this->element->getDisplayName()
  206. );
  207. $results[] = $message;
  208. $this->element->addMessage( $message );
  209. }
  210. }
  211. // load value for validated and not validated inputs
  212. if ( !count( $results ) && $this->element->binaryvalue )
  213. $this->element->_readContents();
  214. }
  215. return $results;
  216. }
  217. // -------------------------------------------------------------------------
  218. function getJSCode( ) {
  219. $code = '';
  220. $fieldvalue = $this->getJSField( $this->element ) . '.value';
  221. // FILENAME LENGTH
  222. /* *******
  223. // type check - not working in some browsers unfortunately
  224. if ( count( $this->types ) ) {
  225. $types = implode('|', $this->types );
  226. $code .=
  227. 'errors.addIf( \'' . $this->element->_getHTMLId() . '\', ( ' . $fieldvalue .
  228. '.match(/^.*'.$types.'$/) == ' . $fieldvalue .
  229. ', "' .
  230. $this->_jsescape( sprintf(
  231. $this->selecthelp( $this->element, CF_STR_FILE_TYPES_ALLOWED ),
  232. $this->element->getDisplayName(),
  233. implode(', ', $this->types )
  234. ) ). "\" ) );\n";
  235. }
  236. ******* */
  237. if ( $this->required && !$this->element->getValue( 0 ) )
  238. $code .=
  239. 'errors.addIf( \'' . $this->element->_getHTMLId() . '\', ( ' . $fieldvalue . '.length == 0, "' .
  240. $this->_jsescape( sprintf(
  241. $this->selecthelp( $this->element, CF_STR_FILE_REQUIRED ),
  242. $this->element->getDisplayName()
  243. ) ). "\" ) );\n";
  244. return $this->injectDependencyJS( $code );
  245. }
  246. }
  247. ?>