-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateAbstraction.php
239 lines (206 loc) · 4.98 KB
/
StateAbstraction.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
class StateAbstraction
{
private $dbConn = null;
//
// Public functions
//
public function init()
{
//if ( !function_exists( "mysqli_init" ) )
if ( !extension_loaded( "mysqli" ) )
return false;
// Include database credentials
include( $_SERVER['DOCUMENT_ROOT']."/../spaceAPI_config.php" );
$this->dbConn = new mysqli($spaceApi_db_servername, $spaceApi_db_username, $spaceApi_db_password, $spaceApi_db_dbname);
// Check connection
if ($this->dbConn->connect_error) {
return false;
}
$this->dbConn->set_charset( "utf8mb4" );
return true;
}
public function getState()
{
$fallBack = $array = array(
"state" => "2",
"updated" => "0",
"created" => "0"
);
if(is_null($this->dbConn))
{
// Database connection not initialised
return $fallBack;
}
// No external input: no need to prepare statement
$dbResult = $this->dbConn->query("
SELECT `state`,
UNIX_TIMESTAMP(`updated`) AS `updated`,
UNIX_TIMESTAMP(`created`) AS `created`
FROM `statechanges`
ORDER BY `statechanges`.`id` DESC
LIMIT 0,1
");
if($dbResult->num_rows == 0)
{
return $fallBack;
}
else
{
$stateInfo = $dbResult->fetch_assoc();
//check if timed out
if((time() - $stateInfo['updated']) > 360)
{
// Set last known timestamp
$fallBack['created'] = $stateInfo['updated'];
$fallBack['updated'] = $stateInfo['updated'];
return $fallBack;
}
else
{
return $stateInfo;
}
}
}
public function getStateDescription( $_nState )
{
if(is_null($this->dbConn))
{
// Database connection not initialised
return null;
}
// Input validation
if( $_nState < 0 )
$_nState = 2 + $_nState;
if( $_nState < 0 || $_nState > 2 || !is_numeric( $_nState ))
{
return false;
}
// Prepare statement for input validation
$stmt = $this->dbConn->prepare(
"SELECT description,
`to` IS NULL AS empty
FROM `statedescriptions`
WHERE state = ?
AND `from` <= NOW()
AND (`to` IS NULL OR `to` >= NOW() )
ORDER BY `empty` ASC, `to` ASC
LIMIT 0,1"
);
if ( !$stmt )
{
return false;
}
else
{
$stmt->bind_param( "i", $_nState );
$stmt->execute( );
$stmt->store_result( );
}
if($stmt->num_rows == 0)
{
return null;
}
$stmt->bind_result( $description, $empty );
$stmt->fetch();
return $description;
}
public function updateState( $_nNewState )
{
// Handle -1 and -2 as 'permanent state', make sure the timestamp updates with every update, and the state resets after a 'normal' updatestate
// Input validation
$nNewStateOrg = (int)$_nNewState;
if( $_nNewState < 0 )
$_nNewState = 2 + $_nNewState;
// Input validation
if( $_nNewState < 0 || $_nNewState > 1 || !is_numeric( $_nNewState ))
return false;
$nCurrentState = $this->getRawState()['state'];
$nCurrentStateOrg = (int)$nCurrentState;
if( $nCurrentState < 0 )
$nCurrentState = 2 + $nCurrentState;
// Update if the new state matches the current state or if the current state is 'permanent', does NOT match the new state
if( ($nCurrentStateOrg == $nNewStateOrg) || ($nCurrentStateOrg < 0 && $nCurrentState != $_nNewState) )
{
// Change last updated time
// No external input: no need to prepare statement
$dbResult = $this->dbConn->query("UPDATE `statechanges` SET `updated` = CURRENT_TIMESTAMP ORDER BY id DESC LIMIT 1");
return $dbResult;
}
else
{
// Set new state
return $this->setState( $nNewStateOrg );
}
}
//
// Private functions
//
private function setState( $_nState )
{
if(is_null($this->dbConn))
{
// Database connection not initialised
return false;
}
// Input validation
if( $_nState < -2 || $_nState > 1 || !is_numeric( $_nState ))
return false;
$stmt = $this->dbConn->prepare(
"INSERT INTO `ackspace_spaceAPI`.`statechanges` (
`id` ,
`state` ,
`updated` ,
`created`
)
VALUES (
NULL ,
?,
CURRENT_TIMESTAMP ,
CURRENT_TIMESTAMP
);"
);
if (!$stmt)
{
return false;
}
else
{
$stmt->bind_param( "i", $_nState );
$stmt->execute();
}
return true;
}
private function getRawState()
{
$fallBack = $array = array(
"state" => "2",
"updated" => "0",
"created" => "0"
);
if(is_null($this->dbConn))
{
// Database connection not initialised
return $fallBack;
}
// No external input: no need to prepare statement
$dbResult = $this->dbConn->query("
SELECT `state`,
UNIX_TIMESTAMP(`updated`) AS `updated`,
UNIX_TIMESTAMP(`created`) AS `created`
FROM `statechanges`
ORDER BY `statechanges`.`id` DESC
LIMIT 0,1
");
if($dbResult->num_rows == 0)
{
return $fallBack;
}
else
{
$stateInfo = $dbResult->fetch_assoc();
return $stateInfo;
}
}
}
?>