/[drupal]/contributions/modules/civicrm_error/civicrm_error.module
ViewVC logotype

Diff of /contributions/modules/civicrm_error/civicrm_error.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1.2.5, Wed Sep 19 22:40:03 2007 UTC revision 1.1.2.6, Tue Jun 30 13:35:48 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2    
3  // $Id: civicrm_error.module,v 1.1.2.4 2007/07/25 22:21:58 dalin Exp $  // $Id: civicrm_error.module,v 1.1.2.5 2007/09/19 22:40:03 dalin Exp $
4    
5    /**
6     * @file
7     * Module to capture errors from CiviCRM
8     */
9    
10  /**  /**
11   *  Custom error function   *  Custom error function
12   *  Set CiviCRM » Administer CiviCRM » Global Settings » Debugging » Fatal Error Handler   *  Set CiviCRM » Administer CiviCRM » Global Settings » Debugging » Fatal Error Handler
13   *  To use this function   *  To use this function.
14   */   */
15  function civicrm_error_handler($civicrm_error) {  function civicrm_error_handler($civicrm_error) {
16    
17    $site = variable_get('site_name', 'drupal');    $site = variable_get('site_name', 'drupal');
18    
19    // generate output    // Generate output.
20    $output .= 'There was a CiviCRM error at '. $site .".\n";    $output = 'There was a CiviCRM error at '. check_plain($site) .".\n";
21    
22    // civicrm error details    // CiviCRM error details.
23    if ($civicrm_error) {    if ($civicrm_error) {
24      $output .= "\n\n\n\n***CIVICRM ERROR***\n";      $output .= "\n\n\n\n\n============================ CIVICRM ERROR ============================\n";
25      $output .= _civicrm_error_parse_array($civicrm_error);      $output .= _civicrm_error_parse_array($civicrm_error);
26    }    }
27    
28    $output .= "\n\n\n\n\n***PHP ERROR***\n";    // PHP error details.
   // php error details  
29    if (function_exists('error_get_last')) {    if (function_exists('error_get_last')) {
30      $output .= _civicrm_error_parse_array(error_get_last());      $output .= "\n\n\n\n\n============================ PHP ERROR ============================\n";
31    }      $output .= _civicrm_error_parse_array(check_plain(error_get_last()));
   elseif ($php_errormsg) {  
     $output .= _civicrm_error_parse_array($php_errormsg);  
32    }    }
33    
34    // user info    // User info.
35    global $user;    global $user;
36    $output .= "\n\n\n\n\n***USER***\n";    $output .= "\n\n\n\n\n============================ USER ============================\n";
37    $output .= _civicrm_error_parse_array($user);    $output .= _civicrm_error_parse_array($user);
38    
39    // $_SERVER    // $_SERVER.
40    $output .= "\n\n\n\n\n***SERVER***\n";    $output .= "\n\n\n\n\n============================ SERVER ============================\n";
41    $output .= _civicrm_error_parse_array($_SERVER);    $output .= _civicrm_error_parse_array($_SERVER);
42    
43      // $_SESSION.
44      $output .= "\n\n\n\n\n============================ SESSION ============================\n";
45      $output .= _civicrm_error_parse_array($_SESSION);
46    
47    // $_SESSION    // $_SESSION
48    $output .= "\n\n\n\n\n***SESSION***\n";    $output .= "\n\n\n\n\n***SESSION***\n";
49    $output .= _civicrm_error_parse_array($_SESSION);    $output .= _civicrm_error_parse_array($_SESSION);
50    
51    // $_REQUEST    // $_REQUEST
52    $output .= "\n\n\n\n\n***REQUEST***\n";    $output .= "\n\n\n\n\n============================ REQUEST ============================\n";
53    $output .= _civicrm_error_parse_array($_REQUEST);    $output .= _civicrm_error_parse_array($_REQUEST);
54    
55    // backtrace    // backtrace
56    $backtrace = debug_backtrace();    $backtrace = debug_backtrace();
57    $output .= "\n\n\n\n\n***BACKTRACE***\n";    $output .= "\n\n\n\n\n============================ BACKTRACE ============================\n";
58    foreach ($backtrace as $call) {    foreach ($backtrace as $call) {
59      $output .= "\n\n**next call**\n";      $output .= "\n\n ------------------------------- next call ------------------------------- \n";
60      $output .= _civicrm_error_parse_array($call);      $output .= _civicrm_error_parse_array($call);
61    }    }
62    
63    // send email    // Log / send error.
64    $subject = 'CiviCRM error at '. $site;    $subject = 'CiviCRM error at '. $site;
65    $to = variable_get('civicrm_error_to', variable_get("site_mail", ini_get("sendmail_from")));    $to = variable_get('civicrm_error_to', variable_get("site_mail", ini_get("sendmail_from")));
66    drupal_mail('civicrm_error', $to, $subject, $output);    $errors_via = variable_get('civicrm_error_via', 'both');
67      if ($errors_via == 'both' || $errors_via == 'watchdog') {
68        watchdog('civicrm_error', t('CiviCRM Error: <pre>!output</pre>', array('!output' => $output)),
69          WATCHDOG_ERROR);
70      }
71      if ($errors_via == 'both' || $errors_via == 'email') {
72        drupal_mail('civicrm_error', $to, $subject, $output);
73      }
74    
75  }  }
76    
77  /**  /**
78   *  Helper function to return a pretty print of the given array   *  Helper function to return a pretty print of the given array.
79     *
80     *  @param array $array
81     *    The array to print out.
82     *  @return string
83     *    The printed array.
84   */   */
85  function _civicrm_error_parse_array($array) {  function _civicrm_error_parse_array($array) {
86    // remove cc numbers    $output = '';
87    $pattern = '/[0-9]{12}/';    foreach ((array)$array as $key => $value) {
   $replace = 'xxxxxxxxxxxx';  
   foreach((array)$array as $key => $value) {  
88      if (is_array($value) || is_object($value)) {      if (is_array($value) || is_object($value)) {
89        $value = print_r($value, true);        $value = print_r($value, TRUE);
90      }      }
91      $key = str_pad($key .':', 20, ' ');      $key = str_pad($key .':', 20, ' ');
92      $line = $key . (string)_civicrm_error_check_length($value) ." \n";      $output .= $key . (string)_civicrm_error_check_length($value) ." \n";
     $output .= preg_replace($pattern, $replace, $line);  
   
93    }    }
94    
95      // HTML may be useful to us, but we don't want to expose an security holes,
96      // so we force tags to be escaped.
97      // Also remove cc numbers.
98      $pattern = '/[0-9]{12}/';
99      $replace = 'xxxxxxxxxxxx';
100      $output = _filter_html(preg_replace($pattern, $replace, $output), NULL);
101    
102    return $output ."\n";    return $output ."\n";
103  }  }
104    
105  /**  /**
106   *  Helper function to add elipses and return spaces if null   *  Helper function to add elipses and return spaces if null.
107     *
108     *  @param string $item
109     *    String to check.
110     *  @return string
111     *    The truncated string.
112   */   */
113  function _civicrm_error_check_length($item) {  function _civicrm_error_check_length($item) {
114    if (is_null($item)) {    if (is_null($item)) {
# Line 100  function civicrm_error_menu($may_cache) Line 127  function civicrm_error_menu($may_cache)
127    $items = array();    $items = array();
128    if ($may_cache) {    if ($may_cache) {
129      $items[] = array(      $items[] = array(
130        'path'                => 'admin/logs/civicrm_error',        'path' => 'admin/logs/civicrm_error',
131        'title'               => t('CiviCRM Error Handler'),        'title' => t('CiviCRM Error Handler'),
132        'description'         => t('Email critical CiviCRM errors.'),        'description' => t('Email critical CiviCRM errors.'),
133        'callback'            => 'drupal_get_form',        'callback' => 'drupal_get_form',
134        'callback arguments'  => 'civicrm_error_settings',        'callback arguments' => 'civicrm_error_settings',
135        'access'              => user_access('administer site configuration'),        'access' => user_access('administer site configuration'),
136        'type'                => MENU_NORMAL_ITEM,        'type' => MENU_NORMAL_ITEM,
137      );      );
138    }    }
139    return $items;    return $items;
140  }  }
141    
142    
143  //  Settings page  /**
144     * Settings page.
145     *
146     * @return array
147     *  FAPI form.
148     */
149  function civicrm_error_settings() {  function civicrm_error_settings() {
150    $form['civicrm_error_to'] = array(    $form['civicrm_error_to'] = array(
151      '#type' => 'textfield',      '#type' => 'textfield',
# Line 123  function civicrm_error_settings() { Line 155  function civicrm_error_settings() {
155      '#description' => t("Select an email address to send all CiviCRM errors to."),      '#description' => t("Select an email address to send all CiviCRM errors to."),
156      '#required' => TRUE,      '#required' => TRUE,
157    );    );
158      $form['civicrm_error_via'] = array(
159        '#type' => 'select',
160        '#title' => t('Errors via'),
161        '#options' => array(
162          'both' => 'Email and Watchdog',
163          'watchdog' => 'Watchdog only',
164          'email' => 'Email only'
165        ),
166        '#default_value' => variable_get('civicrm_error_via', 'both'),
167        '#description' => t("Method for error delivery."),
168      );
169    return system_settings_form($form);    return system_settings_form($form);
170  }  }

Legend:
Removed from v.1.1.2.5  
changed lines
  Added in v.1.1.2.6

  ViewVC Help
Powered by ViewVC 1.1.2