PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/www/system/library/Zend/Mail/Protocol/Smtp/Auth/Crammd5.php

https://bitbucket.org/vmihailenco/zf-blog
PHP | 108 lines | 40 code | 14 blank | 54 comment | 4 complexity | 9930b319cf918ac324580baf49583e70 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Mail
  17. * @subpackage Protocol
  18. * @copyright Copyright (c) 2005-2010 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 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /**
  23. * @see Zend_Mail_Protocol_Smtp
  24. */
  25. /**
  26. * Performs CRAM-MD5 authentication
  27. *
  28. * @category Zend
  29. * @package Zend_Mail
  30. * @subpackage Protocol
  31. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Mail_Protocol_Smtp_Auth_Crammd5 extends Zend_Mail_Protocol_Smtp
  35. {
  36. /**
  37. * Constructor.
  38. *
  39. * @param string $host (Default: 127.0.0.1)
  40. * @param int $port (Default: null)
  41. * @param array $config Auth-specific parameters
  42. * @return void
  43. */
  44. public function __construct($host = '127.0.0.1', $port = null, $config = null)
  45. {
  46. if (is_array($config)) {
  47. if (isset($config['username'])) {
  48. $this->_username = $config['username'];
  49. }
  50. if (isset($config['password'])) {
  51. $this->_password = $config['password'];
  52. }
  53. }
  54. parent::__construct($host, $port, $config);
  55. }
  56. /**
  57. * @todo Perform CRAM-MD5 authentication with supplied credentials
  58. *
  59. * @return void
  60. */
  61. public function auth()
  62. {
  63. // Ensure AUTH has not already been initiated.
  64. parent::auth();
  65. $this->_send('AUTH CRAM-MD5');
  66. $challenge = $this->_expect(334);
  67. $challenge = base64_decode($challenge);
  68. $digest = $this->_hmacMd5($this->_password, $challenge);
  69. $this->_send(base64_encode($this->_username . ' ' . $digest));
  70. $this->_expect(235);
  71. $this->_auth = true;
  72. }
  73. /**
  74. * Prepare CRAM-MD5 response to server's ticket
  75. *
  76. * @param string $key Challenge key (usually password)
  77. * @param string $data Challenge data
  78. * @param string $block Length of blocks
  79. * @return string
  80. */
  81. protected function _hmacMd5($key, $data, $block = 64)
  82. {
  83. if (strlen($key) > 64) {
  84. $key = pack('H32', md5($key));
  85. } elseif (strlen($key) < 64) {
  86. $key = str_pad($key, $block, chr(0));
  87. }
  88. $k_ipad = substr($key, 0, 64) ^ str_repeat(chr(0x36), 64);
  89. $k_opad = substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64);
  90. $inner = pack('H32', md5($k_ipad . $data));
  91. $digest = md5($k_opad . $inner);
  92. return $digest;
  93. }
  94. }