Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.21% covered (success)
86.21%
25 / 29
75.00% covered (success)
75.00%
9 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Ldap
86.21% covered (success)
86.21%
25 / 29
75.00% covered (success)
75.00%
9 / 12
26.64
0.00% covered (danger)
0.00%
0 / 1
 __construct
71.43% covered (success)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
4.37
 setHost
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setPort
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setOption
75.00% covered (success)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
5.39
 setOptions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getHost
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPort
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOption
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getOptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getResource
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 resource
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 authenticate
80.00% covered (success)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
5.20
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 * Ldap 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.3
25 */
26class Ldap extends AbstractAuth
27{
28
29    /**
30     * Ldap host
31     * @var string
32     */
33    protected $host = null;
34
35    /**
36     * Ldap port
37     * @var string
38     */
39    protected $port = null;
40
41    /**
42     * Ldap options
43     * @var array
44     */
45    protected $options = [];
46
47    /**
48     * Ldap resource
49     * @var resource
50     */
51    protected $resource = null;
52
53    /**
54     * Constructor
55     *
56     * Instantiate the Ldap auth adapter object
57     *
58     * @param  string $host
59     * @param  string $port
60     * @param  array  $options
61     */
62    public function __construct($host, $port = null, array $options = null)
63    {
64        $this->host = $host;
65        $this->port = $port;
66
67        if (!empty($host)) {
68            $host = (null !== $this->port) ? $this->host . ':' . $this->port : $this->host;
69            $this->resource = ldap_connect($host);
70        }
71
72        if (null !== $options) {
73            $this->setOptions($options);
74        }
75    }
76
77    /**
78     * Set the host
79     *
80     * @param  string $host
81     * @return string
82     */
83    public function setHost($host)
84    {
85        $this->host = $host;
86        return $this;
87    }
88
89    /**
90     * Set the port
91     *
92     * @param  string $port
93     * @return Ldap
94     */
95    public function setPort($port)
96    {
97        $this->port = $port;
98        return $this;
99    }
100
101    /**
102     * Set an option
103     *
104     * @param  mixed $option
105     * @param  mixed $value
106     * @return Ldap
107     */
108    public function setOption($option, $value)
109    {
110        $this->options[$option] = $value;
111        if (((phpversion() >= 8.1) && ($this->resource instanceof \LDAP\Connection)) || ((phpversion() < 8.1) && is_resource($this->resource))) {
112            ldap_set_option($this->resource, $option, $value);
113        }
114
115        return $this;
116    }
117
118    /**
119     * Set options
120     *
121     * @param  array $options
122     * @return Ldap
123     */
124    public function setOptions(array $options)
125    {
126        foreach ($options as $option => $value) {
127            $this->setOption($option, $value);
128        }
129
130        return $this;
131    }
132
133    /**
134     * Get the host
135     *
136     * @return string
137     */
138    public function getHost()
139    {
140        return $this->host;
141    }
142
143    /**
144     * Get the port
145     *
146     * @return string
147     */
148    public function getPort()
149    {
150        return $this->port;
151    }
152
153    /**
154     * Get an option
155     *
156     * @param  mixed $option
157     * @return mixed
158     */
159    public function getOption($option)
160    {
161        return (isset($this->options[$option])) ? $this->options[$option] : null;
162    }
163
164    /**
165     * Get options
166     *
167     * @return array
168     */
169    public function getOptions()
170    {
171        return $this->options;
172    }
173
174    /**
175     * Get the Ldap resource
176     *
177     * @return resource
178     */
179    public function getResource()
180    {
181        return $this->resource;
182    }
183
184    /**
185     * Get the Ldap resource (alias)
186     *
187     * @return resource
188     */
189    public function resource()
190    {
191        return $this->resource;
192    }
193
194    /**
195     * Method to authenticate
196     *
197     * @param  string $username
198     * @param  string $password
199     * @return int
200     */
201    public function authenticate($username, $password)
202    {
203        $this->setUsername($username);
204        $this->setPassword($password);
205
206        if (((phpversion() >= 8.1) && ($this->resource instanceof \LDAP\Connection)) || ((phpversion() < 8.1) && is_resource($this->resource))) {
207            $this->result = (int)(@ldap_bind($this->resource, $this->username, $this->password));
208        }
209
210        return $this->result;
211    }
212
213}