-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap.php
212 lines (186 loc) · 4.72 KB
/
ldap.php
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* The class for auth with an active directory services.
*
*/
class Ldap {
/**
* @var string
*/
private $ldapServer = '';
/**
* @var int
*/
private $ldapPort = 0;
/**
* @var string
*/
private $baseDn = '';
/**
* @var string
*/
private $basePassword = '';
/**
* @var resource
*/
private $connection = null;
/**
* Constructor method
* @param array $params is an array contains parameters to connection
*/
public function __construct($params = [])
{
$this->setLdapServer($params['ldapserver'])
->setLdapPort($params['ldapport'])
->setBaseDn($params['basedn'])
->setBasePassword($params['basepass']);
$this->connection = $this->connect($this->ldapServer, $this->ldapPort);
$this->bind();
}
/**
* Search public method, return first and one found entry
* @param string $searchdn is a search path, an example: 'OU=Пользователи и компьютеры,DC=megafon,DC=ru'
* @param string $filter an AD filter, an example: "(&(objectClass=user)(sAMAccountName=login))"
* @param array $attributes contains an active directory attributes.
*
* @return array if results exists
*/
public function search(string $searchdn, string $filter, array $attributes = [])
{
if (empty($this->connection)) {
return [];
}
$sr = ldap_search($this->connection, $searchdn, $filter, $attributes);
if (!$sr) {
return [];
}
$info = ldap_get_entries($this->connection, $sr);
if ($info["count"] == 1) {
return $info;
}
return [];
}
/**
* Search public method. distinction with the $this->search() can return more than 1 result
* @param string $searchdn is a search path, an example: 'OU=Пользователи и компьютеры,DC=megafon,DC=ru'
* @param string $filter an AD filter, an example: "(&(objectClass=user)(sAMAccountName=login))"
* @param array $attributes contains an active directory attributes.
*
* @return array if results exists
*/
public function search2($searchdn, $filter, $attributes = array())
{
if (empty($this->connection)) {
return [];
}
$sr = ldap_search($this->connection, $searchdn, $filter, $attributes);
if (!$sr) {
return [];
}
$info = ldap_get_entries($this->connection, $sr);
return $info;
}
/**
* The close method
* @param $connection
*
* @return bool
*/
public function close($connection)
{
ldap_close($connection);
return true;
}
/**
* @return string
*/
public function getLdapServer()
{
return $this->ldapServer;
}
/**
* @param string $ldapServer
* @return $this
*/
public function setLdapServer($ldapServer)
{
$this->ldapServer = $ldapServer;
return $this;
}
/**
* @return int
*/
public function getLdapPort()
{
return $this->ldapPort;
}
/**
* @param int $ldapPort
* @return $this
*/
public function setLdapPort($ldapPort)
{
$this->ldapPort = $ldapPort;
return $this;
}
/**
* @return string
*/
public function getBaseDn()
{
return $this->baseDn;
}
/**
* @param string $baseDn
* @return $this
*/
public function setBaseDn($baseDn)
{
$this->baseDn = $baseDn;
return $this;
}
/**
* @return string
*/
public function getBasePassword()
{
return $this->basePassword;
}
/**
* @param string $basePassword
* @return $this
*/
public function setBasePassword($basePassword)
{
$this->basePassword = $basePassword;
return $this;
}
/**
* private method for connect with a AD server
* @param string $server ip address of a domain controller.
* @param int $port a port of a domain controller.
*
* @return self
*/
private function connect(string $server, int $port)
{
if (empty($this->connection)) {
$connection = ldap_connect($server, $port);
ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, 3);
}
return $this;
}
/**
* private method for bind a domain controller. Return integer, if all good is 1, if something wrong is 0
*
* @return bool
*/
private function bind()
{
$bind = ldap_bind($this->connection, $this->baseDn, $this->basePassword);
if (!empty($bind)) {
return true;
}
return false;
}
}