blob: 9a4e833ccc5abcb63fdfb7ebb07fd7ba55918c0b [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);
Barry Mienydd671972010-10-04 16:33:58 +0200119 }
120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 // --------------------------------------------------------------------
122
123 /**
Derek Jonesee71c802010-03-10 10:05:05 -0600124 * Set Sections
125 *
126 * Sets the private _compile_* properties to enable/disable Profiler sections
127 *
Andrey Andreev55a8c622012-11-06 13:31:21 +0200128 * @param mixed $config
Derek Jonesee71c802010-03-10 10:05:05 -0600129 * @return void
130 */
Greg Aker5ac55942010-11-10 14:59:47 -0600131 public function set_sections($config)
Derek Jonesee71c802010-03-10 10:05:05 -0600132 {
Andrey Andreev0140ddd2012-06-16 01:12:56 +0300133 if (isset($config['query_toggle_count']))
134 {
135 $this->_query_toggle_count = (int) $config['query_toggle_count'];
136 unset($config['query_toggle_count']);
137 }
138
Derek Jonesee71c802010-03-10 10:05:05 -0600139 foreach ($config as $method => $enable)
140 {
141 if (in_array($method, $this->_available_sections))
142 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300143 $this->_compile_{$method} = ($enable !== FALSE);
Derek Jonesee71c802010-03-10 10:05:05 -0600144 }
145 }
146 }
147
148 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Jonesee71c802010-03-10 10:05:05 -0600150 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 * Auto Profiler
152 *
153 * This function cycles through the entire array of mark points and
154 * matches any two points that are named identically (ending in "_start"
Derek Jones4b9c6292011-07-01 17:40:48 -0500155 * and "_end" respectively). It then compiles the execution times for
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 * all points and returns it as an array
Greg Aker5ac55942010-11-10 14:59:47 -0600157 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 * @return array
159 */
Greg Aker5ac55942010-11-10 14:59:47 -0600160 protected function _compile_benchmarks()
Barry Mienydd671972010-10-04 16:33:58 +0200161 {
162 $profile = array();
163 foreach ($this->CI->benchmark->marker as $key => $val)
164 {
165 // We match the "end" marker so that the list ends
166 // up in the order that it was defined
Richard Cunningham70a86262014-03-19 13:11:14 -0400167 if (preg_match('/(.+?)_end$/i', $key, $match)
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300168 && isset($this->CI->benchmark->marker[$match[1].'_end'], $this->CI->benchmark->marker[$match[1].'_start']))
Barry Mienydd671972010-10-04 16:33:58 +0200169 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200170 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
Barry Mienydd671972010-10-04 16:33:58 +0200171 }
172 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000173
174 // Build a table containing the profile data.
175 // Note: At some point we should turn this into a template that can
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300176 // be modified. We also might want to make this data available to be logged
Barry Mienydd671972010-10-04 16:33:58 +0200177
Andrey Andreev195d3112011-12-25 03:59:13 +0200178 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300179 .'<fieldset id="ci_profiler_benchmarks" style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
180 ."\n"
181 .'<legend style="color:#900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks')."&nbsp;&nbsp;</legend>"
182 ."\n\n\n<table style=\"width:100%;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 foreach ($profile as $key => $val)
185 {
186 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300187 $output .= '<tr><td style="padding:5px;width:50%;color:#000;font-weight:bold;background-color:#ddd;">'
188 .$key.'&nbsp;&nbsp;</td><td style="padding:5px;width:50%;color:#900;font-weight:normal;background-color:#ddd;">'
189 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
Andrey Andreev195d3112011-12-25 03:59:13 +0200192 return $output."</table>\n</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200193 }
194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 // --------------------------------------------------------------------
196
197 /**
198 * Compile Queries
199 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200201 */
Greg Aker5ac55942010-11-10 14:59:47 -0600202 protected function _compile_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
204 $dbs = array();
Derek Jonesf0a9b332009-07-29 14:19:18 +0000205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 // Let's determine which databases are currently connected to
Andrey Andreev55a8c622012-11-06 13:31:21 +0200207 foreach (get_object_vars($this->CI) as $name => $cobject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
Andrey Andreev55a8c622012-11-06 13:31:21 +0200209 if (is_object($cobject))
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Andrey Andreev55a8c622012-11-06 13:31:21 +0200211 if ($cobject instanceof CI_DB)
212 {
213 $dbs[get_class($this->CI).':$'.$name] = $cobject;
214 }
215 elseif ($cobject instanceof CI_Model)
216 {
217 foreach (get_object_vars($cobject) as $mname => $mobject)
218 {
219 if ($mobject instanceof CI_DB)
220 {
221 $dbs[get_class($cobject).':$'.$mname] = $mobject;
222 }
223 }
224 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Andrey Andreev195d3112011-12-25 03:59:13 +0200228 if (count($dbs) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200230 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300231 .'<fieldset id="ci_profiler_queries" style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
232 ."\n"
233 .'<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>'
234 ."\n\n\n<table style=\"border:none; width:100%;\">\n"
235 .'<tr><td style="width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;">'
236 .$this->CI->lang->line('profiler_no_db')
237 ."</td></tr>\n</table>\n</fieldset>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 // Load the text helper so we can highlight the SQL
241 $this->CI->load->helper('text');
242
243 // Key words we want bolded
Greg Aker3424bf72010-09-14 17:53:10 -0500244 $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 +0000245
Derek Jones4b9c6292011-07-01 17:40:48 -0500246 $output = "\n\n";
Greg Akere6e6e642011-04-18 15:54:13 -0500247 $count = 0;
Razicanc24f49b2011-04-25 13:43:57 +0200248
Andrey Andreev55a8c622012-11-06 13:31:21 +0200249 foreach ($dbs as $name => $db)
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 {
Greg Akere6e6e642011-04-18 15:54:13 -0500251 $hide_queries = (count($db->queries) > $this->_query_toggle_count) ? ' display:none' : '';
Tim3224f072013-02-14 09:26:24 +0200252 $total_time = number_format(array_sum($db->query_times), 4).' '.$this->CI->lang->line('profiler_seconds');
Razicanc24f49b2011-04-25 13:43:57 +0200253
Greg Akere6e6e642011-04-18 15:54:13 -0500254 $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 +0200255
Alex Bilbied261b1e2012-06-02 11:12:16 +0100256 if ($hide_queries !== '')
Greg Akere6e6e642011-04-18 15:54:13 -0500257 {
258 $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>)';
259 }
Razicanc24f49b2011-04-25 13:43:57 +0200260
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300261 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
262 ."\n"
263 .'<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_database')
Andrey Andreev55a8c622012-11-06 13:31:21 +0200264 .':&nbsp; '.$db->database.' ('.$name.')&nbsp;&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries')
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200265 .': '.count($db->queries).' ('.$total_time.')&nbsp;&nbsp;'.$show_hide_js."</legend>\n\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300266 .'<table style="width:100%;'.$hide_queries.'" id="ci_profiler_queries_db_'.$count."\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200267
Andrey Andreev195d3112011-12-25 03:59:13 +0200268 if (count($db->queries) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300270 $output .= '<tr><td style="width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;">'
271 .$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
273 else
Barry Mienydd671972010-10-04 16:33:58 +0200274 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 foreach ($db->queries as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200276 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 $time = number_format($db->query_times[$key], 4);
Rasmus Lerdorf164a1f22013-05-18 10:29:56 -0400278 $val = highlight_code($val);
Barry Mienydd671972010-10-04 16:33:58 +0200279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 foreach ($highlight as $bold)
281 {
Barry Mienydd671972010-10-04 16:33:58 +0200282 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300285 $output .= '<tr><td style="padding:5px;vertical-align:top;width:1%;color:#900;font-weight:normal;background-color:#ddd;">'
286 .$time.'&nbsp;&nbsp;</td><td style="padding:5px;color:#000;font-weight:normal;background-color:#ddd;">'
287 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Andrey Andreev195d3112011-12-25 03:59:13 +0200291 $output .= "</table>\n</fieldset>";
Cristian Riffo Huez0612e922013-12-13 22:09:44 -0300292 $count++;
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 return $output;
296 }
297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // --------------------------------------------------------------------
299
300 /**
301 * Compile $_GET Data
302 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200304 */
Greg Aker5ac55942010-11-10 14:59:47 -0600305 protected function _compile_get()
Barry Mienydd671972010-10-04 16:33:58 +0200306 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300307 $output = "\n\n"
308 .'<fieldset id="ci_profiler_get" style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
309 ."\n"
310 .'<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data')."&nbsp;&nbsp;</legend>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200311
Andrey Andreev195d3112011-12-25 03:59:13 +0200312 if (count($_GET) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300314 $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 +0000315 }
316 else
317 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300318 $output .= "\n\n<table style=\"width:100%;border:none;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 foreach ($_GET as $key => $val)
321 {
David Cox Jrabcb67c2013-10-16 14:43:52 -0400322 is_int($key) OR $key = "'".$key."'";
Barry Mienydd671972010-10-04 16:33:58 +0200323
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300324 $output .= '<tr><td style="width:50%;color:#000;background-color:#ddd;padding:5px;">&#36;_GET['
325 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#cd6e00;font-weight:normal;background-color:#ddd;">'
326 .((is_array($val) OR is_object($val)) ? '<pre>'.htmlspecialchars(stripslashes(print_r($val, TRUE))).'</pre>' : htmlspecialchars(stripslashes($val)))
327 ."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 $output .= "</table>\n";
331 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000332
Andrey Andreev195d3112011-12-25 03:59:13 +0200333 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 /**
339 * Compile $_POST Data
340 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200342 */
Greg Aker5ac55942010-11-10 14:59:47 -0600343 protected function _compile_post()
Barry Mienydd671972010-10-04 16:33:58 +0200344 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200345 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300346 .'<fieldset id="ci_profiler_post" style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
347 ."\n"
348 .'<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data')."&nbsp;&nbsp;</legend>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200349
David Cox Jrabcb67c2013-10-16 14:43:52 -0400350 if (count($_POST) === 0 && count($_FILES) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300352 $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 +0000353 }
354 else
355 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300356 $output .= "\n\n<table style=\"width:100%;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 foreach ($_POST as $key => $val)
359 {
David Cox Jrabcb67c2013-10-16 14:43:52 -0400360 is_int($key) OR $key = "'".$key."'";
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['
363 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;">';
364
Andrey Andreev963386b2012-03-02 01:52:01 +0200365 if (is_array($val) OR is_object($val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300367 $output .= '<pre>'.htmlspecialchars(stripslashes(print_r($val, TRUE))).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 }
369 else
370 {
371 $output .= htmlspecialchars(stripslashes($val));
372 }
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 $output .= "</td></tr>\n";
375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
David Cox Jr06fa7392013-09-25 01:56:10 -0400377 foreach ($_FILES as $key => $val)
378 {
David Cox Jrabcb67c2013-10-16 14:43:52 -0400379 is_int($key) OR $key = "'".$key."'";
David Cox Jr06fa7392013-09-25 01:56:10 -0400380
381 $output .= '<tr><td style="width:50%;padding:5px;color:#000;background-color:#ddd;">&#36;_FILES['
382 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;">';
383
384 if (is_array($val) OR is_object($val))
385 {
386 $output .= '<pre>'.htmlspecialchars(stripslashes(print_r($val, TRUE))).'</pre>';
387 }
388
389 $output .= "</td></tr>\n";
390 }
391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 $output .= "</table>\n";
393 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000394
Andrey Andreev195d3112011-12-25 03:59:13 +0200395 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 /**
401 * Show query string
402 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200404 */
Greg Aker5ac55942010-11-10 14:59:47 -0600405 protected function _compile_uri_string()
Barry Mienydd671972010-10-04 16:33:58 +0200406 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200407 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300408 .'<fieldset id="ci_profiler_uri_string" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
409 ."\n"
410 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string')."&nbsp;&nbsp;</legend>\n"
411 .'<div style="color:#000;font-weight:normal;padding:4px 0 4px 0;">'
Alex Bilbied261b1e2012-06-02 11:12:16 +0100412 .($this->CI->uri->uri_string === '' ? $this->CI->lang->line('profiler_no_uri') : $this->CI->uri->uri_string)
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300413 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
415
416 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 /**
419 * Show the controller and function that were called
420 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200422 */
Greg Aker5ac55942010-11-10 14:59:47 -0600423 protected function _compile_controller_info()
Barry Mienydd671972010-10-04 16:33:58 +0200424 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200425 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300426 .'<fieldset id="ci_profiler_controller_info" style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
427 ."\n"
428 .'<legend style="color:#995300;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_controller_info')."&nbsp;&nbsp;</legend>\n"
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300429 .'<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 +0300430 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
Derek Allard76af4092010-01-16 19:20:49 +0000432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 /**
436 * Compile memory usage
437 *
438 * Display total used memory
439 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 * @return string
441 */
Greg Aker5ac55942010-11-10 14:59:47 -0600442 protected function _compile_memory_usage()
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200444 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300445 .'<fieldset id="ci_profiler_memory_usage" style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
446 ."\n"
447 .'<legend style="color:#5a0099;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage')."&nbsp;&nbsp;</legend>\n"
448 .'<div style="color:#5a0099;font-weight:normal;padding:4px 0 4px 0;">'
Andrey Andreevc839d282012-06-07 14:35:27 +0300449 .(($usage = memory_get_usage()) != '' ? number_format($usage).' bytes' : $this->CI->lang->line('profiler_no_memory'))
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300450 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
452
453 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 /**
Derek Allard76af4092010-01-16 19:20:49 +0000456 * Compile header information
457 *
458 * Lists HTTP headers
459 *
Derek Allard76af4092010-01-16 19:20:49 +0000460 * @return string
461 */
Greg Aker5ac55942010-11-10 14:59:47 -0600462 protected function _compile_http_headers()
Derek Allard76af4092010-01-16 19:20:49 +0000463 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200464 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300465 .'<fieldset id="ci_profiler_http_headers" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
466 ."\n"
467 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_headers')
468 .'&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"
469 .'<table style="width:100%;display:none;" id="ci_profiler_httpheaders_table">'."\n";
Derek Allard76af4092010-01-16 19:20:49 +0000470
Iacami7c72cfc2013-06-27 12:20:48 -0300471 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 +0000472 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300473 $val = isset($_SERVER[$header]) ? $_SERVER[$header] : '';
474 $output .= '<tr><td style="vertical-align:top;width:50%;padding:5px;color:#900;background-color:#ddd;">'
475 .$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 +0000476 }
477
Andrey Andreev195d3112011-12-25 03:59:13 +0200478 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000479 }
480
481 // --------------------------------------------------------------------
482
483 /**
484 * Compile config information
485 *
486 * Lists developer config variables
487 *
Derek Allard76af4092010-01-16 19:20:49 +0000488 * @return string
489 */
Greg Aker5ac55942010-11-10 14:59:47 -0600490 protected function _compile_config()
Derek Allard76af4092010-01-16 19:20:49 +0000491 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200492 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300493 .'<fieldset id="ci_profiler_config" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
494 ."\n"
495 .'<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"
496 .'<table style="width:100%;display:none;" id="ci_profiler_config_table">'."\n";
Derek Allard76af4092010-01-16 19:20:49 +0000497
Andrey Andreev963386b2012-03-02 01:52:01 +0200498 foreach ($this->CI->config->config as $config => $val)
Derek Allard76af4092010-01-16 19:20:49 +0000499 {
Andrey Andreev963386b2012-03-02 01:52:01 +0200500 if (is_array($val) OR is_object($val))
Greg Akere6026832010-04-29 13:41:39 -0500501 {
502 $val = print_r($val, TRUE);
503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300505 $output .= '<tr><td style="padding:5px;vertical-align:top;color:#900;background-color:#ddd;">'
506 .$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 +0000507 }
508
Andrey Andreev195d3112011-12-25 03:59:13 +0200509 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000510 }
511
512 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard76af4092010-01-16 19:20:49 +0000514 /**
Greg Aker62df1312011-04-18 11:18:02 -0500515 * Compile session userdata
516 *
517 * @return string
518 */
yterajima8310c9a2011-08-22 07:26:54 +0900519 protected function _compile_session_data()
Greg Aker62df1312011-04-18 11:18:02 -0500520 {
521 if ( ! isset($this->CI->session))
522 {
523 return;
524 }
525
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300526 $output = '<fieldset id="ci_profiler_csession" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
527 .'<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>'
528 .'<table style="width:100%;display:none;" id="ci_profiler_session_data">';
Greg Aker62df1312011-04-18 11:18:02 -0500529
Andrey Andreevecc260e2014-01-24 14:20:13 +0200530 foreach ($this->CI->session->userdata() as $key => $val)
Greg Aker62df1312011-04-18 11:18:02 -0500531 {
Andrey Andreev963386b2012-03-02 01:52:01 +0200532 if (is_array($val) OR is_object($val))
Greg Aker62df1312011-04-18 11:18:02 -0500533 {
534 $val = print_r($val, TRUE);
535 }
536
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300537 $output .= '<tr><td style="padding:5px;vertical-align:top;color:#900;background-color:#ddd;">'
538 .$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 -0500539 }
540
Andrey Andreev195d3112011-12-25 03:59:13 +0200541 return $output."</table>\n</fieldset>";
Greg Aker62df1312011-04-18 11:18:02 -0500542 }
543
544 // --------------------------------------------------------------------
545
546 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 * Run the Profiler
548 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200550 */
Greg Aker5ac55942010-11-10 14:59:47 -0600551 public function run()
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300553 $output = '<div id="codeigniter_profiler" style="clear:both;background-color:#fff;padding:10px;">';
Derek Jonesee71c802010-03-10 10:05:05 -0600554 $fields_displayed = 0;
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Jonesee71c802010-03-10 10:05:05 -0600556 foreach ($this->_available_sections as $section)
557 {
558 if ($this->_compile_{$section} !== FALSE)
559 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300560 $func = '_compile_'.$section;
Derek Jonesee71c802010-03-10 10:05:05 -0600561 $output .= $this->{$func}();
562 $fields_displayed++;
563 }
564 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000565
Andrey Andreev195d3112011-12-25 03:59:13 +0200566 if ($fields_displayed === 0)
Derek Jonesee71c802010-03-10 10:05:05 -0600567 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300568 $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee;">'
569 .$this->CI->lang->line('profiler_no_profiles').'</p>';
Derek Jonesee71c802010-03-10 10:05:05 -0600570 }
Barry Mienydd671972010-10-04 16:33:58 +0200571
Andrey Andreev195d3112011-12-25 03:59:13 +0200572 return $output.'</div>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 }
Andrey Andreev56454792012-05-17 14:32:19 +0300574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575}
576
Derek Allard2067d1a2008-11-13 22:59:24 +0000577/* End of file Profiler.php */
Andrey Andreeva6eae872014-01-03 18:25:20 +0200578/* Location: ./system/libraries/Profiler.php */