XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
DisplayHandler.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) NAVER <http://www.navercorp.com> */
3 
12 class DisplayHandler extends Handler
13 {
14 
15  var $content_size = 0; // /< The size of displaying contents
16  var $gz_enabled = FALSE; // / <a flog variable whether to call contents after compressing by gzip
17  var $handler = NULL;
18 
25  function printContent(&$oModule)
26  {
27  // Check if the gzip encoding supported
28  if(
29  (defined('__OB_GZHANDLER_ENABLE__') && __OB_GZHANDLER_ENABLE__ == 1) &&
30  strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE &&
31  extension_loaded('zlib') &&
32  $oModule->gzhandler_enable
33  )
34  {
35  $this->gz_enabled = TRUE;
36  }
37 
38  // Extract contents to display by the request method
39  if(Context::get('xeVirtualRequestMethod') == 'xml')
40  {
42  }
43  else if(Context::getRequestMethod() == 'XMLRPC')
44  {
46  if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
47  {
48  $this->gz_enabled = FALSE;
49  }
50  }
51  else if(Context::getRequestMethod() == 'JSON')
52  {
54  }
55  else if(Context::getRequestMethod() == 'JS_CALLBACK')
56  {
58  }
59  else
60  {
62  }
63 
64  $output = $handler->toDoc($oModule);
65 
66  // call a trigger before display
67  ModuleHandler::triggerCall('display', 'before', $output);
68 
69  // execute add-on
70  $called_position = 'before_display_content';
71  $oAddonController = getController('addon');
72  $addon_file = $oAddonController->getCacheFilePath(Mobile::isFromMobilePhone() ? "mobile" : "pc");
73  if(file_exists($addon_file)) include($addon_file);
74 
75  if(method_exists($handler, "prepareToPrint"))
76  {
77  $handler->prepareToPrint($output);
78  }
79 
80  // header output
81 
82  $httpStatusCode = $oModule->getHttpStatusCode();
83  if($httpStatusCode && $httpStatusCode != 200)
84  {
85  $this->_printHttpStatusCode($httpStatusCode);
86  }
87  else
88  {
89  if(Context::getResponseMethod() == 'JSON' || Context::getResponseMethod() == 'JS_CALLBACK')
90  {
91  $this->_printJSONHeader();
92  }
93  else if(Context::getResponseMethod() != 'HTML')
94  {
95  $this->_printXMLHeader();
96  }
97  else
98  {
99  $this->_printHTMLHeader();
100  }
101  }
102 
103  // debugOutput output
104  $this->content_size = strlen($output);
105  $output .= $this->_debugOutput();
106 
107  // disable gzip if output already exists
108  ob_flush();
109  if(headers_sent())
110  {
111  $this->gz_enabled = FALSE;
112  }
113 
114  // enable gzip using zlib extension
115  if($this->gz_enabled)
116  {
117  ini_set('zlib.output_compression', true);
118  }
119  // results directly output
120  print $output;
121 
122  // call a trigger after display
123  ModuleHandler::triggerCall('display', 'after', $output);
124 
125  flushSlowlog();
126  }
127 
134  function _debugOutput()
135  {
136  if(!__DEBUG__)
137  {
138  return;
139  }
140 
141  $end = getMicroTime();
142 
143  // Firebug console output
144  if(__DEBUG_OUTPUT__ == 2 && version_compare(PHP_VERSION, '6.0.0') === -1)
145  {
146  static $firephp;
147  if(!isset($firephp))
148  {
149  $firephp = FirePHP::getInstance(true);
150  }
151 
152  if(__DEBUG_PROTECT__ == 1 && __DEBUG_PROTECT_IP__ != $_SERVER['REMOTE_ADDR'])
153  {
154  $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.');
155  return;
156  }
157  // display total execution time and Request/Response info
158  if(__DEBUG__ & 2)
159  {
160  $firephp->fb(
161  array(
162  'Request / Response info >>> ' . $_SERVER['REQUEST_METHOD'] . ' / ' . Context::getResponseMethod(),
163  array(
164  array('Request URI', 'Request method', 'Response method', 'Response contents size', 'Memory peak usage'),
165  array(
166  sprintf("%s:%s%s%s%s", $_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'], $_SERVER['PHP_SELF'], $_SERVER['QUERY_STRING'] ? '?' : '', $_SERVER['QUERY_STRING']),
167  $_SERVER['REQUEST_METHOD'],
169  $this->content_size . ' byte',
170  FileHandler::filesize(memory_get_peak_usage())
171  )
172  )
173  ),
174  'TABLE'
175  );
176  $firephp->fb(
177  array(
178  'Elapsed time >>> Total : ' . sprintf('%0.5f sec', $end - __StartTime__),
179  array(array('DB queries', 'class file load', 'Template compile', 'XmlParse compile', 'PHP', 'Widgets', 'Trans Content'),
180  array(
181  sprintf('%0.5f sec', $GLOBALS['__db_elapsed_time__']),
182  sprintf('%0.5f sec', $GLOBALS['__elapsed_class_load__']),
183  sprintf('%0.5f sec (%d called)', $GLOBALS['__template_elapsed__'], $GLOBALS['__TemplateHandlerCalled__']),
184  sprintf('%0.5f sec', $GLOBALS['__xmlparse_elapsed__']),
185  sprintf('%0.5f sec', $end - __StartTime__ - $GLOBALS['__template_elapsed__'] - $GLOBALS['__xmlparse_elapsed__'] - $GLOBALS['__db_elapsed_time__'] - $GLOBALS['__elapsed_class_load__']),
186  sprintf('%0.5f sec', $GLOBALS['__widget_excute_elapsed__']),
187  sprintf('%0.5f sec', $GLOBALS['__trans_content_elapsed__'])
188  )
189  )
190  ),
191  'TABLE'
192  );
193  }
194 
195  // display DB query history
196  if((__DEBUG__ & 4) && $GLOBALS['__db_queries__'])
197  {
198  $queries_output = array(array('Result/'.PHP_EOL.'Elapsed time', 'Query ID', 'Query'));
199  foreach($GLOBALS['__db_queries__'] as $query)
200  {
201  $queries_output[] = array($query['result'] . PHP_EOL . sprintf('%0.5f', $query['elapsed_time']), str_replace(_XE_PATH_, '', $query['called_file']) . PHP_EOL . $query['called_method'] . '()' . PHP_EOL . $query['query_id'], $query['query']);
202  }
203  $firephp->fb(
204  array(
205  'DB Queries >>> ' . count($GLOBALS['__db_queries__']) . ' Queries, ' . sprintf('%0.5f sec', $GLOBALS['__db_elapsed_time__']),
206  $queries_output
207  ),
208  'TABLE'
209  );
210  }
211  // dislpay the file and HTML comments
212  }
213  else
214  {
215 
216  $buff = array();
217  // display total execution time and Request/Response info
218  if(__DEBUG__ & 2)
219  {
220  if(__DEBUG_PROTECT__ == 1 && __DEBUG_PROTECT_IP__ != $_SERVER['REMOTE_ADDR'])
221  {
222  return;
223  }
224 
225  // Request/Response information
226  $buff[] = "\n- Request/ Response info";
227  $buff[] = sprintf("\tRequest URI \t\t\t: %s:%s%s%s%s", $_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'], $_SERVER['PHP_SELF'], $_SERVER['QUERY_STRING'] ? '?' : '', $_SERVER['QUERY_STRING']);
228  $buff[] = sprintf("\tRequest method \t\t\t: %s", $_SERVER['REQUEST_METHOD']);
229  $buff[] = sprintf("\tResponse method \t\t: %s", Context::getResponseMethod());
230  $buff[] = sprintf("\tResponse contents size\t: %d byte", $this->content_size);
231 
232  // total execution time
233  $buff[] = sprintf("\n- Total elapsed time : %0.5f sec", $end - __StartTime__);
234 
235  $buff[] = sprintf("\tclass file load elapsed time \t: %0.5f sec", $GLOBALS['__elapsed_class_load__']);
236  $buff[] = sprintf("\tTemplate compile elapsed time\t: %0.5f sec (%d called)", $GLOBALS['__template_elapsed__'], $GLOBALS['__TemplateHandlerCalled__']);
237  $buff[] = sprintf("\tXmlParse compile elapsed time\t: %0.5f sec", $GLOBALS['__xmlparse_elapsed__']);
238  $buff[] = sprintf("\tPHP elapsed time \t\t\t\t: %0.5f sec", $end - __StartTime__ - $GLOBALS['__template_elapsed__'] - $GLOBALS['__xmlparse_elapsed__'] - $GLOBALS['__db_elapsed_time__'] - $GLOBALS['__elapsed_class_load__']);
239  $buff[] = sprintf("\tDB class elapsed time \t\t\t: %0.5f sec", $GLOBALS['__dbclass_elapsed_time__'] - $GLOBALS['__db_elapsed_time__']);
240 
241  // widget execution time
242  $buff[] = sprintf("\tWidgets elapsed time \t\t\t: %0.5f sec", $GLOBALS['__widget_excute_elapsed__']);
243 
244  // layout execution time
245  $buff[] = sprintf("\tLayout compile elapsed time \t: %0.5f sec", $GLOBALS['__layout_compile_elapsed__']);
246 
247  // Widgets, the editor component replacement time
248  $buff[] = sprintf("\tTrans Content \t\t\t\t\t: %0.5f sec", $GLOBALS['__trans_content_elapsed__']);
249  }
250  // DB Logging
251  if(__DEBUG__ & 4)
252  {
253  if(__DEBUG_PROTECT__ == 1 && __DEBUG_PROTECT_IP__ != $_SERVER['REMOTE_ADDR'])
254  {
255  return;
256  }
257 
258  if($GLOBALS['__db_queries__'])
259  {
260  $buff[] = sprintf("\n- DB Queries : %d Queries. %0.5f sec", count($GLOBALS['__db_queries__']), $GLOBALS['__db_elapsed_time__']);
261  $num = 0;
262 
263  foreach($GLOBALS['__db_queries__'] as $query)
264  {
265  if($query['result'] == 'Success')
266  {
267  $query_result = "Query Success";
268  }
269  else
270  {
271  $query_result = sprintf("Query $s : %d\n\t\t\t %s", $query['result'], $query['errno'], $query['errstr']);
272  }
273  $buff[] = sprintf("\t%02d. %s\n\t\t%0.6f sec. %s.", ++$num, $query['query'], $query['elapsed_time'], $query_result);
274  $buff[] = sprintf("\t\tConnection: %s.", $query['connection']);
275  $buff[] = sprintf("\t\tQuery ID: %s", $query['query_id']);
276  $buff[] = sprintf("\t\tCalled: %s. %s()", str_replace(_XE_PATH_, '', $query['called_file']), $query['called_method']);
277  }
278  }
279  }
280 
281  // Output in HTML comments
282  if($buff && __DEBUG_OUTPUT__ == 1 && Context::getResponseMethod() == 'HTML')
283  {
284  $buff = implode("\r\n", $buff);
285  $buff = sprintf("[%s %s:%d]\r\n%s", date('Y-m-d H:i:s'), $file_name, $line_num, print_r($buff, true));
286 
287  if(__DEBUG_PROTECT__ == 1 && __DEBUG_PROTECT_IP__ != $_SERVER['REMOTE_ADDR'])
288  {
289  $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';
290  }
291 
292  return "<!--\r\n" . $buff . "\r\n-->";
293  }
294 
295  // Output to a file
296  if($buff && __DEBUG_OUTPUT__ == 0)
297  {
298  $debug_file = _XE_PATH_ . 'files/_debug_message.php';
299  $buff = implode(PHP_EOL, $buff);
300  $buff = sprintf("[%s]\n%s", date('Y-m-d H:i:s'), print_r($buff, true));
301 
302  $buff = str_repeat('=', 80) . "\n" . $buff . "\n" . str_repeat('-', 80);
303  $buff = "\n<?php\n/*" . $buff . "*/\n?>\n";
304 
305  if (!@file_put_contents($debug_file, $buff, FILE_APPEND|LOCK_EX))
306  {
307  return;
308  }
309  }
310  }
311  }
312 
317  function _printXMLHeader()
318  {
319  header("Content-Type: text/xml; charset=UTF-8");
320  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
321  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
322  header("Cache-Control: no-store, no-cache, must-revalidate");
323  header("Cache-Control: post-check=0, pre-check=0", false);
324  header("Pragma: no-cache");
325  }
326 
331  function _printHTMLHeader()
332  {
333  header("Content-Type: text/html; charset=UTF-8");
334  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
335  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
336  header("Cache-Control: no-store, no-cache, must-revalidate");
337  header("Cache-Control: post-check=0, pre-check=0", false);
338  header("Pragma: no-cache");
339  }
340 
345  function _printJSONHeader()
346  {
347  header("Content-Type: text/html; charset=UTF-8");
348  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
349  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
350  header("Cache-Control: no-store, no-cache, must-revalidate");
351  header("Cache-Control: post-check=0, pre-check=0", false);
352  header("Pragma: no-cache");
353  }
354 
359  function _printHttpStatusCode($code)
360  {
361  $statusMessage = Context::get('http_status_message');
362  header("HTTP/1.0 $code $statusMessage");
363  }
364 
365 }
366 /* End of file DisplayHandler.class.php */
367 /* Location: ./classes/display/DisplayHandler.class.php */
getController($module_name)
Definition: func.inc.php:90
getMicroTime()
Definition: func.inc.php:986
if(file_exists(_XE_PATH_. 'config/config.user.inc.php')) if(!defined('__DEBUG__')) if(!defined('__DEBUG_OUTPUT__')) if(!defined('__DEBUG_PROTECT__')) if(!defined('__DEBUG_PROTECT_IP__')) if(!defined('__DEBUG_DB_OUTPUT__')) if(!defined('__LOG_SLOW_QUERY__')) if(!defined('__LOG_SLOW_TRIGGER__')) if(!defined('__LOG_SLOW_ADDON__')) if(!defined('__LOG_SLOW_WIDGET__')) if(!defined('__DEBUG_QUERY__')) if(!defined('__OB_GZHANDLER_ENABLE__')) if(!defined('__ENABLE_PHPUNIT_TEST__')) if(!defined('__PROXY_SERVER__')) if(!defined('__ERROR_LOG__')) if(!defined('__DISABLE_DEFAULT_CSS__')) if(!defined('__AUTO_OPCACHE_INVALIDATE__')) if((__DEBUG_OUTPUT__==2)&&version_compare(PHP_VERSION, '6.0.0')===-1) if(version_compare(PHP_VERSION, '5.3.0') >=0) $GLOBALS['__xe_autoload_file_map']
Definition: config.inc.php:324
$output
Definition: ko.install.php:193
flushSlowlog()
Definition: func.inc.php:973
isFromMobilePhone()
const _XE_PATH_
Definition: config.inc.php:49
triggerCall($trigger_name, $called_position, &$obj)