diff --git a/php_errors.info b/php_errors.info index cceaf9c..5f56f9b 100644 --- a/php_errors.info +++ b/php_errors.info @@ -1,22 +1,6 @@ -; $Id: php_errors.info,v 1.8 2010/07/07 18:26:56 ptalindstrom Exp $ name = PHP Errors description = Admin tool to help monitor PHP errors which occur on the site. package = Administration -version = 6.x-1.0 dependencies[] = token dependencies[] = cronplus - core = 6.x -; Information added by drupal.org packaging script on 2010-02-17 -version = "6.x-1.0" -core = "6.x" -project = "php_errors" -datestamp = "1266446708" - - -; Information added by drupal.org packaging script on 2010-11-08 -version = "6.x-1.3" -core = "6.x" -project = "php_errors" -datestamp = "1289187946" - diff --git a/php_errors.install b/php_errors.install index d60d848..bbbe5b0 100644 --- a/php_errors.install +++ b/php_errors.install @@ -49,7 +49,7 @@ function php_errors_uninstall() { drupal_uninstall_schema('php_errors'); // remove variables - $result = update_sql("DELETE FROM {variable} WHERE name like 'php_errors_%'"); + $result = update_sql("DELETE FROM {variable} WHERE name LIKE 'php_errors_%'"); if ($result['success']) { drupal_set_message(t('The PHP Errors module was successfully uninstalled.')); diff --git a/php_errors.module b/php_errors.module index 1118840..39798ee 100644 --- a/php_errors.module +++ b/php_errors.module @@ -6,9 +6,7 @@ function php_errors_help($path='', $arg) { switch ($path) { case 'admin/modules#description': - return t("This module helps to manage your site's PHP Errors. It performs 3 functions: it provides a summary page of the - watchdog's PHP errors, it archive's this summary and it may be set to email a summary report to specific email addresses - or roles."); + return '

' . t("This module helps to manage your site's PHP Errors. It performs 3 functions: it provides a summary page of the watchdog's PHP errors, it archive's this summary and it may be set to email a summary report to specific email addresses or roles.") . '

'; } } @@ -69,7 +67,9 @@ function php_errors_menu() { return $items; } - +/** + * Menu callback - admin settings form. + */ function php_errors_admin_settings() { // system settings $form['php_errors_notify_addresses'] = array( @@ -96,18 +96,18 @@ function php_errors_admin_settings() { return system_settings_form($form); } +/** + * Menu callback - show detail of the php error report. + */ function _php_errors_show($type = NULL) { $rows = array(); - $results = db_query("SELECT count(1) cnt, timestamp ts, message ms, variables v, location - FROM watchdog WHERE type = 'php' + $results = db_query("SELECT COUNT(wid) cnt, timestamp ts, message ms, variables v, location + FROM {watchdog} WHERE type = 'php' GROUP by MID(v, 62, 30) ORDER BY timestamp DESC, cnt DESC"); // and if results - lets print them out in a nice table if ($type == 'links') { - $header = array( - array('data' => t('Count')), - array('data' => t('Location')), - ); + $header = array(t('Count'), t('Location')); while ($result = db_fetch_object($results)) { $row = array(); $row[] = $result->cnt; @@ -116,15 +116,10 @@ function _php_errors_show($type = NULL) { } } else { - $header = array( - array('data' => t('Count')), - array('data' => t('Date')), - array('data' => t('Error')), - array('data' => t('Location')), - ); + $header = array(t('Count'), t('Date'), t('Error'), t('Location')); while ($result = db_fetch_object($results)) { $variables = unserialize($result->v); - $error = str_replace(array('%message', '%file', '%line'), array($variables['%message'], $variables['%file'], $variables['%line']), "%message in %file on line %line."); + $error = t("%message in %file on line %line.", $variables); $row = array(); $row[] = $result->cnt; $row[] = date("m-d-Y", $result->ts); @@ -137,9 +132,12 @@ function _php_errors_show($type = NULL) { return theme('table', $header, $rows); } +/** + * Helper function for email report. + */ function _php_errors_email() { - $results = db_query("SELECT count(1) cnt, timestamp ts, message ms, location - FROM watchdog WHERE type = 'php' + $results = db_query("SELECT COUNT(wid) cnt, timestamp ts, message ms, location + FROM {watchdog} WHERE type = 'php' GROUP by LEFT(ms, 30) ORDER BY timestamp DESC, cnt DESC"); // and if results - lets format them as a table for a txt email @@ -156,22 +154,21 @@ function _php_errors_email() { $rows .= strip_tags(html_entity_decode($result->ms, ENT_QUOTES)) ."\n\n"; } - $preamble = "The following is this weeks PHP Error Summary Report.\n\nRegards,\n\n" . variable_get('site_name', 'Your Site'); + $preamble = t("The following is this weeks PHP Error Summary Report.") . "\n\n"; + $preamble .= t("Regards,\n\n@sitename", array('@sitename' => variable_get('site_name', t('Your Site')))); return $preamble . $header . $rows; } +/** + * Menu callback - show list of archived PHP reports. + */ function _php_errors_show_archive() { $results = db_query("SELECT * FROM {php_errors_archive} ORDER BY created DESC"); - $header = array( - array('data' => t('Archived Reports')), - array('' => ""), - array('' => ""), - array('' => "") - ); + $header = array(t('Archived Reports'), '', '', ''); - $row = 1; + $row = 1; $col = 1; while ($report = db_fetch_object($results)) { if ($col > 4) { @@ -190,21 +187,31 @@ function _php_errors_show_archive() { return theme('table', $header, $reports); } -// Show an archived report +/** + * Show an archived report. + */ function _php_errors_archive_view($rid) { - $report = db_fetch_object(db_query("SELECT * FROM {php_errors_archive} WHERE rid = %d", $rid)); - - drupal_set_title(t("PHP Errors") ." - Archived Report for: ". date("M d, Y", $report->created) .""); + $output = ''; - $output = ''; - $output .= "

". $report->report; + if ($report = db_fetch_object(db_query("SELECT * FROM {php_errors_archive} WHERE rid = %d", $rid))) { + drupal_set_title(t('PHP Errors - Archived report for: %date', array('%date' => date('M d, Y', $report->created)))); + $output .= "

"; + $output .= $report->report; + } + else { + drupal_set_message(t("The report wasn't found."), 'warning'); + } return $output; } -/* Uses Cronplus module to do a couple weekly tasks: - * - send email notifications to selected users (frequency based on frequency setting) - * - archive the error report (weekly) +/* Uses CronPLUS module to do a couple weekly tasks: +* - send email notifications to selected users (frequency based on frequency setting) +* - archive the error report (weekly) +*/ + +/** + * Implements hook_cronplus_daily(). */ function php_errors_cronplus_daily($now, $last_cron, $last_this) { if (variable_get('php_errors_notify_frequency', 0) == 1) { @@ -212,9 +219,13 @@ function php_errors_cronplus_daily($now, $last_cron, $last_this) { } } +/** + * Implements hook_cronplus_weekly(). + */ function php_errors_cronplus_weekly($now, $last_cron, $last_this) { + // default to weekly if not set if (variable_get('php_errors_notify_frequency', 2) == 2) { - _php_errors_send_notifications(); // default to weekly if not set + _php_errors_send_notifications(); } // archive the report @@ -222,16 +233,21 @@ function php_errors_cronplus_weekly($now, $last_cron, $last_this) { db_query("INSERT INTO {php_errors_archive} (report, created) VALUES ('%s', %d)", $report, time()); } +/** + * Implements hook_cronplus_monthly(). + */ function php_errors_cronplus_monthly($now, $last_cron, $last_this) { if (variable_get('php_errors_notify_frequency', 0) == 3) { _php_errors_send_notifications(); } } -// email summary report to selected users +/** + * Email summary report to selected users. + */ function _php_errors_send_notifications() { $site_name = variable_get('site_name', 'Your Site'); - $params['from'] = "$site_name <". variable_get('site_mail', ini_get('sendmail_from')) .'>';; + $params['from'] = "$site_name <". variable_get('site_mail', ini_get('sendmail_from')) .'>'; // pull list of all user in selected roles $users = _php_errors_get_roles_emails(); @@ -239,21 +255,28 @@ function _php_errors_send_notifications() { // and add individual csv list of addresses // if there are any individual emails address; lets use those // if none; lets at least send to site admin - if (!$ind_emails = variable_get('php_errors_notify_addresses', '')) $ind_emails = variable_get('site_mail', ''); - $single_emails = split(",", $ind_emails); - if (count($single_emails)) { + if (!$ind_emails = variable_get('php_errors_notify_addresses', '')) { + $ind_emails = variable_get('site_mail', ''); + } + $single_emails = explode(",", $ind_emails); + if (!empty($single_emails)) { foreach ($single_emails as $single) { - if (valid_email_address(trim($single))) $users[trim($single)] = trim($single); + $email_trimmed = trim($single); + if (valid_email_address($email_trimmed)) { + $users[$email_trimmed] = $email_trimmed; + } } } $users = array_unique($users); - if (!count($users)) break; + if (empty($users)) { + return; + } // create single CSV list of addresses foreach ($users as $name => $email) { $emails[] = $name . " <". $email .">"; } - $recipients = join(",", $emails); + $recipients = implode(",", $emails); $params['subject'] = t("PHP Error Summary Report"); $params['body'] = _php_errors_email(); @@ -266,28 +289,24 @@ function _php_errors_send_notifications() { } } -// get array of all email addresses for each user in selected roles +/** + * Return array of all email addresses for each user in selected roles. + */ function _php_errors_get_roles_emails() { $emails = array(); - $roles = variable_get('php_errors_notify_roles', 0); - if (!$roles) return; - - $included = array_filter($roles); - - if (count($included) > 1) { - foreach ($included as $role) $rolesql[] = " ur.rid = $role "; - $roles_where = join(" OR ", $rolesql); - } - else { - $roles_where = " ur.rid = ". current($included); - } - - $result = db_query("SELECT u.uid, u.name, u.mail, u.status FROM {users} u - INNER JOIN {users_roles} ur ON u.uid=ur.uid WHERE $roles_where AND u.status = 1"); - - while ($u = db_fetch_object($result)) { - $emails[$u->name] = $u->mail; + $roles_raw = variable_get('php_errors_notify_roles', 0); + if (!empty($roles_raw) && is_array($roles_raw)) { + $roles = array_filter($roles_raw); + if (!empty($roles)) { + $roles_placeholders = db_placeholders($roles); + $result = db_query("SELECT u.uid, u.name, u.mail, u.status FROM {users} u + INNER JOIN {users_roles} ur ON u.uid = ur.uid + WHERE u.status = 1 AND ur.rid IN ($roles_placeholders)", $roles); + while ($user = db_fetch_object($result)) { + $emails[$user->name] = $user->mail; + } + } } return $emails;