diff --git a/quovadis/quovadis.php b/quovadis/quovadis.php index 96c824bb3aee798ea1cdcd2719367d26dbfd80c4..225fba361ad358f6b3d97b04f022817f74a73274 100644 --- a/quovadis/quovadis.php +++ b/quovadis/quovadis.php @@ -4,7 +4,7 @@ ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); -require_once 'config.php'; +require_once('config.php'); use Ramsey\Uuid\Uuid; use RestService\RestService; @@ -13,14 +13,35 @@ function check_username($username) { return is_array(posix_getpwnam($username)); } -function get_desec_txt($name) { - $restService = new RestService(); -// $response = $restService -// ->setEndpoint('https://jsonplaceholder.typicode.com') -// ->get('/posts/1'); -// curl https://desec.io/api/v1/domains/{name}/rrsets/{subname}/{type}/ \ -// --header "Authorization: Token {token}" +/** + * Remove the first and last quote from a quoted string of text + * + * From https://stackoverflow.com/questions/9734758/remove-quotes-from-start-and-end-of-string-in-php/25353877#25353877 + * @param mixed $text + * */ +function stripQuotes($text) { + return preg_replace('/^(\'(.*)\'|"(.*)")$/', '$2$3', $text); +} + +function get_desec_txt($config, $name) { + $restService = new RestService(); + $path = "/api/v1/domains/" . $config['QV_DOMAIN'] . "/rrsets/" . $name . "/TXT/"; + $auth_string = "Token ". $config['QV_DESEC_API']; + + $response = $restService + ->setEndpoint('https://desec.io') + ->setRequestHeaders([ + 'Authorization' => $auth_string + ]) + ->get($path, [], [], false); + + $result = json_decode($response->getBody(), true); + if (array_key_exists('records', $result)) { + return stripQuotes($result['records'][0]); + } else { + return ""; + } } function check_api_key($api_key) { @@ -72,8 +93,11 @@ $leaf = new Leaf\App; // Add routes $leaf->get('/', function () use($leaf) { + $config=get_config(); + $r = get_desec_txt($config, "mtearle-test"); // since the response object is directly tied to the leaf instance - $leaf->response()->markup('<h5>My first Leaf app</h5>'); + $html = '<h5>My first Leaf app</h5>' . $r; + $leaf->response()->markup($html); }); $leaf->post('/register', function () use($leaf) {