diff --git a/lib/FirePHP/FirePHP.class.php b/lib/FirePHP/FirePHP.class.php index 4744335..1741bb3 100644 --- a/lib/FirePHP/FirePHP.class.php +++ b/lib/FirePHP/FirePHP.class.php @@ -1,44 +1,39 @@ , Copyright 2007, New BSD License +// - qbbr, Sokolov Innokenty , Copyright 2011, New BSD License +// - cadorn, Christoph Dorn , Copyright 2011, MIT License + /** * *** BEGIN LICENSE BLOCK ***** - * - * This file is part of FirePHP (http://www.firephp.org/). - * - * Software License Agreement (New BSD License) - * - * Copyright (c) 2006-2010, Christoph Dorn - * All rights reserved. * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: + * [MIT License](http://www.opensource.org/licenses/mit-license.php) * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. + * Copyright (c) 2007+ [Christoph Dorn](http://www.christophdorn.com/) * - * * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * * Neither the name of Christoph Dorn nor the names of its - * contributors may be used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * 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. * * ***** END LICENSE BLOCK ***** * - * @copyright Copyright (C) 2007-2009 Christoph Dorn + * @copyright Copyright (C) 2007+ Christoph Dorn * @author Christoph Dorn - * @license http://www.opensource.org/licenses/bsd-license.php + * @license [MIT License](http://www.opensource.org/licenses/mit-license.php) * @package FirePHPCore */ @@ -65,9 +60,9 @@ * * For more information see: http://www.firephp.org/ * - * @copyright Copyright (C) 2007-2009 Christoph Dorn + * @copyright Copyright (C) 2007+ Christoph Dorn * @author Christoph Dorn - * @license http://www.opensource.org/licenses/bsd-license.php + * @license [MIT License](http://www.opensource.org/licenses/mit-license.php) * @package FirePHPCore */ class FirePHP { @@ -77,7 +72,7 @@ class FirePHP { * * @var string */ - const VERSION = '0.3'; // @pinf replace '0.3' with '%%package.version%%' + const VERSION = '0.3'; // @pinf replace '0.3' with '%%VERSION%%' /** * Firebug LOG level @@ -197,7 +192,7 @@ class FirePHP { /** * Wildfire protocol message index * - * @var int + * @var integer */ protected $messageIndex = 1; @@ -250,18 +245,18 @@ class FirePHP { */ public function __sleep() { - return array('options','objectFilters','enabled'); + return array('options', 'objectFilters', 'enabled'); } /** * Gets singleton instance of FirePHP * - * @param boolean $AutoCreate + * @param boolean $autoCreate * @return FirePHP */ - public static function getInstance($AutoCreate = false) + public static function getInstance($autoCreate = false) { - if ($AutoCreate===true && !self::$instance) { + if ($autoCreate === true && !self::$instance) { self::init(); } return self::$instance; @@ -296,8 +291,8 @@ public static function setInstance($instance) */ public function setLogToInsightConsole($console) { - if(is_string($console)) { - if(get_class($this)!='FirePHP_Insight' && !is_subclass_of($this, 'FirePHP_Insight')) { + 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); @@ -309,12 +304,12 @@ public function setLogToInsightConsole($console) /** * Enable and disable logging to Firebug * - * @param boolean $Enabled TRUE to enable, FALSE to disable + * @param boolean $enabled TRUE to enable, FALSE to disable * @return void */ - public function setEnabled($Enabled) + public function setEnabled($enabled) { - $this->enabled = $Enabled; + $this->enabled = $enabled; } /** @@ -332,13 +327,13 @@ public function getEnabled() * * Filters are used to exclude object members. * - * @param string $Class The class name of the object - * @param array $Filter An array of members to exclude + * @param string $class The class name of the object + * @param array $filter An array of members to exclude * @return void */ - public function setObjectFilter($Class, $Filter) + public function setObjectFilter($class, $filter) { - $this->objectFilters[strtolower($Class)] = $Filter; + $this->objectFilters[strtolower($class)] = $filter; } /** @@ -351,12 +346,12 @@ public function setObjectFilter($Class, $Filter) * - useNativeJsonEncode: If true will use json_encode() (default: true) * - includeLineNumbers: If true will include line numbers and filenames (default: true) * - * @param array $Options The options to be set + * @param array $options The options to be set * @return void */ - public function setOptions($Options) + public function setOptions($options) { - $this->options = array_merge($this->options,$Options); + $this->options = array_merge($this->options, $options); } /** @@ -372,32 +367,32 @@ public function getOptions() /** * Set an option for the library * - * @param string $Name - * @param mixed $Value - * @throws Exception + * @param string $name + * @param mixed $value * @return void + * @throws Exception */ - public function setOption($Name, $Value) + public function setOption($name, $value) { - if (!isset($this->options[$Name])) { - throw $this->newException('Unknown option: ' . $Name); + if (!isset($this->options[$name])) { + throw $this->newException('Unknown option: ' . $name); } - $this->options[$Name] = $Value; + $this->options[$name] = $value; } /** * Get an option from the library * - * @param string $Name - * @throws Exception + * @param string $name * @return mixed + * @throws Exception */ - public function getOption($Name) + public function getOption($name) { - if (!isset($this->options[$Name])) { - throw $this->newException('Unknown option: ' . $Name); + if (!isset($this->options[$name])) { + throw $this->newException('Unknown option: ' . $name); } - return $this->options[$Name]; + return $this->options[$name]; } /** @@ -416,7 +411,7 @@ public function registerErrorHandler($throwErrorExceptions = false) $this->throwErrorExceptions = $throwErrorExceptions; - return set_error_handler(array($this,'errorHandler')); + return set_error_handler(array($this, 'errorHandler')); } /** @@ -424,10 +419,10 @@ public function registerErrorHandler($throwErrorExceptions = false) * * Throws exception for each php error that will occur. * - * @param int $errno + * @param integer $errno * @param string $errstr * @param string $errfile - * @param int $errline + * @param integer $errline * @param array $errcontext */ public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) @@ -457,7 +452,7 @@ public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) */ public function registerExceptionHandler() { - return set_exception_handler(array($this,'exceptionHandler')); + return set_exception_handler(array($this, 'exceptionHandler')); } /** @@ -465,21 +460,21 @@ public function registerExceptionHandler() * * Logs all exceptions to your firebug console and then stops the script. * - * @param Exception $Exception + * @param Exception $exception * @throws Exception */ - function exceptionHandler($Exception) + function exceptionHandler($exception) { - $this->inExceptionHandler = true; header('HTTP/1.1 500 Internal Server Error'); try { - $this->fb($Exception); + $this->fb($exception); } catch (Exception $e) { echo 'We had an exception: ' . $e; } + $this->inExceptionHandler = false; } @@ -508,14 +503,14 @@ public function registerAssertionHandler($convertAssertionErrorsToExceptions = t * Logs all assertions to your firebug console and then stops the script. * * @param string $file File source of assertion - * @param int $line Line source of assertion - * @param mixed $code Assertion code + * @param integer $line Line source of assertion + * @param mixed $code Assertion code */ public function assertionHandler($file, $line, $code) { if ($this->convertAssertionErrorsToExceptions) { - $exception = new ErrorException('Assertion Failed - Code[ '.$code.' ]', 0, null, $file, $line); + $exception = new ErrorException('Assertion Failed - Code[ ' . $code . ' ]', 0, null, $file, $line); if ($this->throwAssertionExceptions) { throw $exception; @@ -524,7 +519,7 @@ public function assertionHandler($file, $line, $code) } } else { - $this->fb($code, 'Assertion Failed', FirePHP::ERROR, array('File'=>$file,'Line'=>$line)); + $this->fb($code, 'Assertion Failed', FirePHP::ERROR, array('File' => $file, 'Line' => $line)); } } @@ -535,28 +530,28 @@ public function assertionHandler($file, $line, $code) * Collapsed: [true|false] * Color: [#RRGGBB|ColorName] * - * @param string $Name - * @param array $Options OPTIONAL Instructions on how to log the group + * @param string $name + * @param array $options OPTIONAL Instructions on how to log the group * @return true * @throws Exception */ - public function group($Name, $Options = null) + public function group($name, $options = null) { - if (!$Name) { + if ( !isset($name) ) { throw $this->newException('You must specify a label for the group!'); } - - if ($Options) { - if (!is_array($Options)) { + + 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'; + if (array_key_exists('Collapsed', $options)) { + $options['Collapsed'] = ($options['Collapsed']) ? 'true' : 'false'; } } - - return $this->fb(null, $Name, FirePHP::GROUP_START, $Options); + + return $this->fb(null, $name, FirePHP::GROUP_START, $options); } /** @@ -574,106 +569,106 @@ public function groupEnd() * Log object with label to firebug console * * @see FirePHP::LOG - * @param mixes $Object - * @param string $Label + * @param mixes $object + * @param string $label * @return true * @throws Exception */ - public function log($Object, $Label = null, $Options = array()) + public function log($object, $label = null, $options = array()) { - return $this->fb($Object, $Label, FirePHP::LOG, $Options); + return $this->fb($object, $label, FirePHP::LOG, $options); } /** * Log object with label to firebug console * * @see FirePHP::INFO - * @param mixes $Object - * @param string $Label + * @param mixes $object + * @param string $label * @return true * @throws Exception */ - public function info($Object, $Label = null, $Options = array()) + public function info($object, $label = null, $options = array()) { - return $this->fb($Object, $Label, FirePHP::INFO, $Options); + return $this->fb($object, $label, FirePHP::INFO, $options); } /** * Log object with label to firebug console * * @see FirePHP::WARN - * @param mixes $Object - * @param string $Label + * @param mixes $object + * @param string $label * @return true * @throws Exception */ - public function warn($Object, $Label = null, $Options = array()) + public function warn($object, $label = null, $options = array()) { - return $this->fb($Object, $Label, FirePHP::WARN, $Options); + return $this->fb($object, $label, FirePHP::WARN, $options); } /** * Log object with label to firebug console * * @see FirePHP::ERROR - * @param mixes $Object - * @param string $Label + * @param mixes $object + * @param string $label * @return true * @throws Exception */ - public function error($Object, $Label = null, $Options = array()) + public function error($object, $label = null, $options = array()) { - return $this->fb($Object, $Label, FirePHP::ERROR, $Options); + return $this->fb($object, $label, FirePHP::ERROR, $options); } /** * Dumps key and variable to firebug server panel * * @see FirePHP::DUMP - * @param string $Key - * @param mixed $Variable + * @param string $key + * @param mixed $variable * @return true * @throws Exception */ - public function dump($Key, $Variable, $Options = array()) + public function dump($key, $variable, $options = array()) { - if (!is_string($Key)) { + if (!is_string($key)) { throw $this->newException('Key passed to dump() is not a string'); } - if (strlen($Key)>100) { + 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)) { + 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); + return $this->fb($variable, $key, FirePHP::DUMP, $options); } /** * Log a trace in the firebug console * * @see FirePHP::TRACE - * @param string $Label + * @param string $label * @return true * @throws Exception */ - public function trace($Label) + public function trace($label) { - return $this->fb($Label, FirePHP::TRACE); + return $this->fb($label, FirePHP::TRACE); } /** * Log a table in the firebug console * * @see FirePHP::TABLE - * @param string $Label - * @param string $Table + * @param string $label + * @param string $table * @return true * @throws Exception */ - public function table($Label, $Table, $Options = array()) + public function table($label, $table, $options = array()) { - return $this->fb($Table, $Label, FirePHP::TABLE, $Options); + return $this->fb($table, $label, FirePHP::TABLE, $options); } /** @@ -684,8 +679,8 @@ public function table($Label, $Table, $Options = array()) public static function to() { $instance = self::getInstance(); - if (!method_exists($instance, "_to")) { - throw new Exception("FirePHP::to() implementation not loaded"); + 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); @@ -699,8 +694,8 @@ public static function to() public static function plugin() { $instance = self::getInstance(); - if (!method_exists($instance, "_plugin")) { - throw new Exception("FirePHP::plugin() implementation not loaded"); + 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); @@ -714,13 +709,13 @@ public static function plugin() public function detectClientExtension() { // Check if FirePHP is installed on client via User-Agent header - if (@preg_match_all('/\sFirePHP\/([\.\d]*)\s?/si',$this->getUserAgent(),$m) && - version_compare($m[1][0],'0.0.6','>=')) { + if (@preg_match_all('/\sFirePHP\/([\.\d]*)\s?/si', $this->getUserAgent(), $m) && + version_compare($m[1][0], '0.0.6', '>=')) { return true; } else // Check if FirePHP is installed on client via X-FirePHP-Version header - if (@preg_match_all('/^([\.\d]*)$/si',$this->getRequestHeader("X-FirePHP-Version"),$m) && - version_compare($m[1][0],'0.0.6','>=')) { + if (@preg_match_all('/^([\.\d]*)$/si', $this->getRequestHeader('X-FirePHP-Version'), $m) && + version_compare($m[1][0], '0.0.6', '>=')) { return true; } return false; @@ -730,14 +725,14 @@ public function detectClientExtension() * Log varible to Firebug * * @see http://www.firephp.org/Wiki/Reference/Fb - * @param mixed $Object The variable to be logged - * @return true Return TRUE if message was added to headers, FALSE otherwise + * @param mixed $object The variable to be logged + * @return boolean Return TRUE if message was added to headers, FALSE otherwise * @throws Exception */ - public function fb($Object) + public function fb($object) { - if($this instanceof FirePHP_Insight && method_exists($this, '_logUpgradeClientMessage')) { - if(!FirePHP_Insight::$upgradeClientMessageLogged) { // avoid infinite recursion as _logUpgradeClientMessage() logs a message + if ($this instanceof FirePHP_Insight && method_exists($this, '_logUpgradeClientMessage')) { + if (!FirePHP_Insight::$upgradeClientMessageLogged) { // avoid infinite recursion as _logUpgradeClientMessage() logs a message $this->_logUpgradeClientMessage(); } } @@ -752,20 +747,19 @@ public function fb($Object) // If we are logging from within the exception handler we cannot throw another exception if ($this->inExceptionHandler) { // Simply echo the error out to the page - echo '
FirePHP ERROR: 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.
'; + echo '
FirePHP ERROR: 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.
'; } 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.'); + 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(); + $type = null; + $label = null; + $options = array(); - if (func_num_args()==1) { - } else - if (func_num_args()==2) { - switch(func_get_arg(1)) { + 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: @@ -776,178 +770,232 @@ public function fb($Object) case self::TABLE: case self::GROUP_START: case self::GROUP_END: - $Type = func_get_arg(1); + $type = func_get_arg(1); break; default: - $Label = func_get_arg(1); + $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 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'))) { - $msg = $this->logToInsightConsole; - if ($Object instanceof Exception) { - $Type = self::EXCEPTION; + // Get folder name where firephp is located. + $parentFolder = basename(dirname(__FILE__)); + $parentFolderLength = strlen( $parentFolder ); + $fbLength = 7 + $parentFolderLength; + $fireClassLength = 18 + $parentFolderLength; + + 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, -1*$fbLength, $fbLength) == $parentFolder.'/fb.php' || substr($path, -1*$fireClassLength, $fireClassLength) == $parentFolder.'/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']), -1*$fbLength, $fbLength) == $parentFolder.'/fb.php') { + continue; + } + if (isset($trace[$i]['class']) && $trace[$i]['class'] == 'FB' && + isset($trace[$i - 1]['file']) && substr($this->_standardizePath($trace[$i - 1]['file']), -1*$fbLength, $fbLength) == $parentFolder.'/fb.php') { + continue; + } + break; + } + // adjust trace offset + $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); + if ($label && $type != self::TABLE && $type != self::GROUP_START) { + $msg = $msg->label($label); } - switch($Type) { + switch ($type) { case self::DUMP: case self::LOG: - return $msg->log($Object); + return $msg->log($object); case self::INFO: - return $msg->info($Object); + return $msg->info($object); case self::WARN: - return $msg->warn($Object); + return $msg->warn($object); case self::ERROR: - return $msg->error($Object); + return $msg->error($object); case self::TRACE: - return $msg->trace($Object); + return $msg->trace($object); case self::EXCEPTION: - return $this->plugin('engine')->handleException($Object, $msg); + return $this->plugin('error')->handleException($object, $msg); case self::TABLE: - if (isset($Object[0]) && !is_string($Object[0]) && $Label) { - $Object = array($Label, $Object); + 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]); + 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); + $insightGroupStack[] = $msg->group(md5($label))->open(); + return $msg->log($label); case self::GROUP_END: - if(count($insightGroupStack)==0) { - throw new Error('Too many groupEnd() as opposed to group() calls!'); - } - $group = array_pop($insightGroupStack); + if (count($insightGroupStack) == 0) { + throw new Error('Too many groupEnd() as opposed to group() calls!'); + } + $group = array_pop($insightGroupStack); return $group->close(); - default: - return $msg->log($Object); + default: + return $msg->log($object); } } if (!$this->detectClientExtension()) { return false; } - + $meta = array(); $skipFinalObjectEncode = false; - if ($Object instanceof Exception) { + if ($object instanceof Exception) { - $meta['file'] = $this->_escapeTraceFile($Object->getFile()); - $meta['line'] = $Object->getLine(); + $meta['file'] = $this->_escapeTraceFile($object->getFile()); + $meta['line'] = $object->getLine(); - $trace = $Object->getTrace(); - if ($Object instanceof ErrorException + $trace = $object->getTrace(); + if ($object instanceof ErrorException && isset($trace[0]['function']) - && $trace[0]['function']=='errorHandler' + && $trace[0]['function'] == 'errorHandler' && isset($trace[0]['class']) - && $trace[0]['class']=='FirePHP') { + && $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; + 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))); + $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)); + $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; + $type = self::EXCEPTION; - } else - if ($Type==self::TRACE) { + } else if ($type == self::TRACE) { $trace = debug_backtrace(); if (!$trace) return false; - for( $i=0 ; $i_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php' - || substr($this->_standardizePath($trace[$i]['file']),-29,29)=='FirePHPCore/FirePHP.class.php')) { + && ($trace[$i]['class'] == 'FirePHP' + || $trace[$i]['class'] == 'FB') + && (substr($this->_standardizePath($trace[$i]['file']), -1*$fbLength, $fbLength) == $parentFolder.'/fb.php' + || substr($this->_standardizePath($trace[$i]['file']), -1*$fireClassLength, $fireClassLength) == $parentFolder.'/FirePHP.class.php')) { /* Skip - FB::trace(), FB::send(), $firephp->trace(), $firephp->fb() */ } 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') { + && $trace[$i]['class'] == 'FirePHP' + && substr($this->_standardizePath($trace[$i + 1]['file']), -1*$fbLength, $fbLength) == $parentFolder.'/fb.php') { /* Skip fb() */ } 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))); + 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']:''; + $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 ($type == self::TABLE) { - if (isset($Object[0]) && is_string($Object[0])) { - $Object[1] = $this->encodeTable($Object[1]); + if (isset($object[0]) && is_string($object[0])) { + $object[1] = $this->encodeTable($object[1]); } else { - $Object = $this->encodeTable($Object); + $object = $this->encodeTable($object); } $skipFinalObjectEncode = true; - } else - if ($Type==self::GROUP_START) { + } else if ($type == self::GROUP_START) { - if (!$Label) { + if (!$label) { throw $this->newException('You must specify a label for the group!'); } } else { - if ($Type===null) { - $Type = self::LOG; + if ($type === null) { + $type = self::LOG; } } @@ -955,28 +1003,28 @@ public function fb($Object) if (!isset($meta['file']) || !isset($meta['line'])) { $trace = debug_backtrace(); - for( $i=0 ; $trace && $i_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php' - || substr($this->_standardizePath($trace[$i]['file']),-29,29)=='FirePHPCore/FirePHP.class.php')) { + && ($trace[$i]['class'] == 'FirePHP' + || $trace[$i]['class'] == 'FB') + && (substr($this->_standardizePath($trace[$i]['file']), -1*$fbLength, $fbLength) == $parentFolder.'/fb.php' + || substr($this->_standardizePath($trace[$i]['file']), -1*$fireClassLength, $fireClassLength) == $parentFolder.'/FirePHP.class.php')) { /* Skip - FB::trace(), FB::send(), $firephp->trace(), $firephp->fb() */ } 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') { + && isset($trace[$i + 1]['file']) + && $trace[$i]['class'] == 'FirePHP' + && substr($this->_standardizePath($trace[$i + 1]['file']), -1*$fbLength, $fbLength) == $parentFolder.'/fb.php') { /* Skip fb() */ } else if (isset($trace[$i]['file']) - && substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php') { + && substr($this->_standardizePath($trace[$i]['file']), -1*$fbLength, $fbLength) == $parentFolder.'/fb.php') { /* Skip FB::fb() */ } else { - $meta['file'] = isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):''; - $meta['line'] = isset($trace[$i]['line'])?$trace[$i]['line']:''; + $meta['file'] = isset($trace[$i]['file']) ? $this->_escapeTraceFile($trace[$i]['file']) : ''; + $meta['line'] = isset($trace[$i]['line']) ? $trace[$i]['line'] : ''; break; } } @@ -986,49 +1034,49 @@ public function fb($Object) 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); + $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); - $structure_index = 1; - if ($Type==self::DUMP) { - $structure_index = 2; - $this->setHeader('X-Wf-1-Structure-2','http://meta.firephp.org/Wildfire/Structure/FirePHP/Dump/0.1'); + $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'); + $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).'}'; + if ($type == self::DUMP) { + $msg = '{"' . $label . '":' . $this->jsonEncode($object, $skipFinalObjectEncode) . '}'; } else { - $msg_meta = $Options; - $msg_meta['Type'] = $Type; - if ($Label!==null) { - $msg_meta['Label'] = $Label; + $msgMeta = $options; + $msgMeta['Type'] = $type; + if ($label !== null) { + $msgMeta['Label'] = $label; } - if (isset($meta['file']) && !isset($msg_meta['File'])) { - $msg_meta['File'] = $meta['file']; + if (isset($meta['file']) && !isset($msgMeta['File'])) { + $msgMeta['File'] = $meta['file']; } - if (isset($meta['line']) && !isset($msg_meta['Line'])) { - $msg_meta['Line'] = $meta['line']; + if (isset($meta['line']) && !isset($msgMeta['Line'])) { + $msgMeta['Line'] = $meta['line']; } - $msg = '['.$this->jsonEncode($msg_meta).','.$this->jsonEncode($Object, $skipFinalObjectEncode).']'; + $msg = '[' . $this->jsonEncode($msgMeta) . ',' . $this->jsonEncode($object, $skipFinalObjectEncode) . ']'; } - $parts = explode("\n",chunk_split($msg, 5000, "\n")); - - for( $i=0 ; $i2) { + if (count($parts) > 2) { // Message needs to be split into multiple parts - $this->setHeader('X-Wf-1-'.$structure_index.'-'.'1-'.$this->messageIndex, - (($i==0)?strlen($msg):'') + $this->setHeader('X-Wf-1-' . $structureIndex . '-' . '1-' . $this->messageIndex, + (($i == 0) ? strlen($msg) : '') . '|' . $part . '|' - . (($isetHeader('X-Wf-1-'.$structure_index.'-'.'1-'.$this->messageIndex, + $this->setHeader('X-Wf-1-' . $structureIndex . '-' . '1-' . $this->messageIndex, strlen($part) . '|' . $part . '|'); } @@ -1040,7 +1088,7 @@ public function fb($Object) } } - $this->setHeader('X-Wf-1-Index',$this->messageIndex-1); + $this->setHeader('X-Wf-1-Index', $this->messageIndex - 1); return true; } @@ -1048,73 +1096,73 @@ public function fb($Object) /** * Standardizes path for windows systems. * - * @param string $Path + * @param string $path * @return string */ - protected function _standardizePath($Path) + protected function _standardizePath($path) { - return preg_replace('/\\\\+/','/',$Path); + return preg_replace('/\\\\+/', '/', $path); } /** * Escape trace path for windows systems * - * @param array $Trace + * @param array $trace * @return array */ - protected function _escapeTrace($Trace) + protected function _escapeTrace($trace) { - if (!$Trace) return $Trace; - for( $i=0 ; $i_escapeTraceFile($Trace[$i]['file']); + 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']); + if (isset($trace[$i]['args'])) { + $trace[$i]['args'] = $this->encodeObject($trace[$i]['args']); } } - return $Trace; + return $trace; } /** * Escape file information of trace for windows systems * - * @param string $File + * @param string $file * @return string */ - protected function _escapeTraceFile($File) + protected function _escapeTraceFile($file) { /* Check if we have a windows filepath */ - if (strpos($File,'\\')) { + if (strpos($file, '\\')) { /* First strip down to single \ */ - - $file = preg_replace('/\\\\+/','\\',$File); - + + $file = preg_replace('/\\\\+/', '\\', $file); + return $file; } - return $File; + return $file; } /** * Check if headers have already been sent * - * @param string $Filename - * @param integer $Linenum + * @param string $filename + * @param integer $linenum */ - protected function headersSent(&$Filename, &$Linenum) + protected function headersSent(&$filename, &$linenum) { - return headers_sent($Filename, $Linenum); + return headers_sent($filename, $linenum); } /** * Send header * - * @param string $Name - * @param string $Value + * @param string $name + * @param string $value */ - protected function setHeader($Name, $Value) + protected function setHeader($name, $value) { - return header($Name.': '.$Value); + return header($name . ': ' . $value); } /** @@ -1133,24 +1181,25 @@ protected function getUserAgent() * * @return array */ - public static function getAllRequestHeaders() { - static $_cached_headers = false; - if($_cached_headers!==false) { - return $_cached_headers; + public static function getAllRequestHeaders() + { + static $_cachedHeaders = false; + if ($_cachedHeaders !== false) { + return $_cachedHeaders; } $headers = array(); - if(function_exists('getallheaders')) { - foreach( getallheaders() as $name => $value ) { + 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_') { + foreach ($_SERVER as $name => $value) { + if (substr($name, 0, 5) == 'HTTP_') { $headers[strtolower(str_replace(' ', '-', str_replace('_', ' ', substr($name, 5))))] = $value; } } } - return $_cached_headers = $headers; + return $_cachedHeaders = $headers; } /** @@ -1158,11 +1207,11 @@ public static function getAllRequestHeaders() { * * @return string|false */ - protected function getRequestHeader($Name) + protected function getRequestHeader($name) { $headers = self::getAllRequestHeaders(); - if (isset($headers[strtolower($Name)])) { - return $headers[strtolower($Name)]; + if (isset($headers[strtolower($name)])) { + return $headers[strtolower($name)]; } return false; } @@ -1170,12 +1219,12 @@ protected function getRequestHeader($Name) /** * Returns a new exception * - * @param string $Message + * @param string $message * @return Exception */ - protected function newException($Message) + protected function newException($message) { - return new Exception($Message); + return new Exception($message); } /** @@ -1183,131 +1232,126 @@ protected function newException($Message) * * Uses PHP's jeson_encode() if available * - * @param object $Object The object to be encoded + * @param object $object The object to be encoded + * @param boolean $skipObjectEncode * @return string The JSON string */ - public function jsonEncode($Object, $skipObjectEncode = false) + public function jsonEncode($object, $skipObjectEncode = false) { if (!$skipObjectEncode) { - $Object = $this->encodeObject($Object); + $object = $this->encodeObject($object); } if (function_exists('json_encode') - && $this->options['useNativeJsonEncode']!=false) { + && $this->options['useNativeJsonEncode'] != false) { - return json_encode($Object); + return json_encode($object); } else { - return $this->json_encode($Object); + return $this->json_encode($object); } } /** * Encodes a table by encoding each row and column with encodeObject() * - * @param array $Table The table to be encoded + * @param array $table The table to be encoded * @return array */ - protected function encodeTable($Table) + protected function encodeTable($table) { - - if (!$Table) return $Table; + if (!$table) return $table; - $new_table = array(); - foreach($Table as $row) { - + $newTable = array(); + foreach ($table as $row) { + if (is_array($row)) { - $new_row = array(); - - foreach($row as $item) { - $new_row[] = $this->encodeObject($item); + $newRow = array(); + + foreach ($row as $item) { + $newRow[] = $this->encodeObject($item); } - - $new_table[] = $new_row; + + $newTable[] = $newRow; } } - - return $new_table; + + return $newTable; } /** * Encodes an object including members with * protected and private visibility * - * @param Object $Object The object to be encoded - * @param int $Depth The current traversal depth + * @param object $object The object to be encoded + * @param integer $Depth The current traversal depth * @return array All members of the object */ - protected function encodeObject($Object, $ObjectDepth = 1, $ArrayDepth = 1, $MaxDepth = 1) + protected function encodeObject($object, $objectDepth = 1, $arrayDepth = 1, $maxDepth = 1) { - if ($MaxDepth > $this->options['maxDepth']) { - return '** Max Depth ('.$this->options['maxDepth'].') **'; + if ($maxDepth > $this->options['maxDepth']) { + return '** Max Depth (' . $this->options['maxDepth'] . ') **'; } $return = array(); - if (is_resource($Object)) { + //#2801 is_resource reports false for closed resources https://bugs.php.net/bug.php?id=28016 + if (is_resource($object) || gettype($object) === "unknown type") { - return '** '.(string)$Object.' **'; + return '** ' . (string) $object . ' **'; - } else - if (is_object($Object)) { + } else if (is_object($object)) { - if ($ObjectDepth > $this->options['maxObjectDepth']) { - return '** Max Object Depth ('.$this->options['maxObjectDepth'].') **'; + 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).') **'; + if ($refVal === $object) { + return '** Recursion (' . get_class($object) . ') **'; } } - array_push($this->objectStack, $Object); + array_push($this->objectStack, $object); - $return['__className'] = $class = get_class($Object); - $class_lower = strtolower($class); - - $reflectionClass = new ReflectionClass($class); + $return['__className'] = $class = get_class($object); + $classLower = strtolower($class); + + $reflectionClass = new ReflectionClass($class); $properties = array(); - foreach( $reflectionClass->getProperties() as $property) { + foreach ($reflectionClass->getProperties() as $property) { $properties[$property->getName()] = $property; } - $members = (array)$Object; + $members = (array)$object; - foreach( $properties as $plain_name => $property ) { + foreach ($properties as $plainName => $property) { - $name = $raw_name = $plain_name; + $name = $rawName = $plainName; if ($property->isStatic()) { - $name = 'static:'.$name; + $name = 'static:' . $name; } if ($property->isPublic()) { - $name = 'public:'.$name; - } else - if ($property->isPrivate()) { - $name = 'private:'.$name; - $raw_name = "\0".$class."\0".$raw_name; - } else - if ($property->isProtected()) { - $name = 'protected:'.$name; - $raw_name = "\0".'*'."\0".$raw_name; + $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[$class_lower]) - && is_array($this->objectFilters[$class_lower]) - && in_array($plain_name,$this->objectFilters[$class_lower]))) { + if (!(isset($this->objectFilters[$classLower]) + && is_array($this->objectFilters[$classLower]) + && in_array($plainName, $this->objectFilters[$classLower]))) { - if (array_key_exists($raw_name,$members) - && !$property->isStatic()) { - - $return[$name] = $this->encodeObject($members[$raw_name], $ObjectDepth + 1, 1, $MaxDepth + 1); - + 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')) { + if (method_exists($property, 'setAccessible')) { $property->setAccessible(true); - $return[$name] = $this->encodeObject($property->getValue($Object), $ObjectDepth + 1, 1, $MaxDepth + 1); + $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); + $return[$name] = $this->encodeObject($property->getValue($object), $objectDepth + 1, 1, $maxDepth + 1); } else { $return[$name] = '** Need PHP 5.3 to get value **'; } @@ -1319,25 +1363,25 @@ protected function encodeObject($Object, $ObjectDepth = 1, $ArrayDepth = 1, $Max // Include all members that are not defined in the class // but exist in the object - foreach( $members as $raw_name => $value ) { + foreach ($members as $rawName => $value) { - $name = $raw_name; - + $name = $rawName; + if ($name{0} == "\0") { $parts = explode("\0", $name); $name = $parts[2]; } - - $plain_name = $name; + + $plainName = $name; if (!isset($properties[$name])) { - $name = 'undeclared:'.$name; + $name = 'undeclared:' . $name; - if (!(isset($this->objectFilters[$class_lower]) - && is_array($this->objectFilters[$class_lower]) - && in_array($plain_name,$this->objectFilters[$class_lower]))) { + 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); + $return[$name] = $this->encodeObject($value, $objectDepth + 1, 1, $maxDepth + 1); } else { $return[$name] = '** Excluded by Filter **'; } @@ -1346,31 +1390,41 @@ protected function encodeObject($Object, $ObjectDepth = 1, $ArrayDepth = 1, $Max array_pop($this->objectStack); - } elseif (is_array($Object)) { + } elseif (is_array($object)) { - if ($ArrayDepth > $this->options['maxArrayDepth']) { - return '** Max Array Depth ('.$this->options['maxArrayDepth'].') **'; + if ($arrayDepth > $this->options['maxArrayDepth']) { + return '** Max Array Depth (' . $this->options['maxArrayDepth'] . ') **'; } - foreach ($Object as $key => $val) { - + foreach ($object as $key => $val) { + // Encoding the $GLOBALS PHP array causes an infinite loop // if the recursion is not reset here as it contains // a reference to itself. This is the only way I have come up // with to stop infinite recursion in this case. - if ($key=='GLOBALS' + if ($key == 'GLOBALS' && is_array($val) - && array_key_exists('GLOBALS',$val)) { + && array_key_exists('GLOBALS', $val)) { $val['GLOBALS'] = '** Recursion (GLOBALS) **'; } - - $return[$key] = $this->encodeObject($val, 1, $ArrayDepth + 1, $MaxDepth + 1); + + if (!$this->is_utf8($key)) { + $key = utf8_encode($key); + } + + $return[$key] = $this->encodeObject($val, 1, $arrayDepth + 1, $maxDepth + 1); } - } else { - if (self::is_utf8($Object)) { - return $Object; + } elseif ( is_bool($object) ) { + return $object; + } elseif ( is_null($object) ) { + return $object; + } elseif ( is_numeric($object) ) { + return $object; + } else { + if ($this->is_utf8($object)) { + return $object; } else { - return utf8_encode($Object); + return utf8_encode($object); } } return $return; @@ -1382,34 +1436,38 @@ protected function encodeObject($Object, $ObjectDepth = 1, $ArrayDepth = 1, $Max * @param mixed $str String to be tested * @return boolean */ - protected static function is_utf8($str) + protected function is_utf8($str) { - if(function_exists('mb_detect_encoding')) { - return (mb_detect_encoding($str) == 'UTF-8'); + 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){ + $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; + 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){ + if (($i + $bits) > $len) return false; + while($bits > 1) { $i++; - $b=ord($str[$i]); + $b = ord($str[$i]); if ($b < 128 || $b > 191) return false; $bits--; } } } - return true; + return ($str === null || $this->jsonEncode($str, true) !== 'null'); } /** @@ -1463,7 +1521,7 @@ protected static function is_utf8($str) * @author Brett Stimmerman * @author Christoph Dorn * @copyright 2005 Michal Migurski - * @version CVS: $Id: FirePHP.class.php 51 2011-03-31 06:59:05Z michael $ + * @version CVS: $Id: JSON.php,v 1.31 2006/06/28 05:54:17 migurski Exp $ * @license http://www.opensource.org/licenses/bsd-license.php * @link http://pear.php.net/pepr/pepr-proposal-show.php?id=198 */ @@ -1493,7 +1551,7 @@ private function json_utf82utf16($utf8) return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8'); } - switch(strlen($utf8)) { + switch (strlen($utf8)) { case 1: // this case should never be reached, because we are in ASCII range // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 @@ -1532,9 +1590,8 @@ private function json_utf82utf16($utf8) */ private function json_encode($var) { - if (is_object($var)) { - if (in_array($var,$this->json_objectStack)) { + if (in_array($var, $this->json_objectStack)) { return '"** Recursion **"'; } } @@ -1587,7 +1644,7 @@ private function json_encode($var) case $ord_var_c == 0x2F: case $ord_var_c == 0x5C: // double quote, slash, slosh - $ascii .= '\\'.$var{$c}; + $ascii .= '\\' . $var{$c}; break; case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)): @@ -1656,7 +1713,7 @@ private function json_encode($var) } } - return '"'.$ascii.'"'; + return '"' . $ascii . '"'; case 'array': /* @@ -1688,7 +1745,7 @@ private function json_encode($var) array_pop($this->json_objectStack); - foreach($properties as $property) { + foreach ($properties as $property) { if ($property instanceof Exception) { return $property; } @@ -1704,7 +1761,7 @@ private function json_encode($var) array_pop($this->json_objectStack); - foreach($elements as $element) { + foreach ($elements as $element) { if ($element instanceof Exception) { return $element; } @@ -1723,7 +1780,7 @@ private function json_encode($var) array_pop($this->json_objectStack); - foreach($properties as $property) { + foreach ($properties as $property) { if ($property instanceof Exception) { return $property; } @@ -1751,27 +1808,27 @@ private function json_name_value($name, $value) // if the recursion is not reset here as it contains // a reference to itself. This is the only way I have come up // with to stop infinite recursion in this case. - if ($name=='GLOBALS' + if ($name == 'GLOBALS' && is_array($value) - && array_key_exists('GLOBALS',$value)) { + && array_key_exists('GLOBALS', $value)) { $value['GLOBALS'] = '** Recursion **'; } - $encoded_value = $this->json_encode($value); + $encodedValue = $this->json_encode($value); - if ($encoded_value instanceof Exception) { - return $encoded_value; + if ($encodedValue instanceof Exception) { + return $encodedValue; } - return $this->json_encode(strval($name)) . ':' . $encoded_value; + return $this->json_encode(strval($name)) . ':' . $encodedValue; } /** * @deprecated - */ + */ public function setProcessorUrl($URL) { - trigger_error("The FirePHP::setProcessorUrl() method is no longer supported", E_USER_DEPRECATED); + trigger_error('The FirePHP::setProcessorUrl() method is no longer supported', E_USER_DEPRECATED); } /** @@ -1779,6 +1836,6 @@ public function setProcessorUrl($URL) */ public function setRendererUrl($URL) { - trigger_error("The FirePHP::setRendererUrl() method is no longer supported", E_USER_DEPRECATED); - } + trigger_error('The FirePHP::setRendererUrl() method is no longer supported', E_USER_DEPRECATED); + } }