From 357352b27302b3fef93367cc617647def09b04a0 Mon Sep 17 00:00:00 2001 From: ramon Date: Fri, 10 Feb 2017 13:30:31 +0000 Subject: #708: Obtener datos y código HTTP en llamadas {{{curl_multi}}} de PHP; usar fecha en formato ISO 8601 y quitar líneas en blanco al final de ficheros. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://opengnsys.es/svn/branches/version1.1@5185 a21b9725-9963-47de-94b9-378ad31fedc9 --- admin/WebConsole/comandos/gestores/gestor_Comandos.php | 12 +++++------- admin/WebConsole/includes/restfunctions.php | 17 ++++++++++------- admin/WebConsole/principal/sondeo.php | 10 +++++----- admin/WebConsole/rest/common.php | 18 ++++++++++++------ admin/WebConsole/rest/index.php | 1 - admin/WebConsole/rest/remotepc.php | 4 +++- admin/WebConsole/rest/server.php | 4 ++-- 7 files changed, 37 insertions(+), 29 deletions(-) diff --git a/admin/WebConsole/comandos/gestores/gestor_Comandos.php b/admin/WebConsole/comandos/gestores/gestor_Comandos.php index 32bb6ae0..f81bea95 100644 --- a/admin/WebConsole/comandos/gestores/gestor_Comandos.php +++ b/admin/WebConsole/comandos/gestores/gestor_Comandos.php @@ -230,13 +230,11 @@ if($sw_ejya=='on' || $sw_ejprg=="on" ){ // Launch concurrent requests. $responses = multiRequest($urls); // Process responses array (IP as array index). - foreach ($responses as $ip => $data) { - if (isset($data)) { - $status = json_decode($data); - if (!isset($status->error)) { - $ipsuccess .= "'".$ip."',"; - $numip++; - } + foreach ($responses as $ip => $resp)) { + // Check if response code is OK (200). + if ($resp['code'] == 200)) { + $ipsuccess .= "'".$ip."',"; + $numip++; } } // quitamos último carácter ',' diff --git a/admin/WebConsole/includes/restfunctions.php b/admin/WebConsole/includes/restfunctions.php index 8bbd19c4..93ef3e84 100644 --- a/admin/WebConsole/includes/restfunctions.php +++ b/admin/WebConsole/includes/restfunctions.php @@ -1,16 +1,17 @@ false, CURLOPT_SSL_VERIFYPEER => false)) { // array of curl handles $curly = array(); - // data to be returned + // Data to be returned (response data and code) $result = array(); // multi handle @@ -25,6 +26,7 @@ function multiRequest($data, $options=array(CURLOPT_SSL_VERIFYHOST => false, CUR $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d; curl_setopt($curly[$id], CURLOPT_URL, $url); + // HTTP headers? if (is_array($d) && !empty($d['header'])) { curl_setopt($curly[$id], CURLOPT_HTTPHEADER, $d['header']); } else { @@ -56,9 +58,10 @@ function multiRequest($data, $options=array(CURLOPT_SSL_VERIFYHOST => false, CUR } while($running > 0); - // get content and remove handles + // Get content and HTTP code, and remove handles foreach($curly as $id => $c) { - $result[$id] = curl_multi_getcontent($c); + $result[$id]['data'] = curl_multi_getcontent($c); + $result[$id]['code'] = curl_getinfo($c, CURLINFO_HTTP_CODE); curl_multi_remove_handle($mh, $c); } diff --git a/admin/WebConsole/principal/sondeo.php b/admin/WebConsole/principal/sondeo.php index 793ecbd6..27504587 100644 --- a/admin/WebConsole/principal/sondeo.php +++ b/admin/WebConsole/principal/sondeo.php @@ -80,13 +80,13 @@ // Launch concurrent requests. $responses = multiRequest($urls); // Process responses array (IP as array index). - foreach ($responses as $ip => $data) { - if (isset($data)) { - $status = json_decode($data); + foreach ($responses as $ip => $resp)) { + if (isset($resp['data'])) { + $data = json_decode($resp['data']); // If user session is oppened, then append "S" to client status. - if (isset($status->status) and isset($status->loggedin)) { + if (isset($data->status) and isset($data->loggedin)) { // Output format: IP1/Status1;... - echo "$ip/".$status->status.($status->loggedin?"S;":";"); + echo "$ip/".$data->status.($data->loggedin?"S;":";"); } } } diff --git a/admin/WebConsole/rest/common.php b/admin/WebConsole/rest/common.php index 0734739f..0bb28da3 100644 --- a/admin/WebConsole/rest/common.php +++ b/admin/WebConsole/rest/common.php @@ -13,8 +13,6 @@ // Common functions. - - /** * @brief Compose JSON response. * @param int status Status code for HTTP response. @@ -129,19 +127,27 @@ function sendCommand($serverip, $serverport, $reqframe, &$values) { } } +/** + * @brief Show custom message for "not found" error (404). + */ +$app->notFound(function() { + echo "REST route not found.\n"; + } +); + /** * @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 ) { - // Compose error message (max. 50 characters from error response). - $app->log->error(date(DATE_ATOM) . ': ' . + // Compose error message (truncating long lines). + $app->log->error(date(DATE_ISO8601) . ': ' . $app->getName() . ' ' . $app->response->getStatus() . ': ' . $app->request->getMethod() . ' ' . $app->request->getPathInfo() . ': ' . - substr($app->response->getBody(), 0, 50)); + substr($app->response->getBody(), 0, 100)); } } ); @@ -198,4 +204,4 @@ $app->get('/status', function() { jsonResponse(200, $response); } ); - +?> diff --git a/admin/WebConsole/rest/index.php b/admin/WebConsole/rest/index.php index 2b632fff..32aff3d3 100644 --- a/admin/WebConsole/rest/index.php +++ b/admin/WebConsole/rest/index.php @@ -73,4 +73,3 @@ $app->get('/', $app->run(); ?> - diff --git a/admin/WebConsole/rest/remotepc.php b/admin/WebConsole/rest/remotepc.php index 35513eee..60ea8751 100644 --- a/admin/WebConsole/rest/remotepc.php +++ b/admin/WebConsole/rest/remotepc.php @@ -77,7 +77,7 @@ EOD; // Check client's status. $ogagent[$clntip]['url'] = "https://$clntip:8000/opengnsys/status"; $result = multiRequest($ogagent); - if (empty($result[$clntip])) { + if (empty($result[$clntip]['data'])) { // Client is off, send a boot command to ogAdmServer. $reqframe = "nfn=Arrancar\r". "ido=$clntid\r". @@ -91,6 +91,7 @@ EOD; $ogagent[$clntip]['header'] = Array("Authorization: ".$agentkey); $result = multiRequest($ogagent); // ... (check response) + //if ($result[$clntip]['code'] != 200) { // ... } // DB Transaction: mark choosed client as reserved and @@ -300,6 +301,7 @@ EOD; $ogagent[$clntip]['header'] = Array("Authorization: ".$agentkey); $result = multiRequest($ogagent); // ... (check response) + //if ($result[$clntip]['code'] != 200) { // ... // Confirm operation. jsonResponse(200, ""); diff --git a/admin/WebConsole/rest/server.php b/admin/WebConsole/rest/server.php index b1742566..4b275870 100644 --- a/admin/WebConsole/rest/server.php +++ b/admin/WebConsole/rest/server.php @@ -617,12 +617,12 @@ EOD; // If no data, check OGAgent API connection. $url = "https://$clientip:8000/opengnsys/status"; $result = multiRequest(Array($url)); - if (empty($result[0])) { + if (empty($result[0]['data'])) { // Client is off. $response['status'] = $status['OFF']; } else { // Get status and session data. - $data = json_decode($result[0]); + $data = json_decode($result[0]['data']); if (isset($status[$data->status])) { $response['status'] = $status[$data->status]; $response['loggedin'] = $data->loggedin; -- cgit v1.2.3-18-g5258