diff options
author | Ramón M. Gómez <ramongomez@us.es> | 2019-05-30 10:49:13 +0200 |
---|---|---|
committer | Ramón M. Gómez <ramongomez@us.es> | 2019-05-30 10:49:13 +0200 |
commit | e57b608e4e8176750fe25c9c8cebcc38adbca369 (patch) | |
tree | beb06740282262c0632f196055c136332835fa8e /admin/WebConsole/rest | |
parent | d4e02b28f5455cc47dca40ed28ac03655653d61b (diff) |
#891: Boot (wake) command is sent by both server and client repository.
Diffstat (limited to 'admin/WebConsole/rest')
-rw-r--r-- | admin/WebConsole/rest/repository.php | 64 |
1 files changed, 19 insertions, 45 deletions
diff --git a/admin/WebConsole/rest/repository.php b/admin/WebConsole/rest/repository.php index bc2fdec6..736cc1c2 100644 --- a/admin/WebConsole/rest/repository.php +++ b/admin/WebConsole/rest/repository.php @@ -10,12 +10,6 @@ * @date 2016-04-06 */ -include_once("../clases/SockHidra.php"); - -include_once("../includes/comunes.php"); - -define("LENHEXPRM", 5); // Length of hexdecimal chain containing total frame length -define("LENHEAD", 16); // Frame head length // Auxiliar functions. /** @@ -30,8 +24,8 @@ function validateRepositoryApiKey() { @$apikey = htmlspecialchars(function_exists('apache_request_headers') ? apache_request_headers()['Authorization'] : $_SERVER['HTTP_AUTHORIZATION']); if (isset($apikey)) { // fetch repository token from ogAdmRepo.cfg configuration file. - @$confFile = parse_ini_file('../../etc/ogAdmRepo.cfg', 'r'); - if ($confFile) { + @$confFile = parse_ini_file(__DIR__ . '/../../etc/ogAdmRepo.cfg', 'r'); + if (isset($confFile)) { if(@strcmp($apikey, $confFile['ApiToken']) == 0) { // Credentials OK. return true; @@ -191,46 +185,26 @@ $app->get('/repository/image(/:ouname)/:imagename(/)', 'validateRepositoryApiKey */ $app->post('/repository/poweron', 'validateRepositoryApiKey', function() use($app) { - // Fetch repository token from ogAdmServer.cfg configuration file. - @$confFile = parse_ini_file('../../etc/ogAdmServer.cfg', 'r'); - if ($confFile) { - // The macs parameter must come in the post (JSON object with array of MACs) - $data = json_decode($app->request()->getBody()); - if (empty($data->macs)) { - // Print error message. - $response['message'] = 'Required param macs not found'; - jsonResponse(400, $response); + // The macs parameter must come in the post (JSON object with array of MACs) + $data = json_decode($app->request()->getBody()); + if (empty($data->macs)) { + // Print error message. + $response['message'] = 'Required param macs not found'; + jsonResponse(400, $response); + } else { + // Execute local wakeonlan command (may be installed) + if(commandExist("wakeonlan")) { + $strMacs = trim(implode(' ', $data->macs)); + $response["output"] = "Executing wakeonlan ".$strMacs."\n"; + $response["output"] .= shell_exec("wakeonlan ".$strMacs); + jsonResponse(200, $response); } else { - // Execute WakeOnLan command with ogAdmServer - $strMacs = implode(';', $data->macs); - $strMacs = str_replace(':', '', $strMacs); - $strIps = implode(';', $data->ips); - $params="nfn=Arrancar" . chr(13) . "mac=" . $strMacs . chr(13) . "iph=" . $strIps . chr(13) . - "mar=" . $data->mar . chr(13); - $shidra=new SockHidra($confFile['ServidorAdm'], $confFile['PUERTO']); - if ($shidra->conectar()) { // The connection to the hydra server has been established - $resul=$shidra->envia_comando($params); - if($resul) { - $frame=$shidra->recibe_respuesta(); - $hlonprm=hexdec(substr($frame, LENHEAD, LENHEXPRM)); - $params=substr($frame, LENHEAD + LENHEXPRM, $hlonprm); - $ParamsValue=extrae_parametros($params, chr(13), '='); - $resul=$ParamsValue["res"]; - jsonResponse(200, $resul); - } else { - $response['message'] = 'Error in ogAdmServer'; - jsonResponse(404, $response); - } - $shidra->desconectar(); - } + // Print error message. + $response['message'] = 'Wakeonlan command not found in this repository'; + jsonResponse(404, $response); } - } else { - // Cannot access configuration file. - $response['message'] = "An error occurred, please try again"; - jsonResponse(500, $response); - $app->stop(); } + $app->stop(); } ); - |