PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/monica/vendor/zendframework/zendframework/library/Zend/Mail/Protocol/Smtp/Auth/Crammd5.php

https://bitbucket.org/alexandretaz/maniac_divers
PHP | 154 lines | 70 code | 20 blank | 64 comment | 7 complexity | 556b993e7d5fba6014bab5530e0e85d3 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-2013 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\Mail\Protocol\Smtp;
  11. /**
  12. * Performs CRAM-MD5 authentication
  13. */
  14. class Crammd5 extends Smtp
  15. {
  16. /**
  17. * @var string
  18. */
  19. protected $username;
  20. /**
  21. * @var string
  22. */
  23. protected $password;
  24. /**
  25. * Constructor.
  26. *
  27. * All parameters may be passed as an array to the first argument of the
  28. * constructor. If so,
  29. *
  30. * @param string|array $host (Default: 127.0.0.1)
  31. * @param null|int $port (Default: null)
  32. * @param null|array $config Auth-specific parameters
  33. */
  34. public function __construct($host = '127.0.0.1', $port = null, $config = null)
  35. {
  36. // Did we receive a configuration array?
  37. $origConfig = $config;
  38. if (is_array($host)) {
  39. // Merge config array with principal array, if provided
  40. if (is_array($config)) {
  41. $config = array_replace_recursive($host, $config);
  42. } else {
  43. $config = $host;
  44. }
  45. }
  46. if (is_array($config)) {
  47. if (isset($config['username'])) {
  48. $this->setUsername($config['username']);
  49. }
  50. if (isset($config['password'])) {
  51. $this->setPassword($config['password']);
  52. }
  53. }
  54. // Call parent with original arguments
  55. parent::__construct($host, $port, $origConfig);
  56. }
  57. /**
  58. * @todo Perform CRAM-MD5 authentication with supplied credentials
  59. *
  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
  119. * @return string
  120. */
  121. protected function _hmacMd5($key, $data, $block = 64)
  122. {
  123. if (strlen($key) > 64) {
  124. $key = pack('H32', md5($key));
  125. } elseif (strlen($key) < 64) {
  126. $key = str_pad($key, $block, "\0");
  127. }
  128. $kIpad = substr($key, 0, 64) ^ str_repeat(chr(0x36), 64);
  129. $kOpad = substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64);
  130. $inner = pack('H32', md5($kIpad . $data));
  131. $digest = md5($kOpad . $inner);
  132. return $digest;
  133. }
  134. }