-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotected_pages.admin.inc
517 lines (462 loc) · 17.8 KB
/
protected_pages.admin.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<?php
/**
* @file
* Provides page callbacks for configuration page.
*/
/**
* Callback function for add protected page.
*/
function protected_pages_configure($form, &$form_state) {
$form['rules_list'] = array(
'#title' => t("Add Protected Page Relative path and password."),
'#type' => 'fieldset',
'#prefix' => '<div id="rules_list">',
'#suffix' => '</div>',
'#description' => t('Please enter the relative path and its corresponding
password. When user opens this url, they will asked to enter password to
view this page. For example, "node/5", "new-events" etc.'),
);
$form['rules_list']['path'] = array(
'#type' => 'textfield',
'#title' => t('Relative Path'),
'#description' => t('Enter relative drupal path. For example, "node/5", "new-events" etc.'),
'#required' => TRUE,
);
$form['rules_list']['password'] = array(
'#type' => 'password_confirm',
'#size' => 25,
'#required' => TRUE,
);
$form['rules_list']['submit'] = array(
'#type' => 'submit',
'#ajax' => array(
'callback' => 'protected_pages_configure_submit_callback',
'wrapper' => 'rules_list',
'effect' => 'fade',
'method' => 'replace',
'progress' => array('type' => 'throbber', 'message' => t('Please wait. The data is being saved.')),
),
'#value' => t('Save'),
);
$form['rules_list']['pages_table'] = array(
'#markup' => protected_pages_get_pages_list(),
);
return $form;
}
/**
* Callback to generate list of protected pages.
*/
function protected_pages_get_pages_list() {
$header = array(
array(
'data' => t('#'),
'width' => '10%',
),
array(
'data' => t('Relative Path'),
'width' => '60%',
),
array(
'data' => t('Operations'),
'colspan' => 3,
'width' => '30%',
),
);
$rows = array();
$query = db_select('protected_pages', 'pp')->extend('PagerDefault')->extend('TableSort');
$query->fields('pp')
->limit(20)
->orderByHeader($header);
$result = $query->execute();
$rows = array();
$count = 1;
foreach ($result as $data) {
$rows[] = array(
'data' =>
array(
$count,
$data->path,
l(t('Edit'), "admin/config/system/protected_pages/" . $data->pid . "/edit"),
l(t('Delete'), 'admin/config/system/protected_pages/' . $data->pid . "/delete"),
l(t('Send E-mail'), 'admin/config/system/protected_pages/' . $data->pid . "/send_email"),
),
);
$count++;
}
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
"attributes" => array('width' => "500"),
"sticky" => TRUE,
"caption" => "",
"colgroups" => array(),
"empty" => t("No record found!"),
)
);
$output .= theme('pager', array(
'tags' => array(),
)
);
return $output;
}
/**
* Implements hook_validate().
*/
function protected_pages_configure_validate($form, &$form_state) {
$form_state['normal_path'] = drupal_get_normal_path($form_state['values']['path']);
$path_alias = drupal_strtolower(drupal_get_path_alias($form_state['values']['path']));
if (!drupal_valid_path($form_state['normal_path'])) {
form_set_error('path', t('Please enter a correct path!'));
}
$pid = db_select('protected_pages')
->fields('protected_pages', array('pid'))
->condition(db_or()->condition('path', $form_state['normal_path'])->condition('path', $path_alias))
->range(0, 1)
->execute()
->fetchField();
if ($pid) {
form_set_error('path', t('Duplicate path entry is not allowed. There is already a path or its alias exists.'));
}
}
/**
* Ajax submit callback for add protected page form.
*/
function protected_pages_configure_submit_callback($form, &$form_state) {
$errors = form_get_errors();
if (count($errors) < 1) {
db_insert('protected_pages')->fields(array(
'password' => sha1(trim(check_plain($form_state['values']['password']))),
'path' => check_plain($form_state['values']['path']),
))->execute();
drupal_set_message('The protected page settings has been successfully saved.');
$form['rules_list']['pages_table']['#markup'] = protected_pages_get_pages_list();
$form['rules_list']['path']['#value'] = '';
}
return $form['rules_list'];
}
/**
* Callback function for edit protected page form.
*/
function protected_pages_edit($form, &$form_state, $pid) {
$protected_page = db_select('protected_pages')
->fields('protected_pages', array('path'))
->condition('pid', $pid)
->execute()
->fetchObject();
if (!isset($protected_page->path)) {
drupal_access_denied();
exit;
}
$form['rules_list'] = array(
'#title' => t("Edit Protected Page Relative path and password."),
'#type' => 'fieldset',
'#prefix' => '<div id="rules_list">',
'#suffix' => '</div>',
'#description' => t('Please enter the relative path and its corresponding
password. When user opens this url, they will assked to enter password to
view this page. For example, "node/5", "new-events" etc.'),
);
$form['rules_list']['path'] = array(
'#type' => 'textfield',
'#title' => t('Relative Path'),
'#description' => t('Enter relative drupal path. For example, "node/5", "new-events" etc.'),
'#required' => TRUE,
'#default_value' => $protected_page->path,
);
$form['rules_list']['password'] = array(
'#type' => 'password_confirm',
'#size' => 25,
);
$form['rules_list']['pid'] = array(
'#type' => 'hidden',
'#value' => $pid,
);
$form['rules_list']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
/**
* Implements hook_validate().
*/
function protected_pages_edit_validate($form, &$form_state) {
$form_state['normal_path'] = drupal_get_normal_path($form_state['values']['path']);
if (!drupal_valid_path($form_state['normal_path'])) {
form_set_error('path', t('Please enter a correct path!'));
}
$pid = db_select('protected_pages')
->fields('protected_pages', array('pid'))
->condition(db_or()->condition('path', $form_state['normal_path'])->condition('path', $form_state['values']['path']))
->condition('pid', $form_state['values']['pid'], '<>')
->range(0, 1)
->execute()
->fetchField();
if ($pid) {
form_set_error('path', t('Duplicate path entry is not allowed. There is already a path or its alias exists.'));
}
}
/**
* Implements hook_submit().
*/
function protected_pages_edit_submit($form, &$form_state) {
$values['path'] = check_plain($form_state['values']['path']);
if (!empty($form_state['values']['password'])) {
$values['password'] = sha1(trim(check_plain($form_state['values']['password'])));
}
db_update('protected_pages')
->fields($values)
->condition('pid', $form_state['values']['pid'])
->execute();
drupal_set_message('The protected page settings has been successfully saved.');
$form_state['redirect'] = 'admin/config/system/protected_pages';
}
/**
* Callback function for delete protected page.
*/
function protected_pages_delete_confirm($form, &$form_state, $pid) {
$path = db_select('protected_pages')
->fields('protected_pages', array('path'))
->condition('pid', $pid)
->range(0, 1)
->execute()
->fetchField();
$form['pid'] = array('#type' => 'hidden', '#value' => $pid);
return confirm_form($form, t('Are you sure you want to delete <b>"%path"</b> from protected pages list?', array('%path' => $path)), 'admin/config/system/protected_pages', t('This action cannot be undone.'), t('Delete'), t('Cancel')
);
}
/**
* Implements hook_submit().
*/
function protected_pages_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$pid = $form_state['values']['pid'];
db_delete('protected_pages')
->condition('pid', $pid)
->execute();
drupal_set_message(t('The path has been successfully deleted from protected pages.'));
$form_state['redirect'] = 'admin/config/system/protected_pages';
}
}
/**
* Callback function for send protected pages details email form.
*/
function protected_pages_send_email($form, &$form_state, $pid) {
$destination = drupal_get_destination();
$form['send_email_box'] = array(
'#type' => 'fieldset',
'#title' => t('Send email'),
'#description' => t('You send details of this protected page by email to multiple users. Please click !here to configure email settings.', array(
'!here' => l(t('here'), 'admin/config/system/protected_pages/settings', array(
'query' => $destination,
)
),
)
),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form_state['pid'] = $pid;
$form['send_email_box']['recipents'] = array(
'#type' => 'textarea',
'#title' => t('Recipents'),
'#rows' => 5,
'#description' => t('Enter enter comma separated list of recipents.'),
'#required' => TRUE,
);
$form['send_email_box']['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#default_value' => variable_get('protected_pages_email_subject', protected_pages_email_subject()),
'#description' => t('Enter subject of email.'),
'#required' => TRUE,
);
$form['send_email_box']['body'] = array(
'#type' => 'textarea',
'#title' => t('Email Body'),
'#rows' => 15,
'#default_value' => variable_get('protected_pages_email_body', protected_pages_email_body()),
'#description' => t('Enter the body of the email. Only [protected-page-url] and [site-name] tokens are available.
Since password is encrypted, therefore we can not provide it by token.'),
'#required' => TRUE,
);
$form['send_email_box']['submit'] = array(
'#type' => 'submit',
'#value' => t('Send Email'),
);
return $form;
}
/**
* Implements hook_validate().
*/
function protected_pages_send_email_validate($form, &$form_state) {
$emails = explode(',', str_replace(array("\r", "\n"), ',', $form_state['values']['recipents']));
foreach ($emails as $key => $email) {
$email = trim($email);
if ($email) {
if (!valid_email_address($email)) {
form_error($form['send_email_box']['recipents'], t('Invalid email address: @mail. Please correct this email.', array('@mail' => $email)));
unset($emails[$key]);
}
else {
$emails[$key] = $email;
}
}
else {
unset($emails[$key]);
}
}
$form_state['validated_recipents'] = implode(', ', $emails);
}
/**
* Implements hook_submit().
*/
function protected_pages_send_email_submit($form, &$form_state) {
$path = db_select('protected_pages')
->fields('protected_pages', array('path'))
->condition('pid', $form_state['pid'])
->range(0, 1)
->execute()
->fetchField();
$module = 'protected_pages';
$key = 'protected_pages_details_mail';
$to = $form_state['values']['recipents'];
$from = variable_get('site_mail');
$language = language_default();
$send = TRUE;
$params = array();
$params['subject'] = $form_state['values']['subject'];
$params['body'] = $form_state['values']['body'];
$params['protected_page_url'] = url($path, array('absolute' => TRUE));
drupal_mail($module, $key, $to, $language, $params, $from, $send);
drupal_set_message(t('The email has been successfully sent.'));
$form_state['redirect'] = 'admin/config/system/protected_pages';
}
/**
* Callback function for protected pages settings.
*/
function protected_pages_settings() {
$form['protected_pages_password_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Protected Pages Password Settings'),
'#description' => t('Configure password related settings.'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$global_password_help_text = array();
$global_password_help_text[] = t('Allow per page password = Only per page password will be accepted. Global password will not be accepted.');
$global_password_help_text[] = t('Allow per page password or Global password = Per page password and global password both will be accepted.');
$global_password_help_text[] = t('Allow Only Global = Only global password will be accepted for each protected page.');
$form['protected_pages_password_fieldset']['protected_pages_user_global_password'] = array(
'#type' => 'select',
'#title' => t('Global Password Setting'),
'#default_value' => variable_get('protected_pages_user_global_password', 'per_page_or_global'),
'#options' => array(
'per_page_password' => t('Allow per page password'),
'per_page_or_global' => t('Allow per page password or Global password'),
'only_global' => t('Allow Only Global'),
),
'#description' => t('Please select the appropriate option for protected pages handling.') .
'<br />' . theme('item_list', array('items' => $global_password_help_text)),
);
$form['protected_pages_password_fieldset']['protected_pages_global_password_field'] = array(
'#type' => 'password_confirm',
'#title' => t('Global Password'),
'#description' => t('The default password for all protected pages. This
password is necessary if you select the previous checkbox "Allow per page
password or Global password" or "Allow Only Global" options above.'),
);
$form['protected_pages_password_fieldset']['protected_pages_session_expire_time'] = array(
'#type' => 'textfield',
'#title' => t('Session Expire Time'),
'#description' => t('When user enters password a session is created.
The node will be accessible until session expire. Once session expires,
user will need to enter password again. The default session expire time
is 0 (unlimited).'),
'#default_value' => variable_get('protected_pages_session_expire_time', 0),
'#required' => TRUE,
'#size' => 10,
'#element_validate' => array('protected_pages_validate_integer_positive'),
'#field_suffix' => t('in minutes'),
);
$form['protected_pages_email_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Protected pages email settings'),
'#description' => t('The following settings allows admin to send emails to multiple users about protected pages details to access protected pages.'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['protected_pages_email_fieldset']['protected_pages_email_subject'] = array(
'#type' => 'textfield',
'#title' => t('Email subject'),
'#default_value' => variable_get('protected_pages_email_subject', protected_pages_email_subject()),
'#description' => t('Enter the subject of the email.'),
);
$form['protected_pages_email_fieldset']['protected_pages_email_body'] = array(
'#type' => 'textarea',
'#title' => t('Email content'),
'#rows' => 15,
'#default_value' => variable_get('protected_pages_email_body', protected_pages_email_body()),
'#description' => t('Enter the body of the email. Only [protected-page-url] and [site-name] tokens are available.
Since password is encrypted, therefore we can not provide it by token.'),
);
$form['protected_pages_other_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Protected Pages Other Settings'),
'#description' => t('Configure other settings.'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['protected_pages_other_fieldset']['protected_pages_title'] = array(
'#type' => 'textfield',
'#title' => t('Password page title'),
'#default_value' => variable_get('protected_pages_title', t('Protected Page -- Enter password')),
'#description' => t('Enter the title of the protected page.'),
);
$form['protected_pages_other_fieldset']['protected_pages_description'] = array(
'#type' => 'textarea',
'#title' => t('Password page description (inside the field set)'),
'#default_value' => variable_get('protected_pages_description', t('The page you are trying to view is password protected. Please enter the password below to proceed.')),
'#description' => t('Enter specific description for the protected page. This description is displayed inside the fieldset. HTML is accepted.'),
);
$form['protected_pages_other_fieldset']['protected_pages_password_label'] = array(
'#type' => 'textfield',
'#title' => t('Password field label'),
'#default_value' => variable_get('protected_pages_password_label', t('Enter Password')),
'#description' => t('Enter the text for the password field label.'),
);
$form['protected_pages_other_fieldset']['protected_pages_submit_button_text'] = array(
'#type' => 'textfield',
'#title' => t('Submit Button Text'),
'#default_value' => variable_get('protected_pages_submit_button_text', t('Authenticate')),
'#description' => t('Enter the text for the submit button of enter password form.'),
);
$form['protected_pages_other_fieldset']['protected_pages_incorrect_password_msg'] = array(
'#type' => 'textarea',
'#title' => t('Incorrect Password Error Text'),
'#default_value' => variable_get('protected_pages_incorrect_password_msg', t('Incorrect password!')),
'#description' => t('This error text will appear if someone enters wrong password in "Enter password screen".'),
);
$form['#submit'][] = '_protected_pages_settings_submit';
return system_settings_form($form);
}
/**
* Callback function to validate session expire time value.
*/
function protected_pages_validate_integer_positive($element, &$form_state) {
$value = $element['#value'];
if ($value !== '' && (!is_numeric($value) || intval($value) != $value || $value < 0)) {
form_error($element, t('%name must be a positive integer.', array('%name' => $element['#title'])));
}
}
/**
* Custom submit function encrypt password and deleting non-useful variable.
*/
function _protected_pages_settings_submit($form, &$form_state) {
$passwd = $form_state['values']['protected_pages_global_password_field'];
if ($passwd) {
variable_set('protected_pages_global_password', sha1($passwd));
unset($form_state['values']['protected_pages_global_password_field']);
variable_del('protected_pages_global_password_field');
}
}