PageRenderTime 55ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Auth/Adapter/Http/Resolver/File.php

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