Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Authentication / Adapter / Http / FileResolver.php
blobc69813c92bef8c86d396ca6a36219acd87a9f3bd
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
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 */
10 namespace Zend\Authentication\Adapter\Http;
12 use Zend\Stdlib\ErrorHandler;
14 /**
15 * HTTP Authentication File Resolver
17 class FileResolver implements ResolverInterface
19 /**
20 * Path to credentials file
22 * @var string
24 protected $file;
26 /**
27 * Constructor
29 * @param string $path Complete filename where the credentials are stored
31 public function __construct($path = '')
33 if (!empty($path)) {
34 $this->setFile($path);
38 /**
39 * Set the path to the credentials file
41 * @param string $path
42 * @return FileResolver Provides a fluent interface
43 * @throws Exception\InvalidArgumentException if path is not readable
45 public function setFile($path)
47 if (empty($path) || !is_readable($path)) {
48 throw new Exception\InvalidArgumentException('Path not readable: ' . $path);
50 $this->file = $path;
52 return $this;
55 /**
56 * Returns the path to the credentials file
58 * @return string
60 public function getFile()
62 return $this->file;
65 /**
66 * Resolve credentials
68 * Only the first matching username/realm combination in the file is
69 * returned. If the file contains credentials for Digest authentication,
70 * the returned string is the password hash, or h(a1) from RFC 2617. The
71 * returned string is the plain-text password for Basic authentication.
73 * The expected format of the file is:
74 * username:realm:sharedSecret
76 * That is, each line consists of the user's username, the applicable
77 * authentication realm, and the password or hash, each delimited by
78 * colons.
80 * @param string $username Username
81 * @param string $realm Authentication Realm
82 * @return string|false User's shared secret, if the user is found in the
83 * realm, false otherwise.
84 * @throws Exception\ExceptionInterface
86 public function resolve($username, $realm, $password = null)
88 if (empty($username)) {
89 throw new Exception\InvalidArgumentException('Username is required');
90 } elseif (!ctype_print($username) || strpos($username, ':') !== false) {
91 throw new Exception\InvalidArgumentException('Username must consist only of printable characters, '
92 . 'excluding the colon');
94 if (empty($realm)) {
95 throw new Exception\InvalidArgumentException('Realm is required');
96 } elseif (!ctype_print($realm) || strpos($realm, ':') !== false) {
97 throw new Exception\InvalidArgumentException('Realm must consist only of printable characters, '
98 . 'excluding the colon.');
101 // Open file, read through looking for matching credentials
102 ErrorHandler::start(E_WARNING);
103 $fp = fopen($this->file, 'r');
104 $error = ErrorHandler::stop();
105 if (!$fp) {
106 throw new Exception\RuntimeException('Unable to open password file: ' . $this->file, 0, $error);
109 // No real validation is done on the contents of the password file. The
110 // assumption is that we trust the administrators to keep it secure.
111 while (($line = fgetcsv($fp, 512, ':')) !== false) {
112 if ($line[0] == $username && $line[1] == $realm) {
113 $password = $line[2];
114 fclose($fp);
115 return $password;
119 fclose($fp);
120 return false;