[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/net/src/ -> lmbHttpResponse.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/core/src/exception/lmbException.class.php');
  10  lmb_require('limb/net/src/lmbHttpRedirectStrategy.class.php');
  11  
  12  /**

  13   * class lmbHttpResponse.

  14   *

  15   * @package net

  16   * @version $Id: lmbHttpResponse.class.php 5945 2007-06-06 08:31:43Z pachanga $

  17   */
  18  class lmbHttpResponse
  19  {
  20    protected $response_string = '';
  21    protected $response_file_path = '';
  22    protected $headers = array();
  23    protected $cookies = array();
  24    protected $is_redirected = false;
  25    protected $redirected_path = false;
  26    protected $redirect_strategy = null;
  27    protected $transaction_started = false;
  28  
  29    function setRedirectStrategy($strategy)
  30    {
  31      $this->redirect_strategy = $strategy;
  32    }
  33  
  34    function redirect($path)
  35    {
  36      $this->_ensureTransactionStarted();
  37  
  38      if ($this->is_redirected)
  39        return;
  40  
  41      if($this->redirect_strategy === null)
  42        $strategy = $this->_getDefaultRedirectStrategy();
  43      else
  44        $strategy = $this->redirect_strategy;
  45  
  46      $strategy->redirect($this, $path);
  47  
  48      $this->is_redirected = true;
  49      $this->redirected_path = $path;
  50    }
  51  
  52    function getRedirectedPath()
  53    {
  54      if($this->is_redirected)
  55        return $this->redirected_path;
  56      else
  57        return '';
  58    }
  59  
  60    function getRedirectedUrl()
  61    {
  62      return $this->getRedirectedPath();
  63    }
  64  
  65    protected function _getDefaultRedirectStrategy()
  66    {
  67      lmb_require(dirname(__FILE__) . '/lmbHttpRedirectStrategy.class.php');
  68      return new lmbHttpRedirectStrategy();
  69    }
  70  
  71    function reset()
  72    {
  73      $this->response_string = '';
  74      $this->response_file_path = '';
  75      $this->headers = array();
  76      $this->is_redirected = false;
  77      $this->transaction_started = false;
  78    }
  79  
  80    function start()
  81    {
  82      $this->reset();
  83      $this->transaction_started = true;
  84    }
  85  
  86    function isRedirected()
  87    {
  88      return $this->is_redirected;
  89    }
  90  
  91    function getStatus()
  92    {
  93      $status = null;
  94      foreach($this->headers as $header)
  95      {
  96        if(preg_match('~^HTTP/1.\d[^\d]+(\d+)[^\d]*~i', $header, $matches))
  97          $status = (int)$matches[1];
  98      }
  99  
 100      if($status)
 101        return $status;
 102      else
 103        return 200;
 104    }
 105  
 106    function getDirective($directive_name)
 107    {
 108      $directive = null;
 109      $regex = '~^' . preg_quote($directive_name). "\s*:(.*)$~i";
 110      foreach($this->headers as $header)
 111      {
 112        if(preg_match($regex, $header, $matches))
 113          $directive = trim($matches[1]);
 114      }
 115  
 116      if($directive)
 117        return $directive;
 118      else
 119        return false;
 120    }
 121  
 122    function getContentType()
 123    {
 124      if($directive = $this->getDirective('content-type'))
 125      {
 126        list($type, ) = explode(';', $directive);
 127        return trim($type);
 128      }
 129      else
 130        return 'text/html';
 131    }
 132  
 133    function getMimeType()
 134    {
 135      return $this->getContentType();
 136    }
 137  
 138    function getResponseString()
 139    {
 140      return $this->response_string;
 141    }
 142  
 143    function isStarted()
 144    {
 145      return $this->transaction_started;
 146    }
 147  
 148    function isEmpty()
 149    {
 150      $status = $this->getStatus();
 151  
 152      return (
 153        !$this->is_redirected &&
 154        empty($this->response_string) &&
 155        empty($this->response_file_path) &&
 156        ($status != 304 &&  $status != 412));//???

 157    }
 158  
 159    function headersSent()
 160    {
 161      return sizeof($this->headers) > 0;
 162    }
 163  
 164    function fileSent()
 165    {
 166      return !empty($this->response_file_path);
 167    }
 168  
 169    function reload()
 170    {
 171      $this->redirect($_SERVER['PHP_SELF']);
 172    }
 173  
 174    function header($header)
 175    {
 176      $this->_ensureTransactionStarted();
 177  
 178      $this->headers[] = $header;
 179    }
 180  
 181    function setCookie($name, $value, $expire = 0, $path = '/', $domain = '', $secure = false)
 182    {
 183      $this->_ensureTransactionStarted();
 184  
 185      $this->cookies[] = array('name' => $name,
 186                               'value' => $value,
 187                               'expire' => $expire,
 188                               'path' => $path,
 189                               'domain' => $domain,
 190                               'secure' => $secure);
 191    }
 192  
 193    function readFile($file_path)
 194    {
 195      $this->_ensureTransactionStarted();
 196  
 197      $this->response_file_path = $file_path;
 198    }
 199  
 200    function write($string)
 201    {
 202      $this->_ensureTransactionStarted();
 203  
 204      $this->response_string = $string;
 205    }
 206  
 207    function append($string)
 208    {
 209      $this->_ensureTransactionStarted();
 210  
 211      $this->response_string .= $string;
 212    }
 213  
 214    function commit()
 215    {
 216      $this->_ensureTransactionStarted();
 217  
 218      foreach($this->headers as $header)
 219        $this->_sendHeader($header);
 220  
 221      foreach($this->cookies as $cookie)
 222        $this->_sendCookie($cookie);
 223  
 224      if(!empty($this->response_file_path))
 225        $this->_sendFile($this->response_file_path);
 226      else if(!empty($this->response_string))
 227        $this->_sendString($this->response_string);
 228  
 229      $this->transaction_started = false;
 230    }
 231  
 232    protected function _sendHeader($header)
 233    {
 234      header($header);
 235    }
 236  
 237    protected function _sendCookie($cookie)
 238    {
 239      setcookie($cookie['name'], $cookie['value'], $cookie['expire'], $cookie['path'], $cookie['domain'], $cookie['secure']);
 240    }
 241  
 242    protected function _sendString($string)
 243    {
 244      echo $string;
 245    }
 246  
 247    protected function _sendFile($file_path)
 248    {
 249      readfile($file_path);
 250    }
 251  
 252    protected function _ensureTransactionStarted()
 253    {
 254      if(!$this->transaction_started)
 255        $this->start();
 256    }
 257  }
 258  ?>


Generated: Tue Oct 7 05:02:03 2008 Cross-referenced by PHPXref 0.7