FirePHP.class.php 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. <?php
  2. namespace Common\Ext;
  3. class FirePHP
  4. {
  5. const VERSION = '1.0b1rc1';
  6. const LOG = 'LOG';
  7. const INFO = 'INFO';
  8. const WARN = 'WARN';
  9. const ERROR = 'ERROR';
  10. const DUMP = 'DUMP';
  11. const TRACE = 'TRACE';
  12. const EXCEPTION = 'EXCEPTION';
  13. const TABLE = 'TABLE';
  14. const GROUP_START = 'GROUP_START';
  15. const GROUP_END = 'GROUP_END';
  16. /**
  17. * Singleton instance of FirePHP
  18. * @var FirePHP
  19. */
  20. static protected $instance;
  21. /**
  22. * Flag whether we are logging from within the exception handler
  23. * @var boolean
  24. */
  25. protected $inExceptionHandler = false;
  26. /**
  27. * Flag whether to throw PHP errors that have been converted to ErrorExceptions
  28. * @var boolean
  29. */
  30. protected $throwErrorExceptions = true;
  31. /**
  32. * Flag whether to convert PHP assertion errors to Exceptions
  33. * @var boolean
  34. */
  35. protected $convertAssertionErrorsToExceptions = true;
  36. /**
  37. * Flag whether to throw PHP assertion errors that have been converted to Exceptions
  38. * @var boolean
  39. */
  40. protected $throwAssertionExceptions = false;
  41. /**
  42. * Wildfire protocol message index
  43. * @var integer
  44. */
  45. protected $messageIndex = 1;
  46. /**
  47. * Options for the library
  48. * @var array
  49. */
  50. protected $options = array('maxDepth' => 10, 'maxObjectDepth' => 5, 'maxArrayDepth' => 5, 'useNativeJsonEncode' => true, 'includeLineNumbers' => true);
  51. /**
  52. * Filters used to exclude object members when encoding
  53. * @var array
  54. */
  55. protected $objectFilters = array(
  56. 'firephp' => array('objectStack', 'instance', 'json_objectStack'),
  57. 'firephp_test_class' => array('objectStack', 'instance', 'json_objectStack')
  58. );
  59. /**
  60. * A stack of objects used to detect recursion during object encoding
  61. * @var object
  62. */
  63. protected $objectStack = array();
  64. /**
  65. * Flag to enable/disable logging
  66. * @var boolean
  67. */
  68. protected $enabled = true;
  69. /**
  70. * The insight console to log to if applicable
  71. * @var object
  72. */
  73. protected $logToInsightConsole;
  74. /**
  75. * Keep a list of objects as we descend into the array so we can detect recursion.
  76. */
  77. private $json_objectStack = array();
  78. public function __sleep()
  79. {
  80. return array('options', 'objectFilters', 'enabled');
  81. }
  82. static public function getInstance($autoCreate = false)
  83. {
  84. if (($autoCreate === true) && !self::$instance) {
  85. self::init();
  86. }
  87. return self::$instance;
  88. }
  89. static public function init()
  90. {
  91. return self::setInstance(new self());
  92. }
  93. static public function setInstance($instance)
  94. {
  95. return self::$instance = $instance;
  96. }
  97. public function setLogToInsightConsole($console)
  98. {
  99. if (is_string($console)) {
  100. if ((get_class($this) != 'FirePHP_Insight') && !is_subclass_of($this, 'FirePHP_Insight')) {
  101. throw new Exception('FirePHP instance not an instance or subclass of FirePHP_Insight!');
  102. }
  103. $this->logToInsightConsole = $this->to('request')->console($console);
  104. }
  105. else {
  106. $this->logToInsightConsole = $console;
  107. }
  108. }
  109. public function setEnabled($enabled)
  110. {
  111. $this->enabled = $enabled;
  112. }
  113. public function getEnabled()
  114. {
  115. return $this->enabled;
  116. }
  117. public function setObjectFilter($class, $filter)
  118. {
  119. $this->objectFilters[strtolower($class)] = $filter;
  120. }
  121. public function setOptions($options)
  122. {
  123. $this->options = array_merge($this->options, $options);
  124. }
  125. public function getOptions()
  126. {
  127. return $this->options;
  128. }
  129. public function setOption($name, $value)
  130. {
  131. if (!isset($this->options[$name])) {
  132. }
  133. $this->options[$name] = $value;
  134. }
  135. public function getOption($name)
  136. {
  137. if (!isset($this->options[$name])) {
  138. }
  139. return $this->options[$name];
  140. }
  141. public function registerErrorHandler($throwErrorExceptions = false)
  142. {
  143. $this->throwErrorExceptions = $throwErrorExceptions;
  144. return set_error_handler(array($this, 'errorHandler'));
  145. }
  146. public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
  147. {
  148. if (error_reporting() == 0) {
  149. return NULL;
  150. }
  151. if (error_reporting() & $errno) {
  152. $exception = new ErrorException($errstr, 0, $errno, $errfile, $errline);
  153. if ($this->throwErrorExceptions) {
  154. throw $exception;
  155. }
  156. else {
  157. $this->fb($exception);
  158. }
  159. }
  160. }
  161. public function registerExceptionHandler()
  162. {
  163. return set_exception_handler(array($this, 'exceptionHandler'));
  164. }
  165. public function exceptionHandler($exception)
  166. {
  167. $this->inExceptionHandler = true;
  168. header('HTTP/1.1 500 Internal Server Error');
  169. try {
  170. $this->fb($exception);
  171. }
  172. catch (Exception $e) {
  173. echo 'We had an exception: ' . $e;
  174. }
  175. $this->inExceptionHandler = false;
  176. }
  177. public function registerAssertionHandler($convertAssertionErrorsToExceptions = true, $throwAssertionExceptions = false)
  178. {
  179. $this->convertAssertionErrorsToExceptions = $convertAssertionErrorsToExceptions;
  180. $this->throwAssertionExceptions = $throwAssertionExceptions;
  181. if ($throwAssertionExceptions && !$convertAssertionErrorsToExceptions) {
  182. }
  183. return assert_options(ASSERT_CALLBACK, array($this, 'assertionHandler'));
  184. }
  185. public function assertionHandler($file, $line, $code)
  186. {
  187. if ($this->convertAssertionErrorsToExceptions) {
  188. $exception = new ErrorException('Assertion Failed - Code[ ' . $code . ' ]', 0, null, $file, $line);
  189. if ($this->throwAssertionExceptions) {
  190. throw $exception;
  191. }
  192. else {
  193. $this->fb($exception);
  194. }
  195. }
  196. else {
  197. $this->fb($code, 'Assertion Failed', FirePHP::ERROR, array('File' => $file, 'Line' => $line));
  198. }
  199. }
  200. public function group($name, $options = NULL)
  201. {
  202. if (!$name) {
  203. }
  204. if ($options) {
  205. if (!is_array($options)) {
  206. }
  207. if (array_key_exists('Collapsed', $options)) {
  208. $options['Collapsed'] = $options['Collapsed'] ? 'true' : 'false';
  209. }
  210. }
  211. return $this->fb(null, $name, FirePHP::GROUP_START, $options);
  212. }
  213. public function groupEnd()
  214. {
  215. return $this->fb(null, null, FirePHP::GROUP_END);
  216. }
  217. public function log($object, $label = NULL, $options = array())
  218. {
  219. return $this->fb($object, $label, FirePHP::LOG, $options);
  220. }
  221. public function info($object, $label = NULL, $options = array())
  222. {
  223. return $this->fb($object, $label, FirePHP::INFO, $options);
  224. }
  225. public function warn($object, $label = NULL, $options = array())
  226. {
  227. return $this->fb($object, $label, FirePHP::WARN, $options);
  228. }
  229. public function error($object, $label = NULL, $options = array())
  230. {
  231. return $this->fb($object, $label, FirePHP::ERROR, $options);
  232. }
  233. public function dump($key, $variable, $options = array())
  234. {
  235. if (!is_string($key)) {
  236. }
  237. if (100 < strlen($key)) {
  238. }
  239. if (!preg_match_all('/^[a-zA-Z0-9-_\\.:]*$/', $key, $m)) {
  240. }
  241. return $this->fb($variable, $key, FirePHP::DUMP, $options);
  242. }
  243. public function trace($label)
  244. {
  245. return $this->fb($label, FirePHP::TRACE);
  246. }
  247. public function table($label, $table, $options = array())
  248. {
  249. return $this->fb($table, $label, FirePHP::TABLE, $options);
  250. }
  251. static public function to()
  252. {
  253. $instance = self::getInstance();
  254. if (!method_exists($instance, '_to')) {
  255. throw new Exception('FirePHP::to() implementation not loaded');
  256. }
  257. $args = func_get_args();
  258. return call_user_func_array(array($instance, '_to'), $args);
  259. }
  260. static public function plugin()
  261. {
  262. $instance = self::getInstance();
  263. if (!method_exists($instance, '_plugin')) {
  264. throw new Exception('FirePHP::plugin() implementation not loaded');
  265. }
  266. $args = func_get_args();
  267. return call_user_func_array(array($instance, '_plugin'), $args);
  268. }
  269. public function detectClientExtension()
  270. {
  271. if (@preg_match_all('/\\sFirePHP\\/([\\.\\d]*)\\s?/si', $this->getUserAgent(), $m) && version_compare($m[1][0], '0.0.6', '>=')) {
  272. return true;
  273. }
  274. else {
  275. if (@preg_match_all('/^([\\.\\d]*)$/si', $this->getRequestHeader('X-FirePHP-Version'), $m) && version_compare($m[1][0], '0.0.6', '>=')) {
  276. return true;
  277. }
  278. }
  279. return false;
  280. }
  281. public function fb($object)
  282. {
  283. if ($this instanceof FirePHP_Insight && method_exists($this, '_logUpgradeClientMessage')) {
  284. if (!FirePHP_Insight::$upgradeClientMessageLogged) {
  285. $this->_logUpgradeClientMessage();
  286. }
  287. }
  288. static $insightGroupStack = array();
  289. if (!$this->getEnabled()) {
  290. return false;
  291. }
  292. if ($this->headersSent($filename, $linenum)) {
  293. if ($this->inExceptionHandler) {
  294. 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>';
  295. }
  296. }
  297. $type = null;
  298. $label = null;
  299. $options = array();
  300. if (func_num_args() == 1) {
  301. }
  302. else if (func_num_args() == 2) {
  303. switch (func_get_arg(1)) {
  304. case self::LOG:
  305. case self::INFO:
  306. case self::WARN:
  307. case self::ERROR:
  308. case self::DUMP:
  309. case self::TRACE:
  310. case self::EXCEPTION:
  311. case self::TABLE:
  312. case self::GROUP_START:
  313. case self::GROUP_END:
  314. $type = func_get_arg(1);
  315. break;
  316. default:
  317. $label = func_get_arg(1);
  318. break;
  319. }
  320. }
  321. else if (func_num_args() == 3) {
  322. $type = func_get_arg(2);
  323. $label = func_get_arg(1);
  324. }
  325. else if (func_num_args() == 4) {
  326. $type = func_get_arg(2);
  327. $label = func_get_arg(1);
  328. $options = func_get_arg(3);
  329. }
  330. if (($this->logToInsightConsole !== null) && ((get_class($this) == 'FirePHP_Insight') || is_subclass_of($this, 'FirePHP_Insight'))) {
  331. $trace = debug_backtrace();
  332. if (!$trace) {
  333. return false;
  334. }
  335. for ($i = 0; $i < sizeof($trace); $i++) {
  336. if (isset($trace[$i]['class'])) {
  337. if (($trace[$i]['class'] == 'FirePHP') || ($trace[$i]['class'] == 'FB')) {
  338. continue;
  339. }
  340. }
  341. if (isset($trace[$i]['file'])) {
  342. $path = $this->_standardizePath($trace[$i]['file']);
  343. if ((substr($path, -18, 18) == 'FirePHPCore/fb.php') || (substr($path, -29, 29) == 'FirePHPCore/FirePHP.class.php')) {
  344. continue;
  345. }
  346. }
  347. 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')) {
  348. continue;
  349. }
  350. 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')) {
  351. continue;
  352. }
  353. break;
  354. }
  355. $msg = $this->logToInsightConsole->option('encoder.trace.offsetAdjustment', $i);
  356. if ($object instanceof Exception) {
  357. $type = self::EXCEPTION;
  358. }
  359. if ($label && ($type != self::TABLE) && ($type != self::GROUP_START)) {
  360. $msg = $msg->label($label);
  361. }
  362. switch ($type) {
  363. case self::DUMP:
  364. case self::LOG:
  365. return $msg->log($object);
  366. case self::INFO:
  367. return $msg->info($object);
  368. case self::WARN:
  369. return $msg->warn($object);
  370. case self::ERROR:
  371. return $msg->error($object);
  372. case self::TRACE:
  373. return $msg->trace($object);
  374. case self::EXCEPTION:
  375. return $this->plugin('error')->handleException($object, $msg);
  376. case self::TABLE:
  377. if (isset($object[0]) && !is_string($object[0]) && $label) {
  378. $object = array($label, $object);
  379. }
  380. return $msg->table($object[0], array_slice($object[1], 1), $object[1][0]);
  381. case self::GROUP_START:
  382. $insightGroupStack[] = $msg->group(md5($label))->open();
  383. return $msg->log($label);
  384. case self::GROUP_END:
  385. if (count($insightGroupStack) == 0) {
  386. throw new Error('Too many groupEnd() as opposed to group() calls!');
  387. }
  388. $group = array_pop($insightGroupStack);
  389. return $group->close();
  390. default:
  391. return $msg->log($object);
  392. }
  393. }
  394. if (!$this->detectClientExtension()) {
  395. return false;
  396. }
  397. $meta = array();
  398. $skipFinalObjectEncode = false;
  399. if ($object instanceof Exception) {
  400. $meta['file'] = $this->_escapeTraceFile($object->getFile());
  401. $meta['line'] = $object->getLine();
  402. $trace = $object->getTrace();
  403. if ($object instanceof ErrorException && isset($trace[0]['function']) && ($trace[0]['function'] == 'errorHandler') && isset($trace[0]['class']) && ($trace[0]['class'] == 'FirePHP')) {
  404. $severity = false;
  405. switch ($object->getSeverity()) {
  406. case E_WARNING:
  407. $severity = 'E_WARNING';
  408. break;
  409. case E_NOTICE:
  410. $severity = 'E_NOTICE';
  411. break;
  412. case E_USER_ERROR:
  413. $severity = 'E_USER_ERROR';
  414. break;
  415. case E_USER_WARNING:
  416. $severity = 'E_USER_WARNING';
  417. break;
  418. case E_USER_NOTICE:
  419. $severity = 'E_USER_NOTICE';
  420. break;
  421. case E_STRICT:
  422. $severity = 'E_STRICT';
  423. break;
  424. case E_RECOVERABLE_ERROR:
  425. $severity = 'E_RECOVERABLE_ERROR';
  426. break;
  427. case E_DEPRECATED:
  428. $severity = 'E_DEPRECATED';
  429. break;
  430. case E_USER_DEPRECATED:
  431. $severity = 'E_USER_DEPRECATED';
  432. break;
  433. }
  434. $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)));
  435. $skipFinalObjectEncode = true;
  436. }
  437. else {
  438. $object = array('Class' => get_class($object), 'Message' => $object->getMessage(), 'File' => $this->_escapeTraceFile($object->getFile()), 'Line' => $object->getLine(), 'Type' => 'throw', 'Trace' => $this->_escapeTrace($trace));
  439. $skipFinalObjectEncode = true;
  440. }
  441. $type = self::EXCEPTION;
  442. }
  443. else if ($type == self::TRACE) {
  444. $trace = debug_backtrace();
  445. if (!$trace) {
  446. return false;
  447. }
  448. for ($i = 0; $i < sizeof($trace); $i++) {
  449. 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'))) {
  450. }
  451. else {
  452. 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')) {
  453. }
  454. else {
  455. if (($trace[$i]['function'] == 'fb') || ($trace[$i]['function'] == 'trace') || ($trace[$i]['function'] == 'send')) {
  456. $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)));
  457. $skipFinalObjectEncode = true;
  458. $meta['file'] = isset($trace[$i]['file']) ? $this->_escapeTraceFile($trace[$i]['file']) : '';
  459. $meta['line'] = isset($trace[$i]['line']) ? $trace[$i]['line'] : '';
  460. break;
  461. }
  462. }
  463. }
  464. }
  465. }
  466. else if ($type == self::TABLE) {
  467. if (isset($object[0]) && is_string($object[0])) {
  468. $object[1] = $this->encodeTable($object[1]);
  469. }
  470. else {
  471. $object = $this->encodeTable($object);
  472. }
  473. $skipFinalObjectEncode = true;
  474. }
  475. else if ($type == self::GROUP_START) {
  476. if (!$label) {
  477. }
  478. }
  479. else if ($type === null) {
  480. $type = self::LOG;
  481. }
  482. if ($this->options['includeLineNumbers']) {
  483. if (!isset($meta['file']) || !isset($meta['line'])) {
  484. $trace = debug_backtrace();
  485. for ($i = 0; $i < sizeof($trace); $i++) {
  486. $skip = array('FirePHP' => '/FirePHP.class.php', 'FB' => '/fb.php', 'BaseModelDebug' => '/BaseModelDebug.php', 'BaseModelCommon' => '/BaseModelCommon.php', 'BaseModelDB' => '/BaseModelDB.php', 'BaseModelHttp' => '/BaseModelHttp.php');
  487. if (isset($trace[$i]['class']) && isset($trace[$i]['file']) && in_array($trace[$i]['class'], array_keys($skip), true) && in_array(strrchr($this->_standardizePath($trace[$i]['file']), '/'), $skip, true)) {
  488. }
  489. else {
  490. if (isset($trace[$i]['class']) && isset($trace[$i + 1]['file']) && in_array($trace[$i]['class'], array_keys($skip), true) && in_array(strrchr($this->_standardizePath($trace[$i + 1]['file']), '/'), $skip, true)) {
  491. }
  492. else {
  493. if (isset($trace[$i]['file']) && in_array(strrchr($this->_standardizePath($trace[$i]['file']), '/'), $skip, true)) {
  494. }
  495. else {
  496. $meta['file'] = isset($trace[$i]['file']) ? $this->_escapeTraceFile($trace[$i]['file']) : '';
  497. $meta['line'] = isset($trace[$i]['line']) ? $trace[$i]['line'] : '';
  498. break;
  499. }
  500. }
  501. }
  502. }
  503. }
  504. }
  505. else {
  506. unset($meta['file']);
  507. unset($meta['line']);
  508. }
  509. $this->setHeader('X-Wf-Protocol-1', 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
  510. $this->setHeader('X-Wf-1-Plugin-1', 'http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/' . self::VERSION);
  511. $structureIndex = 1;
  512. if ($type == self::DUMP) {
  513. $structureIndex = 2;
  514. $this->setHeader('X-Wf-1-Structure-2', 'http://meta.firephp.org/Wildfire/Structure/FirePHP/Dump/0.1');
  515. }
  516. else {
  517. $this->setHeader('X-Wf-1-Structure-1', 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1');
  518. }
  519. if ($type == self::DUMP) {
  520. $msg = '{"' . $label . '":' . $this->jsonEncode($object, $skipFinalObjectEncode) . '}';
  521. }
  522. else {
  523. $msgMeta = $options;
  524. $msgMeta['Type'] = $type;
  525. if ($label !== null) {
  526. $msgMeta['Label'] = $label;
  527. }
  528. if (isset($meta['file']) && !isset($msgMeta['File'])) {
  529. $msgMeta['File'] = $meta['file'];
  530. }
  531. if (isset($meta['line']) && !isset($msgMeta['Line'])) {
  532. $msgMeta['Line'] = $meta['line'];
  533. }
  534. $msg = '[' . $this->jsonEncode($msgMeta) . ',' . $this->jsonEncode($object, $skipFinalObjectEncode) . ']';
  535. }
  536. $parts = explode("\n", chunk_split($msg, 5000, "\n"));
  537. for ($i = 0; $i < count($parts); $i++) {
  538. $part = $parts[$i];
  539. if ($part) {
  540. if (2 < count($parts)) {
  541. $this->setHeader('X-Wf-1-' . $structureIndex . '-' . '1-' . $this->messageIndex, ($i == 0 ? strlen($msg) : '') . '|' . $part . '|' . ($i < (count($parts) - 2) ? '\\' : ''));
  542. }
  543. else {
  544. $this->setHeader('X-Wf-1-' . $structureIndex . '-' . '1-' . $this->messageIndex, strlen($part) . '|' . $part . '|');
  545. }
  546. $this->messageIndex++;
  547. if (99999 < $this->messageIndex) {
  548. }
  549. }
  550. }
  551. $this->setHeader('X-Wf-1-Index', $this->messageIndex - 1);
  552. return true;
  553. }
  554. protected function _standardizePath($path)
  555. {
  556. return preg_replace('/\\\\+/', '/', $path);
  557. }
  558. protected function _escapeTrace($trace)
  559. {
  560. if (!$trace) {
  561. return $trace;
  562. }
  563. for ($i = 0; $i < sizeof($trace); $i++) {
  564. if (isset($trace[$i]['file'])) {
  565. $trace[$i]['file'] = $this->_escapeTraceFile($trace[$i]['file']);
  566. }
  567. if (isset($trace[$i]['args'])) {
  568. $trace[$i]['args'] = $this->encodeObject($trace[$i]['args']);
  569. }
  570. }
  571. return $trace;
  572. }
  573. protected function _escapeTraceFile($file)
  574. {
  575. if (strpos($file, '\\')) {
  576. $file = preg_replace('/\\\\+/', '\\', $file);
  577. return $file;
  578. }
  579. return $file;
  580. }
  581. protected function headersSent(&$filename, &$linenum)
  582. {
  583. return headers_sent($filename, $linenum);
  584. }
  585. protected function setHeader($name, $value)
  586. {
  587. return @header($name . ': ' . $value);
  588. }
  589. protected function getUserAgent()
  590. {
  591. if (!isset($_SERVER['HTTP_USER_AGENT'])) {
  592. return false;
  593. }
  594. return $_SERVER['HTTP_USER_AGENT'];
  595. }
  596. static public function getAllRequestHeaders()
  597. {
  598. static $_cachedHeaders = false;
  599. if ($_cachedHeaders !== false) {
  600. return $_cachedHeaders;
  601. }
  602. $headers = array();
  603. if (function_exists('getallheaders')) {
  604. foreach (getallheaders() as $name => $value) {
  605. $headers[strtolower($name)] = $value;
  606. }
  607. }
  608. else {
  609. foreach ($_SERVER as $name => $value) {
  610. if (substr($name, 0, 5) == 'HTTP_') {
  611. $headers[strtolower(str_replace(' ', '-', str_replace('_', ' ', substr($name, 5))))] = $value;
  612. }
  613. }
  614. }
  615. return $_cachedHeaders = $headers;
  616. }
  617. protected function getRequestHeader($name)
  618. {
  619. $headers = self::getAllRequestHeaders();
  620. if (isset($headers[strtolower($name)])) {
  621. return $headers[strtolower($name)];
  622. }
  623. return false;
  624. }
  625. protected function newException($message)
  626. {
  627. }
  628. public function jsonEncode($object, $skipObjectEncode = false)
  629. {
  630. if (!$skipObjectEncode) {
  631. $object = $this->encodeObject($object);
  632. }
  633. if (function_exists('json_encode') && ($this->options['useNativeJsonEncode'] != false)) {
  634. return json_encode($object);
  635. }
  636. else {
  637. return $this->json_encode($object);
  638. }
  639. }
  640. protected function encodeTable($table)
  641. {
  642. if (!$table) {
  643. return $table;
  644. }
  645. $newTable = array();
  646. foreach ($table as $row) {
  647. if (is_array($row)) {
  648. $newRow = array();
  649. foreach ($row as $item) {
  650. $newRow[] = $this->encodeObject($item);
  651. }
  652. $newTable[] = $newRow;
  653. }
  654. }
  655. return $newTable;
  656. }
  657. protected function encodeObject($object, $objectDepth = 1, $arrayDepth = 1, $maxDepth = 1)
  658. {
  659. if ($this->options['maxDepth'] < $maxDepth) {
  660. return '** Max Depth (' . $this->options['maxDepth'] . ') **';
  661. }
  662. $return = array();
  663. if (is_resource($object)) {
  664. return '** ' . (string) $object . ' **';
  665. }
  666. else if (is_object($object)) {
  667. if ($this->options['maxObjectDepth'] < $objectDepth) {
  668. return '** Max Object Depth (' . $this->options['maxObjectDepth'] . ') **';
  669. }
  670. foreach ($this->objectStack as $refVal) {
  671. if ($refVal === $object) {
  672. return '** Recursion (' . get_class($object) . ') **';
  673. }
  674. }
  675. array_push($this->objectStack, $object);
  676. $return['__className'] = $class = get_class($object);
  677. $classLower = strtolower($class);
  678. $reflectionClass = new ReflectionClass($class);
  679. $properties = array();
  680. foreach ($reflectionClass->getProperties() as $property) {
  681. $properties[$property->getName()] = $property;
  682. }
  683. $members = (array) $object;
  684. foreach ($properties as $plainName => $property) {
  685. $name = $rawName = $plainName;
  686. if ($property->isStatic()) {
  687. $name = 'static:' . $name;
  688. }
  689. if ($property->isPublic()) {
  690. $name = 'public:' . $name;
  691. }
  692. else if ($property->isPrivate()) {
  693. $name = 'private:' . $name;
  694. $rawName = "\x00" . $class . "\x00" . $rawName;
  695. }
  696. else if ($property->isProtected()) {
  697. $name = 'protected:' . $name;
  698. $rawName = "\x00" . '*' . "\x00" . $rawName;
  699. }
  700. if (!(isset($this->objectFilters[$classLower]) && is_array($this->objectFilters[$classLower]) && in_array($plainName, $this->objectFilters[$classLower]))) {
  701. if (array_key_exists($rawName, $members) && !$property->isStatic()) {
  702. $return[$name] = $this->encodeObject($members[$rawName], $objectDepth + 1, 1, $maxDepth + 1);
  703. }
  704. else if (method_exists($property, 'setAccessible')) {
  705. $property->setAccessible(true);
  706. $return[$name] = $this->encodeObject($property->getValue($object), $objectDepth + 1, 1, $maxDepth + 1);
  707. }
  708. else if ($property->isPublic()) {
  709. $return[$name] = $this->encodeObject($property->getValue($object), $objectDepth + 1, 1, $maxDepth + 1);
  710. }
  711. else {
  712. $return[$name] = '** Need PHP 5.3 to get value **';
  713. }
  714. }
  715. else {
  716. $return[$name] = '** Excluded by Filter **';
  717. }
  718. }
  719. foreach ($members as $rawName => $value) {
  720. $name = $rawName;
  721. if ($name[0] == "\x00") {
  722. $parts = explode("\x00", $name);
  723. $name = $parts[2];
  724. }
  725. $plainName = $name;
  726. if (!isset($properties[$name])) {
  727. $name = 'undeclared:' . $name;
  728. if (!(isset($this->objectFilters[$classLower]) && is_array($this->objectFilters[$classLower]) && in_array($plainName, $this->objectFilters[$classLower]))) {
  729. $return[$name] = $this->encodeObject($value, $objectDepth + 1, 1, $maxDepth + 1);
  730. }
  731. else {
  732. $return[$name] = '** Excluded by Filter **';
  733. }
  734. }
  735. }
  736. array_pop($this->objectStack);
  737. }
  738. else if (is_array($object)) {
  739. if ($this->options['maxArrayDepth'] < $arrayDepth) {
  740. return '** Max Array Depth (' . $this->options['maxArrayDepth'] . ') **';
  741. }
  742. foreach ($object as $key => $val) {
  743. if (($key == 'GLOBALS') && is_array($val) && array_key_exists('GLOBALS', $val)) {
  744. $val['GLOBALS'] = '** Recursion (GLOBALS) **';
  745. }
  746. if (!$this->is_utf8($key)) {
  747. $key = utf8_encode($key);
  748. }
  749. $return[$key] = $this->encodeObject($val, 1, $arrayDepth + 1, $maxDepth + 1);
  750. }
  751. }
  752. else if ($this->is_utf8($object)) {
  753. return $object;
  754. }
  755. else {
  756. return utf8_encode($object);
  757. }
  758. return $return;
  759. }
  760. protected function is_utf8($str)
  761. {
  762. if (function_exists('mb_detect_encoding')) {
  763. return (mb_detect_encoding($str, 'UTF-8', true) == 'UTF-8') && (($str === null) || ($this->jsonEncode($str, true) !== 'null'));
  764. }
  765. $c = 0;
  766. $b = 0;
  767. $bits = 0;
  768. $len = strlen($str);
  769. for ($i = 0; $i < $len; $i++) {
  770. $c = ord($str[$i]);
  771. if (128 < $c) {
  772. if (254 <= $c) {
  773. return false;
  774. }
  775. else if (252 <= $c) {
  776. $bits = 6;
  777. }
  778. else if (248 <= $c) {
  779. $bits = 5;
  780. }
  781. else if (240 <= $c) {
  782. $bits = 4;
  783. }
  784. else if (224 <= $c) {
  785. $bits = 3;
  786. }
  787. else if (192 <= $c) {
  788. $bits = 2;
  789. }
  790. else {
  791. return false;
  792. }
  793. if ($len < ($i + $bits)) {
  794. return false;
  795. }
  796. while (1 < $bits) {
  797. $i++;
  798. $b = ord($str[$i]);
  799. if (($b < 128) || (191 < $b)) {
  800. return false;
  801. }
  802. $bits--;
  803. }
  804. }
  805. }
  806. return ($str === null) || ($this->jsonEncode($str, true) !== 'null');
  807. }
  808. private function json_utf82utf16($utf8)
  809. {
  810. if (function_exists('mb_convert_encoding')) {
  811. return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
  812. }
  813. switch (strlen($utf8)) {
  814. case 1:
  815. return $utf8;
  816. case 2:
  817. return chr(7 & (ord($utf8[0]) >> 2)) . chr((192 & (ord($utf8[0]) << 6)) | (63 & ord($utf8[1])));
  818. case 3:
  819. return chr((240 & (ord($utf8[0]) << 4)) | (15 & (ord($utf8[1]) >> 2))) . chr((192 & (ord($utf8[1]) << 6)) | (127 & ord($utf8[2])));
  820. }
  821. return '';
  822. }
  823. private function json_encode($var)
  824. {
  825. if (is_object($var)) {
  826. if (in_array($var, $this->json_objectStack)) {
  827. return '"** Recursion **"';
  828. }
  829. }
  830. switch (gettype($var)) {
  831. case 'boolean':
  832. return $var ? 'true' : 'false';
  833. case 'NULL':
  834. return 'null';
  835. case 'integer':
  836. return (int) $var;
  837. case 'double':
  838. case 'float':
  839. return (double) $var;
  840. case 'string':
  841. $ascii = '';
  842. $strlen_var = strlen($var);
  843. for ($c = 0; $c < $strlen_var; ++$c) {
  844. $ord_var_c = ord($var[$c]);
  845. switch (true) {
  846. case $ord_var_c == 8:
  847. $ascii .= '\\b';
  848. break;
  849. case $ord_var_c == 9:
  850. $ascii .= '\\t';
  851. break;
  852. case $ord_var_c == 10:
  853. $ascii .= '\\n';
  854. break;
  855. case $ord_var_c == 12:
  856. $ascii .= '\\f';
  857. break;
  858. case $ord_var_c == 13:
  859. $ascii .= '\\r';
  860. break;
  861. case $ord_var_c == 34:
  862. case $ord_var_c == 47:
  863. case $ord_var_c == 92:
  864. $ascii .= '\\' . $var[$c];
  865. break;
  866. case $ord_var_c <= 127:
  867. $ascii .= $var[$c];
  868. break;
  869. case ($ord_var_c & 224) == 192:
  870. $char = pack('C*', $ord_var_c, ord($var[$c + 1]));
  871. $c += 1;
  872. $utf16 = $this->json_utf82utf16($char);
  873. $ascii .= sprintf('\\u%04s', bin2hex($utf16));
  874. break;
  875. case ($ord_var_c & 240) == 224:
  876. $char = pack('C*', $ord_var_c, ord($var[$c + 1]), ord($var[$c + 2]));
  877. $c += 2;
  878. $utf16 = $this->json_utf82utf16($char);
  879. $ascii .= sprintf('\\u%04s', bin2hex($utf16));
  880. break;
  881. case ($ord_var_c & 248) == 240:
  882. $char = pack('C*', $ord_var_c, ord($var[$c + 1]), ord($var[$c + 2]), ord($var[$c + 3]));
  883. $c += 3;
  884. $utf16 = $this->json_utf82utf16($char);
  885. $ascii .= sprintf('\\u%04s', bin2hex($utf16));
  886. break;
  887. case ($ord_var_c & 252) == 248:
  888. $char = pack('C*', $ord_var_c, ord($var[$c + 1]), ord($var[$c + 2]), ord($var[$c + 3]), ord($var[$c + 4]));
  889. $c += 4;
  890. $utf16 = $this->json_utf82utf16($char);
  891. $ascii .= sprintf('\\u%04s', bin2hex($utf16));
  892. break;
  893. case ($ord_var_c & 254) == 252:
  894. $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]));
  895. $c += 5;
  896. $utf16 = $this->json_utf82utf16($char);
  897. $ascii .= sprintf('\\u%04s', bin2hex($utf16));
  898. break;
  899. }
  900. }
  901. return '"' . $ascii . '"';
  902. case 'array':
  903. if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) {
  904. $this->json_objectStack[] = $var;
  905. $properties = array_map(array($this, 'json_name_value'), array_keys($var), array_values($var));
  906. array_pop($this->json_objectStack);
  907. foreach ($properties as $property) {
  908. if ($property instanceof Exception) {
  909. return $property;
  910. }
  911. }
  912. return '{' . join(',', $properties) . '}';
  913. }
  914. $this->json_objectStack[] = $var;
  915. $elements = array_map(array($this, 'json_encode'), $var);
  916. array_pop($this->json_objectStack);
  917. foreach ($elements as $element) {
  918. if ($element instanceof Exception) {
  919. return $element;
  920. }
  921. }
  922. return '[' . join(',', $elements) . ']';
  923. case 'object':
  924. $vars = self::encodeObject($var);
  925. $this->json_objectStack[] = $var;
  926. $properties = array_map(array($this, 'json_name_value'), array_keys($vars), array_values($vars));
  927. array_pop($this->json_objectStack);
  928. foreach ($properties as $property) {
  929. if ($property instanceof Exception) {
  930. return $property;
  931. }
  932. }
  933. return '{' . join(',', $properties) . '}';
  934. default:
  935. return null;
  936. }
  937. }
  938. private function json_name_value($name, $value)
  939. {
  940. if (($name == 'GLOBALS') && is_array($value) && array_key_exists('GLOBALS', $value)) {
  941. $value['GLOBALS'] = '** Recursion **';
  942. }
  943. $encodedValue = $this->json_encode($value);
  944. if ($encodedValue instanceof Exception) {
  945. return $encodedValue;
  946. }
  947. return $this->json_encode(strval($name)) . ':' . $encodedValue;
  948. }
  949. public function setProcessorUrl($URL)
  950. {
  951. trigger_error('The FirePHP::setProcessorUrl() method is no longer supported', E_USER_DEPRECATED);
  952. }
  953. public function setRendererUrl($URL)
  954. {
  955. trigger_error('The FirePHP::setRendererUrl() method is no longer supported', E_USER_DEPRECATED);
  956. }
  957. }
  958. if (!defined('E_STRICT')) {
  959. define('E_STRICT', 2048);
  960. }
  961. if (!defined('E_RECOVERABLE_ERROR')) {
  962. define('E_RECOVERABLE_ERROR', 4096);
  963. }
  964. if (!defined('E_DEPRECATED')) {
  965. define('E_DEPRECATED', 8192);
  966. }
  967. if (!defined('E_USER_DEPRECATED')) {
  968. define('E_USER_DEPRECATED', 16384);
  969. }
  970. ?>