forked from emoncms/emoncms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.php
320 lines (287 loc) · 8.23 KB
/
core.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
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
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project:
http://openenergymonitor.org
*/
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
function get_application_path()
{
// Default to http protocol
$proto = "http";
// Detect if we are running HTTPS or proxied HTTPS
if (server('HTTPS') == 'on') {
// Web server is running native HTTPS
$proto = "https";
} elseif (server('HTTP_X_FORWARDED_PROTO') == "https") {
// Web server is running behind a proxy which is running HTTPS
$proto = "https";
} elseif (request_header('HTTP_X_FORWARDED_PROTO') == "https") {
$proto = "https";
}
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$path = dirname("$proto://" . server('HTTP_X_FORWARDED_HOST') . server('SCRIPT_NAME')) . "/";
} else {
$path = dirname("$proto://" . server('HTTP_HOST') . server('SCRIPT_NAME')) . "/";
}
return $path;
}
function db_check($mysqli, $database)
{
$result = $mysqli->query("SELECT count(table_schema) from information_schema.tables WHERE table_schema = '$database'");
$row = $result->fetch_array();
if ($row['0']>0) {
return true;
} else {
return false;
}
}
function controller($controller_name)
{
$output = array('content'=>EMPTY_ROUTE);
if ($controller_name) {
$controller = $controller_name."_controller";
$controllerScript = "Modules/".$controller_name."/".$controller.".php";
if (is_file($controllerScript)) {
load_language_files("Modules/".$controller_name."/locale");
require_once $controllerScript;
$output = $controller();
if (!is_array($output) || !isset($output["content"])) {
$output = array("content"=>$output);
}
}
}
return $output;
}
function view($filepath, array $args = array())
{
$content = '';
if (file_exists($filepath)) {
extract($args);
ob_start();
include "$filepath";
$content = ob_get_clean();
}
return $content;
}
/**
* strip slashes from GET values or null if not set
*
* @param string $index name of $_GET item
*
**/
function get($index)
{
$val = null;
if (isset($_GET[$index])) {
$val = rawurldecode($_GET[$index]);
}
$val = stripslashes($val);
return $val;
}
/**
* strip slashes from POST values or null if not set
*
* @param string $index name of $_POST item
*
**/
function post($index)
{
$val = null;
if (isset($_POST[$index])) {
// PHP automatically converts POST names with brackets `field[]` to type array
if (!is_array($_POST[$index])) {
$val = rawurldecode($_POST[$index]); // does not decode the plus symbol into spaces
} else {
// sanitize the array values
$SANTIZED_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
if (!empty($SANTIZED_POST[$index])) {
$val = $SANTIZED_POST[$index];
}
}
}
if (is_array($val)) {
$val = array_map("stripslashes", $val);
} else {
$val = stripslashes($val);
}
return $val;
}
/**
* strip slashes from POST or GET values or null if not set
*
* @param string $index name of $_POST or $_GET item
*
**/
function prop($index)
{
$val = null;
if (isset($_GET[$index])) {
$val = $_GET[$index];
}
if (isset($_POST[$index])) {
$val = $_POST[$index];
}
if (is_array($val)) {
$val = array_map("stripslashes", $val);
} else {
$val = stripslashes($val);
}
return $val;
}
function request_header($index)
{
$val = null;
$headers = apache_request_headers();
if (isset($headers[$index])) {
$val = $headers[$index];
}
return $val;
}
function server($index)
{
$val = null;
if (isset($_SERVER[$index])) {
$val = $_SERVER[$index];
}
return $val;
}
function delete($index)
{
parse_str(file_get_contents("php://input"), $_DELETE);//create array with posted (DELETE) method) values
$val = null;
if (isset($_DELETE[$index])) {
$val = $_DELETE[$index];
}
if (is_array($val)) {
$val = array_map("stripslashes", $val);
} else {
$val = stripslashes($val);
}
return $val;
}
function put($index)
{
parse_str(file_get_contents("php://input"), $_PUT);//create array with posted (PUT method) values
$val = null;
if (isset($_PUT[$index])) {
$val = $_PUT[$index];
}
if (is_array($val)) {
$val = array_map("stripslashes", $val);
} else {
$val = stripslashes($val);
};
return $val;
}
function version()
{
$version_file = file_get_contents('./version.txt');
$version = filter_var($version_file, FILTER_SANITIZE_STRING);
return $version;
}
function load_db_schema()
{
$schema = array();
$dir = scandir("Modules");
for ($i=2; $i<count($dir); $i++) {
if (filetype("Modules/".$dir[$i])=='dir' || filetype("Modules/".$dir[$i])=='link') {
if (is_file("Modules/".$dir[$i]."/".$dir[$i]."_schema.php")) {
require "Modules/".$dir[$i]."/".$dir[$i]."_schema.php";
}
}
}
return $schema;
}
/**
* binds the gettext translations to the correct file and domain/type
*
* @param string $path path to the directory containing the .mo files for each language
* @param [string] $domain
* @return void
*/
function load_language_files($path, $domain = 'messages')
{
// Load language files for module
bind_textdomain_codeset($domain, 'UTF-8');
bindtextdomain($domain, $path);
textdomain($domain);
}
function load_menu()
{
global $menu;
$dir = scandir("Modules");
for ($i=2; $i<count($dir); $i++)
{
if (filetype("Modules/".$dir[$i])=='dir' || filetype("Modules/".$dir[$i])=='link')
{
if (is_file("Modules/".$dir[$i]."/".$dir[$i]."_menu.php"))
{
// load_language_files("Modules/".$dir[$i]."/locale");
require "Modules/".$dir[$i]."/".$dir[$i]."_menu.php";
}
}
}
}
function http_request($method, $url, $data)
{
$options = array();
if ($method=="GET") {
$urlencoded = http_build_query($data);
$url = "$url?$urlencoded";
} elseif ($method=="POST") {
$options[CURLOPT_POST] = 1;
$options[CURLOPT_POSTFIELDS] = $data;
}
$options[CURLOPT_URL] = $url;
$options[CURLOPT_RETURNTRANSFER] = 1;
$options[CURLOPT_CONNECTTIMEOUT] = 2;
$options[CURLOPT_TIMEOUT] = 5;
$curl = curl_init();
curl_setopt_array($curl, $options);
$resp = curl_exec($curl);
curl_close($curl);
return $resp;
}
function emoncms_error($message)
{
return array("success"=>false, "message"=>$message);
}
function call_hook($function_name, $args)
{
// @todo: make args parameter optional
$dir = scandir("Modules");
for ($i=2; $i<count($dir); $i++) {
if (filetype("Modules/".$dir[$i])=='dir' || filetype("Modules/".$dir[$i])=='link') {
if (is_file("Modules/".$dir[$i]."/".$dir[$i]."_hooks.php")) {
require "Modules/".$dir[$i]."/".$dir[$i]."_hooks.php";
if (function_exists($dir[$i].'_'.$function_name)==true) {
$hook = $dir[$i].'_'.$function_name;
return $hook($args);
}
}
}
}
}
// ---------------------------------------------------------------------------------------------------------
/**
* return ip address of requesting machine
* the ip address can be stored in different variables by the system.
* which variable name may change dependant on different system setups.
* this function *should return an acceptible value in most cases
* @todo: more testing on different hardware/opperating systems/proxy servers etc.
*
* @return string
*/
function get_client_ip_env()
{
$ipaddress = filter_var(getenv('REMOTE_ADDR'), FILTER_VALIDATE_IP);
if (empty($ipaddress)) {
$ipaddress = '';
}
return $ipaddress;
}