1
0

BxProfilerTemplate.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Profiler Profiler
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxProfilerTemplate extends BxDolModuleTemplate
  12. {
  13. protected $_isAjaxOutput = false;
  14. function __construct(&$oConfig)
  15. {
  16. $oDb = null;
  17. parent::__construct($oConfig, $oDb);
  18. $this->_isAjaxOutput = $this->_isAjaxRequest();
  19. }
  20. function plank($sTitle, $sContent = '')
  21. {
  22. static $i = 0;
  23. $i++;
  24. if ($this->_isAjaxOutput) {
  25. if (!headers_sent() && function_exists('fb')) {
  26. if ($sContent && is_array($sContent))
  27. fb($sContent, $sTitle, FirePHP::TABLE);
  28. else
  29. fb($sTitle . $sContent);
  30. }
  31. elseif (!headers_sent()) {
  32. if ($sContent && is_array($sContent)) {
  33. $sContentEnc = json_encode($sContent);
  34. header("X-Una-Profiler-$i: $sTitle" . ($sContent && $sContentEnc < 4000 ? '; ' . $sContentEnc : ''));
  35. } else {
  36. header("X-Una-Profiler-$i: $sTitle" . ($sContent && $sContent < 4000 ? '; ' . $sContent : ''));
  37. }
  38. }
  39. return '';
  40. }
  41. if ($sContent)
  42. $sContent = '<div class="bx_profiler_switch" onclick="bx_profiler_switch(this)">+</div><div class="bx_profiler_content">'.$sContent.'</div>';
  43. return '<div class="bx_profiler_plank"><span class="bx_profiler_plank_title">' . $sTitle . '</span>' . $sContent . '</div>';
  44. }
  45. function nameValue ($sName, $sVal)
  46. {
  47. return $this->_isAjaxOutput ? "{$sName}{$sVal} | " : "<u>$sName</u><b>$sVal</b>";
  48. }
  49. function table ($a, $sHighlight = '')
  50. {
  51. if ($this->_isAjaxOutput) {
  52. $table = array();
  53. foreach ($a as $r) {
  54. if (!$table)
  55. $table[] = array_keys($r);
  56. $rr = array_values($r);
  57. if (false !== strpos($rr[0], '&#160;'))
  58. $rr[0] = str_replace('&#160;', '-', $rr[0]);
  59. $table[] = $rr;
  60. }
  61. return $table;
  62. }
  63. $sId = md5(time() . rand());
  64. $s = '<table id="'.$sId.'" class="bx_profiler_table">';
  65. $th = '';
  66. foreach ($a as $r) {
  67. if (!$th) {
  68. foreach ($r as $k => $v)
  69. $th .= "<th>$k</th>";
  70. $s .= "<thead><tr>$th</tr></thead><tbody>";
  71. }
  72. $s .= '<tr>';
  73. foreach ($r as $k => $v) {
  74. $sClass = '';
  75. if ($sHighlight && $k == $sHighlight)
  76. $sClass = ' class="highlight" ';
  77. $s .= "<td $sClass>".htmlspecialchars_adv($v)."</td>";
  78. }
  79. $s .= '</tr>';
  80. }
  81. $s .= '</tbody></table>';
  82. $s .= '<script language="javascript">$(document).ready(function(){ $(\'#'.$sId.'\').tablesorter(); });</script>';
  83. return $s;
  84. }
  85. function _isAjaxRequest ()
  86. {
  87. if ('application/json' == $this->getHeaderContentType())
  88. return true;
  89. if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')
  90. return true;
  91. if (isset($_GET['bx_profiler_ajax_request']))
  92. return true;
  93. if (preg_match('/vote\.php/', bx_html_attribute($_SERVER['PHP_SELF'])))
  94. return true;
  95. if (preg_match('/image_transcoder\.php/', bx_html_attribute($_SERVER['PHP_SELF'])))
  96. return true;
  97. if (preg_match('/storage\.php/', bx_html_attribute($_SERVER['PHP_SELF'])))
  98. return true;
  99. if (preg_match('/storage_uploader\.php/', bx_html_attribute($_SERVER['PHP_SELF'])))
  100. return true;
  101. if (preg_match('/searchKeywordContent\.php/', bx_html_attribute($_SERVER['PHP_SELF'])))
  102. return true;
  103. if (preg_match('/menu\.php/', bx_html_attribute($_SERVER['PHP_SELF'])))
  104. return true;
  105. if (preg_match('/gzip_loader\.php/', $_SERVER['PHP_SELF']))
  106. return true;
  107. if (preg_match('/get_rss_feed\.php/', $_SERVER['PHP_SELF']))
  108. return true;
  109. return false;
  110. }
  111. function addCss($mixedFiles, $bDynamic = false)
  112. {
  113. if ($this->_isAjaxOutput)
  114. return '';
  115. return parent::addCss($mixedFiles, $bDynamic);
  116. }
  117. function addJs($mixedFiles, $bDynamic = false)
  118. {
  119. if ($this->_isAjaxOutput)
  120. return '';
  121. return parent::addJs($mixedFiles, $bDynamic);
  122. }
  123. function getHeaderContentType()
  124. {
  125. $aHeaders = headers_list();
  126. foreach ($aHeaders as $s) {
  127. $a = explode(':', $s);
  128. if (isset($a[0]) && isset($a[1]) && 'content-type' == strtolower(trim($a[0]))) {
  129. $aa = explode(';', $a[1]);
  130. return strtolower(trim($aa[0]));
  131. }
  132. }
  133. return false;
  134. }
  135. }
  136. /** @} */