Code cleanups
This commit is contained in:
parent
b4cf92e843
commit
e874b2a14b
|
|
@ -4,3 +4,4 @@ test/testbootstrap.php
|
|||
test/*.pem
|
||||
.idea
|
||||
build/
|
||||
vendor/
|
||||
|
|
|
|||
|
|
@ -1,50 +1,87 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPMailer - PHP email creation and transport class.
|
||||
* PHP Version 5.4
|
||||
* @package PHPMailer
|
||||
* @link https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
|
||||
* @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
|
||||
* @author Jim Jagielski (jimjag) <jimjag@gmail.com>
|
||||
* @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
|
||||
* @author Brent R. Matzelle (original founder)
|
||||
* @copyright 2012 - 2014 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.
|
||||
*/
|
||||
|
||||
class PHPMailer54 extends PHPMailer {
|
||||
|
||||
/**
|
||||
* PHPMailerOAuth - PHPMailer subclass adding OAuth support.
|
||||
* @package PHPMailer
|
||||
* @author @sherryl4george
|
||||
* @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
|
||||
*/
|
||||
class PHPMailerOAuth extends PHPMailer
|
||||
{
|
||||
/**
|
||||
* The OAuth user's email address
|
||||
* @type string
|
||||
*/
|
||||
public $oauthUserEmail = '';
|
||||
|
||||
/**
|
||||
* The OAuth refresh token
|
||||
* @type string
|
||||
*/
|
||||
public $oauthRefreshToken = '';
|
||||
|
||||
/**
|
||||
* The OAuth client ID
|
||||
* @type string
|
||||
*/
|
||||
public $oauthClientId = '';
|
||||
|
||||
/**
|
||||
* The OAuth client secret
|
||||
* @type string
|
||||
*/
|
||||
public $oauthClientSecret = '';
|
||||
|
||||
/**
|
||||
* An instance of the SMTP sender class.
|
||||
* @type SMTP
|
||||
* An instance of the OAuth class.
|
||||
* @type OAuth
|
||||
* @access protected
|
||||
*/
|
||||
protected $oauth = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct($exceptions = false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
//Close any open SMTP connection nicely
|
||||
parent::__destruct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an instance to use for SMTP operations.
|
||||
* Override this function to load your own SMTP implementation
|
||||
* @return SMTP
|
||||
* Get an OAuth instance to use.
|
||||
* @return OAuth
|
||||
*/
|
||||
public function getOAUTHInstance()
|
||||
{
|
||||
if (!is_object($this->oauth)) {
|
||||
$this->oauth = new OAuth($this->oauthUserEmail,
|
||||
$this->oauthClientSecret,
|
||||
$this->oauthClientId,
|
||||
$this->oauthRefreshToken
|
||||
);
|
||||
$this->oauth = new OAuth(
|
||||
$this->oauthUserEmail,
|
||||
$this->oauthClientSecret,
|
||||
$this->oauthClientId,
|
||||
$this->oauthRefreshToken
|
||||
);
|
||||
}
|
||||
return $this->oauth;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initiate a connection to an SMTP server.
|
||||
* Overrides the original smtpConnect method to add support for OAuth.
|
||||
* @param array $options An array of options compatible with stream_context_create()
|
||||
* @uses SMTP
|
||||
* @access public
|
||||
* @throws phpmailerException
|
||||
* @return boolean
|
||||
*/
|
||||
public function smtpConnect($options = array())
|
||||
{
|
||||
if (is_null($this->smtp)) {
|
||||
|
|
@ -129,11 +166,11 @@ class PHPMailer54 extends PHPMailer {
|
|||
}
|
||||
if ($this->SMTPAuth) {
|
||||
if (!$this->smtp->authenticate(
|
||||
$this->Username,
|
||||
$this->Password,
|
||||
$this->AuthType,
|
||||
$this->Realm,
|
||||
$this->Workstation,
|
||||
$this->Username,
|
||||
$this->Password,
|
||||
$this->AuthType,
|
||||
$this->Realm,
|
||||
$this->Workstation,
|
||||
$this->oauth
|
||||
)
|
||||
) {
|
||||
|
|
@ -157,7 +194,4 @@ class PHPMailer54 extends PHPMailer {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
@ -351,13 +351,13 @@ class SMTP
|
|||
* Perform SMTP authentication.
|
||||
* Must be run after hello().
|
||||
* @see hello()
|
||||
* @param string $username The user name
|
||||
* @param string $password The password
|
||||
* @param string $authtype The auth type (PLAIN, LOGIN, NTLM, CRAM-MD5)
|
||||
* @param string $realm The auth realm for NTLM
|
||||
* @param string $username The user name
|
||||
* @param string $password The password
|
||||
* @param string $authtype The auth type (PLAIN, LOGIN, NTLM, CRAM-MD5)
|
||||
* @param string $realm The auth realm for NTLM
|
||||
* @param string $workstation The auth workstation for NTLM
|
||||
* @access public
|
||||
* @return boolean True if successfully authenticated.
|
||||
* @param null|OAuth $OAuth An optional OAuth instance (@see PHPMailerOAuth)
|
||||
* @return bool True if successfully authenticated.* @access public
|
||||
*/
|
||||
public function authenticate(
|
||||
$username,
|
||||
|
|
@ -437,12 +437,12 @@ class SMTP
|
|||
return false;
|
||||
}
|
||||
break;
|
||||
case 'XOAUTH':
|
||||
case 'XOAUTH':
|
||||
//If the OAuth Instance is not set. Can be a case when PHPMailer is used
|
||||
//instead of PHPMailer54
|
||||
if(is_null($OAuth))
|
||||
//instead of PHPMailerOAuth
|
||||
if (is_null($OAuth)) {
|
||||
return false;
|
||||
|
||||
}
|
||||
$oauth = $OAuth->getOauth64();
|
||||
|
||||
// Start authentication
|
||||
|
|
|
|||
|
|
@ -1,43 +1,43 @@
|
|||
{
|
||||
"name": "phpmailer/phpmailer",
|
||||
"type": "library",
|
||||
"description": "PHPMailer is a full-featured email creation and transfer class for PHP",
|
||||
"authors": [
|
||||
"name": "phpmailer/phpmailer",
|
||||
"type": "library",
|
||||
"description": "PHPMailer is a full-featured email creation and transfer class for PHP",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Marcus Bointon",
|
||||
"email": "phpmailer@synchromedia.co.uk"
|
||||
"name": "Marcus Bointon",
|
||||
"email": "phpmailer@synchromedia.co.uk"
|
||||
},
|
||||
{
|
||||
"name": "Jim Jagielski",
|
||||
"email": "jimjag@gmail.com"
|
||||
"name": "Jim Jagielski",
|
||||
"email": "jimjag@gmail.com"
|
||||
},
|
||||
{
|
||||
"name": "Andy Prevost",
|
||||
"email": "codeworxtech@users.sourceforge.net"
|
||||
"name": "Andy Prevost",
|
||||
"email": "codeworxtech@users.sourceforge.net"
|
||||
},
|
||||
{
|
||||
"name": "Brent R. Matzelle"
|
||||
"name": "Brent R. Matzelle"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.0.0",
|
||||
"league/oauth2-client": "0.10.*",
|
||||
"guzzle/guzzle": "~3.7"
|
||||
},
|
||||
"require-dev": {
|
||||
},
|
||||
"require-dev": {
|
||||
"phpdocumentor/phpdocumentor": "*",
|
||||
"phpunit/phpunit": "4.3.*"
|
||||
},
|
||||
"autoload": {
|
||||
"phpunit/phpunit": "4.3.*"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"class.phpmailer.php",
|
||||
"class.phpmailer54.php",
|
||||
"class.oauth.php",
|
||||
"class.smtp.php",
|
||||
"class.pop3.php",
|
||||
"extras/EasyPeasyICS.php",
|
||||
"extras/ntlm_sasl_client.php"
|
||||
"class.phpmailer.php",
|
||||
"class.phpmaileroauth.php",
|
||||
"class.oauth.php",
|
||||
"class.smtp.php",
|
||||
"class.pop3.php",
|
||||
"extras/EasyPeasyICS.php",
|
||||
"extras/ntlm_sasl_client.php"
|
||||
]
|
||||
},
|
||||
"license": "LGPL-2.1"
|
||||
}
|
||||
},
|
||||
"license": "LGPL-2.1"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,8 +10,8 @@ date_default_timezone_set('Etc/UTC');
|
|||
require '../PHPMailerAutoload.php';
|
||||
|
||||
|
||||
//Create a new PHPMailer instance
|
||||
$mail = new PHPMailer54;
|
||||
//Create a new PHPMailerOAuth instance
|
||||
$mail = new PHPMailerOAuth;
|
||||
|
||||
//Tell PHPMailer to use SMTP
|
||||
$mail->isSMTP();
|
||||
|
|
|
|||
|
|
@ -1,22 +1,33 @@
|
|||
<?php
|
||||
require './extras/oauth2/vendor/autoload.php';
|
||||
/**
|
||||
* Get an OAuth2 token from Google.
|
||||
* * Install this script on your server so that it's accessible
|
||||
* as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
|
||||
* e.g.: http://localhost/phpmail/get_oauth_token.php
|
||||
* * Ensure dependencies are installed with 'composer install'
|
||||
* * Set up an app in your Google developer console
|
||||
* * Set the script address as the app's redirect URL
|
||||
* This script requires PHP 5.4 or later
|
||||
*/
|
||||
|
||||
require './vendor/autoload.php';
|
||||
session_start();
|
||||
|
||||
$redirectUri = isset($_SERVER['HTTPS'])?'https://':'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
|
||||
$redirectUri = isset($_SERVER['HTTPS']) ? 'https://' : 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
|
||||
|
||||
//All Details Obtained by setting up APP in Google Developer Console.
|
||||
//Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
|
||||
// eg: http://localhost/phpmail/get_oauth_token.php
|
||||
$provider = new League\OAuth2\Client\Provider\Google ([
|
||||
'clientId' => 'RANDOMCHARS----p05gduv1n2.apps.googleusercontent.com',
|
||||
'clientSecret' => 'RANDOMCHARS----CWufYlGyjPcRtvP',
|
||||
'redirectUri' => $redirectUri,
|
||||
'scopes' => ['https://mail.google.com/'],
|
||||
'accessType' => 'offline'
|
||||
]);
|
||||
$provider = new League\OAuth2\Client\Provider\Google (
|
||||
[
|
||||
'clientId' => 'RANDOMCHARS----p05gduv1n2.apps.googleusercontent.com',
|
||||
'clientSecret' => 'RANDOMCHARS----CWufYlGyjPcRtvP',
|
||||
'redirectUri' => $redirectUri,
|
||||
'scopes' => ['https://mail.google.com/'],
|
||||
'accessType' => 'offline'
|
||||
]
|
||||
);
|
||||
|
||||
if (!isset($_GET['code'])) {
|
||||
|
||||
// If we don't have an authorization code then get one
|
||||
$authUrl = $provider->getAuthorizationUrl();
|
||||
$_SESSION['oauth2state'] = $provider->state;
|
||||
|
|
@ -24,25 +35,23 @@ if (!isset($_GET['code'])) {
|
|||
exit;
|
||||
// Check given state against previously stored one to mitigate CSRF attack
|
||||
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
|
||||
|
||||
unset($_SESSION['oauth2state']);
|
||||
exit('Invalid state');
|
||||
} else {
|
||||
|
||||
$provider->accessType = 'offline';
|
||||
// Try to get an access token (using the authorization code grant)
|
||||
$token = $provider->getAccessToken('authorization_code', [
|
||||
'code' => $_GET['code']
|
||||
]);
|
||||
|
||||
|
||||
// Use this to interact with an API on the users behalf
|
||||
// echo $token->accessToken.'<br>';
|
||||
$token = $provider->getAccessToken(
|
||||
'authorization_code',
|
||||
[
|
||||
'code' => $_GET['code']
|
||||
]
|
||||
);
|
||||
// Use this to interact with an API on the users behalf
|
||||
// echo $token->accessToken.'<br>';
|
||||
|
||||
// Use this to get a new access token if the old one expires
|
||||
echo 'Refresh Token: '.$token->refreshToken;
|
||||
echo 'Refresh Token: ' . $token->refreshToken;
|
||||
|
||||
// Unix timestamp of when the token will expire, and need refreshing
|
||||
// echo $token->expires;
|
||||
// Unix timestamp of when the token will expire, and need refreshing
|
||||
// echo $token->expires;
|
||||
}
|
||||
?>
|
||||
Loading…
Reference in New Issue