diff --git a/base-php-client/.gitignore b/base-php-client/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..35ce444443af38e7cef6092467ef08cf94e330e2 --- /dev/null +++ b/base-php-client/.gitignore @@ -0,0 +1,2 @@ +settings.php + diff --git a/base-php-client/README.md b/base-php-client/README.md new file mode 100644 index 0000000000000000000000000000000000000000..3196bf69588056dca235e6f3c6a52d23f57ab2b6 --- /dev/null +++ b/base-php-client/README.md @@ -0,0 +1,26 @@ +ΞιβλιοθΞ�ΞΊΞ· ΞΊΞ±ΞΉ ΟΟΟΞ³ΟΞ±ΞΌΞΌΞ± ΞΊΞ±ΟανάλΟΟΞ·Ο +==================================== + +# ΞΞ΄Ξ·Ξ³Ξ―Ξ΅Ο + +ΞΞ½ΟΞΉΞ³ΟΞ¬ΟΟΞ΅ ΟΞΏ Ξ±ΟΟΡίο `settings.php.dist` ΟΞ΅ ΞΞ½Ξ± Ξ½ΞΞΏ Ξ±ΟΟΡίο `settings.php` ΞΊΞ±ΞΉ +ΟΟΞΏΟΞΏΟΞΏΞΉΞ�ΟΟΞ΅ Ξ±Ξ½Ξ±Ξ»ΟΞ³ΟΟ, Ξ³ΞΉΞ± ΟΞ±ΟάδΡιγμα ΡιΟάγονΟΞ±Ο ΟΞΏ ΟΞ½ΞΏΞΌΞ± ΟΟΞ�ΟΟΞ· ΟΞΏΟ ΟΞ±Ο ΞΟΡι +Ξ±ΟοδοθΡί. + +# Ξ ΟΟΞ³ΟΞ±ΞΌΞΌΞ± Ξ΅ΟΞ―Ξ΄Ξ΅ΞΉΞΎΞ·Ο + +ΞΞΉΞ± ΟΞ·Ξ½ Ξ΅ΟίδΡιξη ΟΟΞ½ διαθΞΟΞΉΞΌΟΞ½ λΡιΟΞΏΟ ΟΞ³ΞΉΟΞ½ ΞΟΡι Ξ±Ξ½Ξ±ΟΟΟ ΟθΡί ΟΟΟΞ³ΟΞ±ΞΌΞΌΞ± +[index.php](index.php) ΟΞΏΟ ΞΌΟΞΏΟΡίΟΞ΅ Ξ½Ξ± ΡκΟΡλΞΟΞ΅ΟΞ΅ Ξ±ΟΟ ΟΞ· Ξ³ΟΞ±ΞΌΞΌΞ� ΡνΟΞΏΞ»ΟΞ½. +΀ο ΟΟΟΞ³ΟΞ±ΞΌΞΌΞ± Ξ΄ΞΟΞ΅ΟΞ±ΞΉ ΟΞΉΟ ΟΞ±ΟΞ±ΞΊΞ¬ΟΟ ΟΞ±ΟΞ±ΞΌΞΟΟΞΏΟ Ο: + +``` +Ξ§ΟΞ�ΟΞ·: index.php [--list] [--listshow] [--send <file>] [--show <hashid>] [--get <hashid>] [--save <hashid>] + list: λίΟΟΞ± hashids ΟΟΞ½ ΡγγΟΞ¬ΟΟΞ½ + listshow: λίΟΟΞ± Ξ±ΟΟΡίΟΞ½ + send <file>: ΞΊΞ±ΟΞ±ΟΟΟΞ·ΟΞ· ΟΟΟΟΞΏΞΊΟΞ»Ξ»ΞΏΟ ΞΌΞ΅ Ξ±ΟΞΏΟΟΞΏΞ»Ξ� ΟΞΏΟ Ξ±ΟΟΞ΅Ξ―ΞΏΟ file + show <hash>: Ξ±Ξ½Ξ±Ξ»Ο ΟΞΉΞΊΞΟ ΟληΟΞΏΟΞΏΟΞ―Ξ΅Ο Ξ±ΟΟΞ΅Ξ―ΞΏΟ ΞΌΞ΅ δΡδομΞΞ½ΞΏ hashid + get <hash>: Ξ»Ξ�ΟΞ· Ξ±ΟΟΞ΅Ξ―ΞΏΟ ΞΌΞ΅ δΡδομΞΞ½ΞΏ hashid + save <hash>: Ξ»Ξ�ΟΞ· ΞΊΞ±ΞΉ Ξ±ΟΞΏΞΈΞ�ΞΊΞ΅Ο ΟΞ· Ξ±ΟΟΞ΅Ξ―ΞΏΟ ΞΌΞ΅ δΡδομΞΞ½ΞΏ hashid + ΟΟΞΏΞ½ ΟΟΞΟΞΏΞ½ΟΞ± ΟάκΡλο ΞΊΞ±ΞΉ ΞΌΞ΅ ΟΞΏ ΟΞ½ΞΏΞΌΞ± Ξ±ΟΟΞ΅Ξ―ΞΏΟ ΟΞΏΟ Ξ΅ΟΞΉΟΟΟΞΟΞ΅ΟΞ±ΞΉ +``` + diff --git a/base-php-client/index.php b/base-php-client/index.php new file mode 100644 index 0000000000000000000000000000000000000000..6757110b6b0689b58cae7cd45b02d79ee0833b01 --- /dev/null +++ b/base-php-client/index.php @@ -0,0 +1,75 @@ +<?php +// barebone app - autoload classes from src/ dir +spl_autoload_register(function ($class_name) { + $class_name_parts = explode('\\', $class_name); + $class_filename = __DIR__ . '/src/' . end($class_name_parts) . '.php'; + if (file_exists($class_filename)) { + include $class_filename; + if (class_exists($class_name)) { + return true; + } + } + return false; +}); + +use Gr\Gov\Minedu\Osteam\App; + +$settings = require(__DIR__ . '/settings.php'); + +$app = new App($settings); +$app->setDebug(true); + +/** + * ΞΞ�ΟΞ· ΟΞ±ΟΞ±ΞΌΞΟΟΟΞ½ ΞΊΞ±ΞΈΞΏΟΞΉΟΞΌΞΏΟ Ξ»Ξ΅ΞΉΟΞΏΟ ΟΞ³Ξ―Ξ±Ο Ξ±ΟΟ ΟΞ· Ξ³ΟΞ±ΞΌΞΌΞ� ΡνΟΞΏΞ»ΟΞ½ + */ +$options = getopt('', ['send:', 'list', 'listshow', 'show:', 'get:', 'save:']); + +$send = isset($options['send']); +$list = isset($options['list']) || isset($options['listshow']); +$listdetails = isset($options['listshow']); +$show = isset($options['show']); +$get = isset($options['get']) || isset($options['save']); +$save = isset($options['save']); + +/** + * ΞλΡγΟΞΏΟ ΟΞ±ΟΞ±ΞΌΞΟΟΟΞ½ + */ +if (!$send && !$list && !$show && !$get) { + echo "Ξ§ΟΞ�ΟΞ·: {$argv[0]} [--list] [--listshow] [--send <file>] [--show <hashid>] [--get <hashid>] [--save <hashid>]", PHP_EOL, + " list: λίΟΟΞ± hashids ΟΟΞ½ ΡγγΟΞ¬ΟΟΞ½", PHP_EOL, + " listshow: λίΟΟΞ± Ξ±ΟΟΡίΟΞ½", PHP_EOL, + " send <file>: ΞΊΞ±ΟΞ±ΟΟΟΞ·ΟΞ· ΟΟΟΟΞΏΞΊΟΞ»Ξ»ΞΏΟ ΞΌΞ΅ Ξ±ΟΞΏΟΟΞΏΞ»Ξ� ΟΞΏΟ Ξ±ΟΟΞ΅Ξ―ΞΏΟ file", PHP_EOL, + " show <hash>: Ξ±Ξ½Ξ±Ξ»Ο ΟΞΉΞΊΞΟ ΟληΟΞΏΟΞΏΟΞ―Ξ΅Ο Ξ±ΟΟΞ΅Ξ―ΞΏΟ ΞΌΞ΅ δΡδομΞΞ½ΞΏ hashid", PHP_EOL, + " get <hash>: Ξ»Ξ�ΟΞ· Ξ±ΟΟΞ΅Ξ―ΞΏΟ ΞΌΞ΅ δΡδομΞΞ½ΞΏ hashid", PHP_EOL, + " save <hash>: Ξ»Ξ�ΟΞ· ΞΊΞ±ΞΉ Ξ±ΟΞΏΞΈΞ�ΞΊΞ΅Ο ΟΞ· Ξ±ΟΟΞ΅Ξ―ΞΏΟ ΞΌΞ΅ δΡδομΞΞ½ΞΏ hashid", PHP_EOL, + " ΟΟΞΏΞ½ ΟΟΞΟΞΏΞ½ΟΞ± ΟάκΡλο ΞΊΞ±ΞΉ ΞΌΞ΅ ΟΞΏ ΟΞ½ΞΏΞΌΞ± Ξ±ΟΟΞ΅Ξ―ΞΏΟ ΟΞΏΟ Ξ΅ΟΞΉΟΟΟΞΟΞ΅ΟΞ±ΞΉ", PHP_EOL, + exit(0); +} +if ($send) { + echo "ΞλΡγΟΞΏΟ Ξ³ΞΉΞ± ΟΞΏ Ξ±ΟΟΡίο {$options['send']}... "; + if (is_readable($options['send'])) { + $file = base64_encode(file_get_contents($options['send'])); + if ($file === false) { + echo PHP_EOL, "ΞΞΞΞΞ£: ΞΞ΄Ο Ξ½Ξ±ΞΌΞ―Ξ± ΞΊΟδικοΟΞΏΞ―Ξ·ΟΞ·Ο ΟΞΏΟ Ξ±ΟΟΞ΅Ξ―ΞΏΟ .", PHP_EOL; + } + echo "OK", PHP_EOL; + } else { + echo PHP_EOL, "ΞΞΞΞΞ£: ΀ο Ξ±ΟΟΡίο δΡν Ρίναι Ξ±Ξ½Ξ±Ξ³Ξ½ΟΟΞΉΞΌΞΏ.", PHP_EOL; + exit(-1); + } +} + +/** + * If any work is to be done, an api key is required + */ +echo "ΞνάκΟΞ·ΟΞ· API key...", PHP_EOL; +try { + $apikey = $app->getApiKey(); + echo "΀ο API key Ρίναι: ", $apikey, PHP_EOL; +} catch (\Exception $e) { + echo 'ΞΞΞΞΞ£: ΞΞ΄Ο Ξ½Ξ±ΞΌΞ―Ξ± ανάκΟΞ·ΟΞ·Ο API key. ', PHP_EOL, $e->getMessage(), PHP_EOL; + exit(-1); +} + +echo "Done.", PHP_EOL; +exit(0); diff --git a/base-php-client/settings.php.dist b/base-php-client/settings.php.dist new file mode 100644 index 0000000000000000000000000000000000000000..facaf06cd3acc9c9f9c08aca51aee68f086bbfe2 --- /dev/null +++ b/base-php-client/settings.php.dist @@ -0,0 +1,8 @@ +<?php +return [ + // username and password to authenticate + 'username' => 'test', + 'password' => '123456', + // sender id for test lookups + 'sender_id' => 100000001 +]; diff --git a/base-php-client/src/App.php b/base-php-client/src/App.php new file mode 100644 index 0000000000000000000000000000000000000000..ba1824318892cde361a700425bfbc8d13e013287 --- /dev/null +++ b/base-php-client/src/App.php @@ -0,0 +1,102 @@ +<?php +// +namespace Gr\Gov\Minedu\Osteam; + +use Gr\Gov\Minedu\Osteam\Client; + +/** + * Description of app + * + * @author spapad + */ +class App +{ + + private $client = null; + private $_settings = []; + private $_debug = false; + + /** + * + * @param array $settings is an array of configuration parameters + */ + function __construct($settings = []) + { + $this->_settings = array_merge($this->_settings, $settings); + $this->client = new Client(); + } + + protected function setting($name) + { + return (isset($this->_settings["$name"]) ? + $this->_settings["$name"] : + null); + } + + /** + * + * @return string The api key + * @throws \Swagger\Client\ApiException + */ + public function getApiKey() + { + // debug? + + $payload = json_encode([ + 'username' => $this->setting('username'), + 'password' => $this->setting('password') + ]); + + $response = json_decode($this->client->pauth($payload)); + return $response->apiKey; + } + + public function searchDocuments($apikey = null) + { + + } + + public function getDocData($hashid, $apikey = null) + { + + } + + public function submit($submission_data, $apikey = null) + { + + } + + public function getPdf($hashid, $apikey = null) + { + + } + + public function savePdf($hashid, $apikey = null) + { + + } + + /** + * Try to return the object fields in a hash array + * + * @param object $resultobj The object + * @return type + */ + public function apiResultObjAsArray($resultobj) + { + $result = []; + if (method_exists($resultobj, 'getters')) { + $getters = $resultobj->getters(); + foreach ($getters as $property => $callfunc) { + $result[$property] = $resultobj->$callfunc(); + } + } + return $result; + } + + public function setDebug($debug = true) + { + $this->_debug = ($debug === true); + return; + } +} diff --git a/base-php-client/src/Client.php b/base-php-client/src/Client.php new file mode 100644 index 0000000000000000000000000000000000000000..ff5cb7ec5e75f6d9051bb77237bccb52c9b6a465 --- /dev/null +++ b/base-php-client/src/Client.php @@ -0,0 +1,100 @@ +<?php +/* + * + */ +namespace Gr\Gov\Minedu\Osteam; + +use Exception; + +/** + * Description of Client + * + * @author spapad + */ +class Client +{ + + private $_debug = false; + private $_settings = [ + 'base_uri' => 'https://protocoltest.minedu.gov.gr:443/openpapyros/api' + ]; + + public function __construct($settings = []) + { + $this->_settings = array_merge($this->_settings, $settings); + } + + public function pauth($payload) + { + $result = $this->put("{$this->_settings['base_uri']}/pauthenticate/pauth", $payload, [ + "Content-Type: application/json", + "Accept: application/json", + ] + ); + return $result; + } + + protected function setCommonCurlOptions($ch, $uri, $headers) + { + curl_setopt($ch, CURLOPT_URL, $uri); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_USERAGENT, "OSTEAM barebone php client"); + + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_MAXREDIRS, 3); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); + if ($this->_debug === true) { + curl_setopt($ch, CURLOPT_VERBOSE, true); + } + } + + public function put($uri, $payload, $headers = []) + { + $ch = curl_init(); + + $this->setCommonCurlOptions($ch, $uri, $headers); + + // curl_setopt($ch, CURLOPT_PUT, true); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); + curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $result = curl_exec($ch); + + if (curl_errno($ch)) { + throw new Exception("ΞΞ¬ΞΈΞΏΟ ΞΊΞ±ΟΞ¬ ΟΞ·Ξ½ ΞΊΞ»Ξ�ΟΞ· ΟΞΏΟ {$uri}. Curl error: " . curl_error($ch) . " Curl info: ", var_export(curl_getinfo($ch), true)); + } + if (intval(($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) / 100) != 2) { + // ΟΟΞ±Ξ³ΞΌΞ±ΟΞΏΟΞΏΞΉΞ�ΞΈΞ·ΞΊΞ΅ ΞΊΞ»Ξ�ΟΞ· αλλά δΡν Ξ�ΟΞ±Ξ½ "Ξ΅ΟΞΉΟΟ ΟΞ�Ο" + throw new Exception("ΞΟΞΏΟΟ ΟΞ·ΞΌΞΞ½Ξ· ΞΊΞ»Ξ�ΟΞ·. HTTP STATUS {$http_code}. Ξ Ξ±ΟάνΟΞ·ΟΞ· Ξ�ΟΞ±Ξ½: {$result}", $http_code); + } + curl_close($ch); + return $result; + } + + public function post($uri, $payload, $headers = []) + { + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $uri); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $result = curl_exec($ch); + + echo "Error posting to {$uri}. Curl error: " . curl_error($ch) . " Curl info: ", var_export(curl_getinfo($ch), true); + if (curl_errno($ch)) { + throw new Exception("Error posting to {$uri}. Curl error: " . curl_error($ch) . " Curl info: ", var_export(curl_getinfo($ch), true)); + } + curl_close($ch); + return $result; + } + + public function setDebug($debug = true) + { + $this->_debug = ($debug === true); + return; + } +}