/lib/jelix/core/jResponse.class.php

https://github.com/foxmask/Booster · PHP · 148 lines · 51 code · 16 blank · 81 comment · 8 complexity · a10d957873490b5c370b551df9f0d15d MD5 · raw file

  1. <?php
  2. /**
  3. * @package jelix
  4. * @subpackage core
  5. * @author Laurent Jouanneau
  6. * @contributor Julien Issler, Brice Tence
  7. * @copyright 2005-2010 Laurent Jouanneau
  8. * @copyright 2010 Julien Issler, 2011 Brice Tence
  9. * @link http://www.jelix.org
  10. * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  11. */
  12. /**
  13. * base class for response object
  14. * A response object is responsible to generate a content in a specific format.
  15. * @package jelix
  16. * @subpackage core
  17. */
  18. abstract class jResponse {
  19. /**
  20. * @var string ident of the response type
  21. */
  22. protected $_type = null;
  23. /**
  24. * @var array list of http headers that will be send to the client
  25. */
  26. protected $_httpHeaders = array();
  27. /**
  28. * @var boolean indicates if http headers have already been sent to the client
  29. */
  30. protected $_httpHeadersSent = false;
  31. /**
  32. * @var string the http status code to send
  33. */
  34. protected $_httpStatusCode ='200';
  35. /**
  36. * @var string the http status message to send
  37. */
  38. protected $_httpStatusMsg ='OK';
  39. public $httpVersion = '1.1';
  40. public $forcedHttpVersion = false;
  41. /**
  42. * constructor
  43. */
  44. function __construct() {
  45. if( $GLOBALS['gJConfig']->httpVersion != "" ) {
  46. $this->httpVersion = $GLOBALS['gJConfig']->httpVersion;
  47. $this->forcedHttpVersion = true;
  48. }
  49. }
  50. /**
  51. * Send the response in the correct format. If errors or exceptions appears
  52. * during this method, outputErrors will be called. So the
  53. * the content should be generated using the output buffer if errors can
  54. * be appeared during this generation. Be care of http headers.
  55. *
  56. * @return boolean true if the output is ok
  57. * @internal should take care about errors
  58. */
  59. abstract public function output();
  60. /**
  61. * Send a response with a generic error message.
  62. */
  63. public function outputErrors() {
  64. // if accept text/html
  65. if (isset($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'],'text/html')) {
  66. require_once(JELIX_LIB_CORE_PATH.'responses/jResponseBasicHtml.class.php');
  67. $response = new jResponseBasicHtml();
  68. $response->outputErrors();
  69. }
  70. else {
  71. // output text response
  72. header("HTTP/{$this->httpVersion} 500 Internal jelix error");
  73. header('Content-type: text/plain');
  74. echo $GLOBALS['gJCoord']->getGenericErrorMessage();
  75. }
  76. }
  77. /**
  78. * return the response type name
  79. * @return string the name
  80. */
  81. public final function getType(){ return $this->_type;}
  82. /**
  83. * return the format type name (eg the family type name)
  84. * @return string the name
  85. */
  86. public function getFormatType(){ return $this->_type;}
  87. /**
  88. * add an http header to the response.
  89. * will be send during the output of the response
  90. * @param string $htype the header type ("Content-Type", "Date-modified"...)
  91. * @param string $hcontent value of the header type
  92. * @param boolean $overwrite false if the value should be set only if it doesn't still exist
  93. */
  94. public function addHttpHeader($htype, $hcontent, $overwrite=true){
  95. if(!$overwrite && isset($this->_httpHeaders[$htype]))
  96. return;
  97. $this->_httpHeaders[$htype]=$hcontent;
  98. }
  99. /**
  100. * delete all http headers
  101. */
  102. public function clearHttpHeaders(){
  103. $this->_httpHeaders = array();
  104. $this->_httpStatusCode ='200';
  105. $this->_httpStatusMsg ='OK';
  106. }
  107. /**
  108. * set the http status code for the http header
  109. * @param string $code the status code (200, 404...)
  110. * @param string $msg the message following the status code ("OK", "Not Found"..)
  111. */
  112. public function setHttpStatus($code, $msg){ $this->_httpStatusCode=$code; $this->_httpStatusMsg=$msg;}
  113. /**
  114. * send http headers
  115. */
  116. protected function sendHttpHeaders(){
  117. header( ( isset($_SERVER['SERVER_PROTOCOL']) && !$this->forcedHttpVersion ?
  118. $_SERVER['SERVER_PROTOCOL'] :
  119. 'HTTP/'.$this->httpVersion ) .
  120. ' '.$this->_httpStatusCode.' '.$this->_httpStatusMsg );
  121. foreach($this->_httpHeaders as $ht=>$hc)
  122. header($ht.': '.$hc);
  123. $this->_httpHeadersSent=true;
  124. /*
  125. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  126. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  127. header("Cache-Control: no-store, no-cache, must-revalidate");
  128. header("Cache-Control: post-check=0, pre-check=0", false);
  129. header("Pragma: no-cache");
  130. */
  131. }
  132. }