Code cleanups

This commit is contained in:
Synchro 2015-05-19 17:36:29 +02:00
parent b4cf92e843
commit e874b2a14b
6 changed files with 143 additions and 99 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ test/testbootstrap.php
test/*.pem
.idea
build/
vendor/

View File

@ -1,42 +1,70 @@
<?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->oauth = new OAuth(
$this->oauthUserEmail,
$this->oauthClientSecret,
$this->oauthClientId,
$this->oauthRefreshToken
@ -45,6 +73,15 @@ class PHPMailer54 extends PHPMailer {
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)) {
@ -157,7 +194,4 @@ class PHPMailer54 extends PHPMailer {
}
return false;
}
}
?>

View File

@ -356,8 +356,8 @@ class SMTP
* @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,
@ -439,10 +439,10 @@ class SMTP
break;
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

View File

@ -1,5 +1,5 @@
{
"name": "phpmailer/phpmailer",
"name": "phpmailer/phpmailer",
"type": "library",
"description": "PHPMailer is a full-featured email creation and transfer class for PHP",
"authors": [
@ -31,7 +31,7 @@
"autoload": {
"classmap": [
"class.phpmailer.php",
"class.phpmailer54.php",
"class.phpmaileroauth.php",
"class.oauth.php",
"class.smtp.php",
"class.pop3.php",
@ -40,4 +40,4 @@
]
},
"license": "LGPL-2.1"
}
}

View File

@ -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();

View File

@ -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 ([
$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', [
$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 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;
}
?>