Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.42 KB | None | 0 0
  1. <?php
  2. class Response {
  3.     private $headers = array();
  4.     private $level = 0;
  5.     private $output;
  6.    
  7.     public function addHeader($header) {
  8.         $this->headers[] = $header;
  9.     }
  10.  
  11.     public function redirect($url) {
  12.         header('Location: ' . $url);
  13.         exit;
  14.     }
  15.    
  16.     public function setCompression($level) {
  17.         $this->level = $level;
  18.     }
  19.        
  20.     public function setOutput($output) {
  21.         $this->output = $output;
  22.     }
  23.  
  24.     private function compress($data, $level = 0) {
  25.         if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false)) {
  26.             $encoding = 'gzip';
  27.         }
  28.  
  29.         if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== false)) {
  30.             $encoding = 'x-gzip';
  31.         }
  32.  
  33.         if (!isset($encoding)) {
  34.             return $data;
  35.         }
  36.  
  37.         if (!extension_loaded('zlib') || ini_get('zlib.output_compression')) {
  38.             return $data;
  39.         }
  40.  
  41.         if (headers_sent()) {
  42.             return $data;
  43.         }
  44.  
  45.         if (connection_status()) {
  46.             return $data;
  47.         }
  48.        
  49.         $this->addHeader('Content-Encoding: ' . $encoding);
  50.  
  51.         return gzencode($data, (int)$level);
  52.     }
  53.  
  54.     public function output() {
  55.         if ($this->output) {
  56.             if ($this->level) {
  57.                 $ouput = $this->compress($this->output, $this->level);
  58.             } else {
  59.                 $ouput = $this->output;
  60.             }  
  61.                
  62.             if (!headers_sent()) {
  63.                 foreach ($this->headers as $header) {
  64.                     header($header, true);
  65.                 }
  66.             }
  67.            
  68.             echo $ouput;
  69.         }
  70.     }
  71. }
  72. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement