This repository has been archived by the owner on Nov 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpanels_query.php
204 lines (182 loc) · 6.42 KB
/
panels_query.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
<?php
// Settings
require_once('settings.php');
// AJAX calls, example: paneles_query.php?user=john&togglePanel=P1&set=0
if(isset($_REQUEST['togglePanel'])) {
// Check valid username
if((!isset($_REQUEST['user'])) || (strlen($_REQUEST['user']) < 4) || (strlen($_REQUEST['user']) > 30) || (!ctype_alnum($_REQUEST['user']))) {
echo 'Uername not valid';
http_response_code(400);
exit;
}
// Check valid panel
if(($_REQUEST['togglePanel'] !== 'P1') && ($_REQUEST['togglePanel'] !== 'P2') && ($_REQUEST['togglePanel'] !== 'P3') && ($_REQUEST['togglePanel'] !== 'P4') && ($_REQUEST['togglePanel'] !== 'P5')) {
echo 'Panel name not valid';
http_response_code(400);
exit;
}
// Check valid set to
if((!isset($_REQUEST['set'])) || (($_REQUEST['set'] !== '0') && ($_REQUEST['set'] !== '1'))) {
echo 'Bad panel assignment';
http_response_code(400);
exit;
}
// Connect to the database
$connection = new mysqli($db_server, $db_username, $db_password, $db_name);
if($connection->connect_error) {
echo 'Error connecting to the database';
http_response_code(400);
exit;
}
// Set charset to utf8
$connection->set_charset("utf8");
// Get the userid
$result = $connection->query("SELECT id FROM users WHERE username='" . $_REQUEST['user'] . "';");
if(($result === FALSE) || (empty($result))) {
$connection->close();
echo 'Username does not exist';
http_response_code(400);
exit;
}
$userid = $result->fetch_object()->id;
// Toggle panel
if($connection->query("UPDATE ewatcher SET " . $_REQUEST['togglePanel'] . "=" . $_REQUEST['set'] . " WHERE userid=$userid;") === FALSE) {
$connection->close();
echo 'Error while updating EWatcher panel configuration';
http_response_code(400);
exit;
}
$connection->close();
http_response_code(200);
exit;
}
// Functions
// Check if user exists
//
// Parameters:
// $username: name of the user
//
// Returns
// true: user exists
// false: user does not exist
function check_user($username) {
// Global variables
global $db_server, $db_username, $db_password, $db_name, $schema;
// Check valid username
if((!isset($username)) || (strlen($username) < 4) || (strlen($username) > 30) || (!ctype_alnum($username))) {
return 'Username not valid';
}
// Create connection
$connection = new mysqli($db_server, $db_username, $db_password, $db_name);
if($connection->connect_error) {
return 'Error while connecting to the database';
}
// Set charset to utf8
$connection->set_charset("utf8");
// Create table if it does not exist
if(check_table($connection, 'ewatcher', $schema['ewatcher']) === false) {
$connection->close();
return 'Error while creating the EWatcher configuration table';
}
// Check if user exists in the users table, get userid
$result = $connection->query("SELECT id FROM users WHERE username='$username';");
if(($result === FALSE) || (empty($result)) || ($result->num_rows == 0)) {
$connection->close();
return 'Username does not exist';
}
$userid = $result->fetch_object()->id;
// Check if user exists in the ewatcher table
$result = $connection->query("SELECT * FROM ewatcher WHERE userid=$userid;");
if(($result === FALSE) || (empty($result)) || ($result->num_rows == 0)) {
// Create ewatcher user config if it does not exist
if($connection->query("INSERT INTO ewatcher (userid) VALUES ($userid);") === FALSE) {
$connection->close();
return 'Error while creating user configuration (EWatcher)';
}
}
$connection->close();
return true;
}
// Get panel values for a given user
//
// Parameters:
// $username: name of the user
//
// Returns
// false: error
// *array*: array of panel toggles, 'PanelName' => true, or 'PanelName' => false
function get_panel_values($username) {
// Global variables
global $db_server, $db_username, $db_password, $db_name;
// Create connection
$connection = new mysqli($db_server, $db_username, $db_password, $db_name);
if($connection->connect_error) {
return false;
}
// Set charset to utf8
$connection->set_charset("utf8");
// Get panel values
$result = $connection->query("SELECT id FROM users WHERE username='$username';");
if(($result === FALSE) || (empty($result)) || ($result->num_rows == 0)) {
$connection->close();
return false;
}
$userid = $result->fetch_object()->id;
$result = $connection->query("SELECT * FROM ewatcher WHERE userid=$userid;");
if(($result === FALSE) || (empty($result)) || ($result->num_rows == 0)) {
$connection->close();
return false;
}
$userData = $result->fetch_object();
// Return array of panel values
for ($i = 1; $i <= 5; $i++) {
$panelId = "P" . $i;
$panel[$panelId] = ($userData->$panelId == 1) ? true : false;
}
return $panel;
}
// Create table if it does not exist
//
// Parameters:
// $connection: database connection
// $table: name of the table
// $schema: schema of the table
//
// Returns
// false: error
// true: success
function check_table($connection, $table, $schema) {
$result = $connection->query("SHOW TABLES LIKE '$table';");
if(($result === FALSE) || (empty($result)) || ($result->num_rows == 0)) {
// Create table
$createQuery = "CREATE TABLE $table (";
foreach($schema as $column => $properties) {
$createQuery .= $column . ' ' . $properties['type'];
if($properties['type'] == 'text') {
$createQuery .= ' character set utf8';
}
// Not null
if((isset($properties['Null'])) && ($properties['Null'] == 'NO')) {
$createQuery .= ' NOT NULL';
}
if(isset($properties['default'])) {
$createQuery .= ' DEFAULT ';
if(is_string($properties['default'])) {
$createQuery .= "'" . $properties['default'] . "'";
} else {
$createQuery .= $properties['default'];
}
}
// Default value
$createQuery .= ', ';
}
$createQuery = substr($createQuery, 0, -strlen(', '));
$createQuery .= ');';
if($connection->query($createQuery) === FALSE) {
// Error creating the table
return false;
}
}
return true;
}
?>