-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin_Core.php
More file actions
228 lines (200 loc) · 6.29 KB
/
Plugin_Core.php
File metadata and controls
228 lines (200 loc) · 6.29 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
<?php
class Plugin_Core
{
private $includedLibraries = array();
const PLUGIN_HOME_DIRECTORY = 'PLUGIN_HOME_DIRECTORY';
public function Libraries( $libraries = array() , $preloadInstance = true)
{
$success = true;
$message = '';
## If $libraries is an array we need to step through
## it loading each given library.
if( is_array($libraries) )
{
foreach( $libraries as $library )
{
if(in_array($library,$this->includedLibraries,true)) {
continue;
}
if(!self::loadLibrary( $library , $preloadInstance))
{
$success = false;
$message = $library;
}
}
}
## Else if it's a string we need to just load the one.
else if( is_string( $libraries ) )
{
if(in_array($libraries,$this->includedLibraries)) {
return;
}
if( !self::loadLibrary( $libraries , $preloadInstance) )
{
$success = false;
$message = $libraries;
}
}
else
{
$success = false;
$message = 'No library defined.';
}
if( !$success ) die( "Error loading Core::Libraries ({$message})" );
}
private function loadLibrary( $library = '', $preloadInstance = true)
{
$return = false;
$libaryNamespaceName = "\\Plugin\\$library";
## See if the library exists locally in Plugin/Library
if( file_exists( self::currentDirectory() . '/Libraries/' . $library . '.php' ) )
{
$this->includedLibraries[] = $library;
require_once( self::currentDirectory() . '/Libraries/' . $library . '.php' );
if($preloadInstance) {
try {
$this->$library = new $libaryNamespaceName();
}
catch(Exception $e) {
}
}
$return = true;
}
## Then check if the core library exists, if the local doesn't
else if( file_exists( __DIR__ . '/Libraries/' . $library . '.php' ) )
{
$this->includedLibraries[] = $library;
require_once(__DIR__ . '/Libraries/' . $library . '.php' );
if($preloadInstance) {
try {
$this->$library = new $libaryNamespaceName();
}
catch(Exception $e) {
}
}
$return = true;
}
return $return;
}
public function Helpers( $helpers = array() )
{
$success = true;
$message = '';
## If $libraries is an array we need to step through
## it loading each given library.
if( is_array($helpers) )
{
foreach( $helpers as $helper )
{
if(!self::loadHelper( $helper ))
{
$success = false;
$message = $helper;
}
}
}
## Else if it's a string we need to just load the one.
else if( is_string( $helpers ) )
{
if( !self::loadHelper( $helpers ) )
{
$success = false;
$message = $helpers;
}
}
else
{
$success = false;
$message = 'No helper defined.';
}
if( !$success ) die( "Error loading Core::Helpers ({$message})" );
}
private function loadHelper( $helper )
{
## Check if Helper file exists locally first since that should
## override the Core/Helpers one.
if( file_exists( self::currentDirectory() . '/Helpers/' . $helper . '.php' ) )
{
require_once( self::currentDirectory() . '/Helpers/' . $helper . '.php' );
return true;
}
## If the Helper file did not exist in Core/Helpers then we need to look
## in Plugin/Helpers for it.
elseif( file_exists( __DIR__ . '/Helpers/' . $helper . '.php' ) )
{
require_once(__DIR__ . '/Helpers/' . $helper . '.php' );
return true;
}
## If it didn't exist in either Core or Plugin then it's not found
else
{
return false;
}
}
private function currentDirectory()
{
if(defined(self::PLUGIN_HOME_DIRECTORY)) {
return constant(self::PLUGIN_HOME_DIRECTORY);
}
return getcwd();
}
private function getResourceURL()
{
$pageURL = 'http';
if (array_key_exists('HTTPS', $_SERVER) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"];
}
$pageURL .= substr(__DIR__,strlen(realpath($_SERVER["DOCUMENT_ROOT"])));
return $pageURL;
}
public function getCss()
{
print '<link rel="stylesheet" href="'.self::getResourceURL().'/css/bootstrap.min.css">';
print '<link rel="stylesheet" href="'.self::getResourceURL().'/css/bootstrap-theme.min.css">';
}
public function getJs()
{
print '<script src="'.self::getResourceURL().'/js/jquery.min.js"></script>';
print '<script src="'.self::getResourceURL().'/js/bootstrap.min.js"></script>';
}
public function test()
{
print __DIR__;
}
public function getEnvironment() {
if(!defined("ENVIRONMENT")) {
# Define the environment: options include "DEV", "TEST" or "PROD"
if (is_file('/app001/victrcore/lib/Victr/Env.php'))
include_once('/app001/victrcore/lib/Victr/Env.php');
if(class_exists("Victr_Env")) {
$envConf = Victr_Env::getEnvConf();
if ($envConf[Victr_Env::ENV_CURRENT] === Victr_Env::ENV_PROD) {
define("ENVIRONMENT", "PROD");
}
elseif ($envConf[Victr_Env::ENV_CURRENT] === Victr_Env::ENV_DEV) {
define("ENVIRONMENT", "TEST");
}
}
else {
define("ENVIRONMENT", "DEV");
}
}
return ENVIRONMENT;
}
public static function displayErrorsAndWarnings()
{
ini_set('display_errors', 1);
error_reporting(E_ALL);
set_error_handler(function($errno){
if($errno != E_ERROR){
# REDCAP only shows E_ERROR messages by default.
# We return false here to enable other messages as well (like warnings).
return false;
}
});
}
}