*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Mail / Protocol / Smtp / Auth / Crammd5.php
blobec5594e067579450b4c2a938f8227390769c6953
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Mail
17 * @subpackage Protocol
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Crammd5.php 16219 2009-06-21 19:45:39Z thomas $
24 /**
25 * @see Zend_Mail_Protocol_Smtp
27 require_once 'Zend/Mail/Protocol/Smtp.php';
30 /**
31 * Performs CRAM-MD5 authentication
33 * @category Zend
34 * @package Zend_Mail
35 * @subpackage Protocol
36 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
37 * @license http://framework.zend.com/license/new-bsd New BSD License
39 class Zend_Mail_Protocol_Smtp_Auth_Crammd5 extends Zend_Mail_Protocol_Smtp
41 /**
42 * Constructor.
44 * @param string $host (Default: 127.0.0.1)
45 * @param int $port (Default: null)
46 * @param array $config Auth-specific parameters
47 * @return void
49 public function __construct($host = '127.0.0.1', $port = null, $config = null)
51 if (is_array($config)) {
52 if (isset($config['username'])) {
53 $this->_username = $config['username'];
55 if (isset($config['password'])) {
56 $this->_password = $config['password'];
60 parent::__construct($host, $port, $config);
64 /**
65 * @todo Perform CRAM-MD5 authentication with supplied credentials
67 * @return void
69 public function auth()
71 // Ensure AUTH has not already been initiated.
72 parent::auth();
74 $this->_send('AUTH CRAM-MD5');
75 $challenge = $this->_expect(334);
76 $challenge = base64_decode($challenge);
77 $digest = $this->_hmacMd5($this->_password, $challenge);
78 $this->_send(base64_encode($this->_username . ' ' . $digest));
79 $this->_expect(235);
80 $this->_auth = true;
84 /**
85 * Prepare CRAM-MD5 response to server's ticket
87 * @param string $key Challenge key (usually password)
88 * @param string $data Challenge data
89 * @param string $block Length of blocks
90 * @return string
92 protected function _hmacMd5($key, $data, $block = 64)
94 if (strlen($key) > 64) {
95 $key = pack('H32', md5($key));
96 } elseif (strlen($key) < 64) {
97 $key = str_pad($key, $block, chr(0));
100 $k_ipad = substr($key, 0, 64) ^ str_repeat(chr(0x36), 64);
101 $k_opad = substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64);
103 $inner = pack('H32', md5($k_ipad . $data));
104 $digest = md5($k_opad . $inner);
106 return $digest;