<?php
/**
* @package OpenCart
* @author Daniel Kerr
* @copyright Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/)
* @license https://opensource.org/licenses/GPL-3.0
* @link https://www.opencart.com
*/
/**
* 响应类
*/
class Response {
private $headers = array();
private $level = 0;
private $output;
/**
* Constructor
* 添加消息头
* @param string $header
*
*/
public function addHeader($header) {
$this->headers[] = $header;
}
/**
*
* 跳转
* @param string $url
* @param int $status
*
*/
public function redirect($url, $status = 302) {
header('Location: ' . str_replace(array('&', "\n", "\r"), array('&', '', ''), $url), true, $status);
exit();
}
/**
*
*
* @param int $level
*/
public function setCompression($level) {
$this->level = $level;
}
/**
*
* 获取输出
* @return array
*/
public function getOutput() {
return $this->output;
}
/**
*
* 设置输出
* @param string $output
*/
public function setOutput($output) {
$this->output = $output;
}
/**
*
* 压缩
* @param string $data
* @param int $level
*
* @return string
*/
private function compress($data, $level = 0) {
// 可接受编码gzip
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false)) {
$encoding = 'gzip';
}
// 可接受编码x-gzip
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== false)) {
$encoding = 'x-gzip';
}
if (!isset($encoding) || ($level < -1 || $level > 9)) {
return $data;
}
// 如果扩展没有加载
if (!extension_loaded('zlib') || ini_get('zlib.output_compression')) {
return $data;
}
//如果http标头已被发送
if (headers_sent()) {
return $data;
}
// 连接状态
if (connection_status()) {
return $data;
}
$this->addHeader('Content-Encoding: ' . $encoding);
// 压缩数据
return gzencode($data, (int)$level);
}
/**
* 输出
*/
public function output() {
if ($this->output) {
$output = $this->level ? $this->compress($this->output, $this->level) : $this->output;
//如果http标头没被发送
if (!headers_sent()) {
foreach ($this->headers as $header) {
header($header, true);
}
}
echo $output;
}
}
}
【opencart3源码分析】响应类response.php
猜你喜欢
转载自blog.csdn.net/qq2942713658/article/details/81416616
今日推荐
周排行