forked from emoncms/emoncms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.php
183 lines (155 loc) · 4.58 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
<?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
*/
define('EMONCMS_EXEC', 1);
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";
}
if( isset( $_SERVER['HTTP_X_FORWARDED_SERVER'] ))
$path = dirname("$proto://" . server('HTTP_X_FORWARDED_SERVER') . server('SCRIPT_NAME')) . "/";
else
$path = dirname("$proto://" . server('HTTP_HOST') . server('SCRIPT_NAME')) . "/";
return $path;
}
function emon_session_start()
{
session_set_cookie_params(
3600 * 24 * 30, //lifetime, 30 days
"/", //path
"", //domain
false, //secure
true//http_only
);
session_start();
if (isset($_SESSION['admin'])) $session['admin'] = $_SESSION['admin']; else $session['admin'] = 0;
if (isset($_SESSION['read'])) $session['read'] = $_SESSION['read']; else $session['read'] = 0;
if (isset($_SESSION['write'])) $session['write'] = $_SESSION['write']; else $session['write'] = 0;
if (isset($_SESSION['userid'])) $session['userid'] = $_SESSION['userid']; else $session['userid'] = 0;
return $session;
}
function get($index)
{
$val = null;
if (isset($_GET[$index])) $val = $_GET[$index];
return $val;
}
function post($index)
{
$val = null;
if (isset($_POST[$index])) $val = $_POST[$index];
return $val;
}
function server($index)
{
$val = null;
if (isset($_SERVER[$index])) $val = $_SERVER[$index];
return $val;
}
function decode_route($q)
{
// Init vars
$route = array();
$route['format'] = "html";
$route['controller'] = '';
$route['action'] = '';
$route['subaction'] = '';
// filter out all except a-z / .
$q = preg_replace('/[^.\/A-Za-z0-9]/', '', $q);
// Split
$args = preg_split('/[\/]/', $q);
// get format (part of last argument after . i.e view.json)
$lastarg = sizeof($args) - 1;
$lastarg_split = preg_split('/[.]/', $args[$lastarg]);
if (count($lastarg_split) > 1) { $route['format'] = $lastarg_split[1]; }
$args[$lastarg] = $lastarg_split[0];
if (count($args) > 0) { $route['controller'] = $args[0]; }
if (count($args) > 1) { $route['action'] = $args[1]; }
if (count($args) > 2) { $route['subaction'] = $args[2]; }
return $route;
}
function controller($controller_name)
{
$output = array('content'=>'');
if ($controller_name)
{
$controller = $controller_name."_controller";
$controllerScript = "Modules/".$controller_name."/".$controller.".php";
if (is_file($controllerScript))
{
// Load language files for module
$domain = "messages";
bindtextdomain($domain, "Modules/".$controller_name."/locale");
bind_textdomain_codeset($domain, 'UTF-8');
textdomain($domain);
require $controllerScript;
$output = $controller();
}
}
return $output;
}
function view($filepath, array $args)
{
extract($args);
ob_start();
include "Modules/$filepath";
$content = ob_get_clean();
return $content;
}
function theme($filepath, array $args)
{
global $theme;
extract($args);
ob_start();
include "Theme/$theme/$filepath";
$content = ob_get_clean();
return $content;
}
function load_db_schema()
{
$schema = array();
$dir = scandir("Modules");
for ($i=2; $i<count($dir); $i++)
{
if (filetype("Modules/".$dir[$i])=='dir')
{
if (is_file("Modules/".$dir[$i]."/".$dir[$i]."_schema.php"))
{
require "Modules/".$dir[$i]."/".$dir[$i]."_schema.php";
}
}
}
return $schema;
}
function load_menu()
{
$menu_left = array();
$dir = scandir("Modules");
for ($i=2; $i<count($dir); $i++)
{
if (filetype("Modules/".$dir[$i])=='dir')
{
if (is_file("Modules/".$dir[$i]."/".$dir[$i]."_menu.php"))
{
require "Modules/".$dir[$i]."/".$dir[$i]."_menu.php";
}
}
}
return $menu_left;
}
?>