restrict_abusive_words.module in Restrict Abusive Words 7
Same filename and directory in other branches
Restrict Abusive Words module.
File
restrict_abusive_words.moduleView source
<?php
/**
* @file
* Restrict Abusive Words module.
*/
/**
* Implements hook_help().
*/
function restrict_abusive_words_help($path, $arg) {
switch ($path) {
case 'admin/help#restrict_abusive_words':
return t('The Restrict Abusive Words module restrict to use words or phrases in forms all over the site content. The Restriction can be applied on content form, comment form, user profile form, user registration form and webform.Restriction can also be applied based on user roles.');
case 'admin/config/content/restrict_abusive_words':
return t('Set the basic Configuration to restrict to use in the forms all over the site.');
case 'admin/people/permissions#administer restrict abusive words':
return t('Allow users to configure abusive words which need to be restricted.');
}
}
/**
* Implements hook_permission().
*/
function restrict_abusive_words_permission() {
return array(
'administer restrict abusive words' => array(
'title' => t('Administer Restrict Abusive Words'),
'description' => t('Allow users to configure abusive words which need to be restricted.'),
),
);
}
/**
* Implements hook_menu().
*/
function restrict_abusive_words_menu() {
$items = array();
$items['admin/config/content/restrict_abusive_words'] = array(
'title' => 'Restrict Abusive Words',
'description' => 'General setting for Restrict Abusive Words.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'restrict_abusive_words_admin_form',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer restrict abusive words',
),
'file' => 'restrict_abusive_words.admin.inc',
);
$items['admin/config/content/restrict_abusive_words/list'] = array(
'title' => 'List of Abusive words',
'page callback' => 'restrict_abusive_words_admin_list',
'access callback' => 'user_access',
'access arguments' => array(
'administer restrict abusive words',
),
'type' => MENU_LOCAL_TASK,
'file' => 'restrict_abusive_words.admin.inc',
);
$items['admin/config/content/restrict_abusive_words/add'] = array(
'title' => 'Add abusive words',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'restrict_abusive_words_admin_add_form',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer restrict abusive words',
),
'type' => MENU_LOCAL_TASK,
'file' => 'restrict_abusive_words.admin.inc',
);
$items['admin/config/content/restrict_abusive_words/edit/%'] = array(
'title' => 'Edit Abusive words',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'restrict_abusive_words_admin_edit_form',
5,
),
'access callback' => 'user_access',
'access arguments' => array(
'administer restrict abusive words',
),
'type' => MENU_CALLBACK,
'file' => 'restrict_abusive_words.admin.inc',
);
$items['admin/config/content/restrict_abusive_words/delete/%'] = array(
'title' => 'Delete Abusive word',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'restrict_abusive_words_admin_form_delete_confirm',
5,
),
'access callback' => 'user_access',
'access arguments' => array(
'administer restrict abusive words',
),
'type' => MENU_CALLBACK,
'file' => 'restrict_abusive_words.admin.inc',
);
$items['admin/config/content/restrict_abusive_words/autocomplete'] = array(
'title' => 'Autocomplete for Abusive Word',
'page callback' => '_restrict_abusive_words_abusive_word_autocomplete',
'access callback' => 'user_access',
'access arguments' => array(
'administer restrict abusive words',
),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_form_alter().
*/
function restrict_abusive_words_form_alter(&$form, &$form_state, $form_id) {
global $user;
$web_form = '';
$action = 'validate';
$check_user_roles = FALSE;
$check_user_per = FALSE;
$check_disable_user = FALSE;
$node_form = array();
$comment_form = array();
$user_form = array();
$actions = variable_get('restrict_abusive_words_actions');
$general_form = variable_get('restrict_abusive_words_general_form');
$disable_user_roles = variable_get('restrict_abusive_words_disable_user_roles');
$user_roles = variable_get('restrict_abusive_words_user_roles');
$entity_node = variable_get('restrict_abusive_words_entity_node');
$entity_comment = variable_get('restrict_abusive_words_entity_comment');
if (!empty($actions)) {
if ($actions == 'prevent_form') {
$action = 'validate';
}
elseif ($actions == 'deactive_form') {
$action = 'submit';
}
}
if (isset($disable_user_roles)) {
foreach ($disable_user_roles as $dis_user) {
if (!empty($dis_user) && in_array($dis_user, $user->roles)) {
$check_disable_user = TRUE;
break;
}
}
}
if (isset($user_roles)) {
foreach ($user_roles as $val_r) {
if (!empty($val_r)) {
$check_user_roles = TRUE;
if (in_array($val_r, $user->roles)) {
$check_user_per = TRUE;
break;
}
}
}
}
// Check user permission to use Restrict abusive words.
if (!$check_disable_user && (!$check_user_roles || $check_user_per)) {
// Validate user related form and webform.
if (isset($general_form)) {
foreach ($general_form as $gen_k => $gen_val) {
if (!empty($gen_val)) {
if ($gen_k == 'user_register_form' || $gen_k == 'user_profile_form') {
$user_form[] = $gen_k;
}
elseif ($gen_k == 'webform') {
$web_form = $gen_k . '_client_form';
}
}
}
}
if (in_array($form_id, $user_form)) {
array_unshift($form['#validate'], '_restrict_abusive_words_user_form_' . $action);
}
if (!empty($web_form) && strpos($form_id, $web_form) !== FALSE) {
array_unshift($form['#validate'], '_restrict_abusive_words_web_form_' . $action);
}
// Validate node form.
if (isset($entity_node)) {
foreach ($entity_node as $node_val) {
if (!empty($node_val)) {
$node_form[] = $node_val . '_node_form';
}
}
}
if (in_array($form_id, $node_form)) {
array_unshift($form['#validate'], '_restrict_abusive_words_node_form_' . $action);
}
// Validate comment node form.
if (isset($entity_comment)) {
foreach ($entity_comment as $comment_val) {
if (!empty($comment_val)) {
$comment_form[] = 'comment_node_' . $comment_val . '_form';
}
}
}
if (in_array($form_id, $comment_form)) {
array_unshift($form['#validate'], '_restrict_abusive_words_comment_form_' . $action);
}
}
}
/**
* Callback function to validate user related form for abusive words.
*/
function _restrict_abusive_words_user_form_validate(&$form, &$form_state) {
if (isset($form_state['values'])) {
$search_string = _restrict_abusive_words_get_words_list();
foreach ($form_state['values'] as $key => $fields) {
$check_word = FALSE;
if (stripos($key, 'field_') !== FALSE) {
if (count($fields[LANGUAGE_NONE]) > 0) {
foreach ($fields[LANGUAGE_NONE] as $f_key => $val) {
if (count($val) > 0) {
if (isset($val['value'])) {
$check_word = _restrict_abusive_words_search_words($search_string, $form_state['values'][$key][LANGUAGE_NONE][$f_key]['value']);
}
}
}
}
}
elseif ($key == 'name' || $key == 'mail') {
$check_word = _restrict_abusive_words_search_words($search_string, $form_state['values'][$key]);
}
if ($check_word !== FALSE) {
_restrict_abusive_words_validation_message($key, $check_word);
}
}
}
}
/**
* Callback function to validate web form for abusive words.
*/
function _restrict_abusive_words_web_form_validate(&$form, &$form_state) {
if (isset($form_state['values'])) {
$search_string = _restrict_abusive_words_get_words_list();
foreach ($form_state['values']['submitted'] as $key => $fields) {
$check_word = FALSE;
if (is_string($fields)) {
$check_word = _restrict_abusive_words_search_words($search_string, $form_state['values']['submitted'][$key]);
}
if ($check_word !== FALSE) {
_restrict_abusive_words_validation_message($key, $check_word);
}
}
}
}
/**
* Callback function to validate node form for abusive words.
*/
function _restrict_abusive_words_node_form_validate(&$form, &$form_state) {
if (isset($form_state['values'])) {
$search_string = _restrict_abusive_words_get_words_list();
foreach ($form_state['values'] as $key => $fields) {
$check_word = FALSE;
if (stripos($key, 'field_') !== FALSE || $key == 'body') {
if (count($fields[LANGUAGE_NONE]) > 0) {
foreach ($fields[LANGUAGE_NONE] as $f_key => $val) {
if (count($val) > 0) {
if (isset($val['value'])) {
$check_word = _restrict_abusive_words_search_words($search_string, $form_state['values'][$key][LANGUAGE_NONE][$f_key]['value']);
}
}
}
}
}
elseif ($key == 'title') {
$check_word = _restrict_abusive_words_search_words($search_string, $form_state['values'][$key]);
}
if ($check_word !== FALSE) {
_restrict_abusive_words_validation_message($key, $check_word);
}
}
}
}
/**
* Callback function to validate node comment form for abusive words.
*/
function _restrict_abusive_words_comment_form_validate(&$form, &$form_state) {
if (isset($form_state['values'])) {
$search_string = _restrict_abusive_words_get_words_list();
foreach ($form_state['values'] as $key => $fields) {
$check_word = FALSE;
if (stripos($key, 'field_') !== FALSE || $key == 'comment_body') {
if (count($fields[LANGUAGE_NONE]) > 0) {
foreach ($fields[LANGUAGE_NONE] as $f_key => $val) {
if (count($val) > 0) {
if (isset($val['value'])) {
$check_word = _restrict_abusive_words_search_words($search_string, $form_state['values'][$key][LANGUAGE_NONE][$f_key]['value']);
}
}
}
}
}
elseif ($key == 'subject') {
$check_word = _restrict_abusive_words_search_words($search_string, $form_state['values'][$key]);
}
if ($check_word !== FALSE) {
_restrict_abusive_words_validation_message($key, $check_word);
}
}
}
}
/**
* Callback function to submit user related form for abusive words.
*/
function _restrict_abusive_words_user_form_submit(&$form, &$form_state) {
if (isset($form_state['values'])) {
$search_string = _restrict_abusive_words_get_words_list();
foreach ($form_state['values'] as $key => $fields) {
$check_word = FALSE;
if (stripos($key, 'field_') !== FALSE) {
if (count($fields[LANGUAGE_NONE]) > 0) {
foreach ($fields[LANGUAGE_NONE] as $f_key => $val) {
if (count($val) > 0) {
if (isset($val['value'])) {
$check_word = _restrict_abusive_words_search_words($search_string, $form_state['values'][$key][LANGUAGE_NONE][$f_key]['value']);
}
}
}
}
}
elseif ($key == 'name' || $key == 'mail') {
$check_word = _restrict_abusive_words_search_words($search_string, $form_state['values'][$key]);
}
if ($check_word !== FALSE) {
_restrict_abusive_words_submit_message($check_word);
$form_state['values']['status'] = FALSE;
}
}
}
}
/**
* Callback function to submit web form for abusive words.
*/
function _restrict_abusive_words_web_form_submit(&$form, &$form_state) {
if (isset($form_state['values'])) {
$search_string = _restrict_abusive_words_get_words_list();
foreach ($form_state['values']['submitted'] as $key => $fields) {
$check_word = FALSE;
if (is_string($fields)) {
$check_word = _restrict_abusive_words_search_words($search_string, $form_state['values']['submitted'][$key]);
}
if ($check_word !== FALSE) {
_restrict_abusive_words_submit_message($check_word);
$form_state['values']['status'] = FALSE;
}
}
}
}
/**
* Implement _restrict_abusive_words_node_form_submit.
*/
function _restrict_abusive_words_node_form_submit(&$form, &$form_state) {
if (isset($form_state['values'])) {
$search_string = _restrict_abusive_words_get_words_list();
foreach ($form_state['values'] as $key => $fields) {
$check_word = FALSE;
if (stripos($key, 'field_') !== FALSE || $key == 'body') {
if (count($fields[LANGUAGE_NONE]) > 0) {
foreach ($fields[LANGUAGE_NONE] as $f_key => $val) {
if (count($val) > 0) {
if (isset($val['value'])) {
$check_word = _restrict_abusive_words_search_words($search_string, $form_state['values'][$key][LANGUAGE_NONE][$f_key]['value']);
}
}
}
}
}
elseif ($key == 'title') {
$check_word = _restrict_abusive_words_search_words($search_string, $form_state['values'][$key]);
}
if ($check_word !== FALSE) {
$title = $form_state['values']['title'];
$entity = 'Node';
_restrict_abusive_words_submit_message($entity, $title, $check_word);
$form_state['values']['status'] = FALSE;
}
}
}
}
/**
* Callback function to submit node comment form for abusive words.
*/
function _restrict_abusive_words_comment_form_submit(&$form, &$form_state) {
if (isset($form_state['values'])) {
$search_string = _restrict_abusive_words_get_words_list();
foreach ($form_state['values'] as $key => $fields) {
$check_word = FALSE;
if (stripos($key, 'field_') !== FALSE || $key == 'comment_body') {
if (count($fields[LANGUAGE_NONE]) > 0) {
foreach ($fields[LANGUAGE_NONE] as $f_key => $val) {
if (count($val) > 0) {
if (isset($val['value'])) {
$check_word = _restrict_abusive_words_search_words($search_string, $form_state['values'][$key][LANGUAGE_NONE][$f_key]['value']);
}
}
}
}
}
elseif ($key == 'subject') {
$check_word = _restrict_abusive_words_search_words($search_string, $form_state['values'][$key]);
}
if ($check_word !== FALSE) {
$title = $form_state['values']['subject'];
$entity = 'Comment';
_restrict_abusive_words_submit_message($entity, $title, $check_word);
$form_state['values']['status'] = FALSE;
}
}
}
}
/**
* Check the word or phrase is exist in the abusive word list.
*/
function _restrict_abusive_words_search_words($words, $string) {
if (!empty($string) && isset($words)) {
foreach ($words as $word) {
if (preg_match("/\\b{$word}\\b/i", $string)) {
return $word;
}
}
}
return FALSE;
}
/**
* Check the word or phrase is exists in the abusive word list.
*/
function _restrict_abusive_words_exists_words($words, $string) {
if (!empty($string) && isset($words)) {
foreach ($words as $word) {
if ($string == $word) {
return $word;
}
}
}
return FALSE;
}
/**
* Implement _restrict_abusive_words_get_words_list().
*/
function _restrict_abusive_words_get_words_list() {
$output = array();
$results = db_query('Select * FROM {restrict_abusive_words}');
if (count($results) > 0) {
foreach ($results as $result) {
$output[] = $result->words;
}
return $output;
}
return FALSE;
}
/**
* Error message against the field name(machine name).
*/
function _restrict_abusive_words_validation_message($field, $word) {
$message = t("%word is not allowed word to use as it is abusive words. Please correct the word or contact to site administrator.", array(
"%word" => $word,
));
form_set_error($field, $message);
}
/**
* Warning message against the Content.
*/
function _restrict_abusive_words_submit_message($entity, $title, $word) {
$message = "{$title}( {$entity} ) is deactived, please check the word:{$word}, it is a abusive word.";
drupal_set_message($message, 'warning');
}
/**
* Get list of abusive word in autocomplete form.
*/
function _restrict_abusive_words_abusive_word_autocomplete($string) {
if (!empty($string)) {
$matches = array();
$result = db_select('restrict_abusive_words', 'aw')
->fields('aw', array(
'words',
))
->condition('words', db_like($string) . '%', 'LIKE')
->orderBy('words', 'ASC')
->range(0, 10)
->execute();
// Save the query to matches.
foreach ($result as $row) {
$matches[$row->words] = check_plain($row->words);
}
// Return the result to the form in json.
drupal_json_output($matches);
}
}
Functions
Name | Description |
---|---|
restrict_abusive_words_form_alter | Implements hook_form_alter(). |
restrict_abusive_words_help | Implements hook_help(). |
restrict_abusive_words_menu | Implements hook_menu(). |
restrict_abusive_words_permission | Implements hook_permission(). |
_restrict_abusive_words_abusive_word_autocomplete | Get list of abusive word in autocomplete form. |
_restrict_abusive_words_comment_form_submit | Callback function to submit node comment form for abusive words. |
_restrict_abusive_words_comment_form_validate | Callback function to validate node comment form for abusive words. |
_restrict_abusive_words_exists_words | Check the word or phrase is exists in the abusive word list. |
_restrict_abusive_words_get_words_list | Implement _restrict_abusive_words_get_words_list(). |
_restrict_abusive_words_node_form_submit | Implement _restrict_abusive_words_node_form_submit. |
_restrict_abusive_words_node_form_validate | Callback function to validate node form for abusive words. |
_restrict_abusive_words_search_words | Check the word or phrase is exist in the abusive word list. |
_restrict_abusive_words_submit_message | Warning message against the Content. |
_restrict_abusive_words_user_form_submit | Callback function to submit user related form for abusive words. |
_restrict_abusive_words_user_form_validate | Callback function to validate user related form for abusive words. |
_restrict_abusive_words_validation_message | Error message against the field name(machine name). |
_restrict_abusive_words_web_form_submit | Callback function to submit web form for abusive words. |
_restrict_abusive_words_web_form_validate | Callback function to validate web form for abusive words. |