Révision ac1bc5de
Ajouté par Assos Assos il y a plus de 9 ans
drupal7/sites/all/modules/captcha/captcha.inc | ||
---|---|---|
8 | 8 |
/** |
9 | 9 |
* Helper function for adding/updating a CAPTCHA point. |
10 | 10 |
* |
11 |
* @param $form_id the form ID to configure. |
|
12 |
* @param captcha_type the setting for the given form_id, can be: |
|
11 |
* @param string $form_id |
|
12 |
* the form ID to configure. |
|
13 |
* |
|
14 |
* @param string $captcha_type |
|
15 |
* the setting for the given form_id, can be: |
|
13 | 16 |
* - 'none' to disable CAPTCHA, |
14 | 17 |
* - 'default' to use the default challenge type |
15 | 18 |
* - NULL to remove the entry for the CAPTCHA type |
16 | 19 |
* - something of the form 'image_captcha/Image' |
17 | 20 |
* - an object with attributes $captcha_type->module and $captcha_type->captcha_type |
18 |
* @return nothing |
|
19 | 21 |
*/ |
20 | 22 |
function captcha_set_form_id_setting($form_id, $captcha_type) { |
21 | 23 |
// Handle 'none'. |
... | ... | |
52 | 54 |
->execute(); |
53 | 55 |
} |
54 | 56 |
else { |
55 |
drupal_set_message(t('Failed to set a CAPTCHA type for form %form_id: could not interpret value "@captcha_type"', |
|
56 |
array('%form_id' => $form_id, '@captcha_type' => (string)$captcha_type)), 'warning'); |
|
57 |
drupal_set_message( |
|
58 |
t('Failed to set a CAPTCHA type for form %form_id: could not interpret value "@captcha_type"', |
|
59 |
array( |
|
60 |
'%form_id' => $form_id, |
|
61 |
'@captcha_type' => (string) $captcha_type, |
|
62 |
) |
|
63 |
), |
|
64 |
'warning' |
|
65 |
); |
|
57 | 66 |
} |
58 | 67 |
} |
59 | 68 |
|
60 | 69 |
/** |
61 | 70 |
* Get the CAPTCHA setting for a given form_id. |
62 | 71 |
* |
63 |
* @param $form_id the form_id to query for |
|
64 |
* @param $symbolic flag to return as (symbolic) strings instead of object. |
|
72 |
* @param string $form_id |
|
73 |
* the form_id to query for |
|
74 |
* |
|
75 |
* @param bool $symbolic |
|
76 |
* flag to return as (symbolic) strings instead of object. |
|
65 | 77 |
* |
66 |
* @return NULL if no setting is known |
|
78 |
* @return NULL |
|
79 |
* if no setting is known |
|
67 | 80 |
* or a captcha_point object with fields 'module' and 'captcha_type'. |
68 | 81 |
* If argument $symbolic is true, returns (symbolic) as 'none', 'default' |
69 | 82 |
* or in the form 'captcha/Math'. |
70 | 83 |
*/ |
71 |
function captcha_get_form_id_setting($form_id, $symbolic=FALSE) {
|
|
84 |
function captcha_get_form_id_setting($form_id, $symbolic = FALSE) {
|
|
72 | 85 |
// Fetch setting from database. |
73 |
$result = db_query("SELECT module, captcha_type FROM {captcha_points} WHERE form_id = :form_id", |
|
74 |
array(':form_id' => $form_id)); |
|
86 |
$result = db_query("SELECT module, captcha_type FROM {captcha_points} WHERE form_id = :form_id", array(':form_id' => $form_id)); |
|
75 | 87 |
$captcha_point = $result->fetchObject(); |
76 | 88 |
|
77 | 89 |
// If no setting is available in database for the given form, |
78 |
// but 'captcha_default_challenge_on_nonlisted_forms' is enabled, pick the default type anyway |
|
79 |
if (!$captcha_point && variable_get('captcha_default_challenge_on_nonlisted_forms', FALSE)) |
|
80 |
{ |
|
90 |
// but 'captcha_default_challenge_on_nonlisted_forms' is enabled, pick the default type anyway. |
|
91 |
if (!$captcha_point && variable_get('captcha_default_challenge_on_nonlisted_forms', FALSE)) { |
|
81 | 92 |
$captcha_point = (object) array('captcha_type' => 'default'); |
82 | 93 |
} |
83 | 94 |
|
... | ... | |
104 | 115 |
return $captcha_point; |
105 | 116 |
} |
106 | 117 |
|
107 |
|
|
108 | 118 |
/** |
109 | 119 |
* Helper function for generating a new CAPTCHA session. |
110 | 120 |
* |
111 |
* @param $form_id the form_id of the form to add a CAPTCHA to. |
|
112 |
* @param $status the initial status of the CAPTHCA session. |
|
113 |
* @return the session ID of the new CAPTCHA session. |
|
121 |
* @param string $form_id |
|
122 |
* the form_id of the form to add a CAPTCHA to. |
|
123 |
* |
|
124 |
* @param int $status |
|
125 |
* the initial status of the CAPTHCA session. |
|
126 |
* |
|
127 |
* @return int |
|
128 |
* the session ID of the new CAPTCHA session. |
|
114 | 129 |
*/ |
115 |
function _captcha_generate_captcha_session($form_id=NULL, $status=CAPTCHA_STATUS_UNSOLVED) {
|
|
130 |
function _captcha_generate_captcha_session($form_id = NULL, $status = CAPTCHA_STATUS_UNSOLVED) {
|
|
116 | 131 |
global $user; |
117 | 132 |
// Initialize solution with random data. |
118 | 133 |
$solution = md5(mt_rand()); |
... | ... | |
135 | 150 |
/** |
136 | 151 |
* Helper function for updating the solution in the CAPTCHA session table. |
137 | 152 |
* |
138 |
* @param $captcha_sid the CAPTCHA session ID to update. |
|
139 |
* @param $solution the new solution to associate with the given CAPTCHA session. |
|
153 |
* @param int $captcha_sid |
|
154 |
* the CAPTCHA session ID to update. |
|
155 |
* |
|
156 |
* @param string $solution |
|
157 |
* the new solution to associate with the given CAPTCHA session. |
|
140 | 158 |
*/ |
141 | 159 |
function _captcha_update_captcha_session($captcha_sid, $solution) { |
142 | 160 |
db_update('captcha_sessions') |
... | ... | |
149 | 167 |
} |
150 | 168 |
|
151 | 169 |
/** |
152 |
* Helper function for checking if CAPTCHA is required for user, |
|
153 |
* based on the CAPTCHA persistence setting, the CAPTCHA session ID and |
|
170 |
* Helper function for checking if CAPTCHA is required for user. |
|
171 |
* |
|
172 |
* Based on the CAPTCHA persistence setting, the CAPTCHA session ID and |
|
154 | 173 |
* user session info. |
155 | 174 |
*/ |
156 | 175 |
function _captcha_required_for_user($captcha_sid, $form_id) { |
... | ... | |
174 | 193 |
return TRUE; |
175 | 194 |
} |
176 | 195 |
else { |
177 |
$captcha_success_form_ids = isset($_SESSION['captcha_success_form_ids']) ? (array)($_SESSION['captcha_success_form_ids']) : array(); |
|
196 |
$captcha_success_form_ids = isset($_SESSION['captcha_success_form_ids']) ? (array) ($_SESSION['captcha_success_form_ids']) : array();
|
|
178 | 197 |
switch ($captcha_persistence) { |
179 | 198 |
case CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL: |
180 | 199 |
return (count($captcha_success_form_ids) == 0); |
200 |
|
|
181 | 201 |
case CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_TYPE: |
182 | 202 |
return !isset($captcha_success_form_ids[$form_id]); |
183 | 203 |
} |
... | ... | |
189 | 209 |
|
190 | 210 |
|
191 | 211 |
/** |
192 |
* Get the CAPTCHA description as configured on the general CAPTCHA |
|
193 |
* settings page. |
|
212 |
* Get the CAPTCHA description as configured on the general CAPTCHA settings page. |
|
194 | 213 |
* |
195 | 214 |
* If the locale module is enabled, the description will be returned |
196 | 215 |
* for the current language the page is rendered for. This language |
197 |
* can optionally been overriden with the $lang_code argument. |
|
216 |
* can optionally been overridden with the $lang_code argument.
|
|
198 | 217 |
* |
199 |
* @param $lang_code an optional language code to get the descripion for. |
|
200 |
* @return a string with (localized) CAPTCHA description. |
|
218 |
* @param string|null $lang_code |
|
219 |
* an optional language code to get the description for. |
|
220 |
* |
|
221 |
* @return string |
|
222 |
* String with (localized) CAPTCHA description. |
|
201 | 223 |
*/ |
202 |
function _captcha_get_description($lang_code=NULL) {
|
|
224 |
function _captcha_get_description($lang_code = NULL) {
|
|
203 | 225 |
// If no language code is given: use the language of the current page. |
204 | 226 |
global $language; |
205 | 227 |
$lang_code = isset($lang_code) ? $lang_code : $language->language; |
... | ... | |
217 | 239 |
|
218 | 240 |
/** |
219 | 241 |
* Parse or interpret the given captcha_type. |
220 |
* @param $captcha_type string representation of the CAPTCHA type, |
|
242 |
* |
|
243 |
* @param string $captcha_type |
|
244 |
* string representation of the CAPTCHA type, |
|
221 | 245 |
* e.g. 'default', 'none', 'captcha/Math', 'image_captcha/Image' |
222 |
* @return list($captcha_module, $captcha_type) |
|
246 |
* |
|
247 |
* @return array |
|
248 |
* list($captcha_module, $captcha_type) |
|
223 | 249 |
*/ |
224 | 250 |
function _captcha_parse_captcha_type($captcha_type) { |
225 | 251 |
if ($captcha_type == 'none') { |
... | ... | |
233 | 259 |
|
234 | 260 |
/** |
235 | 261 |
* Helper function to get placement information for a given form_id. |
236 |
* @param $form_id the form_id to get the placement information for. |
|
237 |
* @param $form if a form corresponding to the given form_id, if there |
|
262 |
* |
|
263 |
* @param string $form_id |
|
264 |
* the form_id to get the placement information for. |
|
265 |
* |
|
266 |
* @param array $form |
|
267 |
* if a form corresponding to the given form_id, if there |
|
238 | 268 |
* is no placement info for the given form_id, this form is examined to |
239 | 269 |
* guess the placement. |
240 |
* @return placement info array (@see _captcha_insert_captcha_element() for more |
|
270 |
* |
|
271 |
* @return array |
|
272 |
* placement info array (@see _captcha_insert_captcha_element() for more |
|
241 | 273 |
* info about the fields 'path', 'key' and 'weight'. |
242 | 274 |
*/ |
243 | 275 |
function _captcha_get_captcha_placement($form_id, $form) { |
... | ... | |
298 | 330 |
/** |
299 | 331 |
* Helper function for searching the buttons in a form. |
300 | 332 |
* |
301 |
* @param $form the form to search button elements in |
|
302 |
* @return an array of paths to the buttons. |
|
333 |
* @param array $form |
|
334 |
* the form to search button elements in |
|
335 |
* |
|
336 |
* @return array |
|
337 |
* an array of paths to the buttons. |
|
303 | 338 |
* A path is an array of keys leading to the button, the last |
304 | 339 |
* item in the path is the weight of the button element |
305 | 340 |
* (or NULL if undefined). |
... | ... | |
328 | 363 |
|
329 | 364 |
/** |
330 | 365 |
* Helper function to insert a CAPTCHA element in a form before a given form element. |
331 |
* @param $form the form to add the CAPTCHA element to. |
|
332 |
* @param $placement information where the CAPTCHA element should be inserted. |
|
366 |
* |
|
367 |
* @param array $form |
|
368 |
* the form to add the CAPTCHA element to. |
|
369 |
* |
|
370 |
* @param array $placement |
|
371 |
* information where the CAPTCHA element should be inserted. |
|
333 | 372 |
* $placement should be an associative array with fields: |
334 | 373 |
* - 'path': path (array of path items) of the container in the form where the |
335 | 374 |
* CAPTCHA element should be inserted. |
... | ... | |
339 | 378 |
* - 'weight': if 'key' is not NULL: should be the weight of the element defined by 'key'. |
340 | 379 |
* If 'key' is NULL and weight is not NULL: set the weight property of the CAPTCHA element |
341 | 380 |
* to this value. |
342 |
* @param $captcha_element the CAPTCHA element to insert. |
|
381 |
* |
|
382 |
* @param array $captcha_element |
|
383 |
* the CAPTCHA element to insert. |
|
343 | 384 |
*/ |
344 | 385 |
function _captcha_insert_captcha_element(&$form, $placement, $captcha_element) { |
345 | 386 |
// Get path, target and target weight or use defaults if not available. |
... | ... | |
367 | 408 |
if ($target_weight != NULL) { |
368 | 409 |
$captcha_element['#weight'] = $target_weight; |
369 | 410 |
} |
370 |
$form_stepper['captcha'] = $captcha_element;
|
|
411 |
$form_stepper['captcha'] = $captcha_element; |
|
371 | 412 |
} |
372 | 413 |
// If there is a target available: make sure the CAPTCHA element comes right before it. |
373 | 414 |
else { |
... | ... | |
375 | 416 |
// and just append the CAPTCHA: sorting will fix the ordering anyway. |
376 | 417 |
if ($target_weight != NULL) { |
377 | 418 |
$captcha_element['#weight'] = $target_weight - .1; |
378 |
$form_stepper['captcha'] = $captcha_element;
|
|
419 |
$form_stepper['captcha'] = $captcha_element; |
|
379 | 420 |
} |
380 | 421 |
else { |
381 | 422 |
// If we can't play with weights: insert the CAPTCHA element at the right position. |
... | ... | |
384 | 425 |
// chop of the end, append the CAPTCHA element and put the end back. |
385 | 426 |
$offset = array_search($target_key, array_keys($form_stepper)); |
386 | 427 |
$end = array_splice($form_stepper, $offset); |
387 |
$form_stepper['captcha'] = $captcha_element;
|
|
428 |
$form_stepper['captcha'] = $captcha_element; |
|
388 | 429 |
foreach ($end as $k => $v) { |
389 | 430 |
$form_stepper[$k] = $v; |
390 | 431 |
} |
391 | 432 |
} |
392 | 433 |
} |
393 | 434 |
} |
394 |
|
Formats disponibles : Unified diff
Weekly update of contrib modules