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