-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueueit.module
157 lines (143 loc) · 4.8 KB
/
queueit.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
<?php
/**
* @file
* Queue-it main module file.
*/
define('QUEUEIT_EXCLUDE', "admin\nadmin/*\nsites/*\nsystem/ajax/*\nuser/*");
/**
* Implements hook_help().
*/
function queueit_help($path, $arg) {
// Load help content from the README file.
$content = file_get_contents(drupal_get_path('module', 'queueit') . '/README.md');
$content .= file_get_contents(libraries_get_path('composer') . '/queueit/knownuserv3' . '/README.md');
module_load_include('inc', 'queueit');
switch ($path) {
// Main module help for the queueit module.
case 'admin/help#queueit':
return '<p>' . queueit_md2html($content) . '</p>';
}
}
/**
* Implements hook_init().
*/
function queueit_init() {
// Check if user role is excluded from the Queue-it processing.
// If debug mode is enabled, ignore bypassing for debugging purposes.
if (user_access('bypass queueit') && !variable_get('queueit_debug', FALSE)) {
return;
}
// The KnownUser validation must _only_ be done on page requests.
// So, if you add the KnownUser validation logic to a central place,
// then be sure that the Triggers only fire on page requests
// and not on e.g. image or ajax requests.
$curr_path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
$exclude_paths = variable_get('queueit_exclude_pages', QUEUEIT_EXCLUDE);
foreach (explode("\n", $exclude_paths) as $path) {
// Check for matching page path to exclude.
$page_match = drupal_match_path($curr_path, drupal_strtolower($path));
if ($curr_path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], drupal_strtolower($path));
}
if ($page_match) {
return;
}
}
$known_user = new QueueitKnownUser();
$customer_id = $known_user->getCustomerId();
// Ignore processing if the config is invalid.
if (!$known_user->validateConfig()) {
return;
}
// Ignore processing for when in CLI mode.
if (variable_get('queueit_ignore_cli', TRUE) && drupal_is_cli()) {
return;
}
// Ignore processing for pages sent via POST.
if (variable_get('queueit_ignore_post', TRUE) && !empty($_POST)) {
return;
}
try {
$method = variable_get('queueit_mode', 'code');
switch ($method) {
case 'code':
$result = $known_user->resolveRequestByLocalEventConfig();
break;
case 'integration':
$result = $known_user->validateRequestByIntegrationConfig();
break;
case 'js':
drupal_add_js(QueueitBase::QI_JS_CLIENT_URL, 'external');
/* @fixme: https://drupal.stackexchange.com/q/253634
drupal_add_js(QueueitBase::QI_JS_LOADER_URL,
[
'type' => 'external',
'data-queueit-c' => $customer_id,
]);
*/
$js_code = "<script data-queueit-c='$customer_id' type='text/javascript' src='//static.queue-it.net/script/queueconfigloader.min.js'></script>";
drupal_add_js($js_code, 'inline');
break;
default:
watchdog('queueit', 'Unknown method type: %type', ['%type' => $method], WATCHDOG_WARNING);
break;
}
if (!empty($result)
&& variable_get('queueit_debug', FALSE)
&& user_access('administer queueit')
) {
// Displays response when in debug mode for admin users.
drupal_set_message(var_export($result, TRUE), 'status');
}
if (!user_access('bypass queueit') && $result->doRedirect()) {
// Send the user to the queue.
// Either because hash was missing or because it was invalid.
drupal_goto($result->redirectUrl, ['absolute' => TRUE]);
}
}
catch (\Exception $e) {
watchdog_exception('queueit', $e);
}
}
/**
* Implements hook_menu().
*/
function queueit_menu() {
$items = array();
$items['admin/config/services/queueit'] = array(
'title' => 'Queue-it settings',
'description' => 'Configure settings for Queue-it.',
'page callback' => 'drupal_get_form',
'page arguments' => array('queueit_settings_form'),
'access arguments' => array('administer queueit'),
'file' => 'queueit.admin.inc',
);
return $items;
}
/**
* Implements hook_library().
*/
function queueit_library() {
// Register libraries available in the external directory.
$libraries['queueit/KnownUser'] = array(
'title' => 'KnownUser.V3.PHP',
'website' => 'https://github.com/queueit/KnownUser.V3.PHP',
'version' => '3',
);
return $libraries;
}
/**
* Implements hook_permission().
*/
function queueit_permission() {
$permissions = array();
$permissions['bypass queueit'] = array(
'title' => t('Bypass Queue-it'),
'description' => t('Exclude the queue system for the selected roles.'),
);
$permissions['administer queueit'] = array(
'title' => t('Administer Queue-it'),
'description' => t('Configure Queue-it settings and credentials. Warning: Give to trusted roles only.'),
);
return $permissions;
}