forked from bearsaturday/Panda
/
Panda.php
1281 lines (1209 loc) · 38.5 KB
/
Panda.php
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Panda
*
* PHP versions 5
*
* @category Panda
* @package Panda
* @author Akihito Koriyama <koriyama@users.sourceforge.jp>
* @copyright 2009 Akihito Koriyama All rights reserved.
* @license http://opensource.org/licenses/bsd-license.php BSD
* @version SVN: Release: v.0.1.0 $Id: Panda.php 97 2009-10-08 03:45:58Z koriyama@users.sourceforge.jp $
* @link n/a
*
* debuglib for PHP5
* Thomas Schüßler <debuglib at atomar dot de>
* http://phpdebuglib.de/
*
* FireBug
* http://www.getfirebug.com/
*
* FirePHP
* http://www.christophdorn.com/
* https://addons.mozilla.org/ja/firefox/addon/6149
* http://www.firephp.org/
*/
// for 5.2.x or earlier
if (!defined('E_DEPRECATED')) {
define('E_DEPRECATED', 8192);
define('E_USER_DEPRECATED', 16384);
}
/**
* Panda Class
*
* <pre>
* * enhance PHP standard error more powerful.
* * debug output utility
* * standard HTTP code output
* </pre>
*
* Example 1. HTTP Code error output.
* </pre>
* <code>
* throw new Panda_Exception('Sorry, The Item is not available now.', 404, $debugInfo);
* </code>
*
* @category Panda
* @package Panda
* @author Akihito Koriyama <koriyama@users.sourceforge.jp>
* @copyright 2008 Akihito Koriyama All rights reserved.
* @license http://opensource.org/licenses/bsd-license.php BSD
* @version SVN: Release: $Id: Panda.php 97 2009-10-08 03:45:58Z koriyama@users.sourceforge.jp $
* @link http://api.Panda-project.net/Panda/Panda.html
*/
class Panda
{
/**
* Version
*/
const VERSION = '0.3.41';
/**
* Package name - App
*/
const PACKAGE_APP = 0;
/**
* Package name - PHP
*/
const PACKAGE_PHP = 1;
/**
* Package name - Assert
*
* @var int
*/
const PACKAGE_ASSERT = 2;
/**
* Package name - PEAR
*
* @var int
*/
const PACKAGE_PEAR = 3;
/**
* Package name - Exception
*
* @var string
*/
const PACKAGE_EXECEPTION = 4;
/**
* config debug mode
*
* @var string
*/
const CONFIG_DEBUG = 'debug';
/**
* config report project path
*
* @var string
*/
const CONFIG_VALID_PATH = 'valid';
/**
* config log path
*
* @var string
*/
const CONFIG_LOG_PATH = 'log_path';
/**
* config callback on fire
*
* @var string
*/
const CONFIG_ON_ERROR_FIRED = 'on_error';
/**
* config callback on shutdown script (for logging)
*
* @var string
*/
const CONFIG_ON_ERROR_END = 'on_end';
/**
* config fatal error callback
*
* @var string
*/
const CONFIG_ON_FATAL_ERROR = 'on_fatal_error';
/**
* config - use firephp ?
*
* @var string
*/
const CONFIG_ENABLE_FIREPHP = 'firephp';
/**
* config - fatal error template
*
* @var string
*/
const CONFIG_FATAL_HTML = 'fatal_html';
/**
* config - Http503 template
*
* @var string
*/
const CONFIG_HTTP_TPL = 'http_tpl';
/**
* config catch fatal error ?
*
* @var string
*/
const CONFIG_CATCH_FATAL = 'catch_fatal';
/**
* config catch faatal strict error ?
*
* @var string
*/
const CONFIG_CATCH_STRICT = 'catch_strict';
/**
* config for growl
*
* @var string
*/
const CONFIG_GROWL = 'growl';
/**
* Panda::error options - severity
*
* @var string
*/
const ERROR_OPTION_FILE = 'file';
/**
* Panda::error options - severity
*
* @var string
*/
const ERROR_OPTION_LINE = 'line';
/**
* Panda::error options - severity
*
* @var string
*/
const ERROR_OPTION_TRACE = 'trace';
/**
* Panda::error options - severity
*
* @var string
*/
const ERROR_OPTION_RETRUN = 'return';
/**
* Panda::error options - severity
*
* @var string
*/
const ERROR_OPTION_NO_SCREEN = 'no_screen';
/**
* Panda htdocs path
*/
const CONFIG_PANDA_PATH = 'path';
/**
* Config - Editor
*
* @var string
*/
const CONFIG_EDITOR = 'edit';
/**
* API check
*
* @var
*/
const CONFIG_IS_API_CHECK = 'is_api';
/**
* Editor - Textmate
*
* @var int
*/
const EDITOR_TEXTMATE = 1;
/**
* Editor - Bespin
*
* @var int
*/
const EDITOR_BESPIN = 2;
/**
* Error name data
*
* @static array
*/
static $phpError = array(
E_ERROR => 'E_ERROR',
E_WARNING => 'E_WARNING',
E_PARSE => 'E_PARSE',
E_NOTICE => 'E_NOTICE',
E_CORE_ERROR => 'E_CORE_ERROR',
E_CORE_WARNING => 'E_CORE_WARNING',
E_COMPILE_ERROR => 'E_COMPILE_ERROR',
E_COMPILE_WARNING => 'E_COMPILE_WARNING',
E_USER_ERROR => 'E_USER_ERROR',
E_USER_WARNING => 'E_USER_WARNING',
E_USER_NOTICE => 'E_USER_NOTICE',
E_STRICT => 'E_STRICT',
E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
E_DEPRECATED => 'E_DEPRECATED',
E_USER_DEPRECATED => 'E_USER_DEPRECATED'
);
/**
* PHP Error code statics
*
* @var integer
*
*/
private static $_errorStat = 0;
/**
* Errors in outer path
*
* @var array
*/
private static $_outerPathErrors = array();
/**
* Errors in all path
*
* @var array
*/
private static $_errors = array();
/**
* Default configration
*
* @var array
*/
private static $_config = array(
self::CONFIG_DEBUG => false,
self::CONFIG_VALID_PATH => array('/'),
self::CONFIG_LOG_PATH => '/tmp',
self::CONFIG_ON_ERROR_FIRED => false,
self::CONFIG_ON_FATAL_ERROR => false,
self::CONFIG_ENABLE_FIREPHP => true,
self::CONFIG_FATAL_HTML => 'Panda/template/fatal.php',
self::CONFIG_HTTP_TPL => 'Panda/template/http.php',
self::CONFIG_CATCH_FATAL => false,
self::CONFIG_CATCH_STRICT => true,
self::CONFIG_PANDA_PATH => '/',
self::CONFIG_EDITOR => 0,
self::CONFIG_GROWL => false,
self::CONFIG_IS_API_CHECK => array(__CLASS__, 'isCliOutput')
);
/**
* Get config
*
* @return array
*/
public static function getConfig()
{
return self::$_config;
}
/**
* Init
*
* reset error/exception handler and atach panda handler
*
* @param array $config config
*
* @return void
*/
public static function init(array $config = array())
{
if (isset($_GET['_nopanda'])) {
return;
}
$er = error_reporting(false);
self::$_config = array_merge(self::$_config, $config);
// reset handler
if (self::$_config[self::CONFIG_DEBUG] !== true) {
ini_set('display_errors', 0);
function p($v = ''){
syslog(LOG_INFO, print_r($v, true));
}
if (class_exists('PEAR', false)) {
PEAR::setErrorHandling(PEAR_Exception);
}
set_exception_handler(array('Panda', 'onException'));
} else {
if (self::$_config[self::CONFIG_ENABLE_FIREPHP]) {
include_once 'FirePHPCore/FirePHP.class.php';
include_once 'FirePHPCore/fb.php';
}
self::_initOnDebug();
if (self::$_config[self::CONFIG_CATCH_FATAL] === true) {
ob_start(array('Panda', 'onFatalError'));
}
// catch E_STRICT
if (self::$_config[self::CONFIG_CATCH_STRICT] === true) {
error_reporting(E_ALL | E_STRICT);
ob_start(array('Panda', 'onStrictError'));
}
}
error_reporting($er);
}
/**
* Init in Debug mode
*
* @return void
*/
private static function _initOnDebug()
{
require_once 'Panda/Debug.php';
require_once 'Panda/Debug/util.php';
require_once 'Panda/Exception.php';
include_once 'Net/Growl.php';
ini_set('display_errors', 1);
// アサーションを有効
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
assert_options(ASSERT_CALLBACK, (array('Panda', 'onAssert')));
restore_error_handler();
restore_exception_handler();
set_exception_handler(array('Panda', 'onException'));
if (class_exists('PEAR', false)) {
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array('Panda', 'onPearError'));
}
if (class_exists('PEAR_ErrorStack', false)) {
PEAR_ErrorStack::setDefaultCallback(array('Panda', 'onStackError'));
}
set_error_handler(array('Panda', 'onDebugPhpError'));
}
/**
* Resotre Panda errpr handler
*
* <pre>
* restore Panda error handler after 'restore_error_handler' or 'restore_exception_handler'
*
* (Note) Error in reflection 'invoke' method seems unstable,
* I think its better detache handler before invoke then call this method again.
* </pre>
*
* @returnv void
*
*/
public static function restoreHandler()
{
set_error_handler(array('Panda', 'onDebugPhpError'));
set_exception_handler(array('Panda', 'onException'));
}
/**
* Assert Handler
*
* @param string $file file
* @param integer $line line
* @param integer $code code
*
* @return void
*/
public static function onAssert($file, $line, $code)
{
$trace = debug_backtrace();
array_shift($trace);
$options['file'] = $trace[0]['file'];
$options['line'] = $trace[0]['line'];
$options['trace'] = $trace;
$options['package'] = self::PACKAGE_ASSERT;
self::error('Assert Error', $code, null, $options);
}
/**
* PEAR::ErrorStack Handler
*
* @param PEAR_Error $error PEAR Error object
*
* @return mixed
*/
public static function onStackError($error)
{
return PEAR_ERRORSTACK_PUSH;
}
/**
* PEAR error handler
*
* @param object $error PEAR Error object
*
* @return void
* @ignore
*/
public static function onPearError(PEAR_Error $error)
{
$trace = debug_backtrace();
array_shift($trace);
array_shift($trace);
array_shift($trace);
$options['file'] = $trace[0]['file'];
$options['line'] = $trace[0]['line'];
$options['trace'] = $trace;
$options['package'] = self::PACKAGE_PEAR;
$debugInfo = $error->getDebugInfo();
$userInfo = $error->getUserInfo();
$trace = ($error->getBackTrace());
$info = array('Error Type' => $error->getType(),
'Debug Info' => $error->getUserInfo());
if ($debugInfo !== $userInfo) {
$info['User Info'] = $userInfo;
}
if (self::$_config[self::CONFIG_DEBUG] === true) {
self::error('PEAR Error', $error->getCode() . ' ' . $error->getMessage(), $info, $options);
} else {
error_log('PEAR Error' . $error->getCode() . ' ' . $error->getMessage(). " in file[{$options['file']}] on line [{$options['line']}", 0);
}
}
/**
* PHP error handler in debug mode
*
* @param int $code code
* @param string $message message
* @param string $file file
* @param int $line line
* @param mixed $errcontext error context (local variables)
*
* @return object
*/
public static function onDebugPhpError($code, $message, $file, $line, array $errcontext)
{
$type = self::$phpError[$code];
$simpleErrorString = "[{$type}] {$message} in {$file} on line {$line}";
if ($code !== E_STRICT) {
// apache log all error expect E_STRICT even @ symbol
error_log($simpleErrorString, 0);
}
// @?
if (error_reporting() === 0) {
return;
}
/**
* to avoid repeat reported error
*/
static $_reported = array();
static $_cnt = 0;
// me ? // me ?
if ($file === __FILE__) {
$msg = "<b>Error in Panda ! PHP Error [$simpleErrorString] captured in [" . __FILE__ ." on line ". __LINE__;
throw new Exception($msg);
}
if($_cnt++ > 100) {
return;
}
self::$_errorStat |= $code;
// valid path ?
if (self::isValidPath($file) !== true) {
self::$_outerPathErrors[$code] = $simpleErrorString;
self::$_errors[$code] = $simpleErrorString;
if ($code & E_NOTICE || $code & E_STRICT || $code & E_WARNING) {
return;
}
}
$errorString = self::$phpError[$code];
// igore repeated error (same as ignore_repeated_errors 0)
$key = "{$message}{$file}{$line}";
if (isset($_reported[$key])) {
$_reported[$key]++;
return;
} else {
$_reported[$key] = 1;
}
$trace = debug_backtrace();
array_shift($trace);
$options['file'] = $file;
$options['line'] = $line;
$options['trace'] = $trace;
// hide weak error
$noScreen = ($code & (E_NOTICE | E_STRICT | E_DEPRECATED)) ? true : false;
$noErrorLog = ($code & (E_STRICT)) ? true : false;
if (!$noErrorLog) {
error_log($simpleErrorString);
}
$options['no_screen'] = $noScreen;
$options['severity'] = $code;
// E_STRICT tweak
$matches = array();
if ($code == E_STRICT) {
preg_match('/Non-static method (.*)::(.*)\(\)/', $message, $matches);
if ($matches) {
$class = $matches[1];
assert(class_exists($class, false));
$ref = new ReflectionClass($class);
$file = $ref->getFileName();
if (!self::isValidPath($file)) {
return;
}
}
}
$options['package'] = self::PACKAGE_PHP;
self::error($errorString, $message, $errcontext, $options);
}
/**
* PHP error handler in live mode
*
* @param int $code code
* @param string $message message
* @param string $file file
* @param int $line line
* @param mixed $errcontext error context (local variables)
*
* @return void
*/
public static function onLivePhpError($code, $message, $file, $line, array $errcontext)
{
$trace = debug_backtrace();
array_shift($trace);
$options['file'] = $file;
$options['line'] = $line;
$options['trace'] = $trace;
$errorString = self::$phpError[$code];
$options['package'] = self::PACKAGE_PEAR;
self::error($errorString, $message, $errcontext, $options);
}
/**
* Uncaught exception handler
*
* <pre>
* All uncaught exception but Panda_Exception produce HTTP 503(Service Temporary Unavailable) Error.
* Crawler may not crawl by this code. (and come back later)
* </pre>
*
* @param Exception $e Panda_Exception | any Exception
*
* @return void
*/
public static function onException(Exception $e, $httpScreenOutput = true)
{
try {
$class = get_class($e);
$body = null;
$info = array();
if ($e instanceof Panda_End_Exception) {
exit();
} elseif ($e instanceof Panda_Exception) {
$httpCode = $e->getCode();
$body = $e->getMessage();
} elseif ($e instanceof ErrorException) {
$info['severity'] = $e->getSeverity();
$httpCode = 503;
} else {
$info['code'] = $e->getCode();
$httpCode = 503;
}
$info['info'] = (method_exists($e, 'getInfo')) ? $e->getInfo() : null;
$id = 'e' . $e->getLine() . '-' .substr(md5($e->getFile() . $e->getCode() . $e->getCode()), 0, 6);
if ($httpScreenOutput === true) {
self::outputHttpStatus($httpCode, true, $body, $id);
}
$options = array('file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTrace());
$options['package'] = self::PACKAGE_EXECEPTION;
$code = $e->getCode();
$msg = $e->getMessage();
$infoStr = (method_exists($e, 'getInfo')) ? print_r($e->getInfo(), true) : null;
$trace = $e->getTraceAsString();
$class = get_class($e);
$log = "[$code] $class - $msg - $infoStr";
error_log($log);
if (isset(self::$_config[self::CONFIG_LOG_PATH])) {
$filePath = self::$_config[self::CONFIG_LOG_PATH] . "$id.log";
if (!file_exists($filePath)) {
file_put_contents($filePath, "$log\n[Info]:\n" . print_r($info, true) . "\n[Trace]" . print_r($e->getTrace(), true));
}
}
if (self::$_config['debug'] === true) {
self::error(get_class($e), $e->getCode() . ' ' . $e->getMessage(), $info, $options);
}
} catch(Exception $e) {
$bug = get_class($e) . " thrown within the exception handler. Message: " . $e->getMessage() . " on line " . $e->getLine();
self::_debugPrint($bug);
}
}
/**
* Check if $file is under the path.
*
* @param string $file file path
*
* @return bool
*/
public static function isValidPath($file)
{
$isValidPath = false;
foreach (self::$_config[self::CONFIG_VALID_PATH] as $validPath) {
if (strpos($file, $validPath) === 0) {
$isValidPath = true;
break;
}
}
if ($isValidPath === false) {
// $isValidPath = true;
}
return $isValidPath;
}
/**
* Read css string
*
* @param string $headerColor header color
*
* @return string
*/
private static function _getDebugCss($headerColor = 'red')
{
static $_done = false;
if ($_done) {
return '';
}
$_done = true;
$cssPanda = file_get_contents('Panda/templates/debug.css', FILE_USE_INCLUDE_PATH);
$cssPanda = str_replace('{header_color}', $headerColor, $cssPanda);
$result = '';
if (false && class_exists('DbugL', false) && method_exists('DbugL', 'html_prefix')) {
$cssPrinta = DbugL::html_prefix();
$result .= str_replace("\n", '', $cssPrinta);
}
$result .= '<style type="text/css"><!--' . trim(str_replace("\n", '', $cssPanda));
$result .= ' --></style>';
return $result;
}
/**
* Is CLI Output ?
*
* <pre>
* Check if CLI, Ajax or REST accesss.Logic can be injected by config.
* </pre>
*
* @return bool
*/
public static function isCliOutput()
{
return (PHP_SAPI === 'cli');
}
/**
* Display Error Message in debug mode.
*
* <pre>
*
* @param string $heading Headding
* @param string $subheading Sub headding
* @param mixed $info error info
* @param array $options options
*
* $options key:
*
* string $options['file'] file name
* int $options['line'] line
* array $options['trace'] backtrace
* bool $options['return'] return string(no echo)
* </pre>
*
* @return void
*/
public static function error($heading, $subheading = "", $info = "", array $options = array())
{
// return if live
if (self::$_config[self::CONFIG_DEBUG] !== true) {
return;
}
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') {
if (class_exists('FB', false)) {
$in = isset($options['file']) ? "- in {$options['file']} on line {$options['line']}" : '';
$msg = "$heading - {$subheading} $in";
if ($info) {
FB::group($msg);
FB::error($info);
FB::groupEnd();
return;
}
}
}
static $num = 1; //div id number
$heading = (is_numeric($heading)) ? self::$packageName[$heading] : $heading;
// Application error callback
if (is_callable(self::$_config[self::CONFIG_ON_ERROR_FIRED])) {
call_user_func(self::$_config[self::CONFIG_ON_ERROR_FIRED], $heading, $subheading, $info, $options);
}
$fileInfoString = isset($options['file']) ? "in {$options['file']} on line {$options['line']}" : 'in unknown file';
if (self::$_config[self::CONFIG_ENABLE_FIREPHP] && isset($options['severity'])) {
$fireLevel = FirePHP::ERROR;
switch (true) {
case ($options['severity'] == E_WARNING || $options['severity'] == E_USER_WARNING) :
$fireLevel = FirePHP::WARN;
break;
case ($options['severity'] == E_NOTICE || $options['severity'] == E_USER_NOTICE) :
$fireLevel = FirePHP::INFO;
break;
case ($options['severity'] == E_STRICT) :
$fireLevel = FirePHP::LOG;
break;
default :
$fireLevel = FirePHP::ERROR;
break;
}
FB::send("{$subheading} - {$fileInfoString}", $heading, $fireLevel);
}
self::GrowlNotify($heading, $subheading . "\n{$fileInfoString}");
$defaultOptions = array('file' => null,
'line' => null,
'trace' => array(),
'return' => false,
'no_screen' => false);
$options = array_merge($defaultOptions, $options);
if ($options['no_screen']) {
return '';
}
$output = self::_getDebugCss();
if ($options['trace']) {
$traceId = uniqid();
$traceFile = self::getTempDir() . '/trace-' . $traceId . '.log';
$refData = self::__addReflectInfo($options['trace']);
file_put_contents($traceFile, serialize($options['trace']));
file_put_contents("{$traceFile}.ref.log", serialize($refData));
}
if (PHP_SAPI == 'cli' || call_user_func(self::$_config[self::CONFIG_IS_API_CHECK])) {
$output .= $heading . PHP_EOL;
$output .= $subheading . PHP_EOL;
$output .= $info . PHP_EOL;
return $output;
} else {
if (is_array($info) || is_object($info)) {
if (isset($options['package']) && ($options['package'] === self::PACKAGE_PHP)) {
$info = self::_getContextString($info, '$');
} else {
$info = self::_getContextString($info);
}
}
header('Content-Type: text/html; charset=utf-8');
$output .= '<div id="panda-' . $num . '"class="panda">';
$output .= '<div class="panda-header">';
$output .= '<h1>' . $heading . '</h1>';
$output .= isset($options['file']) ? '<p class="panda-file">' . self::getEditorLink($options['file'], $options['line'], 0) . '</p>' : '';
$output .= '<h2>' . $subheading . '</h2>';
$output .= '<p class="panda-cmd">';
if ($options['trace']) {
$output .= '<a target="_panda_trace_' . $traceId . '" href="' . "http://{$_SERVER['SERVER_NAME']}" . self::$_config[self::CONFIG_PANDA_PATH] . '__panda/trace/?id=' . $traceId . '">trace</a> | ';
}
$output .= '<a href=# onclick="var t = document.getElementById(\'panda-';
$output .= $num . '\'); t.parentNode.removeChild(t);return false;">close</a>';
$output .= '</p>';
$output .= '</div>';
$output .= '<div class="panda-body">';
$output .= $info ? '<h3>Info</h3><p class="panda-info">' . $info . '</p>' : '';
// file summary
if ($options['file']) {
$output .= '<h3>Source</h3>';
$output .= '<a style="text-decoration:none;" href="/__panda/edit/?file=' . $options['file'];
$output .= '&line=' . $options['line'] . '">';
$output .= self::_getFileSummary($options['file'], $options['line'], 6);
$output .= '</a>';
if ($options['trace']) {
// trace summary
$output .= '<h3>Trace</h3>';
$output .= Panda_Debug::getTraceSummary($options['trace']);
}
}
$output .= '</div></div>'; // /body-error-body /panda
}
$num++;
if ($options['return'] === true) {
return $output;
} else {
echo $output;
}
}
/**
* Growl notify
*
* This needs.
*
* 1) Growl installation.
* 2) Growl setting for remote application acception.
*
* @param string $title
* @param string $description
*/
public static function growlNotify($title, $description)
{
static $growlApp;
if (self::CONFIG_GROWL !== true) {
return;
}
if (!$growlApp) {
$growlApp = new Net_Growl_Application('Panda', array('Panda_Growl_Notify'));
}
$growl = Net_Growl::singleton($growlApp, null, null);
$growl->setNotificationLimit(16);
$result = $growl->notify('Panda_Growl_Notify', $title, $description);
}
/**
* Message Output
*
* @param string $heading
* @param message $subheading
* @param array $info
*
* @return void
*/
public static function message($heading, $subheading = '', $info = '')
{
static $num = 1; //div id number
$output = self::_getDebugCss('green');
$output .= '<div id="panda-' . $num . '"class="panda">';
$output .= '<div class="panda-header">';
$output .= '<h1>' . $heading . '</h1>';
$output .= '<h2>' . $subheading . '</h2>';
$output .= '<p class="panda-cmd">';
$output .= '<a href=# onclick="var t = document.getElementById(\'panda-';
$output .= $num . '\'); t.parentNode.removeChild(t);return false;">close</a>';
$output .= '</p>';
$output .= '</div>';
if ($info) {
$output .= '<div class="panda-body"><p class="panda-info">' . $info . '</p>';
}
$output .= '</div></div>'; // /body-error-body /panda
header('Content-Type: text/html; charset=utf-8');
echo $output;
}
private static function _getContextString($context, $prefix = '')
{
$result = '';
foreach ($context as $varName => $varVal) {
if (class_exists('Panda_Debug')) {
$result .= Panda_Debug::dump($varVal, $prefix . $varName);
} else {
$result .= $varName . ' = ' . print_r($varVal, true);
}
}
return $result;
}
/**
* Add reflection info
*
* @param array $trace
*
* @return string
*/
private static function __addReflectInfo($trace)
{
$refLog = array();
$i = 0;
foreach ($trace as $row) {
if (isset($row['class']) && isset($row['function'])) {
if (isset($row['object'])) {
$refLog[$i]['export'] = ReflectionObject::export($row['object'], true);
}
$ref = new ReflectionMethod($row['class'], $row['function']);
$refLog[$i]['file'] = $ref->getFileName();
$refLog[$i]['doc'] = $ref->getDocComment();
$refLog[$i]['start'] = $ref->getStartLine();
$refLog[$i]['end'] = $ref->getEndLine();
}
$i++;
}
return $refLog;
}
/**
* File summery
*
* @param string $file file name
* @param int $line line
* @param int $num show line/2
*
* @return string
*/
static function _getFileSummary($file, $line, $num = 6)
{
if (!file_exists($file)) {
return ;
}
$result = '<div class="file-summary">';
$files = file($file);
$fileArray = array_map('htmlspecialchars', $files);
$hitLineOriginal = $fileArray[$line - 1];
$fileArray[$line - 1] = "<span class=\"hit-line\">{$fileArray[$line - 1]}</span>";
$shortListArray = array_slice($fileArray, $line - $num, $num * 2);
$shortListArray[$num - 1] = '<strog>' . $fileArray[$line - 1] . '</strong>';
$shortList = implode('', $shortListArray);
$shortList = '<pre class="short-list" style="background-color: #F0F0F9;">' . $shortList . '</pre>';
$hitLine = $fileArray[$line - 1];
$result .= $shortList . '</div>';
return $result;
}
/**
* File summery
*
* @param string $file file name
* @param int $line line
* @param int $num show line/2
*
* @return string
*/
static function _getFileSummary2($file, $line, $num = 6)
{
static $editorNum = 0;
static $oneTimeSetUp = false;
if (!file_exists($file)) {
return ;
}
if ($oneTimeSetUp === true) {
return;
}
$oneTimeSetUp = true;
$result = <<<EOD
<script src="/__panda/edit/ace/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="/__panda/edit/ace/mode-php.js" type="text/javascript" charset="utf-8"></script>