| 1 |
<?php
|
| 2 |
|
| 3 |
// $Id: jserrorlog.module,v 1.2 2008/12/05 00:44:55 xuriz Exp $
|
| 4 |
|
| 5 |
/**
|
| 6 |
* Implement hook_menu()
|
| 7 |
*
|
| 8 |
* We add two menu entries; one for system settings, and one for the
|
| 9 |
* form to post to.
|
| 10 |
*
|
| 11 |
* We also add the JS code file for this module.
|
| 12 |
*
|
| 13 |
* @todo The postback URI should not be a standard Drupal path; it
|
| 14 |
* should go to xmlrpc.php instead, in order to avoid conflicting with
|
| 15 |
* any other nodes which occupy the same space.
|
| 16 |
*
|
| 17 |
*/
|
| 18 |
function jserrorlog_menu( $may_cache ) {
|
| 19 |
if ( $may_cache ) {
|
| 20 |
$items[] = array(
|
| 21 |
'path' => 'jserrorlog',
|
| 22 |
'description' => t('Adjust settings for the dropdown Administration Menu.'),
|
| 23 |
'callback' => 'jserrorlog_log_error',
|
| 24 |
'access' => true,
|
| 25 |
'type' => MENU_CALLBACK,
|
| 26 |
);
|
| 27 |
$items[] = array(
|
| 28 |
'path' => 'admin/settings/jserrorlog',
|
| 29 |
'title' => t('JS Error Logging'),
|
| 30 |
'description' => t('Record clientside JS errors on your site'),
|
| 31 |
'callback' => 'drupal_get_form',
|
| 32 |
'callback arguments' => array( 'jserrorlog_admin_settings' ),
|
| 33 |
'access' => user_access( 'administer site configuration' ),
|
| 34 |
) ;
|
| 35 |
return $items ;
|
| 36 |
} else {
|
| 37 |
drupal_add_js( drupal_get_path('module', 'jserrorlog') . '/jserrorlog.js' );
|
| 38 |
}
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Callback function which
|
| 43 |
*
|
| 44 |
* We return some plaintext (which happens to be JSON), perform any
|
| 45 |
* actions required by the logging settings, and then die().
|
| 46 |
*
|
| 47 |
* @todo I'm sure there's a better way to exit Drupal than using
|
| 48 |
* die(). Probably moving this from a Drupal path to xmlrpc.php would
|
| 49 |
* resolve this issue - mostly I'm just avoiding handling back a
|
| 50 |
* themed page.
|
| 51 |
*
|
| 52 |
* @todo investigate whether a crafted POST to the handler could be
|
| 53 |
* exploited in the email log method
|
| 54 |
*
|
| 55 |
* @todo better formatting for the outputs
|
| 56 |
*
|
| 57 |
* @todo don't send any message unless there's really been a submitted
|
| 58 |
* POST
|
| 59 |
*/
|
| 60 |
function jserrorlog_log_error() {
|
| 61 |
$methods = variable_get( 'jserrorlog_methods', array() ) ;
|
| 62 |
if ( $methods == array() ) {
|
| 63 |
die( "['error logging disabled']" ) ;
|
| 64 |
}
|
| 65 |
global $user ;
|
| 66 |
// if we get here, then we will compose a suitable message
|
| 67 |
$error_details = array(
|
| 68 |
'info' => array(
|
| 69 |
'Page' => $_SERVER['HTTP_REFERER'],
|
| 70 |
'File' => $_POST['file'],
|
| 71 |
'Line' => $_POST['line'],
|
| 72 |
'Msg ' => $_POST['message'],
|
| 73 |
),
|
| 74 |
'user' => array(
|
| 75 |
'name' => $user->name,
|
| 76 |
'mail' => $user->mail,
|
| 77 |
'session' => $user->session
|
| 78 |
),
|
| 79 |
'post' => $_POST,
|
| 80 |
'request' => $_REQUEST,
|
| 81 |
'server' => $_SERVER
|
| 82 |
) ;
|
| 83 |
$error_body = print_r( $error_details, 1 ) ;
|
| 84 |
$html_msg = "JavaScript error recorded:<pre>" . $error_body . "</pre>" ;
|
| 85 |
if ( in_array( 'email', $methods ) ) {
|
| 86 |
$txt_msg = "Javascript error recorded. Debug data follows:\n\n" . $error_body ;
|
| 87 |
$recipient = variable_get( 'jserrorlog_mail_address', variable_get( 'site_mail', '' ) ) ;
|
| 88 |
$subject = variable_get( 'site_name', '' ) . ' JS error report' ;
|
| 89 |
drupal_mail( 'jserrorlog', $recipient, $subject, $txt_msg ) ;
|
| 90 |
}
|
| 91 |
if ( in_array( 'watchdog', $methods ) ) {
|
| 92 |
watchdog( 'js error', $html_msg ) ;
|
| 93 |
}
|
| 94 |
die("['thanks']");
|
| 95 |
}
|
| 96 |
|
| 97 |
/**
|
| 98 |
* Callback function to generate the admin settings form.
|
| 99 |
*
|
| 100 |
* Provide facility to override the email address to send reports to
|
| 101 |
* (defaults to site address) and allow people to choose between
|
| 102 |
* watchdog and email logging methods.
|
| 103 |
*/
|
| 104 |
function jserrorlog_admin_settings() {
|
| 105 |
$log_methods = array(
|
| 106 |
'watchdog' => 'Use site logs',
|
| 107 |
'email' => 'Use email'
|
| 108 |
) ;
|
| 109 |
$form['jserrorlog_methods'] = array(
|
| 110 |
'#type' => 'checkboxes',
|
| 111 |
'#title' => t('Record JS errors via'),
|
| 112 |
'#default_value' => variable_get( 'jserrorlog_methods', array() ),
|
| 113 |
'#options' => $log_methods,
|
| 114 |
'#description' => t('Which methods you wish to use for recording JS errors'),
|
| 115 |
) ;
|
| 116 |
$form['jserrorlog_mail_address'] = array(
|
| 117 |
'#type' => 'textfield',
|
| 118 |
'#title' => t('Email to'),
|
| 119 |
'#default_value' => variable_get( 'jserrorlog_mail_address', variable_get( 'site_mail', '' ) ),
|
| 120 |
'#description' => t('Email address to send JS errors to (if enabled)'),
|
| 121 |
) ;
|
| 122 |
return system_settings_form($form) ;
|
| 123 |
}
|