From cebfbbec88040cc3468f145f233941f270f511a8 Mon Sep 17 00:00:00 2001 From: Addin Munawwar <91366027+moonawar@users.noreply.github.com> Date: Fri, 17 Nov 2023 11:40:48 +0700 Subject: [PATCH] feat: soap belom bisa (despair) --- Dockerfile | 10 +- app/clients/SoapConsumer.php | 188 ++++++++++++++++++++++++++ app/controllers/TestController.php | 26 ++++ app/controllers/UserController.php | 5 +- app/main/Main.php | 5 +- app/pages/user/MySubscriptionPage.php | 2 - db/seeding/book.py | 4 +- 7 files changed, 230 insertions(+), 10 deletions(-) create mode 100644 app/clients/SoapConsumer.php create mode 100644 app/controllers/TestController.php diff --git a/Dockerfile b/Dockerfile index afb87c0..989e8b9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,10 @@ WORKDIR /var/www/html # PHP extension and Apache configuration RUN docker-php-ext-install mysqli RUN a2enmod rewrite -RUN apt-get -y update ; \ - apt-get -y upgrade ; \ - apt-get -y install ffmpeg ; \ \ No newline at end of file +RUN apt-get update \ + && apt-get install -y \ + libxml2-dev \ + && rm -rf /var/lib/apt/lists/* + +# Enable SOAP extension +RUN docker-php-ext-install soap \ No newline at end of file diff --git a/app/clients/SoapConsumer.php b/app/clients/SoapConsumer.php new file mode 100644 index 0000000..02be5ae --- /dev/null +++ b/app/clients/SoapConsumer.php @@ -0,0 +1,188 @@ +<? + +class SoapConsumer { + private SoapClient $subscriptionClient; + private SoapClient $requestClient; + private $apiKey; + + public function __construct() { + // Set SOAP client options + $options = [ + 'trace' => 1, // Allows you to trace request and response + 'exceptions' => true, // Enables exceptions for HTTP errors + "encoding" => "UTF-8", + "stream_context" => stream_context_create([ + 'http' => [ + 'ApiKey' => $_ENV['SOAP_API_KEY'] + ] + ]) + ]; + + $this->subscriptionClient = new SoapClient($_ENV['SOAP_URL'] . "subscription?wsdl", $options); + $this->requestClient = new SoapClient($_ENV['SOAP_URL'] . "request?wsdl", $options); + + $this->apiKey = $_ENV['SOAP_API_KEY']; + + $headerBody = new SoapVar($this->apiKey, XSD_STRING, null, null, 'ApiKey', 'http://ws.soap.com/'); + $header = new SoapHeader('http://ws.soap.com/', 'ApiKey', $headerBody); + + $this->subscriptionClient->__setSoapHeaders(array( + new SoapHeader( + 'http://ws.soap.com/', + 'ApiKey', + $this->apiKey, + false + ), + new SoapHeader( + 'ns2', + 'ApiKey', + $this->apiKey, + false + ), + new SoapHeader( + 'ns1', + 'ApiKey', + $this->apiKey, + false + ), + $header + )); + + $this->subscriptionClient->__setSoapHeaders($header); + + $this->requestClient->__setSoapHeaders( + new SoapHeader( + 'http://ws.soap.com/', + 'ApiKey', + $this->apiKey, + false + ) + ); + + } + + public function makeRequest($requestBy, $to, $requesterEmail) { + $response = $this->requestClient->MakeRequest([ + 'RequestBy' => $requestBy, + 'To' => $to, + 'RequesterEmail' => $requesterEmail, + ]); + + + + return $response; + + // $apiKey = $this->apiKey; + + // // Construct SOAP request XML + // $requestXml = <<<XML + // <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> + // <Header> + // <ApiKey xmlns="http://ws.soap.com/">$apiKey</ApiKey> + // </Header> + // <Body> + // <MakeRequest xmlns="http://ws.soap.com/"> + // <RequestBy xmlns="">$requestBy</RequestBy> + // <To xmlns="">$to</To> + // <RequesterEmail xmlns="">$requesterEmail</RequesterEmail> + // </MakeRequest> + // </Body> + // </Envelope> + // XML; + + // $endpoint = $_ENV['SOAP_URL'] . "request"; + + // $headers = [ + // 'Content-Type: text/xml; charset=utf-8', + // 'Content-Length: ' . strlen($requestXml), + // 'SOAPAction: http://ws.soap.com/MakeRequest', + // ]; + + // $ch = curl_init($endpoint); + // curl_setopt($ch, CURLOPT_POST, 1); + // curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXml); + // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + // // Send SOAP request + // $responseXml = curl_exec($ch); + // curl_close($ch); + + // return $responseXml; + } + + public function getSubscriptionOf($requester) { + + // Define the SOAP request parameters + $requestParams = array( + 'ApiKey' => $this->apiKey, + 'GetSubscriptionOf' => array( + 'Username' => $requester, + ), + ); + + $res = $this->subscriptionClient->__soapCall('GetSubscriptionOf', array($requestParams)); + + // Process the result + var_dump($res); + + + // $response = $this->subscriptionClient->GetSubscriptionOf([ + // 'Username' => $requester, + // ]); + + // echo htmlentities( $this->subscriptionClient->__getLastRequest()); + + // return $response; + // $apiKey = $this->apiKey; + + // // Construct SOAP request XML + // $requestXml = <<<XML + // <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> + // <Header> + // <ApiKey xmlns="http://ws.soap.com/">$apiKey</ApiKey> + // </Header> + // <Body> + // <GetSubscriptionOf xmlns="http://ws.soap.com/"> + // <Username xmlns="">$requester</Username> + // </GetSubscriptionOf> + // </Body> + // </Envelope> + // XML; + + // $endpoint = $_ENV['SOAP_URL'] . "subscription"; + + // $headers = [ + // 'Content-Type: text/xml; charset=utf-8', + // 'Content-Length: ' . strlen($requestXml), + // 'SOAPAction: http://ws.soap.com/GetSubscriptionOf', + // ]; + + // $ch = curl_init($endpoint); + // curl_setopt($ch, CURLOPT_POST, 1); + // curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXml); + // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + // // Send SOAP request + // $dataString = curl_exec($ch); + // curl_close($ch); + + // // Use regular expression to extract usernames + // preg_match_all('/\+07:00(.*?)true/', $dataString, $matches); + + // // Extract the usernames from the matches array + // $usernames = $matches[1]; + // for ($i = 0; $i < count($usernames); $i++) { + // // if the username started with 2023 + // if (substr($usernames[$i], 0, 4) == "2023") { + // $usernames[$i] = substr($usernames[$i], 29); + // } + // } + + // // Print the result + // echo "usernames = [" . implode(', ', $usernames) . "]\n"; + // return $dataString; + } +} +?> \ No newline at end of file diff --git a/app/controllers/TestController.php b/app/controllers/TestController.php new file mode 100644 index 0000000..b70418e --- /dev/null +++ b/app/controllers/TestController.php @@ -0,0 +1,26 @@ + +<?php + +class TestController extends Controller implements ControllerInterface +{ + private SoapConsumer $soap; + + public function __construct() { + require_once __DIR__ . '/../clients/SOAPConsumer.php'; + $this->soap = new SoapConsumer(); + } + + public function index() { + + } + + public function req() + { + echo $this->soap->makeRequest('addin', 'yusuf', 'test@gmail.com'); + } + + public function sub() + { + echo $this->soap->getSubscriptionOf('a'); + } +} \ No newline at end of file diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php index 9b17add..7898e91 100644 --- a/app/controllers/UserController.php +++ b/app/controllers/UserController.php @@ -27,7 +27,10 @@ class UserController extends Controller implements ControllerInterface switch ($_SERVER['REQUEST_METHOD']) { case 'GET': // show the login page - $mySubscriptionView = $this->view('user', 'MySubscriptionView'); + $data = [ + 'username' => $_SESSION['username'] + ]; + $mySubscriptionView = $this->view('user', 'MySubscriptionView', $data); $mySubscriptionView->render(); break; default: diff --git a/app/main/Main.php b/app/main/Main.php index 4f2c4d8..86d5ac8 100644 --- a/app/main/Main.php +++ b/app/main/Main.php @@ -12,13 +12,14 @@ class Main { $controllerPart = $url[0] ?? null; $controllerPart = ucfirst($controllerPart); - + if (isset($controllerPart) && file_exists(__DIR__ . '/../controllers/' . $controllerPart . 'Controller.php')) { require_once __DIR__ . '/../controllers/' . $controllerPart . 'Controller.php'; $controllerClass = $controllerPart . 'Controller'; $this->controller = new $controllerClass(); }else{ - header('Location: /book/'); + require_once __DIR__ . '/../controllers/NotFoundController.php'; + $this->controller = new NotFoundController(); } unset($url[0]); diff --git a/app/pages/user/MySubscriptionPage.php b/app/pages/user/MySubscriptionPage.php index 3a26f6c..1621d8b 100644 --- a/app/pages/user/MySubscriptionPage.php +++ b/app/pages/user/MySubscriptionPage.php @@ -27,7 +27,6 @@ <table border="1" class="styled-table"> <thead> <tr> - <th>Id</th> <th>Curator Name</th> <th>Action</th> </tr> @@ -38,7 +37,6 @@ foreach ($authors as $author) { echo "<tr>"; - echo "<td>" . $author['author_id'] . "</td>"; echo "<td>" . $author['full_name'] . "</td>"; echo '<td><a href="/author/update/' . $author['author_id'] .'">Pending/Details</a></td>'; echo "</tr>"; diff --git a/db/seeding/book.py b/db/seeding/book.py index 0409975..984c630 100644 --- a/db/seeding/book.py +++ b/db/seeding/book.py @@ -22,8 +22,8 @@ with open('books.csv', newline='', encoding='utf-8') as csvfile: reader = csv.DictReader(csvfile) for row in reader: books.append({ - 'title': row['original_title'], - 'description': f"Buku {row['original_title']} karya {row['authors']} ini menceritakan tentang {row['original_title']}.", + 'title': row['title'], + 'description': f"Buku {row['title']} karya {row['authors']} ini menceritakan tentang {row['title']}.", 'book_image': row['small_image_url'] }) -- GitLab