-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotected_pages.inc
104 lines (94 loc) · 3.67 KB
/
protected_pages.inc
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* @file
* Provides page callbacks for enter password page.
*/
/**
* Callback function for enter password page.
*/
function protected_pages_enter_password($form, &$form_state) {
if (!isset($_GET['destination']) || empty($_GET['protected_page']) || !is_numeric($_GET['protected_page'])) {
watchdog('protected_page', 'Illegal call to /protected-page', array(), WATCHDOG_WARNING);
drupal_access_denied();
exit();
}
$pid = db_select('protected_pages')
->fields('protected_pages', array('pid'))
->condition('pid', $_GET['protected_page'])
->range(0, 1)
->execute()
->fetchField();
if (!$pid) {
watchdog('protected_page', 'Invalid pid (@pid) used with /protected-page', array('@pid' => $_GET['protected_page']), WATCHDOG_WARNING);
drupal_access_denied();
exit();
}
$form['protected_page_enter_password'] = array(
'#type' => 'fieldset',
'#description' => variable_get('protected_pages_description', t('The page you are trying to view is password protected. Please enter the password below to proceed.')),
'#collapsible' => FALSE,
);
$form['protected_page_enter_password']['password'] = array(
'#type' => 'password',
'#title' => variable_get('protected_pages_password_label', t('Enter Password')),
'#size' => 20,
'#required' => TRUE,
);
$form['protected_page_pid'] = array(
'#type' => 'hidden',
'#value' => $_GET['protected_page'],
);
$form['protected_page_enter_password']['submit'] = array(
'#type' => 'submit',
'#value' => variable_get('protected_pages_submit_button_text', t('Authenticate')),
);
return $form;
}
/**
* Implements hook_validate().
*/
function protected_pages_enter_password_validate($form, &$form_state) {
$password = sha1(trim(check_plain($form_state['values']['password'])));
$global_password_setting = variable_get('protected_pages_user_global_password', 'per_page_or_global');
if ($global_password_setting == 'per_page_password') {
$pid = db_select('protected_pages')
->fields('protected_pages', array('pid'))
->condition('password', $password)
->condition('pid', $form_state['values']['protected_page_pid'])
->range(0, 1)
->execute()
->fetchField();
if (!$pid) {
form_set_error('password', variable_get('protected_pages_incorrect_password_msg', t('Incorrect password!')));
}
}
elseif ($global_password_setting == 'per_page_or_global') {
$pid = db_select('protected_pages')
->fields('protected_pages', array('pid'))
->condition('password', $password)
->condition('pid', $form_state['values']['protected_page_pid'])
->range(0, 1)
->execute()
->fetchField();
$global_password = variable_get('protected_pages_global_password', '');
if (!$pid && $global_password != $password) {
form_set_error('password', variable_get('protected_pages_incorrect_password_msg', t('Incorrect password!')));
}
}
else {
$global_password = variable_get('protected_pages_global_password', '');
if ($global_password != $password) {
form_set_error('password', variable_get('protected_pages_incorrect_password_msg', t('Incorrect password!')));
}
}
}
/**
* Implements hook_submit().
*/
function protected_pages_enter_password_submit($form, &$form_state) {
$_SESSION['_protected_page']['passwords'][$form_state['values']['protected_page_pid']]['request_time'] = REQUEST_TIME;
$session_expire_time = variable_get('protected_pages_session_expire_time', 0);
if ($session_expire_time) {
$_SESSION['_protected_page']['passwords'][$form_state['values']['protected_page_pid']]['expire_time'] = strtotime("+{$session_expire_time} minutes");
}
}