|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029 |
- <?php
-
-
- namespace yii\web;
-
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\base\InvalidParamException;
- use yii\helpers\Inflector;
- use yii\helpers\Url;
- use yii\helpers\FileHelper;
- use yii\helpers\StringHelper;
-
-
- class Response extends \yii\base\Response
- {
-
-
- const EVENT_BEFORE_SEND = 'beforeSend';
-
-
- const EVENT_AFTER_SEND = 'afterSend';
-
-
- const EVENT_AFTER_PREPARE = 'afterPrepare';
- const FORMAT_RAW = 'raw';
- const FORMAT_HTML = 'html';
- const FORMAT_JSON = 'json';
- const FORMAT_JSONP = 'jsonp';
- const FORMAT_XML = 'xml';
-
-
-
- public $format = self::FORMAT_HTML;
-
-
- public $acceptMimeType;
-
-
- public $acceptParams = [];
-
-
- public $formatters = [];
-
-
- public $data;
-
-
- public $content;
-
-
- public $stream;
-
-
- public $charset;
-
-
- public $statusText = 'OK';
-
-
- public $version;
-
-
- public $isSent = false;
-
-
- public static $httpStatuses = [
- 100 => 'Continue',
- 101 => 'Switching Protocols',
- 102 => 'Processing',
- 118 => 'Connection timed out',
- 200 => 'OK',
- 201 => 'Created',
- 202 => 'Accepted',
- 203 => 'Non-Authoritative',
- 204 => 'No Content',
- 205 => 'Reset Content',
- 206 => 'Partial Content',
- 207 => 'Multi-Status',
- 208 => 'Already Reported',
- 210 => 'Content Different',
- 226 => 'IM Used',
- 300 => 'Multiple Choices',
- 301 => 'Moved Permanently',
- 302 => 'Found',
- 303 => 'See Other',
- 304 => 'Not Modified',
- 305 => 'Use Proxy',
- 306 => 'Reserved',
- 307 => 'Temporary Redirect',
- 308 => 'Permanent Redirect',
- 310 => 'Too many Redirect',
- 400 => 'Bad Request',
- 401 => 'Unauthorized',
- 402 => 'Payment Required',
- 403 => 'Forbidden',
- 404 => 'Not Found',
- 405 => 'Method Not Allowed',
- 406 => 'Not Acceptable',
- 407 => 'Proxy Authentication Required',
- 408 => 'Request Time-out',
- 409 => 'Conflict',
- 410 => 'Gone',
- 411 => 'Length Required',
- 412 => 'Precondition Failed',
- 413 => 'Request Entity Too Large',
- 414 => 'Request-URI Too Long',
- 415 => 'Unsupported Media Type',
- 416 => 'Requested range unsatisfiable',
- 417 => 'Expectation failed',
- 418 => 'I\'m a teapot',
- 421 => 'Misdirected Request',
- 422 => 'Unprocessable entity',
- 423 => 'Locked',
- 424 => 'Method failure',
- 425 => 'Unordered Collection',
- 426 => 'Upgrade Required',
- 428 => 'Precondition Required',
- 429 => 'Too Many Requests',
- 431 => 'Request Header Fields Too Large',
- 449 => 'Retry With',
- 450 => 'Blocked by Windows Parental Controls',
- 500 => 'Internal Server Error',
- 501 => 'Not Implemented',
- 502 => 'Bad Gateway or Proxy Error',
- 503 => 'Service Unavailable',
- 504 => 'Gateway Time-out',
- 505 => 'HTTP Version not supported',
- 507 => 'Insufficient storage',
- 508 => 'Loop Detected',
- 509 => 'Bandwidth Limit Exceeded',
- 510 => 'Not Extended',
- 511 => 'Network Authentication Required',
- ];
-
-
-
- private $_statusCode = 200;
-
-
- private $_headers;
-
-
-
-
- public function init()
- {
- if ($this->version === null) {
- if (isset($_SERVER['SERVER_PROTOCOL']) && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.0') {
- $this->version = '1.0';
- } else {
- $this->version = '1.1';
- }
- }
- if ($this->charset === null) {
- $this->charset = Yii::$app->charset;
- }
- $this->formatters = array_merge($this->defaultFormatters(), $this->formatters);
- }
-
-
-
- public function getStatusCode()
- {
- return $this->_statusCode;
- }
-
-
-
- public function setStatusCode($value, $text = null)
- {
- if ($value === null) {
- $value = 200;
- }
- $this->_statusCode = (int) $value;
- if ($this->getIsInvalid()) {
- throw new InvalidParamException("The HTTP status code is invalid: $value");
- }
- if ($text === null) {
- $this->statusText = isset(static::$httpStatuses[$this->_statusCode]) ? static::$httpStatuses[$this->_statusCode] : '';
- } else {
- $this->statusText = $text;
- }
- }
-
-
-
- public function getHeaders()
- {
- if ($this->_headers === null) {
- $this->_headers = new HeaderCollection;
- }
- return $this->_headers;
- }
-
-
-
- public function send()
- {
- if ($this->isSent) {
- return;
- }
- $this->trigger(self::EVENT_BEFORE_SEND);
- $this->prepare();
- $this->trigger(self::EVENT_AFTER_PREPARE);
- $this->sendHeaders();
- $this->sendContent();
- $this->trigger(self::EVENT_AFTER_SEND);
- $this->isSent = true;
- }
-
-
-
- public function clear()
- {
- $this->_headers = null;
- $this->_cookies = null;
- $this->_statusCode = 200;
- $this->statusText = 'OK';
- $this->data = null;
- $this->stream = null;
- $this->content = null;
- $this->isSent = false;
- }
-
-
-
- protected function sendHeaders()
- {
- if (headers_sent()) {
- return;
- }
- if ($this->_headers) {
- $headers = $this->getHeaders();
- foreach ($headers as $name => $values) {
- $name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name)));
-
- $replace = true;
- foreach ($values as $value) {
- header("$name: $value", $replace);
- $replace = false;
- }
- }
- }
- $statusCode = $this->getStatusCode();
- header("HTTP/{$this->version} {$statusCode} {$this->statusText}");
- $this->sendCookies();
- }
-
-
-
- protected function sendCookies()
- {
- if ($this->_cookies === null) {
- return;
- }
- $request = Yii::$app->getRequest();
- if ($request->enableCookieValidation) {
- if ($request->cookieValidationKey == '') {
- throw new InvalidConfigException(get_class($request) . '::cookieValidationKey must be configured with a secret key.');
- }
- $validationKey = $request->cookieValidationKey;
- }
- foreach ($this->getCookies() as $cookie) {
- $value = $cookie->value;
- if ($cookie->expire != 1 && isset($validationKey)) {
- $value = Yii::$app->getSecurity()->hashData(serialize([$cookie->name, $value]), $validationKey);
- }
- setcookie($cookie->name, $value, $cookie->expire, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httpOnly);
- }
- }
-
-
-
- protected function sendContent()
- {
- if ($this->stream === null) {
- echo $this->content;
-
- return;
- }
-
- set_time_limit(0);
- $chunkSize = 8 * 1024 * 1024;
-
- if (is_array($this->stream)) {
- list ($handle, $begin, $end) = $this->stream;
- fseek($handle, $begin);
- while (!feof($handle) && ($pos = ftell($handle)) <= $end) {
- if ($pos + $chunkSize > $end) {
- $chunkSize = $end - $pos + 1;
- }
- echo fread($handle, $chunkSize);
- flush();
- }
- fclose($handle);
- } else {
- while (!feof($this->stream)) {
- echo fread($this->stream, $chunkSize);
- flush();
- }
- fclose($this->stream);
- }
- }
-
-
-
- public function sendFile($filePath, $attachmentName = null, $options = [])
- {
- if (!isset($options['mimeType'])) {
- $options['mimeType'] = FileHelper::getMimeTypeByExtension($filePath);
- }
- if ($attachmentName === null) {
- $attachmentName = basename($filePath);
- }
- $handle = fopen($filePath, 'rb');
- $this->sendStreamAsFile($handle, $attachmentName, $options);
-
- return $this;
- }
-
-
-
- public function sendContentAsFile($content, $attachmentName, $options = [])
- {
- $headers = $this->getHeaders();
-
- $contentLength = StringHelper::byteLength($content);
- $range = $this->getHttpRange($contentLength);
-
- if ($range === false) {
- $headers->set('Content-Range', "bytes */$contentLength");
- throw new HttpException(416, 'Requested range not satisfiable');
- }
-
- list($begin, $end) = $range;
- if ($begin != 0 || $end != $contentLength - 1) {
- $this->setStatusCode(206);
- $headers->set('Content-Range', "bytes $begin-$end/$contentLength");
- $this->content = StringHelper::byteSubstr($content, $begin, $end - $begin + 1);
- } else {
- $this->setStatusCode(200);
- $this->content = $content;
- }
-
- $mimeType = isset($options['mimeType']) ? $options['mimeType'] : 'application/octet-stream';
- $this->setDownloadHeaders($attachmentName, $mimeType, !empty($options['inline']), $end - $begin + 1);
-
- $this->format = self::FORMAT_RAW;
-
- return $this;
- }
-
-
-
- public function sendStreamAsFile($handle, $attachmentName, $options = [])
- {
- $headers = $this->getHeaders();
- if (isset($options['fileSize'])) {
- $fileSize = $options['fileSize'];
- } else {
- fseek($handle, 0, SEEK_END);
- $fileSize = ftell($handle);
- }
-
- $range = $this->getHttpRange($fileSize);
- if ($range === false) {
- $headers->set('Content-Range', "bytes */$fileSize");
- throw new HttpException(416, 'Requested range not satisfiable');
- }
-
- list($begin, $end) = $range;
- if ($begin != 0 || $end != $fileSize - 1) {
- $this->setStatusCode(206);
- $headers->set('Content-Range', "bytes $begin-$end/$fileSize");
- } else {
- $this->setStatusCode(200);
- }
-
- $mimeType = isset($options['mimeType']) ? $options['mimeType'] : 'application/octet-stream';
- $this->setDownloadHeaders($attachmentName, $mimeType, !empty($options['inline']), $end - $begin + 1);
-
- $this->format = self::FORMAT_RAW;
- $this->stream = [$handle, $begin, $end];
-
- return $this;
- }
-
-
-
- public function setDownloadHeaders($attachmentName, $mimeType = null, $inline = false, $contentLength = null)
- {
- $headers = $this->getHeaders();
-
- $disposition = $inline ? 'inline' : 'attachment';
- $headers->setDefault('Pragma', 'public')
- ->setDefault('Accept-Ranges', 'bytes')
- ->setDefault('Expires', '0')
- ->setDefault('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
- ->setDefault('Content-Disposition', $this->getDispositionHeaderValue($disposition, $attachmentName));
-
- if ($mimeType !== null) {
- $headers->setDefault('Content-Type', $mimeType);
- }
-
- if ($contentLength !== null) {
- $headers->setDefault('Content-Length', $contentLength);
- }
-
- return $this;
- }
-
-
-
- protected function getHttpRange($fileSize)
- {
- if (!isset($_SERVER['HTTP_RANGE']) || $_SERVER['HTTP_RANGE'] === '-') {
- return [0, $fileSize - 1];
- }
- if (!preg_match('/^bytes=(\d*)-(\d*)$/', $_SERVER['HTTP_RANGE'], $matches)) {
- return false;
- }
- if ($matches[1] === '') {
- $start = $fileSize - $matches[2];
- $end = $fileSize - 1;
- } elseif ($matches[2] !== '') {
- $start = $matches[1];
- $end = $matches[2];
- if ($end >= $fileSize) {
- $end = $fileSize - 1;
- }
- } else {
- $start = $matches[1];
- $end = $fileSize - 1;
- }
- if ($start < 0 || $start > $end) {
- return false;
- } else {
- return [$start, $end];
- }
- }
-
-
-
- public function xSendFile($filePath, $attachmentName = null, $options = [])
- {
- if ($attachmentName === null) {
- $attachmentName = basename($filePath);
- }
- if (isset($options['mimeType'])) {
- $mimeType = $options['mimeType'];
- } elseif (($mimeType = FileHelper::getMimeTypeByExtension($filePath)) === null) {
- $mimeType = 'application/octet-stream';
- }
- if (isset($options['xHeader'])) {
- $xHeader = $options['xHeader'];
- } else {
- $xHeader = 'X-Sendfile';
- }
-
- $disposition = empty($options['inline']) ? 'attachment' : 'inline';
- $this->getHeaders()
- ->setDefault($xHeader, $filePath)
- ->setDefault('Content-Type', $mimeType)
- ->setDefault('Content-Disposition', $this->getDispositionHeaderValue($disposition, $attachmentName));
-
- $this->format = self::FORMAT_RAW;
-
- return $this;
- }
-
-
-
- protected function getDispositionHeaderValue($disposition, $attachmentName)
- {
- $fallbackName = str_replace('"', '\\"', str_replace(['%', '/', '\\'], '_', Inflector::transliterate($attachmentName, Inflector::TRANSLITERATE_LOOSE)));
- $utfName = rawurlencode(str_replace(['%', '/', '\\'], '', $attachmentName));
-
- $dispositionHeader = "{$disposition}; filename=\"{$fallbackName}\"";
- if ($utfName !== $fallbackName) {
- $dispositionHeader .= "; filename*=utf-8''{$utfName}";
- }
- return $dispositionHeader;
- }
-
-
-
- public function redirect($url, $statusCode = 302, $checkAjax = true)
- {
- if (is_array($url) && isset($url[0])) {
-
- $url[0] = '/' . ltrim($url[0], '/');
- }
- $url = Url::to($url);
- if (strpos($url, '/') === 0 && strpos($url, '//') !== 0) {
- $url = Yii::$app->getRequest()->getHostInfo() . $url;
- }
-
- if ($checkAjax) {
- if (Yii::$app->getRequest()->getIsAjax()) {
- if (Yii::$app->getRequest()->getHeaders()->get('X-Ie-Redirect-Compatibility') !== null && $statusCode === 302) {
-
- $statusCode = 200;
- }
- if (Yii::$app->getRequest()->getIsPjax()) {
- $this->getHeaders()->set('X-Pjax-Url', $url);
- } else {
- $this->getHeaders()->set('X-Redirect', $url);
- }
- } else {
- $this->getHeaders()->set('Location', $url);
- }
- } else {
- $this->getHeaders()->set('Location', $url);
- }
-
- $this->setStatusCode($statusCode);
-
- return $this;
- }
-
-
-
- public function refresh($anchor = '')
- {
- return $this->redirect(Yii::$app->getRequest()->getUrl() . $anchor);
- }
-
- private $_cookies;
-
-
-
- public function getCookies()
- {
- if ($this->_cookies === null) {
- $this->_cookies = new CookieCollection;
- }
- return $this->_cookies;
- }
-
-
-
- public function getIsInvalid()
- {
- return $this->getStatusCode() < 100 || $this->getStatusCode() >= 600;
- }
-
-
-
- public function getIsInformational()
- {
- return $this->getStatusCode() >= 100 && $this->getStatusCode() < 200;
- }
-
-
-
- public function getIsSuccessful()
- {
- return $this->getStatusCode() >= 200 && $this->getStatusCode() < 300;
- }
-
-
-
- public function getIsRedirection()
- {
- return $this->getStatusCode() >= 300 && $this->getStatusCode() < 400;
- }
-
-
-
- public function getIsClientError()
- {
- return $this->getStatusCode() >= 400 && $this->getStatusCode() < 500;
- }
-
-
-
- public function getIsServerError()
- {
- return $this->getStatusCode() >= 500 && $this->getStatusCode() < 600;
- }
-
-
-
- public function getIsOk()
- {
- return $this->getStatusCode() == 200;
- }
-
-
-
- public function getIsForbidden()
- {
- return $this->getStatusCode() == 403;
- }
-
-
-
- public function getIsNotFound()
- {
- return $this->getStatusCode() == 404;
- }
-
-
-
- public function getIsEmpty()
- {
- return in_array($this->getStatusCode(), [201, 204, 304]);
- }
-
-
-
- protected function defaultFormatters()
- {
- return [
- self::FORMAT_HTML => 'yii\web\HtmlResponseFormatter',
- self::FORMAT_XML => 'yii\web\XmlResponseFormatter',
- self::FORMAT_JSON => 'yii\web\JsonResponseFormatter',
- self::FORMAT_JSONP => [
- 'class' => 'yii\web\JsonResponseFormatter',
- 'useJsonp' => true,
- ],
- ];
- }
-
-
-
- protected function prepare()
- {
- if ($this->stream !== null) {
- return;
- }
-
- if (isset($this->formatters[$this->format])) {
- $formatter = $this->formatters[$this->format];
- if (!is_object($formatter)) {
- $this->formatters[$this->format] = $formatter = Yii::createObject($formatter);
- }
- if ($formatter instanceof ResponseFormatterInterface) {
- $formatter->format($this);
- } else {
- throw new InvalidConfigException("The '{$this->format}' response formatter is invalid. It must implement the ResponseFormatterInterface.");
- }
- } elseif ($this->format === self::FORMAT_RAW) {
- if ($this->data !== null) {
- $this->content = $this->data;
- }
- } else {
- throw new InvalidConfigException("Unsupported response format: {$this->format}");
- }
-
- if (is_array($this->content)) {
- throw new InvalidParamException('Response content must not be an array.');
- } elseif (is_object($this->content)) {
- if (method_exists($this->content, '__toString')) {
- $this->content = $this->content->__toString();
- } else {
- throw new InvalidParamException('Response content must be a string or an object implementing __toString().');
- }
- }
- }
- }
|