[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/net/src/ -> lmbHttpRequest.class.php (source)

   1  <?php
   2  /*

   3   * Limb PHP Framework

   4   *

   5   * @link http://limb-project.com

   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)

   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html

   8   */
   9  lmb_require('limb/core/src/lmbSet.class.php');
  10  lmb_require('limb/core/src/lmbArrayHelper.class.php');
  11  lmb_require('limb/net/src/lmbUri.class.php');
  12  lmb_require('limb/net/src/lmbUploadedFilesParser.class.php');
  13  
  14  /**

  15   * class lmbHttpRequest.

  16   *

  17   * @package net

  18   * @version $Id: lmbHttpRequest.class.php 5985 2007-06-11 09:39:42Z pachanga $

  19   */
  20  class lmbHttpRequest extends lmbSet
  21  {
  22    protected $uri;
  23    protected $request = array();
  24    protected $get = array();
  25    protected $post = array();
  26    protected $cookies = array();
  27    protected $files = array();
  28    protected $pretend_post = false;
  29  
  30    function __construct($uri_string = null, $get = null, $post = null, $cookies = null, $files = null)
  31    {
  32      parent :: __construct();
  33      $this->_initRequestProperties($uri_string, $get, $post, $cookies, $files);
  34    }
  35  
  36    protected function _initRequestProperties($uri_string, $get, $post, $cookies, $files)
  37    {
  38      $this->uri = !is_null($uri_string) ? new lmbUri($uri_string) : new lmbUri($this->getRawUriString());
  39  
  40      $this->get = !is_null($get) ? $get : $_GET;
  41      $items = $this->uri->getQueryItems();
  42      foreach($items as $k => $v)
  43        $this->get[$k] = $v;
  44  
  45      $this->post = !is_null($post) ? $post : $_POST;
  46      $this->cookies = !is_null($cookies) ? $cookies : $_COOKIE;
  47      $this->files = !is_null($files) ? $this->_parseUploadedFiles($files) : $this->_parseUploadedFiles($_FILES);
  48  
  49      if(ini_get('magic_quotes_gpc'))
  50      {
  51        $this->get = $this->_stripHttpSlashes($this->get);
  52        $this->post = $this->_stripHttpSlashes($this->post);
  53        $this->cookies = $this->_stripHttpSlashes($this->cookies);
  54      }
  55  
  56      $this->request = lmbArrayHelper :: arrayMerge($this->get, $this->post, $this->files);
  57  
  58      foreach($this->request as $k => $v)
  59        $this->set($k, $v);
  60    }
  61  
  62    protected function _parseUploadedFiles($files)
  63    {
  64      $parser = new lmbUploadedFilesParser();
  65      return $parser->objectify($files);
  66    }
  67  
  68    protected function _stripHttpSlashes($data, $result=array())
  69    {
  70      foreach($data as $k => $v)
  71      {
  72        if(is_array($v))
  73          $result[$k] = $this->_stripHttpSlashes($v);
  74        else
  75          $result[$k] = stripslashes($v);
  76      }
  77      return $result;
  78    }
  79  
  80    /**

  81     * @deprecated

  82     */
  83    function hasAttribute($name)
  84    {
  85      return $this->has($name);
  86    }
  87  
  88    function getFiles($key = null)
  89    {
  90      $this->_ensureMultipartFormData();
  91  
  92      return $this->_get('files', $key);
  93    }
  94  
  95    function getFile($name)
  96    {
  97      $file = $this->getFiles($name);
  98      if(is_object($file))
  99        return $file;
 100    }
 101  
 102    function getRequest($key = null)
 103    {
 104      return $this->_get('request', $key);
 105    }
 106  
 107    function getGet($key = null)
 108    {
 109      return $this->_get('get', $key);
 110    }
 111  
 112    function getPost($key = null)
 113    {
 114      return $this->_get('post', $key);
 115    }
 116  
 117    function hasPost()
 118    {
 119      if($this->pretend_post)
 120        return true;
 121  
 122      return sizeof($this->post) > 0 ||
 123        (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST');
 124    }
 125  
 126    function pretendPost($flag = true)
 127    {
 128      $this->pretend_post = $flag;
 129    }
 130  
 131    function getCookie($key = null)
 132    {
 133      return $this->_get('cookies', $key);
 134    }
 135  
 136    function getSafe($var)
 137    {
 138      return htmlspecialchars(parent :: get($var));
 139    }
 140  
 141    protected function _get($var, $key = null)
 142    {
 143      if(is_null($key))
 144        return $this->$var;
 145  
 146      $arr = $this->$var;
 147      if(isset($arr[$key]))
 148        return $arr[$key];
 149    }
 150  
 151    function getUri()
 152    {
 153      return $this->uri;
 154    }
 155  
 156    function getUriPath()
 157    {
 158      return $this->uri->getPath();
 159    }
 160  
 161    function getRawUriString()
 162    {
 163      $host = 'localhost';
 164      if(!empty($_SERVER['HTTP_HOST']))
 165        list($host) = explode(':', $_SERVER['HTTP_HOST']);
 166      elseif(!empty($_SERVER['SERVER_NAME']))
 167        list($host) = explode(':', $_SERVER['SERVER_NAME']);
 168  
 169      if(isset($_SERVER['HTTPS']) && !strcasecmp($_SERVER['HTTPS'], 'on'))
 170        $protocol = 'https';
 171      else
 172        $protocol = 'http';
 173  
 174      if(!isset($port) || $port != intval($port))
 175        $port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : 80;
 176  
 177      if($protocol == 'http' && $port == 80)
 178        $port = null;
 179  
 180      if($protocol == 'https' && $port == 443)
 181        $port = null;
 182  
 183      $server = $protocol . '://' . $host . (isset($port) ? ':' . $port : '');
 184  
 185      if(isset($_SERVER['REQUEST_URI']))
 186        $url = $_SERVER['REQUEST_URI'];
 187      elseif(isset($_SERVER['QUERY_STRING']))
 188        $url = basename($_SERVER['PHP_SELF']) . '?' . $_SERVER['QUERY_STRING'];
 189      else
 190        $url = $_SERVER['PHP_SELF'];
 191  
 192      return $server . $url;
 193    }
 194  
 195    function toString()
 196    {
 197      $flat = array();
 198      $query = '';
 199  
 200      lmbArrayHelper :: toFlatArray($this->export(), $flat);
 201  
 202      foreach($flat as $key => $value)
 203      {
 204        if(is_object($value)) //skippping uploaded files
 205          continue;
 206        $query .= $key . '=' . $value . '&';
 207      }
 208  
 209      $uri = clone($this->uri);
 210      $uri->removeQueryItems();
 211      return rtrim($uri->toString() . '?' . rtrim($query, '&'), '?');
 212    }
 213  
 214    function dump()
 215    {
 216      return $this->toString();
 217    }
 218  
 219    protected function _ensureMultipartFormData()
 220    {
 221      if(!$this->hasPost() || $this->files)
 222        return;
 223  
 224      if(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') === false)
 225        throw new lmbException("Submitted form does not have enctype='multipart/form-data' attribute, no files loaded!");
 226    }
 227  }
 228  
 229  ?>


Generated: Sat Sep 6 04:46:52 2008 Cross-referenced by PHPXref 0.7