-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotected_pages.module
228 lines (200 loc) · 6.95 KB
/
protected_pages.module
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* @file
* This module allows you to protect any page of your website by secure
* password. You can enter urls of pages to protect and set password per page.
* Admin (uid = 1) or user with bypass protection permission can view page.
*/
/**
* Implements hook_permission().
*/
function protected_pages_permission() {
return array(
'bypass pages password protection' => array(
'title' => t('bypass pages password protection'),
'description' => t('Bypass password protection of protected pages'),
),
'access protected page password screen' => array(
'title' => t('access protected page password screen'),
'description' => t('Access protected page password screen.'),
),
'administer protected pages configuration' => array(
'title' => t('access protected pages configuration screen'),
'description' => t('Access protected pages configuration screen.'),
),
);
}
/**
* Implements hook_menu().
*/
function protected_pages_menu() {
$items = array();
$items['admin/config/system/protected_pages'] = array(
'title' => 'Protected Pages',
'description' => 'Configure protected pages setting.',
'page callback' => 'drupal_get_form',
'page arguments' => array('protected_pages_configure'),
'access arguments' => array('administer protected pages configuration'),
'file' => 'protected_pages.admin.inc',
);
$items['admin/config/system/protected_pages/list'] = array(
'title' => 'Protected Pages',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/system/protected_pages/settings'] = array(
'title' => 'Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('protected_pages_settings'),
'access arguments' => array('administer protected pages configuration'),
'type' => MENU_LOCAL_TASK,
'file' => 'protected_pages.admin.inc',
);
$items['admin/config/system/protected_pages/%/edit'] = array(
'title' => 'Edit Protected Pages',
'page callback' => 'drupal_get_form',
'page arguments' => array('protected_pages_edit', 4),
'access arguments' => array('administer protected pages configuration'),
'file' => 'protected_pages.admin.inc',
);
$items['admin/config/system/protected_pages/%/delete'] = array(
'title' => 'Delete Protected Pages',
'page callback' => 'drupal_get_form',
'page arguments' => array('protected_pages_delete_confirm', 4),
'access arguments' => array('administer protected pages configuration'),
'type' => MENU_CALLBACK,
'file' => 'protected_pages.admin.inc',
);
$items['admin/config/system/protected_pages/%/send_email'] = array(
'title' => 'Send protected pages details to user by email',
'page callback' => 'drupal_get_form',
'page arguments' => array('protected_pages_send_email', 4),
'access arguments' => array('administer protected pages configuration'),
'file' => 'protected_pages.admin.inc',
);
$items['protected-page'] = array(
'title' => 'Protected page - Enter Password',
'title callback' => 'protected_pages_get_title',
'description' => 'Here you can enter the password for protected pages',
'page callback' => 'drupal_get_form',
'page arguments' => array('protected_pages_enter_password'),
'access callback' => 'protected_pages_access_callback',
'type' => MENU_CALLBACK,
'file' => 'protected_pages.inc',
);
return $items;
}
/**
* Callback function to determine who can enter a password.
*/
function protected_pages_access_callback() {
global $user;
if ($user->uid == 1) {
return TRUE;
}
if (!user_access('access protected page password screen')) {
return FALSE;
}
if (empty($_GET['protected_page']) || !is_numeric($_GET['protected_page'])) {
return FALSE;
}
return TRUE;
}
/**
* Implements hook_init().
*/
function protected_pages_init() {
if (user_access('bypass pages password protection')) {
return;
}
$current_path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
$normal_path = drupal_get_normal_path($current_path);
$pid = protected_pages_is_page_locked($current_path, $normal_path);
if ($pid) {
$query = drupal_get_destination();
if (!empty($_SERVER['HTTP_REFERER'])) {
$query['back'] = urlencode($current_path);
}
$query['protected_page'] = $pid;
drupal_goto('protected-page', array('query' => $query));
}
else {
$page_node = menu_get_object();
if (isset($page_node->nid) && is_numeric($page_node->nid)) {
$path_to_node = 'node/' . $page_node->nid;
$current_path = drupal_strtolower(drupal_get_path_alias($path_to_node));
$normal_path = drupal_get_normal_path($current_path);
$pid = protected_pages_is_page_locked($current_path, $normal_path);
if ($pid) {
$query = drupal_get_destination();
if (!empty($_SERVER['HTTP_REFERER'])) {
$query['back'] = urlencode($current_path);
}
$query['protected_page'] = $pid;
drupal_goto('protected-page', array('query' => $query));
}
}
}
}
/**
* Helper function to check whether the path is protected or not.
*/
function protected_pages_is_page_locked($current_path, $normal_path) {
$pid = db_select('protected_pages')
->fields('protected_pages', array('pid'))
->condition(db_or()->condition('path', $current_path)->condition('path', $normal_path))
->range(0, 1)
->execute()
->fetchField();
if (isset($_SESSION['_protected_page']['passwords'][$pid]['expire_time'])) {
if (time() >= $_SESSION['_protected_page']['passwords'][$pid]['expire_time']) {
unset($_SESSION['_protected_page']['passwords'][$pid]['request_time']);
unset($_SESSION['_protected_page']['passwords'][$pid]['expire_time']);
}
}
if (isset($_SESSION['_protected_page']['passwords'][$pid]['request_time'])) {
return FALSE;
}
return $pid;
}
/**
* Title Callback for enter password page.
*/
function protected_pages_get_title() {
return check_plain(variable_get('protected_pages_title', t('Protected Page -- Enter password')));
}
/**
* The default email subject.
*/
function protected_pages_email_subject() {
return t("Please visit this new page");
}
/**
* The default email body.
*/
function protected_pages_email_body() {
return t("Dear friend,
I just created a new page on my website and wanted to invite you to
visit. The page is protected by password. Please find the details below:-
Page Url = [protected-page-url]
Page Password = password here
Thank you.
[site-name]"
);
}
/**
* Implements hook_mail().
*/
function protected_pages_mail($key, &$message, $params) {
switch ($key) {
case 'protected_pages_details_mail':
$tokens = array('[protected-page-url]', '[site-name]');
$replcements = array(
$params['protected_page_url'], check_plain(variable_get('site_name', '')),
);
$body = str_replace($tokens, $replcements, $params['body']);
$subject = $params['subject'];
$message['subject'] = $subject;
$message['body'][] = $body;
break;
}
}