[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/ -> AkResponse.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  // +----------------------------------------------------------------------+
   5  // | Akelos Framework - http://www.akelos.org                             |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
   8  // | Released under the GNU Lesser General Public License, see LICENSE.txt|
   9  // +----------------------------------------------------------------------+
  10  
  11  if(!class_exists('AkResponse')){
  12  
  13  /**
  14   * @package ActionController
  15   * @subpackage Response
  16   * @author Bermi Ferrer <bermi a.t akelos c.om>
  17   * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  18   * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  19   */
  20  class AkResponse extends AkObject
  21  {
  22      var $_resutl_stack = array();
  23      var $_headers = array();
  24      var $_headers_sent = array();
  25      var $body = '';
  26      var $__Logger;
  27      
  28      var $_output_flushed = false;
  29      
  30      var $_default_status = 200;
  31      
  32      function set($data, $id = null)
  33      {
  34          if(isset($id)){
  35              $this->_resutl_stack[$id] = $data;
  36          }else{
  37              $this->_resutl_stack[] = $data;
  38          }
  39      }
  40  
  41      function &get($id)
  42      {
  43          if(isset($this->_resutl_stack[$id])){
  44              return $this->_resutl_stack[$id];
  45          }
  46          return false;
  47      }
  48  
  49      function getAll()
  50      {
  51          return $this->_resutl_stack;
  52      }
  53  
  54      function addHeader()
  55      {
  56          $args = func_get_args();
  57          if(!empty($args[1])){
  58              $this->_headers[$args[0]] = $args[1];
  59          }elseif (!empty($args[0]) && is_array($args[0])){
  60              $this->_headers = array_merge($this->_headers,$args[0]);
  61          }elseif (!empty($args[0])){
  62              $this->_headers[] = $args[0];
  63          }
  64      }
  65      function setContentTypeForFormat($format)
  66      {
  67          if (!empty($format)) {
  68              $mime_type = Ak::mime_content_type('file.'.$format);
  69              if (!empty($mime_type)) {
  70                  $this->addHeader('Content-Type', $mime_type);
  71              }
  72          }
  73      }
  74      function outputResults()
  75      {
  76          $this->sendHeaders();
  77          if(is_object($this->body) && method_exists($this->body,'stream')){
  78              AK_LOG_EVENTS && !empty($this->_Logger) ? $this->_Logger->message("Sending response as stream") : null;
  79              $this->body->stream();
  80          }else{
  81              AK_LOG_EVENTS && !empty($this->_Logger) ? $this->_Logger->message("Sending response") : null;
  82              echo $this->body;
  83          }
  84      }
  85      function getStatus()
  86      {
  87          return isset($this->_headers['Status'])?$this->_headers['Status']:$this->_default_status;
  88      }
  89      function sendHeaders($terminate_if_redirected = true)
  90      {
  91          /**
  92           * Fix a problem with IE 6.0 on opening downloaded files:
  93           * If Cache-Control: IE removes the file it just downloaded from 
  94           * its cache immediately 
  95           * after it displays the "open/save" dialog, which means that if you 
  96           * hit "open" the file isn't there anymore when the application that 
  97           * is called for handling the download is run, so let's workaround that
  98           */
  99          if(isset($this->_headers['Cache-Control']) && $this->_headers['Cache-Control'] == 'no-cache'){
 100              $this->_headers['Cache-Control'] = 'private';
 101          }
 102          if (empty($this->_headers['Status'])) {
 103              $this->_headers['Status'] = $this->_default_status;
 104          }
 105  
 106          $status = $this->_getStatusHeader($this->_headers['Status']);
 107          array_unshift($this->_headers,  $status ? $status : (strstr('HTTP/1.1 '.$this->_headers['Status'],'HTTP') ? $this->_headers['Status'] : 'HTTP/1.1 '.$this->_headers['Status']));
 108          unset($this->_headers['Status']);
 109  
 110          
 111          if(!empty($this->_headers) && is_array($this->_headers)){
 112              $this->addHeader('Connection: close');
 113              foreach ($this->_headers as $k=>$v){
 114                  $header = trim((!is_numeric($k) ? $k.': ' : '').$v);
 115                  $this->_headers_sent[] = $header;
 116                  if(strtolower(substr($header,0,9)) == 'location:'){
 117                      $header = str_replace(array("\n","\r"), '', $header);
 118                      $_redirected = true;
 119                      if(AK_DESKTOP){
 120                          $javascript_redirection = '<title>'.Ak::t('Loading...').'</title><script type="text/javascript">location = "'.substr($header,9).'";</script>';
 121                          continue;
 122                      }
 123                  }
 124                  if(strtolower(substr($header,0,13)) == 'content-type:'){
 125                      $_has_content_type = true;
 126                  }
 127                  AK_LOG_EVENTS && !empty($this->_Logger) ? $this->_Logger->message("Sending header:  $header") : null;
 128                  header($header);
 129              }
 130          }
 131          
 132          if(empty($_has_content_type) && defined('AK_CHARSET') && (empty($_redirected) || (!empty($_redirected) && !empty($javascript_redirection)))){
 133              header('Content-Type: text/html; charset='.AK_CHARSET);
 134              $this->_headers_sent[] = 'Content-Type: text/html; charset='.AK_CHARSET;
 135          }
 136          
 137          if(!empty($javascript_redirection)){
 138              echo $javascript_redirection;
 139          }
 140          
 141          $terminate_if_redirected ? (!empty($_redirected) ? exit() : null) : null;
 142      }
 143      function addSentHeader($header)
 144      {
 145          $this->_headers_sent[] = $header;
 146      }
 147      function deleteHeader($header)
 148      {
 149          unset($this->_headers[$header]);
 150      }
 151  
 152      /**
 153      * Redirects to given $url, after turning off $this->autoRender.
 154      *
 155      * @param unknown_type $url
 156      */
 157      function redirect ($url)
 158      {
 159          $this->autoRender = false;
 160          if(!empty($this->_headers['Status']) && substr($this->_headers['Status'],0,3) != '301'){
 161              $this->_headers['Status'] = 302;    
 162          }
 163          $this->addHeader('Location', $url);
 164          $this->sendHeaders();
 165      }
 166  
 167  
 168      function _getStatusHeader($status_code)
 169      {
 170          $status_codes = array (
 171          100 => "HTTP/1.1 100 Continue",
 172          101 => "HTTP/1.1 101 Switching Protocols",
 173          200 => "HTTP/1.1 200 OK",
 174          201 => "HTTP/1.1 201 Created",
 175          202 => "HTTP/1.1 202 Accepted",
 176          203 => "HTTP/1.1 203 Non-Authoritative Information",
 177          204 => "HTTP/1.1 204 No Content",
 178          205 => "HTTP/1.1 205 Reset Content",
 179          206 => "HTTP/1.1 206 Partial Content",
 180          300 => "HTTP/1.1 300 Multiple Choices",
 181          301 => "HTTP/1.1 301 Moved Permanently",
 182          302 => "HTTP/1.1 302 Found",
 183          303 => "HTTP/1.1 303 See Other",
 184          304 => "HTTP/1.1 304 Not Modified",
 185          305 => "HTTP/1.1 305 Use Proxy",
 186          307 => "HTTP/1.1 307 Temporary Redirect",
 187          400 => "HTTP/1.1 400 Bad Request",
 188          401 => "HTTP/1.1 401 Unauthorized",
 189          402 => "HTTP/1.1 402 Payment Required",
 190          403 => "HTTP/1.1 403 Forbidden",
 191          404 => "HTTP/1.1 404 Not Found",
 192          405 => "HTTP/1.1 405 Method Not Allowed",
 193          406 => "HTTP/1.1 406 Not Acceptable",
 194          407 => "HTTP/1.1 407 Proxy Authentication Required",
 195          408 => "HTTP/1.1 408 Request Time-out",
 196          409 => "HTTP/1.1 409 Conflict",
 197          410 => "HTTP/1.1 410 Gone",
 198          411 => "HTTP/1.1 411 Length Required",
 199          412 => "HTTP/1.1 412 Precondition Failed",
 200          413 => "HTTP/1.1 413 Request Entity Too Large",
 201          414 => "HTTP/1.1 414 Request-URI Too Large",
 202          415 => "HTTP/1.1 415 Unsupported Media Type",
 203          416 => "HTTP/1.1 416 Requested range not satisfiable",
 204          417 => "HTTP/1.1 417 Expectation Failed",
 205          500 => "HTTP/1.1 500 Internal Server Error",
 206          501 => "HTTP/1.1 501 Not Implemented",
 207          502 => "HTTP/1.1 502 Bad Gateway",
 208          503 => "HTTP/1.1 503 Service Unavailable",
 209          504 => "HTTP/1.1 504 Gateway Time-out"
 210          );
 211          return empty($status_codes[$status_code]) ? false : $status_codes[$status_code];
 212      }
 213  }
 214  
 215  
 216  function &AkResponse()
 217  {
 218      $null = null;
 219      $AkResponse =& Ak::singleton('AkResponse', $null);
 220      AK_LOG_EVENTS && empty($AkResponse->_Logger) ? ($AkResponse->_Logger =& Ak::getLogger()) : null;
 221      return $AkResponse;
 222  }
 223  
 224  }
 225  
 226  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6