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

Contents of /contributions/modules/riddler/riddler.module

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


Revision 1.4 - (show annotations) (download) (as text)
Fri Jul 3 12:19:39 2009 UTC (4 months, 3 weeks ago) by awolfey
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, DRUPAL-6--1-1, HEAD
Changes since 1.3: +4 -4 lines
File MIME type: text/x-php
Updated to work with Captcha 6.x-2.x-dev from July 02, 2009.
Minor instructions change.
1 <?php
2 // $Id: riddler.module,v 1.3 2009/06/26 15:48:16 awolfey Exp $
3
4 /**
5 * @file
6 * Adds a question and answer type to the Captcha module.
7 *
8 * When enabled this module will add a required field to forms of your choice
9 * asking a simple question to avoid automatic content creation by
10 * spambots. The question and answers are custimizable.
11 * Thanks to: nkoponen. Ported to D6 by awolfey.
12 */
13
14 /**
15 * Implementation of hook_help().
16 */
17
18 function riddler_help($path, $arg) {
19 switch ($path) {
20 case 'admin/modules#description':
21 return t('Requires anonymous users to answer a simple question to be answered forms are processed. A primitive but effective way to counter spam.');
22 break;
23 }
24 }
25
26 /**
27 * Implementation of hook_perm().
28 */
29
30 function riddler_perm() {
31 return array(
32 'administer riddler',
33 );
34 }
35
36 /**
37 * Implementation of hook_menu().
38 */
39
40 function riddler_menu() {
41 $items = array();
42 $items['admin/user/captcha/riddler'] = array(
43 'title' => 'Riddler',
44 'description' => 'Allows you to force a question to a number of forms to counter f.e. spammers.',
45 'page callback' => 'drupal_get_form',
46 'page arguments' => array('riddler_settings'),
47 'access arguments' => array('administer riddler'),
48 'type' => MENU_LOCAL_TASK
49 );
50 return $items;
51 }
52
53 // Riddler settings form, called by drupal_get_form in menu.
54 function riddler_settings() {
55 $form = array();
56 $form['riddler_weight'] = array(
57 '#type' => 'select',
58 '#title' => t('Weight'),
59 '#default_value' => variable_get('riddler_weight', 0),
60 '#options' => drupal_map_assoc(range(-10, 10)),
61 '#description' => t('Weight of the Riddler form element'),
62 '#required' => TRUE,
63 );
64 $form['riddler_groups'] = array(
65 '#type' => 'fieldset',
66 '#title' => t('Riddles'),
67 '#description' => t('If you need more riddles, just submit the form, 3 empty riddles will appear.'),
68 );
69 $form['riddler_groups']['riddler_group_0'] = array(
70 '#type' => 'fieldset',
71 '#title' => variable_get('riddler_question_0', '') ? 'Default riddle: '. variable_get('riddler_question_0', '') : t('Riddle !i', array('!i' => 1)),
72 '#description' => t('Empty question and answer to erase this group.'),
73 '#collapsible' => TRUE,
74 '#collapsed' => TRUE,
75 );
76 $form['riddler_groups']['riddler_group_0']['riddler_question_0'] = array(
77 '#type' => 'textfield',
78 '#title' => t('Question'),
79 '#description' => t('A question that you require anonymous users to answer'),
80 '#default_value' => variable_get('riddler_question_0', 'Do you hate spam? (yes or no)'),
81 '#required' => FALSE,
82 );
83 $form['riddler_groups']['riddler_group_0']['riddler_answer_0'] = array(
84 '#type' => 'textfield',
85 '#title' => t('Answer'),
86 '#default_value' => variable_get('riddler_answer_0', 'yes'),
87 '#description' => t('Answer to the above question. You may allow more than one correct answer by entering a comma or space-separated list. Answers are not case sensitive. Answers must be only one word.'),
88 '#required' => FALSE,
89 );
90 $number = variable_get('riddler_number', 1);
91 for ( $i=1; $i<$number + 3; $i++) {
92 $form['riddler_groups']['riddler_group_'. $i] = array(
93 '#type' => 'fieldset',
94 '#title' => variable_get('riddler_question_'. $i, '') ? 'Riddle '. $i . ': '. variable_get('riddler_question_'. $i, '') : t('Riddle !i', array('!i' => $i)),
95 '#description' => t('Empty question and answer to erase this group.'),
96 '#collapsible' => TRUE,
97 '#collapsed' => $i < $number,
98 );
99 $form['riddler_groups']['riddler_group_'. $i]['riddler_question_'. $i] = array(
100 '#type' => 'textfield',
101 '#title' => t('Question'),
102 '#description' => t('A question that you require anonymous users to answer'),
103 '#default_value' => variable_get('riddler_question_'. $i, ''),
104 '#required' => FALSE,
105 );
106 $form['riddler_groups']['riddler_group_'. $i]['riddler_answer_'. $i] = array(
107 '#type' => 'textfield',
108 '#title' => t('Answer'),
109 '#default_value' => variable_get('riddler_answer_'. $i, ''),
110 '#description' => t('Answer to the above question. You may allow more than one correct answer by entering a comma or space-separated list. Answers are not case sensitive. Answers must be only one word.'),
111 '#required' => FALSE,
112 );
113 }
114 $form['#validate'][] = 'riddler_settings_validate';
115 $form['#submit'][] = 'riddler_settings_submit';
116
117 return system_settings_form($form);
118 }
119
120 // Validating the settings form.
121 function riddler_settings_validate($form, &$form_state) {
122 $i = 0;
123 while (array_key_exists('riddler_question_'. $i, $form_state['values'])) {
124 if ($form_state['values']['riddler_question_'. $i] != '' && $form_state['values']['riddler_answer_'. $i] == '') {
125 form_set_error('riddler_answer_'. $i, t('Group !i is incomplete (answer is missing).', array('!i' => $i+1)));
126 }
127 if ($form_state['values']['riddler_question_'. $i] == '' && $form_state['values']['riddler_answer_'. $i] != '') {
128 form_set_error('riddler_question_'. $i, t('Group !i is incomplete (question is missing).', array('!i' => $i+1)));
129 }
130 $i++;
131 }
132 }
133
134 // Processing the settings form.
135 function riddler_settings_submit($form, &$form_state) {
136 variable_set('riddler_weight', $form_state['values']['riddler_weight']);
137 $i = 0;
138 $number = 0;
139 while (array_key_exists('riddler_question_'. $i, $form_state['values'])) {
140 if ($form_state['values']['riddler_question_'. $i] != '') {
141 variable_set('riddler_question_'. $number, $form_state['values']['riddler_question_'. $i]);
142 variable_set('riddler_answer_'. $number, $form_state['values']['riddler_answer_'. $i]);
143 $number++;
144 }
145 $i++;
146 }
147 variable_set('riddler_number', $number);
148 }
149
150 /**
151 * Implementation of hook_captcha().
152 */
153 function riddler_captcha($op, $captcha_type='') {
154 switch ($op) {
155 case 'list' :
156 return array('Riddler');
157 break;
158 case 'generate' :
159 $result = array();
160 if ($captcha_type == 'Riddler') {
161 $i = rand(1, variable_get('riddler_number', 1)) - 1;
162 $result['form']['captcha_response'] = array(
163 '#type' => 'textfield',
164 '#title' => variable_get('riddler_question_'. $i, 'Do you hate spam? (yes or no)'),
165 '#description' => t('Fill in the blank'),
166 '#size' => 50,
167 '#maxlength' => 50,
168 '#required' => TRUE,
169 '#weight' => variable_get('riddler_weight', 0),
170 );
171 $result['solution'] = (string)(drupal_strtolower(variable_get('riddler_answer_'. $i, 'yes')));
172 $result['captcha_validate'] = 'riddler_captcha_validate';
173 return $result;
174 }
175 break;
176 }
177 }
178
179 // Custom captcha validation.
180 function riddler_captcha_validate($solution, $response) {
181 $solution = str_ireplace(',', '', $solution);
182 $solution = explode(' ', $solution);
183 return in_array(drupal_strtolower($response), $solution);
184 }

  ViewVC Help
Powered by ViewVC 1.1.2