Browse Source

Web WIP

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
72ff81f73d
  1. 4
      framework/base/Component.php
  2. 47
      framework/util/FileHelper.php
  3. 187
      framework/util/mimeTypes.php
  4. 23
      framework/web/Cookie.php
  5. 27
      framework/web/Pagination.php
  6. 352
      framework/web/Request.php
  7. 144
      framework/web/Response.php
  8. 137
      framework/web/Sort.php

4
framework/base/Component.php

@ -308,7 +308,7 @@ class Component extends \yii\base\Object
* where `$event` is an [[Event]] object which includes parameters associated with the event. * where `$event` is an [[Event]] object which includes parameters associated with the event.
* *
* @param string $name the event name * @param string $name the event name
* @param callback $handler the event handler * @param string|array|\Closure $handler the event handler
* @see off * @see off
*/ */
public function on($name, $handler) public function on($name, $handler)
@ -320,7 +320,7 @@ class Component extends \yii\base\Object
* Detaches an existing event handler. * Detaches an existing event handler.
* This method is the opposite of [[on]]. * This method is the opposite of [[on]].
* @param string $name event name * @param string $name event name
* @param callback $handler the event handler to be removed * @param string|array|\Closure $handler the event handler to be removed
* @return boolean if a handler is found and detached * @return boolean if a handler is found and detached
* @see on * @see on
*/ */

47
framework/util/FileHelper.php

@ -87,4 +87,51 @@ class FileHelper
$desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $sourceLanguage . DIRECTORY_SEPARATOR . basename($file); $desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $sourceLanguage . DIRECTORY_SEPARATOR . basename($file);
return is_file($desiredFile) ? $desiredFile : $file; return is_file($desiredFile) ? $desiredFile : $file;
} }
/**
* Determines the MIME type of the specified file.
* This method will first try to determine the MIME type based on
* [finfo_open](http://php.net/manual/en/function.finfo-open.php). If this doesn't work, it will
* fall back to [[getMimeTypeByExtension()]].
* @param string $file the file name.
* @param string $magicFile name of the optional magic database file, usually something like `/path/to/magic.mime`.
* This will be passed as the second parameter to [finfo_open](http://php.net/manual/en/function.finfo-open.php).
* @param boolean $checkExtension whether to use the file extension to determine the MIME type in case
* `finfo_open()` cannot determine it.
* @return string the MIME type (e.g. `text/plain`). Null is returned if the MIME type cannot be determined.
*/
public static function getMimeType($file, $magicFile = null, $checkExtension = true)
{
if (function_exists('finfo_open')) {
$info = finfo_open(FILEINFO_MIME_TYPE, $magicFile);
if ($info && ($result = finfo_file($info, $file)) !== false) {
return $result;
}
}
return $checkExtension ? self::getMimeTypeByExtension($file) : null;
}
/**
* Determines the MIME type based on the extension name of the specified file.
* This method will use a local map between extension names and MIME types.
* @param string $file the file name.
* @param string $magicFile the path of the file that contains all available MIME type information.
* If this is not set, the default file aliased by `@yii/util/mimeTypes.php` will be used.
* @return string the MIME type. Null is returned if the MIME type cannot be determined.
*/
public static function getMimeTypeByExtension($file, $magicFile = null)
{
if ($magicFile === null) {
$magicFile = \Yii::getAlias('@yii/util/mimeTypes.php');
}
$mimeTypes = require($magicFile);
if (($ext = pathinfo($file, PATHINFO_EXTENSION)) !== '') {
$ext = strtolower($ext);
if (isset($mimeTypes[$ext])) {
return $mimeTypes[$ext];
}
}
return null;
}
} }

187
framework/util/mimeTypes.php

@ -0,0 +1,187 @@
<?php
/**
* MIME types.
*
* This file contains most commonly used MIME types
* according to file extension names.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @since 2.0
*/
return array(
'ai' => 'application/postscript',
'aif' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'anx' => 'application/annodex',
'asc' => 'text/plain',
'au' => 'audio/basic',
'avi' => 'video/x-msvideo',
'axa' => 'audio/annodex',
'axv' => 'video/annodex',
'bcpio' => 'application/x-bcpio',
'bin' => 'application/octet-stream',
'bmp' => 'image/bmp',
'c' => 'text/plain',
'cc' => 'text/plain',
'ccad' => 'application/clariscad',
'cdf' => 'application/x-netcdf',
'class' => 'application/octet-stream',
'cpio' => 'application/x-cpio',
'cpt' => 'application/mac-compactpro',
'csh' => 'application/x-csh',
'css' => 'text/css',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dms' => 'application/octet-stream',
'doc' => 'application/msword',
'drw' => 'application/drafting',
'dvi' => 'application/x-dvi',
'dwg' => 'application/acad',
'dxf' => 'application/dxf',
'dxr' => 'application/x-director',
'eps' => 'application/postscript',
'etx' => 'text/x-setext',
'exe' => 'application/octet-stream',
'ez' => 'application/andrew-inset',
'f' => 'text/plain',
'f90' => 'text/plain',
'flac' => 'audio/flac',
'fli' => 'video/x-fli',
'flv' => 'video/x-flv',
'gif' => 'image/gif',
'gtar' => 'application/x-gtar',
'gz' => 'application/x-gzip',
'h' => 'text/plain',
'hdf' => 'application/x-hdf',
'hh' => 'text/plain',
'hqx' => 'application/mac-binhex40',
'htm' => 'text/html',
'html' => 'text/html',
'ice' => 'x-conference/x-cooltalk',
'ief' => 'image/ief',
'iges' => 'model/iges',
'igs' => 'model/iges',
'ips' => 'application/x-ipscript',
'ipx' => 'application/x-ipix',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'js' => 'application/x-javascript',
'kar' => 'audio/midi',
'latex' => 'application/x-latex',
'lha' => 'application/octet-stream',
'lsp' => 'application/x-lisp',
'lzh' => 'application/octet-stream',
'm' => 'text/plain',
'man' => 'application/x-troff-man',
'me' => 'application/x-troff-me',
'mesh' => 'model/mesh',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'mif' => 'application/vnd.mif',
'mime' => 'www/mime',
'mov' => 'video/quicktime',
'movie' => 'video/x-sgi-movie',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'mpe' => 'video/mpeg',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpga' => 'audio/mpeg',
'ms' => 'application/x-troff-ms',
'msh' => 'model/mesh',
'nc' => 'application/x-netcdf',
'oga' => 'audio/ogg',
'ogg' => 'audio/ogg',
'ogv' => 'video/ogg',
'ogx' => 'application/ogg',
'oda' => 'application/oda',
'pbm' => 'image/x-portable-bitmap',
'pdb' => 'chemical/x-pdb',
'pdf' => 'application/pdf',
'pgm' => 'image/x-portable-graymap',
'pgn' => 'application/x-chess-pgn',
'png' => 'image/png',
'pnm' => 'image/x-portable-anymap',
'pot' => 'application/mspowerpoint',
'ppm' => 'image/x-portable-pixmap',
'pps' => 'application/mspowerpoint',
'ppt' => 'application/mspowerpoint',
'ppz' => 'application/mspowerpoint',
'pre' => 'application/x-freelance',
'prt' => 'application/pro_eng',
'ps' => 'application/postscript',
'qt' => 'video/quicktime',
'ra' => 'audio/x-realaudio',
'ram' => 'audio/x-pn-realaudio',
'ras' => 'image/cmu-raster',
'rgb' => 'image/x-rgb',
'rm' => 'audio/x-pn-realaudio',
'roff' => 'application/x-troff',
'rpm' => 'audio/x-pn-realaudio-plugin',
'rtf' => 'text/rtf',
'rtx' => 'text/richtext',
'scm' => 'application/x-lotusscreencam',
'set' => 'application/set',
'sgm' => 'text/sgml',
'sgml' => 'text/sgml',
'sh' => 'application/x-sh',
'shar' => 'application/x-shar',
'silo' => 'model/mesh',
'sit' => 'application/x-stuffit',
'skd' => 'application/x-koan',
'skm' => 'application/x-koan',
'skp' => 'application/x-koan',
'skt' => 'application/x-koan',
'smi' => 'application/smil',
'smil' => 'application/smil',
'snd' => 'audio/basic',
'sol' => 'application/solids',
'spl' => 'application/x-futuresplash',
'spx' => 'audio/ogg',
'src' => 'application/x-wais-source',
'step' => 'application/STEP',
'stl' => 'application/SLA',
'stp' => 'application/STEP',
'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc',
'swf' => 'application/x-shockwave-flash',
't' => 'application/x-troff',
'tar' => 'application/x-tar',
'tcl' => 'application/x-tcl',
'tex' => 'application/x-tex',
'texi' => 'application/x-texinfo',
'texinfo' => 'application/x-texinfo',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'tr' => 'application/x-troff',
'tsi' => 'audio/TSP-audio',
'tsp' => 'application/dsptype',
'tsv' => 'text/tab-separated-values',
'txt' => 'text/plain',
'unv' => 'application/i-deas',
'ustar' => 'application/x-ustar',
'vcd' => 'application/x-cdlink',
'vda' => 'application/vda',
'viv' => 'video/vnd.vivo',
'vivo' => 'video/vnd.vivo',
'vrml' => 'model/vrml',
'wav' => 'audio/x-wav',
'wrl' => 'model/vrml',
'xbm' => 'image/x-xbitmap',
'xlc' => 'application/vnd.ms-excel',
'xll' => 'application/vnd.ms-excel',
'xlm' => 'application/vnd.ms-excel',
'xls' => 'application/vnd.ms-excel',
'xlw' => 'application/vnd.ms-excel',
'xml' => 'application/xml',
'xpm' => 'image/x-xpixmap',
'xspf' => 'application/xspf+xml',
'xwd' => 'image/x-xwindowdump',
'xyz' => 'chemical/x-pdb',
'zip' => 'application/zip',
);

