PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/zendframework1/library/Zend/Auth/Adapter/Http/Resolver/File.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 166 lines | 51 code | 12 blank | 103 comment | 17 complexity | 56c75079c07a8f3081841dff74f4f8b5 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_Auth
  17. * @subpackage Zend_Auth_Adapter_Http
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Auth_Adapter_Http_Resolver_Interface
  24. */
  25. #require_once 'Zend/Auth/Adapter/Http/Resolver/Interface.php';
  26. /**
  27. * HTTP Authentication File Resolver
  28. *
  29. * @category Zend
  30. * @package Zend_Auth
  31. * @subpackage Zend_Auth_Adapter_Http
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Auth_Adapter_Http_Resolver_File implements Zend_Auth_Adapter_Http_Resolver_Interface
  36. {
  37. /**
  38. * Path to credentials file
  39. *
  40. * @var string
  41. */
  42. protected $_file;
  43. /**
  44. * Constructor
  45. *
  46. * @param string $path Complete filename where the credentials are stored
  47. */
  48. public function __construct($path = '')
  49. {
  50. if (!empty($path)) {
  51. $this->setFile($path);
  52. }
  53. }
  54. /**
  55. * Set the path to the credentials file
  56. *
  57. * @param string $path
  58. * @throws Zend_Auth_Adapter_Http_Resolver_Exception
  59. * @return Zend_Auth_Adapter_Http_Resolver_File Provides a fluent interface
  60. */
  61. public function setFile($path)
  62. {
  63. if (empty($path) || !is_readable($path)) {
  64. /**
  65. * @see Zend_Auth_Adapter_Http_Resolver_Exception
  66. */
  67. #require_once 'Zend/Auth/Adapter/Http/Resolver/Exception.php';
  68. throw new Zend_Auth_Adapter_Http_Resolver_Exception('Path not readable: ' . $path);
  69. }
  70. $this->_file = $path;
  71. return $this;
  72. }
  73. /**
  74. * Returns the path to the credentials file
  75. *
  76. * @return string
  77. */
  78. public function getFile()
  79. {
  80. return $this->_file;
  81. }
  82. /**
  83. * Resolve credentials
  84. *
  85. * Only the first matching username/realm combination in the file is
  86. * returned. If the file contains credentials for Digest authentication,
  87. * the returned string is the password hash, or h(a1) from RFC 2617. The
  88. * returned string is the plain-text password for Basic authentication.
  89. *
  90. * The expected format of the file is:
  91. * username:realm:sharedSecret
  92. *
  93. * That is, each line consists of the user's username, the applicable
  94. * authentication realm, and the password or hash, each delimited by
  95. * colons.
  96. *
  97. * @param string $username Username
  98. * @param string $realm Authentication Realm
  99. * @throws Zend_Auth_Adapter_Http_Resolver_Exception
  100. * @return string|false User's shared secret, if the user is found in the
  101. * realm, false otherwise.
  102. */
  103. public function resolve($username, $realm)
  104. {
  105. if (empty($username)) {
  106. /**
  107. * @see Zend_Auth_Adapter_Http_Resolver_Exception
  108. */
  109. #require_once 'Zend/Auth/Adapter/Http/Resolver/Exception.php';
  110. throw new Zend_Auth_Adapter_Http_Resolver_Exception('Username is required');
  111. } else if (!ctype_print($username) || strpos($username, ':') !== false) {
  112. /**
  113. * @see Zend_Auth_Adapter_Http_Resolver_Exception
  114. */
  115. #require_once 'Zend/Auth/Adapter/Http/Resolver/Exception.php';
  116. throw new Zend_Auth_Adapter_Http_Resolver_Exception('Username must consist only of printable characters, '
  117. . 'excluding the colon');
  118. }
  119. if (empty($realm)) {
  120. /**
  121. * @see Zend_Auth_Adapter_Http_Resolver_Exception
  122. */
  123. #require_once 'Zend/Auth/Adapter/Http/Resolver/Exception.php';
  124. throw new Zend_Auth_Adapter_Http_Resolver_Exception('Realm is required');
  125. } else if (!ctype_print($realm) || strpos($realm, ':') !== false) {
  126. /**
  127. * @see Zend_Auth_Adapter_Http_Resolver_Exception
  128. */
  129. #require_once 'Zend/Auth/Adapter/Http/Resolver/Exception.php';
  130. throw new Zend_Auth_Adapter_Http_Resolver_Exception('Realm must consist only of printable characters, '
  131. . 'excluding the colon.');
  132. }
  133. // Open file, read through looking for matching credentials
  134. $fp = @fopen($this->_file, 'r');
  135. if (!$fp) {
  136. /**
  137. * @see Zend_Auth_Adapter_Http_Resolver_Exception
  138. */
  139. #require_once 'Zend/Auth/Adapter/Http/Resolver/Exception.php';
  140. throw new Zend_Auth_Adapter_Http_Resolver_Exception('Unable to open password file: ' . $this->_file);
  141. }
  142. // No real validation is done on the contents of the password file. The
  143. // assumption is that we trust the administrators to keep it secure.
  144. while (($line = fgetcsv($fp, 512, ':')) !== false) {
  145. if ($line[0] == $username && $line[1] == $realm) {
  146. $password = $line[2];
  147. fclose($fp);
  148. return $password;
  149. }
  150. }
  151. fclose($fp);
  152. return false;
  153. }
  154. }