blob: 77af7b99b53eb6aea687ea4b742dd9ae7b18c558 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev195d3112011-12-25 03:59:13 +02008 *
Instructor, BCIT0e59db62019-01-01 08:34:36 -08009 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
Andrey Andreev195d3112011-12-25 03:59:13 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Instructor, BCIT0e59db62019-01-01 08:34:36 -080032 * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33 * @license https://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * CodeIgniter Profiler Class
42 *
43 * This class enables you to display benchmark, query, and other data
44 * in order to help with debugging and optimization.
45 *
46 * Note: At some point it would be good to move all the HTML in this class
47 * into a set of template files in order to allow customization.
48 *
49 * @package CodeIgniter
50 * @subpackage Libraries
51 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050052 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020053 * @link https://codeigniter.com/user_guide/general/profiling.html
Derek Allard2067d1a2008-11-13 22:59:24 +000054 */
55class CI_Profiler {
56
Timothy Warren0688ac92012-04-20 10:25:04 -040057 /**
58 * List of profiler sections available to show
59 *
60 * @var array
61 */
Greg Aker5ac55942010-11-10 14:59:47 -060062 protected $_available_sections = array(
Timothy Warren0688ac92012-04-20 10:25:04 -040063 'benchmarks',
64 'get',
65 'memory_usage',
66 'post',
67 'uri_string',
68 'controller_info',
69 'queries',
70 'http_headers',
71 'session_data',
72 'config'
73 );
Razicanc24f49b2011-04-25 13:43:57 +020074
Timothy Warren0688ac92012-04-20 10:25:04 -040075 /**
76 * Number of queries to show before making the additional queries togglable
77 *
78 * @var int
79 */
Greg Akere6e6e642011-04-18 15:54:13 -050080 protected $_query_toggle_count = 25;
Razicanc24f49b2011-04-25 13:43:57 +020081
Timothy Warren0688ac92012-04-20 10:25:04 -040082 /**
83 * Reference to the CodeIgniter singleton
84 *
85 * @var object
86 */
Razicanc24f49b2011-04-25 13:43:57 +020087 protected $CI;
Derek Jonesee71c802010-03-10 10:05:05 -060088
Andrey Andreev55a8c622012-11-06 13:31:21 +020089 // --------------------------------------------------------------------
90
Timothy Warren0688ac92012-04-20 10:25:04 -040091 /**
Andrey Andreev55a8c622012-11-06 13:31:21 +020092 * Class constructor
Timothy Warren0688ac92012-04-20 10:25:04 -040093 *
94 * Initialize Profiler
95 *
Andrey Andreev55a8c622012-11-06 13:31:21 +020096 * @param array $config Parameters
Timothy Warren0688ac92012-04-20 10:25:04 -040097 */
Greg Aker5ac55942010-11-10 14:59:47 -060098 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +020099 {
100 $this->CI =& get_instance();
101 $this->CI->load->language('profiler');
102
Derek Jonesee71c802010-03-10 10:05:05 -0600103 // default all sections to display
104 foreach ($this->_available_sections as $section)
105 {
106 if ( ! isset($config[$section]))
107 {
108 $this->_compile_{$section} = TRUE;
109 }
110 }
Barry Mienydd671972010-10-04 16:33:58 +0200111
Derek Jonesee71c802010-03-10 10:05:05 -0600112 $this->set_sections($config);
Andrey Andreev90726b82015-01-20 12:39:22 +0200113 log_message('info', 'Profiler Class Initialized');
Barry Mienydd671972010-10-04 16:33:58 +0200114 }
115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 // --------------------------------------------------------------------
117
118 /**
Derek Jonesee71c802010-03-10 10:05:05 -0600119 * Set Sections
120 *
121 * Sets the private _compile_* properties to enable/disable Profiler sections
122 *
Andrey Andreev55a8c622012-11-06 13:31:21 +0200123 * @param mixed $config
Derek Jonesee71c802010-03-10 10:05:05 -0600124 * @return void
125 */
Greg Aker5ac55942010-11-10 14:59:47 -0600126 public function set_sections($config)
Derek Jonesee71c802010-03-10 10:05:05 -0600127 {
Andrey Andreev0140ddd2012-06-16 01:12:56 +0300128 if (isset($config['query_toggle_count']))
129 {
130 $this->_query_toggle_count = (int) $config['query_toggle_count'];
131 unset($config['query_toggle_count']);
132 }
133
Derek Jonesee71c802010-03-10 10:05:05 -0600134 foreach ($config as $method => $enable)
135 {
136 if (in_array($method, $this->_available_sections))
137 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300138 $this->_compile_{$method} = ($enable !== FALSE);
Derek Jonesee71c802010-03-10 10:05:05 -0600139 }
140 }
141 }
142
143 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Jonesee71c802010-03-10 10:05:05 -0600145 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 * Auto Profiler
147 *
148 * This function cycles through the entire array of mark points and
149 * matches any two points that are named identically (ending in "_start"
Derek Jones4b9c6292011-07-01 17:40:48 -0500150 * and "_end" respectively). It then compiles the execution times for
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 * all points and returns it as an array
Greg Aker5ac55942010-11-10 14:59:47 -0600152 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 * @return array
154 */
Greg Aker5ac55942010-11-10 14:59:47 -0600155 protected function _compile_benchmarks()
Barry Mienydd671972010-10-04 16:33:58 +0200156 {
157 $profile = array();
158 foreach ($this->CI->benchmark->marker as $key => $val)
159 {
160 // We match the "end" marker so that the list ends
161 // up in the order that it was defined
Richard Cunningham70a86262014-03-19 13:11:14 -0400162 if (preg_match('/(.+?)_end$/i', $key, $match)
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300163 && isset($this->CI->benchmark->marker[$match[1].'_end'], $this->CI->benchmark->marker[$match[1].'_start']))
Barry Mienydd671972010-10-04 16:33:58 +0200164 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200165 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
Barry Mienydd671972010-10-04 16:33:58 +0200166 }
167 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000168
169 // Build a table containing the profile data.
170 // Note: At some point we should turn this into a template that can
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300171 // be modified. We also might want to make this data available to be logged
Barry Mienydd671972010-10-04 16:33:58 +0200172
Andrey Andreev195d3112011-12-25 03:59:13 +0200173 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300174 .'<fieldset id="ci_profiler_benchmarks" style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
175 ."\n"
176 .'<legend style="color:#900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks')."&nbsp;&nbsp;</legend>"
177 ."\n\n\n<table style=\"width:100%;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 foreach ($profile as $key => $val)
180 {
181 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300182 $output .= '<tr><td style="padding:5px;width:50%;color:#000;font-weight:bold;background-color:#ddd;">'
183 .$key.'&nbsp;&nbsp;</td><td style="padding:5px;width:50%;color:#900;font-weight:normal;background-color:#ddd;">'
184 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
Barry Mienydd671972010-10-04 16:33:58 +0200186
Andrey Andreev195d3112011-12-25 03:59:13 +0200187 return $output."</table>\n</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200188 }
189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 // --------------------------------------------------------------------
191
192 /**
193 * Compile Queries
194 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200196 */
Greg Aker5ac55942010-11-10 14:59:47 -0600197 protected function _compile_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
199 $dbs = array();
Derek Jonesf0a9b332009-07-29 14:19:18 +0000200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 // Let's determine which databases are currently connected to
Andrey Andreev55a8c622012-11-06 13:31:21 +0200202 foreach (get_object_vars($this->CI) as $name => $cobject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
Andrey Andreev55a8c622012-11-06 13:31:21 +0200204 if (is_object($cobject))
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
Andrey Andreev55a8c622012-11-06 13:31:21 +0200206 if ($cobject instanceof CI_DB)
207 {
208 $dbs[get_class($this->CI).':$'.$name] = $cobject;
209 }
210 elseif ($cobject instanceof CI_Model)
211 {
212 foreach (get_object_vars($cobject) as $mname => $mobject)
213 {
214 if ($mobject instanceof CI_DB)
215 {
216 $dbs[get_class($cobject).':$'.$mname] = $mobject;
217 }
218 }
219 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
Andrey Andreev195d3112011-12-25 03:59:13 +0200223 if (count($dbs) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200225 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300226 .'<fieldset id="ci_profiler_queries" style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
227 ."\n"
228 .'<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>'
229 ."\n\n\n<table style=\"border:none; width:100%;\">\n"
230 .'<tr><td style="width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;">'
231 .$this->CI->lang->line('profiler_no_db')
232 ."</td></tr>\n</table>\n</fieldset>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 // Load the text helper so we can highlight the SQL
236 $this->CI->load->helper('text');
237
238 // Key words we want bolded
Greg Aker3424bf72010-09-14 17:53:10 -0500239 $highlight = array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'AND', 'LEFT&nbsp;JOIN', 'ORDER&nbsp;BY', 'GROUP&nbsp;BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR&nbsp;', 'HAVING', 'OFFSET', 'NOT&nbsp;IN', 'IN', 'LIKE', 'NOT&nbsp;LIKE', 'COUNT', 'MAX', 'MIN', 'ON', 'AS', 'AVG', 'SUM', '(', ')');
Derek Allard2067d1a2008-11-13 22:59:24 +0000240
Derek Jones4b9c6292011-07-01 17:40:48 -0500241 $output = "\n\n";
Greg Akere6e6e642011-04-18 15:54:13 -0500242 $count = 0;
Razicanc24f49b2011-04-25 13:43:57 +0200243
Andrey Andreev55a8c622012-11-06 13:31:21 +0200244 foreach ($dbs as $name => $db)
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Greg Akere6e6e642011-04-18 15:54:13 -0500246 $hide_queries = (count($db->queries) > $this->_query_toggle_count) ? ' display:none' : '';
Tim3224f072013-02-14 09:26:24 +0200247 $total_time = number_format(array_sum($db->query_times), 4).' '.$this->CI->lang->line('profiler_seconds');
Razicanc24f49b2011-04-25 13:43:57 +0200248
Greg Akere6e6e642011-04-18 15:54:13 -0500249 $show_hide_js = '(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_queries_db_'.$count.'\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI->lang->line('profiler_section_hide').'\'?\''.$this->CI->lang->line('profiler_section_show').'\':\''.$this->CI->lang->line('profiler_section_hide').'\';">'.$this->CI->lang->line('profiler_section_hide').'</span>)';
Razicanc24f49b2011-04-25 13:43:57 +0200250
Alex Bilbied261b1e2012-06-02 11:12:16 +0100251 if ($hide_queries !== '')
Greg Akere6e6e642011-04-18 15:54:13 -0500252 {
253 $show_hide_js = '(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_queries_db_'.$count.'\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI->lang->line('profiler_section_show').'\'?\''.$this->CI->lang->line('profiler_section_hide').'\':\''.$this->CI->lang->line('profiler_section_show').'\';">'.$this->CI->lang->line('profiler_section_show').'</span>)';
254 }
Razicanc24f49b2011-04-25 13:43:57 +0200255
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300256 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
257 ."\n"
258 .'<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_database')
Andrey Andreev55a8c622012-11-06 13:31:21 +0200259 .':&nbsp; '.$db->database.' ('.$name.')&nbsp;&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries')
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200260 .': '.count($db->queries).' ('.$total_time.')&nbsp;&nbsp;'.$show_hide_js."</legend>\n\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300261 .'<table style="width:100%;'.$hide_queries.'" id="ci_profiler_queries_db_'.$count."\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200262
Andrey Andreev195d3112011-12-25 03:59:13 +0200263 if (count($db->queries) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300265 $output .= '<tr><td style="width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;">'
266 .$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 }
268 else
Barry Mienydd671972010-10-04 16:33:58 +0200269 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 foreach ($db->queries as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200271 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 $time = number_format($db->query_times[$key], 4);
Rasmus Lerdorf164a1f22013-05-18 10:29:56 -0400273 $val = highlight_code($val);
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 foreach ($highlight as $bold)
276 {
Barry Mienydd671972010-10-04 16:33:58 +0200277 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
Barry Mienydd671972010-10-04 16:33:58 +0200279
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300280 $output .= '<tr><td style="padding:5px;vertical-align:top;width:1%;color:#900;font-weight:normal;background-color:#ddd;">'
281 .$time.'&nbsp;&nbsp;</td><td style="padding:5px;color:#000;font-weight:normal;background-color:#ddd;">'
282 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
284 }
Barry Mienydd671972010-10-04 16:33:58 +0200285
Andrey Andreev195d3112011-12-25 03:59:13 +0200286 $output .= "</table>\n</fieldset>";
Cristian Riffo Huez0612e922013-12-13 22:09:44 -0300287 $count++;
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 return $output;
291 }
292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 // --------------------------------------------------------------------
294
295 /**
296 * Compile $_GET Data
297 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200299 */
Greg Aker5ac55942010-11-10 14:59:47 -0600300 protected function _compile_get()
Barry Mienydd671972010-10-04 16:33:58 +0200301 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300302 $output = "\n\n"
303 .'<fieldset id="ci_profiler_get" style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
304 ."\n"
305 .'<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data')."&nbsp;&nbsp;</legend>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200306
Andrey Andreev195d3112011-12-25 03:59:13 +0200307 if (count($_GET) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300309 $output .= '<div style="color:#cd6e00;font-weight:normal;padding:4px 0 4px 0;">'.$this->CI->lang->line('profiler_no_get').'</div>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
311 else
312 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300313 $output .= "\n\n<table style=\"width:100%;border:none;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 foreach ($_GET as $key => $val)
316 {
Andrey Andreeve6a5f792016-03-07 11:34:43 +0200317 is_int($key) OR $key = "'".htmlspecialchars($key, ENT_QUOTES, config_item('charset'))."'";
318 $val = (is_array($val) OR is_object($val))
Andrey Andreevcd8d5282017-01-23 11:42:47 +0200319 ? '<pre>'.htmlspecialchars(print_r($val, TRUE), ENT_QUOTES, config_item('charset')).'</pre>'
Andrey Andreeve6a5f792016-03-07 11:34:43 +0200320 : htmlspecialchars($val, ENT_QUOTES, config_item('charset'));
Barry Mienydd671972010-10-04 16:33:58 +0200321
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300322 $output .= '<tr><td style="width:50%;color:#000;background-color:#ddd;padding:5px;">&#36;_GET['
323 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#cd6e00;font-weight:normal;background-color:#ddd;">'
Andrey Andreeve6a5f792016-03-07 11:34:43 +0200324 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 $output .= "</table>\n";
328 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000329
Andrey Andreev195d3112011-12-25 03:59:13 +0200330 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 }
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 /**
336 * Compile $_POST Data
337 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200339 */
Greg Aker5ac55942010-11-10 14:59:47 -0600340 protected function _compile_post()
Barry Mienydd671972010-10-04 16:33:58 +0200341 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200342 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300343 .'<fieldset id="ci_profiler_post" style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
344 ."\n"
345 .'<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data')."&nbsp;&nbsp;</legend>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200346
David Cox Jrabcb67c2013-10-16 14:43:52 -0400347 if (count($_POST) === 0 && count($_FILES) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300349 $output .= '<div style="color:#009900;font-weight:normal;padding:4px 0 4px 0;">'.$this->CI->lang->line('profiler_no_post').'</div>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
351 else
352 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300353 $output .= "\n\n<table style=\"width:100%;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 foreach ($_POST as $key => $val)
356 {
Andrey Andreeve6a5f792016-03-07 11:34:43 +0200357 is_int($key) OR $key = "'".htmlspecialchars($key, ENT_QUOTES, config_item('charset'))."'";
358 $val = (is_array($val) OR is_object($val))
Andrey Andreevcd8d5282017-01-23 11:42:47 +0200359 ? '<pre>'.htmlspecialchars(print_r($val, TRUE), ENT_QUOTES, config_item('charset')).'</pre>'
Andrey Andreeve6a5f792016-03-07 11:34:43 +0200360 : htmlspecialchars($val, ENT_QUOTES, config_item('charset'));
Barry Mienydd671972010-10-04 16:33:58 +0200361
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300362 $output .= '<tr><td style="width:50%;padding:5px;color:#000;background-color:#ddd;">&#36;_POST['
Andrey Andreeve6a5f792016-03-07 11:34:43 +0200363 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;">'
364 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
David Cox Jr06fa7392013-09-25 01:56:10 -0400367 foreach ($_FILES as $key => $val)
368 {
Andrey Andreeve6a5f792016-03-07 11:34:43 +0200369 is_int($key) OR $key = "'".htmlspecialchars($key, ENT_QUOTES, config_item('charset'))."'";
370 $val = (is_array($val) OR is_object($val))
Andrey Andreevcd8d5282017-01-23 11:42:47 +0200371 ? '<pre>'.htmlspecialchars(print_r($val, TRUE), ENT_QUOTES, config_item('charset')).'</pre>'
Andrey Andreeve6a5f792016-03-07 11:34:43 +0200372 : htmlspecialchars($val, ENT_QUOTES, config_item('charset'));
David Cox Jr06fa7392013-09-25 01:56:10 -0400373
374 $output .= '<tr><td style="width:50%;padding:5px;color:#000;background-color:#ddd;">&#36;_FILES['
Andrey Andreeve6a5f792016-03-07 11:34:43 +0200375 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;">'
376 .$val."</td></tr>\n";
David Cox Jr06fa7392013-09-25 01:56:10 -0400377 }
378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 $output .= "</table>\n";
380 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000381
Andrey Andreev195d3112011-12-25 03:59:13 +0200382 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 /**
388 * Show query string
389 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200391 */
Greg Aker5ac55942010-11-10 14:59:47 -0600392 protected function _compile_uri_string()
Barry Mienydd671972010-10-04 16:33:58 +0200393 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200394 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300395 .'<fieldset id="ci_profiler_uri_string" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
396 ."\n"
397 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string')."&nbsp;&nbsp;</legend>\n"
398 .'<div style="color:#000;font-weight:normal;padding:4px 0 4px 0;">'
Alex Bilbied261b1e2012-06-02 11:12:16 +0100399 .($this->CI->uri->uri_string === '' ? $this->CI->lang->line('profiler_no_uri') : $this->CI->uri->uri_string)
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300400 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
402
403 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 /**
406 * Show the controller and function that were called
407 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200409 */
Greg Aker5ac55942010-11-10 14:59:47 -0600410 protected function _compile_controller_info()
Barry Mienydd671972010-10-04 16:33:58 +0200411 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200412 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300413 .'<fieldset id="ci_profiler_controller_info" style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
414 ."\n"
415 .'<legend style="color:#995300;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_controller_info')."&nbsp;&nbsp;</legend>\n"
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300416 .'<div style="color:#995300;font-weight:normal;padding:4px 0 4px 0;">'.$this->CI->router->class.'/'.$this->CI->router->method
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300417 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
Derek Allard76af4092010-01-16 19:20:49 +0000419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 /**
423 * Compile memory usage
424 *
425 * Display total used memory
426 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 * @return string
428 */
Greg Aker5ac55942010-11-10 14:59:47 -0600429 protected function _compile_memory_usage()
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200431 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300432 .'<fieldset id="ci_profiler_memory_usage" style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
433 ."\n"
434 .'<legend style="color:#5a0099;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage')."&nbsp;&nbsp;</legend>\n"
435 .'<div style="color:#5a0099;font-weight:normal;padding:4px 0 4px 0;">'
Andrey Andreevc839d282012-06-07 14:35:27 +0300436 .(($usage = memory_get_usage()) != '' ? number_format($usage).' bytes' : $this->CI->lang->line('profiler_no_memory'))
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300437 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
439
440 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 /**
Derek Allard76af4092010-01-16 19:20:49 +0000443 * Compile header information
444 *
445 * Lists HTTP headers
446 *
Derek Allard76af4092010-01-16 19:20:49 +0000447 * @return string
448 */
Greg Aker5ac55942010-11-10 14:59:47 -0600449 protected function _compile_http_headers()
Derek Allard76af4092010-01-16 19:20:49 +0000450 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200451 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300452 .'<fieldset id="ci_profiler_http_headers" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
453 ."\n"
454 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_headers')
455 .'&nbsp;&nbsp;(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_httpheaders_table\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI->lang->line('profiler_section_show').'\'?\''.$this->CI->lang->line('profiler_section_hide').'\':\''.$this->CI->lang->line('profiler_section_show').'\';">'.$this->CI->lang->line('profiler_section_show')."</span>)</legend>\n\n\n"
456 .'<table style="width:100%;display:none;" id="ci_profiler_httpheaders_table">'."\n";
Derek Allard76af4092010-01-16 19:20:49 +0000457
Iacami7c72cfc2013-06-27 12:20:48 -0300458 foreach (array('HTTP_ACCEPT', 'HTTP_USER_AGENT', 'HTTP_CONNECTION', 'SERVER_PORT', 'SERVER_NAME', 'REMOTE_ADDR', 'SERVER_SOFTWARE', 'HTTP_ACCEPT_LANGUAGE', 'SCRIPT_NAME', 'REQUEST_METHOD',' HTTP_HOST', 'REMOTE_HOST', 'CONTENT_TYPE', 'SERVER_PROTOCOL', 'QUERY_STRING', 'HTTP_ACCEPT_ENCODING', 'HTTP_X_FORWARDED_FOR', 'HTTP_DNT') as $header)
Derek Allard76af4092010-01-16 19:20:49 +0000459 {
Andrey Andreeve6a5f792016-03-07 11:34:43 +0200460 $val = isset($_SERVER[$header]) ? htmlspecialchars($_SERVER[$header], ENT_QUOTES, config_item('charset')) : '';
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300461 $output .= '<tr><td style="vertical-align:top;width:50%;padding:5px;color:#900;background-color:#ddd;">'
462 .$header.'&nbsp;&nbsp;</td><td style="width:50%;padding:5px;color:#000;background-color:#ddd;">'.$val."</td></tr>\n";
Derek Allard76af4092010-01-16 19:20:49 +0000463 }
464
Andrey Andreev195d3112011-12-25 03:59:13 +0200465 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000466 }
467
468 // --------------------------------------------------------------------
469
470 /**
471 * Compile config information
472 *
473 * Lists developer config variables
474 *
Derek Allard76af4092010-01-16 19:20:49 +0000475 * @return string
476 */
Greg Aker5ac55942010-11-10 14:59:47 -0600477 protected function _compile_config()
Derek Allard76af4092010-01-16 19:20:49 +0000478 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200479 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300480 .'<fieldset id="ci_profiler_config" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
481 ."\n"
482 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_config').'&nbsp;&nbsp;(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_config_table\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI->lang->line('profiler_section_show').'\'?\''.$this->CI->lang->line('profiler_section_hide').'\':\''.$this->CI->lang->line('profiler_section_show').'\';">'.$this->CI->lang->line('profiler_section_show')."</span>)</legend>\n\n\n"
483 .'<table style="width:100%;display:none;" id="ci_profiler_config_table">'."\n";
Derek Allard76af4092010-01-16 19:20:49 +0000484
Andrey Andreev963386b2012-03-02 01:52:01 +0200485 foreach ($this->CI->config->config as $config => $val)
Derek Allard76af4092010-01-16 19:20:49 +0000486 {
Andrey Andreev9c068a12017-07-10 14:40:06 +0300487 $pre = '';
488 $pre_close = '';
489
Andrey Andreev963386b2012-03-02 01:52:01 +0200490 if (is_array($val) OR is_object($val))
Greg Akere6026832010-04-29 13:41:39 -0500491 {
492 $val = print_r($val, TRUE);
Andrey Andreev9c068a12017-07-10 14:40:06 +0300493
494 $pre = '<pre>' ;
495 $pre_close = '</pre>';
Greg Akere6026832010-04-29 13:41:39 -0500496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300498 $output .= '<tr><td style="padding:5px;vertical-align:top;color:#900;background-color:#ddd;">'
Andrey Andreev9c068a12017-07-10 14:40:06 +0300499 .$config.'&nbsp;&nbsp;</td><td style="padding:5px;color:#000;background-color:#ddd;">'.$pre.htmlspecialchars($val, ENT_QUOTES, config_item('charset')).$pre_close."</td></tr>\n";
Derek Allard76af4092010-01-16 19:20:49 +0000500 }
501
Andrey Andreev195d3112011-12-25 03:59:13 +0200502 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000503 }
504
505 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Allard76af4092010-01-16 19:20:49 +0000507 /**
Greg Aker62df1312011-04-18 11:18:02 -0500508 * Compile session userdata
509 *
510 * @return string
511 */
yterajima8310c9a2011-08-22 07:26:54 +0900512 protected function _compile_session_data()
Greg Aker62df1312011-04-18 11:18:02 -0500513 {
514 if ( ! isset($this->CI->session))
515 {
516 return;
517 }
518
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300519 $output = '<fieldset id="ci_profiler_csession" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
520 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_session_data').'&nbsp;&nbsp;(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_session_data\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI->lang->line('profiler_section_show').'\'?\''.$this->CI->lang->line('profiler_section_hide').'\':\''.$this->CI->lang->line('profiler_section_show').'\';">'.$this->CI->lang->line('profiler_section_show').'</span>)</legend>'
521 .'<table style="width:100%;display:none;" id="ci_profiler_session_data">';
Greg Aker62df1312011-04-18 11:18:02 -0500522
Andrey Andreevecc260e2014-01-24 14:20:13 +0200523 foreach ($this->CI->session->userdata() as $key => $val)
Greg Aker62df1312011-04-18 11:18:02 -0500524 {
Andrey Andreev9c068a12017-07-10 14:40:06 +0300525 $pre = '';
526 $pre_close = '';
527
Andrey Andreev963386b2012-03-02 01:52:01 +0200528 if (is_array($val) OR is_object($val))
Greg Aker62df1312011-04-18 11:18:02 -0500529 {
530 $val = print_r($val, TRUE);
Andrey Andreev9c068a12017-07-10 14:40:06 +0300531
532 $pre = '<pre>' ;
533 $pre_close = '</pre>';
Greg Aker62df1312011-04-18 11:18:02 -0500534 }
535
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300536 $output .= '<tr><td style="padding:5px;vertical-align:top;color:#900;background-color:#ddd;">'
Andrey Andreev9c068a12017-07-10 14:40:06 +0300537 .$key.'&nbsp;&nbsp;</td><td style="padding:5px;color:#000;background-color:#ddd;">'.$pre.htmlspecialchars($val, ENT_QUOTES, config_item('charset')).$pre_close."</td></tr>\n";
Greg Aker62df1312011-04-18 11:18:02 -0500538 }
539
Andrey Andreev195d3112011-12-25 03:59:13 +0200540 return $output."</table>\n</fieldset>";
Greg Aker62df1312011-04-18 11:18:02 -0500541 }
542
543 // --------------------------------------------------------------------
544
545 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 * Run the Profiler
547 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200549 */
Greg Aker5ac55942010-11-10 14:59:47 -0600550 public function run()
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300552 $output = '<div id="codeigniter_profiler" style="clear:both;background-color:#fff;padding:10px;">';
Derek Jonesee71c802010-03-10 10:05:05 -0600553 $fields_displayed = 0;
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Jonesee71c802010-03-10 10:05:05 -0600555 foreach ($this->_available_sections as $section)
556 {
557 if ($this->_compile_{$section} !== FALSE)
558 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300559 $func = '_compile_'.$section;
Derek Jonesee71c802010-03-10 10:05:05 -0600560 $output .= $this->{$func}();
561 $fields_displayed++;
562 }
563 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000564
Andrey Andreev195d3112011-12-25 03:59:13 +0200565 if ($fields_displayed === 0)
Derek Jonesee71c802010-03-10 10:05:05 -0600566 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300567 $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee;">'
568 .$this->CI->lang->line('profiler_no_profiles').'</p>';
Derek Jonesee71c802010-03-10 10:05:05 -0600569 }
Barry Mienydd671972010-10-04 16:33:58 +0200570
Andrey Andreev195d3112011-12-25 03:59:13 +0200571 return $output.'</div>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 }
Andrey Andreev56454792012-05-17 14:32:19 +0300573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574}