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