Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * Start of the Error framework. We should check out and inherit from | |
14 | * PEAR_ErrorStack and use that framework | |
15 | * | |
16 | * @package CRM | |
ca5cec67 | 17 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
18 | */ |
19 | ||
20 | require_once 'PEAR/ErrorStack.php'; | |
21 | require_once 'PEAR/Exception.php'; | |
dcc4f6a7 | 22 | require_once 'CRM/Core/Exception.php'; |
6a488035 | 23 | |
df8de0eb SL |
24 | require_once 'Log.php'; |
25 | ||
a0ee3941 EM |
26 | /** |
27 | * Class CRM_Core_Error | |
28 | */ | |
6a488035 TO |
29 | class CRM_Core_Error extends PEAR_ErrorStack { |
30 | ||
31 | /** | |
0880a9d0 | 32 | * Status code of various types of errors. |
6a488035 | 33 | */ |
7da04cde TO |
34 | const FATAL_ERROR = 2; |
35 | const DUPLICATE_CONTACT = 8001; | |
36 | const DUPLICATE_CONTRIBUTION = 8002; | |
37 | const DUPLICATE_PARTICIPANT = 8003; | |
6a488035 TO |
38 | |
39 | /** | |
40 | * We only need one instance of this object. So we use the singleton | |
41 | * pattern and cache the instance in this variable | |
42 | * @var object | |
6a488035 TO |
43 | */ |
44 | private static $_singleton = NULL; | |
45 | ||
46 | /** | |
d09edf64 | 47 | * The logger object for this application. |
6a488035 | 48 | * @var object |
6a488035 TO |
49 | */ |
50 | private static $_log = NULL; | |
51 | ||
52 | /** | |
53 | * If modeException == true, errors are raised as exception instead of returning civicrm_errors | |
518fa0ee | 54 | * @var bool |
6a488035 TO |
55 | */ |
56 | public static $modeException = NULL; | |
57 | ||
58 | /** | |
100fef9d | 59 | * Singleton function used to manage this object. |
6a488035 | 60 | * |
dd244018 EM |
61 | * @param null $package |
62 | * @param bool $msgCallback | |
63 | * @param bool $contextCallback | |
64 | * @param bool $throwPEAR_Error | |
65 | * @param string $stackClass | |
66 | * | |
30208fab | 67 | * @return CRM_Core_Error |
6a488035 | 68 | */ |
2aa397bc | 69 | public static function &singleton($package = NULL, $msgCallback = FALSE, $contextCallback = FALSE, $throwPEAR_Error = FALSE, $stackClass = 'PEAR_ErrorStack') { |
6a488035 TO |
70 | if (self::$_singleton === NULL) { |
71 | self::$_singleton = new CRM_Core_Error('CiviCRM'); | |
72 | } | |
73 | return self::$_singleton; | |
74 | } | |
75 | ||
76 | /** | |
0880a9d0 | 77 | * Constructor. |
6a488035 | 78 | */ |
00be9182 | 79 | public function __construct() { |
6a488035 TO |
80 | parent::__construct('CiviCRM'); |
81 | ||
82 | $log = CRM_Core_Config::getLog(); | |
83 | $this->setLogger($log); | |
84 | ||
a6272a48 | 85 | // PEAR<=1.9.0 does not declare "static" properly. |
be2fb01f CW |
86 | if (!is_callable(['PEAR', '__callStatic'])) { |
87 | $this->setDefaultCallback([$this, 'handlePES']); | |
a6272a48 TO |
88 | } |
89 | else { | |
be2fb01f | 90 | PEAR_ErrorStack::setDefaultCallback([$this, 'handlePES']); |
a6272a48 | 91 | } |
6a488035 TO |
92 | } |
93 | ||
a0ee3941 EM |
94 | /** |
95 | * @param $error | |
96 | * @param string $separator | |
97 | * | |
98 | * @return array|null|string | |
99 | */ | |
518fa0ee | 100 | public static function getMessages(&$error, $separator = '<br />') { |
6a488035 TO |
101 | if (is_a($error, 'CRM_Core_Error')) { |
102 | $errors = $error->getErrors(); | |
be2fb01f | 103 | $message = []; |
6a488035 TO |
104 | foreach ($errors as $e) { |
105 | $message[] = $e['code'] . ': ' . $e['message']; | |
106 | } | |
107 | $message = implode($separator, $message); | |
108 | return $message; | |
109 | } | |
31176a73 SP |
110 | elseif (is_a($error, 'Civi\Payment\Exception\PaymentProcessorException')) { |
111 | return $error->getMessage(); | |
112 | } | |
6a488035 TO |
113 | return NULL; |
114 | } | |
115 | ||
dbddfb08 | 116 | /** |
0880a9d0 | 117 | * Status display function specific to payment processor errors. |
dbddfb08 EM |
118 | * @param $error |
119 | * @param string $separator | |
120 | */ | |
00be9182 | 121 | public static function displaySessionError(&$error, $separator = '<br />') { |
6a488035 TO |
122 | $message = self::getMessages($error, $separator); |
123 | if ($message) { | |
124 | $status = ts("Payment Processor Error message") . "{$separator} $message"; | |
125 | $session = CRM_Core_Session::singleton(); | |
126 | $session->setStatus($status); | |
127 | } | |
128 | } | |
129 | ||
130 | /** | |
100fef9d | 131 | * Create the main callback method. this method centralizes error processing. |
6a488035 TO |
132 | * |
133 | * the errors we expect are from the pear modules DB, DB_DataObject | |
134 | * which currently use PEAR::raiseError to notify of error messages. | |
135 | * | |
3bdca100 | 136 | * @param object $pearError PEAR_Error |
6a488035 TO |
137 | */ |
138 | public static function handle($pearError) { | |
2b2c4099 TO |
139 | if (defined('CIVICRM_TEST')) { |
140 | return self::simpleHandler($pearError); | |
141 | } | |
6a488035 TO |
142 | |
143 | // setup smarty with config, session and template location. | |
144 | $template = CRM_Core_Smarty::singleton(); | |
145 | $config = CRM_Core_Config::singleton(); | |
146 | ||
147 | if ($config->backtrace) { | |
148 | self::backtrace(); | |
149 | } | |
150 | ||
151 | // create the error array | |
ca8416c8 | 152 | $error = self::getErrorDetails($pearError); |
2e4ade96 TO |
153 | |
154 | // We access connection info via _DB_DATAOBJECT instead | |
155 | // of, e.g., calling getDatabaseConnection(), so that we | |
156 | // can avoid infinite loops. | |
157 | global $_DB_DATAOBJECT; | |
158 | ||
0828e4ad | 159 | if (isset($_DB_DATAOBJECT['CONFIG']['database'])) { |
6a488035 | 160 | $dao = new CRM_Core_DAO(); |
6a488035 TO |
161 | if (isset($_DB_DATAOBJECT['CONNECTIONS'][$dao->_database_dsn_md5])) { |
162 | $conn = $_DB_DATAOBJECT['CONNECTIONS'][$dao->_database_dsn_md5]; | |
6a488035 | 163 | |
0828e4ad TO |
164 | // FIXME: Polymorphism for the win. |
165 | if ($conn instanceof DB_mysqli) { | |
166 | $link = $conn->connection; | |
167 | if (mysqli_error($link)) { | |
168 | $mysql_error = mysqli_error($link) . ', ' . mysqli_errno($link); | |
518fa0ee SL |
169 | // execute a dummy query to clear error stack |
170 | mysqli_query($link, 'select 1'); | |
0828e4ad TO |
171 | } |
172 | } | |
173 | elseif ($conn instanceof DB_mysql) { | |
174 | if (mysql_error()) { | |
175 | $mysql_error = mysql_error() . ', ' . mysql_errno(); | |
518fa0ee SL |
176 | // execute a dummy query to clear error stack |
177 | mysql_query('select 1'); | |
0828e4ad TO |
178 | } |
179 | } | |
180 | else { | |
181 | $mysql_error = 'fixme-unknown-db-cxn'; | |
6a488035 | 182 | } |
0828e4ad | 183 | $template->assign_by_ref('mysql_code', $mysql_error); |
6a488035 TO |
184 | } |
185 | } | |
186 | ||
67bb87ed MW |
187 | // Use the custom fatalErrorHandler if defined |
188 | if ($config->fatalErrorHandler && function_exists($config->fatalErrorHandler)) { | |
189 | $name = $config->fatalErrorHandler; | |
190 | $vars = [ | |
191 | 'pearError' => $pearError, | |
192 | ]; | |
193 | $ret = $name($vars); | |
194 | if ($ret) { | |
195 | // the call has been successfully handled so we just exit | |
196 | self::abend(CRM_Core_Error::FATAL_ERROR); | |
197 | } | |
198 | } | |
199 | ||
6a488035 TO |
200 | $template->assign_by_ref('error', $error); |
201 | $errorDetails = CRM_Core_Error::debug('', $error, FALSE); | |
202 | $template->assign_by_ref('errorDetails', $errorDetails); | |
203 | ||
120f8e64 | 204 | CRM_Core_Error::debug_var('Fatal Error Details', $error, TRUE, TRUE, '', PEAR_LOG_ERR); |
6a488035 TO |
205 | CRM_Core_Error::backtrace('backTrace', TRUE); |
206 | ||
207 | if ($config->initialized) { | |
208 | $content = $template->fetch('CRM/common/fatal.tpl'); | |
209 | echo CRM_Utils_System::theme($content); | |
210 | } | |
211 | else { | |
212 | echo "Sorry. A non-recoverable error has occurred. The error trace below might help to resolve the issue<p>"; | |
213 | CRM_Core_Error::debug(NULL, $error); | |
214 | } | |
3a210ec0 E |
215 | static $runOnce = FALSE; |
216 | if ($runOnce) { | |
217 | exit; | |
218 | } | |
219 | $runOnce = TRUE; | |
67bb87ed | 220 | self::abend(CRM_Core_Error::FATAL_ERROR); |
6a488035 TO |
221 | } |
222 | ||
a0ee3941 | 223 | /** |
4f1f1f2a CW |
224 | * this function is used to trap and print errors |
225 | * during system initialization time. Hence the error | |
226 | * message is quite ugly | |
227 | * | |
a0ee3941 EM |
228 | * @param $pearError |
229 | */ | |
6a488035 TO |
230 | public static function simpleHandler($pearError) { |
231 | ||
ca8416c8 | 232 | $error = self::getErrorDetails($pearError); |
233 | ||
234 | // ensure that debug does not check permissions since we are in bootstrap | |
235 | // mode and need to print a decent message to help the user | |
236 | CRM_Core_Error::debug('Initialization Error', $error, TRUE, TRUE, FALSE); | |
237 | ||
238 | // always log the backtrace to a file | |
239 | self::backtrace('backTrace', TRUE); | |
240 | ||
241 | exit(0); | |
242 | } | |
243 | ||
244 | /** | |
245 | * this function is used to return error details | |
246 | * | |
247 | * @param $pearError | |
248 | * | |
249 | * @return array $error | |
250 | */ | |
251 | public static function getErrorDetails($pearError) { | |
6a488035 | 252 | // create the error array |
be2fb01f | 253 | $error = []; |
353ffa53 TO |
254 | $error['callback'] = $pearError->getCallback(); |
255 | $error['code'] = $pearError->getCode(); | |
256 | $error['message'] = $pearError->getMessage(); | |
257 | $error['mode'] = $pearError->getMode(); | |
6a488035 | 258 | $error['debug_info'] = $pearError->getDebugInfo(); |
353ffa53 TO |
259 | $error['type'] = $pearError->getType(); |
260 | $error['user_info'] = $pearError->getUserInfo(); | |
261 | $error['to_string'] = $pearError->toString(); | |
6a488035 | 262 | |
ca8416c8 | 263 | return $error; |
6a488035 TO |
264 | } |
265 | ||
266 | /** | |
267 | * Handle errors raised using the PEAR Error Stack. | |
268 | * | |
269 | * currently the handler just requests the PES framework | |
270 | * to push the error to the stack (return value PEAR_ERRORSTACK_PUSH). | |
271 | * | |
272 | * Note: we can do our own error handling here and return PEAR_ERRORSTACK_IGNORE. | |
273 | * | |
274 | * Also, if we do not return any value the PEAR_ErrorStack::push() then does the | |
275 | * action of PEAR_ERRORSTACK_PUSHANDLOG which displays the errors on the screen, | |
276 | * since the logger set for this error stack is 'display' - see CRM_Core_Config::getLog(); | |
ea3ddccf | 277 | * |
278 | * @param mixed $pearError | |
279 | * | |
280 | * @return int | |
6a488035 TO |
281 | */ |
282 | public static function handlePES($pearError) { | |
283 | return PEAR_ERRORSTACK_PUSH; | |
284 | } | |
285 | ||
286 | /** | |
0880a9d0 | 287 | * Display an error page with an error message describing what happened. |
6a488035 | 288 | * |
c6b9b29d CB |
289 | * @deprecated |
290 | * This is a really annoying function. We ❤ exceptions. Be exceptional! | |
291 | * | |
292 | * @see CRM-20181 | |
293 | * | |
6a0b768e TO |
294 | * @param string $message |
295 | * The error message. | |
296 | * @param string $code | |
297 | * The error code if any. | |
298 | * @param string $email | |
299 | * The email address to notify of this situation. | |
77b97be7 EM |
300 | * |
301 | * @throws Exception | |
6a488035 | 302 | */ |
00be9182 | 303 | public static function fatal($message = NULL, $code = NULL, $email = NULL) { |
33968e94 | 304 | CRM_Core_Error::deprecatedFunctionWarning('throw new CRM_Core_Exception or use CRM_Core_Error::statusBounce', 'CRM_Core_Error::fatal'); |
be2fb01f | 305 | $vars = [ |
84509850 | 306 | 'message' => $message, |
6a488035 | 307 | 'code' => $code, |
be2fb01f | 308 | ]; |
6a488035 TO |
309 | |
310 | if (self::$modeException) { | |
311 | // CRM-11043 | |
120f8e64 | 312 | CRM_Core_Error::debug_var('Fatal Error Details', $vars, TRUE, TRUE, '', PEAR_LOG_ERR); |
6a488035 TO |
313 | CRM_Core_Error::backtrace('backTrace', TRUE); |
314 | ||
315 | $details = 'A fatal error was triggered'; | |
316 | if ($message) { | |
317 | $details .= ': ' . $message; | |
2aa397bc | 318 | } |
6a488035 TO |
319 | throw new Exception($details, $code); |
320 | } | |
321 | ||
322 | if (!$message) { | |
be2fb01f | 323 | $message = ts('We experienced an unexpected error. You may have found a bug. For more information on how to provide a bug report, please read: %1', [1 => 'https://civicrm.org/bug-reporting']); |
6a488035 TO |
324 | } |
325 | ||
326 | if (php_sapi_name() == "cli") { | |
327 | print ("Sorry. A non-recoverable error has occurred.\n$message \n$code\n$email\n\n"); | |
0c4a3d8b | 328 | // Fix for CRM-16899 |
4c68cf7b | 329 | echo static::formatBacktrace(debug_backtrace()); |
6a488035 TO |
330 | die("\n"); |
331 | // FIXME: Why doesn't this call abend()? | |
332 | // Difference: abend() will cleanup transaction and (via civiExit) store session state | |
333 | // self::abend(CRM_Core_Error::FATAL_ERROR); | |
334 | } | |
335 | ||
336 | $config = CRM_Core_Config::singleton(); | |
337 | ||
338 | if ($config->fatalErrorHandler && | |
339 | function_exists($config->fatalErrorHandler) | |
340 | ) { | |
341 | $name = $config->fatalErrorHandler; | |
342 | $ret = $name($vars); | |
343 | if ($ret) { | |
344 | // the call has been successfully handled | |
345 | // so we just exit | |
346 | self::abend(CRM_Core_Error::FATAL_ERROR); | |
347 | } | |
348 | } | |
349 | ||
25dc2153 PH |
350 | if ($config->backtrace) { |
351 | self::backtrace(); | |
352 | } | |
353 | ||
120f8e64 | 354 | CRM_Core_Error::debug_var('Fatal Error Details', $vars, TRUE, TRUE, '', PEAR_LOG_ERR); |
25dc2153 PH |
355 | CRM_Core_Error::backtrace('backTrace', TRUE); |
356 | ||
34866662 CW |
357 | // If we are in an ajax callback, format output appropriately |
358 | if (CRM_Utils_Array::value('snippet', $_REQUEST) === CRM_Core_Smarty::PRINT_JSON) { | |
be2fb01f | 359 | $out = [ |
34866662 | 360 | 'status' => 'fatal', |
34d6cec4 | 361 | 'content' => '<div class="messages status no-popup">' . CRM_Core_Page::crmIcon('fa-info-circle') . ' ' . ts('Sorry but we are not able to provide this at the moment.') . '</div>', |
be2fb01f | 362 | ]; |
34866662 CW |
363 | if ($config->backtrace && CRM_Core_Permission::check('view debug output')) { |
364 | $out['backtrace'] = self::parseBacktrace(debug_backtrace()); | |
365 | $message .= '<p><em>See console for backtrace</em></p>'; | |
366 | } | |
1528ac1d | 367 | CRM_Core_Session::setStatus($message, ts('Sorry an error occurred'), 'error'); |
34866662 CW |
368 | CRM_Core_Transaction::forceRollbackIfEnabled(); |
369 | CRM_Core_Page_AJAX::returnJsonResponse($out); | |
370 | } | |
371 | ||
6a488035 TO |
372 | $template = CRM_Core_Smarty::singleton(); |
373 | $template->assign($vars); | |
f431d51f | 374 | $config->userSystem->outputError($template->fetch('CRM/common/fatal.tpl')); |
6a488035 TO |
375 | |
376 | self::abend(CRM_Core_Error::FATAL_ERROR); | |
377 | } | |
378 | ||
379 | /** | |
0880a9d0 | 380 | * Display an error page with an error message describing what happened. |
6a488035 TO |
381 | * |
382 | * This function is evil -- it largely replicates fatal(). Hopefully the | |
383 | * entire CRM_Core_Error system can be hollowed out and replaced with | |
384 | * something that follows a cleaner separation of concerns. | |
385 | * | |
386 | * @param Exception $exception | |
6a488035 | 387 | */ |
00be9182 | 388 | public static function handleUnhandledException($exception) { |
4b57bc9f EM |
389 | try { |
390 | CRM_Utils_Hook::unhandledException($exception); | |
0db6c3e1 TO |
391 | } |
392 | catch (Exception $other) { | |
4b57bc9f | 393 | // if the exception-handler generates an exception, then that sucks! oh, well. carry on. |
120f8e64 | 394 | CRM_Core_Error::debug_var('handleUnhandledException_nestedException', self::formatTextException($other), TRUE, TRUE, '', PEAR_LOG_ERR); |
4b57bc9f | 395 | } |
6a488035 | 396 | $config = CRM_Core_Config::singleton(); |
be2fb01f | 397 | $vars = [ |
6a488035 TO |
398 | 'message' => $exception->getMessage(), |
399 | 'code' => NULL, | |
400 | 'exception' => $exception, | |
be2fb01f | 401 | ]; |
6a488035 | 402 | if (!$vars['message']) { |
be2fb01f | 403 | $vars['message'] = ts('We experienced an unexpected error. You may have found a bug. For more information on how to provide a bug report, please read: %1', [1 => 'https://civicrm.org/bug-reporting']); |
6a488035 TO |
404 | } |
405 | ||
406 | // Case A: CLI | |
407 | if (php_sapi_name() == "cli") { | |
408 | printf("Sorry. A non-recoverable error has occurred.\n%s\n", $vars['message']); | |
409 | print self::formatTextException($exception); | |
410 | die("\n"); | |
411 | // FIXME: Why doesn't this call abend()? | |
412 | // Difference: abend() will cleanup transaction and (via civiExit) store session state | |
413 | // self::abend(CRM_Core_Error::FATAL_ERROR); | |
414 | } | |
415 | ||
416 | // Case B: Custom error handler | |
417 | if ($config->fatalErrorHandler && | |
418 | function_exists($config->fatalErrorHandler) | |
419 | ) { | |
420 | $name = $config->fatalErrorHandler; | |
421 | $ret = $name($vars); | |
422 | if ($ret) { | |
423 | // the call has been successfully handled | |
424 | // so we just exit | |
425 | self::abend(CRM_Core_Error::FATAL_ERROR); | |
426 | } | |
427 | } | |
428 | ||
429 | // Case C: Default error handler | |
430 | ||
431 | // log to file | |
120f8e64 | 432 | CRM_Core_Error::debug_var('Fatal Error Details', $vars, FALSE, TRUE, '', PEAR_LOG_ERR); |
6a488035 TO |
433 | CRM_Core_Error::backtrace('backTrace', TRUE); |
434 | ||
435 | // print to screen | |
436 | $template = CRM_Core_Smarty::singleton(); | |
437 | $template->assign($vars); | |
f431d51f | 438 | $content = $template->fetch('CRM/common/fatal.tpl'); |
2dae8a18 | 439 | |
6a488035 TO |
440 | if ($config->backtrace) { |
441 | $content = self::formatHtmlException($exception) . $content; | |
442 | } | |
2dae8a18 AT |
443 | |
444 | echo CRM_Utils_System::theme($content); | |
6a488035 TO |
445 | |
446 | // fin | |
447 | self::abend(CRM_Core_Error::FATAL_ERROR); | |
448 | } | |
449 | ||
450 | /** | |
100fef9d | 451 | * Outputs pre-formatted debug information. Flushes the buffers |
6a488035 TO |
452 | * so we can interrupt a potential POST/redirect |
453 | * | |
3bdca100 | 454 | * @param string $name name of debug section |
455 | * @param $variable mixed reference to variables that we need a trace of | |
456 | * @param bool $log should we log or return the output | |
457 | * @param bool $html whether to generate a HTML-escaped output | |
458 | * @param bool $checkPermission should we check permissions before displaying output | |
90154ece DL |
459 | * useful when we die during initialization and permissioning |
460 | * subsystem is not initialized - CRM-13765 | |
6a488035 | 461 | * |
a6c01b45 CW |
462 | * @return string |
463 | * the generated output | |
6a488035 | 464 | */ |
00be9182 | 465 | public static function debug($name, $variable = NULL, $log = TRUE, $html = TRUE, $checkPermission = TRUE) { |
6a488035 TO |
466 | $error = self::singleton(); |
467 | ||
468 | if ($variable === NULL) { | |
469 | $variable = $name; | |
470 | $name = NULL; | |
471 | } | |
472 | ||
473 | $out = print_r($variable, TRUE); | |
474 | $prefix = NULL; | |
475 | if ($html) { | |
476 | $out = htmlspecialchars($out); | |
477 | if ($name) { | |
478 | $prefix = "<p>$name</p>"; | |
479 | } | |
480 | $out = "{$prefix}<p><pre>$out</pre></p><p></p>"; | |
481 | } | |
482 | else { | |
483 | if ($name) { | |
484 | $prefix = "$name:\n"; | |
485 | } | |
486 | $out = "{$prefix}$out\n"; | |
487 | } | |
90154ece DL |
488 | if ( |
489 | $log && | |
490 | (!$checkPermission || CRM_Core_Permission::check('view debug output')) | |
491 | ) { | |
6a488035 TO |
492 | echo $out; |
493 | } | |
494 | ||
495 | return $out; | |
496 | } | |
497 | ||
498 | /** | |
499 | * Similar to the function debug. Only difference is | |
500 | * in the formatting of the output. | |
501 | * | |
c490a46a | 502 | * @param string $variable_name |
6b5c5203 | 503 | * Variable name. |
c490a46a | 504 | * @param mixed $variable |
6b5c5203 | 505 | * Variable value. |
6a0b768e | 506 | * @param bool $print |
6b5c5203 | 507 | * Use print_r (if true) or var_dump (if false). |
6a0b768e | 508 | * @param bool $log |
6b5c5203 CB |
509 | * Log or return the output? |
510 | * @param string $prefix | |
511 | * Prefix for output logfile. | |
120f8e64 | 512 | * @param int $priority |
513 | * The log priority level. | |
2a6da8d7 | 514 | * |
a6c01b45 | 515 | * @return string |
6b5c5203 | 516 | * The generated output |
6a488035 TO |
517 | * |
518 | * @see CRM_Core_Error::debug() | |
519 | * @see CRM_Core_Error::debug_log_message() | |
520 | */ | |
120f8e64 | 521 | public static function debug_var($variable_name, $variable, $print = TRUE, $log = TRUE, $prefix = '', $priority = NULL) { |
6a488035 TO |
522 | // check if variable is set |
523 | if (!isset($variable)) { | |
524 | $out = "\$$variable_name is not set"; | |
525 | } | |
526 | else { | |
527 | if ($print) { | |
528 | $out = print_r($variable, TRUE); | |
529 | $out = "\$$variable_name = $out"; | |
530 | } | |
531 | else { | |
10e59978 | 532 | // Use Symfony var-dumper to avoid circular references that exhaust |
533 | // memory when using var_dump(). | |
534 | // Use its CliDumper since if we use the simpler `dump()` then it | |
535 | // comes out as some overly decorated html which is hard to read. | |
536 | $dump = (new \Symfony\Component\VarDumper\Dumper\CliDumper('php://output')) | |
537 | ->dump( | |
538 | (new \Symfony\Component\VarDumper\Cloner\VarCloner())->cloneVar($variable), | |
539 | TRUE); | |
6a488035 TO |
540 | $out = "\n\$$variable_name = $dump"; |
541 | } | |
542 | // reset if it is an array | |
543 | if (is_array($variable)) { | |
544 | reset($variable); | |
545 | } | |
546 | } | |
120f8e64 | 547 | return self::debug_log_message($out, FALSE, $prefix, $priority); |
6a488035 TO |
548 | } |
549 | ||
550 | /** | |
edc8adfc | 551 | * Display the error message on terminal and append it to the log file. |
552 | * | |
553 | * Provided the user has the 'view debug output' the output should be displayed. In all | |
554 | * cases it should be logged. | |
6a488035 | 555 | * |
ea3ddccf | 556 | * @param string $message |
6a0b768e TO |
557 | * @param bool $out |
558 | * Should we log or return the output. | |
6a488035 | 559 | * |
6b5c5203 CB |
560 | * @param string $prefix |
561 | * Message prefix. | |
ea3ddccf | 562 | * @param string $priority |
6a488035 | 563 | * |
ea3ddccf | 564 | * @return string |
565 | * Format of the backtrace | |
6a488035 | 566 | */ |
6b5c5203 | 567 | public static function debug_log_message($message, $out = FALSE, $prefix = '', $priority = NULL) { |
213a5f19 | 568 | $config = CRM_Core_Config::singleton(); |
6a488035 | 569 | |
6b5c5203 | 570 | $file_log = self::createDebugLogger($prefix); |
6e5ad5ee | 571 | $file_log->log("$message\n", $priority); |
0ac9fd52 | 572 | |
9726c118 | 573 | $str = '<p/><code>' . htmlspecialchars($message) . '</code>'; |
6a488035 TO |
574 | if ($out && CRM_Core_Permission::check('view debug output')) { |
575 | echo $str; | |
576 | } | |
577 | $file_log->close(); | |
578 | ||
21ce4627 | 579 | if (!isset(\Civi::$statics[__CLASS__]['userFrameworkLogging'])) { |
580 | // Set it to FALSE first & then try to set it. This is to prevent a loop as calling | |
581 | // $config->userFrameworkLogging can trigger DB queries & under log mode this | |
582 | // then gets called again. | |
583 | \Civi::$statics[__CLASS__]['userFrameworkLogging'] = FALSE; | |
584 | \Civi::$statics[__CLASS__]['userFrameworkLogging'] = $config->userFrameworkLogging; | |
585 | } | |
586 | ||
587 | if (!empty(\Civi::$statics[__CLASS__]['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 | 589 | if ($config->userSystem->is_drupal and function_exists('watchdog')) { |
2e1f50d6 | 590 | watchdog('civicrm', '%message', ['%message' => $message], $priority ?? WATCHDOG_DEBUG); |
213a5f19 | 591 | } |
6f12b344 | 592 | elseif ($config->userSystem->is_drupal and CIVICRM_UF == 'Drupal8') { |
75f08499 | 593 | \Drupal::logger('civicrm')->log($priority ?? \Drupal\Core\Logger\RfcLogLevel::DEBUG, '%message', ['%message' => $message]); |
b6cda82a | 594 | } |
213a5f19 | 595 | } |
6a488035 TO |
596 | |
597 | return $str; | |
598 | } | |
599 | ||
600 | /** | |
601 | * Append to the query log (if enabled) | |
ad37ac8e | 602 | * |
603 | * @param string $string | |
6a488035 | 604 | */ |
00be9182 | 605 | public static function debug_query($string) { |
f9920e29 | 606 | $debugLogQuery = CRM_Utils_Constant::value('CIVICRM_DEBUG_LOG_QUERY', FALSE); |
607 | if ($debugLogQuery === 'backtrace') { | |
fbebca7a | 608 | CRM_Core_Error::backtrace($string, TRUE); |
609 | } | |
f9920e29 | 610 | elseif ($debugLogQuery) { |
611 | CRM_Core_Error::debug_var('Query', $string, TRUE, TRUE, 'sql_log' . $debugLogQuery, PEAR_LOG_DEBUG); | |
6a488035 TO |
612 | } |
613 | } | |
614 | ||
d090b80b TO |
615 | /** |
616 | * Execute a query and log the results. | |
617 | * | |
618 | * @param string $query | |
619 | */ | |
00be9182 | 620 | public static function debug_query_result($query) { |
7d0b8a47 | 621 | $results = CRM_Core_DAO::executeQuery($query)->fetchAll(); |
120f8e64 | 622 | CRM_Core_Error::debug_var('dao result', ['query' => $query, 'results' => $results], TRUE, TRUE, '', PEAR_LOG_DEBUG); |
d090b80b TO |
623 | } |
624 | ||
6a488035 | 625 | /** |
0880a9d0 | 626 | * Obtain a reference to the error log. |
6a488035 | 627 | * |
edc8adfc | 628 | * @param string $prefix |
77b97be7 | 629 | * |
2aafb0fc | 630 | * @return Log_file |
6a488035 | 631 | */ |
edc8adfc | 632 | public static function createDebugLogger($prefix = '') { |
633 | self::generateLogFileName($prefix); | |
634 | return Log::singleton('file', \Civi::$statics[__CLASS__]['logger_file' . $prefix], ''); | |
635 | } | |
636 | ||
44c32d0a CB |
637 | /** |
638 | * Generate a hash for the logfile. | |
bf48aa29 | 639 | * |
44c32d0a | 640 | * CRM-13640. |
bf48aa29 | 641 | * |
642 | * @param CRM_Core_Config $config | |
643 | * | |
644 | * @return string | |
44c32d0a | 645 | */ |
f0f1e508 CB |
646 | public static function generateLogFileHash($config) { |
647 | // Use multiple (but stable) inputs for hash information. | |
be2fb01f | 648 | $md5inputs = [ |
262e9b08 CB |
649 | defined('CIVICRM_SITE_KEY') ? CIVICRM_SITE_KEY : 'NO_SITE_KEY', |
650 | $config->userFrameworkBaseURL, | |
44c32d0a CB |
651 | md5($config->dsn), |
652 | $config->dsn, | |
be2fb01f | 653 | ]; |
262e9b08 CB |
654 | // Trim 8 chars off the string, make it slightly easier to find |
655 | // but reveals less information from the hash. | |
315ca480 | 656 | return substr(md5(var_export($md5inputs, 1)), 8); |
44c32d0a CB |
657 | } |
658 | ||
edc8adfc | 659 | /** |
660 | * Generate the name of the logfile to use and store it as a static. | |
661 | * | |
e047612e CB |
662 | * This function includes simplistic log rotation and a check as to whether |
663 | * the file exists. | |
edc8adfc | 664 | * |
665 | * @param string $prefix | |
666 | */ | |
667 | protected static function generateLogFileName($prefix) { | |
668 | if (!isset(\Civi::$statics[__CLASS__]['logger_file' . $prefix])) { | |
931ba0f3 | 669 | $config = CRM_Core_Config::singleton(); |
6a488035 | 670 | |
edc8adfc | 671 | $prefixString = $prefix ? ($prefix . '.') : ''; |
6a488035 | 672 | |
9e618bf2 DH |
673 | if (CRM_Utils_Constant::value('CIVICRM_LOG_HASH', TRUE)) { |
674 | $hash = self::generateLogFileHash($config) . '.'; | |
675 | } | |
676 | else { | |
677 | $hash = ''; | |
678 | } | |
679 | $fileName = $config->configAndLogDir . 'CiviCRM.' . $prefixString . $hash . 'log'; | |
931ba0f3 | 680 | |
9e618bf2 | 681 | // Roll log file monthly or if greater than our threshold. |
e047612e CB |
682 | // Size-based rotation introduced in response to filesize limits on |
683 | // certain OS/PHP combos. | |
9e618bf2 DH |
684 | $maxBytes = CRM_Utils_Constant::value('CIVICRM_LOG_ROTATESIZE', 256 * 1024 * 1024); |
685 | if ($maxBytes) { | |
686 | if (file_exists($fileName)) { | |
687 | $fileTime = date("Ym", filemtime($fileName)); | |
688 | $fileSize = filesize($fileName); | |
689 | if (($fileTime < date('Ym')) || | |
690 | ($fileSize > $maxBytes) || | |
691 | ($fileSize < 0) | |
692 | ) { | |
693 | rename($fileName, | |
694 | $fileName . '.' . date('YmdHi') | |
695 | ); | |
696 | } | |
931ba0f3 | 697 | } |
698 | } | |
edc8adfc | 699 | \Civi::$statics[__CLASS__]['logger_file' . $prefix] = $fileName; |
931ba0f3 | 700 | } |
6a488035 TO |
701 | } |
702 | ||
a0ee3941 EM |
703 | /** |
704 | * @param string $msg | |
705 | * @param bool $log | |
706 | */ | |
00be9182 | 707 | public static function backtrace($msg = 'backTrace', $log = FALSE) { |
6a488035 TO |
708 | $backTrace = debug_backtrace(); |
709 | $message = self::formatBacktrace($backTrace); | |
710 | if (!$log) { | |
711 | CRM_Core_Error::debug($msg, $message); | |
712 | } | |
713 | else { | |
120f8e64 | 714 | CRM_Core_Error::debug_var($msg, $message, TRUE, TRUE, '', PEAR_LOG_DEBUG); |
6a488035 TO |
715 | } |
716 | } | |
717 | ||
718 | /** | |
0880a9d0 | 719 | * Render a backtrace array as a string. |
6a488035 | 720 | * |
6a0b768e TO |
721 | * @param array $backTrace |
722 | * Array of stack frames. | |
723 | * @param bool $showArgs | |
724 | * 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. | |
725 | * @param int $maxArgLen | |
726 | * Maximum number of characters to show from each argument string. | |
a6c01b45 CW |
727 | * @return string |
728 | * printable plain-text | |
6a488035 | 729 | */ |
00be9182 | 730 | public static function formatBacktrace($backTrace, $showArgs = TRUE, $maxArgLen = 80) { |
6a488035 | 731 | $message = ''; |
34866662 CW |
732 | foreach (self::parseBacktrace($backTrace, $showArgs, $maxArgLen) as $idx => $trace) { |
733 | $message .= sprintf("#%s %s\n", $idx, $trace); | |
734 | } | |
2aa397bc | 735 | $message .= sprintf("#%s {main}\n", 1 + $idx); |
34866662 CW |
736 | return $message; |
737 | } | |
738 | ||
739 | /** | |
0880a9d0 | 740 | * Render a backtrace array as an array. |
34866662 | 741 | * |
6a0b768e TO |
742 | * @param array $backTrace |
743 | * Array of stack frames. | |
744 | * @param bool $showArgs | |
745 | * 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. | |
746 | * @param int $maxArgLen | |
747 | * Maximum number of characters to show from each argument string. | |
34866662 CW |
748 | * @return array |
749 | * @see debug_backtrace | |
750 | * @see Exception::getTrace() | |
751 | */ | |
00be9182 | 752 | public static function parseBacktrace($backTrace, $showArgs = TRUE, $maxArgLen = 80) { |
be2fb01f | 753 | $ret = []; |
34866662 | 754 | foreach ($backTrace as $trace) { |
be2fb01f | 755 | $args = []; |
9c1bc317 | 756 | $fnName = $trace['function'] ?? NULL; |
34866662 | 757 | $className = isset($trace['class']) ? ($trace['class'] . $trace['type']) : ''; |
6a488035 | 758 | |
83e7cba6 | 759 | // Do not show args for a few password related functions |
63d76404 | 760 | $skipArgs = $className == 'DB::' && $fnName == 'connect'; |
6a488035 | 761 | |
83e7cba6 CB |
762 | if (!empty($trace['args'])) { |
763 | foreach ($trace['args'] as $arg) { | |
353ffa53 | 764 | if (!$showArgs || $skipArgs) { |
83e7cba6 CB |
765 | $args[] = '(' . gettype($arg) . ')'; |
766 | continue; | |
767 | } | |
768 | switch ($type = gettype($arg)) { | |
769 | case 'boolean': | |
770 | $args[] = $arg ? 'TRUE' : 'FALSE'; | |
771 | break; | |
2aa397bc | 772 | |
83e7cba6 CB |
773 | case 'integer': |
774 | case 'double': | |
775 | $args[] = $arg; | |
776 | break; | |
2aa397bc | 777 | |
83e7cba6 | 778 | case 'string': |
86bfa4f6 | 779 | $args[] = '"' . CRM_Utils_String::ellipsify(addcslashes((string) $arg, "\r\n\t\""), $maxArgLen) . '"'; |
83e7cba6 | 780 | break; |
2aa397bc | 781 | |
83e7cba6 | 782 | case 'array': |
92fcb95f | 783 | $args[] = '(Array:' . count($arg) . ')'; |
83e7cba6 | 784 | break; |
2aa397bc | 785 | |
83e7cba6 CB |
786 | case 'object': |
787 | $args[] = 'Object(' . get_class($arg) . ')'; | |
788 | break; | |
2aa397bc | 789 | |
83e7cba6 CB |
790 | case 'resource': |
791 | $args[] = 'Resource'; | |
792 | break; | |
2aa397bc | 793 | |
83e7cba6 CB |
794 | case 'NULL': |
795 | $args[] = 'NULL'; | |
796 | break; | |
2aa397bc | 797 | |
83e7cba6 CB |
798 | default: |
799 | $args[] = "($type)"; | |
800 | break; | |
801 | } | |
6a488035 TO |
802 | } |
803 | } | |
804 | ||
34866662 CW |
805 | $ret[] = sprintf( |
806 | "%s(%s): %s%s(%s)", | |
6a488035 TO |
807 | CRM_Utils_Array::value('file', $trace, '[internal function]'), |
808 | CRM_Utils_Array::value('line', $trace, ''), | |
809 | $className, | |
810 | $fnName, | |
811 | implode(", ", $args) | |
812 | ); | |
813 | } | |
34866662 | 814 | return $ret; |
6a488035 TO |
815 | } |
816 | ||
817 | /** | |
0880a9d0 | 818 | * Render an exception as HTML string. |
6a488035 | 819 | * |
614fcec4 | 820 | * @param Throwable $e |
a6c01b45 CW |
821 | * @return string |
822 | * printable HTML text | |
6a488035 | 823 | */ |
614fcec4 | 824 | public static function formatHtmlException(Throwable $e) { |
6a488035 TO |
825 | $msg = ''; |
826 | ||
827 | // Exception metadata | |
828 | ||
829 | // Exception backtrace | |
830 | if ($e instanceof PEAR_Exception) { | |
831 | $ei = $e; | |
ca5b3d14 | 832 | if (is_callable([$ei, 'getCause'])) { |
d216dc24 SL |
833 | // DB_ERROR doesn't have a getCause but does have a __call function which tricks is_callable. |
834 | if (!$ei instanceof DB_Error) { | |
835 | if ($ei->getCause() instanceof PEAR_Error) { | |
836 | $msg .= '<table class="crm-db-error">'; | |
837 | $msg .= sprintf('<thead><tr><th>%s</th><th>%s</th></tr></thead>', ts('Error Field'), ts('Error Value')); | |
838 | $msg .= '<tbody>'; | |
839 | foreach (['Type', 'Code', 'Message', 'Mode', 'UserInfo', 'DebugInfo'] as $f) { | |
840 | $msg .= sprintf('<tr><td>%s</td><td>%s</td></tr>', $f, call_user_func([$ei->getCause(), "get$f"])); | |
841 | } | |
842 | $msg .= '</tbody></table>'; | |
2aa397bc | 843 | } |
d216dc24 | 844 | $ei = $ei->getCause(); |
2aa397bc | 845 | } |
2aa397bc | 846 | } |
6a488035 | 847 | $msg .= $e->toHtml(); |
0db6c3e1 TO |
848 | } |
849 | else { | |
6a488035 TO |
850 | $msg .= '<p><b>' . get_class($e) . ': "' . htmlentities($e->getMessage()) . '"</b></p>'; |
851 | $msg .= '<pre>' . htmlentities(self::formatBacktrace($e->getTrace())) . '</pre>'; | |
852 | } | |
853 | return $msg; | |
854 | } | |
855 | ||
856 | /** | |
0880a9d0 | 857 | * Write details of an exception to the log. |
6a488035 | 858 | * |
614fcec4 | 859 | * @param Throwable $e |
a6c01b45 CW |
860 | * @return string |
861 | * printable plain text | |
6a488035 | 862 | */ |
614fcec4 | 863 | public static function formatTextException(Throwable $e) { |
6a488035 TO |
864 | $msg = get_class($e) . ": \"" . $e->getMessage() . "\"\n"; |
865 | ||
866 | $ei = $e; | |
be2fb01f | 867 | while (is_callable([$ei, 'getCause'])) { |
d216dc24 SL |
868 | // DB_ERROR doesn't have a getCause but does have a __call function which tricks is_callable. |
869 | if (!$ei instanceof DB_Error) { | |
870 | if ($ei->getCause() instanceof PEAR_Error) { | |
871 | foreach (['Type', 'Code', 'Message', 'Mode', 'UserInfo', 'DebugInfo'] as $f) { | |
872 | $msg .= sprintf(" * ERROR %s: %s\n", strtoupper($f), call_user_func([$ei->getCause(), "get$f"])); | |
873 | } | |
6a488035 | 874 | } |
d216dc24 SL |
875 | $ei = $ei->getCause(); |
876 | } | |
877 | // if we have reached a DB_Error assume that is the end of the road. | |
878 | else { | |
879 | $ei = NULL; | |
6a488035 | 880 | } |
6a488035 TO |
881 | } |
882 | $msg .= self::formatBacktrace($e->getTrace()); | |
883 | return $msg; | |
884 | } | |
885 | ||
a0ee3941 EM |
886 | /** |
887 | * @param $message | |
888 | * @param int $code | |
889 | * @param string $level | |
100fef9d | 890 | * @param array $params |
a0ee3941 EM |
891 | * |
892 | * @return object | |
893 | */ | |
00be9182 | 894 | public static function createError($message, $code = 8000, $level = 'Fatal', $params = NULL) { |
6a488035 | 895 | $error = CRM_Core_Error::singleton(); |
be2fb01f | 896 | $error->push($code, $level, [$params], $message); |
6a488035 TO |
897 | return $error; |
898 | } | |
899 | ||
900 | /** | |
901 | * Set a status message in the session, then bounce back to the referrer. | |
902 | * | |
6a0b768e TO |
903 | * @param string $status |
904 | * The status message to set. | |
6a488035 | 905 | * |
2a6da8d7 EM |
906 | * @param null $redirect |
907 | * @param string $title | |
6a488035 | 908 | */ |
50ecd413 | 909 | public static function statusBounce($status, $redirect = NULL, $title = NULL) { |
6a488035 TO |
910 | $session = CRM_Core_Session::singleton(); |
911 | if (!$redirect) { | |
912 | $redirect = $session->readUserContext(); | |
913 | } | |
50ecd413 CW |
914 | if ($title === NULL) { |
915 | $title = ts('Error'); | |
916 | } | |
be2fb01f | 917 | $session->setStatus($status, $title, 'alert', ['expires' => 0]); |
34866662 | 918 | if (CRM_Utils_Array::value('snippet', $_REQUEST) === CRM_Core_Smarty::PRINT_JSON) { |
be2fb01f | 919 | CRM_Core_Page_AJAX::returnJsonResponse(['status' => 'error']); |
34866662 | 920 | } |
6a488035 TO |
921 | CRM_Utils_System::redirect($redirect); |
922 | } | |
923 | ||
924 | /** | |
0880a9d0 | 925 | * Reset the error stack. |
6a488035 | 926 | * |
6a488035 TO |
927 | */ |
928 | public static function reset() { | |
929 | $error = self::singleton(); | |
be2fb01f CW |
930 | $error->_errors = []; |
931 | $error->_errorsByLevel = []; | |
6a488035 TO |
932 | } |
933 | ||
6a4257d4 | 934 | /** |
42c0daee TO |
935 | * PEAR error-handler which converts errors to exceptions |
936 | * | |
937 | * @param $pearError | |
938 | * @throws PEAR_Exception | |
6a4257d4 | 939 | */ |
6a488035 | 940 | public static function exceptionHandler($pearError) { |
120f8e64 | 941 | CRM_Core_Error::debug_var('Fatal Error Details', self::getErrorDetails($pearError), TRUE, TRUE, '', PEAR_LOG_ERR); |
6a488035 TO |
942 | CRM_Core_Error::backtrace('backTrace', TRUE); |
943 | throw new PEAR_Exception($pearError->getMessage(), $pearError); | |
944 | } | |
945 | ||
946 | /** | |
42c0daee | 947 | * PEAR error-handler to quietly catch otherwise fatal errors. Intended for use with smtp transport. |
6a488035 | 948 | * |
6a0b768e TO |
949 | * @param object $obj |
950 | * The PEAR_ERROR object. | |
a6c01b45 CW |
951 | * @return object |
952 | * $obj | |
6a488035 TO |
953 | */ |
954 | public static function nullHandler($obj) { | |
120f8e64 | 955 | CRM_Core_Error::debug_log_message("Ignoring exception thrown by nullHandler: {$obj->code}, {$obj->message}", FALSE, '', PEAR_LOG_ERR); |
6a488035 TO |
956 | CRM_Core_Error::backtrace('backTrace', TRUE); |
957 | return $obj; | |
958 | } | |
959 | ||
d424ffde | 960 | /** |
6a488035 TO |
961 | * @deprecated |
962 | * This function is no longer used by v3 api. | |
963 | * @fixme Some core files call it but it should be re-thought & renamed or removed | |
d424ffde | 964 | * |
a0ee3941 EM |
965 | * @param $msg |
966 | * @param null $data | |
967 | * | |
968 | * @return array | |
969 | * @throws Exception | |
970 | */ | |
6a488035 TO |
971 | public static function &createAPIError($msg, $data = NULL) { |
972 | if (self::$modeException) { | |
973 | throw new Exception($msg, $data); | |
974 | } | |
975 | ||
be2fb01f | 976 | $values = []; |
6a488035 TO |
977 | |
978 | $values['is_error'] = 1; | |
979 | $values['error_message'] = $msg; | |
980 | if (isset($data)) { | |
981 | $values = array_merge($values, $data); | |
982 | } | |
983 | return $values; | |
984 | } | |
985 | ||
a0ee3941 EM |
986 | /** |
987 | * @param $file | |
988 | */ | |
6a488035 TO |
989 | public static function movedSiteError($file) { |
990 | $url = CRM_Utils_System::url('civicrm/admin/setting/updateConfigBackend', | |
991 | 'reset=1', | |
992 | TRUE | |
993 | ); | |
994 | echo "We could not write $file. Have you moved your site directory or server?<p>"; | |
995 | echo "Please fix the setting by running the <a href=\"$url\">update config script</a>"; | |
996 | exit(); | |
997 | } | |
998 | ||
999 | /** | |
0880a9d0 | 1000 | * Terminate execution abnormally. |
ad37ac8e | 1001 | * |
1002 | * @param string $code | |
6a488035 TO |
1003 | */ |
1004 | protected static function abend($code) { | |
1005 | // do a hard rollback of any pending transactions | |
1006 | // if we've come here, its because of some unexpected PEAR errors | |
1007 | CRM_Core_Transaction::forceRollbackIfEnabled(); | |
1008 | CRM_Utils_System::civiExit($code); | |
1009 | } | |
1010 | ||
a0ee3941 | 1011 | /** |
3ab5efa9 EM |
1012 | * @param array $error |
1013 | * @param int $type | |
a0ee3941 EM |
1014 | * |
1015 | * @return bool | |
1016 | */ | |
6a488035 | 1017 | public static function isAPIError($error, $type = CRM_Core_Error::FATAL_ERROR) { |
8cc574cf | 1018 | if (is_array($error) && !empty($error['is_error'])) { |
6a488035 TO |
1019 | $code = $error['error_message']['code']; |
1020 | if ($code == $type) { | |
1021 | return TRUE; | |
1022 | } | |
1023 | } | |
1024 | return FALSE; | |
1025 | } | |
96025800 | 1026 | |
496320c3 MW |
1027 | /** |
1028 | * Output a deprecated function warning to log file. Deprecated class:function is automatically generated from calling function. | |
1029 | * | |
5a0aaa1e | 1030 | * @param string $newMethod |
496320c3 | 1031 | * description of new method (eg. "buildOptions() method in the appropriate BAO object"). |
5a0aaa1e MW |
1032 | * @param string $oldMethod |
1033 | * optional description of old method (if not the calling method). eg. CRM_MyClass::myOldMethodToGetTheOptions() | |
496320c3 | 1034 | */ |
5a0aaa1e MW |
1035 | public static function deprecatedFunctionWarning($newMethod, $oldMethod = NULL) { |
1036 | if (!$oldMethod) { | |
1037 | $dbt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); | |
1038 | $callerFunction = $dbt[1]['function'] ?? NULL; | |
1039 | $callerClass = $dbt[1]['class'] ?? NULL; | |
1040 | $oldMethod = "{$callerClass}::{$callerFunction}"; | |
1041 | } | |
e9dc5230 SL |
1042 | self::deprecatedWarning("Deprecated function $oldMethod, use $newMethod."); |
1043 | } | |
1044 | ||
1045 | /** | |
1046 | * Output a deprecated notice about a deprecated call path, rather than deprecating a whole function. | |
1047 | * @param string $message | |
1048 | */ | |
1049 | public static function deprecatedWarning($message) { | |
160aed81 | 1050 | // Even though the tag is no longer used within the log() function, |
1051 | // \Civi\API\LogObserver instances may still be monitoring it. | |
e9dc5230 | 1052 | Civi::log()->warning($message, ['civi.tag' => 'deprecated']); |
160aed81 | 1053 | trigger_error($message, E_USER_DEPRECATED); |
496320c3 MW |
1054 | } |
1055 | ||
6a488035 TO |
1056 | } |
1057 | ||
1058 | $e = new PEAR_ErrorStack('CRM'); | |
1059 | $e->singleton('CRM', FALSE, NULL, 'CRM_Core_Error'); |