class.ldap.php
3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* Handle ldap authentication functionality
*
*
* LICENSE: This source file is subject to version 3.01 of the GPL license
* that is available through the world-wide-web at the following URI:
* http://www.gnu.org/licenses/gpl.html. If you did not receive a copy of
* the GPL License and are unable to obtain it through the web, please
*
* @category authentication
* @package phpMyFramework
* @author Original Author <jason.gerfen@gmail.com>
* @copyright 2010 Jason Gerfen
* @license http://www.gnu.org/licenses/gpl.html GPL License 3
* @version 0.1
*/
class openLDAP
{
protected static $instance;
private $handle;
private $bound;
private function __construct($configuration, $username, $password)
{
if (function_exists('ldap_connect'))
{
$this->main($configuration);
}
else
{
echo 'A extensão LDAP não está disponível ao PHP!!';
unset($instance);
exit;
}
$this->main($configuration, $username, $password);
}
public static function instance($configuration, $username=NULL, $password=NULL)
{
if (!isset(self::$instance))
{
$c = __CLASS__;
self::$instance = new self($configuration, $username, $password);
}
return self::$instance;
}
private function main($configuration, $username, $password)
{
$this->handle = $this->connect($configuration);
$this->setoptions($this->handle, $configuration);
$this->bound = $this->bind($this->handle, $configuration['username'], $configuration['password']);
}
private function connect($configuration)
{
return ldap_connect($configuration['servers'], $configuration['port']);
}
private function setoptions($handle, $configuration)
{
ldap_set_option($handle, LDAP_OPT_PROTOCOL_VERSION, $configuration['protocol']);
ldap_set_option($handle, LDAP_OPT_REFERRALS, $configuration['referrals']);
ldap_set_option($handle, LDAP_OPT_TIMELIMIT, $configuration['timelimit']);
ldap_set_option($handle, LDAP_OPT_NETWORK_TIMEOUT, $configuration['timeout']);
}
private function bind($handle, $username, $password)
{
return ldap_bind($handle, $username, $password);
}
public function authenticate($configuration, $username, $password)
{
if ((!empty($username))&&(!empty($password)))
{
return $this->bind($this->handle, $username, $password);
}
else
{
return $this->bind($this->handle, $configuration['username'], $configuration['password']);
}
}
public function search($base, $filter)
{
return ldap_search($this->handle, $base, $filter);
}
public function results($data)
{
return ldap_get_entries($this->handle, $data);
}
private function errors($handle)
{
return ldap_error($resource)." => ".ldap_errno($resource);
}
private function __destruct()
{
if (isset($this->handle))
{
ldap_free_result($this->handle);
ldap_unbind($this->handle);
}
return;
}
}
?>