PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/gmarrot/jelix
PHP | 298 lines | 119 code | 48 blank | 131 comment | 15 complexity | 9bb5565cadd599e602d3cb7ef54c497e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  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 = false;
  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. * top content for head
  72. */
  73. protected $_headTop = array ();
  74. /**
  75. * bottom content for head
  76. */
  77. protected $_headBottom = array ();
  78. /**#@+
  79. * content for the body
  80. * @var array
  81. */
  82. protected $_bodyTop = array();
  83. protected $_bodyBottom = array();
  84. /**#@-*/
  85. /**
  86. * full path of php file to output. it should content php instruction
  87. * to display these variables:
  88. * - $HEADTOP: content added just after the opening <head> tag
  89. * - $HEADBOTTOM: content before the closing </head> tag
  90. * - $BODYTOP: content just after the <body> tag, at the top of the page
  91. * - $BODYBOTTOM: content just before the </body> tag, at the bottom of the page
  92. * - $BASEPATH: base path of the application, for links of your style sheets etc..
  93. * @var string
  94. */
  95. public $htmlFile = '';
  96. /**
  97. * list of plugins
  98. * @var array array of jIHTMLResponsePlugin
  99. * @since 1.3a1
  100. */
  101. protected $plugins = array();
  102. /**
  103. * constructor;
  104. * setup the charset, the lang
  105. */
  106. function __construct (){
  107. $this->_charset = jApp::config()->charset;
  108. $this->_lang = jApp::config()->locale;
  109. // load plugins
  110. $plugins = jApp::config()->jResponseHtml['plugins'];
  111. if ($plugins) {
  112. $plugins = preg_split('/ *, */', $plugins);
  113. foreach ($plugins as $name) {
  114. if (!$name)
  115. continue;
  116. $plugin = jApp::loadPlugin($name, 'htmlresponse', '.htmlresponse.php', $name.'HTMLResponsePlugin', $this);
  117. if ($plugin)
  118. $this->plugins[$name] = $plugin;
  119. // do nothing if the plugin does not exist, we could be already into the error handle
  120. }
  121. }
  122. parent::__construct();
  123. }
  124. /**
  125. * return the corresponding plugin
  126. * @param string $name the name of the plugin
  127. * @return jIHTMLResponsePlugin|null the plugin or null if it isn't loaded
  128. * @since 1.3a1
  129. */
  130. function getPlugin($name) {
  131. if (isset($this->plugins[$name]))
  132. return $this->plugins[$name];
  133. return null;
  134. }
  135. /**
  136. * add additional content into the document head
  137. * @param string $content
  138. * @param boolean $toTop true if you want to add it at the top of the head content, else false for the bottom
  139. * @since 1.0b1
  140. */
  141. final public function addHeadContent ($content, $toTop = false) {
  142. if ($toTop) {
  143. $this->_headTop[] = $content;
  144. }
  145. else {
  146. $this->_headBottom[] = $content;
  147. }
  148. }
  149. /**
  150. * add content to the body
  151. * you can add additionnal content, before or after the content of body
  152. * @param string $content additionnal html content
  153. * @param boolean $before true if you want to add it before the content, else false for after
  154. */
  155. function addContent($content, $before = false){
  156. if ($before) {
  157. $this->_bodyTop[]=$content;
  158. }
  159. else {
  160. $this->_bodyBottom[]=$content;
  161. }
  162. }
  163. /**
  164. * set the content-type in the http headers
  165. */
  166. protected function setContentType() {
  167. if($this->_isXhtml && $this->xhtmlContentType && strstr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml')){
  168. $this->_httpHeaders['Content-Type']='application/xhtml+xml;charset='.$this->_charset;
  169. }else{
  170. $this->_httpHeaders['Content-Type']='text/html;charset='.$this->_charset;
  171. }
  172. }
  173. /**
  174. * output the html content
  175. *
  176. * @return boolean true if the generated content is ok
  177. */
  178. public function output(){
  179. if($this->_outputOnlyHeaders){
  180. $this->sendHttpHeaders();
  181. return true;
  182. }
  183. foreach($this->plugins as $name=>$plugin)
  184. $plugin->afterAction();
  185. $this->doAfterActions();
  186. if ($this->htmlFile == '')
  187. throw new Exception('static page is missing');
  188. $this->setContentType();
  189. jLog::outputLog($this);
  190. foreach($this->plugins as $name=>$plugin)
  191. $plugin->beforeOutput();
  192. $HEADTOP = implode("\n", $this->_headTop);
  193. $HEADBOTTOM = implode("\n", $this->_headBottom);
  194. $BODYTOP = implode("\n", $this->_bodyTop);
  195. $BODYBOTTOM = implode("\n", $this->_bodyBottom);
  196. $BASEPATH = jApp::config()->urlengine['basePath'];
  197. ob_start();
  198. foreach($this->plugins as $name=>$plugin)
  199. $plugin->atBottom();
  200. $BODYBOTTOM .= ob_get_clean();
  201. $this->sendHttpHeaders();
  202. include($this->htmlFile);
  203. return true;
  204. }
  205. /**
  206. * The method you can overload in your inherited html response
  207. * overload it if you want to add processes (stylesheet, head settings, additionnal content etc..)
  208. * after any actions
  209. * @since 1.1
  210. */
  211. protected function doAfterActions(){
  212. }
  213. /**
  214. * output errors
  215. */
  216. public function outputErrors(){
  217. if (file_exists(jApp::appPath('responses/error.en_US.php')))
  218. $file = jApp::appPath('responses/error.en_US.php');
  219. else
  220. $file = JELIX_LIB_CORE_PATH.'response/error.en_US.php';
  221. // we erase already generated content
  222. $this->_headTop = array();
  223. $this->_headBottom = array();
  224. $this->_bodyBottom = array();
  225. $this->_bodyTop = array();
  226. jLog::outputLog($this);
  227. foreach($this->plugins as $name=>$plugin)
  228. $plugin->beforeOutputError();
  229. $HEADTOP = implode("\n", $this->_headTop);
  230. $HEADBOTTOM = implode("\n", $this->_headBottom);
  231. $BODYTOP = implode("\n", $this->_bodyTop);
  232. $BODYBOTTOM = implode("\n", $this->_bodyBottom);
  233. $BASEPATH = jApp::config()->urlengine['basePath'];
  234. header("HTTP/{$this->httpVersion} 500 Internal jelix error");
  235. header('Content-Type: text/html;charset='.$this->_charset);
  236. include($file);
  237. }
  238. /**
  239. * change the type of html for the output
  240. * @param boolean $xhtml true if you want xhtml, false if you want html
  241. */
  242. public function setXhtmlOutput($xhtml = true){
  243. $this->_isXhtml = $xhtml;
  244. }
  245. /**
  246. * says if the response will be xhtml or html
  247. * @return boolean true if it is xhtml
  248. */
  249. final public function isXhtml(){ return $this->_isXhtml; }
  250. }