forked from vanderbilt-redcap/cross-project-piping-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks_common.php
executable file
·120 lines (94 loc) · 3.97 KB
/
hooks_common.php
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
<?php
namespace Vanderbilt\CrossprojectpipingExternalModule;
/**
This file contains common functions used by the redcap_hooks and redcap hook functions
The hook_log function is intended to help you debug your hooks and is a work-in-progress...
TODO: Allow you to specify a default file to log to instead of the php error log
TODO: Make sure it actually works :-) I haven't had much time to play with this
TODO: Maybe try wrapping hook code in try/catch so errors are easier to debug...
Each project can have a debug level for its hooks:
0 = only errors are logged (production),
1 = error and info statements are logged
2 = all statements are logged
3 = all statements are logged to error file AND screen
Andy Martin
Stanford University
**/
// Set the base hook folder to be one level higher than this file
define('HOOK_PATH_ROOT', dirname(__DIR__).DS);
define('HOOK_PATH_FRAMEWORK', dirname(__FILE__).DS);
define('HOOK_PATH_SERVER', HOOK_PATH_ROOT . "server" . DS);
// In order to access these configuration parameters inside the hook function, they must be global
global $hook_debug_default, $hook_debug_projects; //global $hook_functions, $hook_fields;
$hook_debug_default = 2;
$hook_debug_projects = array(
//PID => HOOK DEBUG LEVEL (0-3)
'17' => 2, //Primary Example Project
'21' => 2 //Hooks parsing tester
);
// Returns an array of paths to be included for the hook
function get_hook_include_files($function, $project_id = null) {
$paths = array();
// GLOBAL SINGLE HOOK FILE
$script = HOOK_PATH_SERVER."global".DS."global_hooks.php";
if (file_exists($script)) $paths[] = $script;
// GLOBAL HOOKS PER-FILE
$script = HOOK_PATH_SERVER."global".DS.$function.".php";
if (file_exists($script)) $paths[] = $script;
// PROJECT-SPECIFIED HOOKS IN ONE FILE
$script = HOOK_PATH_SERVER."pid".$project_id.DS."custom_hooks.php";
if (file_exists($script)) $paths[] = $script;
// PROJECT-SPECIFIC HOOKS PER-FILE (PREVIOUS VERSION)
$script = HOOK_PATH_SERVER."pid".$project_id.DS.$function.".php";
if (file_exists($script)) $paths[] = $script;
return $paths;
}
// Logging function for all hook activity
/*
The message parameter can be an object/array/string
Type can be: ERROR, INFO, DEBUG
0 = only errors are logged (production),
1 = error and info statements are logged
2 = all statements (including DEBUG) are logged
3 = all statements are logged to error file AND screen
*/
function hook_log($message, $type = 'INFO', $prefix = '') {
global $hook_debug_default, $hook_debug_projects, $project_id;
// Set the debug level
$hook_debug_level = array_key_exists($project_id, $hook_debug_projects) ? $hook_debug_projects[$project_id] : $hook_debug_default;
global $hook_debug_local;
//echo "<br>hook_debug_local: $hook_debug_local / hdl: $hook_debug_level";
if ($type == 'ERROR' || ($hook_debug_level == 1 && $type == 'INFO') || $hook_debug_level > 1) {
//echo "type: $type";
// Get calling file using php backtrace to help label where the log entry is coming from
$bt = debug_backtrace();
$calling_file = $bt[0]['file'];
$calling_function = $bt[3]['function'];
// Convert arrays/objects into string for logging
if (is_array($message)) {
$msg = "(array): " . print_r($message,true);
} elseif (is_object($message)) {
$msg = "(object): " . json_encode($message);
} elseif (is_string($message)) {
$msg = $message;
} else {
$msg = "(unknown): " . print_r($message,true);
}
// Prepend prefix
if ($prefix) $msg = "[$prefix] " . $msg;
// Output to error log
//error_log($project_id . "\t" . basename($calling_file, '.php') . "\t" . $calling_function . "\t" . $type . "\t" . $msg);
// Output to screen
if ($hook_debug_level == 3) {
print "
<pre style='background: #eee; border: 1px solid #ccc; padding: 5px;'>
Type: $type
File: ".basename($calling_file, '.php')."
Func: $calling_function
Msg : $msg</pre>";
}
}
}
function hook_exec_time($hook_start_time) {
return round((microtime(true) - $hook_start_time) * 1000,4) . " ms";
}