PageRenderTime 27ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/external/phpmailer/class.phpmaileroauth.php

https://bitbucket.org/navigatecms/navigatecms
PHP | 197 lines | 110 code | 11 blank | 76 comment | 24 complexity | c708aa1378554c94e8f41c202db5468f MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-2.1, BSD-3-Clause, AGPL-3.0, Apache-2.0
  1. <?php
  2. /**
  3. * PHPMailer - PHP email creation and transport class.
  4. * PHP Version 5.4
  5. * @package PHPMailer
  6. * @link https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
  7. * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
  8. * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
  9. * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
  10. * @author Brent R. Matzelle (original founder)
  11. * @copyright 2012 - 2014 Marcus Bointon
  12. * @copyright 2010 - 2012 Jim Jagielski
  13. * @copyright 2004 - 2009 Andy Prevost
  14. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  15. * @note This program is distributed in the hope that it will be useful - WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE.
  18. */
  19. /**
  20. * PHPMailerOAuth - PHPMailer subclass adding OAuth support.
  21. * @package PHPMailer
  22. * @author @sherryl4george
  23. * @author Marcus Bointon (@Synchro) <phpmailer@synchromedia.co.uk>
  24. */
  25. class PHPMailerOAuth extends PHPMailer
  26. {
  27. /**
  28. * The OAuth user's email address
  29. * @var string
  30. */
  31. public $oauthUserEmail = '';
  32. /**
  33. * The OAuth refresh token
  34. * @var string
  35. */
  36. public $oauthRefreshToken = '';
  37. /**
  38. * The OAuth client ID
  39. * @var string
  40. */
  41. public $oauthClientId = '';
  42. /**
  43. * The OAuth client secret
  44. * @var string
  45. */
  46. public $oauthClientSecret = '';
  47. /**
  48. * An instance of the PHPMailerOAuthGoogle class.
  49. * @var PHPMailerOAuthGoogle
  50. * @access protected
  51. */
  52. protected $oauth = null;
  53. /**
  54. * Get a PHPMailerOAuthGoogle instance to use.
  55. * @return PHPMailerOAuthGoogle
  56. */
  57. public function getOAUTHInstance()
  58. {
  59. if (!is_object($this->oauth)) {
  60. $this->oauth = new PHPMailerOAuthGoogle(
  61. $this->oauthUserEmail,
  62. $this->oauthClientSecret,
  63. $this->oauthClientId,
  64. $this->oauthRefreshToken
  65. );
  66. }
  67. return $this->oauth;
  68. }
  69. /**
  70. * Initiate a connection to an SMTP server.
  71. * Overrides the original smtpConnect method to add support for OAuth.
  72. * @param array $options An array of options compatible with stream_context_create()
  73. * @uses SMTP
  74. * @access public
  75. * @return bool
  76. * @throws phpmailerException
  77. */
  78. public function smtpConnect($options = array())
  79. {
  80. if (is_null($this->smtp)) {
  81. $this->smtp = $this->getSMTPInstance();
  82. }
  83. if (is_null($this->oauth)) {
  84. $this->oauth = $this->getOAUTHInstance();
  85. }
  86. // Already connected?
  87. if ($this->smtp->connected()) {
  88. return true;
  89. }
  90. $this->smtp->setTimeout($this->Timeout);
  91. $this->smtp->setDebugLevel($this->SMTPDebug);
  92. $this->smtp->setDebugOutput($this->Debugoutput);
  93. $this->smtp->setVerp($this->do_verp);
  94. $hosts = explode(';', $this->Host);
  95. $lastexception = null;
  96. foreach ($hosts as $hostentry) {
  97. $hostinfo = array();
  98. if (!preg_match('/^((ssl|tls):\/\/)*([a-zA-Z0-9\.-]*):?([0-9]*)$/', trim($hostentry), $hostinfo)) {
  99. // Not a valid host entry
  100. continue;
  101. }
  102. // $hostinfo[2]: optional ssl or tls prefix
  103. // $hostinfo[3]: the hostname
  104. // $hostinfo[4]: optional port number
  105. // The host string prefix can temporarily override the current setting for SMTPSecure
  106. // If it's not specified, the default value is used
  107. $prefix = '';
  108. $secure = $this->SMTPSecure;
  109. $tls = ($this->SMTPSecure == 'tls');
  110. if ('ssl' == $hostinfo[2] or ('' == $hostinfo[2] and 'ssl' == $this->SMTPSecure)) {
  111. $prefix = 'ssl://';
  112. $tls = false; // Can't have SSL and TLS at the same time
  113. $secure = 'ssl';
  114. } elseif ($hostinfo[2] == 'tls') {
  115. $tls = true;
  116. // tls doesn't use a prefix
  117. $secure = 'tls';
  118. }
  119. //Do we need the OpenSSL extension?
  120. $sslext = defined('OPENSSL_ALGO_SHA1');
  121. if ('tls' === $secure or 'ssl' === $secure) {
  122. //Check for an OpenSSL constant rather than using extension_loaded, which is sometimes disabled
  123. if (!$sslext) {
  124. throw new phpmailerException($this->lang('extension_missing').'openssl', self::STOP_CRITICAL);
  125. }
  126. }
  127. $host = $hostinfo[3];
  128. $port = $this->Port;
  129. $tport = (integer)$hostinfo[4];
  130. if ($tport > 0 and $tport < 65536) {
  131. $port = $tport;
  132. }
  133. if ($this->smtp->connect($prefix . $host, $port, $this->Timeout, $options)) {
  134. try {
  135. if ($this->Helo) {
  136. $hello = $this->Helo;
  137. } else {
  138. $hello = $this->serverHostname();
  139. }
  140. $this->smtp->hello($hello);
  141. //Automatically enable TLS encryption if:
  142. // * it's not disabled
  143. // * we have openssl extension
  144. // * we are not already using SSL
  145. // * the server offers STARTTLS
  146. if ($this->SMTPAutoTLS and $sslext and $secure != 'ssl' and $this->smtp->getServerExt('STARTTLS')) {
  147. $tls = true;
  148. }
  149. if ($tls) {
  150. if (!$this->smtp->startTLS()) {
  151. throw new phpmailerException($this->lang('connect_host'));
  152. }
  153. // We must resend HELO after tls negotiation
  154. $this->smtp->hello($hello);
  155. }
  156. if ($this->SMTPAuth) {
  157. if (!$this->smtp->authenticate(
  158. $this->Username,
  159. $this->Password,
  160. $this->AuthType,
  161. $this->Realm,
  162. $this->Workstation,
  163. $this->oauth
  164. )
  165. ) {
  166. throw new phpmailerException($this->lang('authenticate'));
  167. }
  168. }
  169. return true;
  170. } catch (phpmailerException $exc) {
  171. $lastexception = $exc;
  172. $this->edebug($exc->getMessage());
  173. // We must have connected, but then failed TLS or Auth, so close connection nicely
  174. $this->smtp->quit();
  175. }
  176. }
  177. }
  178. // If we get here, all connection attempts have failed, so close connection hard
  179. $this->smtp->close();
  180. // As we've caught all exceptions, just report whatever the last one was
  181. if ($this->exceptions and !is_null($lastexception)) {
  182. throw $lastexception;
  183. }
  184. return false;
  185. }
  186. }