|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * Webform authcode module functions for frontend. |
| 6 | + */ |
| 7 | + |
| 8 | + /** |
| 9 | + * Callback function to build pass phrase form. |
| 10 | + */ |
| 11 | +function webform_authcode_pass_phrase_form($form, &$form_state) { |
| 12 | + |
| 13 | + $form['description'] = array( |
| 14 | + '#prefix' => '<div class="introduction-text">', |
| 15 | + '#suffix' => '</div>', |
| 16 | + '#markup' => t('This form is protected by a pass phrase. Please enter the the pass phrase provided to you to gain access to the form.'), |
| 17 | + ); |
| 18 | + |
| 19 | + $form['authcode_pass_phrase'] = array( |
| 20 | + '#type' => 'textfield', |
| 21 | + '#title' => t('Pass phrase'), |
| 22 | + '#maxlength' => 255, |
| 23 | + '#size' => 50, |
| 24 | + '#description' => t('Please enter the pass phrase provided to you.'), |
| 25 | + ); |
| 26 | + |
| 27 | + $form['submit'] = array( |
| 28 | + '#type' => 'submit', |
| 29 | + '#value' => t('Continue'), |
| 30 | + ); |
| 31 | + |
| 32 | + // Add validation handler. |
| 33 | + $form['#validate'][] = 'webform_authcode_pass_phrase_form_validate'; |
| 34 | + // Add submit handler. |
| 35 | + $form['#submit'][] = 'webform_authcode_pass_phrase_form_submit'; |
| 36 | + |
| 37 | + return $form; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Validation handler for webform_authcode_pass_phrase_form. |
| 42 | + */ |
| 43 | +function webform_authcode_pass_phrase_form_validate($form, $form_state) { |
| 44 | + // Retrieve values from form_state. |
| 45 | + $values = $form_state['values']; |
| 46 | + |
| 47 | + // Retrieve the node id. |
| 48 | + $node = menu_get_object(); |
| 49 | + $nid = $node->nid; |
| 50 | + |
| 51 | + // Retrieve the stored pass phrases from the variable store. |
| 52 | + $authcode_phrases = variable_get('authtcode_pass_phrases', array()); |
| 53 | + |
| 54 | + // Check for emtpty pass phrase. |
| 55 | + if (empty($values['authcode_pass_phrase'])) { |
| 56 | + form_set_error('authcode_pass_phrase', t("The field pass phrase can't be empty.")); |
| 57 | + } |
| 58 | + |
| 59 | + // Check if the pass phrase is correct. |
| 60 | + if (!empty($values['authcode_pass_phrase']) && $values['authcode_pass_phrase'] !== $authcode_phrases[$nid]) { |
| 61 | + form_set_error('authcode_pass_phrase', t('The pass phrase you entered is incorrect. Please note that the pass phrase is case sensitive.')); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Submit handler for webform_authcode_pass_phrase_form. |
| 67 | + */ |
| 68 | +function webform_authcode_pass_phrase_form_submit($form, $form_state) { |
| 69 | + // Retrieve values from form_state. |
| 70 | + $values = $form_state['values']; |
| 71 | + |
| 72 | + // Retrieve the node id. |
| 73 | + $node = menu_get_object(); |
| 74 | + $nid = $node->nid; |
| 75 | + |
| 76 | + // Store the authcode in a cookie. |
| 77 | + $cookie_name = 'webform_authcode_' . $nid; |
| 78 | + user_cookie_save(array($cookie_name => check_plain($values['authcode_pass_phrase']))); |
| 79 | +} |
0 commit comments