diff --git a/composer.json b/composer.json index 3b62358..93b21bb 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "vanilla/garden-cli": "~2.0", "phonetworks/pho-plugins-feeds": "^0.1", "get-stream/stream": "~2.7", - "mailgun/mailgun-php": "^2.5.0", + "mailgun/mailgun-php": "^3.0", "ramsey/uuid": "^3.0", "superbalist/php-pubsub-redis": "^2.0", "stripe/stripe-php": "^6.19", diff --git a/composer.lock b/composer.lock index 2092194..7ab76cf 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": "26deef9756199e9a5d35ad38d136f503", + "content-hash": "342d90fd14f3c007dfff7e353df688e4", "packages": [ { "name": "alchemy/binary-driver", @@ -1239,41 +1239,45 @@ }, { "name": "mailgun/mailgun-php", - "version": "2.8.1", + "version": "3.0.0", "source": { "type": "git", "url": "https://github.com/mailgun/mailgun-php.git", - "reference": "4af0346851914ae0d9a58bf9ddf17eb48f6498c8" + "reference": "4055fea33deb038df067e78a2116f9ccb0125e16" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/mailgun/mailgun-php/zipball/4af0346851914ae0d9a58bf9ddf17eb48f6498c8", - "reference": "4af0346851914ae0d9a58bf9ddf17eb48f6498c8", + "url": "https://api.github.com/repos/mailgun/mailgun-php/zipball/4055fea33deb038df067e78a2116f9ccb0125e16", + "reference": "4055fea33deb038df067e78a2116f9ccb0125e16", "shasum": "" }, "require": { - "php": "^5.5 || ^7.0", - "php-http/client-common": "^1.1", - "php-http/discovery": "^1.0", - "php-http/httplug": "^1.0 || ^2.0", - "php-http/message": "^1.0", + "php": "^7.1", + "php-http/client-common": "^1.9 || ^2.0", + "php-http/discovery": "^1.6", "php-http/multipart-stream-builder": "^1.0", + "psr/http-client": "^1.0", "webmozart/assert": "^1.2" }, "require-dev": { - "guzzlehttp/psr7": "^1.4", "nyholm/nsa": "^1.1", + "nyholm/psr7": "^1.0", "php-http/guzzle6-adapter": "^1.0", - "phpunit/phpunit": "~4.8" + "phpunit/phpunit": "^7.5" }, "suggest": { "guzzlehttp/psr7": "PSR-7 message implementation that also provides common utility methods", "php-http/curl-client": "cURL client for PHP-HTTP" }, "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, "autoload": { - "psr-0": { - "Mailgun": "src/" + "psr-4": { + "Mailgun\\": "src/" } }, "notification-url": "https://packagist.org/downloads/", @@ -1287,7 +1291,7 @@ } ], "description": "The Mailgun SDK provides methods for all API functions.", - "time": "2019-02-02T07:14:32+00:00" + "time": "2019-09-13T20:14:46+00:00" }, { "name": "monolog/monolog", @@ -3210,6 +3214,55 @@ ], "time": "2017-02-14T16:28:37+00:00" }, + { + "name": "psr/http-client", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-client.git", + "reference": "496a823ef742b632934724bf769560c2a5c7c44e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-client/zipball/496a823ef742b632934724bf769560c2a5c7c44e", + "reference": "496a823ef742b632934724bf769560c2a5c7c44e", + "shasum": "" + }, + "require": { + "php": "^7.0", + "psr/http-message": "^1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Client\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP clients", + "homepage": "https://github.com/php-fig/http-client", + "keywords": [ + "http", + "http-client", + "psr", + "psr-18" + ], + "time": "2018-10-30T23:29:13+00:00" + }, { "name": "psr/http-factory", "version": "1.0.1", @@ -5308,6 +5361,7 @@ "config", "zf" ], + "abandoned": "laminas/laminas-config", "time": "2019-06-08T18:58:54+00:00" }, { @@ -5365,6 +5419,7 @@ "file", "zf" ], + "abandoned": "laminas/laminas-file", "time": "2019-02-06T16:53:31+00:00" }, { @@ -5411,6 +5466,7 @@ "stdlib", "zf" ], + "abandoned": "laminas/laminas-stdlib", "time": "2018-08-28T21:34:05+00:00" } ], diff --git a/src/GraphJS/Controllers/AuthenticationController.php b/src/GraphJS/Controllers/AuthenticationController.php index f146fbe..539f5ec 100644 --- a/src/GraphJS/Controllers/AuthenticationController.php +++ b/src/GraphJS/Controllers/AuthenticationController.php @@ -11,23 +11,24 @@ namespace GraphJS\Controllers; +use SmtpAwareTrait; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use GraphJS\Session; use Pho\Kernel\Kernel; use PhoNetworksAutogenerated\User; -use Mailgun\Mailgun; use GraphJS\Crypto; /** * Takes care of Authentication - * + * * @author Emre Sokullu */ class AuthenticationController extends AbstractController { - - const PASSWORD_RECOVERY_EXPIRY = 15*60; + use SmtpAwareTrait; + + const PASSWORD_RECOVERY_EXPIRY = 15 * 60; public function tokenSignup(ServerRequestInterface $request, ResponseInterface $response) { @@ -63,12 +64,12 @@ public function tokenSignup(ServerRequestInterface $request, ResponseInterface $ /** * Sign Up - * + * * [username, email, password] - * + * * @param ServerRequestInterface $request * @param ResponseInterface $response - * + * * @return void */ public function signup(ServerRequestInterface $request, ResponseInterface $response) @@ -116,10 +117,10 @@ protected function actualSignup(ServerRequestInterface $request, ResponseInterfa if($validation->fails()) { return $this->fail($response, "Valid ".addslashes(implode(", ", $extra_reqs_to_validate)). " required."); } - + $result = $this->kernel->index()->query( "MATCH (n:user) WHERE n.Username= {username} OR n.Email = {email} RETURN n", - [ + [ "username" => $username, "email" => $email ] @@ -155,13 +156,12 @@ protected function actualSignup(ServerRequestInterface $request, ResponseInterfa if($verification) { $pin = rand(100000, 999999); $new_user->setPendingVerification($pin); - $mgClient = new Mailgun(getenv("MAILGUN_KEY")); - $mgClient->sendMessage(getenv("MAILGUN_DOMAIN"), - array('from' => 'GraphJS ', - 'to' => $data["email"], - 'subject' => 'Please Verify', - 'text' => 'Please enter this 6 digit passcode to verify your email: '.$pin) - ); + $this->smtpSend( + 'GraphJS ', + $data["email"], + 'Please Verify', + 'Please enter this 6 digit passcode to verify your email: ' . $pin + ); } Session::begin($response, (string) $new_user->id()); @@ -177,12 +177,12 @@ protected function actualSignup(ServerRequestInterface $request, ResponseInterfa /** * Log In - * + * * [username, password] * * @param ServerRequestInterface $request * @param ResponseInterface $response - * + * * @return void */ public function login(ServerRequestInterface $request, ResponseInterface $response) @@ -202,12 +202,12 @@ public function login(ServerRequestInterface $request, ResponseInterface $respon /** * Log In Via Token - * + * * [token] * * @param ServerRequestInterface $request * @param ResponseInterface $response - * + * * @return void */ public function tokenLogin(ServerRequestInterface $request, ResponseInterface $response) @@ -231,16 +231,16 @@ public function tokenLogin(ServerRequestInterface $request, ResponseInterface $r } $password = str_replace(["/","\\"], "", substr(password_hash($username, PASSWORD_BCRYPT, ["salt"=>$key]), -8)); // substr(password_hash($username, PASSWORD_BCRYPT, ["salt"=>$key]), -8); //error_log("username is: ".$username."\npassword is: ".$password); - + return $this->actualLogin($request, $response, $username, $password); - + } protected function actualLoginViaEmail(string $email, string $password): ?array { $result = $this->kernel->index()->query( "MATCH (n:user {Email: {email}, Password: {password}}) RETURN n", - [ + [ "email" => $email, "password" => md5($password) ] @@ -254,10 +254,10 @@ protected function actualLoginViaEmail(string $email, string $password): ?array protected function actualLogin(ServerRequestInterface $request, ResponseInterface $response, string $username, string $password) { - + $result = $this->kernel->index()->query( "MATCH (n:user {Username: {username}, Password: {password}}) RETURN n", - [ + [ "username" => $username, "password" => md5($password) ] @@ -276,7 +276,7 @@ protected function actualLogin(ServerRequestInterface $request, ResponseInterfac error_log("is a success"); $user = $result->results()[0]; } - + error_log(print_r($user)); error_log(intval($this->isMembershipModerated())); error_log("Done"); @@ -306,7 +306,7 @@ protected function actualLogin(ServerRequestInterface $request, ResponseInterfac * @param ResponseInterface $response * @return void */ - public function logout(ServerRequestInterface $request, ResponseInterface $response) + public function logout(ServerRequestInterface $request, ResponseInterface $response) { Session::destroy($response); return $this->succeed($response); @@ -314,7 +314,7 @@ public function logout(ServerRequestInterface $request, ResponseInterface $respo /** * Who Am I? - * + * * @param ServerRequestInterface $request * @param ResponseInterface $response * @return void @@ -330,14 +330,14 @@ public function whoami(ServerRequestInterface $request, ResponseInterface $respo catch(\Exception $e) { return $this->fail($response, "Invalid user"); } - + return $this->succeed($response, [ - "id" => $id, + "id" => $id, "admin" => (bool) ($id==$this->kernel->founder()->id()->toString()), "username" => (string) $i->getUsername(), - "editor" => ( - (($id==$this->kernel->founder()->id()->toString())) - || + "editor" => ( + (($id==$this->kernel->founder()->id()->toString())) + || (isset($i->attributes()->IsEditor) && (bool) $i->getIsEditor()) ), "pending" => ( @@ -360,7 +360,7 @@ public function resetPassword(ServerRequestInterface $request, ResponseInterface $result = $this->kernel->index()->query( "MATCH (n:user {Email: {email}}) RETURN n", - [ + [ "email" => $data["email"] ] ); @@ -379,23 +379,21 @@ public function resetPassword(ServerRequestInterface $request, ResponseInterface else{ file_put_contents(getenv("PASSWORD_REMINDER").md5($data["email"]), "{$pin}:".time()."\n", LOCK_EX); } - $mgClient = new Mailgun(getenv("MAILGUN_KEY")); - $mgClient->sendMessage(getenv("MAILGUN_DOMAIN"), - array('from' => 'GraphJS ', - 'to' => $data["email"], - 'subject' => 'Password Reminder', - 'text' => 'You may enter this 6 digit passcode: '.$pin) - ); + $this->smtpSend( + 'GraphJS ', + $data["email"], + 'Password Reminder', + 'You may enter this 6 digit passcode: ' . $pin); return $this->succeed($response); } - + protected function _isRedisPasswordReminder(): bool { $redis_password_reminder = getenv("PASSWORD_REMINDER_ON_REDIS"); error_log("password reminder is ".$redis_password_reminder); return($redis_password_reminder===1||$redis_password_reminder==="1"||$redis_password_reminder==="on"); } - + public function verifyEmailCode(ServerRequestInterface $request, ResponseInterface $response) { $data = $request->getQueryParams(); @@ -429,7 +427,7 @@ public function verifyEmailCode(ServerRequestInterface $request, ResponseInterfa $i->setPendingVerification(0); $data["id"] = strtolower($data["id"]); - + Session::begin($response, $data['id']); return $this->succeed($response, [ @@ -460,15 +458,15 @@ public function verifyReset(ServerRequestInterface $request, ResponseInterface $ if($pins[0]!=$data["code"]) { return $this->fail($response, "Code does not match."); } - + //if((int) $pins[1]_isRedisPasswordReminder() && (int) $pins[1]fail($response, "Expired."); } - + $result = $this->kernel->index()->query( "MATCH (n:user {Email: {email}}) RETURN n", - [ + [ "email" => $data["email"] ] ); diff --git a/src/GraphJS/Controllers/MessagingController.php b/src/GraphJS/Controllers/MessagingController.php index 1a67f87..768c3b0 100644 --- a/src/GraphJS/Controllers/MessagingController.php +++ b/src/GraphJS/Controllers/MessagingController.php @@ -11,13 +11,12 @@ namespace GraphJS\Controllers; +use SmtpAwareTrait; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use GraphJS\Session; use Pho\Kernel\Kernel; -use PhoNetworksAutogenerated\User; use Pho\Lib\Graph\ID; -use Mailgun\Mailgun; use GraphJS\Utils; /** @@ -27,6 +26,7 @@ */ class MessagingController extends AbstractController { + use SmtpAwareTrait; public function sendAnonymousMessage(ServerRequestInterface $request, ResponseInterface $response) { @@ -35,15 +35,15 @@ public function sendAnonymousMessage(ServerRequestInterface $request, ResponseIn /** * Send a Message - * + * * [to, message] * * @param ServerRequestInterface $request * @param ResponseInterface $response - + * @param Kernel $this->kernel * @param string $id - * + * * @return void */ public function sendMessage(ServerRequestInterface $request, ResponseInterface $response, bool $anonymous = false) @@ -82,12 +82,11 @@ public function sendMessage(ServerRequestInterface $request, ResponseInterface $ $i = $this->kernel->gs()->node($id); $msg = $i->message($recipient, $data["message"]); } - $mgClient = new Mailgun(getenv("MAILGUN_KEY")); - $mgClient->sendMessage(getenv("MAILGUN_DOMAIN"), - array('from' => ($anonymous && is_null($id)) ? $data["sender"] : $i->getUsername() . ' ', - 'to' => $recipient->getEmail(), - 'subject' => 'Private Message', - 'text' => $data["message"] . PHP_EOL . (!is_null($id) ? (string) $msg->id() : "") ) + $this->smtpSend( + ($anonymous && is_null($id)) ? $data["sender"] : $i->getUsername() . ' ', + $recipient->getEmail(), + 'Private Message', + $data["message"] . PHP_EOL . (!is_null($id) ? (string) $msg->id() : "") ); if(!is_null($id)) return $this->succeed( diff --git a/src/GraphJS/SmtpAwareTrait.php b/src/GraphJS/SmtpAwareTrait.php new file mode 100644 index 0000000..09ed5c8 --- /dev/null +++ b/src/GraphJS/SmtpAwareTrait.php @@ -0,0 +1,27 @@ +smtpClient)) { + // change to another Smtp client if needed here + $this->smtpClient = new SmtpMailerMailGun(); + } + + return $this->smtpClient->sendMessage($from, $to, $subject, $body); + } + +} diff --git a/src/GraphJS/SmtpMailerInterface.php b/src/GraphJS/SmtpMailerInterface.php new file mode 100644 index 0000000..3338e2a --- /dev/null +++ b/src/GraphJS/SmtpMailerInterface.php @@ -0,0 +1,22 @@ +client = Mailgun::create(getenv("MAILGUN_KEY")); + } + + /** + * @param string $from + * @param string $to + * @param string $subject + * @param string $body + * @return SendResponse|ResponseInterface + */ + public function sendMessage(string $from, string $to, string $subject, string $body) + { + return $this->client->messages()->send(getenv("MAILGUN_DOMAIN"), [ + 'from' => $from, + 'to' => $to, + 'subject' => $subject, + 'text' => $body + ]); + } + +}