PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/zendframework/zf2
PHP | 139 lines | 62 code | 14 blank | 63 comment | 6 complexity | f0d3b351f6369aaf498273ee45f499ed MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Mail\Protocol\Smtp\Auth;
  10. use Zend\Crypt\Hmac;
  11. use Zend\Mail\Protocol\Smtp;
  12. /**
  13. * Performs CRAM-MD5 authentication
  14. */
  15. class Crammd5 extends Smtp
  16. {
  17. /**
  18. * @var string
  19. */
  20. protected $username;
  21. /**
  22. * @var string
  23. */
  24. protected $password;
  25. /**
  26. * Constructor.
  27. *
  28. * All parameters may be passed as an array to the first argument of the
  29. * constructor. If so,
  30. *
  31. * @param string|array $host (Default: 127.0.0.1)
  32. * @param null|int $port (Default: null)
  33. * @param null|array $config Auth-specific parameters
  34. */
  35. public function __construct($host = '127.0.0.1', $port = null, $config = null)
  36. {
  37. // Did we receive a configuration array?
  38. $origConfig = $config;
  39. if (is_array($host)) {
  40. // Merge config array with principal array, if provided
  41. if (is_array($config)) {
  42. $config = array_replace_recursive($host, $config);
  43. } else {
  44. $config = $host;
  45. }
  46. }
  47. if (is_array($config)) {
  48. if (isset($config['username'])) {
  49. $this->setUsername($config['username']);
  50. }
  51. if (isset($config['password'])) {
  52. $this->setPassword($config['password']);
  53. }
  54. }
  55. // Call parent with original arguments
  56. parent::__construct($host, $port, $origConfig);
  57. }
  58. /**
  59. * Performs CRAM-MD5 authentication with supplied credentials
  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->getPassword(), $challenge);
  69. $this->_send(base64_encode($this->getUsername() . ' ' . $digest));
  70. $this->_expect(235);
  71. $this->auth = true;
  72. }
  73. /**
  74. * Set value for username
  75. *
  76. * @param string $username
  77. * @return Crammd5
  78. */
  79. public function setUsername($username)
  80. {
  81. $this->username = $username;
  82. return $this;
  83. }
  84. /**
  85. * Get username
  86. *
  87. * @return string
  88. */
  89. public function getUsername()
  90. {
  91. return $this->username;
  92. }
  93. /**
  94. * Set value for password
  95. *
  96. * @param string $password
  97. * @return Crammd5
  98. */
  99. public function setPassword($password)
  100. {
  101. $this->password = $password;
  102. return $this;
  103. }
  104. /**
  105. * Get password
  106. *
  107. * @return string
  108. */
  109. public function getPassword()
  110. {
  111. return $this->password;
  112. }
  113. /**
  114. * Prepare CRAM-MD5 response to server's ticket
  115. *
  116. * @param string $key Challenge key (usually password)
  117. * @param string $data Challenge data
  118. * @param int $block Length of blocks (deprecated; unused)
  119. * @return string
  120. */
  121. protected function _hmacMd5($key, $data, $block = 64)
  122. {
  123. return Hmac::compute($key, 'md5', $data);
  124. }
  125. }