blob: 50ba1673f13430e3e8d407dd276c2150d9a82a97 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev195d3112011-12-25 03:59:13 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev195d3112011-12-25 03:59:13 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Profiler Class
31 *
32 * This class enables you to display benchmark, query, and other data
33 * in order to help with debugging and optimization.
34 *
35 * Note: At some point it would be good to move all the HTML in this class
36 * into a set of template files in order to allow customization.
37 *
38 * @package CodeIgniter
39 * @subpackage Libraries
40 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050041 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000042 * @link http://codeigniter.com/user_guide/general/profiling.html
43 */
44class CI_Profiler {
45
Timothy Warren0688ac92012-04-20 10:25:04 -040046 /**
47 * List of profiler sections available to show
48 *
49 * @var array
50 */
Greg Aker5ac55942010-11-10 14:59:47 -060051 protected $_available_sections = array(
Timothy Warren0688ac92012-04-20 10:25:04 -040052 'benchmarks',
53 'get',
54 'memory_usage',
55 'post',
56 'uri_string',
57 'controller_info',
58 'queries',
59 'http_headers',
60 'session_data',
61 'config'
62 );
Razicanc24f49b2011-04-25 13:43:57 +020063
Timothy Warren0688ac92012-04-20 10:25:04 -040064 /**
65 * Number of queries to show before making the additional queries togglable
66 *
67 * @var int
68 */
Greg Akere6e6e642011-04-18 15:54:13 -050069 protected $_query_toggle_count = 25;
Razicanc24f49b2011-04-25 13:43:57 +020070
Timothy Warren0688ac92012-04-20 10:25:04 -040071 /**
72 * Reference to the CodeIgniter singleton
73 *
74 * @var object
75 */
Razicanc24f49b2011-04-25 13:43:57 +020076 protected $CI;
Derek Jonesee71c802010-03-10 10:05:05 -060077
Andrey Andreev55a8c622012-11-06 13:31:21 +020078 // --------------------------------------------------------------------
79
Timothy Warren0688ac92012-04-20 10:25:04 -040080 /**
Andrey Andreev55a8c622012-11-06 13:31:21 +020081 * Class constructor
Timothy Warren0688ac92012-04-20 10:25:04 -040082 *
83 * Initialize Profiler
84 *
Andrey Andreev55a8c622012-11-06 13:31:21 +020085 * @param array $config Parameters
Timothy Warren0688ac92012-04-20 10:25:04 -040086 */
Greg Aker5ac55942010-11-10 14:59:47 -060087 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +020088 {
89 $this->CI =& get_instance();
90 $this->CI->load->language('profiler');
91
Greg Akere6e6e642011-04-18 15:54:13 -050092 if (isset($config['query_toggle_count']))
93 {
94 $this->_query_toggle_count = (int) $config['query_toggle_count'];
95 unset($config['query_toggle_count']);
96 }
97
Derek Jonesee71c802010-03-10 10:05:05 -060098 // default all sections to display
99 foreach ($this->_available_sections as $section)
100 {
101 if ( ! isset($config[$section]))
102 {
103 $this->_compile_{$section} = TRUE;
104 }
105 }
Barry Mienydd671972010-10-04 16:33:58 +0200106
Derek Jonesee71c802010-03-10 10:05:05 -0600107 $this->set_sections($config);
Barry Mienydd671972010-10-04 16:33:58 +0200108 }
109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 // --------------------------------------------------------------------
111
112 /**
Derek Jonesee71c802010-03-10 10:05:05 -0600113 * Set Sections
114 *
115 * Sets the private _compile_* properties to enable/disable Profiler sections
116 *
Andrey Andreev55a8c622012-11-06 13:31:21 +0200117 * @param mixed $config
Derek Jonesee71c802010-03-10 10:05:05 -0600118 * @return void
119 */
Greg Aker5ac55942010-11-10 14:59:47 -0600120 public function set_sections($config)
Derek Jonesee71c802010-03-10 10:05:05 -0600121 {
Andrey Andreev0140ddd2012-06-16 01:12:56 +0300122 if (isset($config['query_toggle_count']))
123 {
124 $this->_query_toggle_count = (int) $config['query_toggle_count'];
125 unset($config['query_toggle_count']);
126 }
127
Derek Jonesee71c802010-03-10 10:05:05 -0600128 foreach ($config as $method => $enable)
129 {
130 if (in_array($method, $this->_available_sections))
131 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300132 $this->_compile_{$method} = ($enable !== FALSE);
Derek Jonesee71c802010-03-10 10:05:05 -0600133 }
134 }
135 }
136
137 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Jonesee71c802010-03-10 10:05:05 -0600139 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 * Auto Profiler
141 *
142 * This function cycles through the entire array of mark points and
143 * matches any two points that are named identically (ending in "_start"
Derek Jones4b9c6292011-07-01 17:40:48 -0500144 * and "_end" respectively). It then compiles the execution times for
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 * all points and returns it as an array
Greg Aker5ac55942010-11-10 14:59:47 -0600146 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 * @return array
148 */
Greg Aker5ac55942010-11-10 14:59:47 -0600149 protected function _compile_benchmarks()
Barry Mienydd671972010-10-04 16:33:58 +0200150 {
151 $profile = array();
152 foreach ($this->CI->benchmark->marker as $key => $val)
153 {
154 // We match the "end" marker so that the list ends
155 // up in the order that it was defined
Andrey Andreev195d3112011-12-25 03:59:13 +0200156 if (preg_match('/(.+?)_end/i', $key, $match)
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300157 && isset($this->CI->benchmark->marker[$match[1].'_end'], $this->CI->benchmark->marker[$match[1].'_start']))
Barry Mienydd671972010-10-04 16:33:58 +0200158 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200159 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
Barry Mienydd671972010-10-04 16:33:58 +0200160 }
161 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000162
163 // Build a table containing the profile data.
164 // Note: At some point we should turn this into a template that can
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300165 // be modified. We also might want to make this data available to be logged
Barry Mienydd671972010-10-04 16:33:58 +0200166
Andrey Andreev195d3112011-12-25 03:59:13 +0200167 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300168 .'<fieldset id="ci_profiler_benchmarks" style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
169 ."\n"
170 .'<legend style="color:#900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks')."&nbsp;&nbsp;</legend>"
171 ."\n\n\n<table style=\"width:100%;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 foreach ($profile as $key => $val)
174 {
175 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300176 $output .= '<tr><td style="padding:5px;width:50%;color:#000;font-weight:bold;background-color:#ddd;">'
177 .$key.'&nbsp;&nbsp;</td><td style="padding:5px;width:50%;color:#900;font-weight:normal;background-color:#ddd;">'
178 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Andrey Andreev195d3112011-12-25 03:59:13 +0200181 return $output."</table>\n</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200182 }
183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 // --------------------------------------------------------------------
185
186 /**
187 * Compile Queries
188 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200190 */
Greg Aker5ac55942010-11-10 14:59:47 -0600191 protected function _compile_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
193 $dbs = array();
Derek Jonesf0a9b332009-07-29 14:19:18 +0000194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 // Let's determine which databases are currently connected to
Andrey Andreev55a8c622012-11-06 13:31:21 +0200196 foreach (get_object_vars($this->CI) as $name => $cobject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
Andrey Andreev55a8c622012-11-06 13:31:21 +0200198 if (is_object($cobject))
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
Andrey Andreev55a8c622012-11-06 13:31:21 +0200200 if ($cobject instanceof CI_DB)
201 {
202 $dbs[get_class($this->CI).':$'.$name] = $cobject;
203 }
204 elseif ($cobject instanceof CI_Model)
205 {
206 foreach (get_object_vars($cobject) as $mname => $mobject)
207 {
208 if ($mobject instanceof CI_DB)
209 {
210 $dbs[get_class($cobject).':$'.$mname] = $mobject;
211 }
212 }
213 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Andrey Andreev195d3112011-12-25 03:59:13 +0200217 if (count($dbs) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200219 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300220 .'<fieldset id="ci_profiler_queries" style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
221 ."\n"
222 .'<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>'
223 ."\n\n\n<table style=\"border:none; width:100%;\">\n"
224 .'<tr><td style="width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;">'
225 .$this->CI->lang->line('profiler_no_db')
226 ."</td></tr>\n</table>\n</fieldset>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 // Load the text helper so we can highlight the SQL
230 $this->CI->load->helper('text');
231
232 // Key words we want bolded
Greg Aker3424bf72010-09-14 17:53:10 -0500233 $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 +0000234
Derek Jones4b9c6292011-07-01 17:40:48 -0500235 $output = "\n\n";
Greg Akere6e6e642011-04-18 15:54:13 -0500236 $count = 0;
Razicanc24f49b2011-04-25 13:43:57 +0200237
Andrey Andreev55a8c622012-11-06 13:31:21 +0200238 foreach ($dbs as $name => $db)
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
Greg Akere6e6e642011-04-18 15:54:13 -0500240 $hide_queries = (count($db->queries) > $this->_query_toggle_count) ? ' display:none' : '';
Tim3224f072013-02-14 09:26:24 +0200241 $total_time = number_format(array_sum($db->query_times), 4).' '.$this->CI->lang->line('profiler_seconds');
Razicanc24f49b2011-04-25 13:43:57 +0200242
Greg Akere6e6e642011-04-18 15:54:13 -0500243 $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 +0200244
Alex Bilbied261b1e2012-06-02 11:12:16 +0100245 if ($hide_queries !== '')
Greg Akere6e6e642011-04-18 15:54:13 -0500246 {
247 $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>)';
248 }
Razicanc24f49b2011-04-25 13:43:57 +0200249
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300250 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
251 ."\n"
252 .'<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_database')
Andrey Andreev55a8c622012-11-06 13:31:21 +0200253 .':&nbsp; '.$db->database.' ('.$name.')&nbsp;&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries')
Andrey Andreeva107a0f2013-02-15 22:30:31 +0200254 .': '.count($db->queries).' ('.$total_time.')&nbsp;&nbsp;'.$show_hide_js."</legend>\n\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300255 .'<table style="width:100%;'.$hide_queries.'" id="ci_profiler_queries_db_'.$count."\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200256
Andrey Andreev195d3112011-12-25 03:59:13 +0200257 if (count($db->queries) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300259 $output .= '<tr><td style="width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;">'
260 .$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 }
262 else
Barry Mienydd671972010-10-04 16:33:58 +0200263 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 foreach ($db->queries as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200265 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 $time = number_format($db->query_times[$key], 4);
Rasmus Lerdorf164a1f22013-05-18 10:29:56 -0400267 $val = highlight_code($val);
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 foreach ($highlight as $bold)
270 {
Barry Mienydd671972010-10-04 16:33:58 +0200271 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
Barry Mienydd671972010-10-04 16:33:58 +0200273
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300274 $output .= '<tr><td style="padding:5px;vertical-align:top;width:1%;color:#900;font-weight:normal;background-color:#ddd;">'
275 .$time.'&nbsp;&nbsp;</td><td style="padding:5px;color:#000;font-weight:normal;background-color:#ddd;">'
276 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 }
278 }
Barry Mienydd671972010-10-04 16:33:58 +0200279
Andrey Andreev195d3112011-12-25 03:59:13 +0200280 $output .= "</table>\n</fieldset>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 return $output;
284 }
285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 // --------------------------------------------------------------------
287
288 /**
289 * Compile $_GET Data
290 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200292 */
Greg Aker5ac55942010-11-10 14:59:47 -0600293 protected function _compile_get()
Barry Mienydd671972010-10-04 16:33:58 +0200294 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300295 $output = "\n\n"
296 .'<fieldset id="ci_profiler_get" style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
297 ."\n"
298 .'<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data')."&nbsp;&nbsp;</legend>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200299
Andrey Andreev195d3112011-12-25 03:59:13 +0200300 if (count($_GET) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300302 $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 +0000303 }
304 else
305 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300306 $output .= "\n\n<table style=\"width:100%;border:none;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 foreach ($_GET as $key => $val)
309 {
David Cox Jrabcb67c2013-10-16 14:43:52 -0400310 is_int($key) OR $key = "'".$key."'";
Barry Mienydd671972010-10-04 16:33:58 +0200311
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300312 $output .= '<tr><td style="width:50%;color:#000;background-color:#ddd;padding:5px;">&#36;_GET['
313 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#cd6e00;font-weight:normal;background-color:#ddd;">'
314 .((is_array($val) OR is_object($val)) ? '<pre>'.htmlspecialchars(stripslashes(print_r($val, TRUE))).'</pre>' : htmlspecialchars(stripslashes($val)))
315 ."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 $output .= "</table>\n";
319 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000320
Andrey Andreev195d3112011-12-25 03:59:13 +0200321 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 /**
327 * Compile $_POST Data
328 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200330 */
Greg Aker5ac55942010-11-10 14:59:47 -0600331 protected function _compile_post()
Barry Mienydd671972010-10-04 16:33:58 +0200332 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200333 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300334 .'<fieldset id="ci_profiler_post" style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
335 ."\n"
336 .'<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data')."&nbsp;&nbsp;</legend>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200337
David Cox Jrabcb67c2013-10-16 14:43:52 -0400338 if (count($_POST) === 0 && count($_FILES) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300340 $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 +0000341 }
342 else
343 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300344 $output .= "\n\n<table style=\"width:100%;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 foreach ($_POST as $key => $val)
347 {
David Cox Jrabcb67c2013-10-16 14:43:52 -0400348 is_int($key) OR $key = "'".$key."'";
Barry Mienydd671972010-10-04 16:33:58 +0200349
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300350 $output .= '<tr><td style="width:50%;padding:5px;color:#000;background-color:#ddd;">&#36;_POST['
351 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;">';
352
Andrey Andreev963386b2012-03-02 01:52:01 +0200353 if (is_array($val) OR is_object($val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300355 $output .= '<pre>'.htmlspecialchars(stripslashes(print_r($val, TRUE))).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
357 else
358 {
359 $output .= htmlspecialchars(stripslashes($val));
360 }
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 $output .= "</td></tr>\n";
363 }
Barry Mienydd671972010-10-04 16:33:58 +0200364
David Cox Jr06fa7392013-09-25 01:56:10 -0400365 foreach ($_FILES as $key => $val)
366 {
David Cox Jrabcb67c2013-10-16 14:43:52 -0400367 is_int($key) OR $key = "'".$key."'";
David Cox Jr06fa7392013-09-25 01:56:10 -0400368
369 $output .= '<tr><td style="width:50%;padding:5px;color:#000;background-color:#ddd;">&#36;_FILES['
370 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;">';
371
372 if (is_array($val) OR is_object($val))
373 {
374 $output .= '<pre>'.htmlspecialchars(stripslashes(print_r($val, TRUE))).'</pre>';
375 }
376
377 $output .= "</td></tr>\n";
378 }
379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 $output .= "</table>\n";
381 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000382
Andrey Andreev195d3112011-12-25 03:59:13 +0200383 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 /**
389 * Show query string
390 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200392 */
Greg Aker5ac55942010-11-10 14:59:47 -0600393 protected function _compile_uri_string()
Barry Mienydd671972010-10-04 16:33:58 +0200394 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200395 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300396 .'<fieldset id="ci_profiler_uri_string" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
397 ."\n"
398 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string')."&nbsp;&nbsp;</legend>\n"
399 .'<div style="color:#000;font-weight:normal;padding:4px 0 4px 0;">'
Alex Bilbied261b1e2012-06-02 11:12:16 +0100400 .($this->CI->uri->uri_string === '' ? $this->CI->lang->line('profiler_no_uri') : $this->CI->uri->uri_string)
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300401 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
403
404 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 /**
407 * Show the controller and function that were called
408 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200410 */
Greg Aker5ac55942010-11-10 14:59:47 -0600411 protected function _compile_controller_info()
Barry Mienydd671972010-10-04 16:33:58 +0200412 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200413 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300414 .'<fieldset id="ci_profiler_controller_info" style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
415 ."\n"
416 .'<legend style="color:#995300;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_controller_info')."&nbsp;&nbsp;</legend>\n"
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300417 .'<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 +0300418 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
Derek Allard76af4092010-01-16 19:20:49 +0000420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 /**
424 * Compile memory usage
425 *
426 * Display total used memory
427 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * @return string
429 */
Greg Aker5ac55942010-11-10 14:59:47 -0600430 protected function _compile_memory_usage()
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200432 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300433 .'<fieldset id="ci_profiler_memory_usage" style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
434 ."\n"
435 .'<legend style="color:#5a0099;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage')."&nbsp;&nbsp;</legend>\n"
436 .'<div style="color:#5a0099;font-weight:normal;padding:4px 0 4px 0;">'
Andrey Andreevc839d282012-06-07 14:35:27 +0300437 .(($usage = memory_get_usage()) != '' ? number_format($usage).' bytes' : $this->CI->lang->line('profiler_no_memory'))
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300438 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
440
441 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 /**
Derek Allard76af4092010-01-16 19:20:49 +0000444 * Compile header information
445 *
446 * Lists HTTP headers
447 *
Derek Allard76af4092010-01-16 19:20:49 +0000448 * @return string
449 */
Greg Aker5ac55942010-11-10 14:59:47 -0600450 protected function _compile_http_headers()
Derek Allard76af4092010-01-16 19:20:49 +0000451 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200452 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300453 .'<fieldset id="ci_profiler_http_headers" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
454 ."\n"
455 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_headers')
456 .'&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"
457 .'<table style="width:100%;display:none;" id="ci_profiler_httpheaders_table">'."\n";
Derek Allard76af4092010-01-16 19:20:49 +0000458
Iacami7c72cfc2013-06-27 12:20:48 -0300459 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 +0000460 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300461 $val = isset($_SERVER[$header]) ? $_SERVER[$header] : '';
462 $output .= '<tr><td style="vertical-align:top;width:50%;padding:5px;color:#900;background-color:#ddd;">'
463 .$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 +0000464 }
465
Andrey Andreev195d3112011-12-25 03:59:13 +0200466 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000467 }
468
469 // --------------------------------------------------------------------
470
471 /**
472 * Compile config information
473 *
474 * Lists developer config variables
475 *
Derek Allard76af4092010-01-16 19:20:49 +0000476 * @return string
477 */
Greg Aker5ac55942010-11-10 14:59:47 -0600478 protected function _compile_config()
Derek Allard76af4092010-01-16 19:20:49 +0000479 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200480 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300481 .'<fieldset id="ci_profiler_config" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
482 ."\n"
483 .'<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"
484 .'<table style="width:100%;display:none;" id="ci_profiler_config_table">'."\n";
Derek Allard76af4092010-01-16 19:20:49 +0000485
Andrey Andreev963386b2012-03-02 01:52:01 +0200486 foreach ($this->CI->config->config as $config => $val)
Derek Allard76af4092010-01-16 19:20:49 +0000487 {
Andrey Andreev963386b2012-03-02 01:52:01 +0200488 if (is_array($val) OR is_object($val))
Greg Akere6026832010-04-29 13:41:39 -0500489 {
490 $val = print_r($val, TRUE);
491 }
Barry Mienydd671972010-10-04 16:33:58 +0200492
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300493 $output .= '<tr><td style="padding:5px;vertical-align:top;color:#900;background-color:#ddd;">'
494 .$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 +0000495 }
496
Andrey Andreev195d3112011-12-25 03:59:13 +0200497 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000498 }
499
500 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200501
Derek Allard76af4092010-01-16 19:20:49 +0000502 /**
Greg Aker62df1312011-04-18 11:18:02 -0500503 * Compile session userdata
504 *
505 * @return string
506 */
yterajima8310c9a2011-08-22 07:26:54 +0900507 protected function _compile_session_data()
Greg Aker62df1312011-04-18 11:18:02 -0500508 {
509 if ( ! isset($this->CI->session))
510 {
511 return;
512 }
513
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300514 $output = '<fieldset id="ci_profiler_csession" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
515 .'<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>'
516 .'<table style="width:100%;display:none;" id="ci_profiler_session_data">';
Greg Aker62df1312011-04-18 11:18:02 -0500517
518 foreach ($this->CI->session->all_userdata() as $key => $val)
519 {
Andrey Andreev963386b2012-03-02 01:52:01 +0200520 if (is_array($val) OR is_object($val))
Greg Aker62df1312011-04-18 11:18:02 -0500521 {
522 $val = print_r($val, TRUE);
523 }
524
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300525 $output .= '<tr><td style="padding:5px;vertical-align:top;color:#900;background-color:#ddd;">'
526 .$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 -0500527 }
528
Andrey Andreev195d3112011-12-25 03:59:13 +0200529 return $output."</table>\n</fieldset>";
Greg Aker62df1312011-04-18 11:18:02 -0500530 }
531
532 // --------------------------------------------------------------------
533
534 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 * Run the Profiler
536 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200538 */
Greg Aker5ac55942010-11-10 14:59:47 -0600539 public function run()
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300541 $output = '<div id="codeigniter_profiler" style="clear:both;background-color:#fff;padding:10px;">';
Derek Jonesee71c802010-03-10 10:05:05 -0600542 $fields_displayed = 0;
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Jonesee71c802010-03-10 10:05:05 -0600544 foreach ($this->_available_sections as $section)
545 {
546 if ($this->_compile_{$section} !== FALSE)
547 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300548 $func = '_compile_'.$section;
Derek Jonesee71c802010-03-10 10:05:05 -0600549 $output .= $this->{$func}();
550 $fields_displayed++;
551 }
552 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000553
Andrey Andreev195d3112011-12-25 03:59:13 +0200554 if ($fields_displayed === 0)
Derek Jonesee71c802010-03-10 10:05:05 -0600555 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300556 $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee;">'
557 .$this->CI->lang->line('profiler_no_profiles').'</p>';
Derek Jonesee71c802010-03-10 10:05:05 -0600558 }
Barry Mienydd671972010-10-04 16:33:58 +0200559
Andrey Andreev195d3112011-12-25 03:59:13 +0200560 return $output.'</div>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 }
Andrey Andreev56454792012-05-17 14:32:19 +0300562
Derek Allard2067d1a2008-11-13 22:59:24 +0000563}
564
Derek Allard2067d1a2008-11-13 22:59:24 +0000565/* End of file Profiler.php */
Andrey Andreevc5a7c5f2013-07-17 20:10:09 +0300566/* Location: ./system/libraries/Profiler.php */