From aa4d378e801f1e2731990813a6020c50e9429daa Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 27 Jan 2017 20:56:02 +0100 Subject: [PATCH] Renamed and logged api:sync --- lib/api/request | 3 - lib/api/source/request.php | 109 ------------------------------------- lib/api/source/sync.php | 45 ++++++++++++--- lib/include/const | 2 + 4 files changed, 38 insertions(+), 121 deletions(-) delete mode 100755 lib/api/request delete mode 100755 lib/api/source/request.php diff --git a/lib/api/request b/lib/api/request deleted file mode 100755 index 1fc33c7..0000000 --- a/lib/api/request +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -php $(realpath $(dirname $0))/source/request.php; diff --git a/lib/api/source/request.php b/lib/api/source/request.php deleted file mode 100755 index c006473..0000000 --- a/lib/api/source/request.php +++ /dev/null @@ -1,109 +0,0 @@ - POST */ - curl_setopt($curl, CURLOPT_POST, true); - - - - /* [3] Manage post data - =========================================================*/ - /* (1) Set post data */ - $postarray = [ - 'token' => $hash, - 'data' => $data - ]; - - /* (2) Add renew if renew */ - if( strlen($new) == 128 ) - $postarray['renew'] = $new; - - /* (3) Parse postfiels to multipart format */ - $postraw = "--$boundary"; - - foreach($postarray as $postkey=>$postvalue) - $postraw .= "\r\ncontent-disposition: form-data; name=\"$postkey\"\r\n\r\n$postvalue\r\n--$boundary"; - - $postraw .= "--"; - - - /* (4) Set postdata raw to curl */ - curl_setopt($curl, CURLOPT_POSTFIELDS, $postraw); - - - - /* [4] Manage headers - =========================================================*/ - curl_setopt($curl, CURLOPT_HTTPHEADER, [ - "Content-Type: multipart/form-data; boundary=$boundary", - "Content-Length: ".strlen($postraw) - ]); - - - /* [5] Send and catch request response - =========================================================*/ - /* (1) Send and catch response */ - $response = curl_exec($curl); - - /* (2) Close request */ - curl_close($curl); - - /* (3) Return response as result */ - if( $response === false ) - return 127; - - return $response; - - } - - echo api_request(); - - - -?> diff --git a/lib/api/source/sync.php b/lib/api/source/sync.php index 8ac3bda..084dfca 100755 --- a/lib/api/source/sync.php +++ b/lib/api/source/sync.php @@ -2,42 +2,52 @@ require_once __DIR__.'/../../include/const'; - function api_sync(){ + function api_request(){ /* [1] Fetch & generate useful data =========================================================*/ /* (1) Fetch target url */ $url = @file_get_contents(URL_CONF); - if( $url === false ) + if( $url === false ){ + slog("Cannot find server's api url", 'api:sync'); return 127; + } /* (2) Fetch cyclic hash */ $hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/hash'); - if( strlen($hash) != 128 ) + if( strlen($hash) != 128 ){ + slog("Wrong cyclic-hash:hash hash length (".strlen($hash).")", 'api:sync'); return 127; + } /* (3) Try new hash if available */ $new = syscall(SOURCE_DIR.'/lib/cyclic-hash/new'); - if( $new === false ) + if( $new === false ){ + slog("cyclic-hash:new returned $new EXIT_STATUS", 'api:sync'); return 127; + } /* (4) Decrement the hash */ $decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr'); - if( $decr === false ) + if( $decr === false ){ + slog("cyclic-hash:decr returned $decr EXIT_STATUS", 'api:sync'); return 127; + } /* (5) Generate the multipart boundary */ $boundary = 'boundary--'.hash('sha512', uniqid()).'--boundary'; /* (6) Fetch data */ - $data = json_decode(syscall(SOURCE_DIR.'/lib/api/fetchdata')); + $data = json_decode(syscall(SOURCE_DIR.'/lib/api/fetch')); - if( is_null($data) ) + if( is_null($data) ){ + slog("api:fetch returned unreadable content", 'api:sync'); $data = "{}"; + } @@ -95,11 +105,28 @@ curl_close($curl); /* (3) Return response as result */ - return $response; + if( $response === false ){ + slog("Request error", 'api:sync'); + return 127; + } + + + + + /* [6] Response management + =========================================================*/ + /* (1) Writes request to temporary pipe */ + file_put_contents(TMP_DIR.'/api.response', $response); + + /* (2) Manage response deployement */ + $deploy = syscall(SOURCE_DIR.'/lib/api/deploy'); + + /* (3) Return state */ + return ($deploy === true); } - echo api_sync(); + echo api_request(); diff --git a/lib/include/const b/lib/include/const index b7e12a5..d496fa0 100755 --- a/lib/include/const +++ b/lib/include/const @@ -7,10 +7,12 @@ #define('LOG_DIR', ROOT_DIR.'/log'); #define('CONF_DIR', ROOT_DIR.'/conf'); #define('SOURCE_DIR', ROOT_DIR.'/source'); + #define('TMP_DIR', ROOT_DIR.'/tmp'); define('ROOT_DIR', '/home/xdrm-brackets/SANDBOX/sats-local'); define('LOG_DIR', ROOT_DIR.'/log'); define('CONF_DIR', ROOT_DIR.'/conf'); + define('TMP_DIR', ROOT_DIR.'/tmp'); define('SOURCE_DIR', '/home/xdrm-brackets/Desktop/git.xdrm.io/logauth-sats'); # CONFIGURATION FILES