From 3a41f91fe2289c47d1780cc588721e6bd6bb478b Mon Sep 17 00:00:00 2001 From: Marcus Date: Tue, 5 Apr 2016 12:13:39 +0200 Subject: [PATCH] Rearrangements for new XOAUTH approach, see #675 --- README.md | 3 +- composer.lock | 6 +- examples/gmail_xoauth.phps | 4 +- examples/xoauth2_gmail.phps | 30 ------ examples/xoauth2_microsoft.phps | 48 --------- examples/xoauth2_yahoo.phps | 45 -------- get_oauth_token.php | 123 +++++++++++----------- src/{OAuthProvider/Base.php => OAuth.php} | 4 +- src/OAuthProvider/Google.php | 55 ---------- src/OAuthProvider/Microsoft.php | 54 ---------- src/OAuthProvider/Yahoo.php | 46 -------- src/PHPMailer.php | 14 +-- src/SMTP.php | 5 +- 13 files changed, 82 insertions(+), 355 deletions(-) delete mode 100644 examples/xoauth2_gmail.phps delete mode 100644 examples/xoauth2_microsoft.phps delete mode 100644 examples/xoauth2_yahoo.phps rename src/{OAuthProvider/Base.php => OAuth.php} (98%) delete mode 100644 src/OAuthProvider/Google.php delete mode 100644 src/OAuthProvider/Microsoft.php delete mode 100644 src/OAuthProvider/Yahoo.php diff --git a/README.md b/README.md index 9231a73f..f52de0cb 100644 --- a/README.md +++ b/README.md @@ -60,8 +60,7 @@ If you don't speak git or just want a tarball, click the 'zip' button on the rig ### Minimal installation -While installing the entire package manually or with composer is simple, convenient and reliable, you may want to include only vital files in your project. At the very least you will need [src/PHPMailer.php](https://github.com/PHPMailer/PHPMailer/tree/master/src/PHPMailer.php). If you're using SMTP, you'll need [src/SMTP.php](https://github.com/PHPMailer/PHPMailer/tree/master/src/SMTP.php), and if you're using POP-before SMTP, you'll need [src/POP3.php](https://github.com/PHPMailer/PHPMailer/tree/master/src/POP3.php). You can skip the [language](https://github.com/PHPMailer/PHPMailer/tree/master/language/) folder if you're not showing errors to users and can make do with English-only errors. If you're using Google XOAUTH2 you will need `src/PHPMailerOAuth.php` and `src/OAuthProvider/Google.php` classes, as well as the composer dependencies. Really, it's much easier to use composer! -While installing the entire package manually or with composer is simple, convenient and reliable, you may want to include only vital files in your project. At the very least you will need [src/PHPMailer.php](https://github.com/PHPMailer/PHPMailer/tree/master/src/PHPMailer.php). If you're using SMTP, you'll need [src/SMTP.php](https://github.com/PHPMailer/PHPMailer/tree/master/src/SMTP.php), and if you're using POP-before SMTP, you'll need [src/POP3.php](https://github.com/PHPMailer/PHPMailer/tree/master/src/POP3.php). You can skip the [language](https://github.com/PHPMailer/PHPMailer/tree/master/language/) folder if you're not showing errors to users and can make do with English-only errors. If you're using Google XOAUTH2 you will need [src/PHPMailerOAuth.php](https://github.com/PHPMailer/PHPMailer/tree/master/src/PHPMailerOAuth.php) and [src/OAuthProvider/Google.php](https://github.com/PHPMailer/PHPMailer/tree/master/src/OAuthProvider/Google.php) classes, as well as the composer dependencies. Really, it's much easier to use composer! +While installing the entire package manually or with composer is simple, convenient and reliable, you may want to include only vital files in your project. At the very least you will need [src/PHPMailer.php](https://github.com/PHPMailer/PHPMailer/tree/master/src/PHPMailer.php). If you're using SMTP, you'll need [src/SMTP.php](https://github.com/PHPMailer/PHPMailer/tree/master/src/SMTP.php), and if you're using POP-before SMTP, you'll need [src/POP3.php](https://github.com/PHPMailer/PHPMailer/tree/master/src/POP3.php). You can skip the [language](https://github.com/PHPMailer/PHPMailer/tree/master/language/) folder if you're not showing errors to users and can make do with English-only errors. If you're using XOAUTH2 you will need [src/OAuth.php](https://github.com/PHPMailer/PHPMailer/tree/master/src/OAuth.php) as well as the composer dependencies for the services you wish to authenticate with. Really, it's much easier to use composer! ## A Simple Example diff --git a/composer.lock b/composer.lock index ad2e5f22..225ecb4b 100644 --- a/composer.lock +++ b/composer.lock @@ -3482,12 +3482,12 @@ "version": "1.9", "source": { "type": "git", - "url": "https://github.com/zetacomponents/Base.git", + "url": "https://github.com/zetacomponents/OAuth.git", "reference": "f20df24e8de3e48b6b69b2503f917e457281e687" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/zetacomponents/Base/zipball/f20df24e8de3e48b6b69b2503f917e457281e687", + "url": "https://api.github.com/repos/zetacomponents/OAuth/zipball/f20df24e8de3e48b6b69b2503f917e457281e687", "reference": "f20df24e8de3e48b6b69b2503f917e457281e687", "shasum": "" }, @@ -3536,7 +3536,7 @@ "name": "Alexandru Stanoi" } ], - "description": "The Base package provides the basic infrastructure that all packages rely on. Therefore every component relies on this package.", + "description": "The OAuth package provides the basic infrastructure that all packages rely on. Therefore every component relies on this package.", "homepage": "https://github.com/zetacomponents", "time": "2014-09-19 03:28:34" }, diff --git a/examples/gmail_xoauth.phps b/examples/gmail_xoauth.phps index 7e2b3c7c..2e2bc11e 100644 --- a/examples/gmail_xoauth.phps +++ b/examples/gmail_xoauth.phps @@ -70,9 +70,9 @@ if (!isset($provider)) { ]); } -$mail->setOAuth(new OAuthProvider\Base( +$mail->setOAuth(new OAuth( [ - 'provdier' => $provider, + 'provider' => $provider, 'clientId' => $clientId, 'clientSecret' => $clientSecret, 'refreshToken' => $refreshToken, diff --git a/examples/xoauth2_gmail.phps b/examples/xoauth2_gmail.phps deleted file mode 100644 index 5361b00e..00000000 --- a/examples/xoauth2_gmail.phps +++ /dev/null @@ -1,30 +0,0 @@ -isSMTP(); -$mail->SMTPDebug = 2; -$mail->Host = 'smtp.gmail.com'; -$mail->Port = 587; -$mail->SMTPSecure = 'tls'; -$mail->SMTPAuth = true; -$mail->Subject = 'PHPMailer GMail SMTP test'; -$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__)); -$mail->setFrom('sender@gmail.com', 'Test'); -$mail->addAddress('receiver@hotmail.com', 'Test'); - -//send the message, check for errors - -if (!$mail->send()) { - echo "Mailer Error: " . $mail->ErrorInfo; -} else { - echo "Message sent!"; -} diff --git a/examples/xoauth2_microsoft.phps b/examples/xoauth2_microsoft.phps deleted file mode 100644 index ced46ed0..00000000 --- a/examples/xoauth2_microsoft.phps +++ /dev/null @@ -1,48 +0,0 @@ -isSMTP(); -$mail->SMTPDebug = 2; -$mail->Host = 'smtp-mail.outlook.com'; -$mail->Port = 587; -$mail->SMTPSecure = 'tls'; -$mail->SMTPAuth = true; - -//Set AuthType -$mail->AuthType = 'XOAUTH2'; - -//User Email to use for SMTP authentication - Who authorised to access Outlook mail -$mail->oauthUserEmail = "sender@hotmail.com"; - -//Obtained From https://account.live.com/developers/applications/index -$mail->oauthClientId = "{YOUR_CLIENT_ID}"; - -//Obtained From https://account.live.com/developers/applications/index -$mail->oauthClientSecret = "{YOUR_CLIENT_SECRET}"; - -//Obtained By running get_oauth_token.php -$mail->oauthRefreshToken = "{OAUTH_REFRESH_TOKEN}"; - -$mail->setFrom('sender@hotmail.com', 'Test'); -$mail->addAddress('receiver@gmail.com', 'Test'); -$mail->Subject = 'PHPMailer hotmail XOAUTH2 SMTP test'; -$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__)); -if (!$mail->send()) { - echo "Mailer Error: " . $mail->ErrorInfo; -} else { - echo "Message sent!"; -} diff --git a/examples/xoauth2_yahoo.phps b/examples/xoauth2_yahoo.phps deleted file mode 100644 index eb142efa..00000000 --- a/examples/xoauth2_yahoo.phps +++ /dev/null @@ -1,45 +0,0 @@ -isSMTP(); -$mail->SMTPDebug = 2; -$mail->Host = 'smtp.mail.yahoo.com'; -$mail->Port = 587; -$mail->SMTPSecure = 'tls'; -$mail->SMTPAuth = true; - -//Set AuthType -$mail->AuthType = 'XOAUTH2'; - -//User Email to use for SMTP authentication - Who authorised to send Yahoo mail -$mail->oauthUserEmail = 'sender@yahoo.com'; - -//Obtained From https://developer.yahoo.com/apps/ -$mail->oauthClientId = '{YAHOO_CLIENT_ID}'; - -//Obtained From https://developer.yahoo.com/apps/ -$mail->oauthClientSecret = '{CLIENT_SECRET}''; - -// eg: http://localhost/phpmail/get_oauth_token.php -$mail->oauthRefreshToken = '{REFRESH_TOKEN}'; - -$mail->setFrom('sender@yahoo.com', 'test'); -$mail->addAddress('receiver@gmail.com', 'test'); -$mail->Subject = 'PHPMailer Yahoo XOAUTH2 SMTP test'; -$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__)); -if (!$mail->send()) { - echo 'Mailer Error: ' . $mail->ErrorInfo; -} else { - echo 'Message sent!'; -} diff --git a/get_oauth_token.php b/get_oauth_token.php index c0dce3b4..744be0f7 100644 --- a/get_oauth_token.php +++ b/get_oauth_token.php @@ -30,85 +30,90 @@ namespace PHPMailer\PHPMailer; -// Give alias to the League Provider Classes that may be used +// Aliases for League Provider Classes that may be used use League\OAuth2\Client\Provider\Google as Google; use Stevenmaguire\OAuth2\Client\Provider\Microsoft as Microsoft; use Hayageek\OAuth2\Client\Provider\Yahoo as Yahoo; if (!isset($_GET['code']) && !isset($_GET['provider'])) { - ?> - - Select Provider:
- Google
- Yahoo
- Microsoft/Outlook/Hotmail/Live/Office365
- - + +Select Provider:
+Google
+Yahoo
+Microsoft/Outlook/Hotmail/Live/Office365
+ + $clientId, 'clientSecret' => $clientSecret, 'redirectUri' => $redirectUri, - 'accessType' => 'offline'] - ); + 'accessType' => 'offline' + ] +); - // Define scopes as a array here. - $options = ['scope' => [ - 'https://mail.google.com/' - ]]; +// Define scopes as a array here. +$options = [ + 'scope' => [ + 'https://mail.google.com/' + ] +]; - if (!isset($_GET['code'])) { +if (!isset($_GET['code'])) { - // If we don't have an authorization code then get one - $authUrl = $provider->getAuthorizationUrl($options); + // If we don't have an authorization code then get one + $authUrl = $provider->getAuthorizationUrl($options); - $_SESSION['oauth2state'] = $provider->getState(); + $_SESSION['oauth2state'] = $provider->getState(); - header('Location: ' . $authUrl); - exit; + header('Location: ' . $authUrl); + exit; // Check given state against previously stored one to mitigate CSRF attack - } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { - unset($_SESSION['oauth2state']); - unset($_SESSION['provider']); - exit('Invalid state'); - } else { - unset($_SESSION['provider']); +} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { + unset($_SESSION['oauth2state']); + unset($_SESSION['provider']); + exit('Invalid state'); +} else { + unset($_SESSION['provider']); - // Try to get an access token (using the authorization code grant) - $token = $provider->getAccessToken('authorization_code', array( + // Try to get an access token (using the authorization code grant) + $token = $provider->getAccessToken( + 'authorization_code', + array( 'code' => $_GET['code'] - )); - // Use this to interact with an API on the users behalf - // Use this to get a new access token if the old one expires - echo 'Refresh Token: ' . $token->getRefreshToken(); - } - ?> + ) + ); + // Use this to interact with an API on the users behalf + // Use this to get a new access token if the old one expires + echo 'Refresh Token: ' . $token->getRefreshToken(); +} diff --git a/src/OAuthProvider/Base.php b/src/OAuth.php similarity index 98% rename from src/OAuthProvider/Base.php rename to src/OAuth.php index 38f6aab2..cea36d9e 100644 --- a/src/OAuthProvider/Base.php +++ b/src/OAuth.php @@ -18,9 +18,9 @@ * FITNESS FOR A PARTICULAR PURPOSE. */ -namespace PHPMailer\PHPMailer\OAuthProvider; +namespace PHPMailer\PHPMailer; -class Base { +class OAuth { /** * @var League\OAuth2\Client\Provider\AbstractProvider diff --git a/src/OAuthProvider/Google.php b/src/OAuthProvider/Google.php deleted file mode 100644 index 874c47bb..00000000 --- a/src/OAuthProvider/Google.php +++ /dev/null @@ -1,55 +0,0 @@ - - * @author Jim Jagielski (jimjag) - * @author Andy Prevost (codeworxtech) - * @author Brent R. Matzelle (original founder) - * @copyright 2012 - 2015 Marcus Bointon - * @copyright 2010 - 2012 Jim Jagielski - * @copyright 2004 - 2009 Andy Prevost - * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License - * @note This program is distributed in the hope that it will be useful - WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - */ - -namespace PHPMailer\PHPMailer\OAuthProvider; - -use League\OAuth2\Client\Provider\Google as LeagueGoogle; - -/** - * Wrapper for League Google OAuth2 provider. - * @package PHPMailer - * @author @sherryl4george - * @author Marcus Bointon (@Synchro) - * @link https://github.com/thephpleague/oauth2-client - */ -class Google extends Base -{ - /** - * Return the OAuth provider implementation for this adaptor. - * @return League\OAuth2\Client\Provider\AbstractProvider - */ - public function getProvider() - { - if (is_null($this->provider)) { - $this->provider = new LeagueGoogle([ - 'clientId' => $this->oauthClientId, - 'clientSecret' => $this->oauthClientSecret - ]); - } - return $this->provider; - } - - public function getOptions() - { - return [ - 'scope' => ['https://mail.google.com/'], - 'approval_prompt' => 'force' - ]; - } -} diff --git a/src/OAuthProvider/Microsoft.php b/src/OAuthProvider/Microsoft.php deleted file mode 100644 index cc9ee020..00000000 --- a/src/OAuthProvider/Microsoft.php +++ /dev/null @@ -1,54 +0,0 @@ - - * @author Jim Jagielski (jimjag) - * @author Andy Prevost (codeworxtech) - * @author Brent R. Matzelle (original founder) - * @copyright 2012 - 2015 Marcus Bointon - * @copyright 2010 - 2012 Jim Jagielski - * @copyright 2004 - 2009 Andy Prevost - * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License - * @note This program is distributed in the hope that it will be useful - WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - */ - -namespace PHPMailer\PHPMailer\OAuthProvider; - -use Stevenmaguire\OAuth2\Client\Provider\Microsoft as StevenmaguireMicrosoft; - -/** - * Wrapper for League Microsoft OAuth2 provider. - * @package PHPMailer - * @author @hayageek - * @author Ravishanker Kusuma (hayageek@gmail.com) - * @link https://github.com/hayageek - */ - -class Microsoft extends Base -{ - public function getProvider() - { - if (is_null($this->provider)) { - $this->provider = new StevenmaguireMicrosoft([ - 'clientId' => $this->oauthClientId, - 'clientSecret' => $this->oauthClientSecret - ]); - } - return $this->provider; - } - - public function getOptions() - { - return [ - 'scope' => [ - 'wl.imap', - 'wl.offline_access' - ] - ]; - } -} diff --git a/src/OAuthProvider/Yahoo.php b/src/OAuthProvider/Yahoo.php deleted file mode 100644 index 85d1ad7a..00000000 --- a/src/OAuthProvider/Yahoo.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @author Jim Jagielski (jimjag) - * @author Andy Prevost (codeworxtech) - * @author Brent R. Matzelle (original founder) - * @copyright 2012 - 2015 Marcus Bointon - * @copyright 2010 - 2012 Jim Jagielski - * @copyright 2004 - 2009 Andy Prevost - * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License - * @note This program is distributed in the hope that it will be useful - WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - */ - -namespace PHPMailer\PHPMailer\OAuthProvider; - -use Hayageek\OAuth2\Client\Provider\Yahoo as HayageekYahoo; - -/** - * Wrapper for League Yahoo OAuth2 provider. - * @package PHPMailer - * @author @hayageek - * @author Ravishanker Kusuma (hayageek@gmail.com) - * @link https://github.com/hayageek - */ - -class Yahoo extends Base -{ - public function getProvider() - { - if (is_null($this->provider)) { - $this->provider = new HayageekYahoo( - [ - 'clientId' => $this->oauthClientId, - 'clientSecret' => $this->oauthClientSecret - ] - ); - } - return $this->provider; - } -} diff --git a/src/PHPMailer.php b/src/PHPMailer.php index 4bc71085..662eb4ad 100644 --- a/src/PHPMailer.php +++ b/src/PHPMailer.php @@ -269,8 +269,8 @@ class PHPMailer public $AuthType = ''; /** - * An instance of an OAuthProvider\Base derivative class. - * @var OAuthProvider\Base + * An instance of the PHPMailer OAuth class. + * @var OAuth * @access protected */ protected $oauth = null; @@ -3806,8 +3806,8 @@ class PHPMailer } /** - * Get the OAuthProvider instance. - * @return OAuthProvider\Base + * Get the OAuth instance. + * @return OAuth */ public function getOAuth() { @@ -3815,10 +3815,10 @@ class PHPMailer } /** - * Set an OAuthProvider instance. - * @param OAuthProvider\Base $oauth + * Set an OAuth instance. + * @param OAuth $oauth */ - public function setOAuth(OAuthProvider\Base $oauth) + public function setOAuth(OAuth $oauth) { $this->oauth = $oauth; } diff --git a/src/SMTP.php b/src/SMTP.php index 67e2590a..730ded67 100644 --- a/src/SMTP.php +++ b/src/SMTP.php @@ -332,8 +332,9 @@ class SMTP * @param string $username The user name * @param string $password The password * @param string $authtype The auth type (PLAIN, LOGIN, NTLM, CRAM-MD5, XOAUTH2) - * @param OAuthProvider\Base $OAuth An optional OAuth provider instance (@see PHPMailerOAuth) - * @return bool True if successfully authenticated.* @access public + * @param OAuth $OAuth An optional OAuth instance for XOAUTH2 authentication + * @return bool True if successfully authenticated. + * @access public */ public function authenticate( $username,