-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathdashboard_controller.php
More file actions
169 lines (149 loc) · 7.82 KB
/
Copy pathdashboard_controller.php
File metadata and controls
169 lines (149 loc) · 7.82 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
<?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
*/
defined('EMONCMS_EXEC') or die('Restricted access');
function dashboard_controller()
{
global $mysqli, $session, $user, $route, $path;
require "Modules/dashboard/dashboard_model.php";
$dashboard = new Dashboard($mysqli);
// id, userid, content, height, name, alias, description, main, public, published, showdescription, fullscreen
$result = false; $submenu = '';
if ($route->format == 'html')
{
if ($route->action == "list" && $session['write'])
{
load_language_files("Modules/dashboard/locale", "dashboard_messages");
$result = view("Modules/dashboard/Views/dashboard_list.php", array(
'path' => $path
));
}
else if ($route->action == "view")
{
// 4 access modes:
// - as a session user either login or apikey
// - with a readkey, does not create a session
// - via public dashboard username
// - via dashboard id for public dashboard
// - via dashboard alias for public dashboard with public feeds
$result = EMPTY_ROUTE;
$userid = false;
$apikey = "";
// $owner_context is true when $userid identifies the *requester* and so
// proves ownership: an interactive/apikey session, or a readkey (the
// owner's own read apikey). It is false on the public-profile path, where
// $userid is the *content owner* being browsed, not the requester - there
// ownership must never be inferred from $userid and only public
// dashboards may be shown.
$owner_context = false;
if (isset($session['read']) && $session['read']) {
$userid = $session['userid'];
$owner_context = true;
if (isset($_GET['apikey'])) {
$apikey = $user->get_apikey_read($session['userid']);
}
} else if (isset($_GET['readkey'])) {
if ($userid = $user->get_id_from_apikey($_GET['readkey'])) {
$apikey = $user->get_apikey_read($userid);
$owner_context = true;
}
} else if ($session['public_userid']) {
$userid = (int) $session['public_userid'];
}
$dashid = (int) get('id');
if ($dashid) {
$dash = $dashboard->get($dashid);
} else if ($route->subaction && $userid) {
$dash = $dashboard->get_from_alias($userid,$route->subaction);
} else if ($userid) {
$dash = $dashboard->get_main($userid);
} else if (!$userid and $route->subaction) {
$dash = $dashboard->get_from_public_alias($route->subaction);
}
if (isset($dash)) {
$public_userid = 0;
if (!$session['read'] && $dash['public']) {
$public_userid = $dash['userid'];
}
// Access control. A dashboard is shown if it is public, or - only in
// an owner context (session or the owner's own readkey) - if the
// requester owns it. On the public-profile path $userid is the content
// owner, not the requester, so the ownership clause is gated behind
// $owner_context; otherwise browsing /<username>/dashboard/view?id=N
// would expose every one of that user's dashboards, private included.
// Note: $apikey is NOT an authorisation signal here - it is the read
// key injected into the page so the feed widgets can load data, and in
// the logged-in branch it is the requester's own key. Testing it here
// previously let any key holder open any dashboard by id.
$owner = ($owner_context && $userid && $dash['userid']==$userid);
if ($dash['public'] || $owner) {
// A dashboard is meant to sit in an iframe on another
// site, so it takes the embed frame policy. That includes
// a private one: the way an owner embeds it is to put a
// key in the iframe url, and a key in the url is not the
// ambient authority a session cookie is. The site doing
// the framing had to know the key to write the url, and
// knowing it already grants everything framing the page
// could reach. set_frame_policy in core.php holds the
// relaxation back for a page the session cookie
// authenticated, which is the clickjacking case, so a
// logged in visitor is never relaxed whichever dashboard
// this is.
allow_public_embed();
$result = view("Modules/dashboard/Views/dashboard_view.php",array(
'dashboard'=>$dash,
'page_html'=>$dashboard->content_html($dash),
'apikey'=>$apikey,
'public_userid'=>$public_userid,
'owner'=>$owner
));
}
}
}
else if ($route->action == "edit" && $session['write'])
{
// The editor only ever opens the requester's own dashboard. It was
// loaded by id alone, so any writer could read the content of a
// private dashboard by asking for its id.
$dash = false;
if ($route->subaction) $dash = $dashboard->get_from_alias($session['userid'],$route->subaction);
elseif (isset($_GET['id'])) $dash = $dashboard->get_owned($session['userid'], get('id'));
if (!$dash) {
$result = EMPTY_ROUTE;
} else {
// Rendered once and given to both views, so the config modal shows
// the same content the page does.
$page_html = $dashboard->content_html($dash);
$result = view("Modules/dashboard/Views/dashboard_edit_view.php",array(
'dashboard'=>$dash,
'page_html'=>$page_html
));
$result .= view("Modules/dashboard/Views/dashboard_config.php", array(
'dashboard'=>$dash
));
$submenu = view("Modules/dashboard/Views/dashboard_menu.php", array('id'=>$dash['id'],'type'=>"edit"));
}
}
}
else if ($route->format == 'json')
{
if ($session['read']) {
if ($route->action=='list') $result = $dashboard->get_list($session['userid'], false, false);
}
if ($session['write']) {
if ($route->action=='set') $result = $dashboard->set($session['userid'],prop('id'),prop('fields'));
else if ($route->action=='getcontent') $result = $dashboard->get_content($session['userid'],get('id'));
else if ($route->action=='setcontent') $result = $dashboard->set_content($session['userid'],post('id'),post('content'),post('height'));
else if ($route->action=='create') $result = $dashboard->create($session['userid']);
else if ($route->action=='delete') $result = $dashboard->delete($session['userid'],get('id'));
else if ($route->action=='clone') $result = $dashboard->dashclone($session['userid'], get('id'));
}
}
return array('content'=>$result, 'submenu'=>$submenu);
}