diff --git a/app/init.php b/app/init.php index 2440da8158..e7f183c2fa 100644 --- a/app/init.php +++ b/app/init.php @@ -23,12 +23,6 @@ use Ahc\Jwt\JWT; use Ahc\Jwt\JWTException; use Appwrite\Extend\Exception; use Appwrite\Auth\Auth; -use Appwrite\SMS\Adapter\Mock; -use Appwrite\SMS\Adapter\Telesign; -use Appwrite\SMS\Adapter\TextMagic; -use Appwrite\SMS\Adapter\Twilio; -use Appwrite\SMS\Adapter\Msg91; -use Appwrite\SMS\Adapter\Vonage; use Appwrite\DSN\DSN; use Appwrite\Event\Audit; use Appwrite\Event\Database as EventDatabase; @@ -47,6 +41,12 @@ use Utopia\Database\ID; use Utopia\Logger\Logger; use Utopia\Config\Config; use Utopia\Locale\Locale; +use Utopia\Messaging\Adapters\SMS\Mock; +use Utopia\Messaging\Adapters\SMS\Msg91; +use Utopia\Messaging\Adapters\SMS\Telesign; +use Utopia\Messaging\Adapters\SMS\TextMagic; +use Utopia\Messaging\Adapters\SMS\Twilio; +use Utopia\Messaging\Adapters\SMS\Vonage; use Utopia\Registry\Registry; use MaxMind\Db\Reader; use PHPMailer\PHPMailer\PHPMailer; diff --git a/app/workers/messaging.php b/app/workers/messaging.php index 8d13919f2e..8373d07ba3 100644 --- a/app/workers/messaging.php +++ b/app/workers/messaging.php @@ -1,16 +1,17 @@ args['recipient']; - $message = $this->args['message']; + $message = new SMS( + to: [$this->args['recipient']], + content: $this->args['message'], + from: $this->from, + ); try { - $this->sms->send($this->from, $recipient, $message); + $this->sms->send($message); } catch (\Exception $error) { throw new Exception('Error sending message: ' . $error->getMessage(), 500); } diff --git a/composer.json b/composer.json index 973aec8539..1583b51d04 100644 --- a/composer.json +++ b/composer.json @@ -43,8 +43,6 @@ "ext-sockets": "*", "appwrite/php-clamav": "1.1.*", "appwrite/php-runtimes": "0.11.*", - "utopia-php/framework": "0.21.*", - "utopia-php/logger": "0.3.*", "utopia-php/abuse": "0.13.*", "utopia-php/analytics": "0.2.*", "utopia-php/audit": "0.14.*", @@ -52,15 +50,18 @@ "utopia-php/cli": "0.13.*", "utopia-php/config": "0.2.*", "utopia-php/database": "0.25.*", - "utopia-php/locale": "0.4.*", - "utopia-php/registry": "0.5.*", - "utopia-php/preloader": "0.2.*", "utopia-php/domains": "1.1.*", - "utopia-php/swoole": "0.3.*", - "utopia-php/storage": "0.11.*", - "utopia-php/websocket": "0.1.0", + "utopia-php/framework": "0.21.*", "utopia-php/image": "0.5.*", + "utopia-php/locale": "0.4.*", + "utopia-php/logger": "0.3.*", + "utopia-php/messaging": "0.1.*", "utopia-php/orchestration": "0.6.*", + "utopia-php/preloader": "0.2.*", + "utopia-php/registry": "0.5.*", + "utopia-php/storage": "0.11.*", + "utopia-php/swoole": "0.3.*", + "utopia-php/websocket": "0.1.0", "resque/php-resque": "1.3.6", "matomo/device-detector": "6.0.0", "dragonmantank/cron-expression": "3.3.1", diff --git a/composer.lock b/composer.lock index cd2040eb1a..86ff738a9a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "568151395a8877f87d9bdce048adc2dc", + "content-hash": "e81c5582d74cc867fbf80e9bdf4f4872", "packages": [ { "name": "adhocore/jwt", @@ -2394,6 +2394,60 @@ }, "time": "2022-03-18T10:56:57+00:00" }, + { + "name": "utopia-php/messaging", + "version": "0.1.0", + "source": { + "type": "git", + "url": "https://github.com/utopia-php/messaging.git", + "reference": "501272fad666f06bec8f130076862e7981a73f8c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/utopia-php/messaging/zipball/501272fad666f06bec8f130076862e7981a73f8c", + "reference": "501272fad666f06bec8f130076862e7981a73f8c", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "php": ">=8.0.0" + }, + "require-dev": { + "phpmailer/phpmailer": "6.6.*", + "phpunit/phpunit": "9.5.*", + "squizlabs/php_codesniffer": "^3.6" + }, + "type": "library", + "autoload": { + "psr-4": { + "Utopia\\Messaging\\": "src/Utopia/Messaging" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jake Barnby", + "email": "jake@appwrite.io" + } + ], + "description": "A simple, light and advanced PHP messaging library", + "keywords": [ + "library", + "messaging", + "php", + "upf", + "utopia", + "utopia-php" + ], + "support": { + "issues": "https://github.com/utopia-php/messaging/issues", + "source": "https://github.com/utopia-php/messaging/tree/0.1.0" + }, + "time": "2022-09-29T11:22:48+00:00" + }, { "name": "utopia-php/orchestration", "version": "0.6.0", diff --git a/src/Appwrite/SMS/Adapter.php b/src/Appwrite/SMS/Adapter.php deleted file mode 100644 index 9f029bca69..0000000000 --- a/src/Appwrite/SMS/Adapter.php +++ /dev/null @@ -1,75 +0,0 @@ -user = $user; - $this->secret = $secret; - } - - /** - * Send Message to phone. - * @param string $from - * @param string $to - * @param string $message - * @return void - */ - abstract public function send(string $from, string $to, string $message): void; - - /** - * @param string $method - * @param string $url - * @param array $headers - * @param string $payload - * - * @return string - */ - protected function request(string $method, string $url, array $headers = [], ?string $payload = null, ?string $userpwd = null): string - { - $ch = \curl_init($url); - - \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); - \curl_setopt($ch, CURLOPT_HEADER, 0); - \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - \curl_setopt($ch, CURLOPT_USERAGENT, 'Appwrite Phone Authentication'); - - if (!is_null($payload)) { - \curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); - } - - if (!is_null($userpwd)) { - \curl_setopt($ch, CURLOPT_USERPWD, $userpwd); - } - - $headers[] = 'Content-length: ' . \strlen($payload); - - \curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - - $response = (string) \curl_exec($ch); - $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); - - \curl_close($ch); - - if ($code >= 400) { - throw new \Exception($response); - } - - return $response; - } -} diff --git a/src/Appwrite/SMS/Adapter/Mock.php b/src/Appwrite/SMS/Adapter/Mock.php deleted file mode 100644 index 52f2fec578..0000000000 --- a/src/Appwrite/SMS/Adapter/Mock.php +++ /dev/null @@ -1,38 +0,0 @@ -request( - method: 'POST', - url: $this->endpoint, - payload: \json_encode([ - 'message' => $message, - 'from' => $from, - 'to' => $to - ]), - headers: [ - "content-type: application/json", - "x-username: {$this->user}", - "x-key: {$this->secret}", - ] - ); - } -} diff --git a/src/Appwrite/SMS/Adapter/Msg91.php b/src/Appwrite/SMS/Adapter/Msg91.php deleted file mode 100644 index d75f023243..0000000000 --- a/src/Appwrite/SMS/Adapter/Msg91.php +++ /dev/null @@ -1,46 +0,0 @@ - utilized from for flow id - * @param string $to - * @param string $message - * @return void - */ - public function send(string $from, string $to, string $message): void - { - $to = ltrim($to, '+'); - $this->request( - method: 'POST', - url: $this->endpoint, - payload: json_encode([ - 'sender' => $this->user, - 'otp' => $message, - 'flow_id' => $from, - 'mobiles' => $to - ]), - headers: [ - "content-type: application/JSON", - "authkey: {$this->secret}", - ] - ); - } -} diff --git a/src/Appwrite/SMS/Adapter/Telesign.php b/src/Appwrite/SMS/Adapter/Telesign.php deleted file mode 100644 index 1ac6c1b284..0000000000 --- a/src/Appwrite/SMS/Adapter/Telesign.php +++ /dev/null @@ -1,39 +0,0 @@ -request( - method: 'POST', - url: $this->endpoint, - payload: \http_build_query([ - 'message' => $message, - 'message_type' => 'otp', - 'phone_number' => $to - ]), - userpwd: "{$this->user}:{$this->secret}" - ); - } -} diff --git a/src/Appwrite/SMS/Adapter/TextMagic.php b/src/Appwrite/SMS/Adapter/TextMagic.php deleted file mode 100644 index a7f391b76c..0000000000 --- a/src/Appwrite/SMS/Adapter/TextMagic.php +++ /dev/null @@ -1,42 +0,0 @@ -request( - method: 'POST', - url: $this->endpoint . '/messages', - payload: \http_build_query([ - 'text' => $message, - 'from' => $from, - 'phones' => $to - ]), - headers: [ - "X-TM-Username: {$this->user}", - "X-TM-Key: {$this->secret}", - ] - ); - } -} diff --git a/src/Appwrite/SMS/Adapter/Twilio.php b/src/Appwrite/SMS/Adapter/Twilio.php deleted file mode 100644 index 4988c720ad..0000000000 --- a/src/Appwrite/SMS/Adapter/Twilio.php +++ /dev/null @@ -1,36 +0,0 @@ -request( - method: 'POST', - url: "{$this->endpoint}/Accounts/{$this->user}/Messages.json", - payload: \http_build_query([ - 'Body' => $message, - 'From' => $from, - 'To' => $to - ]), - userpwd: "{$this->user}:{$this->secret}" - ); - } -} diff --git a/src/Appwrite/SMS/Adapter/Vonage.php b/src/Appwrite/SMS/Adapter/Vonage.php deleted file mode 100644 index cd7ef3c5bf..0000000000 --- a/src/Appwrite/SMS/Adapter/Vonage.php +++ /dev/null @@ -1,41 +0,0 @@ -request( - method: 'POST', - url: $this->endpoint, - headers: $headers, - payload: \http_build_query([ - 'text' => $message, - 'from' => $from, - 'to' => $to, - 'api_key' => $this->user, - 'api_secret' => $this->secret - ]) - ); - } -} diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index 0b7ef2d5bb..65567b22e5 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -716,12 +716,12 @@ class AccountCustomClientTest extends Scope $this->assertEquals(400, $response['headers']['status-code']); - \sleep(2); + \sleep(5); $smsRequest = $this->getLastRequest(); $this->assertEquals('http://request-catcher:5000/mock-sms', $smsRequest['url']); - $this->assertEquals('Appwrite Phone Authentication', $smsRequest['headers']['User-Agent']); + $this->assertEquals('Appwrite Mock Message Sender', $smsRequest['headers']['User-Agent']); $this->assertEquals('username', $smsRequest['headers']['X-Username']); $this->assertEquals('password', $smsRequest['headers']['X-Key']); $this->assertEquals('POST', $smsRequest['method']);