23
framework/web/Cookie.php

@ -1,22 +1,19 @@
<?php <?php
/** /**
* CHttpCookie class file. * Cookie class file.
* *
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
/** /**
* A CHttpCookie instance stores a single cookie, including the cookie name, value, domain, path, expire, and secure. * Cookie represents information related with a cookie, such as [[name]], [[value]], [[domain]], etc.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$ * @since 2.0
* @package system.web
* @since 1.0
*/ */
class CHttpCookie extends CComponent class Cookie extends \yii\base\Object
{ {
/** /**
* @var string name of the cookie * @var string name of the cookie
@ -31,7 +28,8 @@ class CHttpCookie extends CComponent
*/ */
public $domain = ''; public $domain = '';
/** /**
* @var integer the timestamp at which the cookie expires. This is the server timestamp. Defaults to 0, meaning "until the browser is closed". * @var integer the timestamp at which the cookie expires. This is the server timestamp.
* Defaults to 0, meaning "until the browser is closed".
*/ */
public $expire = 0; public $expire = 0;
/** /**
@ -45,8 +43,7 @@ class CHttpCookie extends CComponent
/** /**
* @var boolean whether the cookie should be accessible only through the HTTP protocol. * @var boolean whether the cookie should be accessible only through the HTTP protocol.
* By setting this property to true, the cookie will not be accessible by scripting languages, * By setting this property to true, the cookie will not be accessible by scripting languages,
* such as JavaScript, which can effectly help to reduce identity theft through XSS attacks. * such as JavaScript, which can effectively help to reduce identity theft through XSS attacks.
* Note, this property is only effective for PHP 5.2.0 or above.
*/ */
public $httpOnly = false; public $httpOnly = false;
@ -54,10 +51,12 @@ class CHttpCookie extends CComponent
* Constructor. * Constructor.
* @param string $name name of this cookie * @param string $name name of this cookie
* @param string $value value of this cookie * @param string $value value of this cookie
* @param array $config name-value pairs that will be used to initialize the object properties
*/ */
public function __construct($name,$value) public function __construct($name, $value, $config = array())
{ {
$this->name = $name; $this->name = $name;
$this->value = $value; $this->value = $value;
parent::__construct($config);
} }
} }

27
framework/web/Pagination.php

@ -123,9 +123,10 @@ class CPagination extends CComponent
*/ */
public function setPageSize($value) public function setPageSize($value)
{ {
if(($this->_pageSize=$value)<=0) if (($this->_pageSize = $value) <= 0) {
$this->_pageSize = self::DEFAULT_PAGE_SIZE; $this->_pageSize = self::DEFAULT_PAGE_SIZE;
} }
}
/** /**
* @return integer total number of items. Defaults to 0. * @return integer total number of items. Defaults to 0.
@ -140,9 +141,10 @@ class CPagination extends CComponent
*/ */
public function setItemCount($value) public function setItemCount($value)
{ {
if(($this->_itemCount=$value)<0) if (($this->_itemCount = $value) < 0) {
$this->_itemCount = 0; $this->_itemCount = 0;
} }
}
/** /**
* @return integer number of pages * @return integer number of pages
@ -158,23 +160,22 @@ class CPagination extends CComponent
*/ */
public function getCurrentPage($recalculate = true) public function getCurrentPage($recalculate = true)
{ {
if($this->_currentPage===null || $recalculate) if ($this->_currentPage === null || $recalculate) {
{ if (isset($_GET[$this->pageVar])) {
if(isset($_GET[$this->pageVar]))
{
$this->_currentPage = (int)$_GET[$this->pageVar] - 1; $this->_currentPage = (int)$_GET[$this->pageVar] - 1;
if($this->validateCurrentPage) if ($this->validateCurrentPage) {
{
$pageCount = $this->getPageCount(); $pageCount = $this->getPageCount();
if($this->_currentPage>=$pageCount) if ($this->_currentPage >= $pageCount) {
$this->_currentPage = $pageCount - 1; $this->_currentPage = $pageCount - 1;
} }
if($this->_currentPage<0) }
if ($this->_currentPage < 0) {
$this->_currentPage = 0; $this->_currentPage = 0;
} }
else } else {
$this->_currentPage = 0; $this->_currentPage = 0;
} }
}
return $this->_currentPage; return $this->_currentPage;
} }
@ -202,9 +203,11 @@ class CPagination extends CComponent
{ {
$params = $this->params === null ? $_GET : $this->params; $params = $this->params === null ? $_GET : $this->params;
if ($page > 0) // page 0 is the default if ($page > 0) // page 0 is the default
{
$params[$this->pageVar] = $page + 1; $params[$this->pageVar] = $page + 1;
else } else {
unset($params[$this->pageVar]); unset($params[$this->pageVar]);
}
return $controller->createUrl($this->route, $params); return $controller->createUrl($this->route, $params);
} }

352
framework/web/Request.php

