1 | <?php |
---|
2 | /** |
---|
3 | * @file remotepc.php |
---|
4 | * @brief OpenGnsys Server REST API consumed by UDS Server for Remote PC implementation. |
---|
5 | * @warning All input and output messages are formatted in JSON. |
---|
6 | * @note Some ideas are based on article "How to create REST API for Android app using PHP, Slim and MySQL" by Ravi Tamada, thanx. |
---|
7 | * @license GNU GPLv3+ |
---|
8 | * @author Ramón M. Gómez, ETSII Univ. Sevilla |
---|
9 | * @version 1.1.0 - First version |
---|
10 | * @date 2017-02-01 |
---|
11 | */ |
---|
12 | |
---|
13 | |
---|
14 | // REST routes. |
---|
15 | |
---|
16 | /** |
---|
17 | * @brief Reserve a random client with an installed image and send a boot/reboot operation depending on its status. |
---|
18 | * @warning If "lab" parameter is specified, then choose a client from this lab. |
---|
19 | * @note Route: /ous/:ouid/labs/:labid/clients/:clntid/events, Method: POST |
---|
20 | * @param integer ouid OU identificator |
---|
21 | * @param integer imageid image identificator |
---|
22 | * @param integer labid lab. identificator (optional) |
---|
23 | */ |
---|
24 | $app->post('/ous/:ouid/images/:imageid/reserve', 'validateApiKey', |
---|
25 | function($ouid, $imageid) use ($app) { |
---|
26 | global $cmd; |
---|
27 | global $AMBITO_ORDENADORES; |
---|
28 | global $EJECUCION_COMANDO; |
---|
29 | global $ACCION_INICIADA; |
---|
30 | global $ACCION_SINRESULTADO; |
---|
31 | global $userid; |
---|
32 | $response = Array(); |
---|
33 | $ogagent = Array(); |
---|
34 | |
---|
35 | // Checking parameters. |
---|
36 | $ouid = htmlspecialchars($ouid); |
---|
37 | $imageid = htmlspecialchars($imageid); |
---|
38 | $labid = str_replace("%", "\%", htmlspecialchars($app->request()->params('lab'))); |
---|
39 | if (empty($labid)) $labid = '%'; // Clients in any lab. |
---|
40 | // Randomly choose a client with image installed and get ogAdmServer data. |
---|
41 | $cmd->texto = <<<EOD |
---|
42 | SELECT adm.idadministradorcentro, entornos.ipserveradm, entornos.portserveradm, |
---|
43 | ordenadores.idordenador, ordenadores.ip, ordenadores.mac, ordenadores.agentkey, |
---|
44 | ordenadores_particiones.numdisk, ordenadores_particiones.numpar, |
---|
45 | aulas.idaula, aulas.idcentro, remotepc.reserved |
---|
46 | FROM entornos, ordenadores |
---|
47 | JOIN aulas USING(idaula) |
---|
48 | RIGHT JOIN administradores_centros AS adm USING(idcentro) |
---|
49 | RIGHT JOIN usuarios USING(idusuario) |
---|
50 | RIGHT JOIN ordenadores_particiones USING(idordenador) |
---|
51 | RIGHT JOIN imagenes USING(idimagen) |
---|
52 | LEFT JOIN remotepc ON remotepc.id=ordenadores.idordenador |
---|
53 | WHERE adm.idadministradorcentro = '$userid' |
---|
54 | AND aulas.idcentro = '$ouid' AND aulas.idaula LIKE '$labid' AND aulas.inremotepc = 1 |
---|
55 | AND imagenes.idimagen = '$imageid' AND imagenes.inremotepc = 1 |
---|
56 | ORDER BY RAND() LIMIT 1; |
---|
57 | EOD; |
---|
58 | $rs=new Recordset; |
---|
59 | $rs->Comando=&$cmd; |
---|
60 | if (!$rs->Abrir()) return(false); // Error opening recordset. |
---|
61 | // Check if user is admin and client exists. |
---|
62 | $rs->Primero(); |
---|
63 | if (checkAdmin($rs->campos["idadministradorcentro"]) and checkParameter($rs->campos["idordenador"])) { |
---|
64 | // Check if client is not reserved. |
---|
65 | if ($rs->campos["reserved"] !== 1) { |
---|
66 | // Read query data. |
---|
67 | $serverip = $rs->campos["ipserveradm"]; |
---|
68 | $serverport = $rs->campos["portserveradm"]; |
---|
69 | $clntid = $rs->campos["idordenador"]; |
---|
70 | $clntip = $rs->campos["ip"]; |
---|
71 | $clntmac = $rs->campos["mac"]; |
---|
72 | $agentkey = $rs->campos["agentkey"]; |
---|
73 | $disk = $rs->campos["numdisk"]; |
---|
74 | $part = $rs->campos["numpar"]; |
---|
75 | $labid = $rs->campos["idaula"]; |
---|
76 | $ouid = $rs->campos["idcentro"]; |
---|
77 | // Check client's status. |
---|
78 | $ogagent[$clntip]['url'] = "https://$clntip:8000/opengnsys/status"; |
---|
79 | $result = multiRequest($ogagent); |
---|
80 | if (empty($result[$clntip]['data'])) { |
---|
81 | // Client is off, send a boot command to ogAdmServer. |
---|
82 | $reqframe = "nfn=Arrancar\r". |
---|
83 | "ido=$clntid\r". |
---|
84 | "iph=$clntip\r". |
---|
85 | "mac=$clntmac\r". |
---|
86 | "mar=1\r"; |
---|
87 | sendCommand($serverip, $serverport, $reqframe, $values); |
---|
88 | } else { |
---|
89 | // Client is on, send a reboot command to its OGAgent. |
---|
90 | $ogagent[$clntip]['url'] = "https://$clntip:8000/opengnsys/reboot"; |
---|
91 | $ogagent[$clntip]['header'] = Array("Authorization: ".$agentkey); |
---|
92 | $result = multiRequest($ogagent); |
---|
93 | // ... (check response) |
---|
94 | //if ($result[$clntip]['code'] != 200) { |
---|
95 | // ... |
---|
96 | } |
---|
97 | // DB Transaction: mark choosed client as reserved and |
---|
98 | // create an init session command into client's actions queue. |
---|
99 | $cmd->texto = "START TRANSACTION;"; |
---|
100 | $cmd->Ejecutar(); |
---|
101 | $timestamp = time(); |
---|
102 | $cmd->texto = <<<EOD |
---|
103 | INSERT INTO remotepc |
---|
104 | SET id='$clntid', reserved=1, urllogin=NULL, urllogout=NULL |
---|
105 | ON DUPLICATE KEY UPDATE |
---|
106 | id=VALUES(id), reserved=VALUES(reserved), |
---|
107 | urllogin=VALUES(urllogin), urllogout=VALUES(urllogout); |
---|
108 | EOD; |
---|
109 | $t1 = $cmd->Ejecutar(); |
---|
110 | $cmd->texto = <<<EOD |
---|
111 | INSERT INTO acciones |
---|
112 | SET tipoaccion=$EJECUCION_COMANDO, |
---|
113 | idtipoaccion=9, |
---|
114 | idcomando=9, |
---|
115 | parametros='nfn=IniciarSesion\rdsk=$disk\rpar=$part', |
---|
116 | descriaccion='RemotePC Session', |
---|
117 | idordenador=$clntid, |
---|
118 | ip='$clntip', |
---|
119 | sesion=$timestamp, |
---|
120 | fechahorareg=NOW(), |
---|
121 | estado=$ACCION_INICIADA, |
---|
122 | resultado=$ACCION_SINRESULTADO, |
---|
123 | ambito=$AMBITO_ORDENADORES, |
---|
124 | idambito=$clntid, |
---|
125 | restrambito='$clntip', |
---|
126 | idcentro=$ouid; |
---|
127 | EOD; |
---|
128 | $t2 = $cmd->Ejecutar(); |
---|
129 | if ($t1 and $t2) { |
---|
130 | // Commit transaction on success. |
---|
131 | $cmd->texto = "COMMIT;"; |
---|
132 | $cmd->Ejecutar(); |
---|
133 | // Send init session command if client is booted on ogLive. |
---|
134 | $reqframe = "nfn=IniciarSesion\r". |
---|
135 | "ido=$clntid\r". |
---|
136 | "iph=$clntip\r". |
---|
137 | "dsk=$disk\r". |
---|
138 | "par=$part\r"; |
---|
139 | sendCommand($serverip, $serverport, $reqframe, $values); |
---|
140 | // Compose JSON response. |
---|
141 | $response['id'] = $clntid; |
---|
142 | $response['ip'] = $clntip; |
---|
143 | $response['mac'] = $clntmac; |
---|
144 | $response['lab']['id'] = $labid; |
---|
145 | $response['ou']['id'] = $ouid; |
---|
146 | jsonResponse(200, $response); |
---|
147 | } else{ |
---|
148 | // Roll-back transaction on DB error. |
---|
149 | $cmd->texto = "ROLLBACK;"; |
---|
150 | $cmd->Ejecutar(); |
---|
151 | // Error message. |
---|
152 | $response["message"] = "Error updating database"; |
---|
153 | jsonResponse(400, $response); |
---|
154 | $app->stop(); |
---|
155 | } |
---|
156 | } else { |
---|
157 | // Error message. |
---|
158 | $response["message"] = "Client is already reserved"; |
---|
159 | jsonResponse(400, $response); |
---|
160 | $app->stop(); |
---|
161 | } |
---|
162 | } |
---|
163 | $rs->Cerrar(); |
---|
164 | } |
---|
165 | ); |
---|
166 | |
---|
167 | |
---|
168 | /** |
---|
169 | * @brief Store UDS server URLs to resend some events recieved from OGAgent. |
---|
170 | * @note Route: /ous/:ouid/labs/:labid/clients/:clntid/events, Method: POST |
---|
171 | * @param string urlLogin URL to redirect login notification. |
---|
172 | * @param string urlLogout URL to redirect logout notification. |
---|
173 | * @warning Events parameters will be stored in a new "remotepc" table. |
---|
174 | */ |
---|
175 | $app->post('/ous/:ouid/labs/:labid/clients/:clntid/events', 'validateApiKey', |
---|
176 | function($ouid, $labid, $clntid) use ($app) { |
---|
177 | global $cmd; |
---|
178 | global $userid; |
---|
179 | $response = Array(); |
---|
180 | |
---|
181 | // Reading JSON parameters. |
---|
182 | try { |
---|
183 | $input = json_decode($app->request()->getBody()); |
---|
184 | $urlLogin = htmlspecialchars($input->urlLogin); |
---|
185 | $urlLogout = htmlspecialchars($input->urlLogout); |
---|
186 | } catch (Exception $e) { |
---|
187 | // Error message. |
---|
188 | $response["message"] = $e->getMessage(); |
---|
189 | jsonResponse(400, $response); |
---|
190 | $app->stop(); |
---|
191 | } |
---|
192 | |
---|
193 | // Checking parameters. |
---|
194 | $ouid = htmlspecialchars($ouid); |
---|
195 | $labid = htmlspecialchars($labid); |
---|
196 | $clntid = htmlspecialchars($clntid); |
---|
197 | // Select client data for UDS compatibility. |
---|
198 | $cmd->texto = <<<EOD |
---|
199 | SELECT adm.idadministradorcentro, ordenadores.idordenador, remotepc.* |
---|
200 | FROM remotepc |
---|
201 | RIGHT JOIN ordenadores ON remotepc.id=ordenadores.idordenador |
---|
202 | JOIN aulas USING(idaula) |
---|
203 | RIGHT JOIN administradores_centros AS adm USING(idcentro) |
---|
204 | RIGHT JOIN usuarios USING(idusuario) |
---|
205 | WHERE adm.idadministradorcentro = '$userid' |
---|
206 | AND idcentro = '$ouid' AND aulas.idaula ='$labid' |
---|
207 | AND ordenadores.idordenador = '$clntid'; |
---|
208 | EOD; |
---|
209 | $rs=new Recordset; |
---|
210 | $rs->Comando=&$cmd; |
---|
211 | if (!$rs->Abrir()) return(false); // Error opening recordset. |
---|
212 | // Check if user is admin and client exists. |
---|
213 | $rs->Primero(); |
---|
214 | if (checkAdmin($rs->campos["idadministradorcentro"]) and checkParameter($rs->campos["idordenador"])) { |
---|
215 | // Check if client is reserved. |
---|
216 | if ($rs->campos["reserved"] == 1) { |
---|
217 | // Updating DB if client is reserved. |
---|
218 | $cmd->texto = <<<EOD |
---|
219 | INSERT INTO remotepc |
---|
220 | SET id='$clntid', reserved=1, urllogin='$urlLogin', urllogout='$urlLogout' |
---|
221 | ON DUPLICATE KEY UPDATE |
---|
222 | id=VALUES(id), reserved=VALUES(reserved), |
---|
223 | urllogin=VALUES(urllogin), urllogout=VALUES(urllogout); |
---|
224 | EOD; |
---|
225 | $cmd->Ejecutar(); |
---|
226 | // Confirm operation. |
---|
227 | jsonResponse(200, ""); |
---|
228 | } else { |
---|
229 | // Error message. |
---|
230 | $response["message"] = "Client is not reserved"; |
---|
231 | jsonResponse(400, $response); |
---|
232 | $app->stop(); |
---|
233 | } |
---|
234 | } |
---|
235 | $rs->Cerrar(); |
---|
236 | } |
---|
237 | ); |
---|
238 | |
---|
239 | |
---|
240 | $app->post('/ous/:ouid/labs/:labid/clients/:clntid/session', 'validateApiKey', |
---|
241 | function($ouid, $imageid) use ($app) { |
---|
242 | } |
---|
243 | ); |
---|
244 | |
---|
245 | |
---|
246 | $app->delete('/ous/:ouid/labs/:labid/clients/:clntid/unreserve', 'validateApiKey', |
---|
247 | function($ouid, $labid, $clntid) { |
---|
248 | global $cmd; |
---|
249 | global $userid; |
---|
250 | $response = Array(); |
---|
251 | $ogagent = Array(); |
---|
252 | |
---|
253 | // Checking parameters. |
---|
254 | $ouid = htmlspecialchars($ouid); |
---|
255 | $labid = htmlspecialchars($labid); |
---|
256 | $clntid = htmlspecialchars($clntid); |
---|
257 | // Select client data for UDS compatibility. |
---|
258 | $cmd->texto = <<<EOD |
---|
259 | SELECT adm.idadministradorcentro, ordenadores.idordenador, ordenadores.ip, ordenadores.agentkey, remotepc.reserved |
---|
260 | FROM remotepc |
---|
261 | RIGHT JOIN ordenadores ON remotepc.id=ordenadores.idordenador |
---|
262 | JOIN aulas USING(idaula) |
---|
263 | RIGHT JOIN administradores_centros AS adm USING(idcentro) |
---|
264 | RIGHT JOIN usuarios USING(idusuario) |
---|
265 | WHERE adm.idadministradorcentro = '$userid' |
---|
266 | AND idcentro = '$ouid' AND aulas.idaula ='$labid' |
---|
267 | AND ordenadores.idordenador = '$clntid'; |
---|
268 | EOD; |
---|
269 | $rs=new Recordset; |
---|
270 | $rs->Comando=&$cmd; |
---|
271 | if (!$rs->Abrir()) return(false); // Error opening recordset. |
---|
272 | // Check if user is admin and client exists. |
---|
273 | $rs->Primero(); |
---|
274 | if (checkAdmin($rs->campos["idadministradorcentro"]) and checkParameter($rs->campos["idordenador"])) { |
---|
275 | // Check if client is reserved. |
---|
276 | if ($rs->campos["reserved"] == 1) { |
---|
277 | // Read query data. |
---|
278 | $clntip = $rs->campos["ip"]; |
---|
279 | $agentkey = $rs->campos["agentkey"]; |
---|
280 | // DB Transaction: clear client reservation data and |
---|
281 | // remove pending boot commands from client's actions queue. |
---|
282 | $cmd->texto = "START TRANSACTION;"; |
---|
283 | $cmd->Ejecutar(); |
---|
284 | $cmd->texto = <<<EOD |
---|
285 | UPDATE remotepc |
---|
286 | SET reserved=0, urllogin=NULL, urllogout=NULL |
---|
287 | WHERE id='$clntid'; |
---|
288 | EOD; |
---|
289 | $cmd->Ejecutar(); |
---|
290 | $cmd->texto = <<<EOD |
---|
291 | DELETE FROM acciones |
---|
292 | WHERE idordenador = '$clntid' |
---|
293 | AND descriaccion = 'RemotePC Session' |
---|
294 | AND fechahorafin = '0000-00-00 00:00:00'; |
---|
295 | EOD; |
---|
296 | $cmd->Ejecutar(); |
---|
297 | $cmd->texto = "COMMIT;"; |
---|
298 | $cmd->Ejecutar(); |
---|
299 | // Send a poweroff command to client's OGAgent. |
---|
300 | $ogagent[$clntip]['url'] = "https://$clntip:8000/opengnsys/poweroff"; |
---|
301 | $ogagent[$clntip]['header'] = Array("Authorization: ".$agentkey); |
---|
302 | $result = multiRequest($ogagent); |
---|
303 | // ... (check response) |
---|
304 | //if ($result[$clntip]['code'] != 200) { |
---|
305 | // ... |
---|
306 | // Confirm operation. |
---|
307 | jsonResponse(200, ""); |
---|
308 | } else { |
---|
309 | // Error message. |
---|
310 | $response["message"] = "Client is not reserved"; |
---|
311 | jsonResponse(400, $response); |
---|
312 | } |
---|
313 | } |
---|
314 | $rs->Cerrar(); |
---|
315 | } |
---|
316 | ); |
---|
317 | |
---|
318 | ?> |
---|