forked from emoncms/emoncms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
124 lines (102 loc) · 3.59 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
<?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
*/
$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();
// 2) Database
$mysqli = @new mysqli($server,$username,$password,$database);
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,$rememberme);
if (get('apikey'))
$session = $user->apikey_session($_GET['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 == 'api') $route->controller = 'input';
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);
}
}
// 7) Output
if ($route->format == '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);
}
$ltime = microtime(true) - $ltime;