request_handler.php
来自「Cake Framwork , Excellent」· PHP 代码 · 共 704 行 · 第 1/2 页
PHP
704 行
<?php/* SVN FILE: $Id: request_handler.php 7118 2008-06-04 20:49:29Z gwoo $ *//** * Request object for handling alternative HTTP requests * * Alternative HTTP requests can come from wireless units like mobile phones, palmtop computers, and the like. * These units have no use for Ajax requests, and this Component can tell how Cake should respond to the different * needs of a handheld computer and a desktop machine. * * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> * Copyright 2005-2008, Cake Software Foundation, Inc. * 1785 E. Sahara Avenue, Suite 490-204 * Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @filesource * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project * @package cake * @subpackage cake.cake.libs.controller.components * @since CakePHP(tm) v 0.10.4.1076 * @version $Revision: 7118 $ * @modifiedby $LastChangedBy: gwoo $ * @lastmodified $Date: 2008-06-04 13:49:29 -0700 (Wed, 04 Jun 2008) $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License */if (!defined('REQUEST_MOBILE_UA')) { define('REQUEST_MOBILE_UA', '(iPhone|MIDP|AvantGo|BlackBerry|J2ME|Opera Mini|DoCoMo|NetFront|Nokia|PalmOS|PalmSource|portalmmm|Plucker|ReqwirelessWeb|SonyEricsson|Symbian|UP\.Browser|Windows CE|Xiino)');}/** * Request object for handling HTTP requests * * @package cake * @subpackage cake.cake.libs.controller.components * */class RequestHandlerComponent extends Object {/** * The layout that will be switched to for Ajax requests * * @var string * @access public * @see RequestHandler::setAjax() */ var $ajaxLayout = 'ajax';/** * Determines whether or not callbacks will be fired on this component * * @var boolean * @access public */ var $enabled = true;/** * Holds the content-type of the response that is set when using * RequestHandler::respondAs() * * @var string * @access private */ var $__responseTypeSet = null;/** * Holds the copy of Controller::$params * * @var array * @access public */ var $params = array();/** * Friendly content-type mappings used to set response types and determine * request types. Can be modified with RequestHandler::setContent() * * @var array * @access private * @see RequestHandlerComponent::setContent */ var $__requestContent = array( 'javascript' => 'text/javascript', 'js' => 'text/javascript', 'json' => 'application/json', 'css' => 'text/css', 'html' => array('text/html', '*/*'), 'text' => 'text/plain', 'txt' => 'text/plain', 'csv' => array('application/vnd.ms-excel', 'text/plain'), 'form' => 'application/x-www-form-urlencoded', 'file' => 'multipart/form-data', 'xhtml' => array('application/xhtml+xml', 'application/xhtml', 'text/xhtml'), 'xhtml-mobile' => 'application/vnd.wap.xhtml+xml', 'xml' => array('application/xml', 'text/xml'), 'rss' => 'application/rss+xml', 'atom' => 'application/atom+xml', 'amf' => 'application/x-amf', 'wap' => array('text/vnd.wap.wml', 'text/vnd.wap.wmlscript', 'image/vnd.wap.wbmp'), 'wml' => 'text/vnd.wap.wml', 'wmlscript' => 'text/vnd.wap.wmlscript', 'wbmp' => 'image/vnd.wap.wbmp', 'pdf' => 'application/pdf', 'zip' => 'application/x-zip', 'tar' => 'application/x-tar' );/** * Content-types accepted by the client. If extension parsing is enabled in the * Router, and an extension is detected, the corresponding content-type will be * used as the overriding primary content-type accepted. * * @var array * @access private * @see Router::parseExtensions() */ var $__acceptTypes = array();/** * The template to use when rendering the given content type. * * @var string * @access private */ var $__renderType = null;/** * Contains the file extension parsed out by the Router * * @var string * @access public * @see Router::parseExtensions() */ var $ext = null;/** * Flag set when MIME types have been initialized * * @var boolean * @access private * @see RequestHandler::__initializeTypes() */ var $__typesInitialized = false;/** * Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT * */ function __construct() { $this->__acceptTypes = explode(',', env('HTTP_ACCEPT')); foreach ($this->__acceptTypes as $i => $type) { if (strpos($type, ';')) { $type = explode(';', $type); $this->__acceptTypes[$i] = $type[0]; } } parent::__construct(); }/** * Initializes the component, gets a reference to Controller::$parameters, and * checks to see if a file extension has been parsed by the Router. If yes, the * corresponding content-type is pushed onto the list of accepted content-types * as the first item. * * @param object $controller A reference to the controller * @see Router::parseExtensions() * @access public */ function initialize(&$controller) { if (isset($controller->params['url']['ext'])) { $this->ext = $controller->params['url']['ext']; } }/** * The startup method of the RequestHandler enables several automatic behaviors * related to the detection of certain properties of the HTTP request, including: * * - Disabling layout rendering for Ajax requests (based on the HTTP_X_REQUESTED_WITH header) * - If Router::parseExtensions() is enabled, the layout and template type are * switched based on the parsed extension. For example, if controller/action.xml * is requested, the view path becomes <i>app/views/controller/xml/action.ctp</i>. * - If a helper with the same name as the extension exists, it is added to the controller. * - If the extension is of a type that RequestHandler understands, it will set that * Content-type in the response header. * - If the XML data is POSTed, the data is parsed into an XML object, which is assigned * to the $data property of the controller, which can then be saved to a model object. * * @param object $controller A reference to the controller * @access public */ function startup(&$controller) { if (!$this->enabled) { return; } $this->__initializeTypes(); $controller->params['isAjax'] = $this->isAjax(); if (!empty($this->ext) && !in_array($this->ext, array('html', 'htm')) && in_array($this->ext, array_keys($this->__requestContent))) { $this->renderAs($controller, $this->ext); } elseif ($this->isAjax()) { $this->renderAs($controller, 'ajax'); } if ($this->requestedWith('xml')) { if (!class_exists('XmlNode')) { App::import('Core', 'Xml'); } $xml = new Xml(trim(file_get_contents('php://input'))); if (is_object($xml->child('data')) && count($xml->children) == 1) { $controller->data = $xml->child('data'); } else { $controller->data = $xml; } } }/** * Handles (fakes) redirects for Ajax requests using requestAction() * * @param object $controller A reference to the controller * @param mixed $url A string or array containing the redirect location * @access public */ function beforeRedirect(&$controller, $url) { if (!$this->isAjax()) { return; } foreach ($_POST as $key => $val) { unset($_POST[$key]); } echo $this->requestAction($url, array('return')); $this->_stop(); }/** * Returns true if the current HTTP request is Ajax, false otherwise * * @return boolean True if call is Ajax * @access public */ function isAjax() { return env('HTTP_X_REQUESTED_WITH') === "XMLHttpRequest"; }/** * Returns true if the current HTTP request is coming from a Flash-based client * * @return boolean True if call is from Flash * @access public */ function isFlash() { return env('HTTP_USER_AGENT') === "Shockwave Flash"; }/** * Returns true if the current request is over HTTPS, false otherwise. * * @return bool True if call is over HTTPS * @access public */ function isSSL() { return env('HTTPS'); }/** * Returns true if the current call accepts an XML response, false otherwise * * @return boolean True if client accepts an XML response * @access public */ function isXml() { return $this->prefers('xml'); }/** * Returns true if the current call accepts an RSS response, false otherwise * * @return boolean True if client accepts an RSS response * @access public */ function isRss() { return $this->prefers('rss'); }/** * Returns true if the current call accepts an Atom response, false otherwise * * @return boolean True if client accepts an RSS response * @access public */ function isAtom() { return $this->prefers('atom'); }/** * Returns true if user agent string matches a mobile web browser, or if the * client accepts WAP content. * * @return boolean True if user agent is a mobile web browser * @access public */ function isMobile() { preg_match('/' . REQUEST_MOBILE_UA . '/i', env('HTTP_USER_AGENT'), $match); if (!empty($match) || $this->accepts('wap')) { return true; } return false; }/** * Returns true if the client accepts WAP content * * @return bool * @access public */ function isWap() { return $this->prefers('wap'); }/** * Returns true if the current call a POST request * * @return boolean True if call is a POST * @access public */ function isPost() { return (strtolower(env('REQUEST_METHOD')) == 'post'); }/** * Returns true if the current call a PUT request * * @return boolean True if call is a PUT * @access public */ function isPut() { return (strtolower(env('REQUEST_METHOD')) == 'put'); }/** * Returns true if the current call a GET request * * @return boolean True if call is a GET * @access public */ function isGet() { return (strtolower(env('REQUEST_METHOD')) == 'get'); }/** * Returns true if the current call a DELETE request * * @return boolean True if call is a DELETE * @access public */ function isDelete() { return (strtolower(env('REQUEST_METHOD')) == 'delete'); }/** * Gets Prototype version if call is Ajax, otherwise empty string. * The Prototype library sets a special "Prototype version" HTTP header. * * @return string Prototype version of component making Ajax call * @access public */ function getAjaxVersion() { if (env('HTTP_X_PROTOTYPE_VERSION') != null) { return env('HTTP_X_PROTOTYPE_VERSION'); } return false; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?