@ -1,64 +1,19 @@
<?php <?php
/** /**
* CHttpRequest and CCookieCollection class file. * Request class file.
* *
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\web;
/** /**
* CHttpRequest encapsulates the $_SERVER variable and resolves its inconsistency among different Web servers.
*
* CHttpRequest also manages the cookies sent from and sent to the user.
* By setting {@link enableCookieValidation} to true,
* cookies sent from the user will be validated to see if they are tampered.
* The property {@link getCookies cookies} returns the collection of cookies.
* For more details, see {@link CCookieCollection}.
*
* CHttpRequest is a default application component loaded by {@link CWebApplication}. It can be
* accessed via {@link CWebApplication::getRequest()}.
*
* @property string $url Part of the request URL after the host info.
* @property string $hostInfo Schema and hostname part (with port number if needed) of the request URL (e.g. http://www.yiiframework.com).
* @property string $baseUrl The relative URL for the application.
* @property string $scriptUrl The relative URL of the entry script.
* @property string $pathInfo Part of the request URL that is after the entry script and before the question mark.
* Note, the returned pathinfo is decoded starting from 1.1.4.
* Prior to 1.1.4, whether it is decoded or not depends on the server configuration
* (in most cases it is not decoded).
* @property string $requestUri The request URI portion for the currently requested URL.
* @property string $queryString Part of the request URL that is after the question mark.
* @property boolean $isSecureConnection If the request is sent via secure channel (https).
* @property string $requestType Request type, such as GET, POST, HEAD, PUT, DELETE.
* @property boolean $isPostRequest Whether this is a POST request.
* @property boolean $isDeleteRequest Whether this is a DELETE request.
* @property boolean $isPutRequest Whether this is a PUT request.
* @property boolean $isAjaxRequest Whether this is an AJAX (XMLHttpRequest) request.
* @property boolean $isFlashRequest Whether this is an Adobe Flash or Adobe Flex request.
* @property string $serverName Server name.
* @property integer $serverPort Server port number.
* @property string $urlReferrer URL referrer, null if not present.
* @property string $userAgent User agent, null if not present.
* @property string $userHostAddress User IP address.
* @property string $userHost User host name, null if cannot be determined.
* @property string $scriptFile Entry script file path (processed w/ realpath()).
* @property array $browser User browser capabilities.
* @property string $acceptTypes User browser accept types, null if not present.
* @property integer $port Port number for insecure requests.
* @property integer $securePort Port number for secure requests.
* @property CCookieCollection $cookies The cookie collection.
* @property string $preferredLanguage The user preferred language.
* @property string $csrfToken The random token for CSRF validation.
*
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$ * @since 2.0
* @package system.web
* @since 1.0
*/ */
class CHttpRequest extends CApplicationComponent class Request extends \yii\base\Request
{ {
/** /**
* @var boolean whether cookies should be validated to ensure they are not tampered. Defaults to false. * @var boolean whether cookies should be validated to ensure they are not tampered. Defaults to false.
@ -76,7 +31,7 @@ class CHttpRequest extends CApplicationComponent
public $enableCsrfValidation = false; public $enableCsrfValidation = false;
/** /**
* @var string the name of the token used to prevent CSRF. Defaults to 'YII_CSRF_TOKEN'. * @var string the name of the token used to prevent CSRF. Defaults to 'YII_CSRF_TOKEN'.
* This property is effectively only when {@link enableCsrfValidation} is true. * This property is effective only when {@link enableCsrfValidation} is true.
*/ */
public $csrfTokenName = 'YII_CSRF_TOKEN'; public $csrfTokenName = 'YII_CSRF_TOKEN';
/** /**
@ -116,23 +71,25 @@ class CHttpRequest extends CApplicationComponent
*/ */
protected function normalizeRequest() protected function normalizeRequest()
{ {
// normalize request if (get_magic_quotes_gpc()) {
if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) if (isset($_GET)) {
{
if(isset($_GET))
$_GET = $this->stripSlashes($_GET); $_GET = $this->stripSlashes($_GET);
if(isset($_POST)) }
if (isset($_POST)) {
$_POST = $this->stripSlashes($_POST); $_POST = $this->stripSlashes($_POST);
if(isset($_REQUEST)) }
if (isset($_REQUEST)) {
$_REQUEST = $this->stripSlashes($_REQUEST); $_REQUEST = $this->stripSlashes($_REQUEST);
if(isset($_COOKIE)) }
if (isset($_COOKIE)) {
$_COOKIE = $this->stripSlashes($_COOKIE); $_COOKIE = $this->stripSlashes($_COOKIE);
} }
if($this->enableCsrfValidation)
\Yii::$application->attachEventHandler('onBeginRequest',array($this,'validateCsrfToken'));
} }
if ($this->enableCsrfValidation) {
\Yii::$application->on('beginRequest', array($this, 'validateCsrfToken'));
}
}
/** /**
* Strips slashes from input data. * Strips slashes from input data.
@ -140,7 +97,7 @@ class CHttpRequest extends CApplicationComponent
* @param mixed $data input data to be processed * @param mixed $data input data to be processed
* @return mixed processed data * @return mixed processed data
*/ */
public function stripSlashes(&$data) public function stripSlashes($data)
{ {
return is_array($data) ? array_map(array($this, 'stripSlashes'), $data) : stripslashes($data); return is_array($data) ? array_map(array($this, 'stripSlashes'), $data) : stripslashes($data);
} }
@ -201,11 +158,13 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function getDelete($name, $defaultValue = null) public function getDelete($name, $defaultValue = null)
{ {
if($this->getIsDeleteViaPostRequest()) if ($this->getIsDeleteViaPostRequest()) {
return $this->getPost($name, $defaultValue); return $this->getPost($name, $defaultValue);
}
if($this->_deleteParams===null) if ($this->_deleteParams === null) {
$this->_deleteParams = $this->getIsDeleteRequest() ? $this->getRestParams() : array(); $this->_deleteParams = $this->getIsDeleteRequest() ? $this->getRestParams() : array();
}
return isset($this->_deleteParams[$name]) ? $this->_deleteParams[$name] : $defaultValue; return isset($this->_deleteParams[$name]) ? $this->_deleteParams[$name] : $defaultValue;
} }
@ -222,11 +181,13 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function getPut($name, $defaultValue = null) public function getPut($name, $defaultValue = null)
{ {
if($this->getIsPutViaPostReqest()) if ($this->getIsPutViaPostReqest()) {
return $this->getPost($name, $defaultValue); return $this->getPost($name, $defaultValue);
}
if($this->_putParams===null) if ($this->_putParams === null) {
$this->_putParams = $this->getIsPutRequest() ? $this->getRestParams() : array(); $this->_putParams = $this->getIsPutRequest() ? $this->getRestParams() : array();
}
return isset($this->_putParams[$name]) ? $this->_putParams[$name] : $defaultValue; return isset($this->_putParams[$name]) ? $this->_putParams[$name] : $defaultValue;
} }
@ -238,10 +199,11 @@ class CHttpRequest extends CApplicationComponent
protected function getRestParams() protected function getRestParams()
{ {
$result = array(); $result = array();
if(function_exists('mb_parse_str')) if (function_exists('mb_parse_str')) {
mb_parse_str(file_get_contents('php://input'), $result); mb_parse_str(file_get_contents('php://input'), $result);
else } else {
parse_str(file_get_contents('php://input'), $result); parse_str(file_get_contents('php://input'), $result);
}
return $result; return $result;
} }
@ -266,40 +228,41 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function getHostInfo($schema = '') public function getHostInfo($schema = '')
{ {
if($this->_hostInfo===null) if ($this->_hostInfo === null) {
{ if ($secure = $this->getIsSecureConnection()) {
if($secure=$this->getIsSecureConnection())
$http = 'https'; $http = 'https';
else } else {
$http = 'http'; $http = 'http';
if(isset($_SERVER['HTTP_HOST'])) }
if (isset($_SERVER['HTTP_HOST'])) {
$this->_hostInfo = $http . '://' . $_SERVER['HTTP_HOST']; $this->_hostInfo = $http . '://' . $_SERVER['HTTP_HOST'];
else } else {
{
$this->_hostInfo = $http . '://' . $_SERVER['SERVER_NAME']; $this->_hostInfo = $http . '://' . $_SERVER['SERVER_NAME'];
$port = $secure ? $this->getSecurePort() : $this->getPort(); $port = $secure ? $this->getSecurePort() : $this->getPort();
if(($port!==80 && !$secure) || ($port!==443 && $secure)) if (($port !== 80 && !$secure) || ($port !== 443 && $secure)) {
$this->_hostInfo .= ':' . $port; $this->_hostInfo .= ':' . $port;
} }
} }
if($schema!=='') }
{ if ($schema !== '') {
$secure = $this->getIsSecureConnection(); $secure = $this->getIsSecureConnection();
if($secure && $schema==='https' || !$secure && $schema==='http') if ($secure && $schema === 'https' || !$secure && $schema === 'http') {
return $this->_hostInfo; return $this->_hostInfo;
}
$port = $schema === 'https' ? $this->getSecurePort() : $this->getPort(); $port = $schema === 'https' ? $this->getSecurePort() : $this->getPort();
if($port!==80 && $schema==='http' || $port!==443 && $schema==='https') if ($port !== 80 && $schema === 'http' || $port !== 443 && $schema === 'https') {
$port = ':' . $port; $port = ':' . $port;
else } else {
$port = ''; $port = '';
}
$pos = strpos($this->_hostInfo, ':'); $pos = strpos($this->_hostInfo, ':');
return $schema . substr($this->_hostInfo, $pos, strcspn($this->_hostInfo, ':', $pos + 1) + 1) . $port; return $schema . substr($this->_hostInfo, $pos, strcspn($this->_hostInfo, ':', $pos + 1) + 1) . $port;
} } else {
else
return $this->_hostInfo; return $this->_hostInfo;
} }
}
/** /**
* Sets the schema and host part of the application URL. * Sets the schema and host part of the application URL.
@ -322,8 +285,9 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function getBaseUrl($absolute = false) public function getBaseUrl($absolute = false)
{ {
if($this->_baseUrl===null) if ($this->_baseUrl === null) {
$this->_baseUrl = rtrim(dirname($this->getScriptUrl()), '\\/'); $this->_baseUrl = rtrim(dirname($this->getScriptUrl()), '\\/');
}
return $absolute ? $this->getHostInfo() . $this->_baseUrl : $this->_baseUrl; return $absolute ? $this->getHostInfo() . $this->_baseUrl : $this->_baseUrl;
} }
@ -345,22 +309,30 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function getScriptUrl() public function getScriptUrl()
{ {
if($this->_scriptUrl===null) if ($this->_scriptUrl === null) {
{
$scriptName = basename($_SERVER['SCRIPT_FILENAME']); $scriptName = basename($_SERVER['SCRIPT_FILENAME']);
if(basename($_SERVER['SCRIPT_NAME'])===$scriptName) if (basename($_SERVER['SCRIPT_NAME']) === $scriptName) {
$this->_scriptUrl = $_SERVER['SCRIPT_NAME']; $this->_scriptUrl = $_SERVER['SCRIPT_NAME'];
else if(basename($_SERVER['PHP_SELF'])===$scriptName) } else {
if (basename($_SERVER['PHP_SELF']) === $scriptName) {
$this->_scriptUrl = $_SERVER['PHP_SELF']; $this->_scriptUrl = $_SERVER['PHP_SELF'];
else if(isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME'])===$scriptName) } else {
if (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $scriptName) {
$this->_scriptUrl = $_SERVER['ORIG_SCRIPT_NAME']; $this->_scriptUrl = $_SERVER['ORIG_SCRIPT_NAME'];
else if(($pos=strpos($_SERVER['PHP_SELF'],'/'.$scriptName))!==false) } else {
if (($pos = strpos($_SERVER['PHP_SELF'], '/' . $scriptName)) !== false) {
$this->_scriptUrl = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $scriptName; $this->_scriptUrl = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $scriptName;
else if(isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'],$_SERVER['DOCUMENT_ROOT'])===0) } else {
if (isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['DOCUMENT_ROOT']) === 0) {
$this->_scriptUrl = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_FILENAME'])); $this->_scriptUrl = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_FILENAME']));
else } else {
throw new CException(Yii::t('yii', 'CHttpRequest is unable to determine the entry script URL.')); throw new CException(Yii::t('yii', 'CHttpRequest is unable to determine the entry script URL.'));
} }
}
}
}
}
}
return $this->_scriptUrl; return $this->_scriptUrl;
} }
@ -387,25 +359,30 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function getPathInfo() public function getPathInfo()
{ {
if($this->_pathInfo===null) if ($this->_pathInfo === null) {
{
$pathInfo = $this->getRequestUri(); $pathInfo = $this->getRequestUri();
if(($pos=strpos($pathInfo,'?'))!==false) if (($pos = strpos($pathInfo, '?')) !== false) {
$pathInfo = substr($pathInfo, 0, $pos); $pathInfo = substr($pathInfo, 0, $pos);
}
$pathInfo = $this->decodePathInfo($pathInfo); $pathInfo = $this->decodePathInfo($pathInfo);
$scriptUrl = $this->getScriptUrl(); $scriptUrl = $this->getScriptUrl();
$baseUrl = $this->getBaseUrl(); $baseUrl = $this->getBaseUrl();
if(strpos($pathInfo,$scriptUrl)===0) if (strpos($pathInfo, $scriptUrl) === 0) {
$pathInfo = substr($pathInfo, strlen($scriptUrl)); $pathInfo = substr($pathInfo, strlen($scriptUrl));
else if($baseUrl==='' || strpos($pathInfo,$baseUrl)===0) } else {
if ($baseUrl === '' || strpos($pathInfo, $baseUrl) === 0) {
$pathInfo = substr($pathInfo, strlen($baseUrl)); $pathInfo = substr($pathInfo, strlen($baseUrl));
else if(strpos($_SERVER['PHP_SELF'],$scriptUrl)===0) } else {
if (strpos($_SERVER['PHP_SELF'], $scriptUrl) === 0) {
$pathInfo = substr($_SERVER['PHP_SELF'], strlen($scriptUrl)); $pathInfo = substr($_SERVER['PHP_SELF'], strlen($scriptUrl));
else } else {
throw new CException(Yii::t('yii', 'CHttpRequest is unable to determine the path info of the request.')); throw new CException(Yii::t('yii', 'CHttpRequest is unable to determine the path info of the request.'));
}
}
}
$this->_pathInfo = trim($pathInfo, '/'); $this->_pathInfo = trim($pathInfo, '/');
} }
@ -435,12 +412,10 @@ class CHttpRequest extends CApplicationComponent
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)*$%xs', $pathInfo)) )*$%xs', $pathInfo)
{ ) {
return $pathInfo; return $pathInfo;
} } else {
else
{
return utf8_encode($pathInfo); return utf8_encode($pathInfo);
} }
} }
@ -455,30 +430,33 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function getRequestUri() public function getRequestUri()
{ {
if($this->_requestUri===null) if ($this->_requestUri === null) {
{
if (isset($_SERVER['HTTP_X_REWRITE_URL'])) // IIS if (isset($_SERVER['HTTP_X_REWRITE_URL'])) // IIS
$this->_requestUri=$_SERVER['HTTP_X_REWRITE_URL'];
else if(isset($_SERVER['REQUEST_URI']))
{ {
$this->_requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
} else {
if (isset($_SERVER['REQUEST_URI'])) {
$this->_requestUri = $_SERVER['REQUEST_URI']; $this->_requestUri = $_SERVER['REQUEST_URI'];
if(!empty($_SERVER['HTTP_HOST'])) if (!empty($_SERVER['HTTP_HOST'])) {
{ if (strpos($this->_requestUri, $_SERVER['HTTP_HOST']) !== false) {
if(strpos($this->_requestUri,$_SERVER['HTTP_HOST'])!==false)
$this->_requestUri = preg_replace('/^\w+:\/\/[^\/]+/', '', $this->_requestUri); $this->_requestUri = preg_replace('/^\w+:\/\/[^\/]+/', '', $this->_requestUri);
} }
else } else {
$this->_requestUri = preg_replace('/^(http|https):\/\/[^\/]+/i', '', $this->_requestUri); $this->_requestUri = preg_replace('/^(http|https):\/\/[^\/]+/i', '', $this->_requestUri);
} }
else if(isset($_SERVER['ORIG_PATH_INFO'])) // IIS 5.0 CGI } else {
if (isset($_SERVER['ORIG_PATH_INFO'])) // IIS 5.0 CGI
{ {
$this->_requestUri = $_SERVER['ORIG_PATH_INFO']; $this->_requestUri = $_SERVER['ORIG_PATH_INFO'];
if(!empty($_SERVER['QUERY_STRING'])) if (!empty($_SERVER['QUERY_STRING'])) {
$this->_requestUri .= '?' . $_SERVER['QUERY_STRING']; $this->_requestUri .= '?' . $_SERVER['QUERY_STRING'];
} }
else } else {
throw new CException(Yii::t('yii', 'CHttpRequest is unable to determine the request URI.')); throw new CException(Yii::t('yii', 'CHttpRequest is unable to determine the request URI.'));
} }
}
}
}
return $this->_requestUri; return $this->_requestUri;
} }
@ -510,8 +488,9 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function getRequestType() public function getRequestType()
{ {
if(isset($_POST['_method'])) if (isset($_POST['_method'])) {
return strtoupper($_POST['_method']); return strtoupper($_POST['_method']);
}
return strtoupper(isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'); return strtoupper(isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET');
} }
@ -644,11 +623,12 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function getScriptFile() public function getScriptFile()
{ {
if($this->_scriptFile!==null) if ($this->_scriptFile !== null) {
return $this->_scriptFile; return $this->_scriptFile;
else } else {
return $this->_scriptFile = realpath($_SERVER['SCRIPT_FILENAME']); return $this->_scriptFile = realpath($_SERVER['SCRIPT_FILENAME']);
} }
}
/** /**
* Returns information about the capabilities of user browser. * Returns information about the capabilities of user browser.
@ -684,8 +664,9 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function getPort() public function getPort()
{ {
if($this->_port===null) if ($this->_port === null) {
$this->_port = !$this->getIsSecureConnection() && isset($_SERVER['SERVER_PORT']) ? (int)$_SERVER['SERVER_PORT'] : 80; $this->_port = !$this->getIsSecureConnection() && isset($_SERVER['SERVER_PORT']) ? (int)$_SERVER['SERVER_PORT'] : 80;
}
return $this->_port; return $this->_port;
} }
@ -715,8 +696,9 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function getSecurePort() public function getSecurePort()
{ {
if($this->_securePort===null) if ($this->_securePort === null) {
$this->_securePort = $this->getIsSecureConnection() && isset($_SERVER['SERVER_PORT']) ? (int)$_SERVER['SERVER_PORT'] : 443; $this->_securePort = $this->getIsSecureConnection() && isset($_SERVER['SERVER_PORT']) ? (int)$_SERVER['SERVER_PORT'] : 443;
}
return $this->_securePort; return $this->_securePort;
} }
@ -742,11 +724,12 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function getCookies() public function getCookies()
{ {
if($this->_cookies!==null) if ($this->_cookies !== null) {
return $this->_cookies; return $this->_cookies;
else } else {
return $this->_cookies = new CCookieCollection($this); return $this->_cookies = new CCookieCollection($this);
} }
}
/** /**
* Redirects the browser to the specified URL. * Redirects the browser to the specified URL.
@ -758,12 +741,14 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function redirect($url, $terminate = true, $statusCode = 302) public function redirect($url, $terminate = true, $statusCode = 302)
{ {
if(strpos($url,'/')===0) if (strpos($url, '/') === 0) {
$url = $this->getHostInfo() . $url; $url = $this->getHostInfo() . $url;
}
header('Location: ' . $url, true, $statusCode); header('Location: ' . $url, true, $statusCode);
if($terminate) if ($terminate) {
\Yii::$application->end(); \Yii::$application->end();
} }
}
/** /**
* Returns the user preferred language. * Returns the user preferred language.
@ -773,17 +758,17 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function getPreferredLanguage() public function getPreferredLanguage()
{ {
if($this->_preferredLanguage===null) if ($this->_preferredLanguage === null) {
{ if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && ($n = preg_match_all('/([\w\-_]+)\s*(;\s*q\s*=\s*(\d*\.\d*))?/', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches)) > 0) {
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && ($n=preg_match_all('/([\w\-_]+)\s*(;\s*q\s*=\s*(\d*\.\d*))?/',$_SERVER['HTTP_ACCEPT_LANGUAGE'],$matches))>0)
{
$languages = array(); $languages = array();
for($i=0;$i<$n;++$i) for ($i = 0; $i < $n; ++$i) {
$languages[$matches[1][$i]] = empty($matches[3][$i]) ? 1.0 : floatval($matches[3][$i]); $languages[$matches[1][$i]] = empty($matches[3][$i]) ? 1.0 : floatval($matches[3][$i]);
}
arsort($languages); arsort($languages);
foreach($languages as $language=>$pref) foreach ($languages as $language => $pref) {
return $this->_preferredLanguage = CLocale::getCanonicalID($language); return $this->_preferredLanguage = CLocale::getCanonicalID($language);
} }
}
return $this->_preferredLanguage = false; return $this->_preferredLanguage = false;
} }
return $this->_preferredLanguage; return $this->_preferredLanguage;
@ -798,31 +783,31 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function sendFile($fileName, $content, $mimeType = null, $terminate = true) public function sendFile($fileName, $content, $mimeType = null, $terminate = true)
{ {
if($mimeType===null) if ($mimeType === null) {
{ if (($mimeType = CFileHelper::getMimeTypeByExtension($fileName)) === null) {
if(($mimeType=CFileHelper::getMimeTypeByExtension($fileName))===null)
$mimeType = 'text/plain'; $mimeType = 'text/plain';
} }
}
header('Pragma: public'); header('Pragma: public');
header('Expires: 0'); header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-type: $mimeType"); header("Content-type: $mimeType");
if(ob_get_length()===false) if (ob_get_length() === false) {
header('Content-Length: ' . (function_exists('mb_strlen') ? mb_strlen($content, '8bit') : strlen($content))); header('Content-Length: ' . (function_exists('mb_strlen') ? mb_strlen($content, '8bit') : strlen($content)));
}
header("Content-Disposition: attachment; filename=\"$fileName\""); header("Content-Disposition: attachment; filename=\"$fileName\"");
header('Content-Transfer-Encoding: binary'); header('Content-Transfer-Encoding: binary');
if($terminate) if ($terminate) {
{
// clean up the application first because the file downloading could take long time // clean up the application first because the file downloading could take long time
// which may cause timeout of some resources (such as DB connection) // which may cause timeout of some resources (such as DB connection)
\Yii::$application->end(0, false); \Yii::$application->end(0, false);
echo $content; echo $content;
exit(0); exit(0);
} } else {
else
echo $content; echo $content;
} }
}
/** /**
* Sends existing file to a browser as a download using x-sendfile. * Sends existing file to a browser as a download using x-sendfile.
@ -878,36 +863,41 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function xSendFile($filePath, $options = array()) public function xSendFile($filePath, $options = array())
{ {
if(!isset($options['forceDownload']) || $options['forceDownload']) if (!isset($options['forceDownload']) || $options['forceDownload']) {
$disposition = 'attachment'; $disposition = 'attachment';
else } else {
$disposition = 'inline'; $disposition = 'inline';
}
if(!isset($options['saveName'])) if (!isset($options['saveName'])) {
$options['saveName'] = basename($filePath); $options['saveName'] = basename($filePath);
}
if(!isset($options['mimeType'])) if (!isset($options['mimeType'])) {
{ if (($options['mimeType'] = CFileHelper::getMimeTypeByExtension($filePath)) === null) {
if(($options['mimeType']=CFileHelper::getMimeTypeByExtension($filePath))===null)
$options['mimeType'] = 'text/plain'; $options['mimeType'] = 'text/plain';
} }
}
if(!isset($options['xHeader'])) if (!isset($options['xHeader'])) {
$options['xHeader'] = 'X-Sendfile'; $options['xHeader'] = 'X-Sendfile';
}
if($options['mimeType'] !== null) if ($options['mimeType'] !== null) {
header('Content-type: ' . $options['mimeType']); header('Content-type: ' . $options['mimeType']);
}
header('Content-Disposition: ' . $disposition . '; filename="' . $options['saveName'] . '"'); header('Content-Disposition: ' . $disposition . '; filename="' . $options['saveName'] . '"');
if(isset($options['addHeaders'])) if (isset($options['addHeaders'])) {
{ foreach ($options['addHeaders'] as $header => $value) {
foreach($options['addHeaders'] as $header=>$value)
header($header . ': ' . $value); header($header . ': ' . $value);
} }
}
header(trim($options['xHeader']) . ': ' . $filePath); header(trim($options['xHeader']) . ': ' . $filePath);
if(!isset($options['terminate']) || $options['terminate']) if (!isset($options['terminate']) || $options['terminate']) {
\Yii::$application->end(); \Yii::$application->end();
} }
}
/** /**
* Returns the random token used to perform CSRF validation. * Returns the random token used to perform CSRF validation.
@ -918,11 +908,9 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function getCsrfToken() public function getCsrfToken()
{ {
if($this->_csrfToken===null) if ($this->_csrfToken === null) {
{
$cookie = $this->getCookies()->itemAt($this->csrfTokenName); $cookie = $this->getCookies()->itemAt($this->csrfTokenName);
if(!$cookie || ($this->_csrfToken=$cookie->value)==null) if (!$cookie || ($this->_csrfToken = $cookie->value) == null) {
{
$cookie = $this->createCsrfCookie(); $cookie = $this->createCsrfCookie();
$this->_csrfToken = $cookie->value; $this->_csrfToken = $cookie->value;
$this->getCookies()->add($cookie->name, $cookie); $this->getCookies()->add($cookie->name, $cookie);
@ -942,11 +930,11 @@ class CHttpRequest extends CApplicationComponent
protected function createCsrfCookie() protected function createCsrfCookie()
{ {
$cookie = new CHttpCookie($this->csrfTokenName, sha1(uniqid(mt_rand(), true))); $cookie = new CHttpCookie($this->csrfTokenName, sha1(uniqid(mt_rand(), true)));
if(is_array($this->csrfCookie)) if (is_array($this->csrfCookie)) {
{ foreach ($this->csrfCookie as $name => $value) {
foreach($this->csrfCookie as $name=>$value)
$cookie->$name = $value; $cookie->$name = $value;
} }
}
return $cookie; return $cookie;
} }
@ -960,23 +948,22 @@ class CHttpRequest extends CApplicationComponent
*/ */
public function validateCsrfToken($event) public function validateCsrfToken($event)
{ {
if($this->getIsPostRequest()) if ($this->getIsPostRequest()) {
{
// only validate POST requests // only validate POST requests
$cookies = $this->getCookies(); $cookies = $this->getCookies();
if($cookies->contains($this->csrfTokenName) && isset($_POST[$this->csrfTokenName])) if ($cookies->contains($this->csrfTokenName) && isset($_POST[$this->csrfTokenName])) {
{
$tokenFromCookie = $cookies->itemAt($this->csrfTokenName)->value; $tokenFromCookie = $cookies->itemAt($this->csrfTokenName)->value;
$tokenFromPost = $_POST[$this->csrfTokenName]; $tokenFromPost = $_POST[$this->csrfTokenName];
$valid = $tokenFromCookie === $tokenFromPost; $valid = $tokenFromCookie === $tokenFromPost;
} } else {
else
$valid = false; $valid = false;
if(!$valid) }
if (!$valid) {
throw new CHttpException(400, Yii::t('yii', 'The CSRF token could not be verified.')); throw new CHttpException(400, Yii::t('yii', 'The CSRF token could not be verified.'));
} }
} }
} }
}
/** /**
@ -1027,20 +1014,18 @@ class CCookieCollection extends CMap
protected function getCookies() protected function getCookies()
{ {
$cookies = array(); $cookies = array();
if($this->_request->enableCookieValidation) if ($this->_request->enableCookieValidation) {
{
$sm = \Yii::$application->getSecurityManager(); $sm = \Yii::$application->getSecurityManager();
foreach($_COOKIE as $name=>$value) foreach ($_COOKIE as $name => $value) {
{ if (is_string($value) && ($value = $sm->validateData($value)) !== false) {
if(is_string($value) && ($value=$sm->validateData($value))!==false)
$cookies[$name] = new CHttpCookie($name, @unserialize($value)); $cookies[$name] = new CHttpCookie($name, @unserialize($value));
} }
} }
else } else {
{ foreach ($_COOKIE as $name => $value) {
foreach($_COOKIE as $name=>$value)
$cookies[$name] = new CHttpCookie($name, $value); $cookies[$name] = new CHttpCookie($name, $value);
} }
}
return $cookies; return $cookies;
} }
@ -1054,16 +1039,16 @@ class CCookieCollection extends CMap
*/ */
public function add($name, $cookie) public function add($name, $cookie)
{ {
if($cookie instanceof CHttpCookie) if ($cookie instanceof CHttpCookie) {
{
$this->remove($name); $this->remove($name);
parent::add($name, $cookie); parent::add($name, $cookie);
if($this->_initialized) if ($this->_initialized) {
$this->addCookie($cookie); $this->addCookie($cookie);
} }
else } else {
throw new CException(Yii::t('yii', 'CHttpCookieCollection can only hold CHttpCookie objects.')); throw new CException(Yii::t('yii', 'CHttpCookieCollection can only hold CHttpCookie objects.'));
} }
}
/** /**
* Removes a cookie with the specified name. * Removes a cookie with the specified name.
@ -1074,11 +1059,11 @@ class CCookieCollection extends CMap
*/ */
public function remove($name) public function remove($name)
{ {
if(($cookie=parent::remove($name))!==null) if (($cookie = parent::remove($name)) !== null) {
{ if ($this->_initialized) {
if($this->_initialized)
$this->removeCookie($cookie); $this->removeCookie($cookie);
} }
}
return $cookie; return $cookie;
} }
@ -1089,12 +1074,10 @@ class CCookieCollection extends CMap
protected function addCookie($cookie) protected function addCookie($cookie)
{ {
$value = $cookie->value; $value = $cookie->value;
if($this->_request->enableCookieValidation) if ($this->_request->enableCookieValidation) {
$value = \Yii::$application->getSecurityManager()->hashData(serialize($value)); $value = \Yii::$application->getSecurityManager()->hashData(serialize($value));
if(version_compare(PHP_VERSION,'5.2.0','>=')) }
setcookie($cookie->name, $value, $cookie->expire, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httpOnly); setcookie($cookie->name, $value, $cookie->expire, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httpOnly);
else
setcookie($cookie->name,$value,$cookie->expire,$cookie->path,$cookie->domain,$cookie->secure);
} }
/** /**
@ -1103,9 +1086,6 @@ class CCookieCollection extends CMap
*/ */
protected function removeCookie($cookie) protected function removeCookie($cookie)
{ {
if(version_compare(PHP_VERSION,'5.2.0','>='))
setcookie($cookie->name, null, 0, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httpOnly); setcookie($cookie->name, null, 0, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httpOnly);
else
setcookie($cookie->name,null,0,$cookie->path,$cookie->domain,$cookie->secure);
} }
} }

