1: <?php
2: /**
3: * @package OpenCart
4: *
5: * @author Daniel Kerr
6: * @copyright Copyright (c) 2005 - 2022, OpenCart, Ltd. (https://www.opencart.com/)
7: * @license https://opensource.org/licenses/GPL-3.0
8: *
9: * @see https://www.opencart.com
10: */
11: namespace Opencart\System\Library;
12: /**
13: * Class Response
14: *
15: * Stores the response so the correct headers can go out before the response output is shown.
16: */
17: class Response {
18: /**
19: * @var array<int, string>
20: */
21: private array $headers = [];
22: /**
23: * @var int
24: */
25: private int $level = 0;
26: /**
27: * @var string
28: */
29: private string $output = '';
30:
31: /**
32: * Constructor
33: *
34: * @param string $header
35: */
36: public function addHeader(string $header): void {
37: $this->headers[] = $header;
38: }
39:
40: /**
41: * getHeaders
42: *
43: * @return array<int, string>
44: */
45: public function getHeaders(): array {
46: return $this->headers;
47: }
48:
49: /**
50: * Redirect
51: *
52: * @param string $url
53: * @param int $status
54: *
55: * @return void
56: */
57: public function redirect(string $url, int $status = 302): void {
58: header('Location: ' . str_replace(['&amp;', "\n", "\r"], ['&', '', ''], $url), true, $status);
59: exit();
60: }
61:
62: /**
63: * setCompression
64: *
65: * @param int $level
66: *
67: * @return void
68: */
69: public function setCompression(int $level): void {
70: $this->level = $level;
71: }
72:
73: /**
74: * setOutput
75: *
76: * @param string $output
77: *
78: * @return void
79: */
80: public function setOutput(string $output): void {
81: $this->output = $output;
82: }
83:
84: /**
85: * getOutput
86: *
87: * @return string
88: */
89: public function getOutput(): string {
90: return $this->output;
91: }
92:
93: /**
94: * Compress
95: *
96: * @param string $data
97: * @param int $level
98: *
99: * @return string
100: */
101: private function compress(string $data, int $level = 0): string {
102: if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false)) {
103: $encoding = 'gzip';
104: }
105:
106: if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== false)) {
107: $encoding = 'x-gzip';
108: }
109:
110: if (!isset($encoding) || ($level < -1 || $level > 9)) {
111: return $data;
112: }
113:
114: if (!extension_loaded('zlib') || ini_get('zlib.output_compression')) {
115: return $data;
116: }
117:
118: if (headers_sent()) {
119: return $data;
120: }
121:
122: if (connection_status()) {
123: return $data;
124: }
125:
126: $this->addHeader('Content-Encoding: ' . $encoding);
127:
128: return gzencode($data, $level);
129: }
130:
131: /**
132: * Output
133: *
134: * Displays the set HTML output
135: *
136: * @return void
137: */
138: public function output(): void {
139: if ($this->output) {
140: $output = $this->level ? $this->compress($this->output, $this->level) : $this->output;
141:
142: if (!headers_sent()) {
143: foreach ($this->headers as $header) {
144: header($header, true);
145: }
146: }
147:
148: echo $output;
149: }
150: }
151: }
152: