Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
File
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
8 / 8
20
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getFilename
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRealm
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDelimiter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setFilename
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 setRealm
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setDelimiter
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 authenticate
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
12
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Auth;
15
16/**
17 * File auth class
18 *
19 * @category   Pop
20 * @package    Pop\Auth
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    3.3.0
25 */
26class File extends AbstractAuth
27{
28
29    /**
30     * Auth file
31     * @var string
32     */
33    protected $filename = null;
34
35    /**
36     * Auth realm
37     * @var string
38     */
39    protected $realm = null;
40
41    /**
42     * Auth file delimiter
43     * @var string
44     */
45    protected $delimiter = ':';
46
47    /**
48     * Constructor
49     *
50     * Instantiate the File auth adapter object
51     *
52     * @param  string $filename
53     * @param  string $realm
54     * @param  string $delimiter
55     * @throws Exception
56     */
57    public function __construct($filename, $realm = null, $delimiter = ':')
58    {
59        $this->setFilename($filename);
60        $this->realm     = $realm;
61        $this->delimiter = $delimiter;
62    }
63
64    /**
65     * Get the auth filename
66     *
67     * @return string
68     */
69    public function getFilename()
70    {
71        return $this->filename;
72    }
73
74    /**
75     * Get the auth realm
76     *
77     * @return string
78     */
79    public function getRealm()
80    {
81        return $this->realm;
82    }
83
84    /**
85     * Get the auth file delimiter
86     *
87     * @return string
88     */
89    public function getDelimiter()
90    {
91        return $this->delimiter;
92    }
93
94    /**
95     * Set the auth filename
96     *
97     * @param  string $filename
98     * @throws Exception
99     * @return File
100     */
101    public function setFilename($filename)
102    {
103        if (!file_exists($filename)) {
104            throw new Exception("The access file '" . $filename . "' does not exist.");
105        }
106        $this->filename = $filename;
107        return $this;
108    }
109
110    /**
111     * Set the auth realm
112     *
113     * @param  string $realm
114     * @return File
115     */
116    public function setRealm($realm)
117    {
118        $this->realm = $realm;
119        return $this;
120    }
121
122    /**
123     * Set the auth file delimiter
124     *
125     * @param  string $delimiter
126     * @return File
127     */
128    public function setDelimiter($delimiter)
129    {
130        $this->delimiter = $delimiter;
131        return $this;
132    }
133
134    /**
135     * Method to authenticate
136     *
137     * @param  string $username
138     * @param  string $password
139     * @return int
140     */
141    public function authenticate($username, $password)
142    {
143        $this->setUsername($username);
144        $this->setPassword($password);
145
146        $lines        = file($this->filename);
147        $hash         = null;
148        $this->result = 0;
149
150        foreach ($lines as $line) {
151            $line = trim($line);
152            $user = explode($this->delimiter, $line);
153            if (isset($user[0]) && ($user[0] == $this->username)) {
154                if ((null !== $this->realm) && (count($user) == 3)) {
155                    if (($this->username == $user[0]) && ($user[1] == $this->realm)) {
156                        $hash = $user[2];
157                        break;
158                    }
159                } else if (count($user) == 2) {
160                    if ($this->username == $user[0]) {
161                        $hash = $user[1];
162                        break;
163                    }
164                }
165            }
166        }
167
168        if ((null !== $this->password) && (null !== $hash)) {
169            $this->result = (int)$this->verify($this->password, $hash);
170        }
171
172        return $this->result;
173    }
174
175}