-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmobClass.php
225 lines (217 loc) · 6.97 KB
/
mobClass.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
// Custom PHP Class "mobClass"
// Nothing to edit here.
class mobClass {
// Check if user is logged in.
public function loggedIn($username, $userID) {
if (!isset($username) || trim($username) === '') {
return FALSE;
}
if (!isset($userID) || trim($userID) === '') {
return FALSE;
}
return TRUE;
}
// Validate login information.
public function checkLogin($username, $password) {
global $db;
$username = $db->real_escape_string($username);
$password = $db->real_escape_string($password);
$loginQuery = "SELECT userID, password FROM users WHERE username = '$username';";
$loginResult = $db->query($loginQuery) or die ($db->error);
if($loginResult->num_rows < 1){
return 'invalidUsername';
}
$loginData = $loginResult->fetch_assoc();
If (validate_password($password, $loginData['password']) === FALSE) {
return 'invalidPassword';
} else {
session_regenerate_id();
$_SESSION['username'] = $username;
$_SESSION['userID'] = $loginData['userID'];
return TRUE;
}
$loginResult->free();
}
// Generate pages.
public function newDrawer($id, $title, $label, $submit = false, &$my_array, $messageCode) {
global $thisScript;
$render = ' <div data-role="view" id="drawer-' . $id . '" data-layout="drawer-layout" data-title="' . $title . '">
<ul data-role="listview" class="inboxList">' . "\r\n";
$render .= $this->newMessage($messageCode);
$render .= ' <form action="' . $thisScript . '" method="post">
<ul data-role="listview" data-style="inset" data-type="group">
<li>' . $label . '
<ul>' . "\r\n";
$arrayLength = count($my_array);
for ($row = 0; $row < $arrayLength; $row++) {
if (isset($my_array[$row][4]) && $my_array[$row][4] != "") {
$class = 'class="' . $my_array[$row][4] . '" ';
}
else {
$class = "";
}
$render .= ' <li>' . "\r\n";
$render .= ' <label>' . "\r\n";
$render .= ' ' . $my_array[$row][0] . "\r\n";
$render .= ' <input type="' . $my_array[$row][1] . '" name="' . $my_array[$row][2] . '" value="' . $my_array[$row][3] . '" ' . $class . '/>' . "\r\n";
$render .= ' </label>' . "\r\n";
$render .= ' </li>' . "\r\n";
}
$render .= ' </ul>
</li>
</ul>';
if ($submit) {
$render .= '
<input type="submit" style="margin-left: -1000px" />';
}
$render .= '
</form>
</ul>
</div>' . "\r\n";
return $render;
}
// Generate page to edit pin descriptions.
public function fillEditPinForm() {
global $thisScript, $db;
$fillQuery = "SELECT pinNumber, pinStatus, pinEnabled FROM pinStatus";
$fillResult = $db->query($fillQuery) or die ($db->error);
$fillQuery2 = "SELECT pinNumber, pinDescription FROM pinDescription";
$fillResult2 = $db->query($fillQuery2) or die ($db->error);
$totalGPIOCount = $fillResult->num_rows;
$currentGPIOCount = 0;
while ($currentGPIOCount < $totalGPIOCount) {
$pinRow = $fillResult->fetch_assoc();
$descRow = $fillResult2->fetch_assoc();
$pinEnabled = $pinRow['pinEnabled'];
$pinNumber = $pinRow['pinNumber'];
$pinDescription = $descRow['pinDescription'];
if ($pinEnabled == 1) {
$pinChecked = 'checked="checked" ';
}
else {
$pinChecked = "";
}
print '
<li>
<label>
Pin ' . $pinNumber . '
<input type="text" name="pinDescription' . $pinNumber . '" value="' . $pinDescription . '" />
</label>
</li>
<li>
<label>
Enable Pin ' . $pinNumber . '
<input type="checkbox" name="pinEnabled' . $pinNumber . '" ' . $pinChecked . ' />
</label>
</li>' . "\r\n";
$currentGPIOCount ++;
}
print ' <input type="hidden" name="action" value="update" />
<input type="submit" style="margin-left: -1000px" />' . "\r\n";
$fillResult->free();
$fillResult2->free();
}
// Generate main form to toggle pin status.
public function fillToggleForm() {
global $thisScript, $db;
$fillQuery = "SELECT pinNumber, pinStatus, pinEnabled FROM pinStatus";
$fillResult = $db->query($fillQuery) or die ($db->error);
$fillQuery2 = "SELECT pinNumber, pinDescription FROM pinDescription";
$fillResult2 = $db->query($fillQuery2) or die ($db->error);
$totalGPIOCount = $fillResult->num_rows;
$currentGPIOCount = 0;
while ($currentGPIOCount < $totalGPIOCount) {
$pinRow = $fillResult->fetch_assoc();
$descRow = $fillResult2->fetch_assoc();
$pinEnabled = $pinRow['pinEnabled'];
$pinNumber = $pinRow['pinNumber'];
$pinStatus = $pinRow['pinStatus'];
$pinDescription = $descRow['pinDescription'];
If ($pinStatus == "0") {
$action = "turnOn";
$checked = '';
} else {
$action = "turnOff";
$checked = ' checked="checked"';
}
if ($pinEnabled == 1) {
print ' <li>
<label>
' . $pinDescription . '
<form id="form' . $pinNumber . '" action="' . $thisScript . '" method="post">
<input id="pin' . $pinNumber . '" data-role="switch" data-change="switchChange' . $pinNumber . '"' . $checked . ' />
<input type="hidden" name="pin" value="' . $pinNumber . '" />
<input type="hidden" name="action" value="' . $action . '" />
</form>
</label>
</li>' . "\r\n";
}
$currentGPIOCount ++;
}
$fillResult->free();
$fillResult2->free();
}
// Generate array with pin numbers.
public function arrayPins() {
$newArray = array();
global $thisScript, $db;
$fillQuery = "SELECT pinNumber FROM pinStatus ORDER BY pinID ASC";
$fillResult = $db->query($fillQuery) or die ($db->error);
while (($pinRow = $fillResult->fetch_assoc())) {
$newArray[] = $pinRow['pinNumber'];
}
$fillResult->free();
return $newArray;
}
// Print messages.
public function newMessage($code = '') {
global $thisScript;
if ($code == '') {
return '';
}
if ($code == "passwordChanged") {
$message = "Password Changed Successfullly.";
$type = "success";
}
else if ($code == "pinUpdated") {
$message = "Pin Updated Successfullly.";
$type = "success";
}
else if ($code == "pinDescriptionUpdated") {
$message = "Pin Description Updated Successfullly.";
$type = "success";
}
else if ($code == "passwordsDoNotMatch") {
$message = "New passwords do not match.";
$type = "error";
}
else if ($code == "passwordLength") {
$message = "Password must be 1 to 256 characters long.";
$type = "error";
}
else if ($code == "incorrectUsername") {
$message = "Invalid Username.";
$type = "error";
}
else if ($code == "incorrectPassword") {
$message = "Invalid Password.";
$type = "error";
}
else if ($code == "invalidUsername") {
$message = "Invalid Username.";
$type = "error";
}
else if ($code == "invalidPassword") {
$message = "Invalid Password.";
$type = "error";
}
else {
return '';
}
return ' <li>
<div class="k-block k-' . $type . '-colored" data-role="listview"><a href="' . $thisScript . '" data-icon="closemessage">' . $message . '</a></div>
</li>' . "\r\n";
}
}
?>