[ Index ]
 

Code source de CakePHP 1.1.13.4450

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/cake/libs/controller/components/ -> request_handler.php (source)

   1  <?php
   2  /* SVN FILE: $Id: request_handler.php 4409 2007-02-02 13:20:59Z phpnut $ */
   3  /**
   4   * Request object for handling alternative HTTP requests
   5   *
   6   * Alternative HTTP requests can come from wireless units like mobile phones, palmtop computers, and the like.
   7   * These units have no use for Ajax requests, and this Component can tell how Cake should respond to the different
   8   * needs of a handheld computer and a desktop machine.
   9   *
  10   * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
  11   * Copyright 2005-2007, Cake Software Foundation, Inc.
  12   *                                1785 E. Sahara Avenue, Suite 490-204
  13   *                                Las Vegas, Nevada 89104
  14   *
  15   * Licensed under The MIT License
  16   * Redistributions of files must retain the above copyright notice.
  17   *
  18   * @filesource
  19   * @copyright        Copyright 2005-2007, Cake Software Foundation, Inc.
  20   * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  21   * @package            cake
  22   * @subpackage        cake.cake.libs.controller.components
  23   * @since            CakePHP(tm) v 0.10.4.1076
  24   * @version            $Revision: 4409 $
  25   * @modifiedby        $LastChangedBy: phpnut $
  26   * @lastmodified    $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
  27   * @license            http://www.opensource.org/licenses/mit-license.php The MIT License
  28   */
  29  if (!defined('REQUEST_MOBILE_UA')) {
  30      define('REQUEST_MOBILE_UA',
  31              '(AvantGo|BlackBerry|DoCoMo|NetFront|Nokia|PalmOS|PalmSource|portalmmm|Plucker|ReqwirelessWeb|SonyEricsson|Symbian|UP\.Browser|Windows CE|Xiino)');
  32  }
  33  /**
  34   * Request object for handling alternative HTTP requests
  35   *
  36   * @package        cake
  37   * @subpackage    cake.cake.libs.controller.components
  38   *
  39   */
  40  class RequestHandlerComponent extends Object{
  41  /**
  42   * Enter description here...
  43   *
  44   * @var object
  45   * @access public
  46   */
  47      var $controller = true;
  48  /**
  49   * The layout that will be switched to for Ajax requests
  50   *
  51   * @var string
  52   * @access public
  53   * @see RequestHandler::setAjax()
  54   */
  55      var $ajaxLayout = 'ajax';
  56  /**
  57   * Determines whether or not callbacks will be fired on this component
  58   *
  59   * @var boolean
  60   * @access public
  61   */
  62      var $disableStartup = false;
  63  /**
  64   * Friendly content-type mappings used to set response types and determine
  65   * request types.  Can be modified with RequestHandler::setContent()
  66   *
  67   * @var array
  68   * @access private
  69   * @see RequestHandlerComponent::setContent
  70   */
  71      var $__requestContent = array(
  72          'js' => 'text/javascript',
  73          'css'    => 'text/css',
  74          'html'    => 'text/html',
  75          'form'    => 'application/x-www-form-urlencoded',
  76          'file'    => 'multipart/form-data',
  77          'xhtml'    => array('application/xhtml+xml', 'application/xhtml', 'text/xhtml'),
  78          'xml' => array('application/xml', 'text/xml'),
  79          'rss' => 'application/rss+xml',
  80          'atom' => 'application/atom+xml'
  81      );
  82  /**
  83   * Content-types accepted by the client.  If extension parsing is enabled in the
  84   * Router, and an extension is detected, the corresponding content-type will be
  85   * used as the overriding primary content-type accepted.
  86   *
  87   * @var array
  88   * @access private
  89   */
  90      var $__acceptTypes = array();
  91  /**
  92   * Constructor.  Parses the accepted content types accepted by the client using
  93   * HTTP_ACCEPT
  94   *
  95   * @access public
  96   * @return void
  97   */
  98  	function __construct() {
  99          $this->__acceptTypes = explode(',', env('HTTP_ACCEPT'));
 100  
 101          foreach($this->__acceptTypes as $i => $type) {
 102              if (strpos($type, ';')) {
 103                  $type = explode(';', $type);
 104                  $this->__acceptTypes[$i] = $type[0];
 105              }
 106          }
 107          parent::__construct();
 108      }
 109  /**
 110   * Startup
 111   *
 112   * @param object A reference to the controller
 113   * @return null
 114   * @access public
 115   */
 116  	function startup(&$controller) {
 117          if ($this->disableStartup) {
 118              return;
 119          }
 120          $this->setAjax($controller);
 121      }
 122  /**
 123   * Sets a controller's layout based on whether or not the current call is Ajax
 124   *
 125   * Add UTF-8 header for IE6 on XPsp2 bug if RequestHandlerComponent::isAjax()
 126   *
 127   * @param object The controller object
 128   * @return null
 129   * @access public
 130   */
 131  	function setAjax(&$controller) {
 132          if ($this->isAjax()) {
 133              $controller->layout = $this->ajaxLayout;
 134              // Add UTF-8 header for IE6 on XPsp2 bug
 135              header ('Content-Type: text/html; charset=UTF-8');
 136          }
 137      }
 138  /**
 139   * Returns true if the current call is from Ajax, false otherwise
 140   *
 141   * @return bool True if call is Ajax
 142   * @access public
 143   */
 144  	function isAjax() {
 145          if (env('HTTP_X_REQUESTED_WITH') != null) {
 146              return env('HTTP_X_REQUESTED_WITH') == "XMLHttpRequest";
 147          } else {
 148              return false;
 149          }
 150      }
 151  /**
 152   * Returns true if the current call accepts an XML response, false otherwise
 153   *
 154   * @return bool True if client accepts an XML response
 155   * @access public
 156   */
 157  	function isXml() {
 158          return $this->accepts('xml');
 159      }
 160  /**
 161   * Returns true if the current call accepts an RSS response, false otherwise
 162   *
 163   * @return bool True if client accepts an RSS response
 164   * @access public
 165   */
 166  	function isRss() {
 167          return $this->accepts('rss');
 168      }
 169  /**
 170   * Returns true if the current call accepts an RSS response, false otherwise
 171   *
 172   * @return bool True if client accepts an RSS response
 173   * @access public
 174   */
 175  	function isAtom() {
 176          return $this->accepts('atom');
 177      }
 178  /**
 179   * Returns true if the current call a POST request
 180   *
 181   * @return bool True if call is a POST
 182   * @access public
 183   */
 184  	function isPost() {
 185          return (strtolower(env('REQUEST_METHOD')) == 'post');
 186      }
 187  /**
 188   * Returns true if the current call a PUT request
 189   *
 190   * @return bool True if call is a PUT
 191   * @access public
 192   */
 193  	function isPut() {
 194          return (strtolower(env('REQUEST_METHOD')) == 'put');
 195      }
 196  /**
 197   * Returns true if the current call a GET request
 198   *
 199   * @return bool True if call is a GET
 200   * @access public
 201   */
 202  	function isGet() {
 203          return (strtolower(env('REQUEST_METHOD')) == 'get');
 204      }
 205  /**
 206   * Returns true if the current call a DELETE request
 207   *
 208   * @return bool True if call is a DELETE
 209   * @access public
 210   */
 211  	function isDelete() {
 212          return (strtolower(env('REQUEST_METHOD')) == 'delete');
 213      }
 214  /**
 215   * Gets Prototype version if call is Ajax, otherwise empty string.
 216   * The Prototype library sets a special "Prototype version" HTTP header.
 217   *
 218   * @return string Prototype version of component making Ajax call
 219   * @access public
 220   */
 221  	function getAjaxVersion() {
 222          if (env('HTTP_X_PROTOTYPE_VERSION') != null) {
 223              return env('HTTP_X_PROTOTYPE_VERSION');
 224          }
 225          return false;
 226      }
 227  /**
 228   * Adds/sets the Content-type(s) for the given name
 229   *
 230   * @param string $name The name of the Content-type, i.e. "html", "xml", "css"
 231   * @param mixed $type The Content-type or array of Content-types assigned to the name
 232   * @return void
 233   * @access public
 234   */
 235  	function setContent($name, $type) {
 236          $this->__requestContent[$name] = $type;
 237      }
 238  /**
 239   * Gets the server name from which this request was referred
 240   *
 241   * @return string Server address
 242   * @access public
 243   */
 244  	function getReferrer() {
 245          if (env('HTTP_HOST') != null) {
 246              $sess_host = env('HTTP_HOST');
 247          }
 248  
 249          if (env('HTTP_X_FORWARDED_HOST') != null) {
 250              $sess_host = env('HTTP_X_FORWARDED_HOST');
 251          }
 252          return trim(preg_replace('/:.*/', '', $sess_host));
 253      }
 254  /**
 255   * Gets remote client IP
 256   *
 257   * @return string Client IP address
 258   * @access public
 259   */
 260  	function getClientIP() {
 261          if (env('HTTP_X_FORWARDED_FOR') != null) {
 262              $ipaddr = preg_replace('/,.*/', '', env('HTTP_X_FORWARDED_FOR'));
 263          } else {
 264              if (env('HTTP_CLIENT_IP') != null) {
 265                  $ipaddr = env('HTTP_CLIENT_IP');
 266              } else {
 267                  $ipaddr = env('REMOTE_ADDR');
 268              }
 269          }
 270  
 271          if (env('HTTP_CLIENTADDRESS') != null) {
 272              $tmpipaddr = env('HTTP_CLIENTADDRESS');
 273  
 274              if (!empty($tmpipaddr)) {
 275                  $ipaddr = preg_replace('/,.*/', '', $tmpipaddr);
 276              }
 277          }
 278          return trim($ipaddr);
 279      }
 280  /**
 281   * Returns true if user agent string matches a mobile web browser
 282   *
 283   * @return bool True if user agent is a mobile web browser
 284   * @access public
 285   */
 286  	function isMobile() {
 287          return (preg_match('/' . REQUEST_MOBILE_UA . '/i', env('HTTP_USER_AGENT')) > 0);
 288      }
 289  /**
 290   * Strips extra whitespace from output
 291   *
 292   * @param string $str
 293   * @return string
 294   * @access public
 295   */
 296  	function stripWhitespace($str) {
 297          $r = preg_replace('/[\n\r\t]+/', '', $str);
 298          return preg_replace('/\s{2,}/', ' ', $r);
 299      }
 300  /**
 301   * Strips image tags from output
 302   *
 303   * @param string $str
 304   * @return string
 305   * @access public
 306   */
 307  	function stripImages($str) {
 308          $str = preg_replace('/(<a[^>]*>)(<img[^>]+alt=")([^"]*)("[^>]*>)(<\/a>)/i', '$1$3$5<br />', $str);
 309          $str = preg_replace('/(<img[^>]+alt=")([^"]*)("[^>]*>)/i', '$2<br />', $str);
 310          $str = preg_replace('/<img[^>]*>/i', '', $str);
 311          return $str;
 312      }
 313  /**
 314   * Strips scripts and stylesheets from output
 315   *
 316   * @param string $str
 317   * @return string
 318   * @access public
 319   */
 320  	function stripScripts($str) {
 321          return preg_replace('/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/i', '', $str);
 322      }
 323  /**
 324   * Strips extra whitespace, images, scripts and stylesheets from output
 325   *
 326   * @param string $str
 327   * @return string
 328   * @access public
 329   */
 330  	function stripAll($str) {
 331          $str = $this->stripWhitespace($str);
 332          $str = $this->stripImages($str);
 333          $str = $this->stripScripts($str);
 334          return $str;
 335      }
 336  /**
 337   * Strips the specified tags from output
 338   *
 339   * @return string
 340   * @access public
 341   */
 342  	function stripTags() {
 343          $params = params(func_get_args());
 344          $str = $params[0];
 345  
 346          for($i = 1; $i < count($params); $i++) {
 347              $str = preg_replace('/<' . $params[$i] . '[^>]*>/i', '', $str);
 348              $str = preg_replace('/<\/' . $params[$i] . '[^>]*>/i', '', $str);
 349          }
 350          return $str;
 351      }
 352  
 353  /**
 354   * Determines which content types the client accepts
 355   *
 356   * @param mixed $type Can be null (or no parameter), a string type name, or an
 357   *                    array of types
 358   * @return mixed If null or no parameter is passed, returns an array of content
 359   *                types the client accepts.  If a string is passed, returns true
 360   *                if the client accepts it.  If an array is passed, returns true
 361   *                if the client accepts one or more elements in the array.
 362   * @access public
 363   */
 364  	function accepts($type = null) {
 365          if ($type == null) {
 366              return $this->__acceptTypes;
 367          } else if(is_array($type)) {
 368              foreach($type as $t) {
 369                  if ($this->accepts($t) == true) {
 370                      return true;
 371                  }
 372              }
 373              return false;
 374          } else if(is_string($type)) {
 375              // If client only accepts */*, then assume default HTML browser
 376              if ($type == 'html' && $this->__acceptTypes === array('*/*')) {
 377                  return true;
 378              }
 379  
 380              if (!in_array($type, array_keys($this->__requestContent))) {
 381                  return false;
 382              }
 383  
 384              $content = $this->__requestContent[$type];
 385  
 386              if (is_array($content)) {
 387                  foreach($content as $c) {
 388                      if (in_array($c, $this->__acceptTypes)) {
 389                          return true;
 390                      }
 391                  }
 392              } else {
 393                  if (in_array($content, $this->__acceptTypes)) {
 394                      return true;
 395                  }
 396              }
 397          }
 398      }
 399  /**
 400   * Determines which content types the client prefers
 401   *
 402   * @param mixed $type
 403   * @returns mixed
 404   * @access public
 405   */
 406  	function prefers($type = null) {
 407          if ($type == null) {
 408              return $this->accepts(null);
 409          }
 410      }
 411  }
 412  ?>


Généré le : Sun Feb 25 19:27:47 2007 par Balluche grâce à PHPXref 0.7