-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebform_authcode.module
More file actions
81 lines (63 loc) · 2.57 KB
/
webform_authcode.module
File metadata and controls
81 lines (63 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* @file
* Webform authcode module functions.
*/
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function webform_authcode_form_webform_configure_form_alter(&$form, &$form_state, $form_id) {
form_load_include($form_state, 'inc', 'webform_authcode', 'includes/webform_authcode.admin');
// Retrieve the stored pass phrases from the variable store.
$authcode_phrases = variable_get('authtcode_pass_phrases', array());
// Retrieve the pass phrase for this webform.
$authcode_phrase = isset($authcode_phrases[$form['#node']->nid]) ? $authcode_phrases[$form['#node']->nid] : NULL;
// Add Authorization field to the webform.
$form['submission']['authcode_protect'] = array(
'#type' => 'checkbox',
'#title' => t('Protect this form'),
'#description' => t('Do you want to protect this form with an authorization code?'),
'#default_value' => isset($authcode_phrase),
);
// If authcode is selected, display the textfield.
$form['submission']['authcode_pass_phrase'] = array(
'#type' => 'textfield',
'#title' => t('Pass phrase'),
'#description' => t('Enter the pass phrase to get access to this webform.'),
'#default_value' => check_plain($authcode_phrase),
'#states' => array(
'visible' => array(
':input[name="authcode_protect"]' => array('checked' => TRUE),
),
),
);
// Add a validation handler.
$form['#validate'][] = 'webform_authcode_webform_configure_validate';
// Add a submit handler.
$form['#submit'][] = 'webform_authcode_webform_configure_submit';
}
/**
* Implements hook_node_view_alter().
*/
function webform_authcode_node_view_alter(&$build) {
// Include functions for frontend.
module_load_include('inc', 'webform_authcode', 'includes/webform_authcode.pages');
// Retrieve the stored pass phrases from the variable store.
$authcode_phrases = variable_get('authtcode_pass_phrases', array());
// Retrieve the node id.
$nid = $build['#node']->nid;
// Build the cookie name.
$cookie_name = 'Drupal_visitor_webform_authcode_' . $nid;
// Check if a pass phrase has been set for this webform.
if (isset($authcode_phrases[$build['#node']->nid])) {
// Check if a pass phrase for this webform has been stored in a cookie.
if (isset($_COOKIE[$cookie_name]) && $_COOKIE[$cookie_name] == $authcode_phrases[$nid]) {
return;
}
// Retrieve the pass phrase form.
$pass_phrase_form = drupal_get_form('webform_authcode_pass_phrase_form');
// Disable the body and webform.
$build['body']['#access'] = FALSE;
$build['webform']['#form'] = $pass_phrase_form;
}
}