00001 <?php
00027 require_once 'Zend/Mail/Protocol/Smtp.php';
00028
00029
00039 class Zend_Mail_Protocol_Smtp_Auth_Crammd5 extends Zend_Mail_Protocol_Smtp
00040 {
00049 public function __construct($host = '127.0.0.1', $port = null, $config = null)
00050 {
00051 if (is_array($config)) {
00052 if (isset($config['username'])) {
00053 $this->_username = $config['username'];
00054 }
00055 if (isset($config['password'])) {
00056 $this->_password = $config['password'];
00057 }
00058 }
00059
00060 parent::__construct($host, $port, $config);
00061 }
00062
00063
00069 public function auth()
00070 {
00071
00072 parent::auth();
00073
00074 $this->_send('AUTH CRAM-MD5');
00075 $challenge = $this->_expect(334);
00076 $challenge = base64_decode($challenge);
00077 $digest = $this->_hmacMd5($this->_password, $challenge);
00078 $this->_send(base64_encode($this->_username . ' ' . $digest));
00079 $this->_expect(235);
00080 $this->_auth = true;
00081 }
00082
00083
00092 protected function _hmacMd5($key, $data, $block = 64)
00093 {
00094 if (strlen($key) > 64) {
00095 $key = pack('H32', md5($key));
00096 } elseif (strlen($key) < 64) {
00097 $key = str_pad($key, $block, chr(0));
00098 }
00099
00100 $k_ipad = substr($key, 0, 64) ^ str_repeat(chr(0x36), 64);
00101 $k_opad = substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64);
00102
00103 $inner = pack('H32', md5($k_ipad . $data));
00104 $digest = md5($k_opad . $inner);
00105
00106 return $digest;
00107 }
00108 }