[7829e4e] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * @file index.php |
---|
| 4 | * @brief OpenGnsys REST API manager. |
---|
| 5 | * @note Some ideas are based on article "How to create REST API for Android app using PHP, Slim and MySQL" by Ravi Tamada, thanx. |
---|
| 6 | * @license GNU GPLv3+ |
---|
| 7 | * @author Ramón M. Gómez, ETSII Univ. Sevilla |
---|
| 8 | * @version 1.1 |
---|
| 9 | * @date 2015-04-16 |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | // Inclussion files. |
---|
| 13 | |
---|
| 14 | // Server access data. |
---|
| 15 | include_once("../controlacceso.php"); |
---|
| 16 | include_once("../clases/AdoPhp.php"); |
---|
| 17 | include_once("../includes/CreaComando.php"); |
---|
| 18 | // Connection class. |
---|
[c03c7c3] | 19 | @include_once("../includes/constantes.php"); |
---|
[7829e4e] | 20 | include_once("../includes/comunes.php"); |
---|
| 21 | include_once("../clases/SockHidra.php"); |
---|
| 22 | |
---|
| 23 | // Slim framework. |
---|
| 24 | include_once("Slim/Slim.php"); |
---|
| 25 | \Slim\Slim::registerAutoloader(); |
---|
| 26 | |
---|
| 27 | // Server access control. |
---|
| 28 | $cmd = CreaComando($cnx); |
---|
| 29 | if (!$cmd) |
---|
| 30 | die("Access Error"); |
---|
| 31 | |
---|
| 32 | // Install Slim application (development mode). |
---|
| 33 | //$app = new \Slim\Slim(array('mode' => 'production', 'debug' => false)); |
---|
| 34 | $app = new \Slim\Slim(array( |
---|
| 35 | 'mode' => 'development', |
---|
| 36 | 'debug' => true)); |
---|
| 37 | $app->setName('opengnsys'); |
---|
| 38 | |
---|
| 39 | // Global variables. |
---|
[4454169b] | 40 | $userid = NULL; // User id. with access to REST API. |
---|
[7829e4e] | 41 | |
---|
| 42 | |
---|
| 43 | // Auxiliar functions. |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * @brief Compose JSON response. |
---|
| 47 | * @param int status Status code for HTTP response. |
---|
| 48 | * @param array response Response data. |
---|
| 49 | * @return string JSON response. |
---|
| 50 | */ |
---|
| 51 | function jsonResponse($status, $response) { |
---|
| 52 | $app = \Slim\Slim::getInstance(); |
---|
[4454169b] | 53 | // HTTP status code. |
---|
[7829e4e] | 54 | $app->status($status); |
---|
[4454169b] | 55 | // Content-type HTTP header. |
---|
[7829e4e] | 56 | $app->contentType('application/json'); |
---|
[4454169b] | 57 | // JSON response. |
---|
[7829e4e] | 58 | echo json_encode($response); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * @brief Validate API key included in "Authorization" HTTP header. |
---|
| 63 | * @return JSON response on error. |
---|
| 64 | */ |
---|
| 65 | function validateApiKey() { |
---|
| 66 | global $cmd; |
---|
| 67 | global $userid; |
---|
| 68 | $response = array(); |
---|
| 69 | |
---|
| 70 | // Read Authorization HTTP header. |
---|
| 71 | $headers = apache_request_headers(); |
---|
| 72 | if (! empty($headers['Authorization'])) { |
---|
| 73 | // Assign user id. that match this key to global variable. |
---|
| 74 | $apikey = htmlspecialchars($headers['Authorization']); |
---|
| 75 | $cmd->texto = "SELECT idusuario |
---|
| 76 | FROM usuarios |
---|
| 77 | WHERE apikey='$apikey'"; |
---|
| 78 | $rs=new Recordset; |
---|
| 79 | $rs->Comando=&$cmd; |
---|
| 80 | if ($rs->Abrir()) { |
---|
| 81 | $rs->Primero(); |
---|
| 82 | if (!$rs->EOF){ |
---|
| 83 | // Fetch user id. |
---|
| 84 | $userid = $rs->campos["idusuario"]; |
---|
| 85 | } else { |
---|
| 86 | // Credentials error. |
---|
| 87 | $response['error'] = true; |
---|
| 88 | $response['message'] = 'Login failed. Incorrect credentials'; |
---|
| 89 | jsonResponse(401, $response); |
---|
| 90 | $app->stop(); |
---|
| 91 | } |
---|
| 92 | $rs->Cerrar(); |
---|
| 93 | } else { |
---|
| 94 | // Access error. |
---|
| 95 | $response['error'] = true; |
---|
| 96 | $response['message'] = "An error occurred, please try again"; |
---|
| 97 | jsonResponse(500, $response); |
---|
| 98 | } |
---|
| 99 | } else { |
---|
[4454169b] | 100 | // Error: missing API key. |
---|
[7829e4e] | 101 | $response['error'] = true; |
---|
| 102 | $response['message'] = 'Missing API key'; |
---|
| 103 | jsonResponse(400, $response); |
---|
| 104 | $app->stop(); |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * @brief Check if parameter is set and print error messages if empty. |
---|
| 110 | * @param string param Parameter to check. |
---|
| 111 | * @return boolean "false" if parameter is null, otherwise "true". |
---|
| 112 | */ |
---|
| 113 | function checkParameter($param) { |
---|
| 114 | if (isset($param)) { |
---|
| 115 | return true; |
---|
| 116 | } else { |
---|
| 117 | // Print error message. |
---|
| 118 | $response['error'] = true; |
---|
| 119 | $response['message'] = 'Parameter not found'; |
---|
| 120 | jsonResponse(400, $response); |
---|
| 121 | return false; |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * @brief Check if user is administrator and print error messages if not. |
---|
| 127 | * @param int adminid Administrator id. |
---|
| 128 | * @return boolean "true" if admin id. is equals to global user id., otherwise "false". |
---|
| 129 | */ |
---|
| 130 | function checkAdmin($adminid) { |
---|
| 131 | global $userid; |
---|
| 132 | |
---|
| 133 | if ($adminid == $userid) { |
---|
| 134 | return true; |
---|
| 135 | } else { |
---|
| 136 | // Print error message. |
---|
| 137 | $response['error'] = true; |
---|
| 138 | $response['message'] = 'Cannot access this resource'; |
---|
| 139 | jsonResponse(401, $response); |
---|
| 140 | return false; |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | /** |
---|
| 146 | * @brief Send a command to OpenGnsys Server and get request. |
---|
| 147 | * @param string serverip Server IP address. |
---|
| 148 | * @param string serverport Server port. |
---|
| 149 | * @param string reqframe Request frame (field's separator is "\r"). |
---|
| 150 | * @param array values Response values (out parameter). |
---|
| 151 | * @return boolean "true" if success, otherwise "false". |
---|
| 152 | */ |
---|
| 153 | function sendCommand($serverip, $serverport, $reqframe, &$values) { |
---|
| 154 | global $LONCABECERA; |
---|
| 155 | global $LONHEXPRM; |
---|
| 156 | |
---|
| 157 | // Connect to server. |
---|
| 158 | $respvalues = ""; |
---|
| 159 | $connect = new SockHidra($serverip, $serverport); |
---|
| 160 | if ($connect->conectar()) { |
---|
| 161 | // Send request frame to server. |
---|
| 162 | $result = $connect->envia_peticion($reqframe); |
---|
| 163 | if ($result) { |
---|
| 164 | // Parse request frame. |
---|
| 165 | $respframe = $connect->recibe_respuesta(); |
---|
| 166 | $connect->desconectar(); |
---|
| 167 | $paramlen = hexdec(substr($respframe, $LONCABECERA, $LONHEXPRM)); |
---|
| 168 | $params = substr($respframe, $LONCABECERA+$LONHEXPRM, $paramlen); |
---|
| 169 | // Fetch values and return result. |
---|
| 170 | $values = extrae_parametros($params, "\r", '='); |
---|
| 171 | return ($values); |
---|
| 172 | } else { |
---|
| 173 | // Return with error. |
---|
| 174 | return (false); |
---|
| 175 | } |
---|
| 176 | } else { |
---|
| 177 | // Return with error. |
---|
| 178 | return (false); |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | // Define JSON routes. |
---|
| 183 | |
---|
| 184 | /** |
---|
| 185 | * @brief user login. |
---|
| 186 | * URL: /login |
---|
| 187 | * Método: POST |
---|
| 188 | * @param string username User name. |
---|
| 189 | * @param string password User password. |
---|
| 190 | * @return string JSON response with user id. and API key. |
---|
| 191 | * @note User's API key is stored in a new field of "usuarios" table. |
---|
| 192 | */ |
---|
| 193 | $app->post('/login', |
---|
| 194 | function() use ($app) { |
---|
| 195 | global $cmd; |
---|
| 196 | global $userid; |
---|
| 197 | |
---|
| 198 | // Reading POST parameters. |
---|
| 199 | $user = htmlspecialchars($app->request()->post('username')); |
---|
| 200 | $pass = htmlspecialchars($app->request()->post('password')); |
---|
| 201 | $response = array(); |
---|
| 202 | |
---|
| 203 | if (! empty($user) and ! empty($pass)) { |
---|
| 204 | // Database query. |
---|
| 205 | $cmd->texto = "SELECT idusuario, apikey |
---|
| 206 | FROM usuarios |
---|
| 207 | WHERE usuario='$user' AND pasguor='$pass'"; |
---|
| 208 | $rs=new Recordset; |
---|
| 209 | $rs->Comando=&$cmd; |
---|
| 210 | if ($rs->Abrir()) { |
---|
| 211 | $rs->Primero(); |
---|
| 212 | if (!$rs->EOF){ |
---|
| 213 | // JSON response. |
---|
| 214 | $userid=$rs->campos["idusuario"]; |
---|
| 215 | $apikey=$rs->campos["apikey"]; |
---|
| 216 | $response["error"] = false; |
---|
| 217 | $response['userid'] = $userid; |
---|
| 218 | $response['apikey'] = $apikey; |
---|
| 219 | jsonResponse(200, $response); |
---|
| 220 | } else { |
---|
| 221 | // Credentials error. |
---|
| 222 | $response['error'] = true; |
---|
| 223 | $response['message'] = 'Login failed. Incorrect credentials'; |
---|
| 224 | jsonResponse(401, $response); |
---|
| 225 | $app->stop(); |
---|
| 226 | } |
---|
| 227 | $rs->Cerrar(); |
---|
| 228 | } else { |
---|
| 229 | // Access error. |
---|
| 230 | $response['error'] = true; |
---|
| 231 | $response['message'] = "An error occurred. Please try again"; |
---|
| 232 | jsonResponse(500, $response); |
---|
| 233 | $app->stop(); |
---|
| 234 | } |
---|
| 235 | } else { |
---|
| 236 | # Error: missing some input parameter. |
---|
| 237 | $response['error'] = true; |
---|
| 238 | $response['message'] = 'Missing username or password'; |
---|
| 239 | jsonResponse(400, $response); |
---|
| 240 | $app->stop(); |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | ); |
---|
| 244 | |
---|
| 245 | /** |
---|
| 246 | * Listar todas las Unidades Organizativas definidas. |
---|
| 247 | * URL: /ous |
---|
| 248 | * Método: GET |
---|
| 249 | * Parámetros: no |
---|
| 250 | * Devuelve: array JSON con ouid y ouname de las Unidades Organizativas definidas. |
---|
| 251 | */ |
---|
| 252 | $app->get('/ous', 'validateApiKey', function() { |
---|
| 253 | global $cmd; |
---|
| 254 | |
---|
| 255 | $cmd->texto = "SELECT * FROM centros"; |
---|
| 256 | $rs=new Recordset; |
---|
| 257 | $rs->Comando=&$cmd; |
---|
| 258 | if (!$rs->Abrir()) return(false); // Error al abrir recordset |
---|
| 259 | $response['error'] = false; |
---|
| 260 | $response['ous'] = array(); |
---|
| 261 | $rs->Primero(); |
---|
| 262 | while (!$rs->EOF) { |
---|
| 263 | $tmp = array(); |
---|
| 264 | $tmp['ouid'] = $rs->campos["idcentro"]; |
---|
| 265 | $tmp['ouname'] = $rs->campos["nombrecentro"]; |
---|
| 266 | array_push($response['ous'], $tmp); |
---|
| 267 | $rs->Siguiente(); |
---|
| 268 | } |
---|
| 269 | $rs->Cerrar(); |
---|
| 270 | jsonResponse(200, $response); |
---|
| 271 | } |
---|
| 272 | ); |
---|
| 273 | |
---|
| 274 | /** |
---|
| 275 | * Obtener datos de una Unidad Organizativa. |
---|
| 276 | * URL: /ous/id (id. de la UO) |
---|
| 277 | * Método: GET |
---|
| 278 | * Devuelve: ouid, ouname y description |
---|
| 279 | */ |
---|
| 280 | $app->get('/ous/:ouid', 'validateApiKey', |
---|
| 281 | function($ouid) { |
---|
| 282 | global $cmd; |
---|
| 283 | |
---|
| 284 | $ouid = htmlspecialchars($ouid); |
---|
| 285 | $cmd->texto = "SELECT * FROM centros WHERE idcentro='$ouid';"; |
---|
| 286 | $rs=new Recordset; |
---|
| 287 | $rs->Comando=&$cmd; |
---|
| 288 | if (!$rs->Abrir()) return(false); // Error al abrir recordset |
---|
| 289 | $rs->Primero(); |
---|
| 290 | if (checkParameter($rs->campos["nombrecentro"])) { |
---|
| 291 | $response['error'] = false; |
---|
| 292 | $response['ouid'] = $ouid; |
---|
| 293 | $response['ouname'] = $rs->campos["nombrecentro"]; |
---|
| 294 | $response['description'] = $rs->campos["comentarios"]; |
---|
| 295 | jsonResponse(200, $response); |
---|
| 296 | } |
---|
| 297 | $rs->Cerrar(); |
---|
| 298 | } |
---|
| 299 | ); |
---|
| 300 | |
---|
| 301 | // Listar aulas de una OU. |
---|
[8053aff] | 302 | $app->get('/ous/:ouid/labs', 'validateApiKey', |
---|
[7829e4e] | 303 | function($ouid) { |
---|
| 304 | global $cmd; |
---|
| 305 | |
---|
| 306 | $ouid = htmlspecialchars($ouid); |
---|
| 307 | // Listar las salas de la UO si el usuario de la apikey es su admin. |
---|
| 308 | $cmd->texto = <<<EOD |
---|
| 309 | SELECT aulas.*, adm.idadministradorcentro |
---|
| 310 | FROM aulas |
---|
| 311 | RIGHT JOIN administradores_centros AS adm USING(idcentro) |
---|
| 312 | RIGHT JOIN usuarios USING(idusuario) |
---|
| 313 | WHERE idcentro='$ouid'; |
---|
| 314 | EOD; |
---|
| 315 | $rs=new Recordset; |
---|
| 316 | $rs->Comando=&$cmd; |
---|
| 317 | if (!$rs->Abrir()) return(false); // Error al abrir recordset |
---|
| 318 | // Comprobar que exista la UO y que el usuario sea su administrador. |
---|
| 319 | $rs->Primero(); |
---|
| 320 | if (checkParameter($rs->campos["idcentro"]) and checkAdmin($rs->campos["idadministradorcentro"])) { |
---|
| 321 | $response['error'] = false; |
---|
| 322 | $response['ouid'] = $ouid; |
---|
[8053aff] | 323 | $response['labs'] = array(); |
---|
[7829e4e] | 324 | while (!$rs->EOF) { |
---|
| 325 | $tmp = array(); |
---|
[8053aff] | 326 | $tmp['labid'] = $rs->campos["idaula"]; |
---|
| 327 | $tmp['labname'] = $rs->campos["nombreaula"]; |
---|
[7829e4e] | 328 | $tmp['inremotepc'] = $rs->campos["inremotepc"]==0 ? false: true; |
---|
[8053aff] | 329 | array_push($response['labs'], $tmp); |
---|
[7829e4e] | 330 | $rs->Siguiente(); |
---|
| 331 | } |
---|
| 332 | jsonResponse(200, $response); |
---|
| 333 | } |
---|
| 334 | $rs->Cerrar(); |
---|
| 335 | } |
---|
| 336 | ); |
---|
| 337 | |
---|
| 338 | // Obtener datos de un aula. |
---|
[8053aff] | 339 | // Alternativa: $app->get('/lab/:labid', 'validateApiKey', |
---|
| 340 | // function($labid) { |
---|
| 341 | $app->get('/ous/:ouid/labs/:labid', 'validateApiKey', |
---|
| 342 | function($ouid, $labid) { |
---|
[7829e4e] | 343 | global $cmd; |
---|
| 344 | |
---|
| 345 | $ouid = htmlspecialchars($ouid); |
---|
[8053aff] | 346 | $labid = htmlspecialchars($labid); |
---|
[7829e4e] | 347 | $cmd->texto = <<<EOD |
---|
| 348 | SELECT COUNT(idordenador) AS defclients, aulas.*, adm.idadministradorcentro |
---|
| 349 | FROM aulas |
---|
| 350 | RIGHT JOIN administradores_centros AS adm USING(idcentro) |
---|
| 351 | RIGHT JOIN usuarios USING(idusuario) |
---|
| 352 | LEFT JOIN ordenadores USING(idaula) |
---|
| 353 | WHERE idcentro='$ouid' |
---|
[8053aff] | 354 | AND idaula='$labid'; |
---|
[7829e4e] | 355 | EOD; |
---|
| 356 | $rs=new Recordset; |
---|
| 357 | $rs->Comando=&$cmd; |
---|
| 358 | if (!$rs->Abrir()) return(false); // Error al abrir recordset |
---|
| 359 | $rs->Primero(); |
---|
| 360 | if (checkParameter($rs->campos["idaula"]) and checkAdmin($rs->campos["idadministradorcentro"])) { |
---|
| 361 | $response['error'] = false; |
---|
[8053aff] | 362 | $response['labid'] = $rs->campos["idaula"]; |
---|
| 363 | $response['labname'] = $rs->campos["nombreaula"]; |
---|
[7829e4e] | 364 | $response['description'] = $rs->campos["comentarios"]; |
---|
| 365 | $response['inremotepc'] = $rs->campos["inremotepc"]==0 ? false: true; |
---|
| 366 | $response['maxclients'] = $rs->campos["puestos"]; |
---|
| 367 | $response['defclients'] = $rs->campos["defclients"]; |
---|
| 368 | $response['projector'] = $rs->campos["cagnon"]==0 ? false: true; |
---|
| 369 | $response['board'] = $rs->campos["pizarra"]==0 ? false: true; |
---|
| 370 | $response['routerip'] = $rs->campos["router"]; |
---|
| 371 | $response['netmask'] = $rs->campos["netmask"]; |
---|
[8053aff] | 372 | $response['ntp'] = $rs->campos["ntp"]; |
---|
[7829e4e] | 373 | $response['dns'] = $rs->campos["dns"]; |
---|
| 374 | $response['proxyurl'] = $rs->campos["proxy"]; |
---|
| 375 | switch ($rs->campos["modomul"]) { |
---|
| 376 | case 1: $response['mcastmode'] = "half-duplex"; break; |
---|
| 377 | case 2: $response['mcastmode'] = "full-duplex"; break; |
---|
| 378 | default: $response['mcastmode'] = $rs->campos["modomul"]; |
---|
| 379 | } |
---|
| 380 | $response['mcastip'] = $rs->campos["ipmul"]; |
---|
| 381 | $response['mcastport'] = $rs->campos["pormul"]; |
---|
| 382 | $response['mcastspeed'] = $rs->campos["velmul"]; |
---|
| 383 | $response['p2pmode'] = $rs->campos["modp2p"]; |
---|
| 384 | $response['p2ptime'] = $rs->campos["timep2p"]; |
---|
| 385 | jsonResponse(200, $response); |
---|
| 386 | } |
---|
| 387 | $rs->Cerrar(); |
---|
| 388 | } |
---|
| 389 | ); |
---|
| 390 | |
---|
| 391 | // Listar clientes de un aula. |
---|
[8053aff] | 392 | $app->get('/ous/:ouid/labs/:labid/clients', 'validateApiKey', |
---|
| 393 | function($ouid, $labid) { |
---|
[7829e4e] | 394 | global $cmd; |
---|
| 395 | |
---|
| 396 | $ouid = htmlspecialchars($ouid); |
---|
[8053aff] | 397 | $labid = htmlspecialchars($labid); |
---|
[7829e4e] | 398 | // Listar los clientes del aula si el usuario de la apikey es admin de su UO. |
---|
| 399 | // Consulta temporal, |
---|
[8053aff] | 400 | $cmd->texto = "SELECT * FROM ordenadores WHERE idaula=$labid;"; |
---|
[7829e4e] | 401 | $rs=new Recordset; |
---|
| 402 | $rs->Comando=&$cmd; |
---|
| 403 | if (!$rs->Abrir()) return(false); // Recordset open error. |
---|
| 404 | $rs->Primero(); |
---|
| 405 | if (checkParameter($rs->campos["idaula"])) { |
---|
| 406 | $response['error'] = false; |
---|
| 407 | $response['ouid'] = $ouid; |
---|
[8053aff] | 408 | $response['labid'] = $labid; |
---|
[7829e4e] | 409 | $response['clients'] = array(); |
---|
| 410 | while (!$rs->EOF) { |
---|
| 411 | $tmp = array(); |
---|
| 412 | $tmp['clientid'] = $rs->campos["idordenador"]; |
---|
| 413 | $tmp['clientname'] = $rs->campos["nombreordenador"]; |
---|
| 414 | array_push($response['clients'], $tmp); |
---|
| 415 | $rs->Siguiente(); |
---|
| 416 | } |
---|
| 417 | jsonResponse(200, $response); |
---|
| 418 | } |
---|
| 419 | $rs->Cerrar(); |
---|
| 420 | } |
---|
| 421 | ); |
---|
| 422 | |
---|
| 423 | // Obtener datos de un cliente. |
---|
[8053aff] | 424 | $app->get('/ous/:ouid/labs/:labid/clients/:clntid', 'validateApiKey', |
---|
| 425 | function($ouid, $labid, $clntid) { |
---|
[7829e4e] | 426 | global $cmd; |
---|
| 427 | |
---|
| 428 | $ouid = htmlspecialchars($ouid); |
---|
[8053aff] | 429 | $labid = htmlspecialchars($labid); |
---|
[7829e4e] | 430 | $clntid = htmlspecialchars($clntid); |
---|
| 431 | $cmd->texto = "SELECT * FROM ordenadores WHERE idordenador='$clntid';"; |
---|
| 432 | $rs=new Recordset; |
---|
| 433 | $rs->Comando=&$cmd; |
---|
| 434 | if (!$rs->Abrir()) return(false); // Error al abrir recordset |
---|
| 435 | $rs->Primero(); |
---|
[8053aff] | 436 | // if ($labid != $rs->campos["idaula"]) ... |
---|
[7829e4e] | 437 | if (checkParameter($rs->campos["idordenador"])) { |
---|
| 438 | $response['error'] = false; |
---|
| 439 | $response['clientid'] = $rs->campos["idordenador"]; |
---|
| 440 | $response['clientname'] = $rs->campos["nombreordenador"]; |
---|
| 441 | $response['netiface'] = $rs->campos["netiface"]; |
---|
| 442 | $response['netdriver'] = $rs->campos["netdriver"]; |
---|
| 443 | $response['macaddress'] = $rs->campos["mac"]; |
---|
| 444 | $response['ipaddress'] = $rs->campos["ip"]; |
---|
| 445 | $response['netmask'] = $rs->campos["mascara"]; |
---|
| 446 | $response['routerip'] = $rs->campos["router"]; |
---|
| 447 | $response['repoid'] = $rs->campos["idrepositorio"]; |
---|
| 448 | //$response['hardprofid'] = $rs->campos["idperfilhard"]; |
---|
| 449 | //$response['menuid'] = $rs->campos["idmenu"]; |
---|
| 450 | //$response['validation'] = $rs->campos["arranque"]==0 ? false: true; |
---|
| 451 | $response['boottype'] = $rs->campos["arranque"]; |
---|
| 452 | jsonResponse(200, $response); |
---|
| 453 | } |
---|
| 454 | $rs->Cerrar(); |
---|
| 455 | } |
---|
| 456 | ); |
---|
| 457 | |
---|
| 458 | // Obtener la configuración de hardware de un cliente. |
---|
[8053aff] | 459 | $app->get('/ous/:ouid/labs/:labid/clients/:clntid/hardware', 'validateApiKey', |
---|
| 460 | function($ouid, $labid, $clntid) { |
---|
[7829e4e] | 461 | global $cmd; |
---|
| 462 | |
---|
| 463 | $ouid = htmlspecialchars($ouid); |
---|
[8053aff] | 464 | $labid = htmlspecialchars($labid); |
---|
[7829e4e] | 465 | $clntid = htmlspecialchars($clntid); |
---|
| 466 | $cmd->texto = <<<EOD |
---|
| 467 | SELECT ordenadores.idordenador, ordenadores.nombreordenador, |
---|
| 468 | tipohardwares.nemonico, hardwares.descripcion |
---|
| 469 | FROM perfileshard |
---|
| 470 | RIGHT JOIN ordenadores USING(idperfilhard) |
---|
| 471 | JOIN perfileshard_hardwares USING(idperfilhard) |
---|
| 472 | JOIN hardwares ON perfileshard_hardwares.idhardware=hardwares.idhardware |
---|
| 473 | JOIN tipohardwares ON tipohardwares.idtipohardware=hardwares.idtipohardware |
---|
| 474 | WHERE ordenadores.idordenador='$clntid' |
---|
| 475 | EOD; |
---|
| 476 | $rs=new Recordset; |
---|
| 477 | $rs->Comando=&$cmd; |
---|
| 478 | if (!$rs->Abrir()) return(false); // Error al abrir recordset |
---|
| 479 | $rs->Primero(); |
---|
| 480 | // if ($ouid != $rs->campos["idcentro"]) ... |
---|
[8053aff] | 481 | // if ($labid != $rs->campos["idaula"]) ... |
---|
[7829e4e] | 482 | if (checkParameter($rs->campos["idordenador"])) { |
---|
| 483 | $response['error'] = false; |
---|
| 484 | $response['clientid'] = $rs->campos["idordenador"]; |
---|
| 485 | $response['clientname'] = $rs->campos["nombreordenador"]; |
---|
| 486 | $response['hardware'] = array(); |
---|
| 487 | while (!$rs->EOF) { |
---|
| 488 | $tmp = array(); |
---|
| 489 | $tmp['type'] = $rs->campos["nemonico"]; |
---|
| 490 | $tmp['description'] = $rs->campos["descripcion"]; |
---|
| 491 | array_push($response['hardware'], $tmp); |
---|
| 492 | $rs->Siguiente(); |
---|
| 493 | } |
---|
| 494 | jsonResponse(200, $response); |
---|
| 495 | } |
---|
| 496 | $rs->Cerrar(); |
---|
| 497 | } |
---|
| 498 | ); |
---|
| 499 | |
---|
| 500 | // Obtener datos de configuración de discos del cliente. |
---|
[8053aff] | 501 | $app->get('/ous/:ouid/labs/:labid/clients/:clntid/diskcfg', 'validateApiKey', |
---|
| 502 | function($ouid, $labid, $clntid) { |
---|
[7829e4e] | 503 | global $cmd; |
---|
| 504 | |
---|
| 505 | $ouid = htmlspecialchars($ouid); |
---|
[8053aff] | 506 | $labid = htmlspecialchars($labid); |
---|
[7829e4e] | 507 | $clntid = htmlspecialchars($clntid); |
---|
| 508 | $cmd->texto = <<<EOD |
---|
| 509 | SELECT ordenadores.idordenador AS clientid, ordenadores.nombreordenador, |
---|
| 510 | ordenadores_particiones.*, tipospar.tipopar, |
---|
| 511 | sistemasficheros.nemonico, nombresos.nombreso, imagenes.nombreca |
---|
| 512 | FROM ordenadores_particiones |
---|
| 513 | RIGHT JOIN ordenadores USING(idordenador) |
---|
| 514 | LEFT JOIN tipospar USING(codpar) |
---|
| 515 | LEFT JOIN sistemasficheros USING(idsistemafichero) |
---|
| 516 | LEFT JOIN nombresos USING(idnombreso) |
---|
| 517 | LEFT JOIN imagenes USING(idimagen) |
---|
| 518 | WHERE ordenadores.idordenador='$clntid' |
---|
| 519 | ORDER BY numdisk ASC, numpar ASC; |
---|
| 520 | EOD; |
---|
| 521 | $rs=new Recordset; |
---|
| 522 | $rs->Comando=&$cmd; |
---|
| 523 | if (!$rs->Abrir()) return(false); // Error al abrir recordset |
---|
| 524 | $rs->Primero(); |
---|
[8053aff] | 525 | // if ($labid != $rs->campos["idaula"]) ... |
---|
[7829e4e] | 526 | if (checkParameter($rs->campos["clientid"])) { |
---|
| 527 | $response['error'] = false; |
---|
| 528 | $response['clientid'] = $rs->campos["clientid"]; |
---|
| 529 | $response['clientname'] = $rs->campos["nombreordenador"]; |
---|
| 530 | $response['diskcfg'] = array(); |
---|
| 531 | while (!$rs->EOF) { |
---|
| 532 | if ($rs->campos["numdisk"] == 0) { |
---|
| 533 | $rs->Siguiente(); |
---|
| 534 | continue; |
---|
| 535 | } |
---|
| 536 | $tmp = array(); |
---|
| 537 | if ($rs->campos["numpar"] == 0) { |
---|
| 538 | $tmp['disk'] = $rs->campos["numdisk"]; |
---|
| 539 | switch ($rs->campos["codpar"]) { |
---|
| 540 | case 1: $tmp['parttable'] = "MSDOS"; break; |
---|
| 541 | case 2: $tmp['parttable'] = "GPT"; break; |
---|
| 542 | case 3: $tmp['parttable'] = "LVM"; break; |
---|
| 543 | case 4: $tmp['parttable'] = "ZPOOL"; break; |
---|
| 544 | default: $tmp['parttable'] = $rs->campos["codpar"]; |
---|
| 545 | } |
---|
| 546 | $tmp['size'] = $rs->campos["tamano"]; |
---|
| 547 | } else { |
---|
| 548 | $tmp['partition'] = $rs->campos["numpar"]; |
---|
| 549 | $tmp['parttype'] = $rs->campos["tipopar"]; |
---|
| 550 | $tmp['filesystem'] = $rs->campos["nemonico"]; |
---|
[4454169b] | 551 | $tmp['size'] = $rs->campos["tamano"]; |
---|
| 552 | $tmp['usage'] = $rs->campos["uso"]; |
---|
[7829e4e] | 553 | if ($rs->campos["nombreso"] != null) { |
---|
| 554 | $tmp['os'] = $rs->campos["nombreso"]; |
---|
| 555 | $tmp['idimage'] = $rs->campos["idimagen"]; |
---|
| 556 | $tmp['deploydate'] = $rs->campos["fechadespliegue"]; |
---|
| 557 | } |
---|
| 558 | //$tmp['cachedata'] = $rs->campos["cache"]; |
---|
| 559 | } |
---|
| 560 | array_push($response['diskcfg'], $tmp); |
---|
| 561 | $rs->Siguiente(); |
---|
| 562 | } |
---|
| 563 | jsonResponse(200, $response); |
---|
| 564 | } |
---|
| 565 | $rs->Cerrar(); |
---|
| 566 | } |
---|
| 567 | ); |
---|
| 568 | |
---|
| 569 | // Obtener estado de ejecución del cliente. |
---|
[8053aff] | 570 | $app->get('/ous/:ouid/labs/:labid/clients/:clntid/status', 'validateApiKey', |
---|
| 571 | function($ouid, $labid, $clntid) { |
---|
[7829e4e] | 572 | global $cmd; |
---|
| 573 | global $LONCABECERA; |
---|
| 574 | global $LONHEXPRM; |
---|
| 575 | |
---|
| 576 | // Pparameters. |
---|
| 577 | $ouid = htmlspecialchars($ouid); |
---|
[8053aff] | 578 | $labid = htmlspecialchars($labid); |
---|
[7829e4e] | 579 | $clntid = htmlspecialchars($clntid); |
---|
| 580 | |
---|
| 581 | // Database query. |
---|
| 582 | $cmd->texto = <<<EOD |
---|
| 583 | SELECT serv.ipserveradm, serv.portserveradm, clnt.idordenador, clnt.ip |
---|
| 584 | FROM entornos AS serv, ordenadores AS clnt |
---|
| 585 | WHERE clnt.idordenador='$clntid'; |
---|
| 586 | EOD; |
---|
| 587 | $rs=new Recordset; |
---|
| 588 | $rs->Comando=&$cmd; |
---|
| 589 | if (!$rs->Abrir()) return(false); // Error al abrir recordset |
---|
| 590 | $rs->Primero(); |
---|
| 591 | if (checkParameter($rs->campos["idordenador"])) { |
---|
| 592 | $serverip = $rs->campos["ipserveradm"]; |
---|
| 593 | $serverport = $rs->campos["portserveradm"]; |
---|
| 594 | $clientid = $rs->campos["idordenador"]; |
---|
| 595 | $clientip = $rs->campos["ip"]; |
---|
| 596 | |
---|
| 597 | // Connect to reset client's status. |
---|
| 598 | $reqframe = "nfn=Sondeo\r". |
---|
| 599 | "ido=$clientid\r". |
---|
| 600 | "iph=$clientip\r"; |
---|
[c03c7c3] | 601 | $result = sendCommand($serverip, $serverport, $reqframe, $values); |
---|
[7829e4e] | 602 | |
---|
| 603 | // Connect to fetch client's status. |
---|
| 604 | // Asuming client is off by default. |
---|
| 605 | $values["tso"]="OFF"; |
---|
| 606 | // Iterate to check client's status. |
---|
| 607 | // Exit if status!=OFF or end iterations (status=OFF). |
---|
| 608 | $maxIter = 30; |
---|
| 609 | for ($i=1; $i<$maxIter and preg_match('/OFF/', $values["tso"]); $i++) { |
---|
| 610 | // Connect to check status. |
---|
| 611 | $reqframe = "nfn=respuestaSondeo\r". |
---|
| 612 | "ido=$clientid\r". |
---|
| 613 | "iph=$clientip\r"; |
---|
[c03c7c3] | 614 | $result = sendCommand($serverip, $serverport, $reqframe, $values); |
---|
[7829e4e] | 615 | // Wait until next checking (0.1 ms). |
---|
| 616 | usleep(100000); |
---|
| 617 | } |
---|
| 618 | |
---|
| 619 | // Parse status response. |
---|
| 620 | if ($result) { |
---|
| 621 | // Check status type. |
---|
| 622 | if (checkParameter($values["tso"])) { |
---|
| 623 | // Compose JSON response. |
---|
| 624 | $response['error'] = false; |
---|
| 625 | $response['clientid'] = $clientid; |
---|
| 626 | $response['ip'] = $clientip; |
---|
| 627 | $stat = array(); |
---|
| 628 | preg_match('/\/[A-Z]*;/', $values["tso"], $stat); |
---|
| 629 | // Check if data exists. |
---|
| 630 | if (empty($stat[0])) { |
---|
| 631 | $response['status'] = "nodata"; |
---|
| 632 | } else { |
---|
| 633 | // Status mapping. |
---|
| 634 | $status = array('OFF'=>"off", |
---|
| 635 | 'INI'=>"initializing", |
---|
| 636 | 'OPG'=>"ogclient", |
---|
| 637 | 'BSY'=>"busy", |
---|
| 638 | 'LNX'=>"linux", |
---|
| 639 | 'WIN'=>"windows"); |
---|
| 640 | $response['status'] = $status[substr($stat[0], 1, 3)]; |
---|
| 641 | if (empty($response['status'])) { |
---|
| 642 | $response['status'] = "unknown"; |
---|
| 643 | } |
---|
| 644 | } |
---|
| 645 | jsonResponse(200, $response); |
---|
| 646 | } |
---|
| 647 | } else { |
---|
| 648 | // Access error. |
---|
| 649 | $response['error'] = true; |
---|
| 650 | $response['message'] = "Cannot access to OpenGnsys server"; |
---|
| 651 | jsonResponse(500, $response); |
---|
| 652 | } |
---|
| 653 | } |
---|
| 654 | $rs->Cerrar(); |
---|
| 655 | } |
---|
| 656 | ); |
---|
| 657 | |
---|
| 658 | |
---|
| 659 | // Listar repositorios. |
---|
| 660 | $app->get('/ous/:ouid/repos', 'validateApiKey', |
---|
| 661 | function($ouid) { |
---|
| 662 | global $cmd; |
---|
| 663 | |
---|
| 664 | $ouid = htmlspecialchars($ouid); |
---|
| 665 | // Listar las salas de la UO si el usuario de la apikey es su admin. |
---|
| 666 | // Consulta temporal, |
---|
| 667 | $cmd->texto = "SELECT * FROM repositorios WHERE idcentro='$ouid';"; |
---|
| 668 | $rs=new Recordset; |
---|
| 669 | $rs->Comando=&$cmd; |
---|
| 670 | if (!$rs->Abrir()) return(false); // Error al abrir recordset |
---|
| 671 | $rs->Primero(); |
---|
| 672 | // Comprobar que exista la UO. |
---|
| 673 | if (checkParameter($rs->campos["idcentro"])) { |
---|
| 674 | $response['error'] = false; |
---|
| 675 | $response['ouid'] = $ouid; |
---|
| 676 | $response['repos'] = array(); |
---|
| 677 | while (!$rs->EOF) { |
---|
| 678 | $tmp = array(); |
---|
| 679 | $tmp['repoid'] = $rs->campos["idrepositorio"]; |
---|
| 680 | $tmp['reponame'] = $rs->campos["nombrerepositorio"]; |
---|
| 681 | array_push($response['repos'], $tmp); |
---|
| 682 | $rs->Siguiente(); |
---|
| 683 | } |
---|
| 684 | jsonResponse(200, $response); |
---|
| 685 | } |
---|
| 686 | $rs->Cerrar(); |
---|
| 687 | } |
---|
| 688 | ); |
---|
| 689 | |
---|
| 690 | // Obtener datos de un repositorio. |
---|
| 691 | $app->get('/ous/:ouid/repos/:repoid', 'validateApiKey', |
---|
| 692 | function($ouid, $repoid) { |
---|
| 693 | global $cmd; |
---|
| 694 | |
---|
| 695 | $ouid = htmlspecialchars($ouid); |
---|
| 696 | $repoid = htmlspecialchars($repoid); |
---|
| 697 | $cmd->texto = "SELECT * FROM repositorios WHERE idrepositorio='$repoid';"; |
---|
| 698 | $rs=new Recordset; |
---|
| 699 | $rs->Comando=&$cmd; |
---|
| 700 | if (!$rs->Abrir()) return(false); // Error al abrir recordset |
---|
| 701 | $rs->Primero(); |
---|
| 702 | // Comprobar que exista el repositorio. |
---|
| 703 | if (checkParameter($rs->campos["idrepositorio"])) { |
---|
| 704 | $response['error'] = false; |
---|
| 705 | $response['repoid'] = $rs->campos["idrepositorio"]; |
---|
[8053aff] | 706 | $response['reponame'] = $rs->campos["nombrerepositorio"]; |
---|
[7829e4e] | 707 | $response['description'] = $rs->campos["comentarios"]; |
---|
| 708 | $response['ipaddress'] = $rs->campos["ip"]; |
---|
| 709 | $response['port'] = $rs->campos["puertorepo"]; |
---|
| 710 | jsonResponse(200, $response); |
---|
| 711 | } |
---|
| 712 | $rs->Cerrar(); |
---|
| 713 | } |
---|
| 714 | ); |
---|
| 715 | |
---|
| 716 | // Listar imágenes. |
---|
| 717 | $app->get('/ous/:ouid/images', 'validateApiKey', |
---|
| 718 | function($ouid) { |
---|
| 719 | global $cmd; |
---|
| 720 | |
---|
| 721 | $ouid = htmlspecialchars($ouid); |
---|
| 722 | // Listar las salas de la UO si el usuario de la apikey es su admin. |
---|
| 723 | // Consulta temporal, |
---|
| 724 | $cmd->texto = "SELECT * FROM imagenes WHERE idcentro='$ouid';"; |
---|
| 725 | $rs=new Recordset; |
---|
| 726 | $rs->Comando=&$cmd; |
---|
| 727 | if (!$rs->Abrir()) return(false); // Error al abrir recordset |
---|
| 728 | // Comprobar que exista la UO. |
---|
| 729 | $rs->Primero(); |
---|
| 730 | if (checkParameter($rs->campos["idcentro"])) { |
---|
| 731 | $response['error'] = false; |
---|
| 732 | $response['ouid'] = $ouid; |
---|
| 733 | $response['images'] = array(); |
---|
| 734 | while (!$rs->EOF) { |
---|
| 735 | $tmp = array(); |
---|
| 736 | $tmp['imageid'] = $rs->campos["idimagen"]; |
---|
| 737 | $tmp['imagename'] = $rs->campos["nombreca"]; |
---|
[8053aff] | 738 | $tmp['inremotepc'] = $rs->campos["inremotepc"]==0 ? false: true; |
---|
[7829e4e] | 739 | array_push($response['images'], $tmp); |
---|
| 740 | $rs->Siguiente(); |
---|
| 741 | } |
---|
| 742 | jsonResponse(200, $response); |
---|
| 743 | } |
---|
| 744 | } |
---|
| 745 | ); |
---|
| 746 | |
---|
| 747 | // Obtener datos de una imagen. |
---|
| 748 | $app->get('/ous/:ouid/images/:imgid', 'validateApiKey', |
---|
| 749 | function($ouid, $imgid) { |
---|
| 750 | global $cmd; |
---|
| 751 | |
---|
| 752 | $ouid = htmlspecialchars($ouid); |
---|
| 753 | $imgid = htmlspecialchars($imgid); |
---|
| 754 | $cmd->texto = "SELECT * FROM imagenes WHERE idimagen='$imgid';"; |
---|
| 755 | $rs=new Recordset; |
---|
| 756 | $rs->Comando=&$cmd; |
---|
| 757 | if (!$rs->Abrir()) return(false); // Error al abrir recordset |
---|
| 758 | $rs->Primero(); |
---|
| 759 | // Comprobar que exista el repositorio. |
---|
| 760 | if (checkParameter($rs->campos["idimagen"])) { |
---|
| 761 | $response['error'] = false; |
---|
| 762 | $response['imageid'] = $rs->campos["idimagen"]; |
---|
| 763 | $response['imagename'] = $rs->campos["nombreca"]; |
---|
| 764 | $response['description'] = $rs->campos["descripcion"]; |
---|
| 765 | $response['comments'] = $rs->campos["comentarios"]; |
---|
[8053aff] | 766 | $response['inremotepc'] = $rs->campos["inremotepc"]==0 ? false: true; |
---|
[7829e4e] | 767 | $response['repoid'] = $rs->campos["idrepositorio"]; |
---|
| 768 | switch ($rs->campos["tipo"]) { |
---|
| 769 | case 1: $response['type'] = "monolithic"; break; |
---|
| 770 | case 2: $response['type'] = "base"; break; |
---|
| 771 | case 3: $response['type'] = "incremental"; |
---|
| 772 | $response['baseimg'] = $rs->campos["imagenid"]; |
---|
| 773 | $response['path'] = $rs->campos["ruta"]; |
---|
| 774 | break; |
---|
| 775 | default: $response['type'] = $rs->campos["tipo"]; |
---|
| 776 | } |
---|
| 777 | if ($rs->campos["idordenador"] != 0) { |
---|
| 778 | $response['clientid'] = $rs->campos["idordenador"]; |
---|
| 779 | $response['disk'] = $rs->campos["numdisk"]; |
---|
| 780 | $response['partition'] = $rs->campos["numpar"]; |
---|
| 781 | $response['creationdate'] = $rs->campos["fechacreacion"]; |
---|
| 782 | } |
---|
| 783 | jsonResponse(200, $response); |
---|
| 784 | } |
---|
| 785 | $rs->Cerrar(); |
---|
| 786 | } |
---|
| 787 | ); |
---|
| 788 | |
---|
| 789 | // Lista de softeare instalado en una imagen. |
---|
| 790 | $app->get('/ous/:ouid/images/:imgid/software', 'validateApiKey', |
---|
| 791 | function($ouid, $imgid) { |
---|
| 792 | global $cmd; |
---|
| 793 | |
---|
| 794 | $ouid = htmlspecialchars($ouid); |
---|
| 795 | $imgid = htmlspecialchars($imgid); |
---|
| 796 | $cmd->texto = <<<EOD |
---|
| 797 | SELECT imagenes.idimagen, imagenes.nombreca, softwares.descripcion |
---|
| 798 | FROM perfilessoft |
---|
| 799 | RIGHT JOIN imagenes USING(idperfilsoft) |
---|
| 800 | LEFT JOIN perfilessoft_softwares USING(idperfilsoft) |
---|
| 801 | LEFT JOIN softwares USING(idsoftware) |
---|
| 802 | WHERE imagenes.idimagen='$imgid' |
---|
| 803 | ORDER BY softwares.descripcion ASC; |
---|
| 804 | EOD; |
---|
| 805 | $rs=new Recordset; |
---|
| 806 | $rs->Comando=&$cmd; |
---|
| 807 | if (!$rs->Abrir()) return(false); // Error al abrir recordset |
---|
| 808 | $rs->Primero(); |
---|
| 809 | // Comprobar que exista el repositorio. |
---|
| 810 | if (checkParameter($rs->campos["idimagen"])) { |
---|
| 811 | $response['error'] = false; |
---|
| 812 | $response['imageid'] = $rs->campos["idimagen"]; |
---|
| 813 | $response['imagename'] = $rs->campos["nombreca"]; |
---|
| 814 | $response['software'] = array(); |
---|
| 815 | while (!$rs->EOF) { |
---|
| 816 | if ($rs->campos["descripcion"] == null) { |
---|
| 817 | $rs->Siguiente(); |
---|
| 818 | continue; |
---|
| 819 | } |
---|
| 820 | $tmp = array(); |
---|
| 821 | $tmp['application'] = $rs->campos["descripcion"]; |
---|
| 822 | array_push($response['software'], $tmp); |
---|
| 823 | $rs->Siguiente(); |
---|
| 824 | } |
---|
| 825 | jsonResponse(200, $response); |
---|
| 826 | } |
---|
| 827 | $rs->Cerrar(); |
---|
| 828 | } |
---|
| 829 | ); |
---|
| 830 | |
---|
| 831 | // Arrancar N ordenadores con una imagen instalada. |
---|
| 832 | $app->get('/ous/:id1/images/:id2/boot', 'validateApiKey', |
---|
| 833 | function($ouid, $imageid) { |
---|
| 834 | global $cmd; |
---|
| 835 | global $AMBITO_ORDENADORES; |
---|
| 836 | global $EJECUCION_COMANDO; |
---|
| 837 | global $ACCION_INICIADA; |
---|
| 838 | global $ACCION_SINRESULTADO; |
---|
| 839 | |
---|
| 840 | // Pparameters. |
---|
| 841 | $ouid = htmlspecialchars($ouid); |
---|
| 842 | $imegeid = htmlspecialchars($imageid); |
---|
| 843 | // Boot 1 client. |
---|
| 844 | $nclients = 1; |
---|
| 845 | |
---|
| 846 | // Query: server data and all clients' boot data availabe for Remote PC with this image installed (random order). |
---|
| 847 | $cmd->texto = <<<EOD |
---|
| 848 | SELECT s.ipserveradm, s.portserveradm, |
---|
| 849 | c.idordenador, c.ip, c.mac, p.numdisk, p.numpar |
---|
| 850 | FROM entornos AS s, ordenadores AS c |
---|
| 851 | JOIN aulas USING(idaula) |
---|
| 852 | JOIN centros USING(idcentro) |
---|
| 853 | JOIN ordenadores_particiones AS p USING(idordenador) |
---|
| 854 | JOIN imagenes USING(idimagen) |
---|
| 855 | WHERE centros.idcentro='$ouid' |
---|
| 856 | AND aulas.inremotepc=1 |
---|
| 857 | AND imagenes.idimagen='$imageid' |
---|
[8053aff] | 858 | AND imagenes.inremotepc=1 |
---|
[7829e4e] | 859 | ORDER BY RAND(); |
---|
| 860 | EOD; |
---|
| 861 | $rs=new Recordset; |
---|
| 862 | $rs->Comando=&$cmd; |
---|
| 863 | if (!$rs->Abrir()) return(false); // Error oppening recordset. |
---|
| 864 | $rs->Primero(); |
---|
| 865 | if (checkParameter($rs->campos["ipserveradm"])) { |
---|
| 866 | |
---|
| 867 | $response['error'] = false; |
---|
| 868 | $response['imageid'] = $imageid; |
---|
| 869 | $response['sendto'] = array(); |
---|
| 870 | |
---|
| 871 | // AVISO: Procesar datos del servidor (solo 1er registro). |
---|
| 872 | |
---|
| 873 | $serverip = $rs->campos["ipserveradm"]; |
---|
| 874 | $serverport = $rs->campos["portserveradm"]; |
---|
| 875 | |
---|
| 876 | // AVISO: Procesar datos de los clientes. |
---|
| 877 | |
---|
| 878 | $clientid = array(); |
---|
| 879 | $clientip = array(); |
---|
| 880 | $clientmac = array(); |
---|
| 881 | $clientdisk = array(); |
---|
| 882 | $clientpart = array(); |
---|
| 883 | while (!$rs->EOF) { |
---|
| 884 | array_push($clientid, $rs->campos["idordenador"]); |
---|
| 885 | array_push($clientip, $rs->campos["ip"]); |
---|
| 886 | array_push($clientmac, $rs->campos["mac"]); |
---|
| 887 | array_push($clientdisk, $rs->campos["numdisk"]); |
---|
| 888 | array_push($clientpart, $rs->campos["numpar"]); |
---|
| 889 | $rs->Siguiente(); |
---|
| 890 | } |
---|
| 891 | $rs->Cerrar(); |
---|
| 892 | |
---|
| 893 | // AVISO: consultar el estado de todos los clientes y |
---|
| 894 | // quitar aquellos que no tengan "OFF", "OPG" o "" |
---|
| 895 | // (estudiar si incluir los "BSY") |
---|
| 896 | |
---|
| 897 | // Reset clients' status. |
---|
| 898 | $reqframe = "nfn=Sondeo\r". |
---|
| 899 | "ido=".implode(',', $clientid)."\r". |
---|
| 900 | "iph=".implode(';', $clientip)."\r"; |
---|
[c03c7c3] | 901 | sendCommand($serverip, $serverport, $reqframe, $values); |
---|
[7829e4e] | 902 | // Wait to get response. |
---|
| 903 | sleep(3); |
---|
| 904 | // Connect to check status. |
---|
| 905 | $reqframe = "nfn=respuestaSondeo\r". |
---|
| 906 | "ido=".implode(',', $clientid)."\r". |
---|
| 907 | "iph=".implode(';', $clientip)."\r"; |
---|
[c03c7c3] | 908 | sendCommand($serverip, $serverport, $reqframe, $values); |
---|
[7829e4e] | 909 | // Check status type. |
---|
| 910 | if (isset($values["tso"])) { |
---|
| 911 | preg_match_all('/[A-Z]{3}/', $values["tso"], $stat); |
---|
| 912 | } |
---|
| 913 | if (isset($stat[0])) { |
---|
| 914 | for ($i=0; $i<sizeof($stat[0]); $i++) { |
---|
| 915 | if (! in_array($stat[0][$i], array("OFF", "OPG", ""))) { |
---|
| 916 | unset($clientid[$i]); |
---|
| 917 | unset($clientip[$i]); |
---|
| 918 | unset($clientmac[$i]); |
---|
| 919 | unset($clientdisk[$i]); |
---|
| 920 | unset($clientpart[$i]); |
---|
| 921 | } |
---|
| 922 | } |
---|
| 923 | } |
---|
| 924 | |
---|
| 925 | // AVISO: el siguiente código inicia un único cliente. |
---|
| 926 | // Para iniciar varios: |
---|
| 927 | // - id. clientes separados por carácter ','. |
---|
| 928 | // - IP clientes separadas por carácter ';' |
---|
| 929 | // - MAC clientes separadas por carácter ';' |
---|
| 930 | |
---|
| 931 | // Executing boot command. |
---|
| 932 | $reqframe = "nfn=Arrancar\r". |
---|
| 933 | "ido=".implode(',', $clientid)."\r". |
---|
| 934 | "iph=".implode(';', $clientip)."\r". |
---|
| 935 | "mac=".implode(';', $clientmac)."\r". |
---|
| 936 | "mar=1\r"; |
---|
| 937 | echo "req=".str_replace("\r"," ",$reqframe).".\n"; |
---|
[c03c7c3] | 938 | sendCommand($serverip, $serverport, $reqframe, $values); |
---|
[7829e4e] | 939 | if ($values["res"]) { |
---|
| 940 | print_r($values); |
---|
| 941 | $tmp = array(); |
---|
| 942 | for ($i=0, $boot=0; $i<sizeof($clientid) and $boot!=1; $i++) { |
---|
| 943 | $reqframe = "nfn=IniciarSesion\r". |
---|
| 944 | "ido=".$clientid[$i]."\r". |
---|
| 945 | "iph=".$clientip[$i]."\r". |
---|
| 946 | "dsk=".$clientdisk[$i]."\r". |
---|
| 947 | "par=".$clientpart[$i]."\r"; |
---|
| 948 | echo "i=$i: req=".str_replace("\r"," ",$reqframe).".\n"; |
---|
[c03c7c3] | 949 | sendCommand($serverip, $serverport, $reqframe, $values); |
---|
[7829e4e] | 950 | if ($values["res"]) { |
---|
| 951 | |
---|
| 952 | // AVISO: incluir comando Iniciar sesión en cola de acciones. |
---|
| 953 | $timestamp=time(); |
---|
| 954 | $cmd->texto = <<<EOD |
---|
| 955 | INSERT INTO acciones |
---|
| 956 | SET tipoaccion=$EJECUCION_COMANDO, |
---|
| 957 | idtipoaccion=9, |
---|
| 958 | idcomando=9, |
---|
| 959 | parametros='nfn=IniciarSesion\rdsk=$clientdisk[$i]\rpar=$clientpart[$i]', |
---|
| 960 | descriaccion='RemotePC Session', |
---|
| 961 | idordenador=$clientid[$i], |
---|
| 962 | ip='$clientip[$i]', |
---|
| 963 | sesion=$timestamp, |
---|
| 964 | fechahorareg=NOW(), |
---|
| 965 | estado=$ACCION_INICIADA, |
---|
| 966 | resultado=$ACCION_SINRESULTADO, |
---|
| 967 | ambito=$AMBITO_ORDENADORES, |
---|
| 968 | idambito=$clientid[$i], |
---|
| 969 | restrambito='$clientip[$i]', |
---|
| 970 | idcentro=$ouid; |
---|
| 971 | EOD; |
---|
| 972 | $result = $cmd->Ejecutar(); |
---|
| 973 | if ($result) { |
---|
| 974 | $tmp['clientid'] = $clientid[$i]; |
---|
| 975 | $tmp['ip'] = $clientip[$i]; |
---|
| 976 | $tmp['mac'] = $clientmac[$i]; |
---|
| 977 | array_push($response['sendto'], $tmp); |
---|
| 978 | $boot = 1; |
---|
| 979 | } |
---|
| 980 | } |
---|
| 981 | } |
---|
| 982 | } |
---|
| 983 | jsonResponse(200, $response); |
---|
| 984 | } |
---|
| 985 | } |
---|
| 986 | ); |
---|
| 987 | // Alternativa como método GET. |
---|
| 988 | //$app->get('/ous/:id1/images/:id2/boot/:number', 'validateApiKey', |
---|
| 989 | // function($ouid, $imageid, $number) { |
---|
| 990 | // |
---|
| 991 | // } |
---|
| 992 | //); |
---|
| 993 | |
---|
| 994 | // Ejecutar REST con Slim. |
---|
| 995 | $app->run(); |
---|
| 996 | |
---|
| 997 | ?> |
---|
| 998 | |
---|