1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20:
21: App::uses('CakeLog', 'Log');
22: App::uses('CakeText', 'Utility');
23:
24: 25: 26: 27: 28: 29: 30: 31:
32: class Debugger {
33:
34: 35: 36: 37: 38:
39: public $errors = array();
40:
41: 42: 43: 44: 45:
46: protected $_outputFormat = 'js';
47:
48: 49: 50: 51: 52: 53:
54: protected $_templates = array(
55: 'log' => array(
56: 'trace' => '{:reference} - {:path}, line {:line}',
57: 'error' => "{:error} ({:code}): {:description} in [{:file}, line {:line}]"
58: ),
59: 'js' => array(
60: 'error' => '',
61: 'info' => '',
62: 'trace' => '<pre class="stack-trace">{:trace}</pre>',
63: 'code' => '',
64: 'context' => '',
65: 'links' => array(),
66: 'escapeContext' => true,
67: ),
68: 'html' => array(
69: 'trace' => '<pre class="cake-error trace"><b>Trace</b> <p>{:trace}</p></pre>',
70: 'context' => '<pre class="cake-error context"><b>Context</b> <p>{:context}</p></pre>',
71: 'escapeContext' => true,
72: ),
73: 'txt' => array(
74: 'error' => "{:error}: {:code} :: {:description} on line {:line} of {:path}\n{:info}",
75: 'code' => '',
76: 'info' => ''
77: ),
78: 'base' => array(
79: 'traceLine' => '{:reference} - {:path}, line {:line}',
80: 'trace' => "Trace:\n{:trace}\n",
81: 'context' => "Context:\n{:context}\n",
82: )
83: );
84:
85: 86: 87: 88: 89:
90: protected $_data = array();
91:
92: 93: 94:
95: public function __construct() {
96: $docRef = ini_get('docref_root');
97:
98: if (empty($docRef) && function_exists('ini_set')) {
99: ini_set('docref_root', 'http://php.net/');
100: }
101: if (!defined('E_RECOVERABLE_ERROR')) {
102: define('E_RECOVERABLE_ERROR', 4096);
103: }
104:
105: $e = '<pre class="cake-error">';
106: $e .= '<a href="javascript:void(0);" onclick="document.getElementById(\'{:id}-trace\')';
107: $e .= '.style.display = (document.getElementById(\'{:id}-trace\').style.display == ';
108: $e .= '\'none\' ? \'\' : \'none\');"><b>{:error}</b> ({:code})</a>: {:description} ';
109: $e .= '[<b>{:path}</b>, line <b>{:line}</b>]';
110:
111: $e .= '<div id="{:id}-trace" class="cake-stack-trace" style="display: none;">';
112: $e .= '{:links}{:info}</div>';
113: $e .= '</pre>';
114: $this->_templates['js']['error'] = $e;
115:
116: $t = '<div id="{:id}-trace" class="cake-stack-trace" style="display: none;">';
117: $t .= '{:context}{:code}{:trace}</div>';
118: $this->_templates['js']['info'] = $t;
119:
120: $links = array();
121: $link = '<a href="javascript:void(0);" onclick="document.getElementById(\'{:id}-code\')';
122: $link .= '.style.display = (document.getElementById(\'{:id}-code\').style.display == ';
123: $link .= '\'none\' ? \'\' : \'none\')">Code</a>';
124: $links['code'] = $link;
125:
126: $link = '<a href="javascript:void(0);" onclick="document.getElementById(\'{:id}-context\')';
127: $link .= '.style.display = (document.getElementById(\'{:id}-context\').style.display == ';
128: $link .= '\'none\' ? \'\' : \'none\')">Context</a>';
129: $links['context'] = $link;
130:
131: $this->_templates['js']['links'] = $links;
132:
133: $this->_templates['js']['context'] = '<pre id="{:id}-context" class="cake-context" ';
134: $this->_templates['js']['context'] .= 'style="display: none;">{:context}</pre>';
135:
136: $this->_templates['js']['code'] = '<pre id="{:id}-code" class="cake-code-dump" ';
137: $this->_templates['js']['code'] .= 'style="display: none;">{:code}</pre>';
138:
139: $e = '<pre class="cake-error"><b>{:error}</b> ({:code}) : {:description} ';
140: $e .= '[<b>{:path}</b>, line <b>{:line}]</b></pre>';
141: $this->_templates['html']['error'] = $e;
142:
143: $this->_templates['html']['context'] = '<pre class="cake-context"><b>Context</b> ';
144: $this->_templates['html']['context'] .= '<p>{:context}</p></pre>';
145: }
146:
147: 148: 149: 150: 151: 152:
153: public static function getInstance($class = null) {
154: static $instance = array();
155: if (!empty($class)) {
156: if (!$instance || strtolower($class) != strtolower(get_class($instance[0]))) {
157: $instance[0] = new $class();
158: }
159: }
160: if (!$instance) {
161: $instance[0] = new Debugger();
162: }
163: return $instance[0];
164: }
165:
166: 167: 168: 169: 170: 171: 172: 173: 174:
175: public static function dump($var, $depth = 3) {
176: pr(static::exportVar($var, $depth));
177: }
178:
179: 180: 181: 182: 183: 184: 185: 186: 187: 188:
189: public static function log($var, $level = LOG_DEBUG, $depth = 3) {
190: $source = static::trace(array('start' => 1)) . "\n";
191: CakeLog::write($level, "\n" . $source . static::exportVar($var, $depth));
192: }
193:
194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204:
205: public static function showError($code, $description, $file = null, $line = null, $context = null) {
206: $self = Debugger::getInstance();
207:
208: if (empty($file)) {
209: $file = '[internal]';
210: }
211: if (empty($line)) {
212: $line = '??';
213: }
214:
215: $info = compact('code', 'description', 'file', 'line');
216: if (!in_array($info, $self->errors)) {
217: $self->errors[] = $info;
218: } else {
219: return null;
220: }
221:
222: switch ($code) {
223: case E_PARSE:
224: case E_ERROR:
225: case E_CORE_ERROR:
226: case E_COMPILE_ERROR:
227: case E_USER_ERROR:
228: $error = 'Fatal Error';
229: $level = LOG_ERR;
230: break;
231: case E_WARNING:
232: case E_USER_WARNING:
233: case E_COMPILE_WARNING:
234: case E_RECOVERABLE_ERROR:
235: $error = 'Warning';
236: $level = LOG_WARNING;
237: break;
238: case E_NOTICE:
239: case E_USER_NOTICE:
240: $error = 'Notice';
241: $level = LOG_NOTICE;
242: break;
243: case E_DEPRECATED:
244: case E_USER_DEPRECATED:
245: $error = 'Deprecated';
246: $level = LOG_NOTICE;
247: break;
248: default:
249: return null;
250: }
251:
252: $data = compact(
253: 'level', 'error', 'code', 'description', 'file', 'path', 'line', 'context'
254: );
255: echo $self->outputError($data);
256:
257: if ($error === 'Fatal Error') {
258: exit();
259: }
260: return true;
261: }
262:
263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278:
279: public static function trace($options = array()) {
280: $self = Debugger::getInstance();
281: $defaults = array(
282: 'depth' => 999,
283: 'format' => $self->_outputFormat,
284: 'args' => false,
285: 'start' => 0,
286: 'scope' => null,
287: 'exclude' => array('call_user_func_array', 'trigger_error')
288: );
289: $options = Hash::merge($defaults, $options);
290:
291: $backtrace = debug_backtrace();
292: $count = count($backtrace);
293: $back = array();
294:
295: $_trace = array(
296: 'line' => '??',
297: 'file' => '[internal]',
298: 'class' => null,
299: 'function' => '[main]'
300: );
301:
302: for ($i = $options['start']; $i < $count && $i < $options['depth']; $i++) {
303: $trace = array_merge(array('file' => '[internal]', 'line' => '??'), $backtrace[$i]);
304: $signature = $reference = '[main]';
305:
306: if (isset($backtrace[$i + 1])) {
307: $next = array_merge($_trace, $backtrace[$i + 1]);
308: $signature = $reference = $next['function'];
309:
310: if (!empty($next['class'])) {
311: $signature = $next['class'] . '::' . $next['function'];
312: $reference = $signature . '(';
313: if ($options['args'] && isset($next['args'])) {
314: $args = array();
315: foreach ($next['args'] as $arg) {
316: $args[] = Debugger::exportVar($arg);
317: }
318: $reference .= implode(', ', $args);
319: }
320: $reference .= ')';
321: }
322: }
323: if (in_array($signature, $options['exclude'])) {
324: continue;
325: }
326: if ($options['format'] === 'points' && $trace['file'] !== '[internal]') {
327: $back[] = array('file' => $trace['file'], 'line' => $trace['line']);
328: } elseif ($options['format'] === 'array') {
329: $back[] = $trace;
330: } else {
331: if (isset($self->_templates[$options['format']]['traceLine'])) {
332: $tpl = $self->_templates[$options['format']]['traceLine'];
333: } else {
334: $tpl = $self->_templates['base']['traceLine'];
335: }
336: $trace['path'] = static::trimPath($trace['file']);
337: $trace['reference'] = $reference;
338: unset($trace['object'], $trace['args']);
339: $back[] = CakeText::insert($tpl, $trace, array('before' => '{:', 'after' => '}'));
340: }
341: }
342:
343: if ($options['format'] === 'array' || $options['format'] === 'points') {
344: return $back;
345: }
346: return implode("\n", $back);
347: }
348:
349: 350: 351: 352: 353: 354: 355:
356: public static function trimPath($path) {
357: if (!defined('CAKE_CORE_INCLUDE_PATH') || !defined('APP')) {
358: return $path;
359: }
360:
361: if (strpos($path, APP) === 0) {
362: return str_replace(APP, 'APP' . DS, $path);
363: } elseif (strpos($path, CAKE_CORE_INCLUDE_PATH) === 0) {
364: return str_replace(CAKE_CORE_INCLUDE_PATH, 'CORE', $path);
365: } elseif (strpos($path, ROOT) === 0) {
366: return str_replace(ROOT, 'ROOT', $path);
367: }
368:
369: return $path;
370: }
371:
372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390:
391: public static function excerpt($file, $line, $context = 2) {
392: $lines = array();
393: if (!file_exists($file)) {
394: return array();
395: }
396: $data = file_get_contents($file);
397: if (empty($data)) {
398: return $lines;
399: }
400: if (strpos($data, "\n") !== false) {
401: $data = explode("\n", $data);
402: }
403: if (!isset($data[$line])) {
404: return $lines;
405: }
406: for ($i = $line - ($context + 1); $i < $line + $context; $i++) {
407: if (!isset($data[$i])) {
408: continue;
409: }
410: $string = str_replace(array("\r\n", "\n"), "", static::_highlight($data[$i]));
411: if ($i == $line) {
412: $lines[] = '<span class="code-highlight">' . $string . '</span>';
413: } else {
414: $lines[] = $string;
415: }
416: }
417: return $lines;
418: }
419:
420: 421: 422: 423: 424: 425: 426:
427: protected static function _highlight($str) {
428: if (function_exists('hphp_log') || function_exists('hphp_gettid')) {
429: return htmlentities($str);
430: }
431: $added = false;
432: if (strpos($str, '<?php') === false) {
433: $added = true;
434: $str = "<?php \n" . $str;
435: }
436: $highlight = highlight_string($str, true);
437: if ($added) {
438: $highlight = str_replace(
439: '<?php <br />',
440: '',
441: $highlight
442: );
443: }
444: return $highlight;
445: }
446:
447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466:
467: public static function exportVar($var, $depth = 3) {
468: return static::_export($var, $depth, 0);
469: }
470:
471: 472: 473: 474: 475: 476: 477: 478:
479: protected static function _export($var, $depth, $indent) {
480: switch (static::getType($var)) {
481: case 'boolean':
482: return ($var) ? 'true' : 'false';
483: case 'integer':
484: return '(int) ' . $var;
485: case 'float':
486: return '(float) ' . $var;
487: case 'string':
488: if (trim($var) === '') {
489: return "''";
490: }
491: return "'" . $var . "'";
492: case 'array':
493: return static::_array($var, $depth - 1, $indent + 1);
494: case 'resource':
495: return strtolower(gettype($var));
496: case 'null':
497: return 'null';
498: case 'unknown':
499: return 'unknown';
500: default:
501: return static::_object($var, $depth - 1, $indent + 1);
502: }
503: }
504:
505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520:
521: protected static function _array(array $var, $depth, $indent) {
522: $secrets = array(
523: 'password' => '*****',
524: 'login' => '*****',
525: 'host' => '*****',
526: 'database' => '*****',
527: 'port' => '*****'
528: );
529: $replace = array_intersect_key($secrets, $var);
530: $var = $replace + $var;
531:
532: $out = "array(";
533: $break = $end = null;
534: if (!empty($var)) {
535: $break = "\n" . str_repeat("\t", $indent);
536: $end = "\n" . str_repeat("\t", $indent - 1);
537: }
538: $vars = array();
539:
540: if ($depth >= 0) {
541: foreach ($var as $key => $val) {
542:
543: if ($key === 'GLOBALS' && is_array($val) && isset($val['GLOBALS'])) {
544: $val = '[recursion]';
545: } elseif ($val !== $var) {
546: $val = static::_export($val, $depth, $indent);
547: }
548: $vars[] = $break . static::exportVar($key) .
549: ' => ' .
550: $val;
551: }
552: } else {
553: $vars[] = $break . '[maximum depth reached]';
554: }
555: return $out . implode(',', $vars) . $end . ')';
556: }
557:
558: 559: 560: 561: 562: 563: 564: 565: 566:
567: protected static function _object($var, $depth, $indent) {
568: $out = '';
569: $props = array();
570:
571: $className = get_class($var);
572: $out .= 'object(' . $className . ') {';
573:
574: if ($depth > 0) {
575: $end = "\n" . str_repeat("\t", $indent - 1);
576: $break = "\n" . str_repeat("\t", $indent);
577: $objectVars = get_object_vars($var);
578: foreach ($objectVars as $key => $value) {
579: $value = static::_export($value, $depth - 1, $indent);
580: $props[] = "$key => " . $value;
581: }
582:
583: if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
584: $ref = new ReflectionObject($var);
585:
586: $filters = array(
587: ReflectionProperty::IS_PROTECTED => 'protected',
588: ReflectionProperty::IS_PRIVATE => 'private',
589: );
590: foreach ($filters as $filter => $visibility) {
591: $reflectionProperties = $ref->getProperties($filter);
592: foreach ($reflectionProperties as $reflectionProperty) {
593: $reflectionProperty->setAccessible(true);
594: $property = $reflectionProperty->getValue($var);
595:
596: $value = static::_export($property, $depth - 1, $indent);
597: $key = $reflectionProperty->name;
598: $props[] = sprintf('[%s] %s => %s', $visibility, $key, $value);
599: }
600: }
601: }
602:
603: $out .= $break . implode($break, $props) . $end;
604: }
605: $out .= '}';
606: return $out;
607: }
608:
609: 610: 611: 612: 613: 614: 615: 616:
617: public static function outputAs($format = null) {
618: $self = Debugger::getInstance();
619: if ($format === null) {
620: return $self->_outputFormat;
621: }
622: if ($format !== false && !isset($self->_templates[$format])) {
623: throw new CakeException(__d('cake_dev', 'Invalid Debugger output format.'));
624: }
625: $self->_outputFormat = $format;
626: }
627:
628: 629: 630: 631: 632: 633: 634: 635: 636: 637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662: 663: 664: 665: 666:
667: public static function addFormat($format, array $strings) {
668: $self = Debugger::getInstance();
669: if (isset($self->_templates[$format])) {
670: if (isset($strings['links'])) {
671: $self->_templates[$format]['links'] = array_merge(
672: $self->_templates[$format]['links'],
673: $strings['links']
674: );
675: unset($strings['links']);
676: }
677: $self->_templates[$format] = array_merge($self->_templates[$format], $strings);
678: } else {
679: $self->_templates[$format] = $strings;
680: }
681: return $self->_templates[$format];
682: }
683:
684: 685: 686: 687: 688: 689: 690: 691: 692: 693: 694:
695: public static function output($format = null, $strings = array()) {
696: $self = Debugger::getInstance();
697: $data = null;
698:
699: if ($format === null) {
700: return Debugger::outputAs();
701: }
702:
703: if (!empty($strings)) {
704: return Debugger::addFormat($format, $strings);
705: }
706:
707: if ($format === true && !empty($self->_data)) {
708: $data = $self->_data;
709: $self->_data = array();
710: $format = false;
711: }
712: Debugger::outputAs($format);
713: return $data;
714: }
715:
716: 717: 718: 719: 720: 721:
722: public function outputError($data) {
723: $defaults = array(
724: 'level' => 0,
725: 'error' => 0,
726: 'code' => 0,
727: 'description' => '',
728: 'file' => '',
729: 'line' => 0,
730: 'context' => array(),
731: 'start' => 2,
732: );
733: $data += $defaults;
734:
735: $files = $this->trace(array('start' => $data['start'], 'format' => 'points'));
736: $code = '';
737: $file = null;
738: if (isset($files[0]['file'])) {
739: $file = $files[0];
740: } elseif (isset($files[1]['file'])) {
741: $file = $files[1];
742: }
743: if ($file) {
744: $code = $this->excerpt($file['file'], $file['line'] - 1, 1);
745: }
746: $trace = $this->trace(array('start' => $data['start'], 'depth' => '20'));
747: $insertOpts = array('before' => '{:', 'after' => '}');
748: $context = array();
749: $links = array();
750: $info = '';
751:
752: foreach ((array)$data['context'] as $var => $value) {
753: $context[] = "\${$var} = " . $this->exportVar($value, 3);
754: }
755:
756: switch ($this->_outputFormat) {
757: case false:
758: $this->_data[] = compact('context', 'trace') + $data;
759: return;
760: case 'log':
761: $this->log(compact('context', 'trace') + $data);
762: return;
763: }
764:
765: $data['trace'] = $trace;
766: $data['id'] = 'cakeErr' . uniqid();
767: $tpl = array_merge($this->_templates['base'], $this->_templates[$this->_outputFormat]);
768:
769: if (isset($tpl['links'])) {
770: foreach ($tpl['links'] as $key => $val) {
771: $links[$key] = CakeText::insert($val, $data, $insertOpts);
772: }
773: }
774:
775: if (!empty($tpl['escapeContext'])) {
776: $context = h($context);
777: $data['description'] = h($data['description']);
778: }
779:
780: $infoData = compact('code', 'context', 'trace');
781: foreach ($infoData as $key => $value) {
782: if (empty($value) || !isset($tpl[$key])) {
783: continue;
784: }
785: if (is_array($value)) {
786: $value = implode("\n", $value);
787: }
788: $info .= CakeText::insert($tpl[$key], array($key => $value) + $data, $insertOpts);
789: }
790: $links = implode(' ', $links);
791:
792: if (isset($tpl['callback']) && is_callable($tpl['callback'])) {
793: return call_user_func($tpl['callback'], $data, compact('links', 'info'));
794: }
795: echo CakeText::insert($tpl['error'], compact('links', 'info') + $data, $insertOpts);
796: }
797:
798: 799: 800: 801: 802: 803: 804:
805: public static function getType($var) {
806: if (is_object($var)) {
807: return get_class($var);
808: }
809: if ($var === null) {
810: return 'null';
811: }
812: if (is_string($var)) {
813: return 'string';
814: }
815: if (is_array($var)) {
816: return 'array';
817: }
818: if (is_int($var)) {
819: return 'integer';
820: }
821: if (is_bool($var)) {
822: return 'boolean';
823: }
824: if (is_float($var)) {
825: return 'float';
826: }
827: if (is_resource($var)) {
828: return 'resource';
829: }
830: return 'unknown';
831: }
832:
833: 834: 835: 836: 837:
838: public static function checkSecurityKeys() {
839: if (Configure::read('Security.salt') === 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi') {
840: trigger_error(__d('cake_dev', 'Please change the value of %s in %s to a salt value specific to your application.', '\'Security.salt\'', 'APP/Config/core.php'), E_USER_NOTICE);
841: }
842:
843: if (Configure::read('Security.cipherSeed') === '76859309657453542496749683645') {
844: trigger_error(__d('cake_dev', 'Please change the value of %s in %s to a numeric (digits only) seed value specific to your application.', '\'Security.cipherSeed\'', 'APP/Config/core.php'), E_USER_NOTICE);
845: }
846: }
847:
848: }
849: