XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
CramMd5Authenticator.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of SwiftMailer.
5  * (c) 2004-2009 Chris Corbyn
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 
11 //@require 'Swift/Transport/Esmtp/Authenticator.php';
12 //@require 'Swift/Transport/SmtpAgent.php';
13 //@require 'Swift/TransportException.php';
14 
23 {
24 
29  public function getAuthKeyword()
30  {
31  return 'CRAM-MD5';
32  }
33 
41  public function authenticate(Swift_Transport_SmtpAgent $agent,
42  $username, $password)
43  {
44  try
45  {
46  $challenge = $agent->executeCommand("AUTH CRAM-MD5\r\n", array(334));
47  $challenge = base64_decode(substr($challenge, 4));
48  $message = base64_encode(
49  $username . ' ' . $this->_getResponse($password, $challenge)
50  );
51  $agent->executeCommand(sprintf("%s\r\n", $message), array(235));
52  return true;
53  }
54  catch (Swift_TransportException $e)
55  {
56  $agent->executeCommand("RSET\r\n", array(250));
57  return false;
58  }
59  }
60 
67  private function _getResponse($secret, $challenge)
68  {
69  if (strlen($secret) > 64)
70  {
71  $secret = pack('H32', md5($secret));
72  }
73 
74  if (strlen($secret) < 64)
75  {
76  $secret = str_pad($secret, 64, chr(0));
77  }
78 
79  $k_ipad = substr($secret, 0, 64) ^ str_repeat(chr(0x36), 64);
80  $k_opad = substr($secret, 0, 64) ^ str_repeat(chr(0x5C), 64);
81 
82  $inner = pack('H32', md5($k_ipad . $challenge));
83  $digest = md5($k_opad . $inner);
84 
85  return $digest;
86  }
87 
88 }