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