144
framework/web/Response.php

@ -0,0 +1,144 @@
<?php
/**
* Response class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
use yii\util\FileHelper;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Response extends \yii\base\Response
{
/**
* Sends a file to user.
* @param string $fileName file name
* @param string $content content to be set.
* @param string $mimeType mime type of the content. If null, it will be guessed automatically based on the given file name.
* @param boolean $terminate whether to terminate the current application after calling this method
* @todo
*/
public function sendFile($fileName, $content, $mimeType = null, $terminate = true)
{
if ($mimeType === null && ($mimeType = FileHelper::getMimeType($fileName)) === null) {
$mimeType = 'application/octet-stream';
}
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-type: $mimeType");
if (ob_get_length() === false) {
header('Content-Length: ' . (function_exists('mb_strlen') ? mb_strlen($content, '8bit') : strlen($content)));
}
header("Content-Disposition: attachment; filename=\"$fileName\"");
header('Content-Transfer-Encoding: binary');
if ($terminate) {
// clean up the application first because the file downloading could take long time
// which may cause timeout of some resources (such as DB connection)
Yii::app()->end(0, false);
echo $content;
exit(0);
} else {
echo $content;
}
}
/**
* Sends existing file to a browser as a download using x-sendfile.
*
* X-Sendfile is a feature allowing a web application to redirect the request for a file to the webserver
* that in turn processes the request, this way eliminating the need to perform tasks like reading the file
* and sending it to the user. When dealing with a lot of files (or very big files) this can lead to a great
* increase in performance as the web application is allowed to terminate earlier while the webserver is
* handling the request.
*
* The request is sent to the server through a special non-standard HTTP-header.
* When the web server encounters the presence of such header it will discard all output and send the file
* specified by that header using web server internals including all optimizations like caching-headers.
*
* As this header directive is non-standard different directives exists for different web servers applications:
* <ul>
* <li>Apache: {@link http://tn123.org/mod_xsendfile X-Sendfile}</li>
* <li>Lighttpd v1.4: {@link http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file X-LIGHTTPD-send-file}</li>
* <li>Lighttpd v1.5: {@link http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file X-Sendfile}</li>
* <li>Nginx: {@link http://wiki.nginx.org/XSendfile X-Accel-Redirect}</li>
* <li>Cherokee: {@link http://www.cherokee-project.com/doc/other_goodies.html#x-sendfile X-Sendfile and X-Accel-Redirect}</li>
* </ul>
* So for this method to work the X-SENDFILE option/module should be enabled by the web server and
* a proper xHeader should be sent.
*
* <b>Note:</b>
* This option allows to download files that are not under web folders, and even files that are otherwise protected (deny from all) like .htaccess
*
* <b>Side effects</b>:
* If this option is disabled by the web server, when this method is called a download configuration dialog
* will open but the downloaded file will have 0 bytes.
*
* <b>Example</b>:
* <pre>
* <?php
* Yii::app()->request->xSendFile('/home/user/Pictures/picture1.jpg',array(
* 'saveName'=>'image1.jpg',
* 'mimeType'=>'image/jpeg',
* 'terminate'=>false,
* ));
* ?>
* </pre>
* @param string $filePath file name with full path
* @param array $options additional options:
* <ul>
* <li>saveName: file name shown to the user, if not set real file name will be used</li>
* <li>mimeType: mime type of the file, if not set it will be guessed automatically based on the file name, if set to null no content-type header will be sent.</li>
* <li>xHeader: appropriate x-sendfile header, defaults to "X-Sendfile"</li>
* <li>terminate: whether to terminate the current application after calling this method, defaults to true</li>
* <li>forceDownload: specifies whether the file will be downloaded or shown inline, defaults to true. (Since version 1.1.9.)</li>
* <li>addHeaders: an array of additional http headers in header-value pairs (available since version 1.1.10)</li>
* </ul>
* @todo
*/
public function xSendFile($filePath, $options = array())
{
if (!isset($options['forceDownload']) || $options['forceDownload']) {
$disposition = 'attachment';
} else {
$disposition = 'inline';
}
if (!isset($options['saveName'])) {
$options['saveName'] = basename($filePath);
}
if (!isset($options['mimeType'])) {
if (($options['mimeType'] = CFileHelper::getMimeTypeByExtension($filePath)) === null) {
$options['mimeType'] = 'text/plain';
}
}
if (!isset($options['xHeader'])) {
$options['xHeader'] = 'X-Sendfile';
}
if ($options['mimeType'] !== null) {
header('Content-type: ' . $options['mimeType']);
}
header('Content-Disposition: ' . $disposition . '; filename="' . $options['saveName'] . '"');
if (isset($options['addHeaders'])) {
foreach ($options['addHeaders'] as $header => $value) {
header($header . ': ' . $value);
}
}
header(trim($options['xHeader']) . ': ' . $filePath);
if (!isset($options['terminate']) || $options['terminate']) {
Yii::app()->end();
}
}
}

