Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.10% covered (success)
93.10%
27 / 29
91.67% covered (success)
91.67%
11 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Ldap
93.10% covered (success)
93.10%
27 / 29
91.67% covered (success)
91.67%
11 / 12
18.11
0.00% covered (danger)
0.00%
0 / 1
 __construct
75.00% covered (success)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
5.39
 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
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 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
1
 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
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
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-2024 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-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    4.0.0
25 */
26class Ldap extends AbstractAuth
27{
28
29    /**
30     * Ldap host
31     * @var ?string
32     */
33    protected ?string $host = null;
34
35    /**
36     * Ldap port
37     * @var string|int|null
38     */
39    protected string|int|null $port = null;
40
41    /**
42     * Ldap options
43     * @var array
44     */
45    protected array $options = [];
46
47    /**
48     * Ldap resource
49     * @var mixed
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|int|null $port
60     * @param  ?array          $options
61     */
62    public function __construct(string $host, string|int|null $port = null, ?array $options = null)
63    {
64        $this->setHost($host);
65        if ($port !== null) {
66            $this->setPort($port);
67        }
68
69        if (!empty($host)) {
70            $host = ($this->port !== null) ? $this->host . ':' . $this->port : $this->host;
71            $this->resource = ldap_connect($host);
72        }
73
74        if ($options !== null) {
75            $this->setOptions($options);
76        }
77    }
78
79    /**
80     * Set the host
81     *
82     * @param  string $host
83     * @return Ldap
84     */
85    public function setHost(string $host): Ldap
86    {
87        $this->host = $host;
88        return $this;
89    }
90
91    /**
92     * Set the port
93     *
94     * @param  string|int $port
95     * @return Ldap
96     */
97    public function setPort(string|int $port): Ldap
98    {
99        $this->port = $port;
100        return $this;
101    }
102
103    /**
104     * Set an option
105     *
106     * @param  mixed $option
107     * @param  mixed $value
108     * @return Ldap
109     */
110    public function setOption(mixed $option, mixed $value): Ldap
111    {
112        $this->options[$option] = $value;
113        ldap_set_option($this->resource, $option, $value);
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): Ldap
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(): ?string
139    {
140        return $this->host;
141    }
142
143    /**
144     * Get the port
145     *
146     * @return string|int|null
147     */
148    public function getPort(): string|int|null
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(mixed $option): mixed
160    {
161        return $this->options[$option] ?? null;
162    }
163
164    /**
165     * Get options
166     *
167     * @return array
168     */
169    public function getOptions(): array
170    {
171        return $this->options;
172    }
173
174    /**
175     * Get the Ldap resource
176     *
177     * @return mixed
178     */
179    public function getResource(): mixed
180    {
181        return $this->resource;
182    }
183
184    /**
185     * Get the Ldap resource (alias)
186     *
187     * @return mixed
188     */
189    public function resource(): mixed
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(string $username, string $password): int
202    {
203        $this->setUsername($username);
204        $this->setPassword($password);
205
206        $this->result = ($this->resource !== null) ?
207            (int)(@ldap_bind($this->resource, $this->username, $this->password)) : 0;
208
209        return $this->result;
210    }
211
212}