'admin/settings/riddler', 'title' => t('Riddler'), 'description' => t('Allows you to force a question to a number of forms to counter f.e. spammers.'), 'callback' => 'drupal_get_form', 'callback arguments' => array('riddler_settings'), 'access' => $access, 'type' => MENU_NORMAL_ITEM ); return $items; } /* Riddler settings form, called by drupal_get_form in menu */ function riddler_settings() { $form['forms'] = array( '#type' => 'checkboxes', '#title' => t('Activate for the following forms'), '#default_value' => variable_get("riddler_forms", array('comments' => 'comments')), '#options' => array( 'comments' => t('Comments'), //'registration' => t('User registration'), //'forum_topic' => t('Forum topics'), ), ); $form['question'] = array( '#type' => 'textfield', '#title' => t('Question'), '#description' => t('A question that you require anonymous users to answer'), '#default_value' => variable_get("riddler_question", "Do you hate spam? (yes or no)"), '#required' => TRUE, ); $form['answer'] = array( '#type' => 'textfield', '#title' => t('Answer'), '#default_value' => variable_get("riddler_answer", "yes"), '#description' => t('Answer to the above question. Answers are not case sensitive'), '#required' => TRUE, ); return system_settings_form($form); } /* Processing the settings form */ function riddler_settings_submit ($form_id, $form_values) { variable_set("riddler_forms", $form_values['forms']); variable_set("riddler_question", $form_values['question']); variable_set("riddler_answer", $form_values['answer']); } /* Creates the riddle form entries */ function riddler_form () { $form['riddler']['r_question'] = array( '#type' => 'textfield', '#title' => variable_get("riddler_question", "Do you hate spam? (yes or no)"), '#required' => TRUE, '#description' => t('Security question, designed to stop automated spam bots'), '#weight' => 0, ); return $form; } /* Implementation of form_alter hook to inject the riddle to selected forms */ function riddler_form_alter ($form_id, &$form) { global $user; if ($user->uid == 0) { switch ($form_id) { case "comment_form" : $setting = variable_get("riddler_forms", array("comments" => "comments")); if ($setting['comments'] == "comments") { $form = array_merge($form, riddler_form()); } break; } } } /* Implementation of hook_comment to validate the injected field */ function riddler_comment ($a1, $op) { global $user; if ($user->uid == 0) { $setting = variable_get("riddler_forms", array("comments" => "comments")); $answer = variable_get("riddler_answer", "yes"); if ($setting['comments'] == "comments") { switch ($op) { case "validate" : if (strtolower($a1['r_question']) != strtolower($answer)) { form_set_error('r_question', t('Incorrect answer to the security question. Note that all numbers should be written with numeric letters. The question is case insensitive.')); } break; } } } }