PageRenderTime 29ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/system/debug/debug.php

https://github.com/ot2sen/Molajo
PHP | 334 lines | 232 code | 62 blank | 40 comment | 39 complexity | aa403f05607b6fb2f008976dd59489cf MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: debug.php 21097 2011-04-07 15:38:03Z dextercowley $
  4. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. // no direct access
  8. defined('JPATH_PLATFORM') or die;
  9. jimport('joomla.plugin.plugin');
  10. /**
  11. * Molajo Debug plugin
  12. *
  13. * @package Joomla.Plugin
  14. * @subpackage System.debug
  15. */
  16. class plgSystemDebug extends JPlugin
  17. {
  18. /**
  19. * Constructor
  20. *
  21. * @access protected
  22. * @param object $subject The object to observe
  23. * @param array $config An array that holds the plugin configuration
  24. * @since 1.5
  25. */
  26. function __construct(&$subject, $config)
  27. {
  28. parent::__construct($subject, $config);
  29. // Only if debugging is enabled
  30. if (JDEBUG) {
  31. $config = MolajoFactory::getConfig();
  32. $config->set('gzip', 0);
  33. ob_start();
  34. ob_implicit_flush(false);
  35. }
  36. }
  37. /**
  38. * Show the debug info
  39. *
  40. */
  41. function __destruct()
  42. {
  43. global $_PROFILER;
  44. // Do not render if debugging is not enabled
  45. if (!JDEBUG) {
  46. return;
  47. }
  48. if (!$_PROFILER instanceof JProfiler) {
  49. return;
  50. }
  51. // Load the language
  52. $this->loadLanguage();
  53. // Capture output
  54. $contents = ob_get_contents();
  55. ob_end_clean();
  56. // No debug for Safari and Chrome redirection
  57. if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'webkit') !== false && substr($contents, 0, 50) == '<html><head><meta http-equiv="refresh" content="0;') {
  58. echo $contents;
  59. return;
  60. }
  61. $document = MolajoFactory::getDocument();
  62. $doctype = $document->getType();
  63. // Only render for HTML output
  64. if ($doctype !== 'html') {
  65. echo $contents;
  66. return;
  67. }
  68. // If the user is not allowed to view the output then end here
  69. $filterGroups = (array) $this->params->get('filter_groups', null);
  70. if (!empty($filterGroups)) {
  71. $userGroups = MolajoFactory::getUser()->get('groups');
  72. if (!array_intersect($filterGroups, array_keys($userGroups))) {
  73. echo $contents;
  74. return;
  75. }
  76. }
  77. // Load language file
  78. $this->loadLanguage('plg_system_debug');
  79. $profiler = &$_PROFILER;
  80. ob_start();
  81. echo '<div id="system-debug" class="profiler">';
  82. $errors = JError::getErrors();
  83. if (!empty($errors)) {
  84. echo '<h4>'.JText::_('PLG_DEBUG_ERRORS').'</h4><ol>';
  85. while($error = JError::getError(true)) {
  86. echo '<li>'.$error->getMessage().'<br /><h4>'.JText::_('PLG_DEBUG_INFO').'</h4><pre>'.print_r($error->get('info'), true).'</pre><br /><h4>'.JText::_('PLG_DEBUG_BACKTRACE').'</h4>'.JError::renderBacktrace($error).'</li>';
  87. }
  88. echo '</ol>';
  89. }
  90. if ($this->params->get('profile', 1)) {
  91. echo '<h4>'.JText::_('PLG_DEBUG_PROFILE_INFORMATION').'</h4>';
  92. foreach ($profiler->getBuffer() as $mark) {
  93. echo '<div>'.$mark.'</div>';
  94. }
  95. }
  96. if ($this->params->get('memory', 1)) {
  97. echo '<h4>'.JText::_('PLG_DEBUG_MEMORY_USAGE').'</h4>';
  98. $bytes = $profiler->getMemory();
  99. echo JHtml::_('number.bytes', $bytes);
  100. echo ' ('.number_format($bytes).' Bytes)';
  101. }
  102. if ($this->params->get('queries', 1)) {
  103. $newlineKeywords = '#\b(FROM|LEFT|INNER|OUTER|WHERE|SET|VALUES|ORDER|GROUP|HAVING|LIMIT|ON|AND)\b#i';
  104. $db = MolajoFactory::getDbo();
  105. echo '<h4>'.JText::sprintf('PLG_DEBUG_QUERIES_LOGGED', $db->getTicker()).'</h4>';
  106. if ($log = $db->getLog()) {
  107. echo '<ol>';
  108. $selectQueryTypeTicker = array();
  109. $otherQueryTypeTicker = array();
  110. foreach ($log as $k => $sql) {
  111. // Start Query Type Ticker Additions
  112. $fromStart = stripos($sql, 'from');
  113. $whereStart = stripos($sql, 'where', $fromStart);
  114. if ($whereStart === false) {
  115. $whereStart = stripos($sql, 'order by', $fromStart);
  116. }
  117. if ($whereStart === false) {
  118. $whereStart = strlen($sql) - 1;
  119. }
  120. $fromString = substr($sql, 0, $whereStart);
  121. $fromString = str_replace("\t", " ", $fromString);
  122. $fromString = str_replace("\n", " ", $fromString);
  123. $fromString = trim($fromString);
  124. // Initialize the select/other query type counts the first time:
  125. if (!isset($selectQueryTypeTicker[$fromString])) {
  126. $selectQueryTypeTicker[$fromString] = 0;
  127. }
  128. if (!isset($otherQueryTypeTicker[$fromString])) {
  129. $otherQueryTypeTicker[$fromString] = 0;
  130. }
  131. // Increment the count:
  132. if (stripos($sql, 'select') === 0) {
  133. $selectQueryTypeTicker[$fromString] = $selectQueryTypeTicker[$fromString] + 1;
  134. unset($otherQueryTypeTicker[$fromString]);
  135. }
  136. else {
  137. $otherQueryTypeTicker[$fromString] = $otherQueryTypeTicker[$fromString] + 1;
  138. unset($selectQueryTypeTicker[$fromString]);
  139. }
  140. // Finish Query Type Ticker Additions
  141. $text = htmlspecialchars($sql, ENT_QUOTES);
  142. $text = preg_replace($newlineKeywords, '<br />&#160;&#160;\\0', $text);
  143. echo '<li>'.$text.'</li>';
  144. }
  145. echo '</ol>';
  146. if ($this->params->get('query_types', 1)) {
  147. // Get the totals for the query types:
  148. $totalSelectQueryTypes = count($selectQueryTypeTicker);
  149. $totalOtherQueryTypes = count($otherQueryTypeTicker);
  150. $totalQueryTypes = $totalSelectQueryTypes + $totalOtherQueryTypes;
  151. echo '<h4>'.JText::sprintf('PLG_DEBUG_QUERY_TYPES_LOGGED', $totalQueryTypes) . '</h4>';
  152. if ($totalSelectQueryTypes) {
  153. echo '<h5>'.JText::sprintf('PLG_DEBUG_SELECT_QUERIES').'</h5>';
  154. arsort($selectQueryTypeTicker);
  155. echo '<ol>';
  156. foreach($selectQueryTypeTicker as $table => $occurrences)
  157. {
  158. echo '<li>'.JText::sprintf('PLG_DEBUG_QUERY_TYPE_AND_OCCURRENCES', $table, $occurrences).'</li>';
  159. }
  160. echo '</ol>';
  161. }
  162. if ($totalOtherQueryTypes) {
  163. echo '<h5>'.JText::sprintf('PLG_DEBUG_OTHER_QUERIES').'</h5>';
  164. arsort($otherQueryTypeTicker);
  165. echo '<ol>';
  166. foreach($otherQueryTypeTicker as $table => $occurrences)
  167. {
  168. echo '<li>'.JText::sprintf('PLG_DEBUG_QUERY_TYPE_AND_OCCURRENCES', $table, $occurrences).'</li>';
  169. }
  170. echo '</ol>';
  171. }
  172. }
  173. }
  174. }
  175. // Show language debug only if enabled
  176. if (MolajoFactory::getApplication()->getCfg('debug_lang')) {
  177. $lang = MolajoFactory::getLanguage();
  178. if ($this->params->get('language_errorfiles', 1)) {
  179. echo '<h4>'.JText::_('PLG_DEBUG_LANGUAGE_FILES_IN_ERROR').'</h4>';
  180. $errorfiles = $lang->getErrorFiles();
  181. if (count($errorfiles)) {
  182. echo '<ul>';
  183. foreach ($errorfiles as $file => $error)
  184. {
  185. echo "<li>$error</li>";
  186. }
  187. echo '</ul>';
  188. }
  189. else {
  190. echo '<pre>'.JText::_('JNONE').'</pre>';
  191. }
  192. }
  193. if ($this->params->get('language_files', 1)) {
  194. echo '<h4>'.JText::_('PLG_DEBUG_LANGUAGE_FILES_LOADED').'</h4>';
  195. echo '<ul>';
  196. $extensions = $lang->getPaths();
  197. foreach ($extensions as $extension => $files)
  198. {
  199. foreach ($files as $file => $status)
  200. {
  201. echo "<li>$file $status</li>";
  202. }
  203. }
  204. echo '</ul>';
  205. }
  206. if ($this->params->get('language_strings')) {
  207. $stripFirst = $this->params->get('strip-first');
  208. $stripPref = $this->params->get('strip-prefix');
  209. $stripSuff = $this->params->get('strip-suffix');
  210. echo '<h4>'.JText::_('PLG_DEBUG_UNTRANSLATED_STRINGS').'</h4>';
  211. echo '<pre>';
  212. $orphans = $lang->getOrphans();
  213. if (count($orphans)) {
  214. ksort($orphans, SORT_STRING);
  215. $guesses = array();
  216. foreach ($orphans as $key => $occurance)
  217. {
  218. if (is_array($occurance) AND isset($occurance[0])) {
  219. $info = &$occurance[0];
  220. $file = @$info['file'];
  221. if (!isset($guesses[$file])) {
  222. $guesses[$file] = array();
  223. }
  224. // Prepare the key
  225. if (($pos = strpos($info['string'], '=')) > 0) {
  226. $parts = explode('=', $info['string']);
  227. $key = $parts[0];
  228. $guess = $parts[1];
  229. }
  230. else {
  231. $guess = str_replace('_', ' ', $info['string']);
  232. if ($stripFirst) {
  233. $parts = explode(' ', $guess);
  234. if (count($parts) > 1) {
  235. array_shift($parts);
  236. $guess = implode(' ', $parts);
  237. }
  238. }
  239. $guess = trim($guess);
  240. if ($stripPref) {
  241. $guess = trim(preg_replace(chr(1).'^'.$stripPref.chr(1).'i', '', $guess));
  242. }
  243. if ($stripSuff) {
  244. $guess = trim(preg_replace(chr(1).$stripSuff.'$'.chr(1).'i', '', $guess));
  245. }
  246. }
  247. $key = trim(strtoupper($key));
  248. $key = preg_replace('#\s+#', '_', $key);
  249. $key = preg_replace('#\W#', '', $key);
  250. // Prepare the text
  251. $guesses[$file][] = $key.'="'.$guess.'"';
  252. }
  253. }
  254. foreach ($guesses as $file => $keys)
  255. {
  256. echo "\n\n# ".($file ? $file : JText::_('PLG_DEBUG_UNKNOWN_FILE'))."\n\n";
  257. echo implode("\n", $keys);
  258. }
  259. }
  260. else {
  261. echo JText::_('JNONE');
  262. }
  263. echo '</pre>';
  264. }
  265. }
  266. echo '</div>';
  267. $debug = ob_get_clean();
  268. $body = JResponse::getBody();
  269. $body = str_replace('</body>', $debug.'</body>', $body);
  270. echo str_replace('</body>', $debug.'</body>', $contents);
  271. }
  272. }