Skip to content

Commit

Permalink
Code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Synchro committed May 19, 2015
1 parent b4cf92e commit e874b2a
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 99 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ test/testbootstrap.php
test/*.pem
.idea
build/
vendor/
104 changes: 69 additions & 35 deletions class.phpmailer54.php → class.phpmaileroauth.php
Original file line number Diff line number Diff line change
@@ -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) <[email protected]>
* @author Jim Jagielski (jimjag) <[email protected]>
* @author Andy Prevost (codeworxtech) <[email protected]>
* @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) <[email protected]>
*/
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 = '';
public $oauthClientSecret = '';

/**
* An instance of the SMTP sender class.
* @type SMTP
* @access protected
* The OAuth client secret
* @type string
*/
protected $oauth = null;

public function __construct()
{
parent::__construct($exceptions = false);
}
public $oauthClientSecret = '';

/**
* Destructor.
* An instance of the OAuth class.
* @type OAuth
* @access protected
*/
public function __destruct()
{
//Close any open SMTP connection nicely
parent::__destruct();
}
protected $oauth = null;

/**
* 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)) {
Expand Down Expand Up @@ -129,11 +166,11 @@ public function smtpConnect($options = array())
}
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
)
) {
Expand All @@ -157,7 +194,4 @@ public function smtpConnect($options = array())
}
return false;
}

}

?>
20 changes: 10 additions & 10 deletions class.smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,13 @@ public function startTLS()
* 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,
Expand Down Expand Up @@ -437,12 +437,12 @@ public function authenticate(
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
Expand Down
56 changes: 28 additions & 28 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
"name": "Marcus Bointon",
"email": "[email protected]"
},
{
"name": "Jim Jagielski",
"email": "[email protected]"
"name": "Jim Jagielski",
"email": "[email protected]"
},
{
"name": "Andy Prevost",
"email": "[email protected]"
"name": "Andy Prevost",
"email": "[email protected]"
},
{
"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"
}
4 changes: 2 additions & 2 deletions examples/gmail_xoauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
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();
Expand Down
57 changes: 33 additions & 24 deletions get_oauth_token.php
Original file line number Diff line number Diff line change
@@ -1,48 +1,57 @@
<?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;
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']);
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;
}
?>

0 comments on commit e874b2a

Please sign in to comment.