Button.php
6 years ago
Date.php
6 years ago
Input.php
6 years ago
InputButton.php
6 years ago
InputCheckable.php
6 years ago
InputCheckbox.php
6 years ago
InputFile.php
6 years ago
InputHidden.php
6 years ago
InputImage.php
6 years ago
InputPassword.php
6 years ago
InputRadio.php
6 years ago
InputReset.php
6 years ago
InputSubmit.php
6 years ago
InputText.php
6 years ago
Select.php
6 years ago
Static.php
6 years ago
Textarea.php
6 years ago
InputFile.php
269 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class for <input type="file" /> elements |
| 4 | * |
| 5 | * PHP version 5 |
| 6 | * |
| 7 | * LICENSE: |
| 8 | * |
| 9 | * Copyright (c) 2006-2010, Alexey Borzov <avb@php.net>, |
| 10 | * Bertrand Mansion <golgote@mamasam.com> |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * Redistribution and use in source and binary forms, with or without |
| 14 | * modification, are permitted provided that the following conditions |
| 15 | * are met: |
| 16 | * |
| 17 | * * Redistributions of source code must retain the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer. |
| 19 | * * Redistributions in binary form must reproduce the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer in the |
| 21 | * documentation and/or other materials provided with the distribution. |
| 22 | * * The names of the authors may not be used to endorse or promote products |
| 23 | * derived from this software without specific prior written permission. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 26 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 27 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 28 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 29 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 30 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 31 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 32 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 33 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 34 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 35 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | * |
| 37 | * @category HTML |
| 38 | * @package HTML_QuickForm2 |
| 39 | * @author Alexey Borzov <avb@php.net> |
| 40 | * @author Bertrand Mansion <golgote@mamasam.com> |
| 41 | * @license http://opensource.org/licenses/bsd-license.php New BSD License |
| 42 | * @version SVN: $Id: InputFile.php 300722 2010-06-24 10:15:52Z mansion $ |
| 43 | * @link http://pear.php.net/package/HTML_QuickForm2 |
| 44 | */ |
| 45 | |
| 46 | /** |
| 47 | * Base class for <input> elements |
| 48 | */ |
| 49 | // require_once 'HTML/QuickForm2/Element/Input.php'; |
| 50 | |
| 51 | /** |
| 52 | * Class for <input type="file" /> elements |
| 53 | * |
| 54 | * @category HTML |
| 55 | * @package HTML_QuickForm2 |
| 56 | * @author Alexey Borzov <avb@php.net> |
| 57 | * @author Bertrand Mansion <golgote@mamasam.com> |
| 58 | * @version Release: @package_version@ |
| 59 | */ |
| 60 | class HTML_QuickForm2_Element_InputFile extends HTML_QuickForm2_Element_Input |
| 61 | { |
| 62 | /** |
| 63 | * Default language for error messages |
| 64 | */ |
| 65 | const DEFAULT_LANGUAGE = 'en'; |
| 66 | |
| 67 | /** |
| 68 | * Localized error messages for PHP's file upload errors |
| 69 | * @var array |
| 70 | */ |
| 71 | protected $errorMessages = array( |
| 72 | 'en' => array( |
| 73 | UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds size permitted by PHP configuration (%d bytes)', |
| 74 | UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive in HTML form (%d bytes)', |
| 75 | UPLOAD_ERR_PARTIAL => 'The file was only partially uploaded', |
| 76 | UPLOAD_ERR_NO_TMP_DIR => 'Server error: temporary directory is missing', |
| 77 | UPLOAD_ERR_CANT_WRITE => 'Server error: failed to write the file to disk', |
| 78 | UPLOAD_ERR_EXTENSION => 'File upload was stopped by extension' |
| 79 | ), |
| 80 | 'fr' => array( |
| 81 | UPLOAD_ERR_INI_SIZE => 'Le fichier envoyé excède la taille autorisée par la configuration de PHP (%d octets)', |
| 82 | UPLOAD_ERR_FORM_SIZE => 'Le fichier envoyé excède la taille de MAX_FILE_SIZE spécifiée dans le formulaire HTML (%d octets)', |
| 83 | UPLOAD_ERR_PARTIAL => 'Le fichier n\'a été que partiellement téléchargé', |
| 84 | UPLOAD_ERR_NO_TMP_DIR => 'Erreur serveur: le répertoire temporaire est manquant', |
| 85 | UPLOAD_ERR_CANT_WRITE => 'Erreur serveur: échec de l\'écriture du fichier sur le disque', |
| 86 | UPLOAD_ERR_EXTENSION => 'L\'envoi de fichier est arrêté par l\'extension' |
| 87 | ), |
| 88 | 'ru' => array( |
| 89 | UPLOAD_ERR_INI_SIZE => 'Размер загруженного файла превосходит максимально разрешённый настройками PHP (%d байт)', |
| 90 | UPLOAD_ERR_FORM_SIZE => 'Размер загруженного файла превосходит директиву MAX_FILE_SIZE, указанную в форме (%d байт)', |
| 91 | UPLOAD_ERR_PARTIAL => 'Файл был загружен не полностью', |
| 92 | UPLOAD_ERR_NO_TMP_DIR => 'Ошибка на сервере: отсутствует каталог для временных файлов', |
| 93 | UPLOAD_ERR_CANT_WRITE => 'Ошибка на сервере: не удалось записать файл на диск', |
| 94 | UPLOAD_ERR_EXTENSION => 'Загрузка файла была остановлена расширением' |
| 95 | ) |
| 96 | ); |
| 97 | |
| 98 | /** |
| 99 | * Language to display error messages in |
| 100 | * @var string |
| 101 | */ |
| 102 | protected $language; |
| 103 | |
| 104 | /** |
| 105 | * Information on uploaded file, from submit data source |
| 106 | * @var array |
| 107 | */ |
| 108 | protected $value = null; |
| 109 | |
| 110 | protected $attributes = array('type' => 'file'); |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * Class constructor |
| 115 | * |
| 116 | * Possible keys in $data array are: |
| 117 | * - 'language': language to display error messages in, it should either be |
| 118 | * already available in the class or provided in 'errorMessages' |
| 119 | * - 'errorMessages': an array of error messages with the following format |
| 120 | * <pre> |
| 121 | * 'language code 1' => array( |
| 122 | * UPLOAD_ERR_... => 'message', |
| 123 | * ... |
| 124 | * UPLOAD_ERR_... => 'message' |
| 125 | * ), |
| 126 | * ... |
| 127 | * 'language code N' => array( |
| 128 | * ... |
| 129 | * ) |
| 130 | * </pre> |
| 131 | * Note that error messages for UPLOAD_ERR_INI_SIZE and UPLOAD_ERR_FORM_SIZE |
| 132 | * may contain '%d' placeholders that will be automatically replaced by the |
| 133 | * appropriate size limits. Note also that you don't need to provide messages |
| 134 | * for every possible error code in the arrays, you may e.g. override just |
| 135 | * one error message for a particular language. |
| 136 | * |
| 137 | * @param string Element name |
| 138 | * @param mixed Attributes (either a string or an array) |
| 139 | * @param array Data used to set up error messages for PHP's file |
| 140 | * upload errors. |
| 141 | */ |
| 142 | public function __construct($name = null, $attributes = null, array $data = array()) |
| 143 | { |
| 144 | if (isset($data['errorMessages'])) { |
| 145 | // neither array_merge() nor array_merge_recursive will do |
| 146 | foreach ($data['errorMessages'] as $lang => $ary) { |
| 147 | foreach ($ary as $code => $message) { |
| 148 | $this->errorMessages[$lang][$code] = $message; |
| 149 | } |
| 150 | } |
| 151 | unset($data['errorMessages']); |
| 152 | } |
| 153 | if (!isset($data['language'])) { |
| 154 | $this->language = self::DEFAULT_LANGUAGE; |
| 155 | } else { |
| 156 | $this->language = isset($this->errorMessages[$data['language']])? |
| 157 | $data['language']: self::DEFAULT_LANGUAGE; |
| 158 | unset($data['language']); |
| 159 | } |
| 160 | parent::__construct($name, $attributes, $data); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * File upload elements cannot be frozen |
| 166 | * |
| 167 | * To properly "freeze" a file upload element one has to store the uploaded |
| 168 | * file somewhere and store the file info in session. This is way outside |
| 169 | * the scope of this class. |
| 170 | * |
| 171 | * @param bool Whether element should be frozen or editable. This |
| 172 | * parameter is ignored in case of file uploads |
| 173 | * @return bool Always returns false |
| 174 | */ |
| 175 | public function toggleFrozen($freeze = null) |
| 176 | { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Returns the information on uploaded file |
| 182 | * |
| 183 | * @return array|null |
| 184 | */ |
| 185 | public function getValue() |
| 186 | { |
| 187 | return $this->value; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * File upload's value cannot be set here |
| 192 | * |
| 193 | * @param mixed Value for file element, this parameter is ignored |
| 194 | * @return HTML_QuickForm2_Element_InputFile |
| 195 | */ |
| 196 | public function setValue($value) |
| 197 | { |
| 198 | return $this; |
| 199 | } |
| 200 | |
| 201 | public function updateValue() |
| 202 | { |
| 203 | foreach ($this->getDataSources() as $ds) { |
| 204 | if ($ds instanceof HTML_QuickForm2_DataSource_Submit) { |
| 205 | $value = $ds->getUpload($this->getName()); |
| 206 | if (null !== $value) { |
| 207 | $this->value = $value; |
| 208 | return; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | $this->value = null; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Performs the server-side validation |
| 217 | * |
| 218 | * Before the Rules added to the element kick in, the element checks the |
| 219 | * error code added to the $_FILES array by PHP. If the code isn't |
| 220 | * UPLOAD_ERR_OK or UPLOAD_ERR_NO_FILE then a built-in error message will be |
| 221 | * displayed and no further validation will take place. |
| 222 | * |
| 223 | * @return boolean Whether the element is valid |
| 224 | */ |
| 225 | protected function validate() |
| 226 | { |
| 227 | if (strlen($this->error)) { |
| 228 | return false; |
| 229 | } |
| 230 | if (isset($this->value['error']) && |
| 231 | !in_array($this->value['error'], array(UPLOAD_ERR_OK, UPLOAD_ERR_NO_FILE))) |
| 232 | { |
| 233 | if (isset($this->errorMessages[$this->language][$this->value['error']])) { |
| 234 | $errorMessage = $this->errorMessages[$this->language][$this->value['error']]; |
| 235 | } else { |
| 236 | $errorMessage = $this->errorMessages[self::DEFAULT_LANGUAGE][$this->value['error']]; |
| 237 | } |
| 238 | if (UPLOAD_ERR_INI_SIZE == $this->value['error']) { |
| 239 | $iniSize = ini_get('upload_max_filesize'); |
| 240 | $size = intval($iniSize); |
| 241 | switch (strtoupper(substr($iniSize, -1))) { |
| 242 | case 'G': $size *= 1024; |
| 243 | case 'M': $size *= 1024; |
| 244 | case 'K': $size *= 1024; |
| 245 | } |
| 246 | |
| 247 | } elseif (UPLOAD_ERR_FORM_SIZE == $this->value['error']) { |
| 248 | foreach ($this->getDataSources() as $ds) { |
| 249 | if ($ds instanceof HTML_QuickForm2_DataSource_Submit) { |
| 250 | $size = intval($ds->getValue('MAX_FILE_SIZE')); |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | $this->error = isset($size)? sprintf($errorMessage, $size): $errorMessage; |
| 256 | return false; |
| 257 | } |
| 258 | return parent::validate(); |
| 259 | } |
| 260 | |
| 261 | public function addFilter($callback, array $options = null, $recursive = true) |
| 262 | { |
| 263 | throw new HTML_QuickForm2_Exception( |
| 264 | 'InputFile elements do not support filters' |
| 265 | ); |
| 266 | } |
| 267 | } |
| 268 | ?> |
| 269 |