| 1 | <?php␊ |
| 2 | /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */␊ |
| 3 | /*␊ |
| 4 | # ***** BEGIN LICENSE BLOCK *****␊ |
| 5 | # This file is part of Plume Framework, a simple PHP Application Framework.␊ |
| 6 | # Copyright (C) 2001-2007 Loic d'Anterroches and contributors.␊ |
| 7 | #␊ |
| 8 | # Plume Framework is free software; you can redistribute it and/or modify␊ |
| 9 | # it under the terms of the GNU Lesser General Public License as published by␊ |
| 10 | # the Free Software Foundation; either version 2.1 of the License, or␊ |
| 11 | # (at your option) any later version.␊ |
| 12 | #␊ |
| 13 | # Plume Framework is distributed in the hope that it will be useful,␊ |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of␊ |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the␊ |
| 16 | # GNU Lesser General Public License for more details.␊ |
| 17 | #␊ |
| 18 | # You should have received a copy of the GNU Lesser General Public License␊ |
| 19 | # along with this program; if not, write to the Free Software␊ |
| 20 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA␊ |
| 21 | #␊ |
| 22 | # ***** END LICENSE BLOCK ***** */␊ |
| 23 | ␊ |
| 24 | /**␊ |
| 25 | * Debug middleware.␊ |
| 26 | *␊ |
| 27 | * Simply display small debug information at the end of the page. It␊ |
| 28 | * requires the xdebug extension.␊ |
| 29 | */␊ |
| 30 | class Pluf_Middleware_Debug␊ |
| 31 | {␊ |
| 32 | /**␊ |
| 33 | * Process the response of a view.␊ |
| 34 | *␊ |
| 35 | * If the status code and content type are allowed, add the debug␊ |
| 36 | * information. Debug must be set to true in the config file to␊ |
| 37 | * active it.␊ |
| 38 | *␊ |
| 39 | * @param Pluf_HTTP_Request The request␊ |
| 40 | * @param Pluf_HTTP_Response The response␊ |
| 41 | * @return Pluf_HTTP_Response The response␊ |
| 42 | */␊ |
| 43 | function process_response($request, $response)␊ |
| 44 | {␊ |
| 45 | if (!Pluf::f('debug', false)) {␊ |
| 46 | return $response;␊ |
| 47 | }␊ |
| 48 | if (!in_array($response->status_code, ␊ |
| 49 | array(200, 201, 202, 203, 204, 205, 206, 404, 501))) {␊ |
| 50 | return $response;␊ |
| 51 | }␊ |
| 52 | $ok = false;␊ |
| 53 | $cts = array('text/html', 'text/html', 'application/xhtml+xml');␊ |
| 54 | foreach ($cts as $ct) {␊ |
| 55 | if (false !== strripos($response->headers['Content-Type'], $ct)) {␊ |
| 56 | $ok = true;␊ |
| 57 | break;␊ |
| 58 | }␊ |
| 59 | }␊ |
| 60 | if ($ok == false) {␊ |
| 61 | return $response;␊ |
| 62 | }␊ |
| 63 | $js = '<script type="text/javascript">␊ |
| 64 | // <!--␊ |
| 65 | function pxDebugGetElementsByClassName(oElm, strTagName, strClassName){␊ |
| 66 | // Written by Jonathan Snook, http://www.snook.ca/jon; ␊ |
| 67 | // Add-ons by Robert Nyman, http://www.robertnyman.com␊ |
| 68 | var arrElements = (strTagName == "*" && document.all)? document.all :␊ |
| 69 | oElm.getElementsByTagName(strTagName);␊ |
| 70 | var arrReturnElements = new Array();␊ |
| 71 | strClassName = strClassName.replace(/\-/g, "\\-");␊ |
| 72 | var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");␊ |
| 73 | var oElement;␊ |
| 74 | for(var i=0; i<arrElements.length; i++){␊ |
| 75 | oElement = arrElements[i];␊ |
| 76 | if(oRegExp.test(oElement.className)){␊ |
| 77 | arrReturnElements.push(oElement);␊ |
| 78 | }␊ |
| 79 | }␊ |
| 80 | return (arrReturnElements)␊ |
| 81 | }␊ |
| 82 | function pxDebugHideAll(elems) {␊ |
| 83 | for (var e = 0; e < elems.length; e++) {␊ |
| 84 | elems[e].style.display = \'none\';␊ |
| 85 | }␊ |
| 86 | }␊ |
| 87 | function pxDebugToggle() {␊ |
| 88 | for (var i = 0; i < arguments.length; i++) {␊ |
| 89 | var e = document.getElementById(arguments[i]);␊ |
| 90 | if (e) {␊ |
| 91 | e.style.display = e.style.display == \'none\' ? \'block\' : \'none\';␊ |
| 92 | }␊ |
| 93 | }␊ |
| 94 | return false;␊ |
| 95 | } ␊ |
| 96 | // -->␊ |
| 97 | </script>';␊ |
| 98 | $text = '<pre style="text-align: left;">';␊ |
| 99 | $text .= 'Peak mem: '.(int)(memory_get_peak_usage()/1024).'kB'."\n";␊ |
| 100 | $text .= 'Exec time: '.sprintf('%.5f', (microtime(true) - $GLOBALS['_PX_starttime'])).'s'."\n";␊ |
| 101 | $included_files = get_included_files();␊ |
| 102 | sort($included_files);␊ |
| 103 | $text .= '<a href=\'#\' onclick="return pxDebugToggle(\'debug-included-files\')">';␊ |
| 104 | $text .= 'Included files #: '.count($included_files);␊ |
| 105 | $text .= '</a></pre>'."\n";␊ |
| 106 | $text .= $js.'<div id="debug-included-files" class="debug-queries"><pre style="text-align: left;">';␊ |
| 107 | foreach ($included_files as $filename) {␊ |
| 108 | $text .= htmlspecialchars($filename)."\n";␊ |
| 109 | }␊ |
| 110 | $text .= '</pre></div>';␊ |
| 111 | if (isset($GLOBALS['_PX_debug_data']['sql_queries'])) {␊ |
| 112 | $text .= '<pre style="text-align: left;">'; ␊ |
| 113 | $text .= '<a href=\'#\' onclick="return pxDebugToggle(\'debug-queries\')">';␊ |
| 114 | $text .= 'DB query #: '.count($GLOBALS['_PX_debug_data']['sql_queries']);␊ |
| 115 | $text .= '</a>'."\n\n";␊ |
| 116 | $text .= '</pre>';␊ |
| 117 | $text .= '<div id="debug-queries" class="debug-queries"><pre style="text-align: left;">';␊ |
| 118 | foreach ($GLOBALS['_PX_debug_data']['sql_queries'] as $q) {␊ |
| 119 | $text .= htmlspecialchars($q)."\n";␊ |
| 120 | }␊ |
| 121 | $text .= '</pre></div>';␊ |
| 122 | } else {␊ |
| 123 | $text .= '</pre>';␊ |
| 124 | }␊ |
| 125 | $text .= '<script type="text/javascript">␊ |
| 126 | pxDebugHideAll(pxDebugGetElementsByClassName(document, \'div\', \'debug-queries\'));␊ |
| 127 | </script>';␊ |
| 128 | ␊ |
| 129 | $response->content = str_replace('</body>', $text.'</body>', $response->content);␊ |
| 130 | return $response;␊ |
| 131 | }␊ |
| 132 | }␊ |
| 133 | |