-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.debug.php
More file actions
437 lines (379 loc) · 10.4 KB
/
Copy pathclass.debug.php
File metadata and controls
437 lines (379 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php
namespace Sleepy;
/**
* Provides custom debugging functions.
*
* This class can send emails, log to a database, or display on screen debug
* information. You can set the enabled flags to enable the debug functions or
* set them to false to quiet them down. This way you can leave them as a part
* of your code with little overhead. For email and database logging, don't
* forget to setup the public properties.
*
* ## Usage
*
* ~~~ php
* // Turn debugging to screen on
* \Sleepy\Debug::$enable_show = true;
* \Sleepy\Debug::out("This will goto the screen because $enable_show == true");
*
* // Turn off debugging to screen
* \Sleepy\Debug::$enable_show = false;
* ~~~
*
* ## Changelog
*
* ### Version 1.10
* * Added the ability to debug straight to the JS console
* ### Version 1.9
* * Updated private suffix (_) and documentation for consistency
*
* ### Version 1.8
* * Added namespacing
*
* @date April 19, 2017
* @author Jaime A. Rodriguez <hi.i.am.jaime@gmail.com>
* @version 1.10
* @license http://opensource.org/licenses/MIT
*/
class Debug {
/**
* The single instance is stored here.
*
* @var Debug
*/
private static $_instance = NULL;
/**
* PDO Database object
*
* @var PDO
*/
private static $_dbPDO;
/**
* Enable output to JS Console
*
* @var bool
*/
public static $enable_console = false;
/**
* Enable output to screen
*
* @var bool
*/
public static $enable_show = false;
/**
* Enabled logging to a database
*
* @var bool
*/
public static $enable_log = false;
/**
* Enabled logging via email
*
* @var bool
*/
public static $enable_send = false;
/**
* Email address to send email to.
*
* @var string
*/
public static $emailTo;
/**
* Email address cc send email to.
*
* @var string
*/
public static $emailCC;
/**
* Email address bcc send email to.
*
* @var string
*/
public static $emailBCC;
/**
* Email address to send email from.
*
* @var string
*/
public static $emailFrom;
/**
* The subject of the email.
*
* @var string
*/
public static $emailSubject;
/**
* The body of the email.
*
* @var string[]
*/
public static $emailBuffer;
/**
* Database Host
*
* @var string
*/
public static $dbHost;
/**
* Database Name
*
* @var string
*/
public static $dbName;
/**
* Database User Name
*
* @var string
*/
public static $dbUser;
/**
* Database Password
*
* @var string
*/
public static $dbPass;
/**
* Database Table to use for logging
*
* @var string
*/
public static $dbTable;
/**
* Prevent class from being cloned
*
* @return void
*/
private function __clone() {}
/**
* The constructor is private to ensure we only have one instance
*
* @return void
*/
private function __construct() {
// Setup email defaults
$server_ip = (isset($_SERVER['SERVER_ADDR'])) ? $_SERVER['SERVER_ADDR'] : '';
$user_ip = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '';
$filename = (isset($_SERVER['SCRIPT_FILENAME'])) ? $_SERVER['SCRIPT_FILENAME'] : '';
$date = date(DATE_ATOM, mktime(date('G'), date('i'), 0, date('m'), date('d'), date('Y')));
Debug::$emailBuffer = array();
Debug::$emailBuffer[] = "Date: {$date}";
Debug::$emailBuffer[] = "Server IP: {$server_ip}";
Debug::$emailBuffer[] = "Client IP: {$user_ip}";
Debug::$emailBuffer[] = "Filename: {$filename}";
Debug::$emailBuffer[] = '---';
Debug::$emailTo = EMAIL_TO;
Debug::$emailFrom = EMAIL_FROM;
Debug::$emailSubject = $date;
Debug::$emailCC = EMAIL_CC;
Debug::$emailBCC = EMAIL_BCC;
// Setup logging defaults
Debug::$dbHost = DBHOST;
Debug::$dbName = DBNAME;
Debug::$dbUser = DBUSER;
Debug::$dbPass = DBPASS;
Debug::$dbTable = 'log';
}
/**
* Send the email when the page is unloaded
*
* @return void
*/
public function __destruct() {
if (self::$enable_send) {
self::sendEmail();
}
}
/**
* Return instance or create initial instance
*
* @return Debug
*/
private static function _initialize() {
if (!self::$_instance) {
self::$_instance = new Debug();
}
return self::$_instance;
}
/**
* Displays debug information in JS Console
*
* @param mixed $var Anything you want to log
* @return bool
* @todo create a hook so the dev can create custom views when outputting
* debug data.
*/
private static function console($var) {
$output = '';
if (!self::$enable_console) {
return false;
}
echo '<script>console.log(';
if (is_object($var) && \method_exists($var, '__debugInfo')) {
$output = json_encode($var->__debugInfo());
} else if (is_array($var) || is_object($var)) {
$output = json_encode($var);
} else {
$output = "'{$var}'";
}
if (class_exists('\Sleepy\Hook')) {
echo Hook::addFilter('debug_console_output', $output);
} else {
echo $output;
}
echo ');</script>';
return true;
}
/**
* sets the Exception Handler
*/
public function setHandler() {
self::_initialize();
set_exception_handler(array('Debug', 'exceptionHandler'));
}
/**
* Exception Handler
*/
public function exceptionHandler($e) {
if (headers_sent()) {
echo 'Error: ' , $e->getMessage(), "\n";
} else {
$_SESSION['exception'] = $e->getMessage() . '<br />' . str_replace("\n", '<br />', $e->getTraceAsString()) . '';
header('Location: /error/');
}
}
/**
* Writes to a database log table. The table should be called log, or set
* $this->dbTable. It should contain 2 columns: 'datetime, message'
*
* @param mixed $var Anything you want to log
* @return bool
* @todo add a create for the log table
*/
private function log($var) {
if (!self::$enable_log) {
return false;
}
if (is_array($var) || is_object($var)) {
$buffer = print_r($var, true);
} else {
$buffer = $var;
}
try {
// MySQL with PDO_MYSQL
if (!is_object(self::$_dbPDO)) {
self::$_dbPDO = new \PDO('mysql:host=' . self::$dbHost . ';dbname=' . self::$dbName, self::$dbUser, self::$dbPass);
self::$_dbPDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
$query = self::$_dbPDO->prepare('INSERT INTO ' . self::$dbTable . ' (datetime, message) values (:datetime, :message)');
$datetime = date(DATE_ATOM, mktime(date('G'), date('i'), 0, date('m'), date('d'), date('Y')));
$query->bindParam(':datetime', $datetime);
$query->bindParam(':message', $buffer);
$query->execute();
} catch(\PDOException $e) {
self::show($e->getMessage());
return false;
}
return true;
}
/**
* Displays debug information on screen
*
* @param mixed $var Anything you want to log
* @return bool
* @todo create a hook so the dev can create custom views when outputting
* debug data.
*/
private static function show($var) {
if (!self::$enable_show) {
return false;
}
echo '<pre>';
if (is_array($var) || is_object($var)) {
print_r($var);
} else {
echo $var;
}
echo '</pre>';
return true;
}
/**
* Iterates a buffer that gets emailed on __destruct()
*
* @param mixed $var
* Anything you want to log
* @return bool
*/
private static function send($var) {
if (!self::$enable_send) {
return false;
}
if (is_array($var) || is_object($var)) {
self::$emailBuffer[] = print_r($var, true);
} else {
self::$emailBuffer[] = $var;
}
return true;
}
/**
* Determines what output methods are enabled and passes $var to it.
*
* @param mixed $var Anything you want to log
* @return void
*/
public static function out($var) {
$result = true;
self::_initialize();
if (self::$enable_console) {
$result = $result && self::$_instance->console($var);
}
if (self::$enable_send) {
$result = $result && self::$_instance->send($var);
}
if (self::$enable_log) {
$result = $result && self::$_instance->log($var);
}
if (self::$enable_show) {
$result = $result && self::$_instance->show($var);
}
if (!self::$enable_console &&
!self::$enable_show &&
!self::$enable_send &&
!self::$enable_log) {
$result = false;
}
return $result;
}
/**
* Sets all the enabled flags to false
*
* @return void
*/
public static function disable() {
self::$enable_console = false;
self::$enable_log = false;
self::$enable_send = false;
self::$enable_show = false;
}
/**
* Sends the email.
*
* @return bool true if sent successfully
* @todo make this private, I cannot remember why this is public...
*/
public static function sendEmail() {
if (!self::$enable_send) {
return false;
}
$headers = array();
$headers[] = 'From: ' . self::$emailFrom;
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
if (self::$emailCC != '') {
$headers[] = 'Cc: ' . self::$emailCC;
}
if (self::$emailBCC != '') {
$headers[] = 'Bcc: ' . self::$emailBCC;
}
return mail(self::$emailTo, self::$emailSubject, implode("<br />\n", self::$emailBuffer), implode("\n", $headers));
}
}