class Response { private $headers = array(); private $level = 0; private $output; /** * Add header to the response */ public function addHeader($header) { $this->headers[] = $header; } /** * Redirect to a new URL with a specific status * * @param string $url The URL to redirect to * @param int $status The status code for the redirection (default is 302) */ public function redirect($url, $status = 302) { // Ensure no output before sending headers if (headers_sent()) { exit('Headers already sent. Cannot redirect.'); } // Clean up URL $cleaned_url = str_replace(array('&', "\n", "\r"), array('&', '', ''), $url); // Send location header header('Location: ' . $cleaned_url, true, $status); // Exit to prevent further execution exit(); } /** * Set compression level for output * * @param int $level The level of compression (0-9) */ public function setCompression($level) { $this->level = $level; } /** * Get the current output * * @return string The current output */ public function getOutput() { return $this->output; } /** * Set the output content * * @param string $output The output to set */ public function setOutput($output) { $this->output = $output; } /** * Compress data (if supported) * * @param string $data The data to compress * @param int $level The compression level (0-9) * @return string The compressed data */ private function compress($data, $level = 0) { // Check if gzip compression is supported and accepted by the client if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false)) { $encoding = 'gzip'; } else { $encoding = ''; } // If compression is not supported or allowed, return the original data if (!$encoding || !extension_loaded('zlib') || ini_get('zlib.output_compression')) { return $data; } // Ensure headers are not already sent if (headers_sent() || connection_status()) { return $data; } // Add the appropriate header for content encoding $this->addHeader('Content-Encoding: ' . $encoding); return gzencode($data, (int)$level); } /** * Output the final content */ public function output() { if ($this->output) { // Journal Theme Minification and other custom logic if (defined('JOURNAL3_ACTIVE')) { if (\Journal3::getInstance()->settings->get('performanceHTMLMinify')) { $this->output = \Journal3\Minifier::minifyHTML($this->output); } if (\Journal3::getInstance()->settings->get('performanceJSDefer')) { $this->output = str_replace('