Skip to content
This repository
branch: master
file 1265 lines (1101 sloc) 42.285 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 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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
<?php
/**
* Provides low-level debugging, error and exception functionality
*
* @copyright Copyright (c) 2007-2011 Will Bond, others
* @author Will Bond [wb] <will@flourishlib.com>
* @author Will Bond, iMarc LLC [wb-imarc] <will@imarc.net>
* @author Nick Trew [nt]
* @author Kevin Hamer [kh] <kevin@imarc.net>
* @author Jeff Turcotte [jt] <jeff@imarc.net>
* @license http://flourishlib.com/license
*
* @package Flourish
* @link http://flourishlib.com/fCore
*
* @version 1.0.0b26
* @changes 1.0.0b26 Added handle_fatal_errors flag to enableErrorHandling [jt, 2013-06-10]
* @changes 1.0.0b25 exposing ->generateContext() [kh, 2012-12-18]
* @changes 1.0.0b24 Backwards Compatibility Break - moved ::detectOpcodeCache() to fLoader::hasOpcodeCache() [wb, 2011-08-26]
* @changes 1.0.0b23 Backwards Compatibility Break - changed the email subject of error/exception emails to include relevant file info, instead of the timestamp, for better email message threading [wb, 2011-06-20]
* @changes 1.0.0b22 Fixed a bug with dumping arrays containing integers [wb, 2011-05-26]
* @changes 1.0.0b21 Changed ::startErrorCapture() to allow "stacking" it via multiple calls, fixed a couple of bugs with ::dump() mangling strings in the form `int(1)`, fixed mispelling of `occurred` [wb, 2011-05-09]
* @changes 1.0.0b20 Backwards Compatibility Break - Updated ::expose() to not wrap the data in HTML when running via CLI, and instead just append a newline [wb, 2011-02-24]
* @changes 1.0.0b19 Added detection of AIX to ::checkOS() [wb, 2011-01-19]
* @changes 1.0.0b18 Updated ::expose() to be able to accept multiple parameters [wb, 2011-01-10]
* @changes 1.0.0b17 Fixed a bug with ::backtrace() triggering notices when an argument is not UTF-8 [wb, 2010-08-17]
* @changes 1.0.0b16 Added the `$types` and `$regex` parameters to ::startErrorCapture() and the `$regex` parameter to ::stopErrorCapture() [wb, 2010-08-09]
* @changes 1.0.0b15 Added ::startErrorCapture() and ::stopErrorCapture() [wb, 2010-07-05]
* @changes 1.0.0b14 Changed ::enableExceptionHandling() to only call fException::printMessage() when the destination is not `html` and no callback has been defined, added ::configureSMTP() to allow using fSMTP for error and exception emails [wb, 2010-06-04]
* @changes 1.0.0b13 Added the `$backtrace` parameter to ::backtrace() [wb, 2010-03-05]
* @changes 1.0.0b12 Added ::getDebug() to check for the global debugging flag, added more specific BSD checks to ::checkOS() [wb, 2010-03-02]
* @changes 1.0.0b11 Added ::detectOpcodeCache() [nt+wb, 2009-10-06]
* @changes 1.0.0b10 Fixed ::expose() to properly display when output includes non-UTF-8 binary data [wb, 2009-06-29]
* @changes 1.0.0b9 Added ::disableContext() to remove context info for exception/error handling, tweaked output for exceptions/errors [wb, 2009-06-28]
* @changes 1.0.0b8 ::enableErrorHandling() and ::enableExceptionHandling() now accept multiple email addresses, and a much wider range of emails [wb-imarc, 2009-06-01]
* @changes 1.0.0b7 ::backtrace() now properly replaces document root with {doc_root} on Windows [wb, 2009-05-02]
* @changes 1.0.0b6 Fixed a bug with getting the server name for error messages when running on the command line [wb, 2009-03-11]
* @changes 1.0.0b5 Fixed a bug with checking the error/exception destination when a log file is specified [wb, 2009-03-07]
* @changes 1.0.0b4 Backwards compatibility break - ::getOS() and ::getPHPVersion() removed, replaced with ::checkOS() and ::checkVersion() [wb, 2009-02-16]
* @changes 1.0.0b3 ::handleError() now displays what kind of error occurred as the heading [wb, 2009-02-15]
* @changes 1.0.0b2 Added ::registerDebugCallback() [wb, 2009-02-07]
* @changes 1.0.0b The initial implementation [wb, 2007-09-25]
*/
class fCore
{
// The following constants allow for nice looking callbacks to static methods
const backtrace = 'fCore::backtrace';
const call = 'fCore::call';
const callback = 'fCore::callback';
const checkOS = 'fCore::checkOS';
const checkVersion = 'fCore::checkVersion';
const configureSMTP = 'fCore::configureSMTP';
const debug = 'fCore::debug';
const disableContext = 'fCore::disableContext';
const dump = 'fCore::dump';
const enableDebugging = 'fCore::enableDebugging';
const enableDynamicConstants = 'fCore::enableDynamicConstants';
const enableErrorHandling = 'fCore::enableErrorHandling';
const enableExceptionHandling = 'fCore::enableExceptionHandling';
const expose = 'fCore::expose';
const getDebug = 'fCore::getDebug';
const handleError = 'fCore::handleError';
const handleFatalError = 'fCore::handleFatalError';
const handleException = 'fCore::handleException';
const registerDebugCallback = 'fCore::registerDebugCallback';
const reset = 'fCore::reset';
const sendMessagesOnShutdown = 'fCore::sendMessagesOnShutdown';
const startErrorCapture = 'fCore::startErrorCapture';
const stopErrorCapture = 'fCore::stopErrorCapture';


/**
* The nesting level of error capturing
*
* @var integer
*/
static private $captured_error_level = 0;

/**
* A stack of regex to match errors to capture, one string per level
*
* @var array
*/
static private $captured_error_regex = array();

/**
* A stack of the types of errors to capture, one integer per level
*
* @var array
*/
static private $captured_error_types = array();

/**
* A stack of arrays of errors that have been captured, one array per level
*
* @var array
*/
static private $captured_errors = array();

/**
* A stack of the previous error handler, one callback per level
*
* @var array
*/
static private $captured_errors_previous_handler = array();

/**
* If the context info has been shown
*
* @var boolean
*/
static private $context_shown = FALSE;

/**
* If global debugging is enabled
*
* @var boolean
*/
static private $debug = NULL;

/**
* A callback to pass debug messages to
*
* @var callback
*/
static private $debug_callback = NULL;

/**
* If dynamic constants should be created
*
* @var boolean
*/
static private $dynamic_constants = FALSE;

/**
* Error destination
*
* @var string
*/
static private $error_destination = 'html';

/**
* An array of errors to be send to the destination upon page completion
*
* @var array
*/
static private $error_message_queue = array();

/**
* Exception destination
*
* @var string
*/
static private $exception_destination = 'html';

/**
* Exception handler callback
*
* @var mixed
*/
static private $exception_handler_callback = NULL;

/**
* Exception handler callback parameters
*
* @var array
*/
static private $exception_handler_parameters = array();

/**
* The message generated by the uncaught exception
*
* @var string
*/
static private $exception_message = NULL;

/**
* If this class is handling errors
*
* @var boolean
*/
static private $handles_errors = FALSE;

/**
* If this class is handling exceptions
*
* @var boolean
*/
static private $handles_exceptions = FALSE;

/**
* If the context info should be shown with errors/exceptions
*
* @var boolean
*/
static private $show_context = TRUE;

/**
* An array of the most significant lines from error and exception backtraces
*
* @var array
*/
static private $significant_error_lines = array();

/**
* An SMTP connection for sending error and exception emails
*
* @var fSMTP
*/
static private $smtp_connection = NULL;

/**
* The email address to send error emails from
*
* @var string
*/
static private $smtp_from_email = NULL;


/**
* Creates a nicely formatted backtrace to the the point where this method is called
*
* @param integer $remove_lines The number of trailing lines to remove from the backtrace
* @param array $backtrace A backtrace from [http://php.net/backtrace `debug_backtrace()`] to format - this is not usually required or desired
* @return string The formatted backtrace
*/
static public function backtrace($remove_lines=0, $backtrace=NULL)
{
if ($remove_lines !== NULL && !is_numeric($remove_lines)) {
$remove_lines = 0;
}

settype($remove_lines, 'integer');

$doc_root = realpath($_SERVER['DOCUMENT_ROOT']);
$doc_root .= (substr($doc_root, -1) != DIRECTORY_SEPARATOR) ? DIRECTORY_SEPARATOR : '';

if ($backtrace === NULL) {
$backtrace = debug_backtrace();
}

while ($remove_lines > 0) {
array_shift($backtrace);
$remove_lines--;
}

$backtrace = array_reverse($backtrace);

$bt_string = '';
$i = 0;
foreach ($backtrace as $call) {
if ($i) {
$bt_string .= "\n";
}
if (isset($call['file'])) {
$bt_string .= str_replace($doc_root, '{doc_root}' . DIRECTORY_SEPARATOR, $call['file']) . '(' . $call['line'] . '): ';
} else {
$bt_string .= '[internal function]: ';
}
if (isset($call['class'])) {
$bt_string .= $call['class'] . $call['type'];
}
if (isset($call['class']) || isset($call['function'])) {
$bt_string .= $call['function'] . '(';
$j = 0;
if (!isset($call['args'])) {
$call['args'] = array();
}
foreach ($call['args'] as $arg) {
if ($j) {
$bt_string .= ', ';
}
if (is_bool($arg)) {
$bt_string .= ($arg) ? 'true' : 'false';
} elseif (is_null($arg)) {
$bt_string .= 'NULL';
} elseif (is_array($arg)) {
$bt_string .= 'Array';
} elseif (is_object($arg)) {
$bt_string .= 'Object(' . get_class($arg) . ')';
} elseif (is_string($arg)) {
// Shorten the UTF-8 string if it is too long
if (strlen(utf8_decode($arg)) > 18) {
// If we can't match as unicode, try single byte
if (!preg_match('#^(.{0,15})#us', $arg, $short_arg)) {
preg_match('#^(.{0,15})#s', $arg, $short_arg);
}
$arg = $short_arg[0] . '...';
}
$bt_string .= "'" . $arg . "'";
} else {
$bt_string .= (string) $arg;
}
$j++;
}
$bt_string .= ')';
}
$i++;
}

return $bt_string;
}


/**
* Performs a [http://php.net/call_user_func call_user_func()], while translating PHP 5.2 static callback syntax for PHP 5.1 and 5.0
*
* Parameters can be passed either as a single array of parameters or as
* multiple parameters.
*
* {{{
* #!php
* // Passing multiple parameters in a normal fashion
* fCore::call('Class::method', TRUE, 0, 'test');
*
* // Passing multiple parameters in a parameters array
* fCore::call('Class::method', array(TRUE, 0, 'test'));
* }}}
*
* To pass parameters by reference they must be assigned to an
* array by reference and the function/method being called must accept those
* parameters by reference. If either condition is not met, the parameter
* will be passed by value.
*
* {{{
* #!php
* // Passing parameters by reference
* fCore::call('Class::method', array(&$var1, &$var2));
* }}}
*
* @param callback $callback The function or method to call
* @param array $parameters The parameters to pass to the function/method
* @return mixed The return value of the called function/method
*/
static public function call($callback, $parameters=array())
{
// Fix PHP 5.0 and 5.1 static callback syntax
if (is_string($callback) && strpos($callback, '::') !== FALSE) {
$callback = explode('::', $callback);
}

$parameters = array_slice(func_get_args(), 1);
if (sizeof($parameters) == 1 && is_array($parameters[0])) {
$parameters = $parameters[0];
}

return call_user_func_array($callback, $parameters);
}


/**
* Translates a Class::method style static method callback to array style for compatibility with PHP 5.0 and 5.1 and built-in PHP functions
*
* @param callback $callback The callback to translate
* @return array The translated callback
*/
static public function callback($callback)
{
if (is_string($callback) && strpos($callback, '::') !== FALSE) {
return explode('::', $callback);
}

return $callback;
}


/**
* Checks an error/exception destination to make sure it is valid
*
* @param string $destination The destination for the exception. An email, file or the string `'html'`.
* @return string|boolean `'email'`, `'file'`, `'html'` or `FALSE`
*/
static private function checkDestination($destination)
{
if ($destination == 'html') {
return 'html';
}

if (preg_match('~^(?: # Allow leading whitespace
(?:[^\x00-\x20\(\)<>@,;:\\\\"\.\[\]]+|"[^"\\\\\n\r]+") # An "atom" or a quoted string
(?:\.[ \t]*(?:[^\x00-\x20\(\)<>@,;:\\\\"\.\[\]]+|"[^"\\\\\n\r]+"[ \t]*))* # A . plus another "atom" or a quoted string, any number of times
)@(?: # The @ symbol
(?:[a-z0-9\\-]+\.)+[a-z]{2,}| # Domain name
(?:(?:[01]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d?\d|2[0-4]\d|25[0-5]) # (or) IP addresses
)
(?:\s*,\s* # Any number of other emails separated by a comma with surrounding spaces
(?:
(?:[^\x00-\x20\(\)<>@,;:\\\\"\.\[\]]+|"[^"\\\\\n\r]+")
(?:\.[ \t]*(?:[^\x00-\x20\(\)<>@,;:\\\\"\.\[\]]+|"[^"\\\\\n\r]+"[ \t]*))*
)@(?:
(?:[a-z0-9\\-]+\.)+[a-z]{2,}|
(?:(?:[01]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d?\d|2[0-4]\d|25[0-5])
)
)*$~xiD', $destination)) {
return 'email';
}

$path_info = pathinfo($destination);
$dir_exists = file_exists($path_info['dirname']);
$dir_writable = ($dir_exists) ? is_writable($path_info['dirname']) : FALSE;
$file_exists = file_exists($destination);
$file_writable = ($file_exists) ? is_writable($destination) : FALSE;

if (!$dir_exists || ($dir_exists && ((!$file_exists && !$dir_writable) || ($file_exists && !$file_writable)))) {
return FALSE;
}

return 'file';
}


/**
* Returns is the current OS is one of the OSes passed as a parameter
*
* Valid OS strings are:
* - `'linux'`
* - `'aix'`
* - `'bsd'`
* - `'freebsd'`
* - `'netbsd'`
* - `'openbsd'`
* - `'osx'`
* - `'solaris'`
* - `'windows'`
*
* @param string $os The operating system to check - see method description for valid OSes
* @param string ...
* @return boolean If the current OS is included in the list of OSes passed as parameters
*/
static public function checkOS($os)
{
$oses = func_get_args();

$valid_oses = array('linux', 'aix', 'bsd', 'freebsd', 'openbsd', 'netbsd', 'osx', 'solaris', 'windows');

if ($invalid_oses = array_diff($oses, $valid_oses)) {
throw new fProgrammerException(
'One or more of the OSes specified, %$1s, is invalid. Must be one of: %2$s.',
join(' ', $invalid_oses),
join(', ', $valid_oses)
);
}

$uname = php_uname('s');

if (stripos($uname, 'linux') !== FALSE) {
return in_array('linux', $oses);

} elseif (stripos($uname, 'aix') !== FALSE) {
return in_array('aix', $oses);

} elseif (stripos($uname, 'netbsd') !== FALSE) {
return in_array('netbsd', $oses) || in_array('bsd', $oses);

} elseif (stripos($uname, 'openbsd') !== FALSE) {
return in_array('openbsd', $oses) || in_array('bsd', $oses);

} elseif (stripos($uname, 'freebsd') !== FALSE) {
return in_array('freebsd', $oses) || in_array('bsd', $oses);

} elseif (stripos($uname, 'solaris') !== FALSE || stripos($uname, 'sunos') !== FALSE) {
return in_array('solaris', $oses);

} elseif (stripos($uname, 'windows') !== FALSE) {
return in_array('windows', $oses);

} elseif (stripos($uname, 'darwin') !== FALSE) {
return in_array('osx', $oses);
}

throw new fEnvironmentException('Unable to determine the current OS');
}


/**
* Checks to see if the running version of PHP is greater or equal to the version passed
*
* @return boolean If the running version of PHP is greater or equal to the version passed
*/
static public function checkVersion($version)
{
static $running_version = NULL;

if ($running_version === NULL) {
$running_version = preg_replace(
'#^(\d+\.\d+\.\d+).*$#D',
'\1',
PHP_VERSION
);
}

return version_compare($running_version, $version, '>=');
}


/**
* Composes text using fText if loaded
*
* @param string $message The message to compose
* @param mixed $component A string or number to insert into the message
* @param mixed ...
* @return string The composed and possible translated message
*/
static private function compose($message)
{
$args = array_slice(func_get_args(), 1);

if (class_exists('fText', FALSE)) {
return call_user_func_array(
array('fText', 'compose'),
array($message, $args)
);
} else {
return vsprintf($message, $args);
}
}


/**
* Sets an fSMTP object to be used for sending error and exception emails
*
* @param fSMTP $smtp The SMTP connection to send emails over
* @param string $from_email The email address to use in the `From:` header
* @return void
*/
static public function configureSMTP($smtp, $from_email)
{
self::$smtp_connection = $smtp;
self::$smtp_from_email = $from_email;
}


/**
* Prints a debugging message if global or code-specific debugging is enabled
*
* @param string $message The debug message
* @param boolean $force If debugging should be forced even when global debugging is off
* @return void
*/
static public function debug($message, $force=FALSE)
{
if ($force || self::$debug) {
if (self::$debug_callback) {
call_user_func(self::$debug_callback, $message);
} else {
self::expose($message);
}
}
}


/**
* Creates a string representation of any variable using predefined strings for booleans, `NULL` and empty strings
*
* The string output format of this method is very similar to the output of
* [http://php.net/print_r print_r()] except that the following values
* are represented as special strings:
*
* - `TRUE`: `'{true}'`
* - `FALSE`: `'{false}'`
* - `NULL`: `'{null}'`
* - `''`: `'{empty_string}'`
*
* @param mixed $data The value to dump
* @return string The string representation of the value
*/
static public function dump($data)
{
if (is_bool($data)) {
return ($data) ? '{true}' : '{false}';

} elseif (is_null($data)) {
return '{null}';

} elseif ($data === '') {
return '{empty_string}';

} elseif (is_array($data) || is_object($data)) {

ob_start();
var_dump($data);
$output = ob_get_contents();
ob_end_clean();

// Make the var dump more like a print_r
$output = preg_replace('#=>\n( )+(?=[a-zA-Z]|&)#m', ' => ', $output);
$output = str_replace('string(0) ""', '{empty_string}', $output);
$output = preg_replace('#=> (&)?NULL#', '=> \1{null}', $output);
$output = preg_replace('#=> (&)?bool\((false|true)\)#', '=> \1{\2}', $output);
$output = preg_replace('#(?<=^|\] => )(?:float|int)\((-?\d+(?:.\d+)?)\)#', '\1', $output);
$output = preg_replace('#string\(\d+\) "#', '', $output);
$output = preg_replace('#"(\n( )*)(?=\[|\})#', '\1', $output);
$output = preg_replace('#((?: )+)\["(.*?)"\]#', '\1[\2]', $output);
$output = preg_replace('#(?:&)?array\(\d+\) \{\n((?: )*)((?: )(?=\[)|(?=\}))#', "Array\n\\1(\n\\1\\2", $output);
$output = preg_replace('/object\((\w+)\)#\d+ \(\d+\) {\n((?: )*)((?: )(?=\[)|(?=\}))/', "\\1 Object\n\\2(\n\\2\\3", $output);
$output = preg_replace('#^((?: )+)}(?=\n|$)#m', "\\1)\n", $output);
$output = substr($output, 0, -2) . ')';

// Fix indenting issues with the var dump output
$output_lines = explode("\n", $output);
$new_output = array();
$stack = 0;
foreach ($output_lines as $line) {
if (preg_match('#^((?: )*)([^ ])#', $line, $match)) {
$spaces = strlen($match[1]);
if ($spaces && $match[2] == '(') {
$stack += 1;
}
$new_output[] = str_pad('', ($spaces)+(4*$stack)) . $line;
if ($spaces && $match[2] == ')') {
$stack -= 1;
}
} else {
$new_output[] = str_pad('', ($spaces)+(4*$stack)) . $line;
}
}

return join("\n", $new_output);

} else {
return (string) $data;
}
}


/**
* Disables including the context information with exception and error messages
*
* The context information includes the following superglobals:
*
* - `$_SERVER`
* - `$_POST`
* - `$_GET`
* - `$_SESSION`
* - `$_FILES`
* - `$_COOKIE`
*
* @return void
*/
static public function disableContext()
{
self::$show_context = FALSE;
}


/**
* Enables debug messages globally, i.e. they will be shown for any call to ::debug()
*
* @param boolean $flag If debugging messages should be shown
* @return void
*/
static public function enableDebugging($flag)
{
self::$debug = (boolean) $flag;
}


/**
* Turns on a feature where undefined constants are automatically created with the string value equivalent to the name
*
* This functionality only works if ::enableErrorHandling() has been
* called first. This functionality may have a very slight performance
* impact since a `E_STRICT` error message must be captured and then a
* call to [http://php.net/define define()] is made.
*
* @return void
*/
static public function enableDynamicConstants()
{
if (!self::$handles_errors) {
throw new fProgrammerException(
'Dynamic constants can not be enabled unless error handling has been enabled via %s',
__CLASS__ . '::enableErrorHandling()'
);
}
self::$dynamic_constants = TRUE;
}


/**
* Turns on developer-friendly error handling that includes context information including a backtrace and superglobal dumps
*
* All errors that match the current
* [http://php.net/error_reporting error_reporting()] level will be
* redirected to the destination and will include a full backtrace. In
* addition, dumps of the following superglobals will be made to aid in
* debugging:
*
* - `$_SERVER`
* - `$_POST`
* - `$_GET`
* - `$_SESSION`
* - `$_FILES`
* - `$_COOKIE`
*
* The superglobal dumps are only done once per page, however a backtrace
* in included for each error.
*
* If an email address is specified for the destination, only one email
* will be sent per script execution. If both error and
* [enableExceptionHandling() exception handling] are set to the same
* email address, the email will contain both errors and exceptions.
*
* @param string $destination The destination for the errors and context information - an email address, a file path or the string `'html'`
* @param boolean $handle_fatal_errors If true, a shutdown function will be registered to handle a fatal error.
* Be aware, other shutdown functions could inadvertantly disable this one or exit the process.
*
* @return void
*/
static public function enableErrorHandling($destination, $handle_fatal_errors=FALSE)
{
if (!self::checkDestination($destination)) {
return;
}
self::$error_destination = $destination;
self::$handles_errors = TRUE;
set_error_handler(self::callback(self::handleError));

if ($handle_fatal_errors) {
register_shutdown_function(self::callback(self::handleFatalError));
}

}


/**
* Turns on developer-friendly uncaught exception handling that includes context information including a backtrace and superglobal dumps
*
* Any uncaught exception will be redirected to the destination specified,
* and the page will execute the `$closing_code` callback before exiting.
* The destination will receive a message with the exception messaage, a
* full backtrace and dumps of the following superglobals to aid in
* debugging:
*
* - `$_SERVER`
* - `$_POST`
* - `$_GET`
* - `$_SESSION`
* - `$_FILES`
* - `$_COOKIE`
*
* The superglobal dumps are only done once per page, however a backtrace
* in included for each error.
*
* If an email address is specified for the destination, only one email
* will be sent per script execution.
*
* If an email address is specified for the destination, only one email
* will be sent per script execution. If both exception and
* [enableErrorHandling() error handling] are set to the same
* email address, the email will contain both exceptions and errors.
*
* @param string $destination The destination for the exception and context information - an email address, a file path or the string `'html'`
* @param callback $closing_code This callback will happen after the exception is handled and before page execution stops. Good for printing a footer. If no callback is provided and the exception extends fException, fException::printMessage() will be called.
* @param array $parameters The parameters to send to `$closing_code`
* @return void
*/
static public function enableExceptionHandling($destination, $closing_code=NULL, $parameters=array())
{
if (!self::checkDestination($destination)) {
return;
}
self::$handles_exceptions = TRUE;
self::$exception_destination = $destination;
self::$exception_handler_callback = $closing_code;
if (!is_object($parameters)) {
settype($parameters, 'array');
} else {
$parameters = array($parameters);
}
self::$exception_handler_parameters = $parameters;
set_exception_handler(self::callback(self::handleException));
}


/**
* Prints the ::dump() of a value
*
* The dump will be printed in a `<pre>` tag with the class `exposed` if
* PHP is running anywhere but via the command line (cli). If PHP is
* running via the cli, the data will be printed, followed by a single
* line break (`\n`).
*
* If multiple parameters are passed, they are exposed as an array.
*
* @param mixed $data The value to show
* @param mixed ...
* @return void
*/
static public function expose($data)
{
$args = func_get_args();
if (count($args) > 1) {
$data = $args;
}
if (PHP_SAPI != 'cli') {
echo '<pre class="exposed">' . htmlspecialchars((string) self::dump($data), ENT_QUOTES) . '</pre>';
} else {
echo self::dump($data) . "\n";
}
}


/**
* Generates some information about the context of an error or exception
*
* @return string A string containing `$_SERVER`, `$_GET`, `$_POST`, `$_FILES`, `$_SESSION` and `$_COOKIE`
*/
static public function generateContext()
{
return self::compose('Context') . "\n-------" .
"\n\n\$_SERVER: " . self::dump($_SERVER) .
"\n\n\$_POST: " . self::dump($_POST) .
"\n\n\$_GET: " . self::dump($_GET) .
"\n\n\$_FILES: " . self::dump($_FILES) .
"\n\n\$_SESSION: " . self::dump((isset($_SESSION)) ? $_SESSION : NULL) .
"\n\n\$_COOKIE: " . self::dump($_COOKIE);
}


/**
* If debugging is enabled
*
* @param boolean $force If debugging is forced
* @return boolean If debugging is enabled
*/
static public function getDebug($force=FALSE)
{
return self::$debug || $force;
}

/**
* A shutdown function to handle a fatal error
*
* @internal
*
* @return void
*/
static public function handleFatalError()
{
$error = error_get_last();

$allowed_error_types = array(
E_ERROR, E_CORE_ERROR
);

if ($error != NULL && in_array($error['type'], $allowed_error_types)) {
$error_number = $error['type'];
$error_file = $error['file'];
$error_line = $error['line'];
$error_string = $error['message'];

self::handleError($error_number, $error_string, $error_file, $error_line);
}
}

/**
* Handles an error, creating the necessary context information and sending it to the specified destination
*
* @internal
*
* @param integer $error_number The error type
* @param string $error_string The message for the error
* @param string $error_file The file the error occurred in
* @param integer $error_line The line the error occurred on
* @param array $error_context A references to all variables in scope at the occurence of the error
* @return void
*/
static public function handleError($error_number, $error_string, $error_file=NULL, $error_line=NULL, $error_context=NULL)
{
if (self::$dynamic_constants && $error_number == E_NOTICE) {
if (preg_match("#^Use of undefined constant (\w+) - assumed '\w+'\$#D", $error_string, $matches)) {
define($matches[1], $matches[1]);
return;
}
}

$capturing = (bool) self::$captured_error_level;
$level_match = (bool) (error_reporting() & $error_number);

if (!$capturing && !$level_match) {
return;
}

$doc_root = realpath($_SERVER['DOCUMENT_ROOT']);
$doc_root .= (substr($doc_root, -1) != '/' && substr($doc_root, -1) != '\\') ? '/' : '';

$backtrace = self::backtrace(1);

// Remove the reference to handleError
$backtrace = preg_replace('#: fCore::handleError\(.*?\)$#', '', $backtrace);

$error_string = preg_replace('# \[<a href=\'.*?</a>\]: #', ': ', $error_string);

// This was added in 5.2
if (!defined('E_RECOVERABLE_ERROR')) {
define('E_RECOVERABLE_ERROR', 4096);
}

// These were added in 5.3
if (!defined('E_DEPRECATED')) {
define('E_DEPRECATED', 8192);
}

if (!defined('E_USER_DEPRECATED')) {
define('E_USER_DEPRECATED', 16384);
}

switch ($error_number) {
case E_WARNING: $type = self::compose('Warning'); break;
case E_NOTICE: $type = self::compose('Notice'); break;
case E_USER_ERROR: $type = self::compose('User Error'); break;
case E_USER_WARNING: $type = self::compose('User Warning'); break;
case E_USER_NOTICE: $type = self::compose('User Notice'); break;
case E_STRICT: $type = self::compose('Strict'); break;
case E_RECOVERABLE_ERROR: $type = self::compose('Recoverable Error'); break;
case E_DEPRECATED: $type = self::compose('Deprecated'); break;
case E_USER_DEPRECATED: $type = self::compose('User Deprecated'); break;
case E_CORE_ERROR: $type = self::compose('Fatal Error'); break;
case E_ERROR: $type = self::compose('Fatal Error'); break;
}

if ($capturing) {
$type_to_capture = (bool) (self::$captured_error_types[self::$captured_error_level] & $error_number);
$string_to_capture = !self::$captured_error_regex[self::$captured_error_level] || (self::$captured_error_regex[self::$captured_error_level] && preg_match(self::$captured_error_regex[self::$captured_error_level], $error_string));
if ($type_to_capture && $string_to_capture) {
self::$captured_errors[self::$captured_error_level][] = array(
'number' => $error_number,
'type' => $type,
'string' => $error_string,
'file' => str_replace($doc_root, '{doc_root}/', $error_file),
'line' => $error_line,
'backtrace' => $backtrace,
'context' => $error_context
);
return;
}

// If the old handler is not this method, then we must have been trying to match a regex and failed
// so we pass the error on to the original handler to do its thing
if (self::$captured_errors_previous_handler[self::$captured_error_level] != array('fCore', 'handleError')) {
if (self::$captured_errors_previous_handler[self::$captured_error_level] === NULL) {
return FALSE;
}
return call_user_func(self::$captured_errors_previous_handler[self::$captured_error_level], $error_number, $error_string, $error_file, $error_line, $error_context);

// If we get here, this method is the error handler, but we don't want to actually report the error so we return
} elseif (!$level_match) {
return;
}
}

$error = $type . "\n" . str_pad('', strlen($type), '-') . "\n" . $backtrace . "\n" . $error_string;

$backtrace_lines = explode("\n", $backtrace);

self::sendMessageToDestination('error', $error, end($backtrace_lines));
}


/**
* Handles an uncaught exception, creating the necessary context information, sending it to the specified destination and finally executing the closing callback
*
* @internal
*
* @param object $exception The uncaught exception to handle
* @return void
*/
static public function handleException($exception)
{
$message = ($exception->getMessage()) ? $exception->getMessage() : '{no message}';
if ($exception instanceof fException) {
$trace = $exception->formatTrace();
} else {
$trace = $exception->getTraceAsString();
}
$code = ($exception->getCode()) ? ' (code ' . $exception->getCode() . ')' : '';

$info = $trace . "\n" . $message . $code;
$headline = self::compose("Uncaught") . " " . get_class($exception);
$info_block = $headline . "\n" . str_pad('', strlen($headline), '-') . "\n" . trim($info);

$trace_lines = explode("\n", $trace);

self::sendMessageToDestination('exception', $info_block, end($trace_lines));

if (self::$exception_handler_callback === NULL) {
if (self::$exception_destination != 'html' && $exception instanceof fException) {
$exception->printMessage();
}
return;
}

try {
self::call(self::$exception_handler_callback, self::$exception_handler_parameters);
} catch (Exception $e) {
trigger_error(
self::compose(
'An exception was thrown in the %s closing code callback',
'setExceptionHandling()'
),
E_USER_ERROR
);
}
}


/**
* Registers a callback to handle debug messages instead of the default action of calling ::expose() on the message
*
* @param callback $callback A callback that accepts a single parameter, the string debug message to handle
* @return void
*/
static public function registerDebugCallback($callback)
{
self::$debug_callback = self::callback($callback);
}


/**
* Resets the configuration of the class
*
* @internal
*
* @return void
*/
static public function reset()
{
if (self::$handles_errors) {
restore_error_handler();
}
if (self::$handles_exceptions) {
restore_exception_handler();
}

if (is_array(self::$captured_errors)) {
restore_error_handler();
}

self::$captured_error_level = 0;
self::$captured_error_regex = array();
self::$captured_error_types = array();
self::$captured_errors = array();
self::$captured_errors_previous_handler = array();
self::$context_shown = FALSE;
self::$debug = NULL;
self::$debug_callback = NULL;
self::$dynamic_constants = FALSE;
self::$error_destination = 'html';
self::$error_message_queue = array();
self::$exception_destination = 'html';
self::$exception_handler_callback = NULL;
self::$exception_handler_parameters = array();
self::$exception_message = NULL;
self::$handles_errors = FALSE;
self::$handles_exceptions = FALSE;
self::$significant_error_lines = array();
self::$show_context = TRUE;
self::$smtp_connection = NULL;
self::$smtp_from_email = NULL;
}


/**
* Sends an email or writes a file with messages generated during the page execution
*
* This method prevents multiple emails from being sent or a log file from
* being written multiple times for one script execution.
*
* @internal
*
* @return void
*/
static public function sendMessagesOnShutdown()
{
$messages = array();

if (self::$error_message_queue) {
$message = join("\n\n", self::$error_message_queue);
$messages[self::$error_destination] = $message;
}

if (self::$exception_message) {
if (isset($messages[self::$exception_destination])) {
$messages[self::$exception_destination] .= "\n\n";
} else {
$messages[self::$exception_destination] = '';
}
$messages[self::$exception_destination] .= self::$exception_message;
}

$hash = md5(join('', self::$significant_error_lines), TRUE);
$hash = strtr(base64_encode($hash), '/', '-');
$hash = substr(rtrim($hash, '='), 0, 8);

$first_file_line = preg_replace(
'#^.*[/\\\\](.*)$#',
'\1',
reset(self::$significant_error_lines)
);

$subject = self::compose(
'[%1$s] %2$s error(s) beginning at %3$s {%4$s}',
isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : php_uname('n'),
count($messages),
$first_file_line,
$hash
);

foreach ($messages as $destination => $message) {
if (self::$show_context) {
$message .= "\n\n" . self::generateContext();
}

if (self::checkDestination($destination) == 'email') {
if (self::$smtp_connection) {
$email = new fEmail();
foreach (explode(',', $destination) as $recipient) {
$email->addRecipient($recipient);
}
$email->setFromEmail(self::$smtp_from_email);
$email->setSubject($subject);
$email->setBody($message);
$email->send(self::$smtp_connection);
} else {
mail($destination, $subject, $message);
}

} else {
$handle = fopen($destination, 'a');
fwrite($handle, $subject . "\n\n");
fwrite($handle, $message . "\n\n");
fclose($handle);
}
}
}


/**
* Handles sending a message to a destination
*
* If the destination is an email address or file, the messages will be
* spooled up until the end of the script execution to prevent multiple
* emails from being sent or a log file being written to multiple times.
*
* @param string $type If the message is an error or an exception
* @param string $message The message to send to the destination
* @param string $significant_line The most significant line from an error or exception backtrace
* @return void
*/
static private function sendMessageToDestination($type, $message, $significant_line)
{
$destination = ($type == 'exception') ? self::$exception_destination : self::$error_destination;

if ($destination == 'html') {
if (self::$show_context && !self::$context_shown) {
self::expose(self::generateContext());
self::$context_shown = TRUE;
}
self::expose($message);
return;
}

static $registered_function = FALSE;
if (!$registered_function) {
register_shutdown_function(self::callback(self::sendMessagesOnShutdown));
$registered_function = TRUE;
}

if ($type == 'error') {
self::$error_message_queue[] = $message;
} else {
self::$exception_message = $message;
}

self::$significant_error_lines[] = $significant_line;
}


/**
* Temporarily enables capturing error messages
*
* @param integer $types The error types to capture - this should be as specific as possible - defaults to all (E_ALL | E_STRICT)
* @param string $regex A PCRE regex to match against the error message
* @return void
*/
static public function startErrorCapture($types=NULL, $regex=NULL)
{
if ($types === NULL) {
$types = E_ALL | E_STRICT;
}

self::$captured_error_level++;

self::$captured_error_regex[self::$captured_error_level] = $regex;
self::$captured_error_types[self::$captured_error_level] = $types;
self::$captured_errors[self::$captured_error_level] = array();
self::$captured_errors_previous_handler[self::$captured_error_level] = set_error_handler(self::callback(self::handleError));
}


/**
* Stops capturing error messages, returning all that have been captured
*
* @param string $regex A PCRE regex to filter messages by
* @return array The captured error messages
*/
static public function stopErrorCapture($regex=NULL)
{
$captures = self::$captured_errors[self::$captured_error_level];

self::$captured_error_level--;

self::$captured_error_regex = array_slice(self::$captured_error_regex, 0, self::$captured_error_level, TRUE);
self::$captured_error_types = array_slice(self::$captured_error_types, 0, self::$captured_error_level, TRUE);
self::$captured_errors = array_slice(self::$captured_errors, 0, self::$captured_error_level, TRUE);
self::$captured_errors_previous_handler = array_slice(self::$captured_errors_previous_handler, 0, self::$captured_error_level, TRUE);

restore_error_handler();

if ($regex) {
$new_captures = array();
foreach ($captures as $capture) {
if (!preg_match($regex, $capture['string'])) { continue; }
$new_captures[] = $capture;
}
$captures = $new_captures;
}

return $captures;
}


/**
* Forces use as a static class
*
* @return fCore
*/
private function __construct() { }
}



/**
* Copyright (c) 2007-2011 Will Bond <will@flourishlib.com>, others
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
Something went wrong with that request. Please try again.