diff options
Diffstat (limited to 'admin/WebConsole/rest/common.php')
-rw-r--r-- | admin/WebConsole/rest/common.php | 82 |
1 files changed, 16 insertions, 66 deletions
diff --git a/admin/WebConsole/rest/common.php b/admin/WebConsole/rest/common.php index d26acc4b..5b49cda8 100644 --- a/admin/WebConsole/rest/common.php +++ b/admin/WebConsole/rest/common.php @@ -68,14 +68,10 @@ function jsonResponse($status, $response, $opts=0) { * @return string JSON response. */ function jsonResponseNow($status, $response, $opts=0) { - // Flush buffer. - ob_end_clean(); - ob_end_flush(); - header("Connection: close"); // Compose headers and content. + ignore_user_abort(); http_response_code((int)$status); header('Content-type: application/json; charset=utf-8'); - ignore_user_abort(); ob_start(); echo json_encode($response, $opts); $size = ob_get_length(); @@ -88,12 +84,12 @@ function jsonResponseNow($status, $response, $opts=0) { /** * @brief Validate API key included in "Authorization" HTTP header. - * @return JSON response on error. + * @return string JSON response on error. */ function validateApiKey() { global $cmd; global $userid; - $response = array(); + $response = []; $app = \Slim\Slim::getInstance(); // Read Authorization HTTP header. if (! empty($_SERVER['HTTP_AUTHORIZATION'])) { @@ -110,8 +106,8 @@ function validateApiKey() { // Fetch user id. $userid = $rs->campos["idusuario"]; } else { - // Credentials error. - $response['message'] = 'Login failed. Incorrect credentials'; + // Credentials error. + $response['message'] = 'Login failed, incorrect credentials'; jsonResponse(401, $response); $app->stop(); } @@ -135,6 +131,7 @@ function validateApiKey() { * @return boolean "false" if parameter is null, otherwise "true". */ function checkParameter($param) { + $response = []; if (isset($param)) { return true; } else { @@ -151,7 +148,7 @@ function checkParameter($param) { * @return boolean "true" if all ids are int>0, otherwise "false". */ function checkIds() { - $opts = Array('options' => Array('min_range' => 1)); // Check for int>0 + $opts = ['options' => ['min_range' => 1]]; // Check for int>0 foreach (func_get_args() as $id) { if (filter_var($id, FILTER_VALIDATE_INT, $opts) === false) { return false; @@ -161,62 +158,16 @@ function checkIds() { } /** - * @fn sendCommand($serverip, $serverport, $reqframe, &$values) - * @brief Send a command to an OpenGnsys ogAdmServer and get request. - * @param string serverip Server IP address. - * @param string serverport Server port. - * @param string reqframe Request frame (field's separator is "\r"). - * @param array values Response values (out parameter). - * @return boolean "true" if success, otherwise "false". - */ -function sendCommand($serverip, $serverport, $reqframe, &$values) { - global $LONCABECERA; - global $LONHEXPRM; - - // Connect to server. - $respvalues = ""; - $connect = new SockHidra($serverip, $serverport); - if ($connect->conectar()) { - // Send request frame to server. - $result = $connect->envia_peticion($reqframe); - if ($result) { - // Parse request frame. - $respframe = $connect->recibe_respuesta(); - $connect->desconectar(); - $paramlen = hexdec(substr($respframe, $LONCABECERA, $LONHEXPRM)); - $params = substr($respframe, $LONCABECERA+$LONHEXPRM, $paramlen); - // Fetch values and return result. - $values = extrae_parametros($params, "\r", '='); - return ($values); - } else { - // Return with error. - return (false); - } - } else { - // Return with error. - return (false); - } -} - -/** * @brief Show custom message for "not found" error (404). */ -$app->notFound(function() { - echo "REST route not found."; +$app->notFound( + function() { + $response['message'] = 'REST route not found'; + jsonResponse(404, $response); } ); /** - * @brief Hook to write a REST init log message, if debug is enabled. - * @warning Message will be written in REST log file. - */ -$app->hook('slim.before', function() use ($app) { - if ($app->settings['debug']) - writeRestLog("Init."); - } -); - -/** * @brief Hook to write an error log message and a REST exit log message if debug is enabled. * @warning Error message will be written in web server's error file. * @warning REST message will be written in REST log file. @@ -234,7 +185,7 @@ $app->hook('slim.after', function() use ($app) { substr($app->response->getBody(), 0, 100)); } if ($app->settings['debug']) - writeRestLog("Exit."); + writeRestLog(substr($app->response->getBody(), 0, 30)); } ); @@ -244,8 +195,7 @@ $app->hook('slim.after', function() use ($app) { /** * @brief Get general server information * @note Route: /info, Method: GET - * @param no - * @return JSON object with basic server information (version, services, etc.) + * @return string JSON object with basic server information (version, services, etc.) */ $app->get('/info', function() { $hasOglive = false; @@ -259,7 +209,7 @@ $app->get('/info', function() { } // Getting actived services. @$services = parse_ini_file('/etc/default/opengnsys'); - $response->services = Array(); + $response->services = []; if (@$services["RUN_OGADMSERVER"] === "yes") { array_push($response->services, "server"); $hasOglive = true; @@ -280,10 +230,10 @@ $app->get('/info', function() { /** * @brief Get the server status * @note Route: /status, Method: GET - * @param no - * @return JSON object with all data collected from server status (RAM, %CPU, etc.). + * @return string JSON object with all data collected from server status (RAM, %CPU, etc.). */ $app->get('/status', function() { + $response = []; // Getting memory and CPU information. exec("awk '$1~/Mem/ {print $2}' /proc/meminfo",$memInfo); $memInfo = array("total" => $memInfo[0], "used" => $memInfo[1]); |