1<?php
2/*
3+ ----------------------------------------------------------------------------+
4|     e107 website system
5|
6|     Copyright (C) 2008-2009 e107 Inc (e107.org)
7|     http://e107.org
8|
9|
10|     Released under the terms and conditions of the
11|     GNU General Public License (http://gnu.org).
12|
13|     $Source: /cvs_backup/e107_0.8/e107_handlers/debug_handler.php,v $
14|     $Revision$
15|     $Date$
16|     $Author$
17+----------------------------------------------------------------------------+
18*/
19
20//
21// IMPORTANT Info for devs who want to add to and/or use debug definitions!
22//
23// MAKING NEW DEBUG DEFS
24// The debug levels are Single Bit Binary Values. i.e, 1,2,4,8,16...
25// In the table below, if you want to define a new value:
26// - If it is debug info ALL devs will often want, then pick one of
27//   the remaining "FILLIN" items and give it the name and definition you need
28// - If it is a detail item not often used, simply add yours to the end of the
29//   list, multiplying the previous value by 2 to get the the next 'bit' number
30// - In either case, create one or more shortcut/abbreviations in $aDebugShortcuts
31//   to make it easy for dev's to specify the new display item.
32//
33
34
35if (!defined('e107_INIT')) { exit; }
36
37
38
39class e107_debug {
40
41	private static $debug_level = 0;
42
43    /* DEBUG shortcuts */
44	private static $aDebugShortcuts = array(
45		'all'		 	  => 255,     // all basics
46		'basic'			=> 255,     // all basics
47		'b'				  => 255,     // all basics
48		'warn'			=> 1,       // just php warnings, parse errrors, debug log, etc
49		'showsql'		=> 2,       // sql basics
50		'counts'		=> 4,       // traffic counters
51
52		'detail'		=> 16740351,   // (0+0xfffff-32768-4096) all details, except notice and inline sc
53		'd' 			  => 16740351,   // all details, except notice and inline sc
54		'time' 			=> 256,     // time details and php errors
55		'sql' 			=> 512,     // sql details and php errors
56		'paths' 		=> 1024,		// dump path strings
57		'bbsc' 			=> 2048,		// show bb and sc details
58		'sc'			  => 4096,   		// Shortcode paths dumped inline
59		'backtrace' => 8192,		// show backtrace when PHP has errors
60		'deprecated'	=> 16384,   // show if code is using deprecated functions
61		'notice'		=> 32768,   // detailed notice error messages?
62		'inc'       =>  65536,  // include files
63		'everything'=> 16773119,   //(0+0xffffff-4096) 24 bits set, except shortcode paths
64														// removed: inline debug breaks pages!
65	);
66
67	function __construct()
68	{
69
70	}
71
72    public static function activated()
73    {
74        if ((strstr(e_MENU, "debug") || isset($_COOKIE['e107_debug_level'])) || deftrue('e_DEBUG')) // ADMIN and getperms('0') are not available at this point.
75        {
76            return true;
77        }
78
79        return false;
80    }
81
82
83
84	public static function init()
85    {
86        if(!self::activated())
87        {
88            self::setConstants();
89            return false;
90        }
91
92        if (preg_match('/debug(=?)(.*?),?(\+|stick|-|unstick|$)/', e_MENU, $debug_param) || isset($_COOKIE['e107_debug_level']))
93        {
94            $dVals = '';
95            if (!isset($debug_param[1]) || ($debug_param[1] == '')) $debug_param[1] = '=';
96            if (isset($_COOKIE['e107_debug_level']))
97            {
98                $dVals = substr($_COOKIE['e107_debug_level'], 6);
99            }
100
101            if (preg_match('/debug(=?)(.*?),?(\+|stick|-|unstick|$)/', e_MENU))
102            {
103                $dVals = $debug_param[1] == '=' ? $debug_param[2] : 'everything';
104            }
105
106            $aDVal = explode('.', $dVals); // support multiple values, OR'd together
107            $dVal = 0;
108
109
110            foreach ($aDVal as $curDVal)
111            {
112                if (isset(self::$aDebugShortcuts[$curDVal]))
113                {
114                    $dVal |= self::$aDebugShortcuts[$curDVal];
115                }
116                else
117                {
118                    $dVal |= intval($curDVal);
119                }
120            }
121
122            if (isset($debug_param[3]))
123            {
124                if ($debug_param[3] == '+' || $debug_param[3] == 'stick')
125                {
126                    cookie('e107_debug_level', 'level=' . $dVal, time() + 86400);
127                }
128
129                if ($debug_param[3] == '-' || $debug_param[3] == 'unstick')
130                {
131                    cookie('e107_debug_level', '', time() - 3600);
132                }
133            }
134
135            self::$debug_level = $dVal;
136        }
137
138        self::setConstants();
139
140        return true;
141    }
142
143
144    /**
145     * Define all debug constants -- each one will be zero or a value
146     * USING DEBUG DEFINITIONS
147     * Since these are Bit Values, **never** test using < or > comparisons. Always
148     * test using boolean operations, such as
149     * @example if (E107_DBG_PATH)
150     * @example if (E107_DBG_SQLQUERIES | E107_DBG_SQLDETAILS)
151     * Since constants are defined for all possible bits, you should never need to use a number value like
152     * @example if (E107_DEBUG_LEVEL & 256)
153     * And there's never a reason to use
154     * if (E107_DEBUG_LEVEL > 254)
155     */
156	private static function setConstants()
157    {
158
159        if(!defined('E107_DEBUG_LEVEL'))
160        {
161            define('E107_DEBUG_LEVEL', self::getLevel());
162        }
163
164        // Basic levels
165        define('E107_DBG_BASIC',		(E107_DEBUG_LEVEL & 1));       // basics: worst php errors, sql errors, log, etc
166        define('E107_DBG_SQLQUERIES',	(E107_DEBUG_LEVEL & 2));       // display all sql queries
167        define('E107_DBG_TRAFFIC',		(E107_DEBUG_LEVEL & 4));       // display traffic counters
168        define('E107_DBG_FILLIN8',		(E107_DEBUG_LEVEL & 8));       // fill in what it is
169        define('E107_DBG_FILLIN16',		(E107_DEBUG_LEVEL & 16));      // fill in what it is
170        define('E107_DBG_FILLIN32',		(E107_DEBUG_LEVEL & 32));      // fill in what it is
171        define('E107_DBG_FILLIN64',		(E107_DEBUG_LEVEL & 64));      // fill in what it is
172        define('E107_DBG_FILLIN128',	(E107_DEBUG_LEVEL & 128));     // fill in what it is
173
174        // Gory detail levels
175        define('E107_DBG_TIMEDETAILS',(E107_DEBUG_LEVEL &   256));    // detailed time profile
176        define('E107_DBG_SQLDETAILS',	(E107_DEBUG_LEVEL &   512));    // detailed sql analysis
177        define('E107_DBG_PATH',     	(E107_DEBUG_LEVEL &  1024));    // show e107 predefined paths
178        define('E107_DBG_BBSC',     	(E107_DEBUG_LEVEL &  2048));    // Show BBCode/ Shortcode usage in postings
179        define('E107_DBG_SC',       	(E107_DEBUG_LEVEL &  4096));    // Dump (inline) SC filenames as used
180        define('E107_DBG_ERRBACKTRACE',	(E107_DEBUG_LEVEL &  8192));    // show backtrace for php errors
181        define('E107_DBG_DEPRECATED', (E107_DEBUG_LEVEL & 16384));    // Show use of deprecated functions
182        define('E107_DBG_ALLERRORS',	(E107_DEBUG_LEVEL & 32768));    // show ALL php errors (including notices), not just fatal issues
183        define('E107_DBG_INCLUDES',   (E107_DEBUG_LEVEL & 65536));    // show included file list
184        define('E107_DBG_NOTICES',   (E107_DEBUG_LEVEL & 32768));    // show included file list
185
186        if(!defined('e_DEBUG'))
187        {
188            $e_DEBUG = (E107_DEBUG_LEVEL > 0);
189            define('e_DEBUG', $e_DEBUG);
190        }
191
192    }
193
194    public static function getLevel()
195    {
196        return self::$debug_level;
197    }
198
199    public static function setLevel($level = 0)
200    {
201       self::$debug_level = $level;
202    }
203
204
205	function set_error_reporting()
206	{
207	}
208}
209
210// Quick debug message logger
211// Example: e7debug(__FILE__.__LINE__.": myVar is ".print_r($myVar,TRUE));
212/*
213function e7debug($message,$TraceLev=1)
214{
215  if (!E107_DEBUG_LEVEL) return;
216	global $db_debug;
217	if (is_object($db_debug))
218	{
219		$db_debug->log($message,$TraceLev);
220	}
221}
222*/
223