Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
39de6fd5 | 5 | | CiviCRM version 4.6 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
06b69b18 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * Start of the Error framework. We should check out and inherit from | |
31 | * PEAR_ErrorStack and use that framework | |
32 | * | |
33 | * @package CRM | |
06b69b18 | 34 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
35 | * $Id$ |
36 | * | |
37 | */ | |
38 | ||
39 | require_once 'PEAR/ErrorStack.php'; | |
40 | require_once 'PEAR/Exception.php'; | |
dcc4f6a7 | 41 | require_once 'CRM/Core/Exception.php'; |
6a488035 TO |
42 | |
43 | require_once 'Log.php'; | |
28518c90 EM |
44 | |
45 | /** | |
46 | * Class CRM_Exception | |
47 | */ | |
6a488035 | 48 | class CRM_Exception extends PEAR_Exception { |
b5c2afd0 | 49 | /** |
4f1f1f2a | 50 | * Redefine the exception so message isn't optional |
b5c2afd0 EM |
51 | * Supported signatures: |
52 | * - PEAR_Exception(string $message); | |
53 | * - PEAR_Exception(string $message, int $code); | |
54 | * - PEAR_Exception(string $message, Exception $cause); | |
55 | * - PEAR_Exception(string $message, Exception $cause, int $code); | |
56 | * - PEAR_Exception(string $message, PEAR_Error $cause); | |
57 | * - PEAR_Exception(string $message, PEAR_Error $cause, int $code); | |
58 | * - PEAR_Exception(string $message, array $causes); | |
59 | * - PEAR_Exception(string $message, array $causes, int $code); | |
da3c7979 | 60 | * |
b5c2afd0 | 61 | * @param string exception message |
da3c7979 EM |
62 | * @param int $code |
63 | * @param Exception $previous | |
b5c2afd0 | 64 | */ |
353ffa53 | 65 | public function __construct($message = NULL, $code = 0, Exception $previous = NULL) { |
6a488035 TO |
66 | parent::__construct($message, $code, $previous); |
67 | } | |
68 | } | |
69 | ||
a0ee3941 EM |
70 | /** |
71 | * Class CRM_Core_Error | |
72 | */ | |
6a488035 TO |
73 | class CRM_Core_Error extends PEAR_ErrorStack { |
74 | ||
75 | /** | |
100fef9d | 76 | * Status code of various types of errors |
6a488035 | 77 | */ |
7da04cde TO |
78 | const FATAL_ERROR = 2; |
79 | const DUPLICATE_CONTACT = 8001; | |
80 | const DUPLICATE_CONTRIBUTION = 8002; | |
81 | const DUPLICATE_PARTICIPANT = 8003; | |
6a488035 TO |
82 | |
83 | /** | |
84 | * We only need one instance of this object. So we use the singleton | |
85 | * pattern and cache the instance in this variable | |
86 | * @var object | |
87 | * @static | |
88 | */ | |
89 | private static $_singleton = NULL; | |
90 | ||
91 | /** | |
92 | * The logger object for this application | |
93 | * @var object | |
94 | * @static | |
95 | */ | |
96 | private static $_log = NULL; | |
97 | ||
98 | /** | |
99 | * If modeException == true, errors are raised as exception instead of returning civicrm_errors | |
100 | * @static | |
101 | */ | |
102 | public static $modeException = NULL; | |
103 | ||
104 | /** | |
100fef9d | 105 | * Singleton function used to manage this object. |
6a488035 | 106 | * |
dd244018 EM |
107 | * @param null $package |
108 | * @param bool $msgCallback | |
109 | * @param bool $contextCallback | |
110 | * @param bool $throwPEAR_Error | |
111 | * @param string $stackClass | |
112 | * | |
6a488035 TO |
113 | * @return object |
114 | * @static | |
115 | */ | |
2aa397bc | 116 | public static function &singleton($package = NULL, $msgCallback = FALSE, $contextCallback = FALSE, $throwPEAR_Error = FALSE, $stackClass = 'PEAR_ErrorStack') { |
6a488035 TO |
117 | if (self::$_singleton === NULL) { |
118 | self::$_singleton = new CRM_Core_Error('CiviCRM'); | |
119 | } | |
120 | return self::$_singleton; | |
121 | } | |
122 | ||
123 | /** | |
100fef9d | 124 | * Constructor |
6a488035 | 125 | */ |
00be9182 | 126 | public function __construct() { |
6a488035 TO |
127 | parent::__construct('CiviCRM'); |
128 | ||
129 | $log = CRM_Core_Config::getLog(); | |
130 | $this->setLogger($log); | |
131 | ||
132 | // set up error handling for Pear Error Stack | |
133 | $this->setDefaultCallback(array($this, 'handlePES')); | |
134 | } | |
135 | ||
a0ee3941 EM |
136 | /** |
137 | * @param $error | |
138 | * @param string $separator | |
139 | * | |
140 | * @return array|null|string | |
141 | */ | |
93a11cd6 | 142 | static public function getMessages(&$error, $separator = '<br />') { |
6a488035 TO |
143 | if (is_a($error, 'CRM_Core_Error')) { |
144 | $errors = $error->getErrors(); | |
145 | $message = array(); | |
146 | foreach ($errors as $e) { | |
147 | $message[] = $e['code'] . ': ' . $e['message']; | |
148 | } | |
149 | $message = implode($separator, $message); | |
150 | return $message; | |
151 | } | |
152 | return NULL; | |
153 | } | |
154 | ||
dbddfb08 EM |
155 | /** |
156 | * Status display function specific to payment processor errors | |
157 | * @param $error | |
158 | * @param string $separator | |
159 | */ | |
00be9182 | 160 | public static function displaySessionError(&$error, $separator = '<br />') { |
6a488035 TO |
161 | $message = self::getMessages($error, $separator); |
162 | if ($message) { | |
163 | $status = ts("Payment Processor Error message") . "{$separator} $message"; | |
164 | $session = CRM_Core_Session::singleton(); | |
165 | $session->setStatus($status); | |
166 | } | |
167 | } | |
168 | ||
169 | /** | |
100fef9d | 170 | * Create the main callback method. this method centralizes error processing. |
6a488035 TO |
171 | * |
172 | * the errors we expect are from the pear modules DB, DB_DataObject | |
173 | * which currently use PEAR::raiseError to notify of error messages. | |
174 | * | |
175 | * @param object PEAR_Error | |
176 | * | |
177 | * @return void | |
6a488035 TO |
178 | */ |
179 | public static function handle($pearError) { | |
180 | ||
181 | // setup smarty with config, session and template location. | |
182 | $template = CRM_Core_Smarty::singleton(); | |
183 | $config = CRM_Core_Config::singleton(); | |
184 | ||
185 | if ($config->backtrace) { | |
186 | self::backtrace(); | |
187 | } | |
188 | ||
189 | // create the error array | |
353ffa53 TO |
190 | $error = array(); |
191 | $error['callback'] = $pearError->getCallback(); | |
192 | $error['code'] = $pearError->getCode(); | |
193 | $error['message'] = $pearError->getMessage(); | |
194 | $error['mode'] = $pearError->getMode(); | |
6a488035 | 195 | $error['debug_info'] = $pearError->getDebugInfo(); |
353ffa53 TO |
196 | $error['type'] = $pearError->getType(); |
197 | $error['user_info'] = $pearError->getUserInfo(); | |
198 | $error['to_string'] = $pearError->toString(); | |
6a488035 TO |
199 | if (function_exists('mysql_error') && |
200 | mysql_error() | |
201 | ) { | |
202 | $mysql_error = mysql_error() . ', ' . mysql_errno(); | |
203 | $template->assign_by_ref('mysql_code', $mysql_error); | |
204 | ||
205 | // execute a dummy query to clear error stack | |
206 | mysql_query('select 1'); | |
207 | } | |
208 | elseif (function_exists('mysqli_error')) { | |
209 | $dao = new CRM_Core_DAO(); | |
210 | ||
211 | // we do it this way, since calling the function | |
212 | // getDatabaseConnection could potentially result | |
213 | // in an infinite loop | |
214 | global $_DB_DATAOBJECT; | |
215 | if (isset($_DB_DATAOBJECT['CONNECTIONS'][$dao->_database_dsn_md5])) { | |
216 | $conn = $_DB_DATAOBJECT['CONNECTIONS'][$dao->_database_dsn_md5]; | |
217 | $link = $conn->connection; | |
218 | ||
219 | if (mysqli_error($link)) { | |
220 | $mysql_error = mysqli_error($link) . ', ' . mysqli_errno($link); | |
221 | $template->assign_by_ref('mysql_code', $mysql_error); | |
222 | ||
223 | // execute a dummy query to clear error stack | |
224 | mysqli_query($link, 'select 1'); | |
225 | } | |
226 | } | |
227 | } | |
228 | ||
229 | $template->assign_by_ref('error', $error); | |
230 | $errorDetails = CRM_Core_Error::debug('', $error, FALSE); | |
231 | $template->assign_by_ref('errorDetails', $errorDetails); | |
232 | ||
233 | CRM_Core_Error::debug_var('Fatal Error Details', $error); | |
234 | CRM_Core_Error::backtrace('backTrace', TRUE); | |
235 | ||
236 | if ($config->initialized) { | |
237 | $content = $template->fetch('CRM/common/fatal.tpl'); | |
238 | echo CRM_Utils_System::theme($content); | |
239 | } | |
240 | else { | |
241 | echo "Sorry. A non-recoverable error has occurred. The error trace below might help to resolve the issue<p>"; | |
242 | CRM_Core_Error::debug(NULL, $error); | |
243 | } | |
3a210ec0 E |
244 | static $runOnce = FALSE; |
245 | if ($runOnce) { | |
246 | exit; | |
247 | } | |
248 | $runOnce = TRUE; | |
6a488035 TO |
249 | self::abend(1); |
250 | } | |
251 | ||
a0ee3941 | 252 | /** |
4f1f1f2a CW |
253 | * this function is used to trap and print errors |
254 | * during system initialization time. Hence the error | |
255 | * message is quite ugly | |
256 | * | |
a0ee3941 EM |
257 | * @param $pearError |
258 | */ | |
6a488035 TO |
259 | public static function simpleHandler($pearError) { |
260 | ||
261 | // create the error array | |
353ffa53 TO |
262 | $error = array(); |
263 | $error['callback'] = $pearError->getCallback(); | |
264 | $error['code'] = $pearError->getCode(); | |
265 | $error['message'] = $pearError->getMessage(); | |
266 | $error['mode'] = $pearError->getMode(); | |
6a488035 | 267 | $error['debug_info'] = $pearError->getDebugInfo(); |
353ffa53 TO |
268 | $error['type'] = $pearError->getType(); |
269 | $error['user_info'] = $pearError->getUserInfo(); | |
270 | $error['to_string'] = $pearError->toString(); | |
6a488035 | 271 | |
005db542 DL |
272 | // ensure that debug does not check permissions since we are in bootstrap |
273 | // mode and need to print a decent message to help the user | |
90154ece | 274 | CRM_Core_Error::debug('Initialization Error', $error, TRUE, TRUE, FALSE); |
6a488035 TO |
275 | |
276 | // always log the backtrace to a file | |
277 | self::backtrace('backTrace', TRUE); | |
278 | ||
279 | exit(0); | |
280 | } | |
281 | ||
282 | /** | |
283 | * Handle errors raised using the PEAR Error Stack. | |
284 | * | |
285 | * currently the handler just requests the PES framework | |
286 | * to push the error to the stack (return value PEAR_ERRORSTACK_PUSH). | |
287 | * | |
288 | * Note: we can do our own error handling here and return PEAR_ERRORSTACK_IGNORE. | |
289 | * | |
290 | * Also, if we do not return any value the PEAR_ErrorStack::push() then does the | |
291 | * action of PEAR_ERRORSTACK_PUSHANDLOG which displays the errors on the screen, | |
292 | * since the logger set for this error stack is 'display' - see CRM_Core_Config::getLog(); | |
6a488035 TO |
293 | */ |
294 | public static function handlePES($pearError) { | |
295 | return PEAR_ERRORSTACK_PUSH; | |
296 | } | |
297 | ||
298 | /** | |
100fef9d | 299 | * Display an error page with an error message describing what happened |
6a488035 | 300 | * |
6a0b768e TO |
301 | * @param string $message |
302 | * The error message. | |
303 | * @param string $code | |
304 | * The error code if any. | |
305 | * @param string $email | |
306 | * The email address to notify of this situation. | |
77b97be7 EM |
307 | * |
308 | * @throws Exception | |
6a488035 TO |
309 | * |
310 | * @return void | |
311 | * @static | |
6a488035 | 312 | */ |
00be9182 | 313 | public static function fatal($message = NULL, $code = NULL, $email = NULL) { |
6a488035 TO |
314 | $vars = array( |
315 | 'message' => $message, | |
316 | 'code' => $code, | |
317 | ); | |
318 | ||
319 | if (self::$modeException) { | |
320 | // CRM-11043 | |
321 | CRM_Core_Error::debug_var('Fatal Error Details', $vars); | |
322 | CRM_Core_Error::backtrace('backTrace', TRUE); | |
323 | ||
324 | $details = 'A fatal error was triggered'; | |
325 | if ($message) { | |
326 | $details .= ': ' . $message; | |
2aa397bc | 327 | } |
6a488035 TO |
328 | throw new Exception($details, $code); |
329 | } | |
330 | ||
331 | if (!$message) { | |
332 | $message = ts('We experienced an unexpected error. Please post a detailed description and the backtrace on the CiviCRM forums: %1', array(1 => 'http://forum.civicrm.org/')); | |
333 | } | |
334 | ||
335 | if (php_sapi_name() == "cli") { | |
336 | print ("Sorry. A non-recoverable error has occurred.\n$message \n$code\n$email\n\n"); | |
337 | debug_print_backtrace(); | |
338 | die("\n"); | |
339 | // FIXME: Why doesn't this call abend()? | |
340 | // Difference: abend() will cleanup transaction and (via civiExit) store session state | |
341 | // self::abend(CRM_Core_Error::FATAL_ERROR); | |
342 | } | |
343 | ||
344 | $config = CRM_Core_Config::singleton(); | |
345 | ||
346 | if ($config->fatalErrorHandler && | |
347 | function_exists($config->fatalErrorHandler) | |
348 | ) { | |
349 | $name = $config->fatalErrorHandler; | |
350 | $ret = $name($vars); | |
351 | if ($ret) { | |
352 | // the call has been successfully handled | |
353 | // so we just exit | |
354 | self::abend(CRM_Core_Error::FATAL_ERROR); | |
355 | } | |
356 | } | |
357 | ||
25dc2153 PH |
358 | if ($config->backtrace) { |
359 | self::backtrace(); | |
360 | } | |
361 | ||
362 | CRM_Core_Error::debug_var('Fatal Error Details', $vars); | |
363 | CRM_Core_Error::backtrace('backTrace', TRUE); | |
364 | ||
34866662 CW |
365 | // If we are in an ajax callback, format output appropriately |
366 | if (CRM_Utils_Array::value('snippet', $_REQUEST) === CRM_Core_Smarty::PRINT_JSON) { | |
367 | $out = array( | |
368 | 'status' => 'fatal', | |
369 | 'content' => '<div class="messages status no-popup"><div class="icon inform-icon"></div>' . ts('Sorry but we are not able to provide this at the moment.') . '</div>', | |
370 | ); | |
371 | if ($config->backtrace && CRM_Core_Permission::check('view debug output')) { | |
372 | $out['backtrace'] = self::parseBacktrace(debug_backtrace()); | |
373 | $message .= '<p><em>See console for backtrace</em></p>'; | |
374 | } | |
375 | CRM_Core_Session::setStatus($message, ts('Sorry an Error Occured'), 'error'); | |
376 | CRM_Core_Transaction::forceRollbackIfEnabled(); | |
377 | CRM_Core_Page_AJAX::returnJsonResponse($out); | |
378 | } | |
379 | ||
6a488035 TO |
380 | $template = CRM_Core_Smarty::singleton(); |
381 | $template->assign($vars); | |
382 | ||
f85b1d20 | 383 | $config->userSystem->outputError($template->fetch($config->fatalErrorTemplate)); |
6a488035 TO |
384 | |
385 | self::abend(CRM_Core_Error::FATAL_ERROR); | |
386 | } | |
387 | ||
388 | /** | |
100fef9d | 389 | * Display an error page with an error message describing what happened |
6a488035 TO |
390 | * |
391 | * This function is evil -- it largely replicates fatal(). Hopefully the | |
392 | * entire CRM_Core_Error system can be hollowed out and replaced with | |
393 | * something that follows a cleaner separation of concerns. | |
394 | * | |
395 | * @param Exception $exception | |
396 | * | |
397 | * @return void | |
398 | * @static | |
6a488035 | 399 | */ |
00be9182 | 400 | public static function handleUnhandledException($exception) { |
4b57bc9f EM |
401 | try { |
402 | CRM_Utils_Hook::unhandledException($exception); | |
0db6c3e1 TO |
403 | } |
404 | catch (Exception $other) { | |
4b57bc9f | 405 | // if the exception-handler generates an exception, then that sucks! oh, well. carry on. |
4fd5f07e | 406 | CRM_Core_Error::debug_var('handleUnhandledException_nestedException', self::formatTextException($other)); |
4b57bc9f | 407 | } |
6a488035 TO |
408 | $config = CRM_Core_Config::singleton(); |
409 | $vars = array( | |
410 | 'message' => $exception->getMessage(), | |
411 | 'code' => NULL, | |
412 | 'exception' => $exception, | |
413 | ); | |
414 | if (!$vars['message']) { | |
415 | $vars['message'] = ts('We experienced an unexpected error. Please post a detailed description and the backtrace on the CiviCRM forums: %1', array(1 => 'http://forum.civicrm.org/')); | |
416 | } | |
417 | ||
418 | // Case A: CLI | |
419 | if (php_sapi_name() == "cli") { | |
420 | printf("Sorry. A non-recoverable error has occurred.\n%s\n", $vars['message']); | |
421 | print self::formatTextException($exception); | |
422 | die("\n"); | |
423 | // FIXME: Why doesn't this call abend()? | |
424 | // Difference: abend() will cleanup transaction and (via civiExit) store session state | |
425 | // self::abend(CRM_Core_Error::FATAL_ERROR); | |
426 | } | |
427 | ||
428 | // Case B: Custom error handler | |
429 | if ($config->fatalErrorHandler && | |
430 | function_exists($config->fatalErrorHandler) | |
431 | ) { | |
432 | $name = $config->fatalErrorHandler; | |
433 | $ret = $name($vars); | |
434 | if ($ret) { | |
435 | // the call has been successfully handled | |
436 | // so we just exit | |
437 | self::abend(CRM_Core_Error::FATAL_ERROR); | |
438 | } | |
439 | } | |
440 | ||
441 | // Case C: Default error handler | |
442 | ||
443 | // log to file | |
444 | CRM_Core_Error::debug_var('Fatal Error Details', $vars); | |
445 | CRM_Core_Error::backtrace('backTrace', TRUE); | |
446 | ||
447 | // print to screen | |
448 | $template = CRM_Core_Smarty::singleton(); | |
449 | $template->assign($vars); | |
450 | $content = $template->fetch($config->fatalErrorTemplate); | |
451 | if ($config->backtrace) { | |
452 | $content = self::formatHtmlException($exception) . $content; | |
453 | } | |
454 | if ($config->userFramework == 'Joomla' && | |
455 | class_exists('JError') | |
456 | ) { | |
457 | JError::raiseError('CiviCRM-001', $content); | |
458 | } | |
459 | else { | |
460 | echo CRM_Utils_System::theme($content); | |
461 | } | |
462 | ||
463 | // fin | |
464 | self::abend(CRM_Core_Error::FATAL_ERROR); | |
465 | } | |
466 | ||
467 | /** | |
100fef9d | 468 | * Outputs pre-formatted debug information. Flushes the buffers |
6a488035 TO |
469 | * so we can interrupt a potential POST/redirect |
470 | * | |
6a0b768e TO |
471 | * @param string name of debug section |
472 | * @param mixed reference to variables that we need a trace of | |
473 | * @param bool should we log or return the output | |
474 | * @param bool whether to generate a HTML-escaped output | |
475 | * @param bool should we check permissions before displaying output | |
90154ece DL |
476 | * useful when we die during initialization and permissioning |
477 | * subsystem is not initialized - CRM-13765 | |
6a488035 | 478 | * |
a6c01b45 CW |
479 | * @return string |
480 | * the generated output | |
6a488035 TO |
481 | * @static |
482 | */ | |
00be9182 | 483 | public static function debug($name, $variable = NULL, $log = TRUE, $html = TRUE, $checkPermission = TRUE) { |
6a488035 TO |
484 | $error = self::singleton(); |
485 | ||
486 | if ($variable === NULL) { | |
487 | $variable = $name; | |
488 | $name = NULL; | |
489 | } | |
490 | ||
491 | $out = print_r($variable, TRUE); | |
492 | $prefix = NULL; | |
493 | if ($html) { | |
494 | $out = htmlspecialchars($out); | |
495 | if ($name) { | |
496 | $prefix = "<p>$name</p>"; | |
497 | } | |
498 | $out = "{$prefix}<p><pre>$out</pre></p><p></p>"; | |
499 | } | |
500 | else { | |
501 | if ($name) { | |
502 | $prefix = "$name:\n"; | |
503 | } | |
504 | $out = "{$prefix}$out\n"; | |
505 | } | |
90154ece DL |
506 | if ( |
507 | $log && | |
508 | (!$checkPermission || CRM_Core_Permission::check('view debug output')) | |
509 | ) { | |
6a488035 TO |
510 | echo $out; |
511 | } | |
512 | ||
513 | return $out; | |
514 | } | |
515 | ||
516 | /** | |
517 | * Similar to the function debug. Only difference is | |
518 | * in the formatting of the output. | |
519 | * | |
c490a46a CW |
520 | * @param string $variable_name |
521 | * @param mixed $variable | |
6a0b768e TO |
522 | * @param bool $print |
523 | * Should we use print_r ? (else we use var_dump). | |
524 | * @param bool $log | |
525 | * Should we log or return the output. | |
526 | * @param string $comp | |
527 | * Variable name. | |
2a6da8d7 | 528 | * |
a6c01b45 CW |
529 | * @return string |
530 | * the generated output | |
6a488035 | 531 | * |
6a488035 TO |
532 | * |
533 | * @static | |
534 | * | |
535 | * @see CRM_Core_Error::debug() | |
536 | * @see CRM_Core_Error::debug_log_message() | |
537 | */ | |
f9f40af3 TO |
538 | static function debug_var( |
539 | $variable_name, | |
6a488035 TO |
540 | $variable, |
541 | $print = TRUE, | |
2aa397bc TO |
542 | $log = TRUE, |
543 | $comp = '' | |
6a488035 TO |
544 | ) { |
545 | // check if variable is set | |
546 | if (!isset($variable)) { | |
547 | $out = "\$$variable_name is not set"; | |
548 | } | |
549 | else { | |
550 | if ($print) { | |
551 | $out = print_r($variable, TRUE); | |
552 | $out = "\$$variable_name = $out"; | |
553 | } | |
554 | else { | |
555 | // use var_dump | |
556 | ob_start(); | |
557 | var_dump($variable); | |
558 | $dump = ob_get_contents(); | |
559 | ob_end_clean(); | |
560 | $out = "\n\$$variable_name = $dump"; | |
561 | } | |
562 | // reset if it is an array | |
563 | if (is_array($variable)) { | |
564 | reset($variable); | |
565 | } | |
566 | } | |
567 | return self::debug_log_message($out, FALSE, $comp); | |
568 | } | |
569 | ||
570 | /** | |
100fef9d | 571 | * Display the error message on terminal |
6a488035 | 572 | * |
2a6da8d7 | 573 | * @param $message |
6a0b768e TO |
574 | * @param bool $out |
575 | * Should we log or return the output. | |
6a488035 | 576 | * |
6a0b768e TO |
577 | * @param string $comp |
578 | * Message to be output. | |
a6c01b45 CW |
579 | * @return string |
580 | * format of the backtrace | |
6a488035 | 581 | * |
6a488035 TO |
582 | * |
583 | * @static | |
584 | */ | |
00be9182 | 585 | public static function debug_log_message($message, $out = FALSE, $comp = '') { |
213a5f19 | 586 | $config = CRM_Core_Config::singleton(); |
6a488035 TO |
587 | |
588 | $file_log = self::createDebugLogger($comp); | |
589 | $file_log->log("$message\n"); | |
590 | $str = "<p/><code>$message</code>"; | |
591 | if ($out && CRM_Core_Permission::check('view debug output')) { | |
592 | echo $str; | |
593 | } | |
594 | $file_log->close(); | |
595 | ||
213a5f19 | 596 | if ($config->userFrameworkLogging) { |
1fadd891 | 597 | // should call $config->userSystem->logger($message) here - but I got a situation where userSystem was not an object - not sure why |
213a5f19 EM |
598 | if ($config->userSystem->is_drupal and function_exists('watchdog')) { |
599 | watchdog('civicrm', $message, NULL, WATCHDOG_DEBUG); | |
600 | } | |
601 | } | |
6a488035 TO |
602 | |
603 | return $str; | |
604 | } | |
605 | ||
606 | /** | |
607 | * Append to the query log (if enabled) | |
608 | */ | |
00be9182 | 609 | public static function debug_query($string) { |
481a74f4 TO |
610 | if (defined('CIVICRM_DEBUG_LOG_QUERY')) { |
611 | if (CIVICRM_DEBUG_LOG_QUERY == 'backtrace') { | |
612 | CRM_Core_Error::backtrace($string, TRUE); | |
0db6c3e1 | 613 | } |
481a74f4 TO |
614 | elseif (CIVICRM_DEBUG_LOG_QUERY) { |
615 | CRM_Core_Error::debug_var('Query', $string, FALSE, TRUE); | |
6a488035 TO |
616 | } |
617 | } | |
618 | } | |
619 | ||
d090b80b TO |
620 | /** |
621 | * Execute a query and log the results. | |
622 | * | |
623 | * @param string $query | |
624 | */ | |
00be9182 | 625 | public static function debug_query_result($query) { |
d090b80b TO |
626 | $dao = CRM_Core_DAO::executeQuery($query); |
627 | $results = array(); | |
628 | while ($dao->fetch()) { | |
2aa397bc | 629 | $results[] = (array) $dao; |
d090b80b TO |
630 | } |
631 | CRM_Core_Error::debug_var('dao result', array('query' => $query, 'results' => $results)); | |
632 | } | |
633 | ||
6a488035 TO |
634 | /** |
635 | * Obtain a reference to the error log | |
636 | * | |
77b97be7 EM |
637 | * @param string $comp |
638 | * | |
6a488035 TO |
639 | * @return Log |
640 | */ | |
00be9182 | 641 | public static function createDebugLogger($comp = '') { |
6a488035 TO |
642 | $config = CRM_Core_Config::singleton(); |
643 | ||
644 | if ($comp) { | |
645 | $comp = $comp . '.'; | |
646 | } | |
647 | ||
648 | $fileName = "{$config->configAndLogDir}CiviCRM." . $comp . md5($config->dsn) . '.log'; | |
649 | ||
650 | // Roll log file monthly or if greater than 256M | |
651 | // note that PHP file functions have a limit of 2G and hence | |
652 | // the alternative was introduce | |
653 | if (file_exists($fileName)) { | |
654 | $fileTime = date("Ym", filemtime($fileName)); | |
655 | $fileSize = filesize($fileName); | |
656 | if (($fileTime < date('Ym')) || | |
657 | ($fileSize > 256 * 1024 * 1024) || | |
658 | ($fileSize < 0) | |
659 | ) { | |
660 | rename($fileName, | |
661 | $fileName . '.' . date('Ymdhs', mktime(0, 0, 0, date("m") - 1, date("d"), date("Y"))) | |
662 | ); | |
663 | } | |
664 | } | |
665 | ||
666 | return Log::singleton('file', $fileName); | |
667 | } | |
668 | ||
a0ee3941 EM |
669 | /** |
670 | * @param string $msg | |
671 | * @param bool $log | |
672 | */ | |
00be9182 | 673 | public static function backtrace($msg = 'backTrace', $log = FALSE) { |
6a488035 TO |
674 | $backTrace = debug_backtrace(); |
675 | $message = self::formatBacktrace($backTrace); | |
676 | if (!$log) { | |
677 | CRM_Core_Error::debug($msg, $message); | |
678 | } | |
679 | else { | |
680 | CRM_Core_Error::debug_var($msg, $message); | |
681 | } | |
682 | } | |
683 | ||
684 | /** | |
685 | * Render a backtrace array as a string | |
686 | * | |
6a0b768e TO |
687 | * @param array $backTrace |
688 | * Array of stack frames. | |
689 | * @param bool $showArgs | |
690 | * TRUE if we should try to display content of function arguments (which could be sensitive); FALSE to display only the type of each function argument. | |
691 | * @param int $maxArgLen | |
692 | * Maximum number of characters to show from each argument string. | |
a6c01b45 CW |
693 | * @return string |
694 | * printable plain-text | |
6a488035 | 695 | */ |
00be9182 | 696 | public static function formatBacktrace($backTrace, $showArgs = TRUE, $maxArgLen = 80) { |
6a488035 | 697 | $message = ''; |
34866662 CW |
698 | foreach (self::parseBacktrace($backTrace, $showArgs, $maxArgLen) as $idx => $trace) { |
699 | $message .= sprintf("#%s %s\n", $idx, $trace); | |
700 | } | |
2aa397bc | 701 | $message .= sprintf("#%s {main}\n", 1 + $idx); |
34866662 CW |
702 | return $message; |
703 | } | |
704 | ||
705 | /** | |
706 | * Render a backtrace array as an array | |
707 | * | |
6a0b768e TO |
708 | * @param array $backTrace |
709 | * Array of stack frames. | |
710 | * @param bool $showArgs | |
711 | * TRUE if we should try to display content of function arguments (which could be sensitive); FALSE to display only the type of each function argument. | |
712 | * @param int $maxArgLen | |
713 | * Maximum number of characters to show from each argument string. | |
34866662 CW |
714 | * @return array |
715 | * @see debug_backtrace | |
716 | * @see Exception::getTrace() | |
717 | */ | |
00be9182 | 718 | public static function parseBacktrace($backTrace, $showArgs = TRUE, $maxArgLen = 80) { |
34866662 CW |
719 | $ret = array(); |
720 | foreach ($backTrace as $trace) { | |
6a488035 TO |
721 | $args = array(); |
722 | $fnName = CRM_Utils_Array::value('function', $trace); | |
34866662 | 723 | $className = isset($trace['class']) ? ($trace['class'] . $trace['type']) : ''; |
6a488035 | 724 | |
83e7cba6 | 725 | // Do not show args for a few password related functions |
6a488035 TO |
726 | $skipArgs = ($className == 'DB::' && $fnName == 'connect') ? TRUE : FALSE; |
727 | ||
83e7cba6 CB |
728 | if (!empty($trace['args'])) { |
729 | foreach ($trace['args'] as $arg) { | |
353ffa53 | 730 | if (!$showArgs || $skipArgs) { |
83e7cba6 CB |
731 | $args[] = '(' . gettype($arg) . ')'; |
732 | continue; | |
733 | } | |
734 | switch ($type = gettype($arg)) { | |
735 | case 'boolean': | |
736 | $args[] = $arg ? 'TRUE' : 'FALSE'; | |
737 | break; | |
2aa397bc | 738 | |
83e7cba6 CB |
739 | case 'integer': |
740 | case 'double': | |
741 | $args[] = $arg; | |
742 | break; | |
2aa397bc | 743 | |
83e7cba6 | 744 | case 'string': |
86bfa4f6 | 745 | $args[] = '"' . CRM_Utils_String::ellipsify(addcslashes((string) $arg, "\r\n\t\""), $maxArgLen) . '"'; |
83e7cba6 | 746 | break; |
2aa397bc | 747 | |
83e7cba6 | 748 | case 'array': |
92fcb95f | 749 | $args[] = '(Array:' . count($arg) . ')'; |
83e7cba6 | 750 | break; |
2aa397bc | 751 | |
83e7cba6 CB |
752 | case 'object': |
753 | $args[] = 'Object(' . get_class($arg) . ')'; | |
754 | break; | |
2aa397bc | 755 | |
83e7cba6 CB |
756 | case 'resource': |
757 | $args[] = 'Resource'; | |
758 | break; | |
2aa397bc | 759 | |
83e7cba6 CB |
760 | case 'NULL': |
761 | $args[] = 'NULL'; | |
762 | break; | |
2aa397bc | 763 | |
83e7cba6 CB |
764 | default: |
765 | $args[] = "($type)"; | |
766 | break; | |
767 | } | |
6a488035 TO |
768 | } |
769 | } | |
770 | ||
34866662 CW |
771 | $ret[] = sprintf( |
772 | "%s(%s): %s%s(%s)", | |
6a488035 TO |
773 | CRM_Utils_Array::value('file', $trace, '[internal function]'), |
774 | CRM_Utils_Array::value('line', $trace, ''), | |
775 | $className, | |
776 | $fnName, | |
777 | implode(", ", $args) | |
778 | ); | |
779 | } | |
34866662 | 780 | return $ret; |
6a488035 TO |
781 | } |
782 | ||
783 | /** | |
784 | * Render an exception as HTML string | |
785 | * | |
786 | * @param Exception $e | |
a6c01b45 CW |
787 | * @return string |
788 | * printable HTML text | |
6a488035 | 789 | */ |
00be9182 | 790 | public static function formatHtmlException(Exception $e) { |
6a488035 TO |
791 | $msg = ''; |
792 | ||
793 | // Exception metadata | |
794 | ||
795 | // Exception backtrace | |
796 | if ($e instanceof PEAR_Exception) { | |
797 | $ei = $e; | |
798 | while (is_callable(array($ei, 'getCause'))) { | |
799 | if ($ei->getCause() instanceof PEAR_Error) { | |
800 | $msg .= '<table class="crm-db-error">'; | |
801 | $msg .= sprintf('<thead><tr><th>%s</th><th>%s</th></tr></thead>', ts('Error Field'), ts('Error Value')); | |
802 | $msg .= '<tbody>'; | |
803 | foreach (array('Type', 'Code', 'Message', 'Mode', 'UserInfo', 'DebugInfo') as $f) { | |
804 | $msg .= sprintf('<tr><td>%s</td><td>%s</td></tr>', $f, call_user_func(array($ei->getCause(), "get$f"))); | |
2aa397bc | 805 | } |
6a488035 | 806 | $msg .= '</tbody></table>'; |
2aa397bc | 807 | } |
6a488035 | 808 | $ei = $ei->getCause(); |
2aa397bc | 809 | } |
6a488035 | 810 | $msg .= $e->toHtml(); |
0db6c3e1 TO |
811 | } |
812 | else { | |
6a488035 TO |
813 | $msg .= '<p><b>' . get_class($e) . ': "' . htmlentities($e->getMessage()) . '"</b></p>'; |
814 | $msg .= '<pre>' . htmlentities(self::formatBacktrace($e->getTrace())) . '</pre>'; | |
815 | } | |
816 | return $msg; | |
817 | } | |
818 | ||
819 | /** | |
820 | * Write details of an exception to the log | |
821 | * | |
822 | * @param Exception $e | |
a6c01b45 CW |
823 | * @return string |
824 | * printable plain text | |
6a488035 | 825 | */ |
00be9182 | 826 | public static function formatTextException(Exception $e) { |
6a488035 TO |
827 | $msg = get_class($e) . ": \"" . $e->getMessage() . "\"\n"; |
828 | ||
829 | $ei = $e; | |
830 | while (is_callable(array($ei, 'getCause'))) { | |
831 | if ($ei->getCause() instanceof PEAR_Error) { | |
832 | foreach (array('Type', 'Code', 'Message', 'Mode', 'UserInfo', 'DebugInfo') as $f) { | |
833 | $msg .= sprintf(" * ERROR %s: %s\n", strtoupper($f), call_user_func(array($ei->getCause(), "get$f"))); | |
834 | } | |
835 | } | |
836 | $ei = $ei->getCause(); | |
837 | } | |
838 | $msg .= self::formatBacktrace($e->getTrace()); | |
839 | return $msg; | |
840 | } | |
841 | ||
a0ee3941 EM |
842 | /** |
843 | * @param $message | |
844 | * @param int $code | |
845 | * @param string $level | |
100fef9d | 846 | * @param array $params |
a0ee3941 EM |
847 | * |
848 | * @return object | |
849 | */ | |
00be9182 | 850 | public static function createError($message, $code = 8000, $level = 'Fatal', $params = NULL) { |
6a488035 TO |
851 | $error = CRM_Core_Error::singleton(); |
852 | $error->push($code, $level, array($params), $message); | |
853 | return $error; | |
854 | } | |
855 | ||
856 | /** | |
857 | * Set a status message in the session, then bounce back to the referrer. | |
858 | * | |
6a0b768e TO |
859 | * @param string $status |
860 | * The status message to set. | |
6a488035 | 861 | * |
2a6da8d7 EM |
862 | * @param null $redirect |
863 | * @param string $title | |
6a488035 | 864 | * @return void |
6a488035 TO |
865 | * @static |
866 | */ | |
50ecd413 | 867 | public static function statusBounce($status, $redirect = NULL, $title = NULL) { |
6a488035 TO |
868 | $session = CRM_Core_Session::singleton(); |
869 | if (!$redirect) { | |
870 | $redirect = $session->readUserContext(); | |
871 | } | |
50ecd413 CW |
872 | if ($title === NULL) { |
873 | $title = ts('Error'); | |
874 | } | |
875 | $session->setStatus($status, $title, 'alert', array('expires' => 0)); | |
34866662 CW |
876 | if (CRM_Utils_Array::value('snippet', $_REQUEST) === CRM_Core_Smarty::PRINT_JSON) { |
877 | CRM_Core_Page_AJAX::returnJsonResponse(array('status' => 'error')); | |
878 | } | |
6a488035 TO |
879 | CRM_Utils_System::redirect($redirect); |
880 | } | |
881 | ||
882 | /** | |
100fef9d | 883 | * Reset the error stack |
6a488035 | 884 | * |
6a488035 TO |
885 | * @static |
886 | */ | |
887 | public static function reset() { | |
888 | $error = self::singleton(); | |
889 | $error->_errors = array(); | |
890 | $error->_errorsByLevel = array(); | |
891 | } | |
892 | ||
6a4257d4 | 893 | /** |
42c0daee TO |
894 | * PEAR error-handler which converts errors to exceptions |
895 | * | |
896 | * @param $pearError | |
897 | * @throws PEAR_Exception | |
6a4257d4 | 898 | */ |
6a488035 TO |
899 | public static function exceptionHandler($pearError) { |
900 | CRM_Core_Error::backtrace('backTrace', TRUE); | |
901 | throw new PEAR_Exception($pearError->getMessage(), $pearError); | |
902 | } | |
903 | ||
904 | /** | |
42c0daee | 905 | * PEAR error-handler to quietly catch otherwise fatal errors. Intended for use with smtp transport. |
6a488035 | 906 | * |
6a0b768e TO |
907 | * @param object $obj |
908 | * The PEAR_ERROR object. | |
a6c01b45 CW |
909 | * @return object |
910 | * $obj | |
6a488035 TO |
911 | * @static |
912 | */ | |
913 | public static function nullHandler($obj) { | |
914 | CRM_Core_Error::debug_log_message("Ignoring exception thrown by nullHandler: {$obj->code}, {$obj->message}"); | |
915 | CRM_Core_Error::backtrace('backTrace', TRUE); | |
916 | return $obj; | |
917 | } | |
918 | ||
6a488035 TO |
919 | /* |
920 | * @deprecated | |
921 | * This function is no longer used by v3 api. | |
922 | * @fixme Some core files call it but it should be re-thought & renamed or removed | |
923 | */ | |
a0ee3941 EM |
924 | /** |
925 | * @param $msg | |
926 | * @param null $data | |
927 | * | |
928 | * @return array | |
929 | * @throws Exception | |
930 | */ | |
6a488035 TO |
931 | public static function &createAPIError($msg, $data = NULL) { |
932 | if (self::$modeException) { | |
933 | throw new Exception($msg, $data); | |
934 | } | |
935 | ||
936 | $values = array(); | |
937 | ||
938 | $values['is_error'] = 1; | |
939 | $values['error_message'] = $msg; | |
940 | if (isset($data)) { | |
941 | $values = array_merge($values, $data); | |
942 | } | |
943 | return $values; | |
944 | } | |
945 | ||
a0ee3941 EM |
946 | /** |
947 | * @param $file | |
948 | */ | |
6a488035 TO |
949 | public static function movedSiteError($file) { |
950 | $url = CRM_Utils_System::url('civicrm/admin/setting/updateConfigBackend', | |
951 | 'reset=1', | |
952 | TRUE | |
953 | ); | |
954 | echo "We could not write $file. Have you moved your site directory or server?<p>"; | |
955 | echo "Please fix the setting by running the <a href=\"$url\">update config script</a>"; | |
956 | exit(); | |
957 | } | |
958 | ||
959 | /** | |
960 | * Terminate execution abnormally | |
961 | */ | |
962 | protected static function abend($code) { | |
963 | // do a hard rollback of any pending transactions | |
964 | // if we've come here, its because of some unexpected PEAR errors | |
965 | CRM_Core_Transaction::forceRollbackIfEnabled(); | |
966 | CRM_Utils_System::civiExit($code); | |
967 | } | |
968 | ||
a0ee3941 EM |
969 | /** |
970 | * @param $error | |
971 | * @param const $type | |
972 | * | |
973 | * @return bool | |
974 | */ | |
6a488035 | 975 | public static function isAPIError($error, $type = CRM_Core_Error::FATAL_ERROR) { |
8cc574cf | 976 | if (is_array($error) && !empty($error['is_error'])) { |
6a488035 TO |
977 | $code = $error['error_message']['code']; |
978 | if ($code == $type) { | |
979 | return TRUE; | |
980 | } | |
981 | } | |
982 | return FALSE; | |
983 | } | |
984 | } | |
985 | ||
986 | $e = new PEAR_ErrorStack('CRM'); | |
987 | $e->singleton('CRM', FALSE, NULL, 'CRM_Core_Error'); |