response.php (2256B)
1 <?php 2 /** 3 * @package OpenCart 4 * @author Daniel Kerr 5 * @copyright Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/) 6 * @license https://opensource.org/licenses/GPL-3.0 7 * @link https://www.opencart.com 8 */ 9 10 /** 11 * Response class 12 */ 13 class Response { 14 private $headers = array(); 15 private $level = 0; 16 private $output; 17 18 /** 19 * Constructor 20 * 21 * @param string $header 22 * 23 */ 24 public function addHeader($header) { 25 $this->headers[] = $header; 26 } 27 28 /** 29 * 30 * 31 * @param string $url 32 * @param int $status 33 * 34 */ 35 public function redirect($url, $status = 302) { 36 header('Location: ' . str_replace(array('&', "\n", "\r"), array('&', '', ''), $url), true, $status); 37 exit(); 38 } 39 40 /** 41 * 42 * 43 * @param int $level 44 */ 45 public function setCompression($level) { 46 $this->level = $level; 47 } 48 49 /** 50 * 51 * 52 * @return array 53 */ 54 public function getOutput() { 55 return $this->output; 56 } 57 58 /** 59 * 60 * 61 * @param string $output 62 */ 63 public function setOutput($output) { 64 $this->output = $output; 65 } 66 67 /** 68 * 69 * 70 * @param string $data 71 * @param int $level 72 * 73 * @return string 74 */ 75 private function compress($data, $level = 0) { 76 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false)) { 77 $encoding = 'gzip'; 78 } 79 80 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== false)) { 81 $encoding = 'x-gzip'; 82 } 83 84 if (!isset($encoding) || ($level < -1 || $level > 9)) { 85 return $data; 86 } 87 88 if (!extension_loaded('zlib') || ini_get('zlib.output_compression')) { 89 return $data; 90 } 91 92 if (headers_sent()) { 93 return $data; 94 } 95 96 if (connection_status()) { 97 return $data; 98 } 99 100 $this->addHeader('Content-Encoding: ' . $encoding); 101 102 return gzencode($data, (int)$level); 103 } 104 105 /** 106 * 107 */ 108 public function output() { 109 if ($this->output) { 110 $output = $this->level ? $this->compress($this->output, $this->level) : $this->output; 111 112 if (!headers_sent()) { 113 foreach ($this->headers as $header) { 114 header($header, true); 115 } 116 } 117 118 echo $output; 119 } 120 } 121 }