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

Contents of /contributions/modules/webpurify/webpurify.module

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


Revision 1.4 - (show annotations) (download) (as text)
Wed Feb 25 02:48:04 2009 UTC (8 months, 4 weeks ago) by rwohleb
Branch: MAIN
CVS Tags: DRUPAL-6--1-1-BETA1, HEAD
Branch point for: DRUPAL-6--1
Changes since 1.3: +498 -205 lines
File MIME type: text/x-php
Initial D6 version.
1 <?php
2 // $Id$
3
4 define('WEBPURIFY_WATCHDOG_TYPE', 'WebPurify');
5 define('WEBPURIFY_REST_URL', 'http://www.webpurify.com/services/rest/');
6 define('WEBPURIFY_REST_URL_METHOD', 'POST');
7
8 define('WEBPURIFY_MODE_OFF', 'off');
9 define('WEBPURIFY_MODE_BLOCK', 'block');
10 define('WEBPURIFY_MODE_REWRITE', 'rewrite');
11
12 define('WEBPURIFY_FAIL_MODE_NOTHING', 'nothing');
13 define('WEBPURIFY_FAIL_MODE_BLOCK', 'block');
14
15 define('WEBPURIFY_NODE_TITLE_FAIL_MESSAGE', 'Node Failure');
16 define('WEBPURIFY_NODE_CREATE_FAIL_MESSAGE', 'Node creation is temporarily disabled');
17 define('WEBPURIFY_NODE_VIEW_FAIL_MESSAGE', 'Node viewing is temporarily disabled');
18
19 define('WEBPURIFY_COMMENT_SUBJECT_FAIL_MESSAGE','Comment Failure');
20 define('WEBPURIFY_COMMENT_CREATE_FAIL_MESSAGE', 'Comment creation is temporarily disabled');
21 define('WEBPURIFY_COMMENT_VIEW_FAIL_MESSAGE', 'Comment viewing is temporarily disabled');
22
23 define('WEBPURIFY_DEFAULT_FILTER_SYMBOL', '*');
24
25 /**
26 * Implementation of hook_help()
27 */
28 function webpurify_help($path, $arg=array()) {
29 switch ($path) {
30 case 'admin/modules#description':
31 return t("The WebPurify module provides an API to the WebPurify API, comment/node profanity checking, and an input filter.");
32 case 'admin/help#webpurify':
33 return t('<p>The WebPurify module allows you to filter words or phrases in site content and replace the filtered words with the specified replacement symbol.</p>');
34 case 'admin/settings/webpurify':
35 return t('In order for filtering to work on the body text of a node or comment, you must activate the WebPurify filter in your input filters. Check your filter settings at !filter.', array('!filter' => l('Input Formats', 'admin/settings/filters')));
36 }
37 }
38
39 /**
40 * Implementation of hook_menu()
41 */
42 function webpurify_menu() {
43 $items = array();
44
45 $items['admin/settings/webpurify'] = array(
46 'title' => t('WebPurify Settings'),
47 'page callback' => 'drupal_get_form',
48 'page arguments' => array('webpurify_settings_form'),
49 'description' => t('Configure WebPurify.'),
50 'access arguments' => array('administer site configuration'),
51 );
52
53 $items['admin/settings/webpurify/test'] = array(
54 'title' => t('WebPurify Test'),
55 'page callback' => 'webpurify_test',
56 'access arguments' => array('administer site configuration'),
57 'type' => MENU_CALLBACK,
58 );
59
60 return $items;
61 }
62
63 /**
64 * Administration Page Form
65 */
66 function webpurify_settings_form() {
67 $form = array();
68
69 if (
70 (variable_get('webpurify_node_body_mode', WEBPURIFY_MODE_OFF) == WEBPURIFY_MODE_REWRITE) ||
71 (variable_get('webpurify_comment_body_mode', WEBPURIFY_MODE_OFF) == WEBPURIFY_MODE_REWRITE)
72 ) {
73 drupal_set_message(t('WARNING: You have configured rewrite mode on at least one context type. You MUST make sure to enable the WebPurify input filter for content to be filtered using WebPurify.'), 'warning', FALSE);
74 };
75
76 $form['keys'] = array(
77 '#type' => 'fieldset',
78 '#title' => t('API Keys'),
79 '#collapsible' => TRUE,
80 '#collapsed' => FALSE,
81 );
82 $form['keys']['webpurify_developerkey'] = array(
83 '#type' => 'textfield',
84 '#title' => t('Developer Key'),
85 '#size' => 120,
86 '#maxlength' => 300,
87 '#default_value' => variable_get('webpurify_developerkey', ''),
88 '#description' => t('Enter the developer key provided to you by !link', array('!link' => 'http://www.webpurify.com')),
89 );
90
91 $form['filter'] = array(
92 '#type' => 'fieldset',
93 '#title' => t('Replacement Filter'),
94 '#collapsible' => TRUE,
95 '#collapsed' => FALSE,
96 );
97 $form['filter']['webpurify_filter_symbol'] = array(
98 '#type' => 'textfield',
99 '#title' => t('Default replacement symbol'),
100 '#description' => t('The replacement symbol will replace each character in a profane word.'),
101 '#size' => 1,
102 '#maxlength' => 1,
103 '#required' => TRUE,
104 '#default_value' => variable_get('webpurify_filter_symbol', WEBPURIFY_DEFAULT_FILTER_SYMBOL),
105 );
106
107 $form['nodes'] = array(
108 '#type' => 'fieldset',
109 '#title' => t('Nodes'),
110 '#collapsible' => TRUE,
111 '#collapsed' => TRUE,
112 );
113 $form['nodes']['title'] = array(
114 '#type' => 'fieldset',
115 '#title' => t('Title'),
116 '#collapsible' => TRUE,
117 '#collapsed' => FALSE,
118 );
119 $form['nodes']['title']['webpurify_node_title_mode'] = array(
120 '#type' => 'select',
121 '#title' => t('Title Mode'),
122 '#options' => _webpurify_get_modes(FALSE),
123 '#default_value' => variable_get('webpurify_node_title_mode', WEBPURIFY_NODE_MODE_OFF),
124 '#description' => t('Select how you want WebPurify to handle node titles. NOTE: If you enable rewriting of node titles, they will be replaced with the filtered text. They are not filtered on the fly as with the input filters.'),
125 );
126 $form['nodes']['title']['webpurify_node_title_fail_mode'] = array(
127 '#type' => 'select',
128 '#title' => t('Title Failure Mode'),
129 '#options' => _webpurify_get_fail_modes(),
130 '#default_value' => variable_get('webpurify_node_title_fail_mode', WEBPURIFY_FAIL_MODE_BLOCK),
131 '#description' => t('Select how you want WebPurify to handle node creation when the API fails to respond'),
132 );
133 $form['nodes']['title']['webpurify_node_title_fail_message'] = array(
134 '#type' => 'textfield',
135 '#title' => t('Title Failure Message'),
136 '#default_value' => variable_get('webpurify_node_title_fail_message', t(WEBPURIFY_NODE_CREATE_FAIL_MESSAGE)),
137 '#description' => t('This is the text that will be saved if node creation will be blocked during an API failure'),
138 );
139 $form['nodes']['body'] = array(
140 '#type' => 'fieldset',
141 '#title' => t('Body'),
142 '#collapsible' => TRUE,
143 '#collapsed' => FALSE,
144 );
145 $form['nodes']['body']['webpurify_node_body_mode'] = array(
146 '#type' => 'select',
147 '#title' => t('Body Mode'),
148 '#options' => _webpurify_get_modes(),
149 '#default_value' => variable_get('webpurify_node_body_mode', WEBPURIFY_NODE_MODE_OFF),
150 '#description' => t('Select how you want WebPurify to handle node bodies. When set to rewrite mode, nothing actually happens. Rewrites happen via the WebPurify input filter. However, this mode does allow you to try and block node creation during WebPurify failure.'),
151 );
152 $form['nodes']['body']['webpurify_node_create_fail_mode'] = array(
153 '#type' => 'select',
154 '#title' => t('Create Failure Mode'),
155 '#options' => _webpurify_get_fail_modes(),
156 '#default_value' => variable_get('webpurify_node_create_fail_mode', WEBPURIFY_FAIL_MODE_BLOCK),
157 '#description' => t('Select how you want WebPurify to handle node creation when the API fails to respond'),
158 );
159 $form['nodes']['body']['webpurify_node_create_fail_message'] = array(
160 '#type' => 'textfield',
161 '#title' => t('Create Failure Message'),
162 '#default_value' => variable_get('webpurify_node_create_fail_message', t(WEBPURIFY_NODE_CREATE_FAIL_MESSAGE)),
163 '#description' => t('This is the text that will be displayed if node creation will be blocked during an API failure'),
164 );
165 $form['nodes']['body']['webpurify_node_view_fail_mode'] = array(
166 '#type' => 'select',
167 '#title' => t('View Failure Mode'),
168 '#options' => _webpurify_get_fail_modes(),
169 '#default_value' => variable_get('webpurify_node_view_fail_mode', WEBPURIFY_FAIL_MODE_BLOCK),
170 '#description' => t('Select how you want WebPurify to handle node viewing when the API fails to respond'),
171 );
172 $form['nodes']['body']['webpurify_node_view_fail_message'] = array(
173 '#type' => 'textfield',
174 '#title' => t('View Failure Message'),
175 '#default_value' => variable_get('webpurify_node_view_fail_message', t(WEBPURIFY_NODE_VIEW_FAIL_MESSAGE)),
176 '#description' => t('This is the text that will be displayed if node viewing will be blocked during an API failure'),
177 );
178
179 if (module_exists('comment')) {
180 $form['comments'] = array(
181 '#type' => 'fieldset',
182 '#title' => t('Comments'),
183 '#collapsible' => TRUE,
184 '#collapsed' => TRUE,
185 );
186 $form['comments']['subject'] = array(
187 '#type' => 'fieldset',
188 '#title' => t('Subject'),
189 '#collapsible' => TRUE,
190 '#collapsed' => FALSE,
191 );
192 $form['comments']['subject']['webpurify_comment_subject_mode'] = array(
193 '#type' => 'select',
194 '#title' => t('Title Mode'),
195 '#options' => _webpurify_get_modes(FALSE),
196 '#default_value' => variable_get('webpurify_comment_subject_mode', WEBPURIFY_NODE_MODE_OFF),
197 '#description' => t('Select how you want WebPurify to handle comment subjects. NOTE: If you enable rewriting of comment subjects, they will be replaced with the filtered text. They are not filtered on the fly as with the input filters.'),
198 );
199 $form['comments']['body']['webpurify_comment_subject_fail_mode'] = array(
200 '#type' => 'select',
201 '#title' => t('Create Failure Mode'),
202 '#options' => _webpurify_get_fail_modes(),
203 '#default_value' => variable_get('webpurify_comment_subject_fail_mode', WEBPURIFY_FAIL_MODE_BLOCK),
204 '#description' => t('Select how you want WebPurify to handle comments when the API fails to respond'),
205 );
206 $form['comments']['subject']['webpurify_comment_subject_fail_message'] = array(
207 '#type' => 'textfield',
208 '#title' => t('Create Failure Message'),
209 '#default_value' => variable_get('webpurify_comment_subject_fail_message', t(WEBPURIFY_COMMENT_CREATE_FAIL_MESSAGE)),
210 '#description' => t('This is the text that will be displayed if node creation will be blocked during an API failure'),
211 );
212 $form['comments']['body'] = array(
213 '#type' => 'fieldset',
214 '#title' => t('Body'),
215 '#collapsible' => TRUE,
216 '#collapsed' => FALSE,
217 );
218 $form['comments']['body']['webpurify_comment_body_mode'] = array(
219 '#type' => 'select',
220 '#title' => t('Body Mode'),
221 '#options' => _webpurify_get_modes(),
222 '#default_value' => variable_get('webpurify_comment_body_mode', WEBPURIFY_MODE_OFF),
223 '#description' => t('Select how you want WebPurify to handle comments. When set to rewrite mode, nothing actually happens. Rewrites happen via the WebPurify input filter. However, this mode does allow you to try and block comment creation during WebPurify failure.'),
224 );
225 $form['comments']['body']['webpurify_comment_create_fail_mode'] = array(
226 '#type' => 'select',
227 '#title' => t('Create Failure Mode'),
228 '#options' => _webpurify_get_fail_modes(),
229 '#default_value' => variable_get('webpurify_comment_create_fail_mode', WEBPURIFY_FAIL_MODE_BLOCK),
230 '#description' => t('Select how you want WebPurify to handle comments when the API fails to respond'),
231 );
232 $form['comments']['body']['webpurify_comment_create_fail_message'] = array(
233 '#type' => 'textfield',
234 '#title' => t('Create Failure Message'),
235 '#default_value' => variable_get('webpurify_comment_create_fail_message', t(WEBPURIFY_COMMENT_CREATE_FAIL_MESSAGE)),
236 '#description' => t('This is the text that will be displayed if comments will be blocked during an API failure'),
237 );
238 /* No way to do this at the moment
239 $form['comments']['body']['webpurify_comment_view_fail_mode'] = array(
240 '#type' => 'select',
241 '#title' => t('View Failure Mode'),
242 '#options' => _webpurify_get_fail_modes(),
243 '#default_value' => variable_get('webpurify_comment_view_fail_mode', WEBPURIFY_FAIL_MODE_BLOCK),
244 '#description' => t('Select how you want WebPurify to handle comments when the API fails to respond'),
245 );
246 $form['comments']['body']['webpurify_comment_view_fail_message'] = array(
247 '#type' => 'textfield',
248 '#title' => t('View Failure Message'),
249 '#default_value' => variable_get('webpurify_comment_view_fail_message', t(WEBPURIFY_COMMENT_VIEW_FAIL_MESSAGE)),
250 '#description' => t('This is the text that will be displayed if comments will be blocked during an API failure'),
251 );
252 */
253 }
254
255 return system_settings_form($form);
256 }
257
258 function _webpurify_get_modes($via_input_filter = TRUE) {
259 return array(
260 WEBPURIFY_MODE_OFF => t('Do nothing'),
261 WEBPURIFY_MODE_BLOCK => t('Block expletives'),
262 WEBPURIFY_MODE_REWRITE => $via_input_filter ? t('Rewrite expletives (via Input Filter)') : t('Rewrite expletives'),
263 );
264 }
265
266 function _webpurify_get_fail_modes() {
267 return array(
268 WEBPURIFY_FAIL_MODE_NOTHING => 'Allow during failure',
269 WEBPURIFY_FAIL_MODE_BLOCK => 'Block during failure',
270 );
271 }
272
273 function webpurify_test() {
274 $output = '';
275
276 $output .= "check('fuck shit cock') = " . print_r(webpurify_api_check('fuck shit cock'), TRUE) . "<br>\n";
277 $output .= "check('This is a test') = " . print_r(webpurify_api_check('This is a test'), TRUE) . "<br>\n";
278
279 $output .= "checkcount('fuck shit cock') = " . print_r(webpurify_api_checkcount('fuck shit cock'), TRUE) . "<br>\n";
280 $output .= "checkcount('This is a test') = " . print_r(webpurify_api_checkcount('This is a test'), TRUE) . "<br>\n";
281
282 $output .= "replace('fuck shit cock') = " . print_r(webpurify_api_replace('fuck shit cock', WEBPURIFY_DEFAULT_FILTER_SYMBOL), TRUE) . "<br>\n";
283 $output .= "replace('This is a test') = " . print_r(webpurify_api_replace('This is a test', WEBPURIFY_DEFAULT_FILTER_SYMBOL), TRUE) . "<br>\n";
284
285 $output .= "return('fuck shit cock') = " . print_r(webpurify_api_return('fuck shit cock', WEBPURIFY_DEFAULT_FILTER_SYMBOL), TRUE) . "<br>\n";
286 $output .= "return('This is a test') = " . print_r(webpurify_api_return('This is a test', WEBPURIFY_DEFAULT_FILTER_SYMBOL), TRUE) . "<br>\n";
287
288 $output .= webpurify_get_error();
289
290 return $output;
291 }
292
293 function webpurify_filter($op, $delta = 0, $format = -1, $text = '', $cache_id = 0) {
294 switch ($op) {
295 case 'list':
296 return array(0 => t('WebPurify filter'));
297 case 'description':
298 return webpurify_help('admin/help#webpurify');
299 case 'settings':
300 $form['webpurify_filter'] = array(
301 '#type' => 'fieldset',
302 '#title' => t('WebPurify filter'),
303 '#collapsible' => TRUE,
304 );
305 $form['webpurify_filter']['webpurify_filter_symbol_'.$format] = array(
306 '#type' => 'textfield',
307 '#title' => t('WebPurify replacement symbol'),
308 '#description' => t('The replacement symbol will replace each character in a profane word. If a symbol is not specified, the default "!symbol" symbol will be used', array('!symbol' => variable_get('webpurify_filter_symbol', WEBPURIFY_DEFAULT_FILTER_SYMBOL))),
309 '#size' => 1,
310 '#maxlength' => 1,
311 '#default_value' => '',
312 );
313 return $form;
314 case 'process':
315 return webpurify_filter_process($op, $delta, $format, $text, $cache_id);
316 case 'no cache':
317 return TRUE;
318 default:
319 return $text;
320 }
321 }
322
323 function webpurify_filter_process($op, $delta = 0, $format = -1, $text = '', $cache_id = 0) {
324 if (webpurify_get_node_body_mode() == WEBPURIFY_MODE_REWRITE) {
325 // get replacement symbol
326 $symbol = variable_get('webpurify_filter_symbol_'.$format, '');
327 if (empty($symbol)) {
328 $symbol = variable_get('webpurify_filter_symbol', WEBPURIFY_DEFAULT_FILTER_SYMBOL);
329 }
330
331 // try replacement
332 $replaced_text = webpurify_api_replace($text, $symbol);
333 if ($replaced_text === FALSE) {
334 if (webpurify_get_node_view_fail_mode() == WEBPURIFY_FAIL_MODE_BLOCK) {
335 return variable_get('webpurify_node_view_fail_message', t(WEBPURIFY_NODE_VIEW_FAIL_MESSAGE));
336 }
337 watchdog(WEBPURIFY_WATCHDOG_TYPE, __FUNCTION__ .': '. __LINE__ .' -- '. webpurify_get_error(), array(), WATCHDOG_ERROR);
338 }
339 elseif ($replaced_text != $text) {
340 watchdog(WEBPURIFY_WATCHDOG_TYPE, __FUNCTION__ .': '. __LINE__ .' -- '. "Input Filter Rewrite: [".$text."]>>>[".$replaced_text."]", array(), WATCHDOG_NOTICE);
341 return $replaced_text;
342 }
343 }
344
345 return $text;
346 }
347
348 /**
349 * Implementation of hook_filter_tips()
350 */
351 /*
352 function webpurify_filter_tips($delta, $format, $long = FALSE) {
353 if ($long) {
354 return t('If you include a word in your post that\'s filtered (usually foul language), it will be replaced by the filtered version of the word.') .'<br />';
355 }
356 else {
357 $GLOBALS['display_wordfilter_block'] = TRUE;
358 return t('Filtered words will be replaced with the filtered version of the word.');
359 }
360 }
361 */
362
363 /**
364 * Implementation of hook_nodeapi().
365 *
366 * @param &$node
367 * editable node object
368 *
369 * @param $op
370 * string of what process we're at
371 *
372 * @param $teaser
373 * boolean if showing teaser
374 *
375 * @param $page
376 * boolean if on full view page of a node
377 *
378 */
379 function webpurify_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
380 switch ($op) {
381 case 'validate':
382 // Are we in block mode
383 if (webpurify_get_node_title_mode() == WEBPURIFY_MODE_BLOCK) {
384 _webpurify_nodeapi_validate_title_block($node, $op, $a3, $a4);
385 }
386 if (webpurify_get_node_body_mode() == WEBPURIFY_MODE_BLOCK) {
387 _webpurify_nodeapi_validate_body_block($node, $op, $a3, $a4);
388 }
389
390 // are we in rewrite mode
391 if (webpurify_get_node_title_mode() == WEBPURIFY_MODE_REWRITE) {
392 _webpurify_nodeapi_validate_title_rewrite($node, $op, $a3, $a4);
393 }
394 if (webpurify_get_node_body_mode() == WEBPURIFY_MODE_REWRITE) {
395 _webpurify_nodeapi_validate_body_rewrite($node, $op, $a3, $a4);
396 }
397 break;
398 case 'view':
399 // if this is a node preview, fix up the unsaved title
400 if (!empty($node->preview)) {
401 // are we in rewrite mode
402 if (webpurify_get_node_title_mode() == WEBPURIFY_MODE_REWRITE) {
403 _webpurify_nodeapi_preview_title_rewrite($node, $op, $a3, $a4);
404 }
405 if (webpurify_get_node_body_mode() == WEBPURIFY_MODE_REWRITE) {
406 _webpurify_nodeapi_preview_body_rewrite($node, $op, $a3, $a4);
407 }
408 }
409 break;
410 //case 'submit':
411 //case 'update':
412 case 'presave':
413 if (webpurify_get_node_title_mode() == WEBPURIFY_MODE_REWRITE) {
414 _webpurify_nodeapi_presave_title_rewrite($node, $op, $a3, $a4);
415 }
416 break;
417 }
418 }
419
420 function _webpurify_nodeapi_validate_title_block(&$node, $op, $a3 = NULL, $a4 = NULL) {
421 // check for expletives
422 $title_expletives = webpurify_api_return($a3['title']['#value']);
423
424 // handle a failure of the API properly
425 if ($title_expletives === FALSE) {
426 watchdog(WEBPURIFY_WATCHDOG_TYPE, __FUNCTION__ .': '. __LINE__ .' -- '. webpurify_get_error(), array(), WATCHDOG_ERROR);
427 if (webpurify_get_node_title_fail_mode() == WEBPURIFY_NODE_FAIL_MODE_BLOCK) {
428 form_set_error('webpurify', variable_get('webpurify_node_title_fail_message', t(WEBPURIFY_NODE_TITLE_FAIL_MESSAGE)));
429 }
430 }
431
432 $type_info = node_get_types('type', $node);
433 //drupal_set_message(__FUNCTION__ .': '. __LINE__ .' -- '. print_r($type_info, TRUE));
434
435 // check if we got expletives back
436 if (is_array($title_expletives) && count($title_expletives)) {
437 form_set_error('title', t('Your @field cannot be posted since it contains objectionable language (%expletives). Please reword your @field.', array('@field' => $type_info->title_label, '%expletives' => implode(', ', $title_expletives))));
438 }
439 }
440
441 function _webpurify_nodeapi_validate_body_block(&$node, $op, $a3 = NULL, $a4 = NULL) {
442 // check for expletives
443 $body_expletives = webpurify_api_return($a3['body_field']['body']['#value']);
444
445 // handle a failure of the API properly
446 if ($body_expletives === FALSE) {
447 watchdog(WEBPURIFY_WATCHDOG_TYPE, __FUNCTION__ .': '. __LINE__ .' -- '. webpurify_get_error(), array(), WATCHDOG_ERROR);
448 if (webpurify_get_node_create_fail_mode() == WEBPURIFY_NODE_FAIL_MODE_BLOCK) {
449 form_set_error('webpurify', variable_get('webpurify_node_create_fail_message', t(WEBPURIFY_NODE_CREATE_FAIL_MESSAGE)));
450 }
451 }
452
453 $type_info = node_get_types('type', $node);
454
455 // check if we got expletives back
456 if (is_array($body_expletives) && count($body_expletives)) {
457 form_set_error('body', t('Your @field cannot be posted since it contains objectionable language (%expletives). Please reword your @field.', array('@field' => $type_info->body_label, '%expletives' => implode(', ', $body_expletives))));
458 }
459 }
460
461 function _webpurify_nodeapi_validate_title_rewrite(&$node, $op, $a3 = NULL, $a4 = NULL) {
462 $nid = $node->nid;
463 $title = webpurify_api_replace($a3['title']['#value'], variable_get('webpurify_filter_symbol', WEBPURIFY_DEFAULT_FILTER_SYMBOL));
464 if ($title === FALSE) {
465 // handle a failure of the API properly
466 if (webpurify_get_node_title_fail_mode() == WEBPURIFY_FAIL_MODE_BLOCK) {
467 form_set_error('webpurify', variable_get('webpurify_node_title_fail_message', t(WEBPURIFY_NODE_FAIL_MESSAGE)));
468 }
469 watchdog(WEBPURIFY_WATCHDOG_TYPE, __FUNCTION__ .': '. __LINE__ .' -- '. webpurify_get_error(), array(), WATCHDOG_ERROR);
470 }
471 }
472
473 function _webpurify_nodeapi_validate_body_rewrite(&$node, $op, $a3 = NULL, $a4 = NULL) {
474 $nid = $node->nid;
475 $body = webpurify_api_replace($a3['body_field']['body']['#value'], variable_get('webpurify_filter_symbol', WEBPURIFY_DEFAULT_FILTER_SYMBOL));
476 if ($body === FALSE) {
477 // handle a failure of the API properly
478 if (webpurify_get_node_body_fail_mode() == WEBPURIFY_FAIL_MODE_BLOCK) {
479 form_set_error('webpurify', variable_get('webpurify_node_body_fail_message', t(WEBPURIFY_NODE_FAIL_MESSAGE)));
480 }
481 watchdog(WEBPURIFY_WATCHDOG_TYPE, __FUNCTION__ .': '. __LINE__ .' -- '. webpurify_get_error(), array(), WATCHDOG_ERROR);
482 }
483 }
484
485 function _webpurify_nodeapi_preview_title_rewrite(&$node, $op, $a3 = NULL, $a4 = NULL) {
486 $nid = $node->nid;
487 $title = webpurify_api_replace($node->title, variable_get('webpurify_filter_symbol', WEBPURIFY_DEFAULT_FILTER_SYMBOL));
488 if ($title === FALSE) {
489 // handle a failure of the API properly
490 if (webpurify_get_node_title_fail_mode() == WEBPURIFY_FAIL_MODE_BLOCK) {
491 form_set_error('webpurify', variable_get('webpurify_node_title_fail_message', t(WEBPURIFY_NODE_FAIL_MESSAGE)));
492 }
493 watchdog(WEBPURIFY_WATCHDOG_TYPE, __FUNCTION__ .': '. __LINE__ .' -- '. webpurify_get_error(), array(), WATCHDOG_ERROR);
494 }
495 else {
496 $node->title = $title;
497 }
498 }
499
500 function _webpurify_nodeapi_preview_body_rewrite(&$node, $op, $a3 = NULL, $a4 = NULL) {
501 $nid = $node->nid;
502 $body = webpurify_api_replace($node->body, variable_get('webpurify_filter_symbol', WEBPURIFY_DEFAULT_FILTER_SYMBOL));
503 if ($body === FALSE) {
504 // handle a failure of the API properly
505 if (webpurify_get_node_body_fail_mode() == WEBPURIFY_FAIL_MODE_BLOCK) {
506 form_set_error('webpurify', variable_get('webpurify_node_body_fail_message', t(WEBPURIFY_NODE_FAIL_MESSAGE)));
507 }
508 watchdog(WEBPURIFY_WATCHDOG_TYPE, __FUNCTION__ .': '. __LINE__ .' -- '. webpurify_get_error(), array(), WATCHDOG_ERROR);
509 }
510 else {
511 $node->body = $body;
512 }
513 }
514
515 function _webpurify_nodeapi_presave_title_rewrite(&$node, $op, $a3 = NULL, $a4 = NULL) {
516 $nid = $node->nid;
517 $title = webpurify_api_replace($node->title, variable_get('webpurify_filter_symbol', WEBPURIFY_DEFAULT_FILTER_SYMBOL));
518 if ($title === FALSE) {
519 // handle a failure of the API properly
520 if (webpurify_get_node_title_fail_mode() == WEBPURIFY_FAIL_MODE_BLOCK) {
521 form_set_error('webpurify', variable_get('webpurify_node_title_fail_message', t(WEBPURIFY_NODE_FAIL_MESSAGE)));
522 }
523 watchdog(WEBPURIFY_WATCHDOG_TYPE, __FUNCTION__ .': '. __LINE__ .' -- '. webpurify_get_error(), array(), WATCHDOG_ERROR);
524 }
525 else {
526 $node->title = $title;
527 }
528 }
529
530 /**
531 * Implementation of hook_comment()
532 */
533 function webpurify_comment($a1, $op) {
534 switch ($op) {
535 case 'validate':
536 // block mode
537 if (webpurify_get_comment_subject_mode() == WEBPURIFY_MODE_BLOCK) {
538 // get expletives
539 $subject_expletives = webpurify_api_return($a1['subject']);
540
541 // handle a failure of the API properly
542 if ($subject_expletives === FALSE) {
543 watchdog(WEBPURIFY_WATCHDOG_TYPE, __FUNCTION__ .': '. __LINE__ .' -- '. webpurify_get_error(), array(), WATCHDOG_ERROR);
544 if (webpurify_get_comment_create_fail_mode() == WEBPURIFY_COMMENT_FAIL_MODE_BLOCK) {
545 form_set_error('webpurify', variable_get('webpurify_comment_fail_message', t(WEBPURIFY_COMMENT_CREATE_FAIL_MESSAGE)));
546 }
547 }
548
549 // check if we got expletives back
550 if (is_array($subject_expletives) && count($subject_expletives)) {
551 form_set_error('subject', t('Your subject cannot be posted since it contains objectionable language (%expletives). Please reword your subject.', array('%expletives' => implode(', ', $subject_expletives))));
552 }
553 }
554 if (webpurify_get_comment_body_mode() == WEBPURIFY_MODE_BLOCK) {
555 // get expletives
556 $comment_expletives = webpurify_api_return($a1['comment']);
557
558 // handle a failure of the API properly
559 if ($comment_expletives === FALSE) {
560 watchdog(WEBPURIFY_WATCHDOG_TYPE, __FUNCTION__ .': '. __LINE__ .' -- '. webpurify_get_error(), array(), WATCHDOG_ERROR);
561 if (webpurify_get_comment_create_fail_mode() == WEBPURIFY_COMMENT_FAIL_MODE_BLOCK) {
562 form_set_error('webpurify', variable_get('webpurify_comment_fail_message', t(WEBPURIFY_COMMENT_CREATE_FAIL_MESSAGE)));
563 }
564 }
565
566 // check if we got expletives back
567 if (is_array($comment_expletives) && count($comment_expletives)) {
568 form_set_error('comment', t('Your comment cannot be posted since it contains objectionable language (%expletives). Please reword your comment.', array('%expletives' => implode(', ', $comment_expletives))));
569 }
570 }
571
572 // rewrite mode
573 if (webpurify_get_comment_subject_mode() == WEBPURIFY_MODE_REWRITE) {
574 $subject = webpurify_api_replace($a1['subject'], variable_get('webpurify_filter_symbol', WEBPURIFY_DEFAULT_FILTER_SYMBOL));
575 // handle a failure of the API properly
576 if ($subject === FALSE) {
577 if (webpurify_get_comment_create_fail_mode() == WEBPURIFY_COMMENT_FAIL_MODE_BLOCK) {
578 form_set_error('webpurify', variable_get('webpurify_comment_fail_message', t(WEBPURIFY_COMMENT_CREATE_FAIL_MESSAGE)));
579 }
580 }
581 $a1['subject'] = $subject;
582 }
583 break;
584 case 'view':
585 if (!empty($a1->preview)) {
586 // rewrite mode
587 if (webpurify_get_comment_subject_mode() == WEBPURIFY_MODE_REWRITE) {
588 $subject = webpurify_api_replace($a1->subject, variable_get('webpurify_filter_symbol', WEBPURIFY_DEFAULT_FILTER_SYMBOL));
589 // handle a failure of the API properly
590 if ($subject === FALSE) {
591 if (webpurify_get_comment_create_fail_mode() == WEBPURIFY_COMMENT_FAIL_MODE_BLOCK) {
592 form_set_error('webpurify', variable_get('webpurify_comment_fail_message', t(WEBPURIFY_COMMENT_CREATE_FAIL_MESSAGE)));
593 }
594 }
595 $a1->subject = $subject;
596 }
597 }
598 break;
599 case 'insert':
600 case 'update':
601 //if (variable_get('webpurify_comment_mode', WEBPURIFY_COMMENT_MODE_OFF) == WEBPURIFY_COMMENT_MODE_REWRITE) {
602 if (!empty($a1) && (webpurify_get_comment_subject_mode() == WEBPURIFY_MODE_REWRITE)) {
603 $cid = $a1['cid'];
604
605 $subject = webpurify_api_replace($a1['subject'], variable_get('webpurify_filter_symbol', WEBPURIFY_DEFAULT_FILTER_SYMBOL));
606 if ($subject === FALSE) {
607 watchdog(WEBPURIFY_WATCHDOG_TYPE, __FUNCTION__ .': '. __LINE__ .' -- '. webpurify_get_error(), array(), WATCHDOG_ERROR);
608 }
609 elseif ($subject != $a1['subject']) {
610 watchdog(WEBPURIFY_WATCHDOG_TYPE, __FUNCTION__ .': '. __LINE__ .' -- '. "Comment Subject Rewrite [cid=".$cid."]: [".$a1['subject']."]>>>[".$subject."]", array(), WATCHDOG_NOTICE);
611 db_query("UPDATE {comments} SET subject = '%s' WHERE cid = %d", $subject, $cid);
612 }
613 }
614 break;
615 }
616 }
617
618 /**
619 * Returns true if there are profane words in text
620 */
621 function webpurify_api_check($text) {
622 static $values = array();
623
624 $hash = md5($text);
625 if (!empty($values[$hash])) {
626 return $values[$hash];
627 }
628
629 $result = _webpurify_api_call('webpurify.live.check', array('text'=>$text));
630
631 if (isset($result['RSP']['#attributes']['STAT'])) {
632 // was there an error?
633 if ($result['RSP']['#attributes']['STAT'] == 'ok') {
634 if (isset($result['RSP']['#children']['FOUND']['#value'])) {
635 $values[$hash] = $result['RSP']['#children']['FOUND']['#value'];
636 return $values[$hash];
637 }
638 }
639 // error
640 else {
641 if (isset($result['RSP']['#children']['ERR']['#attributes']['CODE'])) {
642 $GLOBALS['WEBPURIFY_ERROR'] = t("API error: Code @code - @msg", array('@code'=>$result['RSP']['#children']['ERR']['#attributes']['CODE'], '@msg'=>$result['RSP']['#children']['ERR']['#attributes']['MSG']));
643 return FALSE;
644 }
645 }
646 }
647
648 $GLOBALS['WEBPURIFY_ERROR'] = t("API error: unrecoginzable response");
649 return FALSE;
650 }
651
652 /**
653 * Returns the number of profane words in text
654 */
655 function webpurify_api_checkcount($text) {
656 static $values = array();
657
658 $hash = md5($text);
659 if (!empty($values[$hash])) {
660 return $values[$hash];
661 }
662
663 $result = _webpurify_api_call('webpurify.live.checkcount', array('text'=>$text));
664
665 if (isset($result['RSP']['#attributes']['STAT'])) {
666 // was there an error?
667 if ($result['RSP']['#attributes']['STAT'] == 'ok') {
668 if (isset($result['RSP']['#children']['FOUND']['#value'])) {
669 $values[$hash] = $result['RSP']['#children']['FOUND']['#value'];
670 return $values[$hash];
671 }
672 }
673 // error
674 else {
675 if (isset($result['RSP']['#children']['ERR']['#attributes']['CODE'])) {
676 $GLOBALS['WEBPURIFY_ERROR'] = t("API error: Code @code - @msg", array('@code'=>$result['RSP']['#children']['ERR']['#attributes']['CODE'], '@msg'=>$result['RSP']['#children']['ERR']['#attributes']['MSG']));
677 return FALSE;
678 }
679 }
680 }
681
682 $GLOBALS['WEBPURIFY_ERROR'] = t("API error: unrecoginzable response");
683 return FALSE;
684 }
685
686 /**
687 * Returns the text with all profane words replaced
688 */
689 function webpurify_api_replace($text, $replacesymbol) {
690 static $values = array();
691
692 $hash = md5($text . $replacesymbol);
693 if (!empty($values[$hash])) {
694 return $values[$hash];
695 }
696
697 $result = _webpurify_api_call('webpurify.live.replace', array('text'=>$text, 'replacesymbol'=>$replacesymbol));
698
699 if (isset($result['RSP']['#attributes']['STAT'])) {
700 // was there an error?
701 if ($result['RSP']['#attributes']['STAT'] == 'ok') {
702 if (isset($result['RSP']['#children']['FOUND']['#value'])) {
703 if ($result['RSP']['#children']['FOUND']['#value']) {
704 if (isset($result['RSP']['#children']['TEXT']['#value'])) {
705 $values[$hash] = $result['RSP']['#children']['TEXT']['#value'];
706 return $values[$hash];
707 }
708 }
709 else {
710 $values[$hash] = $text;
711 return $values[$hash];
712 }
713 }
714 }
715 // error
716 else {
717 if (isset($result['RSP']['#children']['ERR']['#attributes']['CODE'])) {
718 $GLOBALS['WEBPURIFY_ERROR'] = t("API error: Code @code - @msg", array('@code'=>$result['RSP']['#children']['ERR']['#attributes']['CODE'], '@msg'=>$result['RSP']['#children']['ERR']['#attributes']['MSG']));
719 return FALSE;
720 }
721 }
722 }
723
724 $GLOBALS['WEBPURIFY_ERROR'] = t("API error: unrecoginzable response");
725 return FALSE;
726 }
727
728 /**
729 * Returns the profane words in text
730 */
731 function webpurify_api_return($text) {
732 static $values = array();
733
734 $hash = md5($text);
735 if (!empty($values[$hash])) {
736 return $values[$hash];
737 }
738
739 $result = _webpurify_api_call('webpurify.live.return', array('text'=>$text));
740 $expletives = array();
741
742 if (isset($result['RSP']['#attributes']['STAT'])) {
743 // was there an error?
744 if ($result['RSP']['#attributes']['STAT'] == 'ok') {
745 if (isset($result['RSP']['#children']['FOUND']['#value'])) {
746 if ($result['RSP']['#children']['FOUND']['#value']) {
747 if (isset($result['RSP']['#children']['EXPLETIVE'])) {
748 if (isset($result['RSP']['#children']['EXPLETIVE']['#tag'])) {
749 $expletives[] = $result['RSP']['#children']['EXPLETIVE']['#value'];
750 }
751 else {
752 foreach ($result['RSP']['#children']['EXPLETIVE'] as $element) {
753 if (isset($element['#value'])) {
754 $expletives[] = $element['#value'];
755 }
756 }
757 }
758 $values[$hash] = $expletives;
759 return $values[$hash];
760 }
761 }
762 else {
763 $values[$hash] = $expletives;
764 return $values[$hash];
765 }
766 }
767 }
768 // error
769 else {
770 if (isset($result['RSP']['#children']['ERR']['#attributes']['CODE'])) {
771 $GLOBALS['WEBPURIFY_ERROR'] = t("API error: Code @code - @msg", array('@code'=>$result['RSP']['#children']['ERR']['#attributes']['CODE'], '@msg'=>$result['RSP']['#children']['ERR']['#attributes']['MSG']));
772 return FALSE;
773 }
774 }
775 }
776
777 $GLOBALS['WEBPURIFY_ERROR'] = t("API error: unrecoginzable response");
778 return FALSE;
779 }
780
781 /**
782 * Calls remote REST interface and returns result as an array.
783 */
784 function _webpurify_api_call($method, $params='') {
785 $url = WEBPURIFY_REST_URL;
786 $http_method = WEBPURIFY_REST_URL_METHOD;
787 $headers = array();
788
789 if ($http_method == 'POST') {
790 $headers['Content-Type'] = 'application/x-www-form-urlencoded';
791 }
792
793 $raw_params = array(
794 'api_key' => variable_get('webpurify_developerkey', ''),
795 'method' => $method,
796 );
797 $raw_params = array_merge($raw_params, $params);
798
799 $encoded_params = array();
800 foreach ($raw_params as $k => $v){
801 $encoded_params[] = urlencode($k).'='.urlencode($v);
802 }
803
804 if ($http_method == 'POST') {
805 $result = drupal_http_request($url, $headers, $http_method, implode('&', $encoded_params));
806 }
807 else {
808 $url = WEBPURIFY_REST_URL."?".implode('&', $encoded_params);
809
810 $result = drupal_http_request($url, $headers, $http_method);
811 }
812
813 switch ($result->code) {
814 case 200: // OK
815 case 304: // Not modified
816 return _webpurify_parse_xml($result->data);
817 break;
818 default:
819 $GLOBALS['WEBPURIFY_ERROR'] = t("drupal_http_request() failed: %code %error", array('%code'=>$result->code, '%error'=>$result->error));
820 return FALSE;
821 break;
822 }
823 }
824
825 /**
826 * Gets XML in a string and parses it into an array.
827 */
828 function _webpurify_parse_xml($xml) {
829 $parser = NULL;
830 $structure = array();
831 $index = array();
832 $result = FALSE;
833
834 // The WebPurify API doesn't wrap the text in cdata, so we will try to fix it
835 if (!preg_match('@<text>\s*<!\[CDATA\[@im', $xml)) {
836 $xml = preg_replace('@<text>@im', '<text><![CDATA[', $xml);
837 $xml = preg_replace('@</text>@im', ']]></text>', $xml);
838 }
839
840 // did we get any xml?
841 if ($xml == "") {
842 $GLOBALS['WEBPURIFY_ERROR'] = t("xml was empty");
843 return FALSE;
844 }
845
846 // create the parser object
847 if (!($parser = xml_parser_create())) {
848 $GLOBALS['WEBPURIFY_ERROR'] = t("xml_parser_create() failed to return parser");
849 return FALSE;
850 }
851
852 // try to parse the xml
853 if (xml_parse_into_struct($parser, trim($xml), &$structure, &$index) === 0) {
854 $err_code = xml_get_error_code($parser);
855 $err_string = xml_error_string($err_code);
856 $GLOBALS['WEBPURIFY_ERROR'] = t("xml_parse_into_struct failed: Code @code - @msg", array('@code'=>$err_code, '@msg'=>$err_string));
857 xml_parser_free($parser);
858 return FALSE;
859 }
860 xml_parser_free($parser);
861
862 // return the parsed xml
863 //dsm($structure);
864 return _webpurify_parse_xml_helper($structure);
865 }
866
867 /**
868 * private helper for webpurify_parse_xml_result, to recusively parsing the result
869 */
870 function _webpurify_parse_xml_helper($input, $depth = 1) {
871 $output = array();
872 $children = array();
873 $attributes = FALSE;
874
875 foreach( $input as $data ) {
876 if ( $data['level'] == $depth ) {
877 switch($data['type']) {
878 case 'complete':
879 $element = array(
880 '#tag' => $data['tag'],
881 '#value' => $data['value'],
882 );
883 if ($data['attributes']) {
884 $element['#attributes'] = $data['attributes'];
885 }
886
887 // see if we need to convert from single element to an array of elements
888 if (isset($output[$data['tag']]['#tag'])) {
889 $temp_element = $output[$data['tag']];
890 $output[$data['tag']] = array();
891 $output[$data['tag']][] = $temp_element;
892 $output[$data['tag']][] = $element;
893 }
894 // already an array of elements
895 elseif (is_array($output[$data['tag']])) {
896 $output[$data['tag']][] = $element;
897 }
898 // a single element
899 else {
900 $output[$data['tag']] = $element;
901 }
902 break;
903
904 case 'open':
905 $children = array();
906 $attributes = FALSE;
907 if ($data['attributes']) {
908 $attributes = $data['attributes'];
909 }
910 break;
911
912 case 'close':
913 $element = array(
914 '#tag' => $data['tag'],
915 '#children' => _webpurify_parse_xml_helper($children, $depth + 1),
916 );
917 if ($attributes) {
918 $element['#attributes'] = $attributes;
919 }
920
921 // see if we need to convert from single element to an array of elements
922 if (isset($output[$data['tag']]['#tag'])) {
923 $temp_element = $output[$data['tag']];
924 $output[$data['tag']] = array();
925 $output[$data['tag']][] = $temp_element;
926 $output[$data['tag']][] = $element;
927 }
928 // already an array of elements
929 elseif (is_array($output[$data['tag']])) {
930 $output[$data['tag']][] = $element;
931 }
932 // a single element
933 else {
934 $output[$data['tag']] = $element;
935 }
936 break;
937 }
938 }
939 else {
940 $children[] = $data;
941 }
942 }
943 return $output;
944 }
945
946 function webpurify_get_error() {
947 if (!empty($GLOBALS['WEBPURIFY_ERROR'])) {
948 return $GLOBALS['WEBPURIFY_ERROR'];
949 }
950 return 'Empty error string';
951 }
952
953 function webpurify_get_node_title_mode() {
954 return variable_get('webpurify_node_title_mode', WEBPURIFY_MODE_OFF);
955 }
956
957 function webpurify_get_node_title_fail_mode() {
958 return variable_get('webpurify_node_create_fail_mode', WEBPURIFY_FAIL_MODE_BLOCK);
959 }
960
961 function webpurify_get_node_body_mode() {
962 return variable_get('webpurify_node_body_mode', WEBPURIFY_MODE_OFF);
963 }
964
965 function webpurify_get_node_create_fail_mode() {
966 return variable_get('webpurify_node_create_fail_mode', WEBPURIFY_FAIL_MODE_BLOCK);
967 }
968
969 function webpurify_get_node_view_fail_mode() {
970 return variable_get('webpurify_node_view_fail_mode', WEBPURIFY_FAIL_MODE_BLOCK);
971 }
972
973 function webpurify_get_comment_subject_mode() {
974 return variable_get('webpurify_comment_subject_mode', WEBPURIFY_MODE_OFF);
975 }
976
977 function webpurify_get_comment_body_mode() {
978 return variable_get('webpurify_comment_body_mode', WEBPURIFY_MODE_OFF);
979 }
980
981 function webpurify_get_comment_create_fail_mode() {
982 return variable_get('webpurify_comment_create_fail_mode', WEBPURIFY_FAIL_MODE_BLOCK);
983 }
984
985 /*
986 function webpurify_get_comment_view_fail_mode() {
987 return variable_get('webpurify_comment_view_fail_mode', WEBPURIFY_FAIL_MODE_BLOCK);
988 }
989 */

  ViewVC Help
Powered by ViewVC 1.1.2