blob: d3f5d8de26bbc190f7c71d60793ec58c20257703 [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 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000053 * @link http://codeigniter.com/user_guide/general/profiling.html
54 */
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
Greg Akere6e6e642011-04-18 15:54:13 -0500103 if (isset($config['query_toggle_count']))
104 {
105 $this->_query_toggle_count = (int) $config['query_toggle_count'];
106 unset($config['query_toggle_count']);
107 }
108
Derek Jonesee71c802010-03-10 10:05:05 -0600109 // default all sections to display
110 foreach ($this->_available_sections as $section)
111 {
112 if ( ! isset($config[$section]))
113 {
114 $this->_compile_{$section} = TRUE;
115 }
116 }
Barry Mienydd671972010-10-04 16:33:58 +0200117
Derek Jonesee71c802010-03-10 10:05:05 -0600118 $this->set_sections($config);
Andrey Andreev90726b82015-01-20 12:39:22 +0200119 log_message('info', 'Profiler Class Initialized');
Barry Mienydd671972010-10-04 16:33:58 +0200120 }
121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 // --------------------------------------------------------------------
123
124 /**
Derek Jonesee71c802010-03-10 10:05:05 -0600125 * Set Sections
126 *
127 * Sets the private _compile_* properties to enable/disable Profiler sections
128 *
Andrey Andreev55a8c622012-11-06 13:31:21 +0200129 * @param mixed $config
Derek Jonesee71c802010-03-10 10:05:05 -0600130 * @return void
131 */
Greg Aker5ac55942010-11-10 14:59:47 -0600132 public function set_sections($config)
Derek Jonesee71c802010-03-10 10:05:05 -0600133 {
Andrey Andreev0140ddd2012-06-16 01:12:56 +0300134 if (isset($config['query_toggle_count']))
135 {
136 $this->_query_toggle_count = (int) $config['query_toggle_count'];
137 unset($config['query_toggle_count']);
138 }
139
Derek Jonesee71c802010-03-10 10:05:05 -0600140 foreach ($config as $method => $enable)
141 {
142 if (in_array($method, $this->_available_sections))
143 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300144 $this->_compile_{$method} = ($enable !== FALSE);
Derek Jonesee71c802010-03-10 10:05:05 -0600145 }
146 }
147 }
148
149 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Jonesee71c802010-03-10 10:05:05 -0600151 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 * Auto Profiler
153 *
154 * This function cycles through the entire array of mark points and
155 * matches any two points that are named identically (ending in "_start"
Derek Jones4b9c6292011-07-01 17:40:48 -0500156 * and "_end" respectively). It then compiles the execution times for
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 * all points and returns it as an array
Greg Aker5ac55942010-11-10 14:59:47 -0600158 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 * @return array
160 */
Greg Aker5ac55942010-11-10 14:59:47 -0600161 protected function _compile_benchmarks()
Barry Mienydd671972010-10-04 16:33:58 +0200162 {
163 $profile = array();
164 foreach ($this->CI->benchmark->marker as $key => $val)
165 {
166 // We match the "end" marker so that the list ends
167 // up in the order that it was defined
Richard Cunningham70a86262014-03-19 13:11:14 -0400168 if (preg_match('/(.+?)_end$/i', $key, $match)
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300169 && isset($this->CI->benchmark->marker[$match[1].'_end'], $this->CI->benchmark->marker[$match[1].'_start']))
Barry Mienydd671972010-10-04 16:33:58 +0200170 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200171 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
Barry Mienydd671972010-10-04 16:33:58 +0200172 }
173 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000174
175 // Build a table containing the profile data.
176 // Note: At some point we should turn this into a template that can
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300177 // be modified. We also might want to make this data available to be logged
Barry Mienydd671972010-10-04 16:33:58 +0200178
Andrey Andreev195d3112011-12-25 03:59:13 +0200179 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300180 .'<fieldset id="ci_profiler_benchmarks" style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
181 ."\n"
182 .'<legend style="color:#900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks')."&nbsp;&nbsp;</legend>"
183 ."\n\n\n<table style=\"width:100%;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 foreach ($profile as $key => $val)
186 {
187 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300188 $output .= '<tr><td style="padding:5px;width:50%;color:#000;font-weight:bold;background-color:#ddd;">'
189 .$key.'&nbsp;&nbsp;</td><td style="padding:5px;width:50%;color:#900;font-weight:normal;background-color:#ddd;">'
190 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 }
Barry Mienydd671972010-10-04 16:33:58 +0200192
Andrey Andreev195d3112011-12-25 03:59:13 +0200193 return $output."</table>\n</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200194 }
195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 // --------------------------------------------------------------------
197
198 /**
199 * Compile Queries
200 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200202 */
Greg Aker5ac55942010-11-10 14:59:47 -0600203 protected function _compile_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
205 $dbs = array();
Derek Jonesf0a9b332009-07-29 14:19:18 +0000206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 // Let's determine which databases are currently connected to
Andrey Andreev55a8c622012-11-06 13:31:21 +0200208 foreach (get_object_vars($this->CI) as $name => $cobject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
Andrey Andreev55a8c622012-11-06 13:31:21 +0200210 if (is_object($cobject))
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
Andrey Andreev55a8c622012-11-06 13:31:21 +0200212 if ($cobject instanceof CI_DB)
213 {
214 $dbs[get_class($this->CI).':$'.$name] = $cobject;
215 }
216 elseif ($cobject instanceof CI_Model)
217 {
218 foreach (get_object_vars($cobject) as $mname => $mobject)
219 {
220 if ($mobject instanceof CI_DB)
221 {
222 $dbs[get_class($cobject).':$'.$mname] = $mobject;
223 }
224 }
225 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Andrey Andreev195d3112011-12-25 03:59:13 +0200229 if (count($dbs) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200231 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300232 .'<fieldset id="ci_profiler_queries" style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
233 ."\n"
234 .'<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>'
235 ."\n\n\n<table style=\"border:none; width:100%;\">\n"
236 .'<tr><td style="width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;">'
237 .$this->CI->lang->line('profiler_no_db')
238 ."</td></tr>\n</table>\n</fieldset>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 // Load the text helper so we can highlight the SQL
242 $this->CI->load->helper('text');
243
244 // Key words we want bolded
Greg Aker3424bf72010-09-14 17:53:10 -0500245 $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 +0000246
Derek Jones4b9c6292011-07-01 17:40:48 -0500247 $output = "\n\n";
Greg Akere6e6e642011-04-18 15:54:13 -0500248 $count = 0;
Razicanc24f49b2011-04-25 13:43:57 +0200249
Andrey Andreev55a8c622012-11-06 13:31:21 +0200250 foreach ($dbs as $name => $db)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
Greg Akere6e6e642011-04-18 15:54:13 -0500252 $hide_queries = (count($db->queries) > $this->_query_toggle_count) ? ' display:none' : '';
Tim3224f072013-02-14 09:26:24 +0200253 $total_time = number_format(array_sum($db->query_times), 4).' '.$this->CI->lang->line('profiler_seconds');
Razicanc24f49b2011-04-25 13:43:57 +0200254
Greg Akere6e6e642011-04-18 15:54:13 -0500255 $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 +0200256
Alex Bilbied261b1e2012-06-02 11:12:16 +0100257 if ($hide_queries !== '')
Greg Akere6e6e642011-04-18 15:54:13 -0500258 {
259 $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>)';
260 }
Razicanc24f49b2011-04-25 13:43:57 +0200261
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300262 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
263 ."\n"
264 .'<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_database')
Andrey Andreev55a8c622012-11-06 13:31:21 +0200265 .':&nbsp; '.$db->database.' ('.$name.')&nbsp;&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries')
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200266 .': '.count($db->queries).' ('.$total_time.')&nbsp;&nbsp;'.$show_hide_js."</legend>\n\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300267 .'<table style="width:100%;'.$hide_queries.'" id="ci_profiler_queries_db_'.$count."\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200268
Andrey Andreev195d3112011-12-25 03:59:13 +0200269 if (count($db->queries) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300271 $output .= '<tr><td style="width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;">'
272 .$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 }
274 else
Barry Mienydd671972010-10-04 16:33:58 +0200275 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 foreach ($db->queries as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200277 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 $time = number_format($db->query_times[$key], 4);
Rasmus Lerdorf164a1f22013-05-18 10:29:56 -0400279 $val = highlight_code($val);
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 foreach ($highlight as $bold)
282 {
Barry Mienydd671972010-10-04 16:33:58 +0200283 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 }
Barry Mienydd671972010-10-04 16:33:58 +0200285
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300286 $output .= '<tr><td style="padding:5px;vertical-align:top;width:1%;color:#900;font-weight:normal;background-color:#ddd;">'
287 .$time.'&nbsp;&nbsp;</td><td style="padding:5px;color:#000;font-weight:normal;background-color:#ddd;">'
288 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Andrey Andreev195d3112011-12-25 03:59:13 +0200292 $output .= "</table>\n</fieldset>";
Cristian Riffo Huez0612e922013-12-13 22:09:44 -0300293 $count++;
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 return $output;
297 }
298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 // --------------------------------------------------------------------
300
301 /**
302 * Compile $_GET Data
303 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200305 */
Greg Aker5ac55942010-11-10 14:59:47 -0600306 protected function _compile_get()
Barry Mienydd671972010-10-04 16:33:58 +0200307 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300308 $output = "\n\n"
309 .'<fieldset id="ci_profiler_get" style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
310 ."\n"
311 .'<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data')."&nbsp;&nbsp;</legend>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200312
Andrey Andreev195d3112011-12-25 03:59:13 +0200313 if (count($_GET) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300315 $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 +0000316 }
317 else
318 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300319 $output .= "\n\n<table style=\"width:100%;border:none;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 foreach ($_GET as $key => $val)
322 {
David Cox Jrabcb67c2013-10-16 14:43:52 -0400323 is_int($key) OR $key = "'".$key."'";
Barry Mienydd671972010-10-04 16:33:58 +0200324
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300325 $output .= '<tr><td style="width:50%;color:#000;background-color:#ddd;padding:5px;">&#36;_GET['
326 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#cd6e00;font-weight:normal;background-color:#ddd;">'
327 .((is_array($val) OR is_object($val)) ? '<pre>'.htmlspecialchars(stripslashes(print_r($val, TRUE))).'</pre>' : htmlspecialchars(stripslashes($val)))
328 ."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 $output .= "</table>\n";
332 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000333
Andrey Andreev195d3112011-12-25 03:59:13 +0200334 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 /**
340 * Compile $_POST Data
341 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200343 */
Greg Aker5ac55942010-11-10 14:59:47 -0600344 protected function _compile_post()
Barry Mienydd671972010-10-04 16:33:58 +0200345 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200346 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300347 .'<fieldset id="ci_profiler_post" style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
348 ."\n"
349 .'<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data')."&nbsp;&nbsp;</legend>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200350
David Cox Jrabcb67c2013-10-16 14:43:52 -0400351 if (count($_POST) === 0 && count($_FILES) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300353 $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 +0000354 }
355 else
356 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300357 $output .= "\n\n<table style=\"width:100%;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 foreach ($_POST as $key => $val)
360 {
David Cox Jrabcb67c2013-10-16 14:43:52 -0400361 is_int($key) OR $key = "'".$key."'";
Barry Mienydd671972010-10-04 16:33:58 +0200362
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300363 $output .= '<tr><td style="width:50%;padding:5px;color:#000;background-color:#ddd;">&#36;_POST['
364 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;">';
365
Andrey Andreev963386b2012-03-02 01:52:01 +0200366 if (is_array($val) OR is_object($val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300368 $output .= '<pre>'.htmlspecialchars(stripslashes(print_r($val, TRUE))).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
370 else
371 {
372 $output .= htmlspecialchars(stripslashes($val));
373 }
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 $output .= "</td></tr>\n";
376 }
Barry Mienydd671972010-10-04 16:33:58 +0200377
David Cox Jr06fa7392013-09-25 01:56:10 -0400378 foreach ($_FILES as $key => $val)
379 {
David Cox Jrabcb67c2013-10-16 14:43:52 -0400380 is_int($key) OR $key = "'".$key."'";
David Cox Jr06fa7392013-09-25 01:56:10 -0400381
382 $output .= '<tr><td style="width:50%;padding:5px;color:#000;background-color:#ddd;">&#36;_FILES['
383 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;">';
384
385 if (is_array($val) OR is_object($val))
386 {
387 $output .= '<pre>'.htmlspecialchars(stripslashes(print_r($val, TRUE))).'</pre>';
388 }
389
390 $output .= "</td></tr>\n";
391 }
392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 $output .= "</table>\n";
394 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000395
Andrey Andreev195d3112011-12-25 03:59:13 +0200396 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 /**
402 * Show query string
403 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200405 */
Greg Aker5ac55942010-11-10 14:59:47 -0600406 protected function _compile_uri_string()
Barry Mienydd671972010-10-04 16:33:58 +0200407 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200408 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300409 .'<fieldset id="ci_profiler_uri_string" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
410 ."\n"
411 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string')."&nbsp;&nbsp;</legend>\n"
412 .'<div style="color:#000;font-weight:normal;padding:4px 0 4px 0;">'
Alex Bilbied261b1e2012-06-02 11:12:16 +0100413 .($this->CI->uri->uri_string === '' ? $this->CI->lang->line('profiler_no_uri') : $this->CI->uri->uri_string)
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300414 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
416
417 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 /**
420 * Show the controller and function that were called
421 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200423 */
Greg Aker5ac55942010-11-10 14:59:47 -0600424 protected function _compile_controller_info()
Barry Mienydd671972010-10-04 16:33:58 +0200425 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200426 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300427 .'<fieldset id="ci_profiler_controller_info" style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
428 ."\n"
429 .'<legend style="color:#995300;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_controller_info')."&nbsp;&nbsp;</legend>\n"
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300430 .'<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 +0300431 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 }
Derek Allard76af4092010-01-16 19:20:49 +0000433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 /**
437 * Compile memory usage
438 *
439 * Display total used memory
440 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 * @return string
442 */
Greg Aker5ac55942010-11-10 14:59:47 -0600443 protected function _compile_memory_usage()
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200445 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300446 .'<fieldset id="ci_profiler_memory_usage" style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
447 ."\n"
448 .'<legend style="color:#5a0099;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage')."&nbsp;&nbsp;</legend>\n"
449 .'<div style="color:#5a0099;font-weight:normal;padding:4px 0 4px 0;">'
Andrey Andreevc839d282012-06-07 14:35:27 +0300450 .(($usage = memory_get_usage()) != '' ? number_format($usage).' bytes' : $this->CI->lang->line('profiler_no_memory'))
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300451 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 }
453
454 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 /**
Derek Allard76af4092010-01-16 19:20:49 +0000457 * Compile header information
458 *
459 * Lists HTTP headers
460 *
Derek Allard76af4092010-01-16 19:20:49 +0000461 * @return string
462 */
Greg Aker5ac55942010-11-10 14:59:47 -0600463 protected function _compile_http_headers()
Derek Allard76af4092010-01-16 19:20:49 +0000464 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200465 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300466 .'<fieldset id="ci_profiler_http_headers" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
467 ."\n"
468 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_headers')
469 .'&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"
470 .'<table style="width:100%;display:none;" id="ci_profiler_httpheaders_table">'."\n";
Derek Allard76af4092010-01-16 19:20:49 +0000471
Iacami7c72cfc2013-06-27 12:20:48 -0300472 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 +0000473 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300474 $val = isset($_SERVER[$header]) ? $_SERVER[$header] : '';
475 $output .= '<tr><td style="vertical-align:top;width:50%;padding:5px;color:#900;background-color:#ddd;">'
476 .$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 +0000477 }
478
Andrey Andreev195d3112011-12-25 03:59:13 +0200479 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000480 }
481
482 // --------------------------------------------------------------------
483
484 /**
485 * Compile config information
486 *
487 * Lists developer config variables
488 *
Derek Allard76af4092010-01-16 19:20:49 +0000489 * @return string
490 */
Greg Aker5ac55942010-11-10 14:59:47 -0600491 protected function _compile_config()
Derek Allard76af4092010-01-16 19:20:49 +0000492 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200493 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300494 .'<fieldset id="ci_profiler_config" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
495 ."\n"
496 .'<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"
497 .'<table style="width:100%;display:none;" id="ci_profiler_config_table">'."\n";
Derek Allard76af4092010-01-16 19:20:49 +0000498
Andrey Andreev963386b2012-03-02 01:52:01 +0200499 foreach ($this->CI->config->config as $config => $val)
Derek Allard76af4092010-01-16 19:20:49 +0000500 {
Andrey Andreev963386b2012-03-02 01:52:01 +0200501 if (is_array($val) OR is_object($val))
Greg Akere6026832010-04-29 13:41:39 -0500502 {
503 $val = print_r($val, TRUE);
504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300506 $output .= '<tr><td style="padding:5px;vertical-align:top;color:#900;background-color:#ddd;">'
507 .$config.'&nbsp;&nbsp;</td><td style="padding:5px;color:#000;background-color:#ddd;">'.htmlspecialchars($val)."</td></tr>\n";
Derek Allard76af4092010-01-16 19:20:49 +0000508 }
509
Andrey Andreev195d3112011-12-25 03:59:13 +0200510 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000511 }
512
513 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Allard76af4092010-01-16 19:20:49 +0000515 /**
Greg Aker62df1312011-04-18 11:18:02 -0500516 * Compile session userdata
517 *
518 * @return string
519 */
yterajima8310c9a2011-08-22 07:26:54 +0900520 protected function _compile_session_data()
Greg Aker62df1312011-04-18 11:18:02 -0500521 {
522 if ( ! isset($this->CI->session))
523 {
524 return;
525 }
526
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300527 $output = '<fieldset id="ci_profiler_csession" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
528 .'<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>'
529 .'<table style="width:100%;display:none;" id="ci_profiler_session_data">';
Greg Aker62df1312011-04-18 11:18:02 -0500530
Andrey Andreevecc260e2014-01-24 14:20:13 +0200531 foreach ($this->CI->session->userdata() as $key => $val)
Greg Aker62df1312011-04-18 11:18:02 -0500532 {
Andrey Andreev963386b2012-03-02 01:52:01 +0200533 if (is_array($val) OR is_object($val))
Greg Aker62df1312011-04-18 11:18:02 -0500534 {
535 $val = print_r($val, TRUE);
536 }
537
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300538 $output .= '<tr><td style="padding:5px;vertical-align:top;color:#900;background-color:#ddd;">'
539 .$key.'&nbsp;&nbsp;</td><td style="padding:5px;color:#000;background-color:#ddd;">'.htmlspecialchars($val)."</td></tr>\n";
Greg Aker62df1312011-04-18 11:18:02 -0500540 }
541
Andrey Andreev195d3112011-12-25 03:59:13 +0200542 return $output."</table>\n</fieldset>";
Greg Aker62df1312011-04-18 11:18:02 -0500543 }
544
545 // --------------------------------------------------------------------
546
547 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 * Run the Profiler
549 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200551 */
Greg Aker5ac55942010-11-10 14:59:47 -0600552 public function run()
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300554 $output = '<div id="codeigniter_profiler" style="clear:both;background-color:#fff;padding:10px;">';
Derek Jonesee71c802010-03-10 10:05:05 -0600555 $fields_displayed = 0;
Barry Mienydd671972010-10-04 16:33:58 +0200556
Derek Jonesee71c802010-03-10 10:05:05 -0600557 foreach ($this->_available_sections as $section)
558 {
559 if ($this->_compile_{$section} !== FALSE)
560 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300561 $func = '_compile_'.$section;
Derek Jonesee71c802010-03-10 10:05:05 -0600562 $output .= $this->{$func}();
563 $fields_displayed++;
564 }
565 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000566
Andrey Andreev195d3112011-12-25 03:59:13 +0200567 if ($fields_displayed === 0)
Derek Jonesee71c802010-03-10 10:05:05 -0600568 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300569 $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee;">'
570 .$this->CI->lang->line('profiler_no_profiles').'</p>';
Derek Jonesee71c802010-03-10 10:05:05 -0600571 }
Barry Mienydd671972010-10-04 16:33:58 +0200572
Andrey Andreev195d3112011-12-25 03:59:13 +0200573 return $output.'</div>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 }
Andrey Andreev56454792012-05-17 14:32:19 +0300575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576}
577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578/* End of file Profiler.php */
Andrey Andreeva6eae872014-01-03 18:25:20 +0200579/* Location: ./system/libraries/Profiler.php */