request_handler.php
来自「Cake Framwork , Excellent」· PHP 代码 · 共 704 行 · 第 1/2 页
PHP
704 行
/** * Adds/sets the Content-type(s) for the given name. This method allows * content-types to be mapped to friendly aliases (or extensions), which allows * RequestHandler to automatically respond to requests of that type in the * startup method. * * @param string $name The name of the Content-type, i.e. "html", "xml", "css" * @param mixed $type The Content-type or array of Content-types assigned to the name, * i.e. "text/html", or "application/xml" * @access public */ function setContent($name, $type = null) { if (is_array($name)) { $this->__requestContent = array_merge($this->__requestContent, $name); return; } $this->__requestContent[$name] = $type; }/** * Gets the server name from which this request was referred * * @return string Server address * @access public */ function getReferrer() { if (env('HTTP_HOST') != null) { $sess_host = env('HTTP_HOST'); } if (env('HTTP_X_FORWARDED_HOST') != null) { $sess_host = env('HTTP_X_FORWARDED_HOST'); } return trim(preg_replace('/:.*/', '', $sess_host)); }/** * Gets remote client IP * * @return string Client IP address * @access public */ function getClientIP() { if (env('HTTP_X_FORWARDED_FOR') != null) { $ipaddr = preg_replace('/,.*/', '', env('HTTP_X_FORWARDED_FOR')); } else { if (env('HTTP_CLIENT_IP') != null) { $ipaddr = env('HTTP_CLIENT_IP'); } else { $ipaddr = env('REMOTE_ADDR'); } } if (env('HTTP_CLIENTADDRESS') != null) { $tmpipaddr = env('HTTP_CLIENTADDRESS'); if (!empty($tmpipaddr)) { $ipaddr = preg_replace('/,.*/', '', $tmpipaddr); } } return trim($ipaddr); }/** * Determines which content types the client accepts. Acceptance is based on * the file extension parsed by the Router (if present), and by the HTTP_ACCEPT * header. * * @param mixed $type Can be null (or no parameter), a string type name, or an * array of types * @return mixed If null or no parameter is passed, returns an array of content * types the client accepts. If a string is passed, returns true * if the client accepts it. If an array is passed, returns true * if the client accepts one or more elements in the array. * @access public * @see RequestHandlerComponent::setContent() */ function accepts($type = null) { $this->__initializeTypes(); if ($type == null) { return $this->mapType($this->__acceptTypes); } elseif (is_array($type)) { foreach ($type as $t) { if ($this->accepts($t) == true) { return true; } } return false; } elseif (is_string($type)) { if (!in_array($type, array_keys($this->__requestContent))) { return false; } $content = $this->__requestContent[$type]; if (is_array($content)) { foreach ($content as $c) { if (in_array($c, $this->__acceptTypes)) { return true; } } } else { if (in_array($content, $this->__acceptTypes)) { return true; } } } }/** * Determines the content type of the data the client has sent (i.e. in a POST request) * * @param mixed $type Can be null (or no parameter), a string type name, or an array of types * @access public */ function requestedWith($type = null) { if (!$this->isPost() && !$this->isPut()) { return null; } if ($type == null) { return $this->mapType(env('CONTENT_TYPE')); } elseif (is_array($type)) { foreach ($type as $t) { if ($this->requestedWith($t)) { return $this->mapType($t); } } return false; } elseif (is_string($type)) { return ($type == $this->mapType(env('CONTENT_TYPE'))); } }/** * Determines which content-types the client prefers. If no parameters are given, * the content-type that the client most likely prefers is returned. If $type is * an array, the first item in the array that the client accepts is returned. * Preference is determined primarily by the file extension parsed by the Router * if provided, and secondarily by the list of content-types provided in * HTTP_ACCEPT. * * @param mixed $type An optional array of 'friendly' content-type names, i.e. * 'html', 'xml', 'js', etc. * @return mixed If $type is null or not provided, the first content-type in the * list, based on preference, is returned. * @access public * @see RequestHandlerComponent::setContent() */ function prefers($type = null) { $this->__initializeTypes(); if ($type == null) { if (empty($this->ext)) { $accept = $this->accepts(null); if (is_array($accept)) { return $accept[0]; } return $accept; } else { return $this->ext; } } App::import('Core', 'Set'); $types = Set::normalize($type, false); $accepts = array(); foreach ($types as $type) { if ($this->accepts($type)) { $accepts[] = $type; } } if (count($accepts) == 0) { return false; } elseif (count($accepts) == 1) { return $accepts[0]; } else { $accepts = array_intersect($this->__acceptTypes, $accepts); return $accepts[0]; } }/** * Sets the layout and template paths for the content type defined by $type. * * @param object $controller A reference to a controller object * @param string $type Type of response to send (e.g: 'ajax') * @access public * @see RequestHandlerComponent::setContent() * @see RequestHandlerComponent::respondAs() */ function renderAs(&$controller, $type) { $this->__initializeTypes(); $options = array('charset' => 'UTF-8'); if (Configure::read('App.encoding') !== null) { $options = array('charset' => Configure::read('App.encoding')); } if ($type == 'ajax') { $controller->layout = $this->ajaxLayout; return $this->respondAs('html', $options); } $controller->ext = '.ctp'; if (empty($this->__renderType)) { $controller->viewPath .= '/' . $type; } else { $controller->viewPath = preg_replace("/\/{$type}$/", '/' . $type, $controller->viewPath); } $this->__renderType = $type; $controller->layoutPath = $type; if (in_array($type, array_keys($this->__requestContent))) { $this->respondAs($type, $options); } $helper = ucfirst($type); if (!in_array($helper, $controller->helpers) && !array_key_exists($helper, $controller->helpers)) { if (App::import('Helper', $helper)) { $controller->helpers[] = $helper; } } }/** * Sets the response header based on type map index name. If DEBUG is greater * than 2, the header is not set. * * @param mixed $type Friendly type name, i.e. 'html' or 'xml', or a full * content-type, like 'application/x-shockwave'. * @param array $options If $type is a friendly type name that is associated with * more than one type of content, $index is used to select * which content-type to use. * @return boolean Returns false if the friendly type name given in $type does * not exist in the type map, or if the Content-type header has * already been set by this method. * @access public * @see RequestHandlerComponent::setContent() */ function respondAs($type, $options = array()) { $this->__initializeTypes(); if ($this->__responseTypeSet != null) { return false; } if (!array_key_exists($type, $this->__requestContent) && strpos($type, '/') === false) { return false; } $options = array_merge(array('index' => 0, 'charset' => null, 'attachment' => false), $options); if (strpos($type, '/') === false && isset($this->__requestContent[$type])) { $cType = null; if (is_array($this->__requestContent[$type]) && isset($this->__requestContent[$type][$options['index']])) { $cType = $this->__requestContent[$type][$options['index']]; } elseif (is_array($this->__requestContent[$type]) && isset($this->__requestContent[$type][0])) { $cType = $this->__requestContent[$type][0]; } elseif (isset($this->__requestContent[$type])) { $cType = $this->__requestContent[$type]; } else { return false; } if (is_array($cType)) { if ($this->prefers($cType)) { $cType = $this->prefers($cType); } else { $cType = $cType[0]; } } } else { $cType = $type; } if ($cType != null) { $header = 'Content-type: ' . $cType; if (!empty($options['charset'])) { $header .= '; charset=' . $options['charset']; } if (!empty($options['attachment'])) { header('Content-Disposition: attachment; filename="' . $options['attachment'] . '"'); } if (Configure::read() < 2 && !defined('CAKEPHP_SHELL')) { @header($header); } $this->__responseTypeSet = $cType; return true; } else { return false; } }/** * Returns the current response type (Content-type header), or null if none has been set * * @return mixed A string content type alias, or raw content type if no alias map exists, * otherwise null * @access public */ function responseType() { if ($this->__responseTypeSet == null) { return null; } return $this->mapType($this->__responseTypeSet); }/** * Maps a content-type back to an alias * * @param mixed $type Content type * @return mixed Alias * @access public */ function mapType($ctype) { if (is_array($ctype)) { $out = array(); foreach ($ctype as $t) { $out[] = $this->mapType($t); } return $out; } else { $keys = array_keys($this->__requestContent); $count = count($keys); for ($i = 0; $i < $count; $i++) { $name = $keys[$i]; $type = $this->__requestContent[$name]; if (is_array($type) && in_array($ctype, $type)) { return $name; } elseif (!is_array($type) && $type == $ctype) { return $name; } } return $ctype; } }/** * Initializes MIME types * * @return void * @access private */ function __initializeTypes() { if ($this->__typesInitialized) { return; } if (isset($this->__requestContent[$this->ext])) { $content = $this->__requestContent[$this->ext]; if (is_array($content)) { $content = $content[0]; } array_unshift($this->__acceptTypes, $content); } $this->__typesInitialized = true; }}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?