Callback.php
2 years ago
Compare.php
2 years ago
Each.php
2 years ago
Empty.php
2 years ago
Length.php
2 years ago
MaxFileSize.php
2 years ago
MimeType.php
2 years ago
Nonempty.php
2 years ago
NotCallback.php
2 years ago
NotRegex.php
2 years ago
Regex.php
2 years ago
Required.php
2 years ago
MaxFileSize.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * Rule checking that uploaded file size does not exceed the given limit |
| 6 | * |
| 7 | * PHP version 5 |
| 8 | * |
| 9 | * LICENSE: |
| 10 | * |
| 11 | * Copyright (c) 2006-2010, Alexey Borzov <avb@php.net>, |
| 12 | * Bertrand Mansion <golgote@mamasam.com> |
| 13 | * All rights reserved. |
| 14 | * |
| 15 | * Redistribution and use in source and binary forms, with or without |
| 16 | * modification, are permitted provided that the following conditions |
| 17 | * are met: |
| 18 | * |
| 19 | * * Redistributions of source code must retain the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer. |
| 21 | * * Redistributions in binary form must reproduce the above copyright |
| 22 | * notice, this list of conditions and the following disclaimer in the |
| 23 | * documentation and/or other materials provided with the distribution. |
| 24 | * * The names of the authors may not be used to endorse or promote products |
| 25 | * derived from this software without specific prior written permission. |
| 26 | * |
| 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 28 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 29 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 30 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 31 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 32 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 33 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 34 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 35 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 36 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 37 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | * |
| 39 | * @category HTML |
| 40 | * @package HTML_QuickForm2 |
| 41 | * @author Alexey Borzov <avb@php.net> |
| 42 | * @author Bertrand Mansion <golgote@mamasam.com> |
| 43 | * @license http://opensource.org/licenses/bsd-license.php New BSD License |
| 44 | * @version SVN: $Id: MaxFileSize.php 294057 2010-01-26 21:10:28Z avb $ |
| 45 | * @link http://pear.php.net/package/HTML_QuickForm2 |
| 46 | */ |
| 47 | /** |
| 48 | * Base class for HTML_QuickForm2 rules |
| 49 | */ |
| 50 | // require_once 'HTML/QuickForm2/Rule.php'; |
| 51 | /** |
| 52 | * Rule checking that uploaded file size does not exceed the given limit |
| 53 | * |
| 54 | * The Rule needs one configuration parameter for its work: the size limit. |
| 55 | * This limit can be passed either to |
| 56 | * {@link HTML_QuickForm2_Rule::__construct() the Rule constructor} as local |
| 57 | * configuration or to {@link HTML_QuickForm2_Factory::registerRule()} as |
| 58 | * global one. As usual, global configuration overrides local one. |
| 59 | * |
| 60 | * Note that if file upload failed due to upload_max_filesize php.ini setting |
| 61 | * or MAX_FILE_SIZE form field, then this rule won't even be called, due to |
| 62 | * File element's built-in validation setting the error message. |
| 63 | * |
| 64 | * The Rule considers missing file uploads (UPLOAD_ERR_NO_FILE) valid. |
| 65 | * |
| 66 | * @category HTML |
| 67 | * @package HTML_QuickForm2 |
| 68 | * @author Alexey Borzov <avb@php.net> |
| 69 | * @author Bertrand Mansion <golgote@mamasam.com> |
| 70 | * @version Release: @package_version@ |
| 71 | */ |
| 72 | class HTML_QuickForm2_Rule_MaxFileSize extends \HTML_QuickForm2_Rule |
| 73 | { |
| 74 | /** |
| 75 | * Validates the owner element |
| 76 | * |
| 77 | * @return bool whether uploaded file's size is within given limit |
| 78 | */ |
| 79 | protected function validateOwner() |
| 80 | { |
| 81 | $value = $this->owner->getValue(); |
| 82 | if (!isset($value['error']) || \UPLOAD_ERR_NO_FILE == $value['error']) { |
| 83 | return \true; |
| 84 | } |
| 85 | return $this->getConfig() >= @\filesize($value['tmp_name']); |
| 86 | } |
| 87 | /** |
| 88 | * Sets maximum allowed file size |
| 89 | * |
| 90 | * @param int Maximum allowed size |
| 91 | * @return HTML_QuickForm2_Rule |
| 92 | * @throws HTML_QuickForm2_InvalidArgumentException if a bogus size limit was provided |
| 93 | */ |
| 94 | public function setConfig($config) |
| 95 | { |
| 96 | if (0 >= $config) { |
| 97 | throw new \HTML_QuickForm2_InvalidArgumentException('MaxFileSize Rule requires a positive size limit, ' . \preg_replace('/\\s+/', ' ', \var_export($config, \true)) . ' given'); |
| 98 | } |
| 99 | return parent::setConfig($config); |
| 100 | } |
| 101 | /** |
| 102 | * Sets the element that will be validated by this rule |
| 103 | * |
| 104 | * @param HTML_QuickForm2_Element_InputFile File upload field to validate |
| 105 | * @throws HTML_QuickForm2_InvalidArgumentException if trying to use |
| 106 | * this Rule on something that isn't a file upload field |
| 107 | */ |
| 108 | public function setOwner(\HTML_QuickForm2_Node $owner) |
| 109 | { |
| 110 | if (!$owner instanceof \HTML_QuickForm2_Element_InputFile) { |
| 111 | throw new \HTML_QuickForm2_InvalidArgumentException('MaxFileSize Rule can only validate file upload fields, ' . \get_class($owner) . ' given'); |
| 112 | } |
| 113 | parent::setOwner($owner); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 |