danielebarchiesi@2
|
1 <?php
|
danielebarchiesi@2
|
2
|
danielebarchiesi@2
|
3 /**
|
danielebarchiesi@2
|
4 * @file
|
danielebarchiesi@2
|
5 * General CAPTCHA functionality and helper functions.
|
danielebarchiesi@2
|
6 */
|
danielebarchiesi@2
|
7
|
danielebarchiesi@2
|
8 /**
|
danielebarchiesi@2
|
9 * Helper function for adding/updating a CAPTCHA point.
|
danielebarchiesi@2
|
10 *
|
danielebarchiesi@2
|
11 * @param $form_id the form ID to configure.
|
danielebarchiesi@2
|
12 * @param captcha_type the setting for the given form_id, can be:
|
danielebarchiesi@2
|
13 * - 'none' to disable CAPTCHA,
|
danielebarchiesi@2
|
14 * - 'default' to use the default challenge type
|
danielebarchiesi@2
|
15 * - NULL to remove the entry for the CAPTCHA type
|
danielebarchiesi@2
|
16 * - something of the form 'image_captcha/Image'
|
danielebarchiesi@2
|
17 * - an object with attributes $captcha_type->module and $captcha_type->captcha_type
|
danielebarchiesi@2
|
18 * @return nothing
|
danielebarchiesi@2
|
19 */
|
danielebarchiesi@2
|
20 function captcha_set_form_id_setting($form_id, $captcha_type) {
|
danielebarchiesi@2
|
21 // Handle 'none'.
|
danielebarchiesi@2
|
22 if ($captcha_type == 'none') {
|
danielebarchiesi@2
|
23 db_merge('captcha_points')
|
danielebarchiesi@2
|
24 ->key(array('form_id' => $form_id))
|
danielebarchiesi@2
|
25 ->fields(array('module' => NULL, 'captcha_type' => NULL))
|
danielebarchiesi@2
|
26 ->execute();
|
danielebarchiesi@2
|
27 }
|
danielebarchiesi@2
|
28 // Handle 'default'.
|
danielebarchiesi@2
|
29 elseif ($captcha_type == 'default') {
|
danielebarchiesi@2
|
30 db_merge('captcha_points')
|
danielebarchiesi@2
|
31 ->key(array('form_id' => $form_id))
|
danielebarchiesi@2
|
32 ->fields(array('module' => NULL, 'captcha_type' => 'default'))
|
danielebarchiesi@2
|
33 ->execute();
|
danielebarchiesi@2
|
34 }
|
danielebarchiesi@2
|
35 // Handle NULL.
|
danielebarchiesi@2
|
36 elseif ($captcha_type == NULL) {
|
danielebarchiesi@2
|
37 db_delete('captcha_points')->condition('form_id', $form_id)->execute();
|
danielebarchiesi@2
|
38 }
|
danielebarchiesi@2
|
39 // Handle a captcha_type object.
|
danielebarchiesi@2
|
40 elseif (is_object($captcha_type) && isset($captcha_type->module) && isset($captcha_type->captcha_type)) {
|
danielebarchiesi@2
|
41 db_merge('captcha_points')
|
danielebarchiesi@2
|
42 ->key(array('form_id' => $form_id))
|
danielebarchiesi@2
|
43 ->fields(array('module' => $captcha_type->module, 'captcha_type' => $captcha_type->captcha_type))
|
danielebarchiesi@2
|
44 ->execute();
|
danielebarchiesi@2
|
45 }
|
danielebarchiesi@2
|
46 // Handle a captcha_type string.
|
danielebarchiesi@2
|
47 elseif (is_string($captcha_type) && substr_count($captcha_type, '/') == 1) {
|
danielebarchiesi@2
|
48 list($module, $type) = explode('/', $captcha_type);
|
danielebarchiesi@2
|
49 db_merge('captcha_points')
|
danielebarchiesi@2
|
50 ->key(array('form_id' => $form_id))
|
danielebarchiesi@2
|
51 ->fields(array('module' => $module, 'captcha_type' => $type))
|
danielebarchiesi@2
|
52 ->execute();
|
danielebarchiesi@2
|
53 }
|
danielebarchiesi@2
|
54 else {
|
danielebarchiesi@2
|
55 drupal_set_message(t('Failed to set a CAPTCHA type for form %form_id: could not interpret value "@captcha_type"',
|
danielebarchiesi@2
|
56 array('%form_id' => $form_id, '@captcha_type' => (string)$captcha_type)), 'warning');
|
danielebarchiesi@2
|
57 }
|
danielebarchiesi@2
|
58 }
|
danielebarchiesi@2
|
59
|
danielebarchiesi@2
|
60 /**
|
danielebarchiesi@2
|
61 * Get the CAPTCHA setting for a given form_id.
|
danielebarchiesi@2
|
62 *
|
danielebarchiesi@2
|
63 * @param $form_id the form_id to query for
|
danielebarchiesi@2
|
64 * @param $symbolic flag to return as (symbolic) strings instead of object.
|
danielebarchiesi@2
|
65 *
|
danielebarchiesi@2
|
66 * @return NULL if no setting is known
|
danielebarchiesi@2
|
67 * or a captcha_point object with fields 'module' and 'captcha_type'.
|
danielebarchiesi@2
|
68 * If argument $symbolic is true, returns (symbolic) as 'none', 'default'
|
danielebarchiesi@2
|
69 * or in the form 'captcha/Math'.
|
danielebarchiesi@2
|
70 */
|
danielebarchiesi@2
|
71 function captcha_get_form_id_setting($form_id, $symbolic=FALSE) {
|
danielebarchiesi@2
|
72 $result = db_query("SELECT module, captcha_type FROM {captcha_points} WHERE form_id = :form_id",
|
danielebarchiesi@2
|
73 array(':form_id' => $form_id));
|
danielebarchiesi@2
|
74 $captcha_point = $result->fetchObject();
|
danielebarchiesi@2
|
75 if (!$captcha_point) {
|
danielebarchiesi@2
|
76 $captcha_point = NULL;
|
danielebarchiesi@2
|
77 }
|
danielebarchiesi@2
|
78 elseif ($captcha_point->captcha_type == 'default') {
|
danielebarchiesi@2
|
79 if (!$symbolic) {
|
danielebarchiesi@2
|
80 list($module, $type) = explode('/', variable_get('captcha_default_challenge', 'captcha/Math'));
|
danielebarchiesi@2
|
81 $captcha_point->module = $module;
|
danielebarchiesi@2
|
82 $captcha_point->captcha_type = $type;
|
danielebarchiesi@2
|
83 }
|
danielebarchiesi@2
|
84 else {
|
danielebarchiesi@2
|
85 $captcha_point = 'default';
|
danielebarchiesi@2
|
86 }
|
danielebarchiesi@2
|
87 }
|
danielebarchiesi@2
|
88 elseif ($captcha_point->module == NULL && $captcha_point->captcha_type == NULL && $symbolic) {
|
danielebarchiesi@2
|
89 $captcha_point = 'none';
|
danielebarchiesi@2
|
90 }
|
danielebarchiesi@2
|
91 elseif ($symbolic) {
|
danielebarchiesi@2
|
92 $captcha_point = $captcha_point->module . '/' . $captcha_point->captcha_type;
|
danielebarchiesi@2
|
93 }
|
danielebarchiesi@2
|
94 return $captcha_point;
|
danielebarchiesi@2
|
95 }
|
danielebarchiesi@2
|
96
|
danielebarchiesi@2
|
97
|
danielebarchiesi@2
|
98 /**
|
danielebarchiesi@2
|
99 * Helper function for generating a new CAPTCHA session.
|
danielebarchiesi@2
|
100 *
|
danielebarchiesi@2
|
101 * @param $form_id the form_id of the form to add a CAPTCHA to.
|
danielebarchiesi@2
|
102 * @param $status the initial status of the CAPTHCA session.
|
danielebarchiesi@2
|
103 * @return the session ID of the new CAPTCHA session.
|
danielebarchiesi@2
|
104 */
|
danielebarchiesi@2
|
105 function _captcha_generate_captcha_session($form_id=NULL, $status=CAPTCHA_STATUS_UNSOLVED) {
|
danielebarchiesi@2
|
106 global $user;
|
danielebarchiesi@2
|
107 // Initialize solution with random data.
|
danielebarchiesi@2
|
108 $solution = md5(mt_rand());
|
danielebarchiesi@2
|
109 // Insert an entry and thankfully receive the value of the autoincrement field 'csid'.
|
danielebarchiesi@2
|
110 $captcha_sid = db_insert('captcha_sessions')
|
danielebarchiesi@2
|
111 ->fields(array(
|
danielebarchiesi@2
|
112 'uid' => $user->uid,
|
danielebarchiesi@2
|
113 'sid' => session_id(),
|
danielebarchiesi@2
|
114 'ip_address' => ip_address(),
|
danielebarchiesi@2
|
115 'timestamp' => REQUEST_TIME,
|
danielebarchiesi@2
|
116 'form_id' => $form_id,
|
danielebarchiesi@2
|
117 'solution' => $solution,
|
danielebarchiesi@2
|
118 'status' => $status,
|
danielebarchiesi@2
|
119 'attempts' => 0,
|
danielebarchiesi@2
|
120 ))
|
danielebarchiesi@2
|
121 ->execute();
|
danielebarchiesi@2
|
122 return $captcha_sid;
|
danielebarchiesi@2
|
123 }
|
danielebarchiesi@2
|
124
|
danielebarchiesi@2
|
125 /**
|
danielebarchiesi@2
|
126 * Helper function for updating the solution in the CAPTCHA session table.
|
danielebarchiesi@2
|
127 *
|
danielebarchiesi@2
|
128 * @param $captcha_sid the CAPTCHA session ID to update.
|
danielebarchiesi@2
|
129 * @param $solution the new solution to associate with the given CAPTCHA session.
|
danielebarchiesi@2
|
130 */
|
danielebarchiesi@2
|
131 function _captcha_update_captcha_session($captcha_sid, $solution) {
|
danielebarchiesi@2
|
132 db_update('captcha_sessions')
|
danielebarchiesi@2
|
133 ->condition('csid', $captcha_sid)
|
danielebarchiesi@2
|
134 ->fields(array(
|
danielebarchiesi@2
|
135 'timestamp' => REQUEST_TIME,
|
danielebarchiesi@2
|
136 'solution' => $solution,
|
danielebarchiesi@2
|
137 ))
|
danielebarchiesi@2
|
138 ->execute();
|
danielebarchiesi@2
|
139 }
|
danielebarchiesi@2
|
140
|
danielebarchiesi@2
|
141 /**
|
danielebarchiesi@2
|
142 * Helper function for checking if CAPTCHA is required for user,
|
danielebarchiesi@2
|
143 * based on the CAPTCHA persistence setting, the CAPTCHA session ID and
|
danielebarchiesi@2
|
144 * user session info.
|
danielebarchiesi@2
|
145 */
|
danielebarchiesi@2
|
146 function _captcha_required_for_user($captcha_sid, $form_id) {
|
danielebarchiesi@2
|
147 // Get the CAPTCHA persistence setting.
|
danielebarchiesi@2
|
148 $captcha_persistence = variable_get('captcha_persistence', CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE);
|
danielebarchiesi@2
|
149
|
danielebarchiesi@2
|
150 // First check: should we always add a CAPTCHA?
|
danielebarchiesi@2
|
151 if ($captcha_persistence == CAPTCHA_PERSISTENCE_SHOW_ALWAYS) {
|
danielebarchiesi@2
|
152 return TRUE;
|
danielebarchiesi@2
|
153 }
|
danielebarchiesi@2
|
154
|
danielebarchiesi@2
|
155 // Get the status of the current CAPTCHA session.
|
danielebarchiesi@2
|
156 $captcha_session_status = db_query('SELECT status FROM {captcha_sessions} WHERE csid = :csid', array(':csid' => $captcha_sid))->fetchField();
|
danielebarchiesi@2
|
157 // Second check: if the current session is already solved: omit further CAPTCHAs.
|
danielebarchiesi@2
|
158 if ($captcha_session_status == CAPTCHA_STATUS_SOLVED) {
|
danielebarchiesi@2
|
159 return FALSE;
|
danielebarchiesi@2
|
160 }
|
danielebarchiesi@2
|
161
|
danielebarchiesi@2
|
162 // Third check: look at the persistence level (per form instance, per form or per user).
|
danielebarchiesi@2
|
163 if ($captcha_persistence == CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE) {
|
danielebarchiesi@2
|
164 return TRUE;
|
danielebarchiesi@2
|
165 }
|
danielebarchiesi@2
|
166 else {
|
danielebarchiesi@2
|
167 $captcha_success_form_ids = isset($_SESSION['captcha_success_form_ids']) ? (array)($_SESSION['captcha_success_form_ids']) : array();
|
danielebarchiesi@2
|
168 switch ($captcha_persistence) {
|
danielebarchiesi@2
|
169 case CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL:
|
danielebarchiesi@2
|
170 return (count($captcha_success_form_ids) == 0);
|
danielebarchiesi@2
|
171 case CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_TYPE:
|
danielebarchiesi@2
|
172 return !isset($captcha_success_form_ids[$form_id]);
|
danielebarchiesi@2
|
173 }
|
danielebarchiesi@2
|
174 }
|
danielebarchiesi@2
|
175
|
danielebarchiesi@2
|
176 // We should never get to this point, but to be sure, we return TRUE.
|
danielebarchiesi@2
|
177 return TRUE;
|
danielebarchiesi@2
|
178 }
|
danielebarchiesi@2
|
179
|
danielebarchiesi@2
|
180
|
danielebarchiesi@2
|
181 /**
|
danielebarchiesi@2
|
182 * Get the CAPTCHA description as configured on the general CAPTCHA
|
danielebarchiesi@2
|
183 * settings page.
|
danielebarchiesi@2
|
184 *
|
danielebarchiesi@2
|
185 * If the locale module is enabled, the description will be returned
|
danielebarchiesi@2
|
186 * for the current language the page is rendered for. This language
|
danielebarchiesi@2
|
187 * can optionally been overriden with the $lang_code argument.
|
danielebarchiesi@2
|
188 *
|
danielebarchiesi@2
|
189 * @param $lang_code an optional language code to get the descripion for.
|
danielebarchiesi@2
|
190 * @return a string with (localized) CAPTCHA description.
|
danielebarchiesi@2
|
191 */
|
danielebarchiesi@2
|
192 function _captcha_get_description($lang_code=NULL) {
|
danielebarchiesi@2
|
193 // If no language code is given: use the language of the current page.
|
danielebarchiesi@2
|
194 global $language;
|
danielebarchiesi@2
|
195 $lang_code = isset($lang_code) ? $lang_code : $language->language;
|
danielebarchiesi@2
|
196 // The hardcoded but localizable default.
|
danielebarchiesi@2
|
197 $default = t('This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.', array(), array('langcode' => $lang_code));
|
danielebarchiesi@2
|
198 // Look up the configured CAPTCHA description or fall back on the (localized) default.
|
danielebarchiesi@2
|
199 if (module_exists('locale')) {
|
danielebarchiesi@2
|
200 $description = variable_get("captcha_description_$lang_code", $default);
|
danielebarchiesi@2
|
201 }
|
danielebarchiesi@2
|
202 else {
|
danielebarchiesi@2
|
203 $description = variable_get('captcha_description', $default);
|
danielebarchiesi@2
|
204 }
|
danielebarchiesi@2
|
205 return filter_xss_admin($description);
|
danielebarchiesi@2
|
206 }
|
danielebarchiesi@2
|
207
|
danielebarchiesi@2
|
208 /**
|
danielebarchiesi@2
|
209 * Parse or interpret the given captcha_type.
|
danielebarchiesi@2
|
210 * @param $captcha_type string representation of the CAPTCHA type,
|
danielebarchiesi@2
|
211 * e.g. 'default', 'none', 'captcha/Math', 'image_captcha/Image'
|
danielebarchiesi@2
|
212 * @return list($captcha_module, $captcha_type)
|
danielebarchiesi@2
|
213 */
|
danielebarchiesi@2
|
214 function _captcha_parse_captcha_type($captcha_type) {
|
danielebarchiesi@2
|
215 if ($captcha_type == 'none') {
|
danielebarchiesi@2
|
216 return array(NULL, NULL);
|
danielebarchiesi@2
|
217 }
|
danielebarchiesi@2
|
218 if ($captcha_type == 'default') {
|
danielebarchiesi@2
|
219 $captcha_type = variable_get('captcha_default_challenge', 'captcha/Math');
|
danielebarchiesi@2
|
220 }
|
danielebarchiesi@2
|
221 return explode('/', $captcha_type);
|
danielebarchiesi@2
|
222 }
|
danielebarchiesi@2
|
223
|
danielebarchiesi@2
|
224 /**
|
danielebarchiesi@2
|
225 * Helper function to get placement information for a given form_id.
|
danielebarchiesi@2
|
226 * @param $form_id the form_id to get the placement information for.
|
danielebarchiesi@2
|
227 * @param $form if a form corresponding to the given form_id, if there
|
danielebarchiesi@2
|
228 * is no placement info for the given form_id, this form is examined to
|
danielebarchiesi@2
|
229 * guess the placement.
|
danielebarchiesi@2
|
230 * @return placement info array (@see _captcha_insert_captcha_element() for more
|
danielebarchiesi@2
|
231 * info about the fields 'path', 'key' and 'weight'.
|
danielebarchiesi@2
|
232 */
|
danielebarchiesi@2
|
233 function _captcha_get_captcha_placement($form_id, $form) {
|
danielebarchiesi@2
|
234 // Get CAPTCHA placement map from cache. Two levels of cache:
|
danielebarchiesi@2
|
235 // static variable in this function and storage in the variables table.
|
danielebarchiesi@2
|
236 static $placement_map = NULL;
|
danielebarchiesi@2
|
237 // Try first level cache.
|
danielebarchiesi@2
|
238 if ($placement_map === NULL) {
|
danielebarchiesi@2
|
239 // If first level cache missed: try second level cache.
|
danielebarchiesi@2
|
240 $placement_map = variable_get('captcha_placement_map_cache', NULL);
|
danielebarchiesi@2
|
241
|
danielebarchiesi@2
|
242 if ($placement_map === NULL) {
|
danielebarchiesi@2
|
243 // If second level cache missed: initialize the placement map
|
danielebarchiesi@2
|
244 // and let other modules hook into this with the hook_captcha_placement_map hook.
|
danielebarchiesi@2
|
245 // By default however, probably all Drupal core forms are already correctly
|
danielebarchiesi@2
|
246 // handled with the best effort guess based on the 'actions' element (see below).
|
danielebarchiesi@2
|
247 $placement_map = module_invoke_all('captcha_placement_map');
|
danielebarchiesi@2
|
248 }
|
danielebarchiesi@2
|
249 }
|
danielebarchiesi@2
|
250
|
danielebarchiesi@2
|
251 // Query the placement map.
|
danielebarchiesi@2
|
252 if (array_key_exists($form_id, $placement_map)) {
|
danielebarchiesi@2
|
253 $placement = $placement_map[$form_id];
|
danielebarchiesi@2
|
254 }
|
danielebarchiesi@2
|
255 // If no placement info is available in placement map: make a best effort guess.
|
danielebarchiesi@2
|
256 else {
|
danielebarchiesi@2
|
257 // If there is an "actions" button group, a good placement is just before that.
|
danielebarchiesi@2
|
258 if (isset($form['actions']) && isset($form['actions']['#type']) && $form['actions']['#type'] === 'actions') {
|
danielebarchiesi@2
|
259 $placement = array(
|
danielebarchiesi@2
|
260 'path' => array(),
|
danielebarchiesi@2
|
261 'key' => 'actions',
|
danielebarchiesi@2
|
262 // #type 'actions' defaults to 100.
|
danielebarchiesi@2
|
263 'weight' => (isset($form['actions']['#weight']) ? $form['actions']['#weight'] - 1 : 99),
|
danielebarchiesi@2
|
264 );
|
danielebarchiesi@2
|
265 }
|
danielebarchiesi@2
|
266 else {
|
danielebarchiesi@2
|
267 // Search the form for buttons and guess placement from it.
|
danielebarchiesi@2
|
268 $buttons = _captcha_search_buttons($form);
|
danielebarchiesi@2
|
269 if (count($buttons)) {
|
danielebarchiesi@2
|
270 // Pick first button.
|
danielebarchiesi@2
|
271 // TODO: make this more sofisticated? Use cases needed.
|
danielebarchiesi@2
|
272 $placement = $buttons[0];
|
danielebarchiesi@2
|
273 }
|
danielebarchiesi@2
|
274 else {
|
danielebarchiesi@2
|
275 // Use NULL when no buttons were found.
|
danielebarchiesi@2
|
276 $placement = NULL;
|
danielebarchiesi@2
|
277 }
|
danielebarchiesi@2
|
278 }
|
danielebarchiesi@2
|
279
|
danielebarchiesi@2
|
280 // Store calculated placement in cache.
|
danielebarchiesi@2
|
281 $placement_map[$form_id] = $placement;
|
danielebarchiesi@2
|
282 variable_set('captcha_placement_map_cache', $placement_map);
|
danielebarchiesi@2
|
283 }
|
danielebarchiesi@2
|
284
|
danielebarchiesi@2
|
285 return $placement;
|
danielebarchiesi@2
|
286 }
|
danielebarchiesi@2
|
287
|
danielebarchiesi@2
|
288 /**
|
danielebarchiesi@2
|
289 * Helper function for searching the buttons in a form.
|
danielebarchiesi@2
|
290 *
|
danielebarchiesi@2
|
291 * @param $form the form to search button elements in
|
danielebarchiesi@2
|
292 * @return an array of paths to the buttons.
|
danielebarchiesi@2
|
293 * A path is an array of keys leading to the button, the last
|
danielebarchiesi@2
|
294 * item in the path is the weight of the button element
|
danielebarchiesi@2
|
295 * (or NULL if undefined).
|
danielebarchiesi@2
|
296 */
|
danielebarchiesi@2
|
297 function _captcha_search_buttons($form) {
|
danielebarchiesi@2
|
298 $buttons = array();
|
danielebarchiesi@2
|
299 foreach (element_children($form) as $key) {
|
danielebarchiesi@2
|
300 // Look for submit or button type elements.
|
danielebarchiesi@2
|
301 if (isset($form[$key]['#type']) && ($form[$key]['#type'] == 'submit' || $form[$key]['#type'] == 'button')) {
|
danielebarchiesi@2
|
302 $weight = isset($form[$key]['#weight']) ? $form[$key]['#weight'] : NULL;
|
danielebarchiesi@2
|
303 $buttons[] = array(
|
danielebarchiesi@2
|
304 'path' => array(),
|
danielebarchiesi@2
|
305 'key' => $key,
|
danielebarchiesi@2
|
306 'weight' => $weight,
|
danielebarchiesi@2
|
307 );
|
danielebarchiesi@2
|
308 }
|
danielebarchiesi@2
|
309 // Process children recurively.
|
danielebarchiesi@2
|
310 $children_buttons = _captcha_search_buttons($form[$key]);
|
danielebarchiesi@2
|
311 foreach ($children_buttons as $b) {
|
danielebarchiesi@2
|
312 $b['path'] = array_merge(array($key), $b['path']);
|
danielebarchiesi@2
|
313 $buttons[] = $b;
|
danielebarchiesi@2
|
314 }
|
danielebarchiesi@2
|
315 }
|
danielebarchiesi@2
|
316 return $buttons;
|
danielebarchiesi@2
|
317 }
|
danielebarchiesi@2
|
318
|
danielebarchiesi@2
|
319 /**
|
danielebarchiesi@2
|
320 * Helper function to insert a CAPTCHA element in a form before a given form element.
|
danielebarchiesi@2
|
321 * @param $form the form to add the CAPTCHA element to.
|
danielebarchiesi@2
|
322 * @param $placement information where the CAPTCHA element should be inserted.
|
danielebarchiesi@2
|
323 * $placement should be an associative array with fields:
|
danielebarchiesi@2
|
324 * - 'path': path (array of path items) of the container in the form where the
|
danielebarchiesi@2
|
325 * CAPTCHA element should be inserted.
|
danielebarchiesi@2
|
326 * - 'key': the key of the element before which the CAPTCHA element
|
danielebarchiesi@2
|
327 * should be inserted. If the field 'key' is undefined or NULL, the CAPTCHA will
|
danielebarchiesi@2
|
328 * just be appended in the container.
|
danielebarchiesi@2
|
329 * - 'weight': if 'key' is not NULL: should be the weight of the element defined by 'key'.
|
danielebarchiesi@2
|
330 * If 'key' is NULL and weight is not NULL: set the weight property of the CAPTCHA element
|
danielebarchiesi@2
|
331 * to this value.
|
danielebarchiesi@2
|
332 * @param $captcha_element the CAPTCHA element to insert.
|
danielebarchiesi@2
|
333 */
|
danielebarchiesi@2
|
334 function _captcha_insert_captcha_element(&$form, $placement, $captcha_element) {
|
danielebarchiesi@2
|
335 // Get path, target and target weight or use defaults if not available.
|
danielebarchiesi@2
|
336 $target_key = isset($placement['key']) ? $placement['key'] : NULL;
|
danielebarchiesi@2
|
337 $target_weight = isset($placement['weight']) ? $placement['weight'] : NULL;
|
danielebarchiesi@2
|
338 $path = isset($placement['path']) ? $placement['path'] : array();
|
danielebarchiesi@2
|
339
|
danielebarchiesi@2
|
340 // Walk through the form along the path.
|
danielebarchiesi@2
|
341 $form_stepper = &$form;
|
danielebarchiesi@2
|
342 foreach ($path as $step) {
|
danielebarchiesi@2
|
343 if (isset($form_stepper[$step])) {
|
danielebarchiesi@2
|
344 $form_stepper = & $form_stepper[$step];
|
danielebarchiesi@2
|
345 }
|
danielebarchiesi@2
|
346 else {
|
danielebarchiesi@2
|
347 // Given path is invalid: stop stepping and
|
danielebarchiesi@2
|
348 // continue in best effort (append instead of insert).
|
danielebarchiesi@2
|
349 $target_key = NULL;
|
danielebarchiesi@2
|
350 break;
|
danielebarchiesi@2
|
351 }
|
danielebarchiesi@2
|
352 }
|
danielebarchiesi@2
|
353
|
danielebarchiesi@2
|
354 // If no target is available: just append the CAPTCHA element to the container.
|
danielebarchiesi@2
|
355 if ($target_key == NULL || !array_key_exists($target_key, $form_stepper)) {
|
danielebarchiesi@2
|
356 // Optionally, set weight of CAPTCHA element.
|
danielebarchiesi@2
|
357 if ($target_weight != NULL) {
|
danielebarchiesi@2
|
358 $captcha_element['#weight'] = $target_weight;
|
danielebarchiesi@2
|
359 }
|
danielebarchiesi@2
|
360 $form_stepper['captcha'] = $captcha_element;
|
danielebarchiesi@2
|
361 }
|
danielebarchiesi@2
|
362 // If there is a target available: make sure the CAPTCHA element comes right before it.
|
danielebarchiesi@2
|
363 else {
|
danielebarchiesi@2
|
364 // If target has a weight: set weight of CAPTCHA element a bit smaller
|
danielebarchiesi@2
|
365 // and just append the CAPTCHA: sorting will fix the ordering anyway.
|
danielebarchiesi@2
|
366 if ($target_weight != NULL) {
|
danielebarchiesi@2
|
367 $captcha_element['#weight'] = $target_weight - .1;
|
danielebarchiesi@2
|
368 $form_stepper['captcha'] = $captcha_element;
|
danielebarchiesi@2
|
369 }
|
danielebarchiesi@2
|
370 else {
|
danielebarchiesi@2
|
371 // If we can't play with weights: insert the CAPTCHA element at the right position.
|
danielebarchiesi@2
|
372 // Because PHP lacks a function for this (array_splice() comes close,
|
danielebarchiesi@2
|
373 // but it does not preserve the key of the inserted element), we do it by hand:
|
danielebarchiesi@2
|
374 // chop of the end, append the CAPTCHA element and put the end back.
|
danielebarchiesi@2
|
375 $offset = array_search($target_key, array_keys($form_stepper));
|
danielebarchiesi@2
|
376 $end = array_splice($form_stepper, $offset);
|
danielebarchiesi@2
|
377 $form_stepper['captcha'] = $captcha_element;
|
danielebarchiesi@2
|
378 foreach ($end as $k => $v) {
|
danielebarchiesi@2
|
379 $form_stepper[$k] = $v;
|
danielebarchiesi@2
|
380 }
|
danielebarchiesi@2
|
381 }
|
danielebarchiesi@2
|
382 }
|
danielebarchiesi@2
|
383 }
|
danielebarchiesi@2
|
384
|