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

/common/libraries/plugin/pear/HTML/QuickForm/file.php

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