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

Contents of /contributions/modules/spam_tokens/spam_tokens.module

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


Revision 1.3 - (show annotations) (download) (as text)
Wed Nov 28 16:54:44 2007 UTC (2 years ago) by nancyw
Branch: MAIN
CVS Tags: DRUPAL-5--2-1, HEAD
Branch point for: DRUPAL-5--2, DRUPAL-5
Changes since 1.2: +96 -96 lines
File MIME type: text/x-php
Coder fixes
1 <?php
2 // $Id: spam_tokens.module,v 1.2 2007/11/28 13:03:15 nancyw Exp $
3
4 /**
5 * Implementation of hook_help().
6 */
7 function spam_tokens_help($path) {
8 switch ($path) {
9 case 'admin/help#spam_tokens':
10 return '<p>'. t("The spam_token module allows Spam module administrators to examine and modify the tokens that are used to identify spam.") .'</p>';
11
12 case 'admin/settings/spam/tokens':
13 return '<big><p>'. t('This page allows you to examine and modify the tokens that are used to identify spam. The spam module is set to examine the !interest most "interesting" tokens in the content.', array('!interest' => variable_get('spam_interesting_tokens', 15))) .'</p></big>';
14 }
15 }
16
17 /**
18 * Implementation of hook_menu().
19 */
20 function spam_tokens_menu($may_cache) {
21 $items = array();
22
23 if ($may_cache) {
24 $items[] = array(
25 'path' => 'admin/settings/spam/tokens',
26 'title' => t('Tokens'),
27 'access' => user_access('administer spam'),
28 'callback' => 'spam_tokens_page',
29 'description' => t('Allows Spam module administrators to examine and modify tokens.'),
30 'weight' => 5,
31 'type' => MENU_LOCAL_TASK,
32 );
33 }
34 else {
35 drupal_add_css(drupal_get_path('module', 'spam_tokens') .'/spam_tokens.css');
36 $items[] = array(
37 'path' => 'admin/settings/spam/tokens/add',
38 'title' => t('Spam Token add'),
39 'access' => user_access('administer spam'),
40 'callback' => 'spam_tokens_add',
41 'type' => MENU_CALLBACK,
42 );
43
44 $items[] = array(
45 'path' => 'admin/settings/spam/tokens/add',
46 'title' => t('Spam Token add'),
47 'access' => user_access('administer spam'),
48 'callback' => 'spam_tokens_add',
49 'type' => MENU_CALLBACK,
50 );
51
52 $items[] = array(
53 'path' => 'admin/settings/spam/tokens/import',
54 'title' => t('Spam Token import'),
55 'access' => user_access('administer spam'),
56 'callback' => 'drupal_get_form',
57 'callback arguments' => 'spam_tokens_import',
58 'type' => MENU_CALLBACK,
59 );
60
61 $items[] = array(
62 'path' => 'admin/settings/spam/tokens/export',
63 'title' => t('Spam Token export'),
64 'access' => user_access('administer spam'),
65 'callback' => 'drupal_get_form',
66 'callback arguments' => array('spam_tokens_export'),
67 'type' => MENU_CALLBACK,
68 );
69
70 $items[] = array(
71 'path' => 'admin/settings/spam/tokens/action',
72 'title' => t('Spam Token actions'),
73 'access' => user_access('administer spam'),
74 'callback' => 'spam_tokens_actions',
75 'type' => MENU_CALLBACK,
76 );
77 }
78 return $items;
79 }
80
81 /* Implementation of hook_spam
82 * This function sets the tab name for the settings intro page.
83 */
84 function spam_tokens_spam($name, $arg1, $arg2, $arg3) {
85 // Do stuff based on the hook type (name).
86 switch ($name) {
87 case 'tab_description':
88 $tabs = array();
89 $tabs['Tokens'] = t('Administer tokens for the spam module.');
90
91 return $tabs;
92 //
93 // case 'advanced_settings':
94 // return array();
95
96 default:
97 return array();
98 }
99 }
100
101 function spam_tokens_action_form() {
102 $form['#action'] = url('admin/settings/spam/tokens/action');
103
104 $form['new_tokens'] = array(
105 '#type' => 'fieldset',
106 // '#title' => t('Add new tokens'),
107 '#collapsible' => FALSE,
108 '#collapsed' => FALSE,
109 '#description' => t('Use this button to add a new token to the filter list.'),
110 // '#prefix' => '<br/>',
111 // '#suffix' => '<br/>',
112 );
113
114 $form['new_tokens']['add_token'] = array(
115 '#type' => 'button',
116 '#button_type' => spam_tokens_op,
117 '#name' => 'admin/settings/spam/tokens/add',
118 '#value' => t('Add token'),
119 '#suffix' => '<br/>',
120 );
121
122 $form['specials'] = array(
123 '#type' => 'fieldset',
124 '#title' => t('Special Token List'),
125 '#collapsible' => TRUE,
126 '#collapsed' => FALSE,
127 '#description' => t('"Special" tokens are inconsequential words (e.g. "the", "and", "for") that should be ignored by the spam filters. These lists may be shared by importing or exporting the list.'),
128 '#prefix' => '<br/>',
129 '#suffix' => '<br/>',
130 );
131
132 $form['specials']['import'] = array(
133 '#type' => 'button',
134 '#button_type' => spam_tokens_op,
135 '#name' => 'admin/settings/spam/tokens/import',
136 '#value' => t('Import'),
137 // '#prefix' => '<br/>',
138 );
139
140 $form['specials']['export'] = array(
141 '#type' => 'button',
142 '#button_type' => spam_tokens_op,
143 '#name' => 'admin/settings/spam/tokens/export',
144 '#value' => t('Export'),
145 );
146
147 return $form;
148 }
149
150 function spam_tokens_actions($x1=null, $x2=null, $x3=null) {
151 foreach ($_POST as $key => $value) {
152 if (in_array($value, array('Add token', 'Import', 'Export', 'Edit'))) {
153 drupal_goto($key);
154 }
155 }
156 }
157
158 function spam_tokens_import() {
159 $form['token_list'] = array(
160 '#type' => 'textarea',
161 '#title' => t('Tokens to be added'),
162 '#cols' => 10,
163 '#rows' => 20,
164 '#required' => TRUE,
165 '#description' => t('Enter the tokens separated by commas or new lines. These tokens will be ignored by the spam filters.'),
166 '#prefix' => '<div class="spam_tokens_import">',
167 '#suffix' => '</div>',
168 );
169
170 $form['add']['attach'] = array(
171 '#type' => 'submit',
172 '#value' => t('Add'),
173 '#weight' => 3
174 );
175
176 return $form;
177 }
178
179 function spam_tokens_import_submit($form_id, $form_values) {
180 // First, turn new lines into commas.
181 $input = str_replace("\n", ',', $form_values['token_list']);
182 $input = str_replace("\r", ',', $input);
183
184 // Now, turn spaces into commas.
185 $input = str_replace(' ', ',', $input);
186
187 // Now, remove extra commas.
188 $input = str_replace(',,', ',', $input);
189 $input = str_replace(',,', ',', $input);
190 $input = str_replace(',,', ',', $input);
191
192 // Finally, split it apart.
193 $token_list = explode(',', $input);
194 $now = time();
195
196 foreach ($token_list as $token) {
197 $token = strtolower($token);
198 $result = db_query("INSERT INTO {spam_tokens} (token, last) VALUES('%s', %d) ON DUPLICATE KEY UPDATE last=%d", $token, $now, $now);
199 if ($result != FALSE) { drupal_set_message(t('Added') .' '. $token); }
200 }
201 return 'admin/settings/spam/tokens';
202 }
203
204 function spam_tokens_export() {
205 $result = db_query('SELECT token FROM {spam_tokens} WHERE probability=0 ORDER BY token');
206 $output = NULL;
207 $form['intro'] = array(
208 '#type' => 'item',
209 '#title' => t('Copy and paste this list to the receiving site'),
210 '#prefix' => '<div class="spam_tokens_intro">',
211 '#suffix' => '</div>',
212 );
213
214 while ($token = db_fetch_array($result)) {
215 $output .= $token['token'] .'<br/>';
216 }
217
218 $form['list'] = array(
219 '#type' => 'item',
220 '#value' => $output,
221 '#prefix' => '<div class="spam_tokens_box">',
222 '#suffix' => '</div>',
223 );
224
225 return $form;
226 }
227
228 /*
229 * Main processing page.
230 * If the page is called back, there will be a tid value, which means we should
231 * bring up the details page. Otherwise, we just produce a list.
232 */
233 function spam_tokens_page($tid=NULL, $op=NULL, $fletter=NULL) {
234 // error_reporting(E_NOTICE);
235 if ($tid) { spam_tokens_detail($tid, $op); }
236
237 $output = '<h2>'. t('Spam Tokens List') .'</h2>';
238 // Strip </form> from the end.
239 $output .= substr(drupal_get_form('spam_tokens_action_form'), 0, -8);
240 drupal_set_message($x);
241 $output .= "\n<p>". t('Note: Tokens beginning with "URL" are set by the URL filters; do not update or delete them here.') ."</p>\n";
242
243 $rows = array();
244
245 // See if we need a filter, and create it if so.
246 $count = db_result(db_query('SELECT COUNT(token) FROM {spam_tokens}'));
247 if ($count > 10) {
248 $first = array();
249 $first_lets = db_query('SELECT DISTINCT(UPPER(SUBSTRING(token, 1, 1))) AS first FROM {spam_tokens} ORDER BY token');
250 while ($letter = db_fetch_array($first_lets)) {
251 $first[] = l($letter['first'], 'admin/settings/spam/tokens/0/filter/'. $letter['first']);
252 }
253 $output .= '<table border="2" cellpadding="5"><tr><td>Filter on '. implode(', ', $first) .'</td></tr></table><br/>';
254 }
255
256 if ($op == 'filter') {
257 // Avoid a bit of user error here.
258 if (strlen($fletter) > 1) {
259 drupal_set_message(t('Filter is invalid.'), 'error');
260 $fletter = NULL;
261 }
262 else {
263 $where = "WHERE SUBSTR(token,1,1)='$fletter'";
264 }
265 }
266 else {
267 $where = NULL;
268 }
269
270 $page_limit = variable_get('spam_display_quantity', 50);
271 $result = pager_query("SELECT * FROM {spam_tokens} $where ORDER BY token", $page_limit);
272 while ($token = db_fetch_array($result)) {
273 // If it's a URL filter, go to that page instead.
274 if (substr($token['token'], 0, 4) == 'URL*') {
275 $where = 'admin/settings/spam/url/'. $token['tid'] .'/edit';
276 }
277 else {
278 $where = 'admin/settings/spam/tokens/'. $token['tid'];
279 }
280 $link = l(t('Edit'), $where);
281 $form['edit'] = array(
282 '#type' => 'button',
283 '#button_type' => spam_tokens_op,
284 '#name' => $where,
285 '#value' => t('Edit'),
286 );
287 $link = theme('button', $form['edit']);
288
289 $prob = $token['probability'] ? $token['probability'] : t('ignore');
290 $rows[] = array(
291 array('data' => $token['tid'], 'align' => 'center'),
292 $token['token'],
293 array('data' => $token['spam'], 'align' => 'right'),
294 array('data' => $token['notspam'], 'align' => 'right'),
295 array('data' => $prob, 'align' => 'center'),
296 format_date($token['last'], 'small'),
297 // format_date($token['last'], 'custom', 'Y/m/d'),
298 array('data' => $link, 'align' => 'center'),
299 );
300 }
301 if (count($rows)) {
302 $header = array(
303 t('tid'),
304 t('Token'),
305 array('data' => t('Spam'), 'align' => right),
306 array('data' => t('Not Spam'), 'align' => right),
307 t('Probability'),
308 t('Last Seen'),
309 t('Operation'),
310 );
311 $attributes = array('cellpadding' => '5',
312 'id' => 'spam_tokens_list',
313 );
314 $output .= theme('table', $header, $rows, $attributes);
315 $output .= theme('pager', array(), $page_limit);
316 }
317 else { $output .= '<br/><p>'. t('No tokens were found.') .'</p>'; }
318 return $output .'</form>';
319 }
320
321 function spam_tokens_detail($tid, $op) {
322 $output = '<h2>'. t('Details for Spam Token Number !tid', array('!tid' => $tid)) .'</h2>';
323 $token = db_fetch_array(db_query('SELECT * FROM {spam_tokens} WHERE tid=%d LIMIT 1', $tid));
324 $output .= drupal_get_form('spam_tokens_detail_form', $token, 'edit');
325 echo theme('page', $output, TRUE);
326 }
327
328 function spam_tokens_add() {
329 $token = array('tid' => 0,
330 'token' => NULL,
331 'probability' => variable_get('spam_default_probability', 40),
332 'last' => time(),
333 );
334 $output .= drupal_get_form('spam_tokens_detail_form', $token, 'add');
335 echo theme('page', $output, TRUE);
336 }
337
338 function spam_tokens_detail_form($token, $op) {
339 $form = array();
340 $threshold = variable_get('spam_threshold', 80);
341
342 $form['tid'] = array(
343 '#title' => t('tid'),
344 '#type' => 'value',
345 '#value' => $token['tid'],
346 );
347
348 $form['token'] = array(
349 '#title' => t('Token'),
350 '#type' => 'textfield',
351 '#default_value' => $token['token'],
352 '#size' => 20,
353 '#description' => t('This is the item which is used as a token when scanning for spam.'),
354 '#weight' => -3,
355 '#required' => TRUE,
356 );
357
358 $form['probability'] = array(
359 '#title' => t('Probability'),
360 '#type' => 'textfield',
361 '#default_value' => $token['probability'],
362 '#size' => 8,
363 '#description' => t('This suggests the likelihood that this token is part of spam content. The default is !default; the "average" is 50. The Spam module examines the !interest most "interesting" tokens. A higher number indicates a greater likelihood that this is spam; a lower number indicates a lesser chance. A value greater than !threshold will cause the content to be marked as spam. This value can change as it is encountered in actual content. A probablility of zero (0) will cause the token to be ignored.', array('!default' => variable_get('spam_default_probability', 40), '!interest' => variable_get('spam_interesting_tokens', 15), '!threshold' => $threshold)),
364 '#weight' => -1,
365 '#required' => TRUE,
366 );
367
368 if ($op == 'add') {
369 $field_type = 'textfield';
370 $value_type = '#default_value';
371 }
372 else {
373 $field_type = 'item';
374 $value_type = '#value';
375 }
376
377 $form['last'] = array(
378 '#title' => t('Last Seen'),
379 '#type' => $field_type,
380 $value_type => format_date($token['last'], 'custom', 'Y/m/d'),
381 '#size' => 15,
382 '#description' => t('This is the date of the last time this token was encountered.'),
383 '#weight' => 0,
384 );
385
386 // Which buttons do we need?
387 switch ($op) {
388 case 'add':
389 $form['add']['attach'] = array(
390 '#type' => 'submit',
391 '#value' => t('Add'),
392 '#weight' => 3
393 );
394 break;
395
396 case 'edit':
397 $form['update']['attach'] = array(
398 '#type' => 'submit',
399 '#value' => t('Update'),
400 '#weight' => 3
401 );
402
403 $form['delete']['attach'] = array(
404 '#type' => 'submit',
405 '#value' => t('Delete'),
406 '#weight' => 4
407 );
408 break;
409 }
410
411 $form['#redirect'] = 'admin/settings/spam/tokens';
412
413 return $form;
414 }
415
416 function spam_tokens_detail_form_validate($form_id, $form_values) {
417 if ($form_values['probability'] < 0 || $form_values['probability'] > 99) {
418 form_set_error('probability', t('Probability must be non-negative and less than 100.'));
419 }
420
421 if ($form_values['op'] == 'Add') {
422 $timestamp = strtotime($form_values['last']);
423 if ($timestamp < 1) {
424 form_set_error('last', t('"Last seen" must be a valid date.'));
425 }
426 }
427
428 if (check_plain($form_values['token']) != $form_values['token'] || strpos($form_values['token'], ' ') > 0) {
429 form_set_error('token', t("Please don't use special characters or blanks in the token name."));
430 }
431 }
432
433 function spam_tokens_detail_form_submit($form_id, $form_values) {
434 $tid = $form_values['tid'];
435 $threshold = variable_get('spam_threshold', 80);
436
437 // Note: We do not set/update the spam/notspam values as they are counters and default to zero.
438 switch ($form_values['op']) {
439 case 'Update':
440 $sql = "UPDATE {spam_tokens} SET token='". $form_values['token'] ."'"
441 .', probability='. $form_values['probability']
442 // .', last='. strtotime($form_values['last'])
443 .' WHERE tid='. $form_values['tid'] .' LIMIT 1';
444 $msg = t('Updated token number %tid.', array('%tid' => $tid));
445 break;
446
447 case 'Add':
448 $sql = 'INSERT INTO {spam_tokens} (token, probability, last) VALUES('
449 ."'". $form_values['token'] ."'"
450 .', '. $form_values['probability']
451 .', '. strtotime($form_values['last'])
452 .')';
453 $msg = t('Added token named %name.', array('%name' => $form_values['token']));
454 break;
455
456 case 'Delete':
457 $output = drupal_get_form('spam_tokens_delete_token_form', $form_values);
458 drupal_goto('admin/settings/spam/tokens/delete/'. $tid);
459 return;
460 }
461
462 db_query($sql);
463 drupal_set_message($msg);
464
465 return;
466 }
467
468 function spam_tokens_delete_token_form($tid) {
469 $token = db_fetch_array(db_query('SELECT * FROM {spam_tokens} WHERE tid=%d LIMIT 1', $tid));
470 $form = array();
471 $form['tid'] = array('#type' => 'value', '#value' => $tid);
472 // $form['token'] = array('#type' => 'value', '#value' => $values['token']);
473 $form['#redirect'] = 'admin/settings/spam/tokens';
474
475 return confirm_form($form, t('Are you sure you want to delete "%token?"', array('%token' => $token['token'])), 'admin/settings/spam/tokens');
476 }
477
478 function spam_tokens_delete_token_form_submit($form, $form_values) {
479 $sql = db_query('DELETE FROM {spam_tokens} WHERE tid='. $form_values['tid'] .' LIMIT 1');
480 drupal_set_message(t('Deleted token number %tid.', array('%tid' => $form_values['tid'])));
481 return;
482 }

  ViewVC Help
Powered by ViewVC 1.1.2