forked from mlutfy/reporterror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporterror.php
305 lines (260 loc) · 8.64 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
<?php
require_once 'reporterror.civix.php';
define('REPORTERROR_CIVICRM_SUBJECT_LEN', 100);
define('REPORTERROR_SETTINGS_GROUP', 'ReportError Extension');
/**
* 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() {
// insert into civicrm_setting (group_name, name, value, domain_id) values ('reporterror', 'mailto', '[email protected]', 1);
return _reporterror_civix_civicrm_install();
}
/**
* Implementation of hook_civicrm_uninstall
*/
function reporterror_civicrm_uninstall() {
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',
));
}
/**
* Custom error function
* Set CiviCRM » Administer CiviCRM » Global Settings » Debugging » Fatal Error Handler
* to use this function.
*/
function reporterror_civicrm_handler($vars) {
$sendreport = TRUE;
$redirect_path = NULL;
$redirect_options = array();
//
// Try to handle the error in a more user-friendly way
//
// Contribution forms: error with no HTTP_REFERER (most likely a bot, restored session, or copy-pasted link)
$config = CRM_Core_Config::singleton();
$urlVar = $config->userFrameworkURLVar;
$arg = explode('/', $_GET[$urlVar]);
// Redirect for Contribution pages without a referrer (close / restore browser page)
if ($arg[0] == 'civicrm' && $arg[1] == 'contribute' && $arg[2] == 'transact' && ! $_SERVER['HTTP_REFERER']) {
$handle = CRM_Core_BAO_Setting::getItem(REPORTERROR_SETTINGS_GROUP, 'noreferer_handle');
$pageid = CRM_Core_BAO_Setting::getItem(REPORTERROR_SETTINGS_GROUP, 'noreferer_pageid');
$sendreport = CRM_Core_BAO_Setting::getItem(REPORTERROR_SETTINGS_GROUP, 'noreferer_sendreport', NULL, 1);
if ($handle == 1 || ($handle == 2 && ! $pageid)) {
$redirect_path = CRM_Utils_System::baseCMSURL();
}
elseif ($handle == 2) {
$redirect_path = CRM_Utils_System::url('civicrm/contribute/transact', 'reset=1&id='.$pageid);
}
}
/*
if (arg(0) == 'civicrm' && arg(1) == 'contribute' && arg(2) == 'campaign' && ! isset($_REQUEST['component'])) {
$handle = variable_get('civicrm_error_fallback_pcp_nocomponent_handle', 0);
$pageid = $_REQUEST['pageId'];
if ($handle == 1) {
$redirect_path = '<front>';
}
elseif ($handle == 2) {
$redirect_path = 'civicrm/contribute/campaign';
$redirect_options['query'] = array(
'reset' => 1,
'action' => 'add',
'pageId' => $pageid,
'component' => 'contribute',
);
}
$sendreport = variable_get('civicrm_error_fallback_pcp_nocomponent_alert', 1);
}
*/
// Send email report
if ($sendreport) {
$domain = CRM_Core_BAO_Domain::getDomain();
$site_name = $domain->name;
$len = REPORTERROR_CIVICRM_SUBJECT_LEN;
if ($redirect_path) {
$subject = ts('CiviCRM error [redirected] at %1', array(1 => $site_name, 'domain' => 'ca.bidon.reporterror'));
}
else {
$subject = ts('CiviCRM error at %1', array(1 => $site_name, 'domain' => 'ca.bidon.reporterror'));
}
if ($len) {
$subject .= ' (' . substr($vars['message'], 0, $len) . ')';
}
$to = CRM_Core_BAO_Setting::getItem(REPORTERROR_SETTINGS_GROUP, 'mailto');
if (! $to) {
// FIXME: log to error log?
return;
}
$destinations = explode(',', $to);
$output = reporterror_civicrm_generatereport($site_name, $vars, $redirect_path);
foreach ($destinations as $dest) {
$dest = trim($dest);
reporterror_civicrm_send_mail($dest, $subject, $output);
}
}
// A redirection avoids displaying the error to the user.
if ($redirect_path) {
// 307 = temporary redirect. Assuming it reduces the chances that the browser
// keeps the redirection in cache.
CRM_Utils_System::redirect($redirect_path);
}
}
/**
* Returns a plain text output for the e-mail report.
*/
function reporterror_civicrm_generatereport($site_name, $vars, $redirect_path) {
$output = ts('There was a CiviCRM error at %1.', array(1 => $site_name)) . "\n";
$output .= ts('Date: %1', array(1 => date('c'))) . "\n\n";
if ($redirect_path) {
$output .= ts("Error handling rules redirected the user to:") . "\n";
$output .= $redirect_path . "\n\n";
}
// Error details
if (function_exists('error_get_last')) {
$output .= "***ERROR***\n";
$output .= print_r(error_get_last(), TRUE);
}
$output .= print_r($vars, TRUE);
// User info
$session = CRM_Core_Session::singleton();
$userId = $session->get('userID');
$params = array(
'version' => 3,
'id' => $userId,
'return' => 'id,display_name,email',
);
$contact = civicrm_api('Contact', 'getsingle', $params);
$output .= "\n\n***LOGGED IN USER***\n";
$output .= _reporterror_civicrm_parse_array($contact);
// $_SERVER
$output .= "\n\n***SERVER***\n";
$output .= _reporterror_civicrm_parse_array($_SERVER);
// Backtrace
$backtrace = debug_backtrace();
$output .= "\n\n***BACKTRACE***\n";
foreach ($backtrace as $call) {
$output .= "**next call**\n";
$output .= _reporterror_civicrm_parse_array($call);
}
return $output;
}
/**
* Send the e-mail using drupal_mail()
*/
function reporterror_civicrm_send_mail($to, $subject, $output) {
if ($domain_id = CRM_Core_Config::domainID()) {
// Gather information from domain settings
$params = array('id' => $domain_id);
CRM_Core_BAO_Domain::retrieve($params, $domain);
unset($params['id']);
$locParams = $params + array('entity_id' => $domain_id, 'entity_table' => 'civicrm_domain');
$defaults = CRM_Core_BAO_Location::getValues($locParams);
$email_struct = reset(CRM_Utils_Array::value('email', $defaults));
$email = CRM_Utils_Array::value('email', $email_struct);
}
if (!$email) { // FIXME: Just in case ...
$email = '[email protected]';
}
$params = array(
'from' => $email,
'toName' => 'Site Administrator',
'toEmail' => $to,
'subject' => $subject,
'text' => $output,
);
if (!CRM_Utils_Mail::send($params)) {
//FIXME: Output an error message to log
}
}
/**
* 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 = '';
foreach ((array)$array as $key => $value) {
if (is_array($value) || is_object($value)) {
$value = print_r($value, TRUE);
}
$key = str_pad($key .':', 20, ' ');
$output .= $key . (string)_reporterror_civicrm_check_length($value) ." \n";
}
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 $item;
}