PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/jelix/core/response/jResponseBasicHtml.class.php

http://github.com/jelix/jelix
PHP | 281 lines | 109 code | 47 blank | 125 comment | 13 complexity | 0fce516811cc4715444f8f9cdbc50748 MD5 | raw file
Possible License(s): BSD-3-Clause, JSON, GPL-3.0, LGPL-3.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * @package jelix
  4. * @subpackage core_response
  5. * @author Laurent Jouanneau
  6. * @contributor Julien Issler, Brice Tence
  7. * @copyright 2010-2012 Laurent Jouanneau
  8. * @copyright 2011 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. * interface for plugins for jResponseBasicHtml or jResponseHtml, which allows
  14. * to make changes in the response at several step
  15. */
  16. interface jIHTMLResponsePlugin {
  17. public function __construct(jResponse $c);
  18. /**
  19. * called just before the jResponseBasicHtml::doAfterActions() call
  20. */
  21. public function afterAction();
  22. /**
  23. * called just before the final output. This is the opportunity
  24. * to make changes before the head and body output. At this step
  25. * the main content (if any) is already generated.
  26. */
  27. public function beforeOutput();
  28. /**
  29. * called when the content is generated, and potentially sent, except
  30. * the body end tag and the html end tags. This method can output
  31. * directly some contents.
  32. */
  33. public function atBottom();
  34. /**
  35. * called just before the output of an error page
  36. */
  37. public function beforeOutputError();
  38. }
  39. /**
  40. * Basic HTML response. the HTML content should be provided by a simple php file.
  41. * @package jelix
  42. * @subpackage core_response
  43. */
  44. class jResponseBasicHtml extends jResponse {
  45. /**
  46. * jresponse id
  47. * @var string
  48. */
  49. protected $_type = 'html';
  50. /**
  51. * the charset of the document
  52. * @var string
  53. */
  54. protected $_charset;
  55. /**
  56. * the lang of the document
  57. * @var string
  58. */
  59. protected $_lang;
  60. /**
  61. * says if the document is in xhtml or html
  62. */
  63. protected $_isXhtml = true;
  64. /**
  65. * says if xhtml content type should be send or not.
  66. * it true, a verification of HTTP_ACCEPT is done.
  67. * @var boolean
  68. */
  69. public $xhtmlContentType = false;
  70. /**
  71. * content for head
  72. */
  73. protected $_headBottom = array ();
  74. /**#@+
  75. * content for the body
  76. * @var array
  77. */
  78. protected $_bodyTop = array();
  79. protected $_bodyBottom = array();
  80. /**#@-*/
  81. /**
  82. * full path of php file to output. it should content php instruction
  83. * to display these variables:
  84. * - $HEADBOTTOM: content before th </head> tag
  85. * - $BODYTOP: content just after the <body> tag, at the top of the page
  86. * - $BODYBOTTOM: content just before the </body> tag, at the bottom of the page
  87. * @var string
  88. */
  89. public $htmlFile = '';
  90. /**
  91. * list of plugins
  92. * @var array array of jIHTMLResponsePlugin
  93. * @since 1.3a1
  94. */
  95. protected $plugins = array();
  96. /**
  97. * constructor;
  98. * setup the charset, the lang
  99. */
  100. function __construct (){
  101. $this->_charset = jApp::config()->charset;
  102. $this->_lang = jApp::config()->locale;
  103. // load plugins
  104. $plugins = jApp::config()->jResponseHtml['plugins'];
  105. if ($plugins) {
  106. $plugins = preg_split('/ *, */', $plugins);
  107. foreach ($plugins as $name) {
  108. if (!$name)
  109. continue;
  110. $plugin = jApp::loadPlugin($name, 'htmlresponse', '.htmlresponse.php', $name.'HTMLResponsePlugin', $this);
  111. if ($plugin)
  112. $this->plugins[$name] = $plugin;
  113. // do nothing if the plugin does not exist, we could be already into the error handle
  114. }
  115. }
  116. parent::__construct();
  117. }
  118. /**
  119. * return the corresponding plugin
  120. * @param string $name the name of the plugin
  121. * @return jIHTMLResponsePlugin|null the plugin or null if it isn't loaded
  122. * @since 1.3a1
  123. */
  124. function getPlugin($name) {
  125. if (isset($this->plugins[$name]))
  126. return $this->plugins[$name];
  127. return null;
  128. }
  129. /**
  130. * add additional content into the document head
  131. * @param string $content
  132. * @since 1.0b1
  133. */
  134. final public function addHeadContent ($content){
  135. $this->_headBottom[] = $content;
  136. }
  137. /**
  138. * add content to the body
  139. * you can add additionnal content, before or after the content of body
  140. * @param string $content additionnal html content
  141. * @param boolean $before true if you want to add it before the content, else false for after
  142. */
  143. function addContent($content, $before = false){
  144. if ($before) {
  145. $this->_bodyTop[]=$content;
  146. }
  147. else {
  148. $this->_bodyBottom[]=$content;
  149. }
  150. }
  151. /**
  152. * set the content-type in the http headers
  153. */
  154. protected function setContentType() {
  155. if($this->_isXhtml && $this->xhtmlContentType && strstr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml')){
  156. $this->_httpHeaders['Content-Type']='application/xhtml+xml;charset='.$this->_charset;
  157. }else{
  158. $this->_httpHeaders['Content-Type']='text/html;charset='.$this->_charset;
  159. }
  160. }
  161. /**
  162. * output the html content
  163. *
  164. * @return boolean true if the generated content is ok
  165. */
  166. public function output(){
  167. if($this->_outputOnlyHeaders){
  168. $this->sendHttpHeaders();
  169. return true;
  170. }
  171. foreach($this->plugins as $name=>$plugin)
  172. $plugin->afterAction();
  173. $this->doAfterActions();
  174. if ($this->htmlFile == '')
  175. throw new Exception('static page is missing');
  176. $this->setContentType();
  177. jLog::outputLog($this);
  178. foreach($this->plugins as $name=>$plugin)
  179. $plugin->beforeOutput();
  180. $HEADBOTTOM = implode("\n", $this->_headBottom);
  181. $BODYTOP = implode("\n", $this->_bodyTop);
  182. $BODYBOTTOM = implode("\n", $this->_bodyBottom);
  183. ob_start();
  184. foreach($this->plugins as $name=>$plugin)
  185. $plugin->atBottom();
  186. $BODYBOTTOM .= ob_get_clean();
  187. $this->sendHttpHeaders();
  188. include($this->htmlFile);
  189. return true;
  190. }
  191. /**
  192. * The method you can overload in your inherited html response
  193. * overload it if you want to add processes (stylesheet, head settings, additionnal content etc..)
  194. * after any actions
  195. * @since 1.1
  196. */
  197. protected function doAfterActions(){
  198. }
  199. /**
  200. * output errors
  201. */
  202. public function outputErrors(){
  203. if (file_exists(jApp::appPath('responses/error.en_US.php')))
  204. $file = jApp::appPath('responses/error.en_US.php');
  205. else
  206. $file = JELIX_LIB_CORE_PATH.'response/error.en_US.php';
  207. // we erase already generated content
  208. $this->_headBottom = array();
  209. $this->_bodyBottom = array();
  210. $this->_bodyTop = array();
  211. jLog::outputLog($this);
  212. foreach($this->plugins as $name=>$plugin)
  213. $plugin->beforeOutputError();
  214. $HEADBOTTOM = implode("\n", $this->_headBottom);
  215. $BODYTOP = implode("\n", $this->_bodyTop);
  216. $BODYBOTTOM = implode("\n", $this->_bodyBottom);
  217. $basePath = jApp::config()->urlengine['basePath'];
  218. header("HTTP/{$this->httpVersion} 500 Internal jelix error");
  219. header('Content-Type: text/html;charset='.$this->_charset);
  220. include($file);
  221. }
  222. /**
  223. * change the type of html for the output
  224. * @param boolean $xhtml true if you want xhtml, false if you want html
  225. */
  226. public function setXhtmlOutput($xhtml = true){
  227. $this->_isXhtml = $xhtml;
  228. }
  229. /**
  230. * says if the response will be xhtml or html
  231. * @return boolean true if it is xhtml
  232. */
  233. final public function isXhtml(){ return $this->_isXhtml; }
  234. }