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

Contents of /contributions/modules/logwatcher/logwatcher.module

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


Revision 1.4 - (show annotations) (download) (as text)
Mon Sep 15 16:45:49 2008 UTC (14 months, 1 week ago) by deekayen
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +29 -32 lines
File MIME type: text/x-php
partial upgrade to Drupal 6
1 <?php
2 // $Id: logwatcher.module,v 1.3 2008/09/15 16:02:16 deekayen Exp $
3
4 /**
5 * @file
6 * Monitor watchdog entries and email administrators with updates.
7 */
8
9 /**
10 * Implementation of hook_perm().
11 */
12 function logwatcher_perm() {
13 return array('administer logwatcher');
14 }
15
16 /**
17 * Implementation of hook_help().
18 */
19 function logwatcher_help($path, $args) {
20 switch ($path) {
21 case 'admin/help#logwatcher':
22 return '<p>'. t('Track site defects by watching logs.') .'</p>';
23 }
24 }
25
26 /**
27 * Implementation of hook_menu().
28 */
29 function logwatcher_menu() {
30 $items = array();
31 $items['admin/logs/logwatcher'] = array(
32 'description' => 'Configure reports, the timeframe between reports, and the number of results to report.',
33 'title' => 'Log Watcher',
34 'page callback' => 'drupal_get_form',
35 'page arguments' => array('logwatcher_admin_settings'),
36 'access callback' => 'user_access',
37 'access arguments' => array('administer logwatcher'),
38 'type' => MENU_NORMAL_ITEM
39 );
40 $items['admin/logs/logwatcher/view/%'] = array(
41 'title' => 'Log Watcher Report',
42 'page callback' => 'logwatcher_report',
43 'page arguments' => array(4),
44 'access callback' => 'user_access',
45 'access arguments' => array('administer logwatcher'),
46 'type' => MENU_CALLBACK
47 );
48 $items['admin/logs/logwatcher/deactivate/%'] = array(
49 'title' => 'Deactivate Log Watcher Report',
50 'page callback' => 'drupal_get_form',
51 'page arguments' => array('logwatcher_deactivate', 4),
52 'access callback' => 'user_access',
53 'access arguments' => array('administer logwatcher'),
54 'type' => MENU_CALLBACK
55 );
56
57 return $items;
58 }
59
60 function logwatcher_deactivate_submit($form_id, $form_values) {
61 if (is_numeric($form_values['id'])) {
62 db_query('UPDATE {logwatcher_settings} SET active = 0 WHERE id = %d', $form_values['id']);
63 drupal_set_message(t('Report deactivated!'));
64 }
65 else {
66 drupal_set_message(t('Error deactivating report'));
67 }
68 drupal_goto('admin/logs/logwatcher');
69 }
70
71 function logwatcher_deactivate($id) {
72 global $base_url;
73 $form = array();
74 $form['id'] = array(
75 '#type' => 'hidden',
76 '#value' => $id
77 );
78 $form['warning'] = array(
79 '#type' => 'item',
80 '#value' => t('Are you sure you wish to deactivate this report? Its data will no longer be accessible without manual database querying.')
81 );
82 $form['op'] = array(
83 '#type' => 'submit',
84 '#value' => 'Delete'
85 );
86 $form['cancel'] = array(
87 '#type' => 'item',
88 '#value' => '<a href="'. $base_url .'/admin/logs/logwatcher">'. t('Cancel') .'</a>'
89 );
90 return $form;
91 }
92
93 function logwatcher_report() {
94 $id = intval(arg(4));
95 if ($id > 0) {
96 $ts = db_result(db_query('SELECT MAX(run_timestamp) FROM {logwatcher_data} WHERE job_id = %d', $id));
97 if ($ts > 0) {
98 $headers = array(t('Message'), t('Count'), t('Last Referer'));
99 $data = array();
100 $result = db_query('SELECT message, num, last_referer FROM {logwatcher_data} WHERE job_id = %d AND run_timestamp = %d', $id, $ts);
101 while ($row = db_fetch_object($result)) {
102 $data[] = array(
103 $row->message,
104 $row->num,
105 $row->last_referer
106 );
107 }
108 print theme('page', theme('table', $headers, $data));
109 }
110 else {
111 print theme('page', t('No data for that report.'));
112 }
113 }
114 print theme('page', t('No data for that report.'));
115 }
116
117 function logwatcher_admin_settings() {
118 $form = array();
119
120 $types = array();
121 $result = db_query('SELECT DISTINCT type FROM {watchdog}');
122 while ($row = db_fetch_object($result)) {
123 $types[$row->type] = $row->type;
124 }
125
126 $form['new'] = array(
127 '#type' => 'fieldset'
128 );
129
130 $form['new']['event_type'] = array(
131 '#id' => 'event_type',
132 '#title' => t('Notice type'),
133 '#type' => 'select',
134 '#options' => $types,
135 '#description' => t('Select the type of log event this action will apply to.')
136 );
137
138 $form['new']['action_frequency'] = array(
139 '#id' => 'action_timeframe',
140 '#title' => t('Timeframe'),
141 '#type' => 'select',
142 '#options' => cronplus_periods(),
143 '#default_value' => 'weekly',
144 '#description' => t('Time period between reports.')
145 );
146
147 $form['new']['action_limit'] = array(
148 '#id' => 'action_limit',
149 '#title' => t('Number of Results'),
150 '#type' => 'textfield',
151 '#size' => 4,
152 '#default_value' => 25,
153 '#description' => t('Number of rows to return in the report.')
154 );
155
156 /* Reserve for later, in case we want to do non-cron, event-based actions.
157
158 $form['new']['action_type'] = array(
159 '#id' => 'action_type',
160 '#title' => t('Action type'),
161 '#type' => 'select',
162 '#options' => array('cron' => 'cron'),
163 '#description' => t('Select the system action this action will apply to.')
164 );
165
166 $form['new']['action'] = array(
167 '#id' => 'action',
168 '#title' => t('Action'),
169 '#type' => 'select',
170 '#options' => array('mail' => 'mail', 'display' => 'display'),
171 '#description' => t('Select the system action that will be invoked. If "mail," mail will be sent to users in the "administer logwatcher" role.')
172 );
173
174 $form['new']['action_display'] = array(
175 '#id' => 'display',
176 '#title' => 'Display content',
177 '#type' => 'textarea',
178 '#description' => t('If "display" selected above, what text should be displayed for this event?')
179 );
180 */
181
182 $output = system_settings_form($form);
183 $headers = array(t('Type'), t('Timeframe'), t('Rows'), t('Last Run'), t('View'), t('Deactivate'));
184 $data = array();
185
186 $result = db_query("SELECT id, type, timeframe, rows, last_run, DATE_FORMAT(FROM_UNIXTIME(last_run), '%s') last FROM {logwatcher_settings} WHERE active = 1", "%m/%d/%Y %H:%i:%s");
187 global $base_url;
188 while ($row = db_fetch_object($result)) {
189 if ($row->last_run == 0) {
190 $row->last = t('Never');
191 $report = t('No report yet');
192 }
193 else {
194 $report = l(t('Last Report'), $base_url .'/admin/logs/logwatcher/view/'. intval($row->id));
195 }
196 $data[] = array(
197 $row->type,
198 $row->timeframe,
199 $row->rows,
200 $row->last,
201 $report,
202 '<a href="'. $base_url .'/admin/logs/logwatcher/deactivate/'. intval($row->id) .'">'. t('Deactivate') .'</a>'
203 );
204 }
205
206 $output['existing'] = array(
207 '#type' => 'fieldset'
208 );
209 $output['existing']['table'] = array(
210 '#type' => 'item',
211 '#value' => theme('table', $headers, $data)
212 );
213
214 return $output;
215 }
216
217 function logwatcher_admin_settings_submit($form_id, $form_values) {
218 db_query("INSERT INTO {logwatcher_settings} (type, timeframe, rows, last_run) VALUES('%s', '%s', %d, 0)", $form_values['event_type'], $form_values['action_frequency'], $form_values['action_limit']);
219 drupal_set_message('Settings saved!');
220 }
221
222 /**
223 * Implementation of hook_cronplus().
224 *
225 * Central function for processing the cron calls from cronplus.
226 */
227 function logwatcher_cronplus($period, $now, $last_cron, $last_this) {
228
229 global $base_url;
230 $recipients = array();
231 $result = db_query("SELECT u.mail FROM {users} u JOIN {users_roles} ur ON u.uid = ur.uid JOIN {permission} p ON ur.rid = p.rid WHERE p.perm LIKE '%administer logwatcher%'");
232 while ($recipient = db_fetch_object($result)) {
233 $recipients[] = $recipient->mail;
234 }
235 $recipients = join(', ', $recipients);
236
237 $results = db_query("SELECT id, type, timeframe, rows FROM {logwatcher_settings} WHERE timeframe = '%s'", $period);
238 while ($row = db_fetch_object($results)) {
239 $res = db_query("SELECT message, COUNT(*) AS cnt, referer FROM {watchdog} WHERE type = '%s' AND DATEDIFF(NOW(), FROM_UNIXTIME(timestamp)) <= %d GROUP BY message ORDER BY cnt DESC, timestamp DESC LIMIT %d", $row->type, $row->timeframe, $row->rows);
240 $subject = 'Logwatcher results for '. $row->type;
241 $body = t('This is a logwatcher report. You can always find the latest version of this report online at !url', array('!url' => $base_url .'/admin/logs/logwatcher/view/'. intval($row->id)));
242 $body .= "\n\n";
243 while ($log = db_fetch_object($res)) {
244 $body .= $log->message ."\t". $log->cnt ."\t". $log->referer ."\n";
245 db_query("INSERT INTO {logwatcher_data} (job_id, message, num, last_referer, run_timestamp, run_timeframe) VALUES (%d, '%s', %d, '%s', %d, %d)", $row->id, $log->message, $log->cnt, $log->referer, $now, $row->timeframe);
246 }
247 db_query('UPDATE {logwatcher_settings} SET last_run = %d WHERE id = %d', $now, $row->id);
248
249 // Some watchdog entries have HTML in them. The Drupal 6
250 // drupal_html_to_text() function removes the \n newlines
251 // so for now, strip_tags() is the solution for making emails with HTML more readable.
252 drupal_mail('logwatcher_'. $row->type, $recipients, $subject, strip_tags($body), variable_get('site_mail', NULL));
253 }
254 }

  ViewVC Help
Powered by ViewVC 1.1.2