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