forked from grezniczek/redcap_javascript_injector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSInjectorExternalModule.php
More file actions
455 lines (422 loc) · 17.7 KB
/
JSInjectorExternalModule.php
File metadata and controls
455 lines (422 loc) · 17.7 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
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
<?php namespace DE\RUB\JSInjectorExternalModule;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
/**
* ExternalModule class for Javascript Injector.
*/
class JSInjectorExternalModule extends AbstractExternalModule {
/**
* EM Framework (tooling support)
* @var \ExternalModules\Framework
*/
private $fw;
function __construct() {
parent::__construct();
$this->fw = $this->framework;
}
#region Hooks
// Workaround for the redcap_module_system_change_version hook not working
function redcap_module_link_check_display($project_id, $link) {
if ($this->getSystemSetting("v1-upgrade") != "done") {
$this->convert_v1_settings();
}
return null;
}
// Perform settings upgrade to v2 model
function redcap_module_system_change_version($version, $old_version) {
$major = explode(".", trim($old_version, "v"))[0] * 1;
if ($major == 1) {
$this->convert_v1_settings();
}
}
// Insert JSMO name into config dialog
function redcap_module_configuration_settings($project_id, $settings) {
$key = $project_id == null ? "sys-jsmo-info" : "proj-jsmo-info";
$jsmo = $this->getJavascriptModuleObjectName();
foreach ($settings as &$setting) {
foreach ($setting["sub_settings"] as &$sub_setting) {
if ($sub_setting["key"] === $key) {
$sub_setting["name"] = str_replace("#JSMO#", $jsmo, $sub_setting["name"]);
}
}
}
return $settings;
}
// Set visibility of Configure button in projects
function redcap_module_configure_button_display() {
if ($this->getSystemSetting("su_only") && !SUPER_USER) return null;
return true;
}
// Determine context and inject
function redcap_every_page_top ($project_id) {
$page = defined("PAGE") ? PAGE : "";
$instrument = null;
$context = [
"cc" => false,
"todo" => false,
"langs" => false,
"browseprojects" => false,
"browseusers" => false,
"edituser" => false,
"emailusers" => false,
"login" => false,
"php" => false,
"rsd" => false,
"aer" => false,
"rhp" => false,
"data_entry" => false,
"survey" => false,
"report" => false,
"db" => false,
"dbp" => false,
];
// System context
if ($project_id == null) {
if (!defined("USERID")) {
$context["login"] = true;
}
else {
if ($page === "ToDoList/index.php") {
$context["todo"] = true;
$context["cc"] = true;
}
else if ($page === "LanguageUpdater/index.php") {
$context["langs"] = true;
$context["cc"] = true;
}
else if ($page === "ControlCenter/view_projects.php") {
$context["browseprojects"] = true;
$context["cc"] = true;
}
else if ($page === "ControlCenter/view_users.php") {
$context["browseusers"] = true;
$context["cc"] = true;
}
else if ($page === "ControlCenter/create_user.php") {
$context["edituser"] = true;
$context["cc"] = true;
}
else if ($page === "ControlCenter/email_users.php") {
$context["emailusers"] = true;
$context["cc"] = true;
}
else if (starts_with($page, "ControlCenter/")) {
$context["cc"] = true;
}
else if ($page === "FhirStatsController:index") {
$context["cc"] = true;
}
else if ($page === "MultiLanguageController:systemConfig") {
$context["cc"] = true;
}
else if (self::IsSystemExternalModulesManager($page)) {
$context["cc"] = true;
}
}
}
// Project context
else {
$Proj = new \Project();
if ($page === "surveys/index.php") {
if (isset($_GET["page"])) {
$context["survey"] = true;
$instrument = isset($Proj->forms[$_GET["page"]]) ? $_GET["page"] : null;
}
else if (isset($_GET["__dashboard"])) {
$context["dbp"] = true;
}
}
else if (!defined("USERID")) {
$context["login"] = true;
}
else {
if ($page === "DataEntry/index.php") {
$context["data_entry"] = true;
$instrument = isset($Proj->forms[$_GET["page"]]) ? $_GET["page"] : null;
}
else if ($page == "index.php") {
$context["php"] = true;
}
else if ($page == "DataEntry/record_status_dashboard.php") {
$context["rsd"] = true;
}
else if ($page == "DataEntry/record_home.php") {
if (isset($_GET["id"])) {
$context["rhp"] = true;
}
else {
$context["aer"] = true;
}
}
else if ($page == "ProjectDashController:view") {
$context["db"] = true;
}
else if ($page == "DataExport/index.php" && isset($_GET["report_id"]) && !isset($_GET["addedit"])) {
$context["report"] = true;
}
}
}
// Inject
$this->inject_js($project_id, $context, $instrument);
}
#endregion
/**
* Inject JS code based on context.
*
* @param string|null $project_id
* @param array $context
* @param string $instrument
*/
function inject_js($project_id, $context, $instrument) {
// Get "work schedule" ;)
$snippets = $this->parse_settings($project_id, array_keys($context));
// Determine if this is a "named" context
$named_context = array_reduce($context, function($carry, $item) {
return $carry || $item;
}, false);
// Then, get the context name
$context_names = array_keys(array_filter($context, function($v) {
return $v;
}));
$inject_jsmo = false;
$debug_jsmo = false;
// Verify snippets
$snippets_to_inject = [];
foreach ($snippets as $snippet) {
$inject = false;
$reject = false;
// Project context? Check project list and enabled state, but not for "login"
if ($project_id != null && !in_array("login", $context_names, true)) {
if (!$snippet["proj-enabled"]) {
$reject = true;
}
else if ($snippet["proj-limit"] == "include") {
if (!in_array($project_id, $snippet["proj-list"], true)) {
$reject = true;
}
}
else if ($snippet["proj-limit"] == "exclude") {
if (in_array($project_id, $snippet["proj-list"], true)) {
$reject = true;
}
}
}
else {
if (!$snippet["sys-enabled"]) {
$reject = true;
}
}
// Rejected? Continue to next item
if ($reject) continue;
// Page type / context
if ($named_context) {
// Check if there is a named match
foreach ($context_names as $this_ctx) {
// Additionally evaluate form for data entry and survey pages
$instrument_check = true;
if ($snippet["type"] == "proj" && in_array($this_ctx, ["data_entry","survey"], true)) {
if (count($snippet["form-list"])) {
$instrument_check = in_array($instrument, $snippet["form-list"], true);
}
}
$inject = $inject || ($snippet["ctx"][$this_ctx] && $instrument_check);
}
}
else {
// Check if the snippet should always be injected
$inject = $snippet["ctx"][$project_id == null ? "sysall" : "projall"];
}
if ($inject) {
$snippets_to_inject[] = $snippet;
$inject_jsmo = $inject_jsmo || $snippet["jsmo"];
$debug_jsmo = $debug_jsmo || ($snippet["jsmo"] && $snippet["debug"]);
}
}
// JSMO
if ($inject_jsmo) {
$this->initializeJavascriptModuleObject();
if ($debug_jsmo) {
$jsmo_name = $this->fw->getJavascriptModuleObjectName();
print "<script>console.log('JS Injector: Injected JavascriptModuleObject \"{$jsmo_name}\"', {$jsmo_name});</script>\n";
}
}
// Inject snippets (after JSMO)
foreach ($snippets_to_inject as $snippet) {
// Add debug info
$info = $snippet["debug"] ? " data-from=\"REDCap JavaScript Injector {$this->VERSION}\"" : "";
$context = $snippet["debug"] ? " data-context=\"{$snippet["type"]}\"" : "";
$name = $snippet["debug"] ? (" data-name=\"" . js_escape($snippet["name"]) . "\"") : "";
// Actual injection
print "<script{$info}{$context}{$name}>\n\t{$snippet["code"]}\n</script>\n";
// More debug info, after the fact
if ($snippet["debug"]) {
print "<script>console.log('JS Injector: Injected snippet \"' + ".json_encode($snippet["name"]) . " + '\"');</script>\n";
}
}
}
#region Settings Parser
/**
* Parses project and system settings into a useable format.
* @param string|null $project_id
* @return array
*/
function parse_settings($project_id = null, $contexts) {
// Make a list of all snippets
$snippets = [];
// Load settings
$ss = $this->getSystemSettings();
$ps = $project_id == null ? [ "proj-snippet" => []] : $this->getProjectSettings($project_id);
// Parse system settings
foreach ($ss["sys-snippet"]["system_value"] as $i => $_) {
$snippet = [
"type" => "sys"
];
$snippet["name"] = $ss["sys-name"]["system_value"][$i];
if (empty($snippet["name"])) {
$snippet["name"] = "<unnamed>";
}
$snippet["jsmo"] = $ss["sys-jsmo"]["system_value"][$i] == true;
$snippet["debug"] = $ss["sys-debug"]["system_value"][$i] == true;
$snippet["code"] = $ss["sys-code"]["system_value"][$i] ?? "";
$snippet["sys-enabled"] = $ss["sys-enabled"]["system_value"][$i] == true;
$snippet["proj-enabled"] = $ss["sys-proj-enabled"]["system_value"][$i] == true;
$snippet["proj-limit"] = "all";
$snippet["proj-list"] = [];
$snippet["form-list"] = [];
if ($snippet["proj-enabled"]) {
$limit = $ss["sys-proj-limit"]["system_value"][$i];
$snippet["proj-limit"] = in_array($limit, ["include","exclude"], true) ? $limit : "all";
$snippet["proj-list"] = array_unique(explode(",", trim($ss["sys-proj-list"]["system_value"][$i] ?? "")));
}
$snippet["ctx"]["projall"] = $ss["sys-proj-context_all"]["system_value"][$i] == true;
$snippet["ctx"]["sysall"] = $ss["sys-context_all"]["system_value"][$i] == true;
foreach ($contexts as $this_context) {
$snippet["ctx"][$this_context] = false;
}
foreach ($ss as $this_key => $_) {
$this_context = array_pop(explode("_", $this_key, 2));
if (in_array($this_context, $contexts, true)) {
$val = $ss[$this_key]["system_value"][$i];
if ($val == "include") {
$snippet["ctx"][$this_context] = true;
}
else if ($val <> "exclude") {
$snippet["ctx"][$this_context] = contains($this_key, "proj-context") ? $snippet["ctx"]["projall"] : $snippet["ctx"]["sysall"];
}
}
}
$snippets[] = $snippet;
}
// Parse project settings
foreach ($ps["proj-snippet"] as $i => $_) {
$snippet = [
"type" => "proj"
];
$snippet["name"] = $ps["proj-name"][$i];
if (empty($snippet["name"])) {
$snippet["name"] = "<unnamed>";
}
$snippet["sys-enabled"] = false;
$snippet["proj-enabled"] = $ps["proj-enabled"][$i] == true;
$snippet["jsmo"] = $ps["proj-jsmo"][$i] == true;
$snippet["debug"] = $ps["proj-debug"][$i] == true;
$snippet["code"] = $ps["proj-code"][$i];
$snippet["proj-limit"] = "include";
$snippet["proj-list"] = [$project_id];
$snippet["form-list"] = array_filter($ps["proj-instruments"][$i], function($item) {
return !empty($item);
});
$snippet["ctx"]["projall"] = $ps["proj-context_all"][$i] == true;
$snippet["ctx"]["sysall"] = false;
foreach ($contexts as $this_context) {
$snippet["ctx"][$this_context] = false;
}
foreach ($ps as $this_key => $this_val) {
if (starts_with($this_key, "proj-context")) {
$this_context = array_pop(explode("_", $this_key, 2));
if (in_array($this_context, $contexts, true)) {
if ($this_val[$i] == "include") {
$snippet["ctx"][$this_context] = true;
}
else if ($this_val[$i] <> "exclude") {
$snippet["ctx"][$this_context] = $snippet["ctx"]["projall"];
}
}
}
}
$snippets[] = $snippet;
}
return $snippets;
}
#endregion
#region Legacy (v1) Settings Conversion
/**
* Converts legacy v1 project settings to the new v2 model
* @return void
*/
private function convert_v1_settings() {
$projects = $this->getProjectsWithModuleEnabled(true);
// Process each project where the module is enabled
foreach ($projects as $pid) {
$old = $this->getProjectSettings($pid);
if (is_array($old["js"]) && count($old["js"])) {
// Prepare new settings format
$new = [
"enabled" => true,
"reserved-hide-from-non-admins-in-project-list" => $old["reserved-hide-from-non-admins-in-project-list"],
// Removes legacy settings
"js" => null,
"js_enabled" => null,
"js_type" => null,
"js_instruments" => null,
"js_code" => null
];
foreach ($old["js"] as $i => $_) {
$new["proj-snippet"][$i] = true;
$new["proj-enabled"][$i] = $old["js_enabled"][$i];
$new["proj-jsmo"][$i] = false;
$new["proj-debug"][$i] = false;
// Set default contexts
$new["proj-context_all"][$i] = false;
$new["proj-context_php"][$i] = null;
$new["proj-context_rsd"][$i] = null;
$new["proj-context_aer"][$i] = null;
$new["proj-context_rhp"][$i] = null;
$new["proj-context_data_entry"][$i] = null;
$new["proj-context_survey"][$i] = null;
$new["proj-context_report"][$i] = null;
$new["proj-context_db"][$i] = null;
$new["proj-context_dbp"][$i] = null;
// Convert legacy type to contexts
switch ($old["js_type"][$i]) {
case "all":
$new["proj-context_all"][$i] = true;
break;
case "survey,data_entry":
$new["proj-context_data_entry"][$i] = "include";
$new["proj-context_survey"][$i] = "include";
break;
default:
$new["proj-context_" . $old["js_type"][$i]][$i] = "include";
break;
}
$new["proj-instruments"][$i] = $old["js_instruments"][$i];
$new["proj-code"][$i] = $old["js_code"][$i];
}
// Store converted settings
$this->setProjectSettings($new, $pid);
}
}
// Workaround for version-change hook not working
$this->setSystemSetting("v1-upgrade", "done");
}
#endregion
#region Helpers
public static function IsSystemExternalModulesManager($page) {
return (strpos($page, "manager/control_center.php") !== false);
}
public static function IsProjectExternalModulesManager($page) {
return (strpos($page, "manager/project.php") !== false);
}
#endregion
}