SuperGlobal.php
159 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * Data source for HTML_QuickForm2 objects based on superglobal arrays |
| 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: SuperGlobal.php 294057 2010-01-26 21:10:28Z avb $ |
| 45 | * @link http://pear.php.net/package/HTML_QuickForm2 |
| 46 | */ |
| 47 | /** |
| 48 | * Interface for data sources containing submitted values |
| 49 | */ |
| 50 | // require_once 'HTML/QuickForm2/DataSource/Submit.php'; |
| 51 | /** |
| 52 | * Array-based data source for HTML_QuickForm2 objects |
| 53 | */ |
| 54 | // require_once 'HTML/QuickForm2/DataSource/Array.php'; |
| 55 | /** |
| 56 | * Data source for HTML_QuickForm2 objects based on superglobal arrays |
| 57 | * |
| 58 | * @category HTML |
| 59 | * @package HTML_QuickForm2 |
| 60 | * @author Alexey Borzov <avb@php.net> |
| 61 | * @author Bertrand Mansion <golgote@mamasam.com> |
| 62 | * @version Release: @package_version@ |
| 63 | */ |
| 64 | class HTML_QuickForm2_DataSource_SuperGlobal extends \HTML_QuickForm2_DataSource_Array implements \HTML_QuickForm2_DataSource_Submit |
| 65 | { |
| 66 | /** |
| 67 | * Information on file uploads (from $_FILES) |
| 68 | * @var array |
| 69 | */ |
| 70 | protected $files = array(); |
| 71 | /** |
| 72 | * Keys present in the $_FILES array |
| 73 | * @var array |
| 74 | */ |
| 75 | private static $_fileKeys = array('name', 'type', 'size', 'tmp_name', 'error'); |
| 76 | /** |
| 77 | * Class constructor, intializes the internal arrays from superglobals |
| 78 | * |
| 79 | * @param string Request method (GET or POST) |
| 80 | * @param bool Whether magic_quotes_gpc directive is on |
| 81 | */ |
| 82 | public function __construct($requestMethod = 'POST', $magicQuotesGPC = \false) |
| 83 | { |
| 84 | if (!$magicQuotesGPC) { |
| 85 | if ('GET' == \strtoupper($requestMethod)) { |
| 86 | $this->values = $_GET; |
| 87 | } else { |
| 88 | $this->values = $_POST; |
| 89 | $this->files = $_FILES; |
| 90 | } |
| 91 | } else { |
| 92 | if ('GET' == \strtoupper($requestMethod)) { |
| 93 | $this->values = $this->arrayMapRecursive('stripslashes', $_GET); |
| 94 | } else { |
| 95 | $this->values = $this->arrayMapRecursive('stripslashes', $_POST); |
| 96 | foreach ($_FILES as $key1 => $val1) { |
| 97 | foreach ($val1 as $key2 => $val2) { |
| 98 | if ('name' == $key2) { |
| 99 | $this->files[$key1][$key2] = $this->arrayMapRecursive('stripslashes', $val2); |
| 100 | } else { |
| 101 | $this->files[$key1][$key2] = $val2; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | /** |
| 109 | * A recursive version of array_map() function |
| 110 | * |
| 111 | * @param callback Callback function to apply |
| 112 | * @param mixed Input array |
| 113 | * @return array with callback applied |
| 114 | */ |
| 115 | protected function arrayMapRecursive($callback, $arr) |
| 116 | { |
| 117 | if (!\is_array($arr)) { |
| 118 | return \call_user_func($callback, $arr); |
| 119 | } |
| 120 | $mapped = array(); |
| 121 | foreach ($arr as $k => $v) { |
| 122 | $mapped[$k] = \is_array($v) ? $this->arrayMapRecursive($callback, $v) : \call_user_func($callback, $v); |
| 123 | } |
| 124 | return $mapped; |
| 125 | } |
| 126 | public function getUpload($name) |
| 127 | { |
| 128 | if (empty($this->files)) { |
| 129 | return null; |
| 130 | } |
| 131 | if (\false !== ($pos = \strpos($name, '['))) { |
| 132 | $tokens = \explode('[', \str_replace(']', '', $name)); |
| 133 | $base = \array_shift($tokens); |
| 134 | $value = array(); |
| 135 | if (!isset($this->files[$base]['name'])) { |
| 136 | return null; |
| 137 | } |
| 138 | foreach (self::$_fileKeys as $key) { |
| 139 | $value[$key] = $this->files[$base][$key]; |
| 140 | } |
| 141 | do { |
| 142 | $token = \array_shift($tokens); |
| 143 | if (!isset($value['name'][$token])) { |
| 144 | return null; |
| 145 | } |
| 146 | foreach (self::$_fileKeys as $key) { |
| 147 | $value[$key] = $value[$key][$token]; |
| 148 | } |
| 149 | } while (!empty($tokens)); |
| 150 | return $value; |
| 151 | } elseif (isset($this->files[$name])) { |
| 152 | return $this->files[$name]; |
| 153 | } else { |
| 154 | return null; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 |