1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831 |
- <?php
- namespace KIF\Debug;
- use ErrorException, Exception, ReflectionClass;
- if (!defined('E_STRICT')) {
- define('E_STRICT', 2048);
- }
- if (!defined('E_RECOVERABLE_ERROR')) {
- define('E_RECOVERABLE_ERROR', 4096);
- }
- if (!defined('E_DEPRECATED')) {
- define('E_DEPRECATED', 8192);
- }
- if (!defined('E_USER_DEPRECATED')) {
- define('E_USER_DEPRECATED', 16384);
- }
-
- class FirePHP {
-
- const VERSION = '0.3';
-
- const LOG = 'LOG';
-
-
- const INFO = 'INFO';
-
-
- const WARN = 'WARN';
-
-
- const ERROR = 'ERROR';
-
-
- const DUMP = 'DUMP';
-
-
- const TRACE = 'TRACE';
-
-
- const EXCEPTION = 'EXCEPTION';
-
-
- const TABLE = 'TABLE';
-
-
- const GROUP_START = 'GROUP_START';
-
-
- const GROUP_END = 'GROUP_END';
-
-
- protected static $instance = null;
-
-
- protected $inExceptionHandler = false;
-
-
- protected $throwErrorExceptions = true;
-
-
- protected $convertAssertionErrorsToExceptions = true;
-
-
- protected $throwAssertionExceptions = false;
-
- protected $messageIndex = 1;
-
-
- protected $options = array('maxDepth' => 10,
- 'maxObjectDepth' => 5,
- 'maxArrayDepth' => 5,
- 'useNativeJsonEncode' => true,
- 'includeLineNumbers' => true);
-
- protected $objectFilters = array(
- 'firephp' => array('objectStack', 'instance', 'json_objectStack'),
- 'firephp_test_class' => array('objectStack', 'instance', 'json_objectStack')
- );
-
- protected $objectStack = array();
-
- protected $enabled = true;
-
- protected $logToInsightConsole = null;
-
-
- public function __sleep()
- {
- return array('options', 'objectFilters', 'enabled');
- }
-
-
- public static function getInstance($autoCreate = false)
- {
- if ($autoCreate === true && !self::$instance) {
- self::init();
- }
- return self::$instance;
- }
-
-
- public static function init()
- {
- return self::setInstance(new self());
- }
-
- public static function setInstance($instance)
- {
- return self::$instance = $instance;
- }
-
- public function setLogToInsightConsole($console)
- {
- if (is_string($console)) {
- if (get_class($this) != 'FirePHP_Insight' && !is_subclass_of($this, 'FirePHP_Insight')) {
- throw new Exception('FirePHP instance not an instance or subclass of FirePHP_Insight!');
- }
- $this->logToInsightConsole = $this->to('request')->console($console);
- } else {
- $this->logToInsightConsole = $console;
- }
- }
-
- public function setEnabled($enabled)
- {
- $this->enabled = $enabled;
- }
-
-
- public function getEnabled()
- {
- return $this->enabled;
- }
-
-
- public function setObjectFilter($class, $filter)
- {
- $this->objectFilters[strtolower($class)] = $filter;
- }
-
-
- public function setOptions($options)
- {
- $this->options = array_merge($this->options, $options);
- }
-
- public function getOptions()
- {
- return $this->options;
- }
-
-
- public function setOption($name, $value)
- {
- if (!isset($this->options[$name])) {
- throw $this->newException('Unknown option: ' . $name);
- }
- $this->options[$name] = $value;
- }
-
- public function getOption($name)
- {
- if (!isset($this->options[$name])) {
- throw $this->newException('Unknown option: ' . $name);
- }
- return $this->options[$name];
- }
-
- public function registerErrorHandler($throwErrorExceptions = false)
- {
-
-
-
-
-
- $this->throwErrorExceptions = $throwErrorExceptions;
-
- return set_error_handler(array($this, 'errorHandler'));
- }
-
- public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
- {
-
- if (error_reporting() == 0) {
- return;
- }
-
- if (error_reporting() & $errno) {
- $exception = new ErrorException($errstr, 0, $errno, $errfile, $errline);
- if ($this->throwErrorExceptions) {
- throw $exception;
- } else {
- $this->fb($exception);
- }
- }
- }
-
-
- public function registerExceptionHandler()
- {
- return set_exception_handler(array($this, 'exceptionHandler'));
- }
-
-
- function exceptionHandler($exception)
- {
- $this->inExceptionHandler = true;
-
- header('HTTP/1.1 500 Internal Server Error');
-
- try {
- $this->fb($exception);
- } catch (Exception $e) {
- echo 'We had an exception: ' . $e;
- }
-
- $this->inExceptionHandler = false;
- }
-
-
- public function registerAssertionHandler($convertAssertionErrorsToExceptions = true, $throwAssertionExceptions = false)
- {
- $this->convertAssertionErrorsToExceptions = $convertAssertionErrorsToExceptions;
- $this->throwAssertionExceptions = $throwAssertionExceptions;
-
- if ($throwAssertionExceptions && !$convertAssertionErrorsToExceptions) {
- throw $this->newException('Cannot throw assertion exceptions as assertion errors are not being converted to exceptions!');
- }
-
- return assert_options(ASSERT_CALLBACK, array($this, 'assertionHandler'));
- }
-
-
- public function assertionHandler($file, $line, $code)
- {
- if ($this->convertAssertionErrorsToExceptions) {
-
- $exception = new ErrorException('Assertion Failed - Code[ ' . $code . ' ]', 0, null, $file, $line);
-
- if ($this->throwAssertionExceptions) {
- throw $exception;
- } else {
- $this->fb($exception);
- }
-
- } else {
- $this->fb($code, 'Assertion Failed', FirePHP::ERROR, array('File' => $file, 'Line' => $line));
- }
- }
-
-
- public function group($name, $options = null)
- {
-
- if (!$name) {
- throw $this->newException('You must specify a label for the group!');
- }
- if ($options) {
- if (!is_array($options)) {
- throw $this->newException('Options must be defined as an array!');
- }
- if (array_key_exists('Collapsed', $options)) {
- $options['Collapsed'] = ($options['Collapsed']) ? 'true' : 'false';
- }
- }
- return $this->fb(null, $name, FirePHP::GROUP_START, $options);
- }
-
-
- public function groupEnd()
- {
- return $this->fb(null, null, FirePHP::GROUP_END);
- }
-
- public function log($object, $label = null, $options = array())
- {
- return $this->fb($object, $label, FirePHP::LOG, $options);
- }
-
- public function info($object, $label = null, $options = array())
- {
- return $this->fb($object, $label, FirePHP::INFO, $options);
- }
-
- public function warn($object, $label = null, $options = array())
- {
- return $this->fb($object, $label, FirePHP::WARN, $options);
- }
-
- public function error($object, $label = null, $options = array())
- {
- return $this->fb($object, $label, FirePHP::ERROR, $options);
- }
-
- public function dump($key, $variable, $options = array())
- {
- if (!is_string($key)) {
- throw $this->newException('Key passed to dump() is not a string');
- }
- if (strlen($key) > 100) {
- throw $this->newException('Key passed to dump() is longer than 100 characters');
- }
- if (!preg_match_all('/^[a-zA-Z0-9-_\.:]*$/', $key, $m)) {
- throw $this->newException('Key passed to dump() contains invalid characters [a-zA-Z0-9-_\.:]');
- }
- return $this->fb($variable, $key, FirePHP::DUMP, $options);
- }
-
-
- public function trace($label)
- {
- return $this->fb($label, FirePHP::TRACE);
- }
-
- public function table($label, $table, $options = array())
- {
- return $this->fb($table, $label, FirePHP::TABLE, $options);
- }
-
- public static function to()
- {
- $instance = self::getInstance();
- if (!method_exists($instance, '_to')) {
- throw new Exception('FirePHP::to() implementation not loaded');
- }
- $args = func_get_args();
- return call_user_func_array(array($instance, '_to'), $args);
- }
-
- public static function plugin()
- {
- $instance = self::getInstance();
- if (!method_exists($instance, '_plugin')) {
- throw new Exception('FirePHP::plugin() implementation not loaded');
- }
- $args = func_get_args();
- return call_user_func_array(array($instance, '_plugin'), $args);
- }
-
- public function detectClientExtension()
- {
-
- if (@preg_match_all('/\sFirePHP\/([\.\d]*)\s?/si', $this->getUserAgent(), $m) &&
- version_compare($m[1][0], '0.0.6', '>=')) {
- return true;
- } else
-
- if (@preg_match_all('/^([\.\d]*)$/si', $this->getRequestHeader('X-FirePHP-Version'), $m) &&
- version_compare($m[1][0], '0.0.6', '>=')) {
- return true;
- }
- return false;
- }
-
-
- public function fb($object)
- {
- if ($this instanceof FirePHP_Insight && method_exists($this, '_logUpgradeClientMessage')) {
- if (!FirePHP_Insight::$upgradeClientMessageLogged) {
- $this->_logUpgradeClientMessage();
- }
- }
- static $insightGroupStack = array();
- if (!$this->getEnabled()) {
- return false;
- }
- if ($this->headersSent($filename, $linenum)) {
-
- if ($this->inExceptionHandler) {
-
- echo '<div style="border: 2px solid red; font-family: Arial; font-size: 12px; background-color: lightgray; padding: 5px;"><span style="color: red; font-weight: bold;">FirePHP ERROR:</span> Headers already sent in <b>' . $filename . '</b> on line <b>' . $linenum . '</b>. Cannot send log data to FirePHP. You must have Output Buffering enabled via ob_start() or output_buffering ini directive.</div>';
- } else {
- throw $this->newException('Headers already sent in ' . $filename . ' on line ' . $linenum . '. Cannot send log data to FirePHP. You must have Output Buffering enabled via ob_start() or output_buffering ini directive.');
- }
- }
-
- $type = null;
- $label = null;
- $options = array();
-
- if (func_num_args() == 1) {
- } else if (func_num_args() == 2) {
- switch (func_get_arg(1)) {
- case self::LOG:
- case self::INFO:
- case self::WARN:
- case self::ERROR:
- case self::DUMP:
- case self::TRACE:
- case self::EXCEPTION:
- case self::TABLE:
- case self::GROUP_START:
- case self::GROUP_END:
- $type = func_get_arg(1);
- break;
- default:
- $label = func_get_arg(1);
- break;
- }
- } else if (func_num_args() == 3) {
- $type = func_get_arg(2);
- $label = func_get_arg(1);
- } else if (func_num_args() == 4) {
- $type = func_get_arg(2);
- $label = func_get_arg(1);
- $options = func_get_arg(3);
- } else {
- throw $this->newException('Wrong number of arguments to fb() function!');
- }
- if ($this->logToInsightConsole !== null && (get_class($this) == 'FirePHP_Insight' || is_subclass_of($this, 'FirePHP_Insight'))) {
- $trace = debug_backtrace();
- if (!$trace) return false;
- for ($i = 0; $i < sizeof($trace); $i++) {
- if (isset($trace[$i]['class'])) {
- if ($trace[$i]['class'] == 'FirePHP' || $trace[$i]['class'] == 'FB') {
- continue;
- }
- }
- if (isset($trace[$i]['file'])) {
- $path = $this->_standardizePath($trace[$i]['file']);
- if (substr($path, -18, 18) == 'FirePHPCore/fb.php' || substr($path, -29, 29) == 'FirePHPCore/FirePHP.class.php') {
- continue;
- }
- }
- if (isset($trace[$i]['function']) && $trace[$i]['function'] == 'fb' &&
- isset($trace[$i - 1]['file']) && substr($this->_standardizePath($trace[$i - 1]['file']), -18, 18) == 'FirePHPCore/fb.php') {
- continue;
- }
- if (isset($trace[$i]['class']) && $trace[$i]['class'] == 'FB' &&
- isset($trace[$i - 1]['file']) && substr($this->_standardizePath($trace[$i - 1]['file']), -18, 18) == 'FirePHPCore/fb.php') {
- continue;
- }
- break;
- }
-
- $msg = $this->logToInsightConsole->option('encoder.trace.offsetAdjustment', $i);
- if ($object instanceof Exception) {
- $type = self::EXCEPTION;
- }
- if ($label && $type != self::TABLE && $type != self::GROUP_START) {
- $msg = $msg->label($label);
- }
- switch ($type) {
- case self::DUMP:
- case self::LOG:
- return $msg->log($object);
- case self::INFO:
- return $msg->info($object);
- case self::WARN:
- return $msg->warn($object);
- case self::ERROR:
- return $msg->error($object);
- case self::TRACE:
- return $msg->trace($object);
- case self::EXCEPTION:
- return $this->plugin('error')->handleException($object, $msg);
- case self::TABLE:
- if (isset($object[0]) && !is_string($object[0]) && $label) {
- $object = array($label, $object);
- }
- return $msg->table($object[0], array_slice($object[1], 1), $object[1][0]);
- case self::GROUP_START:
- $insightGroupStack[] = $msg->group(md5($label))->open();
- return $msg->log($label);
- case self::GROUP_END:
- if (count($insightGroupStack) == 0) {
- throw new ErrorException('Too many groupEnd() as opposed to group() calls!');
- }
- $group = array_pop($insightGroupStack);
- return $group->close();
- default:
- return $msg->log($object);
- }
- }
- if (!$this->detectClientExtension()) {
- return false;
- }
- $meta = array();
- $skipFinalObjectEncode = false;
-
- if ($object instanceof Exception) {
-
- $meta['file'] = $this->_escapeTraceFile($object->getFile());
- $meta['line'] = $object->getLine();
-
- $trace = $object->getTrace();
- if ($object instanceof ErrorException
- && isset($trace[0]['function'])
- && $trace[0]['function'] == 'errorHandler'
- && isset($trace[0]['class'])
- && $trace[0]['class'] == 'FirePHP') {
-
- $severity = false;
- switch ($object->getSeverity()) {
- case E_WARNING:
- $severity = 'E_WARNING';
- break;
- case E_NOTICE:
- $severity = 'E_NOTICE';
- break;
- case E_USER_ERROR:
- $severity = 'E_USER_ERROR';
- break;
- case E_USER_WARNING:
- $severity = 'E_USER_WARNING';
- break;
- case E_USER_NOTICE:
- $severity = 'E_USER_NOTICE';
- break;
- case E_STRICT:
- $severity = 'E_STRICT';
- break;
- case E_RECOVERABLE_ERROR:
- $severity = 'E_RECOVERABLE_ERROR';
- break;
- case E_DEPRECATED:
- $severity = 'E_DEPRECATED';
- break;
- case E_USER_DEPRECATED:
- $severity = 'E_USER_DEPRECATED';
- break;
- }
-
- $object = array('Class' => get_class($object),
- 'Message' => $severity . ': ' . $object->getMessage(),
- 'File' => $this->_escapeTraceFile($object->getFile()),
- 'Line' => $object->getLine(),
- 'Type' => 'trigger',
- 'Trace' => $this->_escapeTrace(array_splice($trace, 2)));
- $skipFinalObjectEncode = true;
- } else {
- $object = array('Class' => get_class($object),
- 'Message' => $object->getMessage(),
- 'File' => $this->_escapeTraceFile($object->getFile()),
- 'Line' => $object->getLine(),
- 'Type' => 'throw',
- 'Trace' => $this->_escapeTrace($trace));
- $skipFinalObjectEncode = true;
- }
- $type = self::EXCEPTION;
-
- } else if ($type == self::TRACE) {
-
- $trace = debug_backtrace();
- if (!$trace) return false;
- for ($i = 0; $i < sizeof($trace); $i++) {
-
- if (isset($trace[$i]['class'])
- && isset($trace[$i]['file'])
- && ($trace[$i]['class'] == 'FirePHP'
- || $trace[$i]['class'] == 'FB')
- && (substr($this->_standardizePath($trace[$i]['file']), -18, 18) == 'FirePHPCore/fb.php'
- || substr($this->_standardizePath($trace[$i]['file']), -29, 29) == 'FirePHPCore/FirePHP.class.php')) {
-
- } else
- if (isset($trace[$i]['class'])
- && isset($trace[$i+1]['file'])
- && $trace[$i]['class'] == 'FirePHP'
- && substr($this->_standardizePath($trace[$i + 1]['file']), -18, 18) == 'FirePHPCore/fb.php') {
-
- } else
- if ($trace[$i]['function'] == 'fb'
- || $trace[$i]['function'] == 'trace'
- || $trace[$i]['function'] == 'send') {
- $object = array('Class' => isset($trace[$i]['class']) ? $trace[$i]['class'] : '',
- 'Type' => isset($trace[$i]['type']) ? $trace[$i]['type'] : '',
- 'Function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : '',
- 'Message' => $trace[$i]['args'][0],
- 'File' => isset($trace[$i]['file']) ? $this->_escapeTraceFile($trace[$i]['file']) : '',
- 'Line' => isset($trace[$i]['line']) ? $trace[$i]['line'] : '',
- 'Args' => isset($trace[$i]['args']) ? $this->encodeObject($trace[$i]['args']) : '',
- 'Trace' => $this->_escapeTrace(array_splice($trace, $i + 1)));
-
- $skipFinalObjectEncode = true;
- $meta['file'] = isset($trace[$i]['file']) ? $this->_escapeTraceFile($trace[$i]['file']) : '';
- $meta['line'] = isset($trace[$i]['line']) ? $trace[$i]['line'] : '';
- break;
- }
- }
-
- } else
- if ($type == self::TABLE) {
-
- if (isset($object[0]) && is_string($object[0])) {
- $object[1] = $this->encodeTable($object[1]);
- } else {
- $object = $this->encodeTable($object);
- }
-
- $skipFinalObjectEncode = true;
-
- } else if ($type == self::GROUP_START) {
-
- if (!$label) {
- throw $this->newException('You must specify a label for the group!');
- }
-
- } else {
- if ($type === null) {
- $type = self::LOG;
- }
- }
-
- if ($this->options['includeLineNumbers']) {
- if (!isset($meta['file']) || !isset($meta['line'])) {
-
- $trace = debug_backtrace();
- for ($i = 0; $trace && $i < sizeof($trace); $i++) {
-
- if (isset($trace[$i]['class'])
- && isset($trace[$i]['file'])
- && ($trace[$i]['class'] == 'FirePHP'
- || $trace[$i]['class'] == 'FB')
- && (substr($this->_standardizePath($trace[$i]['file']), -18, 18) == 'FirePHPCore/fb.php'
- || substr($this->_standardizePath($trace[$i]['file']), -29, 29) == 'FirePHPCore/FirePHP.class.php')) {
-
- } else
- if (isset($trace[$i]['class'])
- && isset($trace[$i + 1]['file'])
- && $trace[$i]['class'] == 'FirePHP'
- && substr($this->_standardizePath($trace[$i + 1]['file']), -18, 18) == 'FirePHPCore/fb.php') {
-
- } else
- if (isset($trace[$i]['file'])
- && substr($this->_standardizePath($trace[$i]['file']), -18, 18) == 'FirePHPCore/fb.php') {
-
- } else {
- $meta['file'] = isset($trace[$i]['file']) ? $this->_escapeTraceFile($trace[$i]['file']) : '';
- $meta['line'] = isset($trace[$i]['line']) ? $trace[$i]['line'] : '';
- break;
- }
- }
- }
- } else {
- unset($meta['file']);
- unset($meta['line']);
- }
- $this->setHeader('X-Wf-Protocol-1', 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
- $this->setHeader('X-Wf-1-Plugin-1', 'http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/' . self::VERSION);
-
- $structureIndex = 1;
- if ($type == self::DUMP) {
- $structureIndex = 2;
- $this->setHeader('X-Wf-1-Structure-2', 'http://meta.firephp.org/Wildfire/Structure/FirePHP/Dump/0.1');
- } else {
- $this->setHeader('X-Wf-1-Structure-1', 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1');
- }
-
- if ($type == self::DUMP) {
- $msg = '{"' . $label . '":' . $this->jsonEncode($object, $skipFinalObjectEncode) . '}';
- } else {
- $msgMeta = $options;
- $msgMeta['Type'] = $type;
- if ($label !== null) {
- $msgMeta['Label'] = $label;
- }
- if (isset($meta['file']) && !isset($msgMeta['File'])) {
- $msgMeta['File'] = $meta['file'];
- }
- if (isset($meta['line']) && !isset($msgMeta['Line'])) {
- $msgMeta['Line'] = $meta['line'];
- }
- $msg = '[' . $this->jsonEncode($msgMeta) . ',' . $this->jsonEncode($object, $skipFinalObjectEncode) . ']';
- }
-
- $parts = explode("\n", chunk_split($msg, 5000, "\n"));
- for ($i = 0; $i < count($parts); $i++) {
-
- $part = $parts[$i];
- if ($part) {
-
- if (count($parts) > 2) {
-
- $this->setHeader('X-Wf-1-' . $structureIndex . '-' . '1-' . $this->messageIndex,
- (($i == 0) ? strlen($msg) : '')
- . '|' . $part . '|'
- . (($i < count($parts) - 2) ? '\\' : ''));
- } else {
- $this->setHeader('X-Wf-1-' . $structureIndex . '-' . '1-' . $this->messageIndex,
- strlen($part) . '|' . $part . '|');
- }
-
- $this->messageIndex++;
-
- if ($this->messageIndex > 99999) {
- throw $this->newException('Maximum number (99,999) of messages reached!');
- }
- }
- }
-
- $this->setHeader('X-Wf-1-Index', $this->messageIndex - 1);
-
- return true;
- }
-
-
- protected function _standardizePath($path)
- {
- return preg_replace('/\\\\+/', '/', $path);
- }
-
-
- protected function _escapeTrace($trace)
- {
- if (!$trace) return $trace;
- for ($i = 0; $i < sizeof($trace); $i++) {
- if (isset($trace[$i]['file'])) {
- $trace[$i]['file'] = $this->_escapeTraceFile($trace[$i]['file']);
- }
- if (isset($trace[$i]['args'])) {
- $trace[$i]['args'] = $this->encodeObject($trace[$i]['args']);
- }
- }
- return $trace;
- }
-
-
- protected function _escapeTraceFile($file)
- {
-
- if (strpos($file, '\\')) {
-
- $file = preg_replace('/\\\\+/', '\\', $file);
- return $file;
- }
- return $file;
- }
-
- protected function headersSent(&$filename, &$linenum)
- {
- return headers_sent($filename, $linenum);
- }
-
- protected function setHeader($name, $value)
- {
- return header($name . ': ' . $value);
- }
-
- protected function getUserAgent()
- {
- if (!isset($_SERVER['HTTP_USER_AGENT'])) return false;
- return $_SERVER['HTTP_USER_AGENT'];
- }
-
- public static function getAllRequestHeaders()
- {
- static $_cachedHeaders = false;
- if ($_cachedHeaders !== false) {
- return $_cachedHeaders;
- }
- $headers = array();
- if (function_exists('getallheaders')) {
- foreach (getallheaders() as $name => $value) {
- $headers[strtolower($name)] = $value;
- }
- } else {
- foreach ($_SERVER as $name => $value) {
- if (substr($name, 0, 5) == 'HTTP_') {
- $headers[strtolower(str_replace(' ', '-', str_replace('_', ' ', substr($name, 5))))] = $value;
- }
- }
- }
- return $_cachedHeaders = $headers;
- }
-
- protected function getRequestHeader($name)
- {
- $headers = self::getAllRequestHeaders();
- if (isset($headers[strtolower($name)])) {
- return $headers[strtolower($name)];
- }
- return false;
- }
-
- protected function newException($message)
- {
- return new Exception($message);
- }
-
-
- public function jsonEncode($object, $skipObjectEncode = false)
- {
- if (!$skipObjectEncode) {
- $object = $this->encodeObject($object);
- }
-
- if (function_exists('json_encode')
- && $this->options['useNativeJsonEncode'] != false) {
-
- return json_encode($object);
- } else {
- return $this->json_encode($object);
- }
- }
-
-
- protected function encodeTable($table)
- {
- if (!$table) return $table;
-
- $newTable = array();
- foreach ($table as $row) {
- if (is_array($row)) {
- $newRow = array();
- foreach ($row as $item) {
- $newRow[] = $this->encodeObject($item);
- }
- $newTable[] = $newRow;
- }
- }
- return $newTable;
- }
-
- protected function encodeObject($object, $objectDepth = 1, $arrayDepth = 1, $maxDepth = 1)
- {
- if ($maxDepth > $this->options['maxDepth']) {
- return '** Max Depth (' . $this->options['maxDepth'] . ') **';
- }
- $return = array();
-
- if (is_resource($object)) {
-
- return '** ' . (string) $object . ' **';
-
- } else if (is_object($object)) {
-
- if ($objectDepth > $this->options['maxObjectDepth']) {
- return '** Max Object Depth (' . $this->options['maxObjectDepth'] . ') **';
- }
-
- foreach ($this->objectStack as $refVal) {
- if ($refVal === $object) {
- return '** Recursion (' . get_class($object) . ') **';
- }
- }
- array_push($this->objectStack, $object);
-
- $return['__className'] = $class = get_class($object);
- $classLower = strtolower($class);
- $reflectionClass = new ReflectionClass($class);
- $properties = array();
- foreach ($reflectionClass->getProperties() as $property) {
- $properties[$property->getName()] = $property;
- }
-
- $members = (array)$object;
-
- foreach ($properties as $plainName => $property) {
-
- $name = $rawName = $plainName;
- if ($property->isStatic()) {
- $name = 'static:' . $name;
- }
- if ($property->isPublic()) {
- $name = 'public:' . $name;
- } else if ($property->isPrivate()) {
- $name = 'private:' . $name;
- $rawName = "\0" . $class . "\0" . $rawName;
- } else if ($property->isProtected()) {
- $name = 'protected:' . $name;
- $rawName = "\0" . '*' . "\0" . $rawName;
- }
-
- if (!(isset($this->objectFilters[$classLower])
- && is_array($this->objectFilters[$classLower])
- && in_array($plainName, $this->objectFilters[$classLower]))) {
-
- if (array_key_exists($rawName, $members) && !$property->isStatic()) {
- $return[$name] = $this->encodeObject($members[$rawName], $objectDepth + 1, 1, $maxDepth + 1);
- } else {
- if (method_exists($property, 'setAccessible')) {
- $property->setAccessible(true);
- $return[$name] = $this->encodeObject($property->getValue($object), $objectDepth + 1, 1, $maxDepth + 1);
- } else
- if ($property->isPublic()) {
- $return[$name] = $this->encodeObject($property->getValue($object), $objectDepth + 1, 1, $maxDepth + 1);
- } else {
- $return[$name] = '** Need PHP 5.3 to get value **';
- }
- }
- } else {
- $return[$name] = '** Excluded by Filter **';
- }
- }
-
-
-
- foreach ($members as $rawName => $value) {
-
- $name = $rawName;
- if ($name{0} == "\0") {
- $parts = explode("\0", $name);
- $name = $parts[2];
- }
- $plainName = $name;
-
- if (!isset($properties[$name])) {
- $name = 'undeclared:' . $name;
-
- if (!(isset($this->objectFilters[$classLower])
- && is_array($this->objectFilters[$classLower])
- && in_array($plainName, $this->objectFilters[$classLower]))) {
-
- $return[$name] = $this->encodeObject($value, $objectDepth + 1, 1, $maxDepth + 1);
- } else {
- $return[$name] = '** Excluded by Filter **';
- }
- }
- }
-
- array_pop($this->objectStack);
-
- } elseif (is_array($object)) {
-
- if ($arrayDepth > $this->options['maxArrayDepth']) {
- return '** Max Array Depth (' . $this->options['maxArrayDepth'] . ') **';
- }
-
- foreach ($object as $key => $val) {
-
-
-
-
- if ($key == 'GLOBALS'
- && is_array($val)
- && array_key_exists('GLOBALS', $val)) {
- $val['GLOBALS'] = '** Recursion (GLOBALS) **';
- }
- if (!$this->is_utf8($key)) {
- $key = utf8_encode($key);
- }
- $return[$key] = $this->encodeObject($val, 1, $arrayDepth + 1, $maxDepth + 1);
- }
- } else {
- if ($this->is_utf8($object)) {
- return $object;
- } else {
- return utf8_encode($object);
- }
- }
- return $return;
- }
-
- protected function is_utf8($str)
- {
- if (function_exists('mb_detect_encoding')) {
- return (
- mb_detect_encoding($str, 'UTF-8', true) == 'UTF-8' &&
- ($str === null || $this->jsonEncode($str, true) !== 'null')
- );
- }
- $c = 0;
- $b = 0;
- $bits = 0;
- $len = strlen($str);
- for ($i = 0; $i < $len; $i++) {
- $c = ord($str[$i]);
- if ($c > 128) {
- if (($c >= 254)) return false;
- elseif ($c >= 252) $bits = 6;
- elseif ($c >= 248) $bits = 5;
- elseif ($c >= 240) $bits = 4;
- elseif ($c >= 224) $bits = 3;
- elseif ($c >= 192) $bits = 2;
- else return false;
- if (($i + $bits) > $len) return false;
- while($bits > 1) {
- $i++;
- $b = ord($str[$i]);
- if ($b < 128 || $b > 191) return false;
- $bits--;
- }
- }
- }
- return ($str === null || $this->jsonEncode($str, true) !== 'null');
- }
-
-
-
-
- private $json_objectStack = array();
-
- private function json_utf82utf16($utf8)
- {
-
- if (function_exists('mb_convert_encoding')) {
- return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
- }
- switch (strlen($utf8)) {
- case 1:
-
-
- return $utf8;
- case 2:
-
-
- return chr(0x07 & (ord($utf8{0}) >> 2))
- . chr((0xC0 & (ord($utf8{0}) << 6))
- | (0x3F & ord($utf8{1})));
- case 3:
-
-
- return chr((0xF0 & (ord($utf8{0}) << 4))
- | (0x0F & (ord($utf8{1}) >> 2)))
- . chr((0xC0 & (ord($utf8{1}) << 6))
- | (0x7F & ord($utf8{2})));
- }
-
- return '';
- }
-
- private function json_encode($var)
- {
- if (is_object($var)) {
- if (in_array($var, $this->json_objectStack)) {
- return '"** Recursion **"';
- }
- }
-
- switch (gettype($var)) {
- case 'boolean':
- return $var ? 'true' : 'false';
- case 'NULL':
- return 'null';
- case 'integer':
- return (int) $var;
- case 'double':
- case 'float':
- return (float) $var;
- case 'string':
-
- $ascii = '';
- $strlen_var = strlen($var);
-
- for ($c = 0; $c < $strlen_var; ++$c) {
- $ord_var_c = ord($var{$c});
- switch (true) {
- case $ord_var_c == 0x08:
- $ascii .= '\b';
- break;
- case $ord_var_c == 0x09:
- $ascii .= '\t';
- break;
- case $ord_var_c == 0x0A:
- $ascii .= '\n';
- break;
- case $ord_var_c == 0x0C:
- $ascii .= '\f';
- break;
- case $ord_var_c == 0x0D:
- $ascii .= '\r';
- break;
- case $ord_var_c == 0x22:
- case $ord_var_c == 0x2F:
- case $ord_var_c == 0x5C:
-
- $ascii .= '\\' . $var{$c};
- break;
- case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
-
- $ascii .= $var{$c};
- break;
- case (($ord_var_c & 0xE0) == 0xC0):
-
-
- $char = pack('C*', $ord_var_c, ord($var{$c + 1}));
- $c += 1;
- $utf16 = $this->json_utf82utf16($char);
- $ascii .= sprintf('\u%04s', bin2hex($utf16));
- break;
- case (($ord_var_c & 0xF0) == 0xE0):
-
-
- $char = pack('C*', $ord_var_c,
- ord($var{$c + 1}),
- ord($var{$c + 2}));
- $c += 2;
- $utf16 = $this->json_utf82utf16($char);
- $ascii .= sprintf('\u%04s', bin2hex($utf16));
- break;
- case (($ord_var_c & 0xF8) == 0xF0):
-
-
- $char = pack('C*', $ord_var_c,
- ord($var{$c + 1}),
- ord($var{$c + 2}),
- ord($var{$c + 3}));
- $c += 3;
- $utf16 = $this->json_utf82utf16($char);
- $ascii .= sprintf('\u%04s', bin2hex($utf16));
- break;
- case (($ord_var_c & 0xFC) == 0xF8):
-
-
- $char = pack('C*', $ord_var_c,
- ord($var{$c + 1}),
- ord($var{$c + 2}),
- ord($var{$c + 3}),
- ord($var{$c + 4}));
- $c += 4;
- $utf16 = $this->json_utf82utf16($char);
- $ascii .= sprintf('\u%04s', bin2hex($utf16));
- break;
- case (($ord_var_c & 0xFE) == 0xFC):
-
-
- $char = pack('C*', $ord_var_c,
- ord($var{$c + 1}),
- ord($var{$c + 2}),
- ord($var{$c + 3}),
- ord($var{$c + 4}),
- ord($var{$c + 5}));
- $c += 5;
- $utf16 = $this->json_utf82utf16($char);
- $ascii .= sprintf('\u%04s', bin2hex($utf16));
- break;
- }
- }
- return '"' . $ascii . '"';
- case 'array':
-
-
- if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) {
-
- $this->json_objectStack[] = $var;
- $properties = array_map(array($this, 'json_name_value'),
- array_keys($var),
- array_values($var));
- array_pop($this->json_objectStack);
- foreach ($properties as $property) {
- if ($property instanceof Exception) {
- return $property;
- }
- }
- return '{' . join(',', $properties) . '}';
- }
- $this->json_objectStack[] = $var;
-
- $elements = array_map(array($this, 'json_encode'), $var);
- array_pop($this->json_objectStack);
- foreach ($elements as $element) {
- if ($element instanceof Exception) {
- return $element;
- }
- }
- return '[' . join(',', $elements) . ']';
- case 'object':
- $vars = self::encodeObject($var);
- $this->json_objectStack[] = $var;
- $properties = array_map(array($this, 'json_name_value'),
- array_keys($vars),
- array_values($vars));
- array_pop($this->json_objectStack);
-
- foreach ($properties as $property) {
- if ($property instanceof Exception) {
- return $property;
- }
- }
-
- return '{' . join(',', $properties) . '}';
- default:
- return null;
- }
- }
-
- private function json_name_value($name, $value)
- {
-
-
-
-
- if ($name == 'GLOBALS'
- && is_array($value)
- && array_key_exists('GLOBALS', $value)) {
- $value['GLOBALS'] = '** Recursion **';
- }
-
- $encodedValue = $this->json_encode($value);
- if ($encodedValue instanceof Exception) {
- return $encodedValue;
- }
- return $this->json_encode(strval($name)) . ':' . $encodedValue;
- }
-
- public function setProcessorUrl($URL)
- {
- trigger_error('The FirePHP::setProcessorUrl() method is no longer supported', E_USER_DEPRECATED);
- }
-
- public function setRendererUrl($URL)
- {
- trigger_error('The FirePHP::setRendererUrl() method is no longer supported', E_USER_DEPRECATED);
- }
- }
|