137
framework/web/Sort.php

@ -216,10 +216,10 @@ class CSort extends CComponent
public function applyOrder($criteria) public function applyOrder($criteria)
{ {
$order = $this->getOrderBy(); $order = $this->getOrderBy();
if(!empty($order)) if (!empty($order)) {
{ if (!empty($criteria->order)) {
if(!empty($criteria->order))
$criteria->order .= ', '; $criteria->order .= ', ';
}
$criteria->order .= $order; $criteria->order .= $order;
} }
} }
@ -232,36 +232,35 @@ class CSort extends CComponent
public function getOrderBy() public function getOrderBy()
{ {
$directions = $this->getDirections(); $directions = $this->getDirections();
if(empty($directions)) if (empty($directions)) {
return is_string($this->defaultOrder) ? $this->defaultOrder : ''; return is_string($this->defaultOrder) ? $this->defaultOrder : '';
else } else {
{ if ($this->modelClass !== null) {
if($this->modelClass!==null)
$schema = CActiveRecord::model($this->modelClass)->getDbConnection()->getSchema(); $schema = CActiveRecord::model($this->modelClass)->getDbConnection()->getSchema();
}
$orders = array(); $orders = array();
foreach($directions as $attribute=>$descending) foreach ($directions as $attribute => $descending) {
{
$definition = $this->resolveAttribute($attribute); $definition = $this->resolveAttribute($attribute);
if(is_array($definition)) if (is_array($definition)) {
{ if ($descending) {
if($descending)
$orders[] = isset($definition['desc']) ? $definition['desc'] : $attribute . ' DESC'; $orders[] = isset($definition['desc']) ? $definition['desc'] : $attribute . ' DESC';
else } else {
$orders[] = isset($definition['asc']) ? $definition['asc'] : $attribute; $orders[] = isset($definition['asc']) ? $definition['asc'] : $attribute;
} }
else if($definition!==false) } else {
{ if ($definition !== false) {
$attribute = $definition; $attribute = $definition;
if(isset($schema)) if (isset($schema)) {
{ if (($pos = strpos($attribute, '.')) !== false) {
if(($pos=strpos($attribute,'.'))!==false)
$attribute = $schema->quoteTableName(substr($attribute, 0, $pos)) . '.' . $schema->quoteColumnName(substr($attribute, $pos + 1)); $attribute = $schema->quoteTableName(substr($attribute, 0, $pos)) . '.' . $schema->quoteColumnName(substr($attribute, $pos + 1));
else } else {
$attribute = CActiveRecord::model($this->modelClass)->getTableAlias(true) . '.' . $schema->quoteColumnName($attribute); $attribute = CActiveRecord::model($this->modelClass)->getTableAlias(true) . '.' . $schema->quoteColumnName($attribute);
} }
}
$orders[] = $descending ? $attribute . ' DESC' : $attribute; $orders[] = $descending ? $attribute . ' DESC' : $attribute;
} }
} }
}
return implode(', ', $orders); return implode(', ', $orders);
} }
} }
@ -278,30 +277,35 @@ class CSort extends CComponent
*/ */
public function link($attribute, $label = null, $htmlOptions = array()) public function link($attribute, $label = null, $htmlOptions = array())
{ {
if($label===null) if ($label === null) {
$label = $this->resolveLabel($attribute); $label = $this->resolveLabel($attribute);
if(($definition=$this->resolveAttribute($attribute))===false) }
if (($definition = $this->resolveAttribute($attribute)) === false) {
return $label; return $label;
}
$directions = $this->getDirections(); $directions = $this->getDirections();
if(isset($directions[$attribute])) if (isset($directions[$attribute])) {
{
$class = $directions[$attribute] ? 'desc' : 'asc'; $class = $directions[$attribute] ? 'desc' : 'asc';
if(isset($htmlOptions['class'])) if (isset($htmlOptions['class'])) {
$htmlOptions['class'] .= ' ' . $class; $htmlOptions['class'] .= ' ' . $class;
else } else {
$htmlOptions['class'] = $class; $htmlOptions['class'] = $class;
}
$descending = !$directions[$attribute]; $descending = !$directions[$attribute];
unset($directions[$attribute]); unset($directions[$attribute]);
} } else {
else if(is_array($definition) && isset($definition['default'])) if (is_array($definition) && isset($definition['default'])) {
$descending = $definition['default'] === 'desc'; $descending = $definition['default'] === 'desc';
else } else {
$descending = false; $descending = false;
}
}
if($this->multiSort) if ($this->multiSort) {
$directions = array_merge(array($attribute => $descending), $directions); $directions = array_merge(array($attribute => $descending), $directions);
else } else {
$directions = array($attribute => $descending); $directions = array($attribute => $descending);
}
$url = $this->createUrl(\Yii::$application->getController(), $directions); $url = $this->createUrl(\Yii::$application->getController(), $directions);
@ -319,18 +323,21 @@ class CSort extends CComponent
public function resolveLabel($attribute) public function resolveLabel($attribute)
{ {
$definition = $this->resolveAttribute($attribute); $definition = $this->resolveAttribute($attribute);
if(is_array($definition)) if (is_array($definition)) {
{ if (isset($definition['label'])) {
if(isset($definition['label']))
return $definition['label']; return $definition['label'];
} }
else if(is_string($definition)) } else {
if (is_string($definition)) {
$attribute = $definition; $attribute = $definition;
if($this->modelClass!==null) }
}
if ($this->modelClass !== null) {
return CActiveRecord::model($this->modelClass)->getAttributeLabel($attribute); return CActiveRecord::model($this->modelClass)->getAttributeLabel($attribute);
else } else {
return $attribute; return $attribute;
} }
}
/** /**
* Returns the currently requested sort information. * Returns the currently requested sort information.
@ -340,34 +347,32 @@ class CSort extends CComponent
*/ */
public function getDirections() public function getDirections()
{ {
if($this->_directions===null) if ($this->_directions === null) {
{
$this->_directions = array(); $this->_directions = array();
if(isset($_GET[$this->sortVar]) && is_string($_GET[$this->sortVar])) if (isset($_GET[$this->sortVar]) && is_string($_GET[$this->sortVar])) {
{
$attributes = explode($this->separators[0], $_GET[$this->sortVar]); $attributes = explode($this->separators[0], $_GET[$this->sortVar]);
foreach($attributes as $attribute) foreach ($attributes as $attribute) {
{ if (($pos = strrpos($attribute, $this->separators[1])) !== false) {
if(($pos=strrpos($attribute,$this->separators[1]))!==false)
{
$descending = substr($attribute, $pos + 1) === $this->descTag; $descending = substr($attribute, $pos + 1) === $this->descTag;
if($descending) if ($descending) {
$attribute = substr($attribute, 0, $pos); $attribute = substr($attribute, 0, $pos);
} }
else } else {
$descending = false; $descending = false;
}
if(($this->resolveAttribute($attribute))!==false) if (($this->resolveAttribute($attribute)) !== false) {
{
$this->_directions[$attribute] = $descending; $this->_directions[$attribute] = $descending;
if(!$this->multiSort) if (!$this->multiSort) {
return $this->_directions; return $this->_directions;
} }
} }
} }
if($this->_directions===array() && is_array($this->defaultOrder)) }
if ($this->_directions === array() && is_array($this->defaultOrder)) {
$this->_directions = $this->defaultOrder; $this->_directions = $this->defaultOrder;
} }
}
return $this->_directions; return $this->_directions;
} }
@ -395,8 +400,9 @@ class CSort extends CComponent
public function createUrl($controller, $directions) public function createUrl($controller, $directions)
{ {
$sorts = array(); $sorts = array();
foreach($directions as $attribute=>$descending) foreach ($directions as $attribute => $descending) {
$sorts[] = $descending ? $attribute . $this->separators[1] . $this->descTag : $attribute; $sorts[] = $descending ? $attribute . $this->separators[1] . $this->descTag : $attribute;
}
$params = $this->params === null ? $_GET : $this->params; $params = $this->params === null ? $_GET : $this->params;
$params[$this->sortVar] = implode($this->separators[0], $sorts); $params[$this->sortVar] = implode($this->separators[0], $sorts);
return $controller->createUrl($this->route, $params); return $controller->createUrl($this->route, $params);
@ -419,27 +425,32 @@ class CSort extends CComponent
*/ */
public function resolveAttribute($attribute) public function resolveAttribute($attribute)
{ {
if($this->attributes!==array()) if ($this->attributes !== array()) {
$attributes = $this->attributes; $attributes = $this->attributes;
else if($this->modelClass!==null) } else {
if ($this->modelClass !== null) {
$attributes = CActiveRecord::model($this->modelClass)->attributeNames(); $attributes = CActiveRecord::model($this->modelClass)->attributeNames();
else } else {
return false; return false;
foreach($attributes as $name=>$definition) }
{ }
if(is_string($name)) foreach ($attributes as $name => $definition) {
{ if (is_string($name)) {
if($name===$attribute) if ($name === $attribute) {
return $definition; return $definition;
} }
else if($definition==='*') } else {
{ if ($definition === '*') {
if($this->modelClass!==null && CActiveRecord::model($this->modelClass)->hasAttribute($attribute)) if ($this->modelClass !== null && CActiveRecord::model($this->modelClass)->hasAttribute($attribute)) {
return $attribute; return $attribute;
} }
else if($definition===$attribute) } else {
if ($definition === $attribute) {
return $attribute; return $attribute;
} }
}
}
}
return false; return false;
} }

Loading…
Cancel
Save