PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/include/HTML/QuickForm/file.php

https://github.com/radicaldesigns/amp
PHP | 346 lines | 137 code | 33 blank | 176 comment | 31 complexity | a358b805501531276eaa2d654f8c5dfe MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Adam Daniel <adaniel1@eesus.jnj.com> |
  17. // | Bertrand Mansion <bmansion@mamasam.com> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: file.php,v 1.19 2004/10/09 19:26:00 avb Exp $
  21. require_once("HTML/QuickForm/input.php");
  22. // register file-related rules
  23. if (class_exists('HTML_QuickForm')) {
  24. HTML_QuickForm::registerRule('uploadedfile', 'callback', '_ruleIsUploadedFile', 'HTML_QuickForm_file');
  25. HTML_QuickForm::registerRule('maxfilesize', 'callback', '_ruleCheckMaxFileSize', 'HTML_QuickForm_file');
  26. HTML_QuickForm::registerRule('mimetype', 'callback', '_ruleCheckMimeType', 'HTML_QuickForm_file');
  27. HTML_QuickForm::registerRule('filename', 'callback', '_ruleCheckFileName', 'HTML_QuickForm_file');
  28. }
  29. /**
  30. * HTML class for a file type element
  31. *
  32. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  33. * @author Bertrand Mansion <bmansion@mamasam.com>
  34. * @version 1.0
  35. * @since PHP4.04pl1
  36. * @access public
  37. */
  38. class HTML_QuickForm_file extends HTML_QuickForm_input
  39. {
  40. // {{{ properties
  41. /**
  42. * Uploaded file data, from $_FILES
  43. * @var array
  44. */
  45. var $_value = null;
  46. // }}}
  47. // {{{ constructor
  48. /**
  49. * Class constructor
  50. *
  51. * @param string Input field name attribute
  52. * @param string Input field label
  53. * @param mixed (optional)Either a typical HTML attribute string
  54. * or an associative array
  55. * @since 1.0
  56. * @access public
  57. */
  58. function HTML_QuickForm_file($elementName=null, $elementLabel=null, $attributes=null)
  59. {
  60. HTML_QuickForm_input::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
  61. $this->setType('file');
  62. } //end constructor
  63. // }}}
  64. // {{{ setSize()
  65. /**
  66. * Sets size of file element
  67. *
  68. * @param int Size of file element
  69. * @since 1.0
  70. * @access public
  71. */
  72. function setSize($size)
  73. {
  74. $this->updateAttributes(array('size' => $size));
  75. } //end func setSize
  76. // }}}
  77. // {{{ getSize()
  78. /**
  79. * Returns size of file element
  80. *
  81. * @since 1.0
  82. * @access public
  83. * @return int
  84. */
  85. function getSize()
  86. {
  87. return $this->getAttribute('size');
  88. } //end func getSize
  89. // }}}
  90. // {{{ freeze()
  91. /**
  92. * Freeze the element so that only its value is returned
  93. *
  94. * @access public
  95. * @return bool
  96. */
  97. function freeze()
  98. {
  99. return false;
  100. } //end func freeze
  101. // }}}
  102. // {{{ setValue()
  103. /**
  104. * Sets value for file element.
  105. *
  106. * Actually this does nothing. The function is defined here to override
  107. * HTML_Quickform_input's behaviour of setting the 'value' attribute. As
  108. * no sane user-agent uses <input type="file">'s value for anything
  109. * (because of security implications) we implement file's value as a
  110. * read-only property with a special meaning.
  111. *
  112. * @param mixed Value for file element
  113. * @since 3.0
  114. * @access public
  115. */
  116. function setValue($value)
  117. {
  118. return null;
  119. } //end func setValue
  120. // }}}
  121. // {{{ getValue()
  122. /**
  123. * Returns information about the uploaded file
  124. *
  125. * @since 3.0
  126. * @access public
  127. * @return array
  128. */
  129. function getValue()
  130. {
  131. return $this->_value;
  132. } // end func getValue
  133. // }}}
  134. // {{{ onQuickFormEvent()
  135. /**
  136. * Called by HTML_QuickForm whenever form event is made on this element
  137. *
  138. * @param string Name of event
  139. * @param mixed event arguments
  140. * @param object calling object
  141. * @since 1.0
  142. * @access public
  143. * @return bool
  144. */
  145. function onQuickFormEvent($event, $arg, &$caller)
  146. {
  147. switch ($event) {
  148. case 'updateValue':
  149. if ($caller->getAttribute('method') == 'get') {
  150. return PEAR::raiseError('Cannot add a file upload field to a GET method form');
  151. }
  152. $this->_value = $this->_findValue();
  153. $caller->updateAttributes(array('enctype' => 'multipart/form-data'));
  154. $caller->setMaxFileSize();
  155. break;
  156. case 'addElement':
  157. $this->onQuickFormEvent('createElement', $arg, $caller);
  158. return $this->onQuickFormEvent('updateValue', null, $caller);
  159. break;
  160. case 'createElement':
  161. $className = get_class($this);
  162. $this->$className($arg[0], $arg[1], $arg[2]);
  163. break;
  164. }
  165. return true;
  166. } // end func onQuickFormEvent
  167. // }}}
  168. // {{{ moveUploadedFile()
  169. /**
  170. * Moves an uploaded file into the destination
  171. *
  172. * @param string Destination directory path
  173. * @param string New file name
  174. * @access public
  175. */
  176. function moveUploadedFile($dest, $fileName = '')
  177. {
  178. if ($dest != '' && substr($dest, -1) != '/') {
  179. $dest .= '/';
  180. }
  181. $fileName = ($fileName != '') ? $fileName : basename($this->_value['name']);
  182. if (move_uploaded_file($this->_value['tmp_name'], $dest . $fileName)) {
  183. return true;
  184. } else {
  185. return false;
  186. }
  187. } // end func moveUploadedFile
  188. // }}}
  189. // {{{ isUploadedFile()
  190. /**
  191. * Checks if the element contains an uploaded file
  192. *
  193. * @access public
  194. * @return bool true if file has been uploaded, false otherwise
  195. */
  196. function isUploadedFile()
  197. {
  198. return $this->_ruleIsUploadedFile($this->_value);
  199. } // end func isUploadedFile
  200. // }}}
  201. // {{{ _ruleIsUploadedFile()
  202. /**
  203. * Checks if the given element contains an uploaded file
  204. *
  205. * @param array Uploaded file info (from $_FILES)
  206. * @access private
  207. * @return bool true if file has been uploaded, false otherwise
  208. */
  209. function _ruleIsUploadedFile($elementValue)
  210. {
  211. if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
  212. (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')) {
  213. return is_uploaded_file($elementValue['tmp_name']);
  214. } else {
  215. return false;
  216. }
  217. } // end func _ruleIsUploadedFile
  218. // }}}
  219. // {{{ _ruleCheckMaxFileSize()
  220. /**
  221. * Checks that the file does not exceed the max file size
  222. *
  223. * @param array Uploaded file info (from $_FILES)
  224. * @param int Max file size
  225. * @access private
  226. * @return bool true if filesize is lower than maxsize, false otherwise
  227. */
  228. function _ruleCheckMaxFileSize($elementValue, $maxSize)
  229. {
  230. if (!empty($elementValue['error']) &&
  231. (UPLOAD_ERR_FORM_SIZE == $elementValue['error'] || UPLOAD_ERR_INI_SIZE == $elementValue['error'])) {
  232. return false;
  233. }
  234. if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
  235. return true;
  236. }
  237. return ($maxSize >= @filesize($elementValue['tmp_name']));
  238. } // end func _ruleCheckMaxFileSize
  239. // }}}
  240. // {{{ _ruleCheckMimeType()
  241. /**
  242. * Checks if the given element contains an uploaded file of the right mime type
  243. *
  244. * @param array Uploaded file info (from $_FILES)
  245. * @param mixed Mime Type (can be an array of allowed types)
  246. * @access private
  247. * @return bool true if mimetype is correct, false otherwise
  248. */
  249. function _ruleCheckMimeType($elementValue, $mimeType)
  250. {
  251. if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
  252. return true;
  253. }
  254. if (is_array($mimeType)) {
  255. return in_array($elementValue['type'], $mimeType);
  256. }
  257. return $elementValue['type'] == $mimeType;
  258. } // end func _ruleCheckMimeType
  259. // }}}
  260. // {{{ _ruleCheckFileName()
  261. /**
  262. * Checks if the given element contains an uploaded file of the filename regex
  263. *
  264. * @param array Uploaded file info (from $_FILES)
  265. * @param string Regular expression
  266. * @access private
  267. * @return bool true if name matches regex, false otherwise
  268. */
  269. function _ruleCheckFileName($elementValue, $regex)
  270. {
  271. if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
  272. return true;
  273. }
  274. return preg_match($regex, $elementValue['name']);
  275. } // end func _ruleCheckFileName
  276. // }}}
  277. // {{{ _findValue()
  278. /**
  279. * Tries to find the element value from the values array
  280. *
  281. * Needs to be redefined here as $_FILES is populated differently from
  282. * other arrays when element name is of the form foo[bar]
  283. *
  284. * @access private
  285. * @return mixed
  286. */
  287. function _findValue()
  288. {
  289. if (empty($_FILES)) {
  290. return null;
  291. }
  292. $elementName = $this->getName();
  293. if (isset($_FILES[$elementName])) {
  294. return $_FILES[$elementName];
  295. } elseif (false !== ($pos = strpos($elementName, '['))) {
  296. $base = substr($elementName, 0, $pos);
  297. $idx = "['" . str_replace(array(']', '['), array('', "']['"), substr($elementName, $pos + 1, -1)) . "']";
  298. $props = array('name', 'type', 'size', 'tmp_name', 'error');
  299. $code = "if (!isset(\$_FILES['{$base}']['name']{$idx})) {\n" .
  300. " return null;\n" .
  301. "} else {\n" .
  302. " \$value = array();\n";
  303. foreach ($props as $prop) {
  304. $code .= " \$value['{$prop}'] = \$_FILES['{$base}']['{$prop}']{$idx};\n";
  305. }
  306. return eval($code . " return \$value;\n}\n");
  307. } else {
  308. return null;
  309. }
  310. }
  311. // }}}
  312. } // end class HTML_QuickForm_file
  313. ?>