diff options
author | ramon <ramongomez@us.es> | 2017-02-08 10:50:23 +0000 |
---|---|---|
committer | ramon <ramongomez@us.es> | 2017-02-08 10:50:23 +0000 |
commit | 1ab1b31df17de5c1ebf80826c33299a1bc517151 (patch) | |
tree | 007ed304ba2c0b77bf754f78ffd3393a61a0c03a /admin | |
parent | a26527db3d1288c340fc503beca4dbde52d65131 (diff) |
#708: Reorganizar funciones comunes y registrar errores en fichero {{{error.log}}} de Apache.
git-svn-id: https://opengnsys.es/svn/branches/version1.1@5178 a21b9725-9963-47de-94b9-378ad31fedc9
Diffstat (limited to 'admin')
-rw-r--r-- | admin/WebConsole/rest/common.php | 122 | ||||
-rw-r--r-- | admin/WebConsole/rest/index.php | 6 | ||||
-rw-r--r-- | admin/WebConsole/rest/server.php | 97 |
3 files changed, 123 insertions, 102 deletions
diff --git a/admin/WebConsole/rest/common.php b/admin/WebConsole/rest/common.php index e5cd30fe..66a6db9b 100644 --- a/admin/WebConsole/rest/common.php +++ b/admin/WebConsole/rest/common.php @@ -1,7 +1,7 @@ <?php /** * @file index.php - * @brief OpenGnsys REST API: common routes + * @brief OpenGnsys REST API: common functions and routes * @warning All input and output messages are formatted in JSON. * @note Some ideas are based on article "How to create REST API for Android app using PHP, Slim and MySQL" by Ravi Tamada, thanx. * @license GNU GPLv3+ @@ -11,14 +11,16 @@ */ -// Auxiliar functions. +// Common functions. + + /** * @brief Compose JSON response. * @param int status Status code for HTTP response. * @param array response Response data. * @return string JSON response. - */ + */ function jsonResponse($status, $response) { $app = \Slim\Slim::getInstance(); // HTTP status code. @@ -29,6 +31,120 @@ function jsonResponse($status, $response) { echo json_encode($response); } +/** + * @brief Validate API key included in "Authorization" HTTP header. + * @return JSON response on error. + */ +function validateApiKey() { + global $cmd; + global $userid; + $response = array(); + $app = \Slim\Slim::getInstance(); + // Read Authorization HTTP header. + $headers = apache_request_headers(); + if (! empty($headers['Authorization'])) { + // Assign user id. that match this key to global variable. + $apikey = htmlspecialchars($headers['Authorization']); + $cmd->texto = "SELECT idusuario + FROM usuarios + WHERE apikey='$apikey' LIMIT 1"; + $rs=new Recordset; + $rs->Comando=&$cmd; + if ($rs->Abrir()) { + $rs->Primero(); + if (!$rs->EOF){ + // Fetch user id. + $userid = $rs->campos["idusuario"]; + } else { + // Credentials error. + $response['message'] = 'Login failed. Incorrect credentials'; + jsonResponse(401, $response); + $app->stop(); + } + $rs->Cerrar(); + } else { + // Access error. + $response['message'] = "An error occurred, please try again"; + jsonResponse(500, $response); + } + } else { + // Error: missing API key. + $response['message'] = 'Missing API key'; + jsonResponse(400, $response); + $app->stop(); + } +} + +/** + * @brief Check if parameter is set and print error messages if empty. + * @param string param Parameter to check. + * @return boolean "false" if parameter is null, otherwise "true". + */ +function checkParameter($param) { + if (isset($param)) { + return true; + } else { + // Print error message. + $response['message'] = 'Parameter not found'; + jsonResponse(400, $response); + return false; + } +} + +/** + * @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 Hook to write an error log message. + * @warning Message will be written in web server's error file. + */ +$app->hook('slim.after', function() use ($app) { + if ($app->response->getStatus() != 200 ) { + $app->log->error(date(DATE_ATOM) . ': ' . + $app->getName() . ' ' . + $app->response->getStatus() . ': ' . + $app->request->getPathInfo() . ': ' . + substr($app->response->getBody(), 0, 50)); + } + } +); + + // Common routes. /** diff --git a/admin/WebConsole/rest/index.php b/admin/WebConsole/rest/index.php index 00a13571..2b632fff 100644 --- a/admin/WebConsole/rest/index.php +++ b/admin/WebConsole/rest/index.php @@ -32,10 +32,12 @@ if (!$cmd) die("Access Error"); // Install Slim application (development mode). -//$app = new \Slim\Slim(array('mode' => 'production', 'debug' => false)); +//$app = new \Slim\Slim(array('mode' => 'production', ... ); $app = new \Slim\Slim(array( 'mode' => 'development', - 'debug' => true)); + 'log.enabled' => true, + 'log.level' => \Slim\Log::ERROR, + 'debug' => false)); $app->setName('opengnsys'); // Global variables. diff --git a/admin/WebConsole/rest/server.php b/admin/WebConsole/rest/server.php index 8119e519..b1742566 100644 --- a/admin/WebConsole/rest/server.php +++ b/admin/WebConsole/rest/server.php @@ -14,66 +14,6 @@ // Auxiliar functions. /** - * @brief Validate API key included in "Authorization" HTTP header. - * @return JSON response on error. - */ -function validateApiKey() { - global $cmd; - global $userid; - $response = array(); - $app = \Slim\Slim::getInstance(); - // Read Authorization HTTP header. - $headers = apache_request_headers(); - if (! empty($headers['Authorization'])) { - // Assign user id. that match this key to global variable. - $apikey = htmlspecialchars($headers['Authorization']); - $cmd->texto = "SELECT idusuario - FROM usuarios - WHERE apikey='$apikey' LIMIT 1"; - $rs=new Recordset; - $rs->Comando=&$cmd; - if ($rs->Abrir()) { - $rs->Primero(); - if (!$rs->EOF){ - // Fetch user id. - $userid = $rs->campos["idusuario"]; - } else { - // Credentials error. - $response['message'] = 'Login failed. Incorrect credentials'; - jsonResponse(401, $response); - $app->stop(); - } - $rs->Cerrar(); - } else { - // Access error. - $response['message'] = "An error occurred, please try again"; - jsonResponse(500, $response); - } - } else { - // Error: missing API key. - $response['message'] = 'Missing API key'; - jsonResponse(400, $response); - $app->stop(); - } -} - -/** - * @brief Check if parameter is set and print error messages if empty. - * @param string param Parameter to check. - * @return boolean "false" if parameter is null, otherwise "true". - */ -function checkParameter($param) { - if (isset($param)) { - return true; - } else { - // Print error message. - $response['message'] = 'Parameter not found'; - jsonResponse(400, $response); - return false; - } -} - -/** * @brief Check if user is administrator and print error messages if not. * @param int adminid Administrator id. * @return boolean "true" if admin id. is equals to global user id., otherwise "false". @@ -114,43 +54,6 @@ function addClassroomGroup(&$classroomGroups, $rs){ }); } -/** - * @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); - } -} // REST routes. |