forked from emoncms/MyHomeEnergyPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
153 lines (125 loc) · 4.7 KB
/
index.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
<?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
*/
$emoncms_version = "8.3.6";
$ltime = microtime(true);
define('EMONCMS_EXEC', 1);
// 1) Load settings and core scripts
require "process_settings.php";
require "core.php";
require "route.php";
require "locale.php";
$path = get_application_path();
require "Modules/log/EmonLogger.php";
// 2) Database
$mysqli = @new mysqli($server,$username,$password,$database);
if (class_exists('Redis') && $redis_enabled) {
$redis = new Redis();
$connected = $redis->connect("127.0.0.1");
if (!$connected) {
echo "Can't connect to redis database, it may be that redis-server is not installed or started see readme for redis installation"; die;
}
} else {
$redis = false;
}
if ( $mysqli->connect_error ) {
echo "Can't connect to database, please verify credentials/configuration in settings.php<br />";
if ( $display_errors ) {
echo "Error message: <b>" . $mysqli->connect_error . "</b>";
}
die();
}
if (!$mysqli->connect_error && $dbtest==true) {
require "Lib/dbschemasetup.php";
if (!db_check($mysqli,$database)) db_schema_setup($mysqli,load_db_schema(),true);
}
// 3) User sessions
require "Modules/user/rememberme_model.php";
$rememberme = new Rememberme($mysqli);
require("Modules/user/user_model.php");
$user = new User($mysqli,$redis,$rememberme);
if (isset($_GET['apikey']))
{
$session = $user->apikey_session($_GET['apikey']);
}
elseif (isset($_POST['apikey']))
{
$session = $user->apikey_session($_POST['apikey']);
}
else
{
$session = $user->emon_session_start();
}
// 4) Language
if (!isset($session['lang'])) $session['lang']='';
set_emoncms_lang($session['lang']);
// 5) Get route and load controller
$route = new Route(get('q'));
if (get('embed')==1) $embed = 1; else $embed = 0;
// If no route specified use defaults
if (!$route->controller && !$route->action)
{
// Non authenticated defaults
if (!$session['read'])
{
$route->controller = $default_controller;
$route->action = $default_action;
}
else // Authenticated defaults
{
$route->controller = $default_controller_auth;
$route->action = $default_action_auth;
}
}
if ($route->controller == 'input' && $route->action == 'post') $route->format = 'json';
if ($route->controller == 'input' && $route->action == 'bulk') $route->format = 'json';
// 6) Load the main page controller
$output = controller($route->controller);
// If no controller of this name - then try username
// need to actually test if there isnt a controller rather than if no content
// is returned from the controller.
if (!$output['content'] && $public_profile_enabled && $route->controller!='admin')
{
$userid = $user->get_id($route->controller);
if ($userid) {
$route->subaction = $route->action;
$session['userid'] = $userid;
$session['username'] = $route->controller;
$session['read'] = 1;
$session['profile'] = 1;
$route->action = $public_profile_action;
$output = controller($public_profile_controller);
}
}
$theme = "assessment";
if ($route->controller=="input") $theme = "monitor";
if ($route->controller=="feed") $theme = "monitor";
if ($route->controller=="vis") $theme = "monitor";
if ($route->controller=="dashboard") $theme = "monitor";
// 7) Output
if ($route->format == 'json')
{
header('Content-Type: application/json');
if ($route->controller=='time') {
print $output['content'];
} elseif ($route->controller=='input' && $route->action=='post') {
print $output['content'];
} elseif ($route->controller=='input' && $route->action=='bulk') {
print $output['content'];
} else {
print json_encode($output['content']);
}
}
if ($route->format == 'html')
{
$menu = load_menu();
$output['mainmenu'] = view("Theme/menu_view.php", array());
if ($embed == 0) print view("Theme/theme.php", $output);
if ($embed == 1) print view("Theme/embed.php", $output);
}