blob: 6320ab50de993a8376be18f5d280dfbee9ea7c0d [file] [log] [blame]
Andrey Andreev195d3112011-12-25 03:59:13 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter Profiler Class
30 *
31 * This class enables you to display benchmark, query, and other data
32 * in order to help with debugging and optimization.
33 *
34 * Note: At some point it would be good to move all the HTML in this class
35 * into a set of template files in order to allow customization.
36 *
37 * @package CodeIgniter
38 * @subpackage Libraries
39 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/general/profiling.html
42 */
43class CI_Profiler {
44
Greg Aker5ac55942010-11-10 14:59:47 -060045 protected $_available_sections = array(
Andrey Andreev0d1c0a72012-04-03 16:59:54 +030046 'benchmarks',
47 'get',
48 'memory_usage',
49 'post',
50 'uri_string',
51 'controller_info',
52 'queries',
53 'http_headers',
54 'session_data',
55 'config'
56 );
Razicanc24f49b2011-04-25 13:43:57 +020057
Greg Akere6e6e642011-04-18 15:54:13 -050058 protected $_query_toggle_count = 25;
Razicanc24f49b2011-04-25 13:43:57 +020059
60 protected $CI;
Derek Jonesee71c802010-03-10 10:05:05 -060061
Greg Aker5ac55942010-11-10 14:59:47 -060062 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +020063 {
64 $this->CI =& get_instance();
65 $this->CI->load->language('profiler');
66
Greg Akere6e6e642011-04-18 15:54:13 -050067 if (isset($config['query_toggle_count']))
68 {
69 $this->_query_toggle_count = (int) $config['query_toggle_count'];
70 unset($config['query_toggle_count']);
71 }
72
Derek Jonesee71c802010-03-10 10:05:05 -060073 // default all sections to display
74 foreach ($this->_available_sections as $section)
75 {
76 if ( ! isset($config[$section]))
77 {
78 $this->_compile_{$section} = TRUE;
79 }
80 }
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Jonesee71c802010-03-10 10:05:05 -060082 $this->set_sections($config);
Barry Mienydd671972010-10-04 16:33:58 +020083 }
84
Derek Allard2067d1a2008-11-13 22:59:24 +000085 // --------------------------------------------------------------------
86
87 /**
Derek Jonesee71c802010-03-10 10:05:05 -060088 * Set Sections
89 *
90 * Sets the private _compile_* properties to enable/disable Profiler sections
91 *
Derek Jonesee71c802010-03-10 10:05:05 -060092 * @param mixed
93 * @return void
94 */
Greg Aker5ac55942010-11-10 14:59:47 -060095 public function set_sections($config)
Derek Jonesee71c802010-03-10 10:05:05 -060096 {
97 foreach ($config as $method => $enable)
98 {
99 if (in_array($method, $this->_available_sections))
100 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300101 $this->_compile_{$method} = ($enable !== FALSE);
Derek Jonesee71c802010-03-10 10:05:05 -0600102 }
103 }
104 }
105
106 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Jonesee71c802010-03-10 10:05:05 -0600108 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 * Auto Profiler
110 *
111 * This function cycles through the entire array of mark points and
112 * matches any two points that are named identically (ending in "_start"
Derek Jones4b9c6292011-07-01 17:40:48 -0500113 * and "_end" respectively). It then compiles the execution times for
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 * all points and returns it as an array
Greg Aker5ac55942010-11-10 14:59:47 -0600115 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 * @return array
117 */
Greg Aker5ac55942010-11-10 14:59:47 -0600118 protected function _compile_benchmarks()
Barry Mienydd671972010-10-04 16:33:58 +0200119 {
120 $profile = array();
121 foreach ($this->CI->benchmark->marker as $key => $val)
122 {
123 // We match the "end" marker so that the list ends
124 // up in the order that it was defined
Andrey Andreev195d3112011-12-25 03:59:13 +0200125 if (preg_match('/(.+?)_end/i', $key, $match)
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300126 && isset($this->CI->benchmark->marker[$match[1].'_end'], $this->CI->benchmark->marker[$match[1].'_start']))
Barry Mienydd671972010-10-04 16:33:58 +0200127 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200128 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
Barry Mienydd671972010-10-04 16:33:58 +0200129 }
130 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000131
132 // Build a table containing the profile data.
133 // Note: At some point we should turn this into a template that can
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300134 // be modified. We also might want to make this data available to be logged
Barry Mienydd671972010-10-04 16:33:58 +0200135
Andrey Andreev195d3112011-12-25 03:59:13 +0200136 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300137 .'<fieldset id="ci_profiler_benchmarks" style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
138 ."\n"
139 .'<legend style="color:#900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks')."&nbsp;&nbsp;</legend>"
140 ."\n\n\n<table style=\"width:100%;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 foreach ($profile as $key => $val)
143 {
144 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300145 $output .= '<tr><td style="padding:5px;width:50%;color:#000;font-weight:bold;background-color:#ddd;">'
146 .$key.'&nbsp;&nbsp;</td><td style="padding:5px;width:50%;color:#900;font-weight:normal;background-color:#ddd;">'
147 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149
Andrey Andreev195d3112011-12-25 03:59:13 +0200150 return $output."</table>\n</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200151 }
152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 // --------------------------------------------------------------------
154
155 /**
156 * Compile Queries
157 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200159 */
Greg Aker5ac55942010-11-10 14:59:47 -0600160 protected function _compile_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
162 $dbs = array();
Derek Jonesf0a9b332009-07-29 14:19:18 +0000163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 // Let's determine which databases are currently connected to
165 foreach (get_object_vars($this->CI) as $CI_object)
166 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300167 if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
169 $dbs[] = $CI_object;
170 }
171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Andrey Andreev195d3112011-12-25 03:59:13 +0200173 if (count($dbs) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200175 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300176 .'<fieldset id="ci_profiler_queries" style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
177 ."\n"
178 .'<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>'
179 ."\n\n\n<table style=\"border:none; width:100%;\">\n"
180 .'<tr><td style="width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;">'
181 .$this->CI->lang->line('profiler_no_db')
182 ."</td></tr>\n</table>\n</fieldset>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 // Load the text helper so we can highlight the SQL
186 $this->CI->load->helper('text');
187
188 // Key words we want bolded
Greg Aker3424bf72010-09-14 17:53:10 -0500189 $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 +0000190
Derek Jones4b9c6292011-07-01 17:40:48 -0500191 $output = "\n\n";
Greg Akere6e6e642011-04-18 15:54:13 -0500192 $count = 0;
Razicanc24f49b2011-04-25 13:43:57 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 foreach ($dbs as $db)
195 {
Greg Akere6e6e642011-04-18 15:54:13 -0500196 $hide_queries = (count($db->queries) > $this->_query_toggle_count) ? ' display:none' : '';
Razicanc24f49b2011-04-25 13:43:57 +0200197
Greg Akere6e6e642011-04-18 15:54:13 -0500198 $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 +0200199
Greg Akere6e6e642011-04-18 15:54:13 -0500200 if ($hide_queries != '')
201 {
202 $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>)';
203 }
Razicanc24f49b2011-04-25 13:43:57 +0200204
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300205 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
206 ."\n"
207 .'<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_database')
208 .':&nbsp; '.$db->database.'&nbsp;&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries')
209 .': '.count($db->queries).'&nbsp;&nbsp;'.$show_hide_js."</legend>\n\n\n"
210 .'<table style="width:100%;'.$hide_queries.'" id="ci_profiler_queries_db_'.$count."\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200211
Andrey Andreev195d3112011-12-25 03:59:13 +0200212 if (count($db->queries) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300214 $output .= '<tr><td style="width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;">'
215 .$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 }
217 else
Barry Mienydd671972010-10-04 16:33:58 +0200218 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 foreach ($db->queries as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200220 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 $time = number_format($db->query_times[$key], 4);
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 $val = highlight_code($val, ENT_QUOTES);
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 foreach ($highlight as $bold)
225 {
Barry Mienydd671972010-10-04 16:33:58 +0200226 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300229 $output .= '<tr><td style="padding:5px;vertical-align:top;width:1%;color:#900;font-weight:normal;background-color:#ddd;">'
230 .$time.'&nbsp;&nbsp;</td><td style="padding:5px;color:#000;font-weight:normal;background-color:#ddd;">'
231 .$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Andrey Andreev195d3112011-12-25 03:59:13 +0200235 $output .= "</table>\n</fieldset>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 }
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 return $output;
239 }
240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 // --------------------------------------------------------------------
242
243 /**
244 * Compile $_GET Data
245 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200247 */
Greg Aker5ac55942010-11-10 14:59:47 -0600248 protected function _compile_get()
Barry Mienydd671972010-10-04 16:33:58 +0200249 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300250 $output = "\n\n"
251 .'<fieldset id="ci_profiler_get" style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
252 ."\n"
253 .'<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data')."&nbsp;&nbsp;</legend>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200254
Andrey Andreev195d3112011-12-25 03:59:13 +0200255 if (count($_GET) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300257 $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 +0000258 }
259 else
260 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300261 $output .= "\n\n<table style=\"width:100%;border:none;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 foreach ($_GET as $key => $val)
264 {
265 if ( ! is_numeric($key))
266 {
267 $key = "'".$key."'";
268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300270 $output .= '<tr><td style="width:50%;color:#000;background-color:#ddd;padding:5px;">&#36;_GET['
271 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#cd6e00;font-weight:normal;background-color:#ddd;">'
272 .((is_array($val) OR is_object($val)) ? '<pre>'.htmlspecialchars(stripslashes(print_r($val, TRUE))).'</pre>' : htmlspecialchars(stripslashes($val)))
273 ."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 $output .= "</table>\n";
277 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000278
Andrey Andreev195d3112011-12-25 03:59:13 +0200279 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 /**
285 * Compile $_POST Data
286 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200288 */
Greg Aker5ac55942010-11-10 14:59:47 -0600289 protected function _compile_post()
Barry Mienydd671972010-10-04 16:33:58 +0200290 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200291 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300292 .'<fieldset id="ci_profiler_post" style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
293 ."\n"
294 .'<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data')."&nbsp;&nbsp;</legend>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 if (count($_POST) == 0)
297 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300298 $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 +0000299 }
300 else
301 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300302 $output .= "\n\n<table style=\"width:100%;\">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 foreach ($_POST as $key => $val)
305 {
306 if ( ! is_numeric($key))
307 {
308 $key = "'".$key."'";
309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300311 $output .= '<tr><td style="width:50%;padding:5px;color:#000;background-color:#ddd;">&#36;_POST['
312 .$key.']&nbsp;&nbsp; </td><td style="width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;">';
313
Andrey Andreev963386b2012-03-02 01:52:01 +0200314 if (is_array($val) OR is_object($val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300316 $output .= '<pre>'.htmlspecialchars(stripslashes(print_r($val, TRUE))).'</pre>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
318 else
319 {
320 $output .= htmlspecialchars(stripslashes($val));
321 }
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 $output .= "</td></tr>\n";
324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 $output .= "</table>\n";
327 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000328
Andrey Andreev195d3112011-12-25 03:59:13 +0200329 return $output.'</fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 /**
335 * Show query string
336 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200338 */
Greg Aker5ac55942010-11-10 14:59:47 -0600339 protected function _compile_uri_string()
Barry Mienydd671972010-10-04 16:33:58 +0200340 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200341 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300342 .'<fieldset id="ci_profiler_uri_string" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
343 ."\n"
344 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string')."&nbsp;&nbsp;</legend>\n"
345 .'<div style="color:#000;font-weight:normal;padding:4px 0 4px 0;">'
346 .($this->CI->uri->uri_string == '' ? $this->CI->lang->line('profiler_no_uri') : $this->CI->uri->uri_string)
347 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
349
350 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 /**
353 * Show the controller and function that were called
354 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200356 */
Greg Aker5ac55942010-11-10 14:59:47 -0600357 protected function _compile_controller_info()
Barry Mienydd671972010-10-04 16:33:58 +0200358 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200359 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300360 .'<fieldset id="ci_profiler_controller_info" style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
361 ."\n"
362 .'<legend style="color:#995300;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_controller_info')."&nbsp;&nbsp;</legend>\n"
363 .'<div style="color:#995300;font-weight:normal;padding:4px 0 4px 0;">'.$this->CI->router->fetch_class().'/'.$this->CI->router->fetch_method()
364 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
Derek Allard76af4092010-01-16 19:20:49 +0000366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 /**
370 * Compile memory usage
371 *
372 * Display total used memory
373 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 * @return string
375 */
Greg Aker5ac55942010-11-10 14:59:47 -0600376 protected function _compile_memory_usage()
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200378 return "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300379 .'<fieldset id="ci_profiler_memory_usage" style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
380 ."\n"
381 .'<legend style="color:#5a0099;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage')."&nbsp;&nbsp;</legend>\n"
382 .'<div style="color:#5a0099;font-weight:normal;padding:4px 0 4px 0;">'
383 .((function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '') ? number_format($usage).' bytes' : $this->CI->lang->line('profiler_no_memory'))
384 .'</div></fieldset>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
386
387 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 /**
Derek Allard76af4092010-01-16 19:20:49 +0000390 * Compile header information
391 *
392 * Lists HTTP headers
393 *
Derek Allard76af4092010-01-16 19:20:49 +0000394 * @return string
395 */
Greg Aker5ac55942010-11-10 14:59:47 -0600396 protected function _compile_http_headers()
Derek Allard76af4092010-01-16 19:20:49 +0000397 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200398 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300399 .'<fieldset id="ci_profiler_http_headers" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
400 ."\n"
401 .'<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_headers')
402 .'&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"
403 .'<table style="width:100%;display:none;" id="ci_profiler_httpheaders_table">'."\n";
Derek Allard76af4092010-01-16 19:20:49 +0000404
Pascal Kriete14287f32011-02-14 13:39:34 -0500405 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') as $header)
Derek Allard76af4092010-01-16 19:20:49 +0000406 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300407 $val = isset($_SERVER[$header]) ? $_SERVER[$header] : '';
408 $output .= '<tr><td style="vertical-align:top;width:50%;padding:5px;color:#900;background-color:#ddd;">'
409 .$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 +0000410 }
411
Andrey Andreev195d3112011-12-25 03:59:13 +0200412 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000413 }
414
415 // --------------------------------------------------------------------
416
417 /**
418 * Compile config information
419 *
420 * Lists developer config variables
421 *
Derek Allard76af4092010-01-16 19:20:49 +0000422 * @return string
423 */
Greg Aker5ac55942010-11-10 14:59:47 -0600424 protected function _compile_config()
Derek Allard76af4092010-01-16 19:20:49 +0000425 {
Andrey Andreev195d3112011-12-25 03:59:13 +0200426 $output = "\n\n"
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300427 .'<fieldset id="ci_profiler_config" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
428 ."\n"
429 .'<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"
430 .'<table style="width:100%;display:none;" id="ci_profiler_config_table">'."\n";
Derek Allard76af4092010-01-16 19:20:49 +0000431
Andrey Andreev963386b2012-03-02 01:52:01 +0200432 foreach ($this->CI->config->config as $config => $val)
Derek Allard76af4092010-01-16 19:20:49 +0000433 {
Andrey Andreev963386b2012-03-02 01:52:01 +0200434 if (is_array($val) OR is_object($val))
Greg Akere6026832010-04-29 13:41:39 -0500435 {
436 $val = print_r($val, TRUE);
437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300439 $output .= '<tr><td style="padding:5px;vertical-align:top;color:#900;background-color:#ddd;">'
440 .$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 +0000441 }
442
Andrey Andreev195d3112011-12-25 03:59:13 +0200443 return $output."</table>\n</fieldset>";
Derek Allard76af4092010-01-16 19:20:49 +0000444 }
445
446 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard76af4092010-01-16 19:20:49 +0000448 /**
Greg Aker62df1312011-04-18 11:18:02 -0500449 * Compile session userdata
450 *
451 * @return string
452 */
yterajima8310c9a2011-08-22 07:26:54 +0900453 protected function _compile_session_data()
Greg Aker62df1312011-04-18 11:18:02 -0500454 {
455 if ( ! isset($this->CI->session))
456 {
457 return;
458 }
459
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300460 $output = '<fieldset id="ci_profiler_csession" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee;">'
461 .'<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>'
462 .'<table style="width:100%;display:none;" id="ci_profiler_session_data">';
Greg Aker62df1312011-04-18 11:18:02 -0500463
464 foreach ($this->CI->session->all_userdata() as $key => $val)
465 {
Andrey Andreev963386b2012-03-02 01:52:01 +0200466 if (is_array($val) OR is_object($val))
Greg Aker62df1312011-04-18 11:18:02 -0500467 {
468 $val = print_r($val, TRUE);
469 }
470
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300471 $output .= '<tr><td style="padding:5px;vertical-align:top;color:#900;background-color:#ddd;">'
472 .$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 -0500473 }
474
Andrey Andreev195d3112011-12-25 03:59:13 +0200475 return $output."</table>\n</fieldset>";
Greg Aker62df1312011-04-18 11:18:02 -0500476 }
477
478 // --------------------------------------------------------------------
479
480 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 * Run the Profiler
482 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200484 */
Greg Aker5ac55942010-11-10 14:59:47 -0600485 public function run()
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300487 $output = '<div id="codeigniter_profiler" style="clear:both;background-color:#fff;padding:10px;">';
Derek Jonesee71c802010-03-10 10:05:05 -0600488 $fields_displayed = 0;
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Jonesee71c802010-03-10 10:05:05 -0600490 foreach ($this->_available_sections as $section)
491 {
492 if ($this->_compile_{$section} !== FALSE)
493 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300494 $func = '_compile_'.$section;
Derek Jonesee71c802010-03-10 10:05:05 -0600495 $output .= $this->{$func}();
496 $fields_displayed++;
497 }
498 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000499
Andrey Andreev195d3112011-12-25 03:59:13 +0200500 if ($fields_displayed === 0)
Derek Jonesee71c802010-03-10 10:05:05 -0600501 {
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300502 $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee;">'
503 .$this->CI->lang->line('profiler_no_profiles').'</p>';
Derek Jonesee71c802010-03-10 10:05:05 -0600504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Andrey Andreev195d3112011-12-25 03:59:13 +0200506 return $output.'</div>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000508}
509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510/* End of file Profiler.php */
Andrey Andreev0d1c0a72012-04-03 16:59:54 +0300511/* Location: ./system/libraries/Profiler.php */