PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/display/DisplayHandler.class.php

http://xe-core.googlecode.com/
PHP | 364 lines | 268 code | 37 blank | 59 comment | 66 complexity | a39dcf2913985b9f1882f41646e9b9c3 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @class DisplayHandler
  4. * @author NHN (developers@xpressengine.com)
  5. * DisplayHandler is responsible for displaying the execution result. \n
  6. * Depending on the request type, it can display either HTML or XML content.\n
  7. * Xml content is simple xml presentation of variables in oModule while html content
  8. * is the combination of the variables of oModue and template files/.
  9. */
  10. class DisplayHandler extends Handler
  11. {
  12. var $content_size = 0; // /< The size of displaying contents
  13. var $gz_enabled = FALSE; // / <a flog variable whether to call contents after compressing by gzip
  14. var $handler = NULL;
  15. /**
  16. * print either html or xml content given oModule object
  17. * @remark addon execution and the trigger execution are included within this method, which might create inflexibility for the fine grained caching
  18. * @param ModuleObject $oModule the module object
  19. * @return void
  20. */
  21. function printContent(&$oModule)
  22. {
  23. // Check if the gzip encoding supported
  24. if(
  25. (defined('__OB_GZHANDLER_ENABLE__') && __OB_GZHANDLER_ENABLE__ == 1) &&
  26. strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE &&
  27. function_exists('ob_gzhandler') &&
  28. extension_loaded('zlib') &&
  29. $oModule->gzhandler_enable
  30. )
  31. {
  32. $this->gz_enabled = TRUE;
  33. }
  34. // Extract contents to display by the request method
  35. if(Context::get('xeVirtualRequestMethod') == 'xml')
  36. {
  37. require_once("./classes/display/VirtualXMLDisplayHandler.php");
  38. $handler = new VirtualXMLDisplayHandler();
  39. }
  40. else if(Context::getRequestMethod() == 'XMLRPC')
  41. {
  42. require_once("./classes/display/XMLDisplayHandler.php");
  43. $handler = new XMLDisplayHandler();
  44. if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
  45. {
  46. $this->gz_enabled = FALSE;
  47. }
  48. }
  49. else if(Context::getRequestMethod() == 'JSON')
  50. {
  51. require_once("./classes/display/JSONDisplayHandler.php");
  52. $handler = new JSONDisplayHandler();
  53. }
  54. else if(Context::getRequestMethod() == 'JS_CALLBACK')
  55. {
  56. require_once("./classes/display/JSCallbackDisplayHandler.php");
  57. $handler = new JSCallbackDisplayHandler();
  58. }
  59. else
  60. {
  61. require_once("./classes/display/HTMLDisplayHandler.php");
  62. $handler = new HTMLDisplayHandler();
  63. }
  64. $output = $handler->toDoc($oModule);
  65. // call a trigger before display
  66. ModuleHandler::triggerCall('display', 'before', $output);
  67. // execute add-on
  68. $called_position = 'before_display_content';
  69. $oAddonController = &getController('addon');
  70. $addon_file = $oAddonController->getCacheFilePath(Mobile::isFromMobilePhone() ? "mobile" : "pc");
  71. @include($addon_file);
  72. if(method_exists($handler, "prepareToPrint"))
  73. {
  74. $handler->prepareToPrint($output);
  75. }
  76. // header output
  77. if($this->gz_enabled)
  78. {
  79. header("Content-Encoding: gzip");
  80. }
  81. $httpStatusCode = $oModule->getHttpStatusCode();
  82. if($httpStatusCode && $httpStatusCode != 200)
  83. {
  84. $this->_printHttpStatusCode($httpStatusCode);
  85. }
  86. else
  87. {
  88. if(Context::getResponseMethod() == 'JSON' || Context::getResponseMethod() == 'JS_CALLBACK')
  89. {
  90. $this->_printJSONHeader();
  91. }
  92. else if(Context::getResponseMethod() != 'HTML')
  93. {
  94. $this->_printXMLHeader();
  95. }
  96. else
  97. {
  98. $this->_printHTMLHeader();
  99. }
  100. }
  101. // debugOutput output
  102. $this->content_size = strlen($output);
  103. $output .= $this->_debugOutput();
  104. // results directly output
  105. if($this->gz_enabled)
  106. {
  107. print ob_gzhandler($output, 5);
  108. }
  109. else
  110. {
  111. print $output;
  112. }
  113. // call a trigger after display
  114. ModuleHandler::triggerCall('display', 'after', $content);
  115. }
  116. /**
  117. * Print debugging message to designated output source depending on the value set to __DEBUG_OUTPUT_. \n
  118. * This method only functions when __DEBUG__ variable is set to 1.
  119. * __DEBUG_OUTPUT__ == 0, messages are written in ./files/_debug_message.php
  120. * @return void
  121. */
  122. function _debugOutput()
  123. {
  124. if(!__DEBUG__)
  125. {
  126. return;
  127. }
  128. $end = getMicroTime();
  129. // Firebug console output
  130. if(__DEBUG_OUTPUT__ == 2 && version_compare(PHP_VERSION, '6.0.0') === -1)
  131. {
  132. static $firephp;
  133. if(!isset($firephp))
  134. {
  135. $firephp = FirePHP::getInstance(true);
  136. }
  137. if(__DEBUG_PROTECT__ == 1 && __DEBUG_PROTECT_IP__ != $_SERVER['REMOTE_ADDR'])
  138. {
  139. $firephp->fb('Change the value of __DEBUG_PROTECT_IP__ into your IP address in config/config.user.inc.php or config/config.inc.php', 'The IP address is not allowed.');
  140. return;
  141. }
  142. // display total execution time and Request/Response info
  143. if(__DEBUG__ & 2)
  144. {
  145. $firephp->fb(
  146. array(
  147. 'Request / Response info >>> ' . $_SERVER['REQUEST_METHOD'] . ' / ' . Context::getResponseMethod(),
  148. array(
  149. array('Request URI', 'Request method', 'Response method', 'Response contents size'),
  150. array(
  151. sprintf("%s:%s%s%s%s", $_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'], $_SERVER['PHP_SELF'], $_SERVER['QUERY_STRING'] ? '?' : '', $_SERVER['QUERY_STRING']),
  152. $_SERVER['REQUEST_METHOD'],
  153. Context::getResponseMethod(),
  154. $this->content_size . ' byte'
  155. )
  156. )
  157. ),
  158. 'TABLE'
  159. );
  160. $firephp->fb(
  161. array(
  162. 'Elapsed time >>> Total : ' . sprintf('%0.5f sec', $end - __StartTime__),
  163. array(array('DB queries', 'class file load', 'Template compile', 'XmlParse compile', 'PHP', 'Widgets', 'Trans Content'),
  164. array(
  165. sprintf('%0.5f sec', $GLOBALS['__db_elapsed_time__']),
  166. sprintf('%0.5f sec', $GLOBALS['__elapsed_class_load__']),
  167. sprintf('%0.5f sec (%d called)', $GLOBALS['__template_elapsed__'], $GLOBALS['__TemplateHandlerCalled__']),
  168. sprintf('%0.5f sec', $GLOBALS['__xmlparse_elapsed__']),
  169. sprintf('%0.5f sec', $end - __StartTime__ - $GLOBALS['__template_elapsed__'] - $GLOBALS['__xmlparse_elapsed__'] - $GLOBALS['__db_elapsed_time__'] - $GLOBALS['__elapsed_class_load__']),
  170. sprintf('%0.5f sec', $GLOBALS['__widget_excute_elapsed__']),
  171. sprintf('%0.5f sec', $GLOBALS['__trans_content_elapsed__'])
  172. )
  173. )
  174. ),
  175. 'TABLE'
  176. );
  177. }
  178. // display DB query history
  179. if((__DEBUG__ & 4) && $GLOBALS['__db_queries__'])
  180. {
  181. $queries_output = array(array('Query', 'Elapsed time', 'Result'));
  182. foreach($GLOBALS['__db_queries__'] as $query)
  183. {
  184. array_push($queries_output, array($query['query'], sprintf('%0.5f', $query['elapsed_time']), $query['result']));
  185. }
  186. $firephp->fb(
  187. array(
  188. 'DB Queries >>> ' . count($GLOBALS['__db_queries__']) . ' Queries, ' . sprintf('%0.5f sec', $GLOBALS['__db_elapsed_time__']),
  189. $queries_output
  190. ),
  191. 'TABLE'
  192. );
  193. }
  194. // dislpay the file and HTML comments
  195. }
  196. else
  197. {
  198. // display total execution time and Request/Response info
  199. if(__DEBUG__ & 2)
  200. {
  201. if(__DEBUG_PROTECT__ == 1 && __DEBUG_PROTECT_IP__ != $_SERVER['REMOTE_ADDR'])
  202. {
  203. return;
  204. }
  205. // Request/Response information
  206. $buff .= "\n- Request/ Response info\n";
  207. $buff .= sprintf("\tRequest URI \t\t\t: %s:%s%s%s%s\n", $_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'], $_SERVER['PHP_SELF'], $_SERVER['QUERY_STRING'] ? '?' : '', $_SERVER['QUERY_STRING']);
  208. $buff .= sprintf("\tRequest method \t\t\t: %s\n", $_SERVER['REQUEST_METHOD']);
  209. $buff .= sprintf("\tResponse method \t\t: %s\n", Context::getResponseMethod());
  210. $buff .= sprintf("\tResponse contents size\t\t: %d byte\n", $this->content_size);
  211. // total execution time
  212. $buff .= sprintf("\n- Total elapsed time : %0.5f sec\n", $end - __StartTime__);
  213. $buff .= sprintf("\tclass file load elapsed time \t: %0.5f sec\n", $GLOBALS['__elapsed_class_load__']);
  214. $buff .= sprintf("\tTemplate compile elapsed time\t: %0.5f sec (%d called)\n", $GLOBALS['__template_elapsed__'], $GLOBALS['__TemplateHandlerCalled__']);
  215. $buff .= sprintf("\tXmlParse compile elapsed time\t: %0.5f sec\n", $GLOBALS['__xmlparse_elapsed__']);
  216. $buff .= sprintf("\tPHP elapsed time \t\t: %0.5f sec\n", $end - __StartTime__ - $GLOBALS['__template_elapsed__'] - $GLOBALS['__xmlparse_elapsed__'] - $GLOBALS['__db_elapsed_time__'] - $GLOBALS['__elapsed_class_load__']);
  217. $buff .= sprintf("\tDB class elapsed time \t\t: %0.5f sec\n", $GLOBALS['__dbclass_elapsed_time__'] - $GLOBALS['__db_elapsed_time__']);
  218. // widget execution time
  219. $buff .= sprintf("\n\tWidgets elapsed time \t\t: %0.5f sec", $GLOBALS['__widget_excute_elapsed__']);
  220. // layout execution time
  221. $buff .= sprintf("\n\tLayout compile elapsed time \t: %0.5f sec", $GLOBALS['__layout_compile_elapsed__']);
  222. // Widgets, the editor component replacement time
  223. $buff .= sprintf("\n\tTrans Content \t\t\t: %0.5f sec\n", $GLOBALS['__trans_content_elapsed__']);
  224. }
  225. // DB Logging
  226. if(__DEBUG__ & 4)
  227. {
  228. if(__DEBUG_PROTECT__ == 1 && __DEBUG_PROTECT_IP__ != $_SERVER['REMOTE_ADDR'])
  229. {
  230. return;
  231. }
  232. if($GLOBALS['__db_queries__'])
  233. {
  234. $buff .= sprintf("\n- DB Queries : %d Queries. %0.5f sec\n", count($GLOBALS['__db_queries__']), $GLOBALS['__db_elapsed_time__']);
  235. $num = 0;
  236. foreach($GLOBALS['__db_queries__'] as $query)
  237. {
  238. $buff .= sprintf("\t%02d. %s\n\t\t%0.6f sec. ", ++$num, $query['query'], $query['elapsed_time']);
  239. if($query['result'] == 'Success')
  240. {
  241. $buff .= "Query Success\n";
  242. }
  243. else
  244. {
  245. $buff .= sprintf("Query $s : %d\n\t\t\t %s\n", $query['result'], $query['errno'], $query['errstr']);
  246. }
  247. $buff .= sprintf("\t\tConnection: %s\n", $query['connection']);
  248. }
  249. }
  250. }
  251. // Output in HTML comments
  252. if($buff && __DEBUG_OUTPUT__ == 1 && Context::getResponseMethod() == 'HTML')
  253. {
  254. $buff = sprintf("[%s %s:%d]\n%s\n", date('Y-m-d H:i:s'), $file_name, $line_num, print_r($buff, true));
  255. if(__DEBUG_PROTECT__ == 1 && __DEBUG_PROTECT_IP__ != $_SERVER['REMOTE_ADDR'])
  256. {
  257. $buff = 'The IP address is not allowed. Change the value of __DEBUG_PROTECT_IP__ into your IP address in config/config.user.inc.php or config/config.inc.php';
  258. }
  259. return "<!--\r\n" . $buff . "\r\n-->";
  260. }
  261. // Output to a file
  262. if($buff && __DEBUG_OUTPUT__ == 0)
  263. {
  264. $debug_file = _XE_PATH_ . 'files/_debug_message.php';
  265. $buff = sprintf("[%s %s:%d]\n%s\n", date('Y-m-d H:i:s'), $file_name, $line_num, print_r($buff, true));
  266. $buff = str_repeat('=', 40) . "\n" . $buff . str_repeat('-', 40);
  267. $buff = "\n<?php\n/*" . $buff . "*/\n?>\n";
  268. if(@!$fp = fopen($debug_file, 'a'))
  269. {
  270. return;
  271. }
  272. fwrite($fp, $buff);
  273. fclose($fp);
  274. }
  275. }
  276. }
  277. /**
  278. * print a HTTP HEADER for XML, which is encoded in UTF-8
  279. * @return void
  280. */
  281. function _printXMLHeader()
  282. {
  283. header("Content-Type: text/xml; charset=UTF-8");
  284. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  285. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  286. header("Cache-Control: no-store, no-cache, must-revalidate");
  287. header("Cache-Control: post-check=0, pre-check=0", false);
  288. header("Pragma: no-cache");
  289. }
  290. /**
  291. * print a HTTP HEADER for HTML, which is encoded in UTF-8
  292. * @return void
  293. */
  294. function _printHTMLHeader()
  295. {
  296. header("Content-Type: text/html; charset=UTF-8");
  297. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  298. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  299. header("Cache-Control: no-store, no-cache, must-revalidate");
  300. header("Cache-Control: post-check=0, pre-check=0", false);
  301. header("Pragma: no-cache");
  302. }
  303. /**
  304. * print a HTTP HEADER for JSON, which is encoded in UTF-8
  305. * @return void
  306. */
  307. function _printJSONHeader()
  308. {
  309. header("Content-Type: text/html; charset=UTF-8");
  310. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  311. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  312. header("Cache-Control: no-store, no-cache, must-revalidate");
  313. header("Cache-Control: post-check=0, pre-check=0", false);
  314. header("Pragma: no-cache");
  315. }
  316. /**
  317. * print a HTTP HEADER for HTML, which is encoded in UTF-8
  318. * @return void
  319. */
  320. function _printHttpStatusCode($code)
  321. {
  322. $statusMessage = Context::get('http_status_message');
  323. header("HTTP/1.0 $code $statusMessage");
  324. }
  325. }
  326. /* End of file DisplayHandler.class.php */
  327. /* Location: ./classes/display/DisplayHandler.class.php */