Debugger.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. <?php
  2. /**
  3. * Framework debugging and PHP error-handling class
  4. *
  5. * Provides enhanced logging, stack traces, and rendering debug views
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package Cake.Utility
  18. * @since CakePHP(tm) v 1.2.4560
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. /**
  22. * Included libraries.
  23. *
  24. */
  25. App::uses('CakeLog', 'Log');
  26. App::uses('String', 'Utility');
  27. /**
  28. * Provide custom logging and error handling.
  29. *
  30. * Debugger overrides PHP's default error handling to provide stack traces and enhanced logging
  31. *
  32. * @package Cake.Utility
  33. * @link http://book.cakephp.org/2.0/en/development/debugging.html#debugger-class
  34. */
  35. class Debugger {
  36. /**
  37. * A list of errors generated by the application.
  38. *
  39. * @var array
  40. */
  41. public $errors = array();
  42. /**
  43. * The current output format.
  44. *
  45. * @var string
  46. */
  47. protected $_outputFormat = 'js';
  48. /**
  49. * Templates used when generating trace or error strings. Can be global or indexed by the format
  50. * value used in $_outputFormat.
  51. *
  52. * @var string
  53. */
  54. protected $_templates = array(
  55. 'log' => array(
  56. 'trace' => '{:reference} - {:path}, line {:line}',
  57. 'error' => "{:error} ({:code}): {:description} in [{:file}, line {:line}]"
  58. ),
  59. 'js' => array(
  60. 'error' => '',
  61. 'info' => '',
  62. 'trace' => '<pre class="stack-trace">{:trace}</pre>',
  63. 'code' => '',
  64. 'context' => '',
  65. 'links' => array()
  66. ),
  67. 'html' => array(
  68. 'trace' => '<pre class="cake-error trace"><b>Trace</b> <p>{:trace}</p></pre>',
  69. 'context' => '<pre class="cake-error context"><b>Context</b> <p>{:context}</p></pre>'
  70. ),
  71. 'txt' => array(
  72. 'error' => "{:error}: {:code} :: {:description} on line {:line} of {:path}\n{:info}",
  73. 'code' => '',
  74. 'info' => ''
  75. ),
  76. 'base' => array(
  77. 'traceLine' => '{:reference} - {:path}, line {:line}',
  78. 'trace' => "Trace:\n{:trace}\n",
  79. 'context' => "Context:\n{:context}\n",
  80. ),
  81. 'log' => array(),
  82. );
  83. /**
  84. * Holds current output data when outputFormat is false.
  85. *
  86. * @var string
  87. */
  88. protected $_data = array();
  89. /**
  90. * Constructor.
  91. *
  92. */
  93. public function __construct() {
  94. $docRef = ini_get('docref_root');
  95. if (empty($docRef) && function_exists('ini_set')) {
  96. ini_set('docref_root', 'http://php.net/');
  97. }
  98. if (!defined('E_RECOVERABLE_ERROR')) {
  99. define('E_RECOVERABLE_ERROR', 4096);
  100. }
  101. $e = '<pre class="cake-error">';
  102. $e .= '<a href="javascript:void(0);" onclick="document.getElementById(\'{:id}-trace\')';
  103. $e .= '.style.display = (document.getElementById(\'{:id}-trace\').style.display == ';
  104. $e .= '\'none\' ? \'\' : \'none\');"><b>{:error}</b> ({:code})</a>: {:description} ';
  105. $e .= '[<b>{:path}</b>, line <b>{:line}</b>]';
  106. $e .= '<div id="{:id}-trace" class="cake-stack-trace" style="display: none;">';
  107. $e .= '{:links}{:info}</div>';
  108. $e .= '</pre>';
  109. $this->_templates['js']['error'] = $e;
  110. $t = '<div id="{:id}-trace" class="cake-stack-trace" style="display: none;">';
  111. $t .= '{:context}{:code}{:trace}</div>';
  112. $this->_templates['js']['info'] = $t;
  113. $links = array();
  114. $link = '<a href="javascript:void(0);" onclick="document.getElementById(\'{:id}-code\')';
  115. $link .= '.style.display = (document.getElementById(\'{:id}-code\').style.display == ';
  116. $link .= '\'none\' ? \'\' : \'none\')">Code</a>';
  117. $links['code'] = $link;
  118. $link = '<a href="javascript:void(0);" onclick="document.getElementById(\'{:id}-context\')';
  119. $link .= '.style.display = (document.getElementById(\'{:id}-context\').style.display == ';
  120. $link .= '\'none\' ? \'\' : \'none\')">Context</a>';
  121. $links['context'] = $link;
  122. $this->_templates['js']['links'] = $links;
  123. $this->_templates['js']['context'] = '<pre id="{:id}-context" class="cake-context" ';
  124. $this->_templates['js']['context'] .= 'style="display: none;">{:context}</pre>';
  125. $this->_templates['js']['code'] = '<pre id="{:id}-code" class="cake-code-dump" ';
  126. $this->_templates['js']['code'] .= 'style="display: none;">{:code}</pre>';
  127. $e = '<pre class="cake-error"><b>{:error}</b> ({:code}) : {:description} ';
  128. $e .= '[<b>{:path}</b>, line <b>{:line}]</b></pre>';
  129. $this->_templates['html']['error'] = $e;
  130. $this->_templates['html']['context'] = '<pre class="cake-context"><b>Context</b> ';
  131. $this->_templates['html']['context'] .= '<p>{:context}</p></pre>';
  132. }
  133. /**
  134. * Returns a reference to the Debugger singleton object instance.
  135. *
  136. * @param string $class
  137. * @return object
  138. */
  139. public static function &getInstance($class = null) {
  140. static $instance = array();
  141. if (!empty($class)) {
  142. if (!$instance || strtolower($class) != strtolower(get_class($instance[0]))) {
  143. $instance[0] = new $class();
  144. }
  145. }
  146. if (!$instance) {
  147. $instance[0] = new Debugger();
  148. }
  149. return $instance[0];
  150. }
  151. /**
  152. * Recursively formats and outputs the contents of the supplied variable.
  153. *
  154. *
  155. * @param mixed $var the variable to dump
  156. * @return void
  157. * @see Debugger::exportVar()
  158. * @link http://book.cakephp.org/2.0/en/development/debugging.html#Debugger::dump
  159. */
  160. public static function dump($var) {
  161. pr(self::exportVar($var));
  162. }
  163. /**
  164. * Creates an entry in the log file. The log entry will contain a stack trace from where it was called.
  165. * as well as export the variable using exportVar. By default the log is written to the debug log.
  166. *
  167. * @param mixed $var Variable or content to log
  168. * @param integer $level type of log to use. Defaults to LOG_DEBUG
  169. * @return void
  170. * @link http://book.cakephp.org/2.0/en/development/debugging.html#Debugger::log
  171. */
  172. public static function log($var, $level = LOG_DEBUG) {
  173. $source = self::trace(array('start' => 1)) . "\n";
  174. CakeLog::write($level, "\n" . $source . self::exportVar($var));
  175. }
  176. /**
  177. * Overrides PHP's default error handling.
  178. *
  179. * @param integer $code Code of error
  180. * @param string $description Error description
  181. * @param string $file File on which error occurred
  182. * @param integer $line Line that triggered the error
  183. * @param array $context Context
  184. * @return boolean true if error was handled
  185. * @deprecated This function is superseded by Debugger::outputError()
  186. */
  187. public static function showError($code, $description, $file = null, $line = null, $context = null) {
  188. $_this = Debugger::getInstance();
  189. if (empty($file)) {
  190. $file = '[internal]';
  191. }
  192. if (empty($line)) {
  193. $line = '??';
  194. }
  195. $path = self::trimPath($file);
  196. $info = compact('code', 'description', 'file', 'line');
  197. if (!in_array($info, $_this->errors)) {
  198. $_this->errors[] = $info;
  199. } else {
  200. return;
  201. }
  202. switch ($code) {
  203. case E_PARSE:
  204. case E_ERROR:
  205. case E_CORE_ERROR:
  206. case E_COMPILE_ERROR:
  207. case E_USER_ERROR:
  208. $error = 'Fatal Error';
  209. $level = LOG_ERROR;
  210. break;
  211. case E_WARNING:
  212. case E_USER_WARNING:
  213. case E_COMPILE_WARNING:
  214. case E_RECOVERABLE_ERROR:
  215. $error = 'Warning';
  216. $level = LOG_WARNING;
  217. break;
  218. case E_NOTICE:
  219. case E_USER_NOTICE:
  220. $error = 'Notice';
  221. $level = LOG_NOTICE;
  222. break;
  223. default:
  224. return;
  225. break;
  226. }
  227. $data = compact(
  228. 'level', 'error', 'code', 'description', 'file', 'path', 'line', 'context'
  229. );
  230. echo $_this->outputError($data);
  231. if ($error == 'Fatal Error') {
  232. exit();
  233. }
  234. return true;
  235. }
  236. /**
  237. * Outputs a stack trace based on the supplied options.
  238. *
  239. * ### Options
  240. *
  241. * - `depth` - The number of stack frames to return. Defaults to 999
  242. * - `format` - The format you want the return. Defaults to the currently selected format. If
  243. * format is 'array' or 'points' the return will be an array.
  244. * - `args` - Should arguments for functions be shown? If true, the arguments for each method call
  245. * will be displayed.
  246. * - `start` - The stack frame to start generating a trace from. Defaults to 0
  247. *
  248. * @param array $options Format for outputting stack trace
  249. * @return mixed Formatted stack trace
  250. * @link http://book.cakephp.org/2.0/en/development/debugging.html#Debugger::trace
  251. */
  252. public static function trace($options = array()) {
  253. $_this = Debugger::getInstance();
  254. $defaults = array(
  255. 'depth' => 999,
  256. 'format' => $_this->_outputFormat,
  257. 'args' => false,
  258. 'start' => 0,
  259. 'scope' => null,
  260. 'exclude' => null
  261. );
  262. $options += $defaults;
  263. $backtrace = debug_backtrace();
  264. $count = count($backtrace);
  265. $back = array();
  266. $_trace = array(
  267. 'line' => '??',
  268. 'file' => '[internal]',
  269. 'class' => null,
  270. 'function' => '[main]'
  271. );
  272. for ($i = $options['start']; $i < $count && $i < $options['depth']; $i++) {
  273. $trace = array_merge(array('file' => '[internal]', 'line' => '??'), $backtrace[$i]);
  274. if (isset($backtrace[$i + 1])) {
  275. $next = array_merge($_trace, $backtrace[$i + 1]);
  276. $reference = $next['function'];
  277. if (!empty($next['class'])) {
  278. $reference = $next['class'] . '::' . $reference . '(';
  279. if ($options['args'] && isset($next['args'])) {
  280. $args = array();
  281. foreach ($next['args'] as $arg) {
  282. $args[] = Debugger::exportVar($arg);
  283. }
  284. $reference .= join(', ', $args);
  285. }
  286. $reference .= ')';
  287. }
  288. } else {
  289. $reference = '[main]';
  290. }
  291. if (in_array($reference, array('call_user_func_array', 'trigger_error'))) {
  292. continue;
  293. }
  294. if ($options['format'] == 'points' && $trace['file'] != '[internal]') {
  295. $back[] = array('file' => $trace['file'], 'line' => $trace['line']);
  296. } elseif ($options['format'] == 'array') {
  297. $back[] = $trace;
  298. } else {
  299. if (isset($_this->_templates[$options['format']]['traceLine'])) {
  300. $tpl = $_this->_templates[$options['format']]['traceLine'];
  301. } else {
  302. $tpl = $_this->_templates['base']['traceLine'];
  303. }
  304. $trace['path'] = self::trimPath($trace['file']);
  305. $trace['reference'] = $reference;
  306. unset($trace['object'], $trace['args']);
  307. $back[] = String::insert($tpl, $trace, array('before' => '{:', 'after' => '}'));
  308. }
  309. }
  310. if ($options['format'] == 'array' || $options['format'] == 'points') {
  311. return $back;
  312. }
  313. return implode("\n", $back);
  314. }
  315. /**
  316. * Shortens file paths by replacing the application base path with 'APP', and the CakePHP core
  317. * path with 'CORE'.
  318. *
  319. * @param string $path Path to shorten
  320. * @return string Normalized path
  321. */
  322. public static function trimPath($path) {
  323. if (!defined('CAKE_CORE_INCLUDE_PATH') || !defined('APP')) {
  324. return $path;
  325. }
  326. if (strpos($path, APP) === 0) {
  327. return str_replace(APP, 'APP' . DS, $path);
  328. } elseif (strpos($path, CAKE_CORE_INCLUDE_PATH) === 0) {
  329. return str_replace(CAKE_CORE_INCLUDE_PATH, 'CORE', $path);
  330. } elseif (strpos($path, ROOT) === 0) {
  331. return str_replace(ROOT, 'ROOT', $path);
  332. }
  333. if (strpos($path, CAKE) === 0) {
  334. return str_replace($corePath, 'CORE' . DS, $path);
  335. }
  336. return $path;
  337. }
  338. /**
  339. * Grabs an excerpt from a file and highlights a given line of code.
  340. *
  341. * Usage:
  342. *
  343. * `Debugger::excerpt('/path/to/file', 100, 4);`
  344. *
  345. * The above would return an array of 8 items. The 4th item would be the provided line,
  346. * and would be wrapped in `<span class="code-highlight"></span>`. All of the lines
  347. * are processed with highlight_string() as well, so they have basic PHP syntax highlighting
  348. * applied.
  349. *
  350. * @param string $file Absolute path to a PHP file
  351. * @param integer $line Line number to highlight
  352. * @param integer $context Number of lines of context to extract above and below $line
  353. * @return array Set of lines highlighted
  354. * @see http://php.net/highlight_string
  355. * @link http://book.cakephp.org/2.0/en/development/debugging.html#Debugger::excerpt
  356. */
  357. public static function excerpt($file, $line, $context = 2) {
  358. $lines = array();
  359. if (!file_exists($file)) {
  360. return array();
  361. }
  362. $data = @explode("\n", file_get_contents($file));
  363. if (empty($data) || !isset($data[$line])) {
  364. return;
  365. }
  366. for ($i = $line - ($context + 1); $i < $line + $context; $i++) {
  367. if (!isset($data[$i])) {
  368. continue;
  369. }
  370. $string = str_replace(array("\r\n", "\n"), "", highlight_string($data[$i], true));
  371. if ($i == $line) {
  372. $lines[] = '<span class="code-highlight">' . $string . '</span>';
  373. } else {
  374. $lines[] = $string;
  375. }
  376. }
  377. return $lines;
  378. }
  379. /**
  380. * Converts a variable to a string for debug output.
  381. *
  382. * *Note:* The following keys will have their contents replaced with
  383. * `*****`:
  384. *
  385. * - password
  386. * - login
  387. * - host
  388. * - database
  389. * - port
  390. * - prefix
  391. * - schema
  392. *
  393. * This is done to protect database credentials, which could be accidentally
  394. * shown in an error message if CakePHP is deployed in development mode.
  395. *
  396. * @param string $var Variable to convert
  397. * @param integer $recursion
  398. * @return string Variable as a formatted string
  399. * @link http://book.cakephp.org/2.0/en/development/debugging.html#Debugger::exportVar
  400. */
  401. public static function exportVar($var, $recursion = 0) {
  402. switch (strtolower(gettype($var))) {
  403. case 'boolean':
  404. return ($var) ? 'true' : 'false';
  405. break;
  406. case 'integer':
  407. case 'double':
  408. return $var;
  409. break;
  410. case 'string':
  411. if (trim($var) == "") {
  412. return '""';
  413. }
  414. return '"' . h($var) . '"';
  415. break;
  416. case 'object':
  417. return get_class($var) . "\n" . self::_object($var);
  418. case 'array':
  419. $var = array_merge($var, array_intersect_key(array(
  420. 'password' => '*****',
  421. 'login' => '*****',
  422. 'host' => '*****',
  423. 'database' => '*****',
  424. 'port' => '*****',
  425. 'prefix' => '*****',
  426. 'schema' => '*****'
  427. ), $var));
  428. $out = "array(";
  429. $vars = array();
  430. foreach ($var as $key => $val) {
  431. if ($recursion >= 0) {
  432. if (is_numeric($key)) {
  433. $vars[] = "\n\t" . self::exportVar($val, $recursion - 1);
  434. } else {
  435. $vars[] = "\n\t" . self::exportVar($key, $recursion - 1)
  436. . ' => ' . self::exportVar($val, $recursion - 1);
  437. }
  438. }
  439. }
  440. $n = null;
  441. if (!empty($vars)) {
  442. $n = "\n";
  443. }
  444. return $out . implode(",", $vars) . "{$n})";
  445. break;
  446. case 'resource':
  447. return strtolower(gettype($var));
  448. break;
  449. case 'null':
  450. return 'null';
  451. break;
  452. }
  453. }
  454. /**
  455. * Handles object to string conversion.
  456. *
  457. * @param string $var Object to convert
  458. * @return string
  459. * @see Debugger::exportVar()
  460. */
  461. protected static function _object($var) {
  462. $out = array();
  463. if (is_object($var)) {
  464. $className = get_class($var);
  465. $objectVars = get_object_vars($var);
  466. foreach ($objectVars as $key => $value) {
  467. if (is_object($value)) {
  468. $value = get_class($value) . ' object';
  469. } elseif (is_array($value)) {
  470. $value = 'array';
  471. } elseif ($value === null) {
  472. $value = 'NULL';
  473. } elseif (in_array(gettype($value), array('boolean', 'integer', 'double', 'string', 'array', 'resource'))) {
  474. $value = Debugger::exportVar($value);
  475. }
  476. $out[] = "$className::$$key = " . $value;
  477. }
  478. }
  479. return implode("\n", $out);
  480. }
  481. /**
  482. * Get/Set the output format for Debugger error rendering.
  483. *
  484. * @param string $format The format you want errors to be output as.
  485. * Leave null to get the current format.
  486. * @return mixed Returns null when setting. Returns the current format when getting.
  487. * @throws CakeException when choosing a format that doesn't exist.
  488. */
  489. public static function outputAs($format = null) {
  490. $self = Debugger::getInstance();
  491. if ($format === null) {
  492. return $self->_outputFormat;
  493. }
  494. if ($format !== false && !isset($self->_templates[$format])) {
  495. throw new CakeException(__d('cake_dev', 'Invalid Debugger output format.'));
  496. }
  497. $self->_outputFormat = $format;
  498. }
  499. /**
  500. * Add an output format or update a format in Debugger.
  501. *
  502. * `Debugger::addFormat('custom', $data);`
  503. *
  504. * Where $data is an array of strings that use String::insert() variable
  505. * replacement. The template vars should be in a `{:id}` style.
  506. * An error formatter can have the following keys:
  507. *
  508. * - 'error' - Used for the container for the error message. Gets the following template
  509. * variables: `id`, `error`, `code`, `description`, `path`, `line`, `links`, `info`
  510. * - 'info' - A combination of `code`, `context` and `trace`. Will be set with
  511. * the contents of the other template keys.
  512. * - 'trace' - The container for a stack trace. Gets the following template
  513. * variables: `trace`
  514. * - 'context' - The container element for the context variables.
  515. * Gets the following templates: `id`, `context`
  516. * - 'links' - An array of HTML links that are used for creating links to other resources.
  517. * Typically this is used to create javascript links to open other sections.
  518. * Link keys, are: `code`, `context`, `help`. See the js output format for an
  519. * example.
  520. * - 'traceLine' - Used for creating lines in the stacktrace. Gets the following
  521. * template variables: `reference`, `path`, `line`
  522. *
  523. * Alternatively if you want to use a custom callback to do all the formatting, you can use
  524. * the callback key, and provide a callable:
  525. *
  526. * `Debugger::addFormat('custom', array('callback' => array($foo, 'outputError'));`
  527. *
  528. * The callback can expect two parameters. The first is an array of all
  529. * the error data. The second contains the formatted strings generated using
  530. * the other template strings. Keys like `info`, `links`, `code`, `context` and `trace`
  531. * will be present depending on the other templates in the format type.
  532. *
  533. * @param string $format Format to use, including 'js' for JavaScript-enhanced HTML, 'html' for
  534. * straight HTML output, or 'txt' for unformatted text.
  535. * @param array $strings Template strings, or a callback to be used for the output format.
  536. * @return The resulting format string set.
  537. */
  538. public static function addFormat($format, array $strings) {
  539. $self = Debugger::getInstance();
  540. if (isset($self->_templates[$format])) {
  541. if (isset($strings['links'])) {
  542. $self->_templates[$format]['links'] = array_merge(
  543. $self->_templates[$format]['links'],
  544. $strings['links']
  545. );
  546. unset($strings['links']);
  547. }
  548. $self->_templates[$format] = array_merge($self->_templates[$format], $strings);
  549. } else {
  550. $self->_templates[$format] = $strings;
  551. }
  552. return $self->_templates[$format];
  553. }
  554. /**
  555. * Switches output format, updates format strings.
  556. * Can be used to switch the active output format:
  557. *
  558. * @param string $format Format to use, including 'js' for JavaScript-enhanced HTML, 'html' for
  559. * straight HTML output, or 'txt' for unformatted text.
  560. * @param array $strings Template strings to be used for the output format.
  561. * @return string
  562. * @deprecated Use Debugger::outputAs() and Debugger::addFormat(). Will be removed
  563. * in 3.0
  564. */
  565. public function output($format = null, $strings = array()) {
  566. $_this = Debugger::getInstance();
  567. $data = null;
  568. if (is_null($format)) {
  569. return Debugger::outputAs();
  570. }
  571. if (!empty($strings)) {
  572. return Debugger::addFormat($format, $strings);
  573. }
  574. if ($format === true && !empty($_this->_data)) {
  575. $data = $_this->_data;
  576. $_this->_data = array();
  577. $format = false;
  578. }
  579. Debugger::outputAs($format);
  580. return $data;
  581. }
  582. /**
  583. * Takes a processed array of data from an error and displays it in the chosen format.
  584. *
  585. * @param string $data
  586. * @return void
  587. */
  588. public function outputError($data) {
  589. $defaults = array(
  590. 'level' => 0,
  591. 'error' => 0,
  592. 'code' => 0,
  593. 'description' => '',
  594. 'file' => '',
  595. 'line' => 0,
  596. 'context' => array(),
  597. 'start' => 2,
  598. );
  599. $data += $defaults;
  600. $files = $this->trace(array('start' => $data['start'], 'format' => 'points'));
  601. $code = $this->excerpt($files[0]['file'], $files[0]['line'] - 1, 1);
  602. $trace = $this->trace(array('start' => $data['start'], 'depth' => '20'));
  603. $insertOpts = array('before' => '{:', 'after' => '}');
  604. $context = array();
  605. $links = array();
  606. $info = '';
  607. foreach ((array)$data['context'] as $var => $value) {
  608. $context[] = "\${$var}\t=\t" . $this->exportVar($value, 1);
  609. }
  610. switch ($this->_outputFormat) {
  611. case false:
  612. $this->_data[] = compact('context', 'trace') + $data;
  613. return;
  614. case 'log':
  615. $this->log(compact('context', 'trace') + $data);
  616. return;
  617. }
  618. $data['trace'] = $trace;
  619. $data['id'] = 'cakeErr' . uniqid();
  620. $tpl = array_merge($this->_templates['base'], $this->_templates[$this->_outputFormat]);
  621. $insert = array('context' => join("\n", $context)) + $data;
  622. $detect = array('context');
  623. if (isset($tpl['links'])) {
  624. foreach ($tpl['links'] as $key => $val) {
  625. if (in_array($key, $detect) && empty($insert[$key])) {
  626. continue;
  627. }
  628. $links[$key] = String::insert($val, $insert, $insertOpts);
  629. }
  630. }
  631. foreach (array('code', 'context', 'trace') as $key) {
  632. if (empty($$key) || !isset($tpl[$key])) {
  633. continue;
  634. }
  635. if (is_array($$key)) {
  636. $$key = join("\n", $$key);
  637. }
  638. $info .= String::insert($tpl[$key], compact($key) + $insert, $insertOpts);
  639. }
  640. $links = join(' ', $links);
  641. unset($data['context']);
  642. if (isset($tpl['callback']) && is_callable($tpl['callback'])) {
  643. return call_user_func($tpl['callback'], $data, compact('links', 'info'));
  644. }
  645. echo String::insert($tpl['error'], compact('links', 'info') + $data, $insertOpts);
  646. }
  647. /**
  648. * Verifies that the application's salt and cipher seed value has been changed from the default value.
  649. *
  650. * @return void
  651. */
  652. public static function checkSecurityKeys() {
  653. if (Configure::read('Security.salt') == 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi') {
  654. trigger_error(__d('cake_dev', 'Please change the value of \'Security.salt\' in app/Config/core.php to a salt value specific to your application'), E_USER_NOTICE);
  655. }
  656. if (Configure::read('Security.cipherSeed') === '76859309657453542496749683645') {
  657. trigger_error(__d('cake_dev', 'Please change the value of \'Security.cipherSeed\' in app/Config/core.php to a numeric (digits only) seed value specific to your application'), E_USER_NOTICE);
  658. }
  659. }
  660. }