| 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 | // OGAgent sessions log file. |
|---|
| 14 | define('REMOTEPC_LOGFILE', '/opt/opengnsys/log/remotepc.log'); |
|---|
| 15 | |
|---|
| 16 | // Function to write a line into log file. |
|---|
| 17 | function writeRemotepcLog($message = "") { |
|---|
| 18 | file_put_contents(REMOTEPC_LOGFILE, date(DATE_ISO8601).": $message\n", FILE_APPEND); |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | // REST routes. |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * @brief Reserve a client with an installed image and the older reservation time, then send a boot/reboot operation depending on its status. |
|---|
| 26 | * @warning If "lab" parameter is specified, then choose a client from this lab. |
|---|
| 27 | * @note Route: /ous/:ouid/images/:imageid/reserve, Method: POST |
|---|
| 28 | * @param integer ouid OU identificator |
|---|
| 29 | * @param integer imageid image identificator |
|---|
| 30 | * @note Input JSON message: {"labid":int_labid,"maxtime":int_hours} |
|---|
| 31 | */ |
|---|
| 32 | $app->post('/ous/:ouid/images/:imageid/reserve(/)', 'validateApiKey', |
|---|
| 33 | function($ouid, $imageid) use ($app) { |
|---|
| 34 | global $cmd; |
|---|
| 35 | global $AMBITO_ORDENADORES; |
|---|
| 36 | global $EJECUCION_COMANDO; |
|---|
| 37 | global $ACCION_INICIADA; |
|---|
| 38 | global $ACCION_FINALIZADA; |
|---|
| 39 | global $ACCION_SINRESULTADO; |
|---|
| 40 | global $ACCION_FALLIDA; |
|---|
| 41 | global $userid; |
|---|
| 42 | $response = Array(); |
|---|
| 43 | $ogagent = Array(); |
|---|
| 44 | |
|---|
| 45 | if ($app->settings['debug']) |
|---|
| 46 | writeRemotepcLog($app->request()->getResourceUri(). ": Init."); |
|---|
| 47 | // Checking parameters. |
|---|
| 48 | try { |
|---|
| 49 | if (empty(preg_match('/^python-requests\//', $_SERVER['HTTP_USER_AGENT']))) { |
|---|
| 50 | throw new Exception("Bad agent: sender=".$_SERVER['REMOTE_ADDR'].", agent=".$_SERVER['HTTP_USER_AGENT']); |
|---|
| 51 | } |
|---|
| 52 | if (!checkIds($ouid, $imageid)) { |
|---|
| 53 | throw new Exception("Ids. must be positive integers"); |
|---|
| 54 | } |
|---|
| 55 | // Reading POST parameters in JSON format. |
|---|
| 56 | $input = json_decode($app->request()->getBody()); |
|---|
| 57 | // Default: no lab. filter. |
|---|
| 58 | if (isset($input->labid)) { |
|---|
| 59 | $labid = $input->labid != "0" ? $input->labid : '%'; |
|---|
| 60 | } else { |
|---|
| 61 | $labid = '%'; |
|---|
| 62 | } |
|---|
| 63 | $maxtime = isset($input->maxtime) ? $input->maxtime : 24; // Default: 24 h. |
|---|
| 64 | $opts = Array('options' => Array('min_range' => 1)); // Check for int>0 |
|---|
| 65 | if (filter_var($labid, FILTER_VALIDATE_INT, $opts) === false and $labid !== '%') { |
|---|
| 66 | throw new Exception("Lab id. must be positive integer"); |
|---|
| 67 | } |
|---|
| 68 | if (filter_var($maxtime, FILTER_VALIDATE_INT, $opts) === false) { |
|---|
| 69 | throw new Exception("Time must be positive integer (in hours)"); |
|---|
| 70 | } |
|---|
| 71 | } catch (Exception $e) { |
|---|
| 72 | // Communication error. |
|---|
| 73 | $response["message"] = $e->getMessage(); |
|---|
| 74 | if ($app->settings['debug']) |
|---|
| 75 | writeRemotepcLog($app->request()->getResourceUri(). ": ERROR: ".$response["message"]."."); |
|---|
| 76 | jsonResponse(400, $response); |
|---|
| 77 | $app->stop(); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | if ($app->settings['debug']) |
|---|
| 81 | writeRemotepcLog($app->request()->getResourceUri(). ": Parameters: labid=$labid, maxtime=$maxtime"); |
|---|
| 82 | // Choose older not-reserved client with image installed and get ogAdmServer data. |
|---|
| 83 | $cmd->texto = <<<EOD |
|---|
| 84 | SELECT adm.idusuario, entornos.ipserveradm, entornos.portserveradm, |
|---|
| 85 | ordenadores.idordenador, ordenadores.nombreordenador, ordenadores.ip, |
|---|
| 86 | ordenadores.mac, ordenadores.agentkey, ordenadores_particiones.numdisk, |
|---|
| 87 | ordenadores_particiones.numpar, aulas.idaula, aulas.idcentro |
|---|
| 88 | FROM entornos, ordenadores |
|---|
| 89 | JOIN aulas USING(idaula) |
|---|
| 90 | RIGHT JOIN administradores_centros AS adm USING(idcentro) |
|---|
| 91 | RIGHT JOIN usuarios USING(idusuario) |
|---|
| 92 | RIGHT JOIN ordenadores_particiones USING(idordenador) |
|---|
| 93 | RIGHT JOIN imagenes USING(idimagen) |
|---|
| 94 | LEFT JOIN remotepc ON remotepc.id=ordenadores.idordenador |
|---|
| 95 | WHERE adm.idusuario = '$userid' |
|---|
| 96 | AND aulas.idcentro = '$ouid' AND aulas.idaula LIKE '$labid' AND aulas.inremotepc = 1 |
|---|
| 97 | AND imagenes.idimagen = '$imageid' AND imagenes.inremotepc = 1 |
|---|
| 98 | AND (remotepc.reserved < NOW() OR ISNULL(reserved)) |
|---|
| 99 | ORDER BY remotepc.reserved ASC LIMIT 1; |
|---|
| 100 | EOD; |
|---|
| 101 | $rs=new Recordset; |
|---|
| 102 | $rs->Comando=&$cmd; |
|---|
| 103 | if (!$rs->Abrir()) return(false); // Error opening recordset. |
|---|
| 104 | // Check if user is admin and client exists. |
|---|
| 105 | $rs->Primero(); |
|---|
| 106 | if (checkAdmin($rs->campos["idusuario"]) and checkParameter($rs->campos["idordenador"])) { |
|---|
| 107 | // Read query data. |
|---|
| 108 | $serverip = $rs->campos["ipserveradm"]; |
|---|
| 109 | $serverport = $rs->campos["portserveradm"]; |
|---|
| 110 | $clntid = $rs->campos["idordenador"]; |
|---|
| 111 | $clntname = $rs->campos["nombreordenador"]; |
|---|
| 112 | $clntip = $rs->campos["ip"]; |
|---|
| 113 | $clntmac = $rs->campos["mac"]; |
|---|
| 114 | $agentkey = $rs->campos["agentkey"]; |
|---|
| 115 | $disk = $rs->campos["numdisk"]; |
|---|
| 116 | $part = $rs->campos["numpar"]; |
|---|
| 117 | $labid = $rs->campos["idaula"]; |
|---|
| 118 | $ouid = $rs->campos["idcentro"]; |
|---|
| 119 | // Check client's status. |
|---|
| 120 | $ogagent[$clntip]['url'] = "https://$clntip:8000/opengnsys/status"; |
|---|
| 121 | if ($app->settings['debug']) |
|---|
| 122 | writeRemotepcLog($app->request()->getResourceUri(). ": OGAgent status, url=".$ogagent[$clntip]['url']."."); |
|---|
| 123 | $result = multiRequest($ogagent); |
|---|
| 124 | if (empty($result[$clntip]['data'])) { |
|---|
| 125 | // Client is off, send a boot command to ogAdmServer. |
|---|
| 126 | // TODO: if client is busy????? |
|---|
| 127 | $reqframe = "nfn=Arrancar\r". |
|---|
| 128 | "ido=$clntid\r". |
|---|
| 129 | "iph=$clntip\r". |
|---|
| 130 | "mac=$clntmac\r". |
|---|
| 131 | "mar=1\r"; |
|---|
| 132 | if ($app->settings['debug']) |
|---|
| 133 | writeRemotepcLog($app->request()->getResourceUri(). "Send Boot command to ogAdmClient, ido=$clntid,iph=$clntip,mac=$clntmac."); |
|---|
| 134 | sendCommand($serverip, $serverport, $reqframe, $values); |
|---|
| 135 | } else { |
|---|
| 136 | // Client is on, send a rieboot command to its OGAgent. |
|---|
| 137 | $ogagent[$clntip]['url'] = "https://$clntip:8000/opengnsys/reboot"; |
|---|
| 138 | $ogagent[$clntip]['header'] = Array("Authorization: ".$agentkey); |
|---|
| 139 | if ($app->settings['debug']) |
|---|
| 140 | writeRemotepcLog($app->request()->getResourceUri(). ": OGAgent reboot, url=".$ogagent[$clntip]['url']."."); |
|---|
| 141 | $result = multiRequest($ogagent); |
|---|
| 142 | // ... (check response) |
|---|
| 143 | //if ($result[$clntip]['code'] != 200) { |
|---|
| 144 | // ... |
|---|
| 145 | } |
|---|
| 146 | // DB Transaction: mark choosed client as reserved and |
|---|
| 147 | // create an init session command into client's actions queue. |
|---|
| 148 | $cmd->texto = "START TRANSACTION;"; |
|---|
| 149 | $cmd->Ejecutar(); |
|---|
| 150 | $timestamp = time(); |
|---|
| 151 | $cmd->texto = <<<EOD |
|---|
| 152 | INSERT INTO remotepc |
|---|
| 153 | SET id='$clntid', reserved=NOW() + INTERVAL $maxtime HOUR, urllogin=NULL, urllogout=NULL |
|---|
| 154 | ON DUPLICATE KEY UPDATE |
|---|
| 155 | id=VALUES(id), reserved=VALUES(reserved), |
|---|
| 156 | urllogin=VALUES(urllogin), urllogout=VALUES(urllogout); |
|---|
| 157 | EOD; |
|---|
| 158 | $t1 = $cmd->Ejecutar(); |
|---|
| 159 | $cmd->texto = <<<EOD |
|---|
| 160 | INSERT INTO acciones |
|---|
| 161 | SET tipoaccion=$EJECUCION_COMANDO, |
|---|
| 162 | idtipoaccion=9, |
|---|
| 163 | idcomando=9, |
|---|
| 164 | parametros='nfn=IniciarSesion\rdsk=$disk\rpar=$part', |
|---|
| 165 | descriaccion='RemotePC Session', |
|---|
| 166 | idordenador=$clntid, |
|---|
| 167 | ip='$clntip', |
|---|
| 168 | sesion=$timestamp, |
|---|
| 169 | fechahorareg=NOW(), |
|---|
| 170 | estado=$ACCION_INICIADA, |
|---|
| 171 | resultado=$ACCION_SINRESULTADO, |
|---|
| 172 | ambito=$AMBITO_ORDENADORES, |
|---|
| 173 | idambito=$clntid, |
|---|
| 174 | restrambito='$clntip', |
|---|
| 175 | idcentro=$ouid; |
|---|
| 176 | EOD; |
|---|
| 177 | $t2 = $cmd->Ejecutar(); |
|---|
| 178 | // Create event to remove reservation on timeout (15 min.). |
|---|
| 179 | $timeout = "15 MINUTE"; |
|---|
| 180 | $cmd->texto = <<<EOD |
|---|
| 181 | CREATE EVENT e_timeout_$clntid |
|---|
| 182 | ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL $timeout DO |
|---|
| 183 | BEGIN |
|---|
| 184 | SET @clntid = NULL; |
|---|
| 185 | UPDATE acciones |
|---|
| 186 | SET estado = $ACCION_FINALIZADA, resultado = $ACCION_FALLIDA, |
|---|
| 187 | descrinotificacion = 'Timeout' |
|---|
| 188 | WHERE descriaccion = 'RemotePC Session' AND estado = $ACCION_INICIADA |
|---|
| 189 | AND idordenador = (SELECT @clntid := '$clntid'); |
|---|
| 190 | IF @clntid IS NOT NULL THEN |
|---|
| 191 | UPDATE remotepc |
|---|
| 192 | SET reserved=NOW() - INTERVAL 1 SECOND, urllogin=NULL, urllogout=NULL |
|---|
| 193 | WHERE id = @clntid; |
|---|
| 194 | DELETE FROM acciones |
|---|
| 195 | WHERE idordenador = @clntid |
|---|
| 196 | AND descriaccion = 'RemotePC Session' |
|---|
| 197 | AND descrinotificacion = 'Timeout'; |
|---|
| 198 | END IF; |
|---|
| 199 | END |
|---|
| 200 | EOD; |
|---|
| 201 | $t3 = $cmd->Ejecutar(); |
|---|
| 202 | if ($t1 and $t2 and $t3) { |
|---|
| 203 | // Commit transaction on success. |
|---|
| 204 | $cmd->texto = "COMMIT;"; |
|---|
| 205 | $cmd->Ejecutar(); |
|---|
| 206 | if ($app->settings['debug']) |
|---|
| 207 | writeRemotepcLog($app->request()->getResourceUri(). ": DB tables and events updated, clntid=$clntid."); |
|---|
| 208 | // Send init session command if client is booted on ogLive. |
|---|
| 209 | $reqframe = "nfn=IniciarSesion\r". |
|---|
| 210 | "ido=$clntid\r". |
|---|
| 211 | "iph=$clntip\r". |
|---|
| 212 | "dsk=$disk\r". |
|---|
| 213 | "par=$part\r"; |
|---|
| 214 | if ($app->settings['debug']) |
|---|
| 215 | writeRemotepcLog($app->request()->getResourceUri(). ": Send Init Session command to ogAdmClient, ido=$clntid,iph=$clntip,dsk=$disk,par=$part."); |
|---|
| 216 | sendCommand($serverip, $serverport, $reqframe, $values); |
|---|
| 217 | // Compose JSON response. |
|---|
| 218 | $response['id'] = (int)$clntid; |
|---|
| 219 | $response['name'] = $clntname; |
|---|
| 220 | $response['ip'] = $clntip; |
|---|
| 221 | $response['mac'] = $clntmac; |
|---|
| 222 | $response['lab']['id'] = $labid; |
|---|
| 223 | $response['ou']['id'] = (int)$ouid; |
|---|
| 224 | if ($app->settings['debug']) |
|---|
| 225 | writeRemotepcLog($app->request()->getResourceUri(). ": Response, ".var_export($response,true)."."); |
|---|
| 226 | jsonResponse(200, $response); |
|---|
| 227 | } else { |
|---|
| 228 | // Roll-back transaction on DB error. |
|---|
| 229 | $cmd->texto = "ROLLBACK;"; |
|---|
| 230 | $cmd->Ejecutar(); |
|---|
| 231 | // Error message. |
|---|
| 232 | $response["message"] = "Database error: $t1, $t2, $t3"; |
|---|
| 233 | if ($app->settings['debug']) |
|---|
| 234 | writeRemotepcLog($app->request()->getResourceUri(). ": ERROR: ".$response["message"]."."); |
|---|
| 235 | jsonResponse(400, $response); |
|---|
| 236 | } |
|---|
| 237 | } else { |
|---|
| 238 | if ($app->settings['debug']) |
|---|
| 239 | writeRemotepcLog($app->request()->getResourceUri(). ": UNASSIGNED"); |
|---|
| 240 | } |
|---|
| 241 | $rs->Cerrar(); |
|---|
| 242 | $app->stop(); |
|---|
| 243 | } |
|---|
| 244 | ); |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | /** |
|---|
| 248 | * @brief Store UDS server URLs to resend some events recieved from OGAgent. |
|---|
| 249 | * @note Route: /ous/:ouid/labs/:labid/clients/:clntid/events, Method: POST |
|---|
| 250 | * @param string urlLogin URL to redirect login notification. |
|---|
| 251 | * @param string urlLogout URL to redirect logout notification. |
|---|
| 252 | * @warning Events parameters will be stored in a new "remotepc" table. |
|---|
| 253 | */ |
|---|
| 254 | $app->post('/ous/:ouid/labs/:labid/clients/:clntid/events', 'validateApiKey', |
|---|
| 255 | function($ouid, $labid, $clntid) use ($app) { |
|---|
| 256 | global $cmd; |
|---|
| 257 | global $userid; |
|---|
| 258 | $response = Array(); |
|---|
| 259 | |
|---|
| 260 | if ($app->settings['debug']) |
|---|
| 261 | writeRemotepcLog($app->request()->getResourceUri(). ": Init."); |
|---|
| 262 | // Checking parameters. |
|---|
| 263 | try { |
|---|
| 264 | if (empty(preg_match('/^python-requests\//', $_SERVER['HTTP_USER_AGENT']))) { |
|---|
| 265 | throw new Exception("Bad agent: sender=".$_SERVER['REMOTE_ADDR'].", agent=".$_SERVER['HTTP_USER_AGENT']); |
|---|
| 266 | } |
|---|
| 267 | if (!checkIds($ouid, $labid, $clntid)) { |
|---|
| 268 | throw new Exception("Ids. must be positive integers"); |
|---|
| 269 | } |
|---|
| 270 | // Reading JSON parameters. |
|---|
| 271 | $input = json_decode($app->request()->getBody()); |
|---|
| 272 | $urlLogin = htmlspecialchars($input->urlLogin); |
|---|
| 273 | $urlLogout = htmlspecialchars($input->urlLogout); |
|---|
| 274 | if (filter_var($urlLogin, FILTER_VALIDATE_URL) === false) { |
|---|
| 275 | throw new Exception("Must be a valid URL for login notification"); |
|---|
| 276 | } |
|---|
| 277 | if (filter_var($urlLogout, FILTER_VALIDATE_URL) === false) { |
|---|
| 278 | throw new Exception("Must be a valid URL for logout notification"); |
|---|
| 279 | } |
|---|
| 280 | } catch (Exception $e) { |
|---|
| 281 | // Error message. |
|---|
| 282 | $response["message"] = $e->getMessage(); |
|---|
| 283 | if ($app->settings['debug']) |
|---|
| 284 | writeRemotepcLog($app->request()->getResourceUri(). ": ERROR: ".$response["message"]."."); |
|---|
| 285 | jsonResponse(400, $response); |
|---|
| 286 | $app->stop(); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | if ($app->settings['debug']) |
|---|
| 290 | writeRemotepcLog($app->request()->getResourceUri(). ": Parameters: urlLogin=$urlLogin, urlLogout=$urlLogout"); |
|---|
| 291 | // Select client data for UDS compatibility. |
|---|
| 292 | $cmd->texto = <<<EOD |
|---|
| 293 | SELECT adm.idusuario, ordenadores.idordenador, remotepc.* |
|---|
| 294 | FROM remotepc |
|---|
| 295 | RIGHT JOIN ordenadores ON remotepc.id=ordenadores.idordenador |
|---|
| 296 | JOIN aulas USING(idaula) |
|---|
| 297 | RIGHT JOIN administradores_centros AS adm USING(idcentro) |
|---|
| 298 | RIGHT JOIN usuarios USING(idusuario) |
|---|
| 299 | WHERE adm.idusuario = '$userid' |
|---|
| 300 | AND idcentro = '$ouid' AND aulas.idaula ='$labid' |
|---|
| 301 | AND ordenadores.idordenador = '$clntid'; |
|---|
| 302 | EOD; |
|---|
| 303 | $rs=new Recordset; |
|---|
| 304 | $rs->Comando=&$cmd; |
|---|
| 305 | if (!$rs->Abrir()) return(false); // Error opening recordset. |
|---|
| 306 | // Check if user is admin and client exists. |
|---|
| 307 | $rs->Primero(); |
|---|
| 308 | if (checkAdmin($rs->campos["idusuario"]) and checkParameter($rs->campos["idordenador"])) { |
|---|
| 309 | // Check if client is reserved. |
|---|
| 310 | if (! is_null($rs->campos["reserved"])) { |
|---|
| 311 | // Updating DB if client is reserved. |
|---|
| 312 | $cmd->CreaParametro("@urllogin", $urlLogin, 0); |
|---|
| 313 | $cmd->CreaParametro("@urllogout", $urlLogout, 0); |
|---|
| 314 | $cmd->texto = <<<EOD |
|---|
| 315 | UPDATE remotepc |
|---|
| 316 | SET urllogin=@urllogin, urllogout=@urllogout |
|---|
| 317 | WHERE id='$clntid'; |
|---|
| 318 | EOD; |
|---|
| 319 | if ($cmd->Ejecutar()) { |
|---|
| 320 | // Confirm operation. |
|---|
| 321 | $response = ""; |
|---|
| 322 | jsonResponse(200, $response); |
|---|
| 323 | } else { |
|---|
| 324 | // Error message. |
|---|
| 325 | $response["message"] = "Database error"; |
|---|
| 326 | jsonResponse(400, $response); |
|---|
| 327 | } |
|---|
| 328 | } else { |
|---|
| 329 | // Error message. |
|---|
| 330 | $response["message"] = "Client is not reserved"; |
|---|
| 331 | jsonResponse(400, $response); |
|---|
| 332 | } |
|---|
| 333 | } |
|---|
| 334 | $rs->Cerrar(); |
|---|
| 335 | $app->stop(); |
|---|
| 336 | } |
|---|
| 337 | ); |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | /* |
|---|
| 341 | * @brief Store session time (in sec). |
|---|
| 342 | * @note Route: /ous/:ouid/labs/:labid/clients/:clntid/session, Method: POST |
|---|
| 343 | * @param int deadLine maximum session time, in seconds (0 for unlimited) |
|---|
| 344 | * @warning Parameters will be stored in a new "remotepc" table. |
|---|
| 345 | */ |
|---|
| 346 | $app->post('/ous/:ouid/labs/:labid/clients/:clntid/session', 'validateApiKey', |
|---|
| 347 | function($ouid, $labid, $clntid) use ($app) { |
|---|
| 348 | global $cmd; |
|---|
| 349 | global $userid; |
|---|
| 350 | $response = Array(); |
|---|
| 351 | |
|---|
| 352 | if ($app->settings['debug']) |
|---|
| 353 | writeRemotepcLog($app->request()->getResourceUri(). ": Init."); |
|---|
| 354 | // Checking parameters. |
|---|
| 355 | try { |
|---|
| 356 | if (empty(preg_match('/^python-requests\//', $_SERVER['HTTP_USER_AGENT']))) { |
|---|
| 357 | throw new Exception("Bad agent: sender=".$_SERVER['REMOTE_ADDR'].", agent=".$_SERVER['HTTP_USER_AGENT']); |
|---|
| 358 | } |
|---|
| 359 | if (!checkIds($ouid, $labid, $clntid)) { |
|---|
| 360 | throw new Exception("Ids. must be positive integers"); |
|---|
| 361 | } |
|---|
| 362 | // Reading JSON parameters. |
|---|
| 363 | $input = json_decode($app->request()->getBody()); |
|---|
| 364 | $deadLine = $input->deadLine; |
|---|
| 365 | if (filter_var($deadLine, FILTER_VALIDATE_INT) === false) { |
|---|
| 366 | throw new Exception("Deadline must be integer"); |
|---|
| 367 | } |
|---|
| 368 | if ($deadLine < 0) { |
|---|
| 369 | throw new Exception("Resource unavailable"); |
|---|
| 370 | } |
|---|
| 371 | } catch (Exception $e) { |
|---|
| 372 | // Error message. |
|---|
| 373 | $response["message"] = $e->getMessage(); |
|---|
| 374 | if ($app->settings['debug']) |
|---|
| 375 | writeRemotepcLog($app->request()->getResourceUri(). ": ERROR: ".$response["message"]."."); |
|---|
| 376 | jsonResponse(400, $response); |
|---|
| 377 | $app->stop(); |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | if ($app->settings['debug']) |
|---|
| 381 | writeRemotepcLog($app->request()->getResourceUri(). ": Parameters: deadLine=$deadLine"); |
|---|
| 382 | // Get client's data. |
|---|
| 383 | $cmd->texto = <<<EOD |
|---|
| 384 | SELECT adm.idusuario, ordenadores.idordenador, remotepc.* |
|---|
| 385 | FROM remotepc |
|---|
| 386 | RIGHT JOIN ordenadores ON remotepc.id=ordenadores.idordenador |
|---|
| 387 | JOIN aulas USING(idaula) |
|---|
| 388 | RIGHT JOIN administradores_centros AS adm USING(idcentro) |
|---|
| 389 | WHERE adm.idusuario = '$userid' |
|---|
| 390 | AND aulas.idcentro = '$ouid' AND aulas.idaula = '$labid' |
|---|
| 391 | AND ordenadores.idordenador = '$clntid'; |
|---|
| 392 | EOD; |
|---|
| 393 | $rs=new Recordset; |
|---|
| 394 | $rs->Comando=&$cmd; |
|---|
| 395 | if (!$rs->Abrir()) return(false); // Error opening recordset. |
|---|
| 396 | // Check if user is admin and client exists. |
|---|
| 397 | $rs->Primero(); |
|---|
| 398 | if (checkAdmin($rs->campos["idusuario"]) and checkParameter($rs->campos["idordenador"])) { |
|---|
| 399 | // Check if client is reserved. |
|---|
| 400 | if (! is_null($rs->campos["urllogin"])) { |
|---|
| 401 | // Read query data. |
|---|
| 402 | $clntid = $rs->campos["idordenador"]; |
|---|
| 403 | # Removing previous commands from OGAgent operations queue. |
|---|
| 404 | if ($app->settings['debug']) |
|---|
| 405 | writeRemotepcLog($app->request()->getResourceUri(). ": Updating database."); |
|---|
| 406 | $cmd->texto = <<<EOD |
|---|
| 407 | DELETE FROM ogagent_queue |
|---|
| 408 | WHERE clientid = '$clntid' AND operation IN ('popup-10', 'popup-5', 'poweroff'); |
|---|
| 409 | EOD; |
|---|
| 410 | $cmd->Ejecutar(); |
|---|
| 411 | # Add new commands to OGAgent operations queue. |
|---|
| 412 | $cmd->texto = "INSERT INTO ogagent_queue (clientid, exectime, operation) VALUES"; |
|---|
| 413 | if ($deadLine > 600) { |
|---|
| 414 | # Add reminder 10 min. before deadline. |
|---|
| 415 | $cmd->texto .= " ($clntid, NOW() + INTERVAL $deadLine SECOND - INTERVAL 10 MINUTE, 'popup-10'),"; |
|---|
| 416 | } |
|---|
| 417 | if ($deadLine > 300) { |
|---|
| 418 | # Add reminder 5 min. before deadline. |
|---|
| 419 | $cmd->texto .= " ($clntid, NOW() + INTERVAL $deadLine SECOND - INTERVAL 5 MINUTE, 'popup-5'),"; |
|---|
| 420 | } |
|---|
| 421 | # Add power off command at deadline time. |
|---|
| 422 | $cmd->texto .= " ($clntid, NOW() + INTERVAL $deadLine SECOND, 'poweroff');"; |
|---|
| 423 | if ($deadLine == 0 or $cmd->Ejecutar()) { |
|---|
| 424 | // Confirm operation. |
|---|
| 425 | $cmd->texto = ""; |
|---|
| 426 | $response = ""; |
|---|
| 427 | jsonResponse(200, $response); |
|---|
| 428 | } else { |
|---|
| 429 | // Error message. |
|---|
| 430 | $response["message"] = "Database error"; |
|---|
| 431 | jsonResponse(400, $response); |
|---|
| 432 | } |
|---|
| 433 | } else { |
|---|
| 434 | // Error message. |
|---|
| 435 | $response["message"] = "Client is not reserved"; |
|---|
| 436 | jsonResponse(400, $response); |
|---|
| 437 | } |
|---|
| 438 | } else { |
|---|
| 439 | // Error message. |
|---|
| 440 | $response["message"] = "Client does not exist"; |
|---|
| 441 | jsonResponse(404, $response); |
|---|
| 442 | } |
|---|
| 443 | $rs->Cerrar(); |
|---|
| 444 | } |
|---|
| 445 | ); |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | /** |
|---|
| 449 | * @brief Store UDS server URLs to resend some events recieved from OGAgent. |
|---|
| 450 | * @brief Unreserve a client and send a poweroff operation. |
|---|
| 451 | * @note Route: /ous/:ouid/labs/:labid/clients/:clntid/unreserve, Method: DELETE |
|---|
| 452 | */ |
|---|
| 453 | $app->delete('/ous/:ouid/labs/:labid/clients/:clntid/unreserve', 'validateApiKey', |
|---|
| 454 | function($ouid, $labid, $clntid) use ($app) { |
|---|
| 455 | global $cmd; |
|---|
| 456 | global $userid; |
|---|
| 457 | global $ACCION_INICIADA; |
|---|
| 458 | $response = Array(); |
|---|
| 459 | $ogagent = Array(); |
|---|
| 460 | |
|---|
| 461 | if ($app->settings['debug']) |
|---|
| 462 | writeRemotepcLog($app->request()->getResourceUri(). ": Init."); |
|---|
| 463 | // Checking parameters. |
|---|
| 464 | try { |
|---|
| 465 | if (empty(preg_match('/^python-requests\//', $_SERVER['HTTP_USER_AGENT']))) { |
|---|
| 466 | throw new Exception("Bad agent: sender=".$_SERVER['REMOTE_ADDR'].", agent=".$_SERVER['HTTP_USER_AGENT']); |
|---|
| 467 | } |
|---|
| 468 | if (!checkIds($ouid, $labid, $clntid)) { |
|---|
| 469 | throw new Exception("Ids. must be positive integers"); |
|---|
| 470 | } |
|---|
| 471 | } catch (Exception $e) { |
|---|
| 472 | // Error message. |
|---|
| 473 | $response["message"] = $e->getMessage(); |
|---|
| 474 | if ($app->settings['debug']) |
|---|
| 475 | writeRemotepcLog($app->request()->getResourceUri(). ": ERROR: ".$response["message"]."."); |
|---|
| 476 | jsonResponse(400, $response); |
|---|
| 477 | $app->stop(); |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | // Select client data for UDS compatibility. |
|---|
| 481 | $cmd->texto = <<<EOD |
|---|
| 482 | SELECT adm.idusuario, ordenadores.idordenador, ordenadores.ip, ordenadores.agentkey, remotepc.reserved |
|---|
| 483 | FROM remotepc |
|---|
| 484 | RIGHT JOIN ordenadores ON remotepc.id=ordenadores.idordenador |
|---|
| 485 | JOIN aulas USING(idaula) |
|---|
| 486 | RIGHT JOIN administradores_centros AS adm USING(idcentro) |
|---|
| 487 | RIGHT JOIN usuarios USING(idusuario) |
|---|
| 488 | WHERE adm.idusuario = '$userid' |
|---|
| 489 | AND idcentro = '$ouid' AND aulas.idaula ='$labid' |
|---|
| 490 | AND ordenadores.idordenador = '$clntid'; |
|---|
| 491 | EOD; |
|---|
| 492 | $rs=new Recordset; |
|---|
| 493 | $rs->Comando=&$cmd; |
|---|
| 494 | if (!$rs->Abrir()) return(false); // Error opening recordset. |
|---|
| 495 | // Check if user is admin and client exists. |
|---|
| 496 | $rs->Primero(); |
|---|
| 497 | if (checkAdmin($rs->campos["idusuario"]) and checkParameter($rs->campos["idordenador"])) { |
|---|
| 498 | // Check if client is reserved. |
|---|
| 499 | if (! is_null($rs->campos["reserved"])) { |
|---|
| 500 | // Read query data. |
|---|
| 501 | $clntip = $rs->campos["ip"]; |
|---|
| 502 | $agentkey = $rs->campos["agentkey"]; |
|---|
| 503 | // DB Transaction: set reservation time to the past and |
|---|
| 504 | // remove pending boot commands from client's and agent's queues. |
|---|
| 505 | if ($app->settings['debug']) |
|---|
| 506 | writeRemotepcLog($app->request()->getResourceUri(). ": Updating database."); |
|---|
| 507 | $cmd->texto = "START TRANSACTION;"; |
|---|
| 508 | $cmd->Ejecutar(); |
|---|
| 509 | $cmd->texto = <<<EOD |
|---|
| 510 | UPDATE remotepc |
|---|
| 511 | SET reserved=NOW() - INTERVAL 1 SECOND, urllogin=NULL, urllogout=NULL |
|---|
| 512 | WHERE id='$clntid'; |
|---|
| 513 | EOD; |
|---|
| 514 | $cmd->Ejecutar(); |
|---|
| 515 | $cmd->texto = <<<EOD |
|---|
| 516 | DELETE FROM acciones |
|---|
| 517 | WHERE idordenador = '$clntid' |
|---|
| 518 | AND descriaccion = 'RemotePC Session'; |
|---|
| 519 | EOD; |
|---|
| 520 | $cmd->Ejecutar(); |
|---|
| 521 | $cmd->texto = <<<EOD |
|---|
| 522 | DELETE FROM ogagent_queue |
|---|
| 523 | WHERE clientid = '$clntid' AND command IN ('popup-10', 'popup-5', 'poweroff'); |
|---|
| 524 | EOD; |
|---|
| 525 | $cmd->Ejecutar(); |
|---|
| 526 | $cmd->texto = "COMMIT;"; |
|---|
| 527 | $cmd->Ejecutar(); |
|---|
| 528 | // Send a poweroff command to client's OGAgent. |
|---|
| 529 | $ogagent[$clntip]['url'] = "https://$clntip:8000/opengnsys/poweroff"; |
|---|
| 530 | $ogagent[$clntip]['header'] = Array("Authorization: ".$agentkey); |
|---|
| 531 | if ($app->settings['debug']) |
|---|
| 532 | writeRemotepcLog($app->request()->getResourceUri(). ": OGAgent poweroff, url=".$ogagent[$clntip]['url']."."); |
|---|
| 533 | $result = multiRequest($ogagent); |
|---|
| 534 | // ... (check response) |
|---|
| 535 | //if ($result[$clntip]['code'] != 200) { |
|---|
| 536 | // ... |
|---|
| 537 | // Confirm operation. |
|---|
| 538 | $response = ""; |
|---|
| 539 | jsonResponse(200, $response); |
|---|
| 540 | } else { |
|---|
| 541 | // Error message. |
|---|
| 542 | $response["message"] = "Client is not reserved"; |
|---|
| 543 | jsonResponse(400, $response); |
|---|
| 544 | } |
|---|
| 545 | } else { |
|---|
| 546 | // Error message. |
|---|
| 547 | $response["message"] = "Client does not exist"; |
|---|
| 548 | jsonResponse(404, $response); |
|---|
| 549 | } |
|---|
| 550 | $rs->Cerrar(); |
|---|
| 551 | } |
|---|
| 552 | ); |
|---|
| 553 | |
|---|
| 554 | |
|---|