HTML_QuickForm2
[ class tree: HTML_QuickForm2 ] [ index: HTML_QuickForm2 ] [ all elements ]

Source for file SuperGlobal.php

Documentation is available at SuperGlobal.php

  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, 2007, 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    CVS: $Id: SuperGlobal.php,v 1.2 2007/10/14 09:40:00 avb Exp $
  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: 0.2.0
  64.  */
  65. {
  66.    /**
  67.     * Information on file uploads (from $_FILES)
  68.     * @var array 
  69.     */
  70.     protected $files = array();
  71.  
  72.    /**
  73.     * Keys present in the $_FILES array
  74.     * @var array 
  75.     */
  76.     private static $_fileKeys = array('name''type''size''tmp_name''error');
  77.  
  78.    /**
  79.     * Class constructor, intializes the internal arrays from superglobals
  80.     *
  81.     * @param    string  Request method (GET or POST)
  82.     * @param    bool    Whether magic_quotes_gpc directive is on
  83.     */
  84.     public function __construct($requestMethod 'POST'$magicQuotesGPC = false)
  85.     {
  86.         if (!$magicQuotesGPC{
  87.             if ('GET' == strtoupper($requestMethod)) {
  88.                 $this->values = $_GET;
  89.             else {
  90.                 $this->values = $_POST;
  91.                 $this->files  = $_FILES;
  92.             }
  93.         else {
  94.             if ('GET' == strtoupper($requestMethod)) {
  95.                 $this->values = $this->arrayMapRecursive('stripslashes'$_GET);
  96.             else {
  97.                 $this->values = $this->arrayMapRecursive('stripslashes'$_POST);
  98.                 foreach ($_FILES as $key1 => $val1{
  99.                     foreach ($val1 as $key2 => $val2{
  100.                         if ('name' == $key2{
  101.                             $this->files[$key1][$key2$this->arrayMapRecursive(
  102.                                                              'stripslashes'$val2
  103.                                                          );
  104.                         else {
  105.                             $this->files[$key1][$key2$val2;
  106.                         }
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.     }
  112.  
  113.    /**
  114.     * A recursive version of array_map() function
  115.     *
  116.     * @param     callback   Callback function to apply
  117.     * @param     mixed      Input array
  118.     * @return    array with callback applied
  119.      */
  120.     protected function arrayMapRecursive($callback$arr)
  121.     {
  122.         if (!is_array($arr)) {
  123.             return call_user_func($callback$arr);
  124.         }
  125.         $mapped = array();
  126.         foreach ($arr as $k => $v{
  127.             $mapped[$kis_array($v)
  128.                           $this->arrayMapRecursive($callback$v):
  129.                           call_user_func($callback$v);
  130.         }
  131.         return $mapped;
  132.     }
  133.  
  134.     public function getUpload($name)
  135.     {
  136.         if (empty($this->files)) {
  137.             return null;
  138.         }
  139.         if (false !== ($pos strpos($name'['))) {
  140.             $tokens explode('['str_replace(']'''$name));
  141.             $base   array_shift($tokens);
  142.             $value  = array();
  143.             if (!isset($this->files[$base]['name'])) {
  144.                 return null;
  145.             }
  146.             foreach (self::$_fileKeys as $key{
  147.                 $value[$key$this->files[$base][$key];
  148.             }
  149.  
  150.             do {
  151.                 $token = array_shift($tokens);
  152.                 if (!isset($value['name'][$token])) {
  153.                     return null;
  154.                 }
  155.                 foreach (self::$_fileKeys as $key{
  156.                     $value[$key$value[$key][$token];
  157.                 }
  158.             while (!empty($tokens));
  159.             return $value;
  160.         elseif(isset($this->files[$name])) {
  161.             return $this->files[$name];
  162.         else {
  163.             return null;
  164.         }
  165.     }
  166. }
  167. ?>

Documentation generated on Mon, 22 Oct 2007 12:30:25 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.