forked from mlutfy/reporterror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporterror.php
408 lines (341 loc) · 12.1 KB
/
reporterror.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
require_once 'reporterror.civix.php';
define('REPORTERROR_CIVICRM_SUBJECT_LEN', 100);
define('REPORTERROR_EMAIL_SEPARATOR', ',');
require_once(__DIR__ . '/vendor/autoload.php');
use CRM_ReportError_ExtensionUtil as E;
/**
* Implementation of hook_civicrm_config
*/
function reporterror_civicrm_config(&$config) {
_reporterror_civix_civicrm_config($config);
// override the error handler
$config = CRM_Core_Config::singleton();
$config->fatalErrorHandler = 'reporterror_civicrm_handler';
}
/**
* Implementation of hook_civicrm_xmlMenu
*
* @param $files array(string)
*/
function reporterror_civicrm_xmlMenu(&$files) {
_reporterror_civix_civicrm_xmlMenu($files);
}
/**
* Implementation of hook_civicrm_install
*/
function reporterror_civicrm_install() {
return _reporterror_civix_civicrm_install();
}
/**
* Implementation of hook_civicrm_uninstall
*/
function reporterror_civicrm_uninstall() {
// Send final email
$subject = E::ts('CiviCRM Error Report was uninstalled');
$output = $subject . _reporterror_civicrm_get_session_info();
$to = Civi::settings()->get('reporterror_mailto');
if (!empty($to)) {
$destinations = explode(REPORTERROR_EMAIL_SEPARATOR, $to);
foreach ($destinations as $dest) {
$dest = trim($dest);
reporterror_civicrm_send_mail($dest, $subject, $output);
}
}
else {
Civi::log()->warning('Report Error Extension could not send since no email address was set.');
}
// Delete our settings
// FIXME: Maybe settings metadata helps? This is redundant.
$settings = [ 'reporterror_show_full_backtrace', 'reporterror_show_post_data', 'reporterror_show_session_data', 'reporterror_noreferer_sendreport', 'reporterror_noreferer_sendreport_event', 'reporterror_bots_sendreport', 'reporterror_bots_404', 'reporterror_bots_regexp' ];
foreach ($settings as $name) {
CRM_Core_DAO::executeQuery('DELETE FROM civicrm_setting WHERE name = %1', array(
1 => array($name, 'String'),
));
}
return _reporterror_civix_civicrm_uninstall();
}
/**
* Implementation of hook_civicrm_enable
*/
function reporterror_civicrm_enable() {
return _reporterror_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_disable
*/
function reporterror_civicrm_disable() {
return _reporterror_civix_civicrm_disable();
}
/**
* Implementation of hook_civicrm_upgrade
*
* @param $op string, the type of operation being performed; 'check' or 'enqueue'
* @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
*
* @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
* for 'enqueue', returns void
*/
function reporterror_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _reporterror_civix_civicrm_upgrade($op, $queue);
}
/**
* Implementation of hook_civicrm_managed
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*/
function reporterror_civicrm_managed(&$entities) {
return _reporterror_civix_civicrm_managed($entities);
}
/**
* Implementation of hook_civicrm_navigationMenu
*/
function reporterror_civicrm_navigationMenu(&$params) {
/*
_reporterror_civix_insert_navigationMenu($params, 'Administer/System Settings', array(
'name' => 'Report Error Settings',
'url' => 'civicrm/admin/reporterror',
'permission' => 'administer CiviCRM',
));
*/
// Get the ID of the 'Administer/System Settings' menu
$adminMenuId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Navigation', 'Administer', 'id', 'name');
$settingsMenuId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Navigation', 'System Settings', 'id', 'name');
// Skip adding menu if there is no administer menu
if (! $adminMenuId) {
CRM_Core_Error::debug_log_message('Report Error Extension could not find the Administer menu item. Menu item to configure this extension will not be added.');
return;
}
if (! $settingsMenuId) {
Civi::log()->warning('Report Error Extension could not find the System Settings menu item. Menu item to configure this extension will not be added.');
return;
}
// get the maximum key under administer menu
$maxSettingsMenuKey = max(array_keys($params[$adminMenuId]['child'][$settingsMenuId]['child']));
$nextSettingsMenuKey = $maxSettingsMenuKey + 1;
$params[$adminMenuId]['child'][$settingsMenuId]['child'][$nextSettingsMenuKey] = array(
'attributes' => array(
'name' => 'Report Error Settings',
'label' => 'Report Error Settings',
'url' => 'civicrm/admin/setting/reporterror?reset=1',
'permission' => 'administer CiviCRM',
'parentID' => $settingsMenuId,
'navID' => $nextSettingsMenuKey,
'active' => 1,
),
);
}
/**
* Custom error handler.
* This is registered as a callback in hook_civicrm_config().
*
* @param $vars Array with the 'message' and 'code' of the error.
*/
function reporterror_civicrm_handler($vars, $options_overrides = array()) {
$handers = [
'IgnoreBots',
'FormsNoReferer',
'SmartGroupRefresh',
'Profiles',
];
foreach ($handers as $h) {
$success = call_user_func_array('CRM_ReportError_Handler_' . $h . '::handler', [$vars, $options_overrides]);
if ($success) {
return TRUE;
}
}
// We let CiviCRM display the regular fatal error
return FALSE;
}
/**
* Returns a plain text output for the e-mail report.
*
* FIXME: the redirect_path should be included in 'vars'
* This should be rewritten under CRM_ReportError_Utils,
* with backwards-compat wrapper.
*/
function reporterror_civicrm_generatereport($site_name, $vars, $redirect_path, $options_overrides = array()) {
$show_full_backtrace = reporterror_setting_get('reporterror_show_full_backtrace', $options_overrides);
$show_post_data = reporterror_setting_get('reporterror_show_post_data', $options_overrides);
$show_session_data = reporterror_setting_get('reporterror_show_session_data', $options_overrides);
$output = E::ts('There was a CiviCRM error at %1.', array(1 => $site_name)) . "\n";
$output .= E::ts('Date: %1', array(1 => date('c'))) . "\n\n";
// Backwards compatibility
if ($redirect_path && empty($vars['redirect_path'])) {
$vars['redirect_path'] = $redirect_path;
}
if (!empty($vars['redirect_path'])) {
$output .= E::ts("Error handling rules redirected the user to:") . "\n";
$output .= $vars['redirect_path'] . "\n\n";
}
// Error details
$output .= "\n\n***ERROR***\n";
$output .= _reporterror_civicrm_parse_array($vars);
// The "last error" can sometimes help, but it can also mislead
// (ex: PHP notice during the error).
if (function_exists('error_get_last')) {
$output .= "***LAST ERROR***\n";
$output .= print_r(error_get_last(), TRUE);
}
// User information and the session variable
$output .= _reporterror_civicrm_get_session_info($show_session_data);
// Backtrace
$output .= "\n\n***BACKTRACE***\n";
$backtrace = debug_backtrace();
$output .= CRM_Core_Error::formatBacktrace($backtrace, TRUE, 120);
// $_POST
if ($show_post_data) {
$output .= "\n\n***POST***\n";
$output .= _reporterror_civicrm_parse_array($_POST);
}
if ($show_full_backtrace) {
$output .= "\n\n***FULL BACKTRACE***\n";
foreach ($backtrace as $call) {
$output .= "**next call**\n";
$output .= _reporterror_civicrm_parse_array($call);
}
}
return $output;
}
/**
* Send the e-mail using CRM_Utils_Mail::send()
*/
function reporterror_civicrm_send_mail($to, $subject, $output) {
$email = '';
// only send notification for DB errors
$match = "DB Error";
if (strpos($subject, $match) == false) {
return;
}
$result = civicrm_api('OptionValue', 'get', array('option_group_name' => 'from_email_address', 'is_default' => TRUE, 'version' => 3));
if ($result['is_error']) {
CRM_Core_Error::debug_log_message('Report Error Extension: failed to get the default from email address');
return;
}
$val = array_pop($result['values']);
$email = $val['label'];
if (! $email) {
return;
}
$params = array(
'from' => $email,
'toName' => 'Site Administrator',
'toEmail' => $to,
'subject' => $subject,
'text' => $output,
);
$mail_sent = CRM_Utils_Mail::send($params);
if (! $mail_sent) {
CRM_Core_Error::debug_log_message('Report Error Extension: Could not send mail');
}
}
/**
* Helper function to return a pretty print of the given array
*
* @param array $array
* The array to print out.
* @return string
* The printed array.
*/
function _reporterror_civicrm_parse_array($array) {
$output = '';
$array = (array) $array;
foreach ($array as $key => $value) {
if (is_array($value) || is_object($value)) {
$value = print_r($value, TRUE);
}
$key = str_pad($key . ':', 20, ' ');
$output .= $key . _reporterror_civicrm_check_length($value) . "\n";
}
// Remove sensitive data.
// We do this hackishly this way, because:
// - doing a search/replace in the $array can cause changes in the $_SESSION, for example, because of references.
// - re-writing print_r() seemed a bit ambitious, and likely to introduce bugs.
$output = preg_replace('/\[credit_card_number\] => (\d{4})\d+/', '[credit_card_number] => \1[removed]', $output);
$output = preg_replace('/\[cvv2\] => \d+/', '[cvv2] => [removed]', $output);
$output = preg_replace('/\[password\] => .*$/', '[password] => [removed]', $output);
// This is for the POST data
$output = preg_replace('/credit_card_number:\s+(\d{4})\d+/', 'credit_card_number: \1[removed]', $output);
$output = preg_replace('/cvv2:\s+\d+/', 'cvv2: [removed]', $output);
$output = preg_replace('/password: .*$/', 'password: [removed]', $output);
return $output . "\n";
}
/**
* Helper function to add elipses and return spaces if null
*
* @param string $item
* String to check.
* @return string
* The truncated string.
*/
function _reporterror_civicrm_check_length($item) {
if (is_null($item)) {
return ' ';
}
if (strlen($item) > 2000) {
$item = substr($item, 0, 2000) .'...';
}
return (string) $item;
}
/**
* Helper function to get user session info for email body.
*
* @return string
* Partial email body string with user session info.
*/
function _reporterror_civicrm_get_session_info($show_session_data = FALSE) {
$output = '';
// User info
$session = CRM_Core_Session::singleton();
$userId = $session->get('userID');
if ($userId) {
$output .= "\n\n***LOGGED IN USER***\n";
try {
$contact = civicrm_api3('Contact', 'getsingle', [
'id' => $userId,
'return' => 'id,display_name,email',
]);
$output .= _reporterror_civicrm_parse_array($contact);
}
catch (Exception $e) {
$output .= "Failed to fetch user info using the API:\n";
}
}
else {
// Show the remote IP and user-agent of anon users, to facilitate
// identification of bots and other source of false positives.
$output .= "\n\n***ANONYMOUS USER***\n";
}
$output .= "REMOTE_ADDR: " . $_SERVER['REMOTE_ADDR'] . "\n";
$output .= "HTTP_USER_AGENT: " . $_SERVER['HTTP_USER_AGENT'] . "\n";
if ($show_session_data) {
$output .= "\n\n***SESSION***\n";
$output .= _reporterror_civicrm_parse_array($_SESSION);
}
// $_SERVER
$output .= "\n\n***SERVER***\n";
$output .= _reporterror_civicrm_parse_array($_SERVER);
return $output;
}
/**
* Helper function to get a specific setting of the extension,
* or lookup an override option.
*
* Option overrides is an array of settings that the calling function
* can set to override the behavior of the report. For example, if a
* payment processor caught an exception doing a curl/soap request, it
* will probably want to disable the full backtrace and session info.
*/
function reporterror_setting_get($name, $options_overrides, $default = NULL) {
if (isset($options_overrides[$name])) {
return $options_overrides[$name];
}
return Civi::settings()->get($name);
}
/**
* Implements hook_civicrm_alterSettingsFolders().
*/
function reporterror_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_reporterror_civix_civicrm_alterSettingsFolders($metaDataFolders);
}