1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Ldap |
5
|
|
|
* |
6
|
|
|
* @category lib |
7
|
|
|
* @author Judicaël Paquet <[email protected]> |
8
|
|
|
* @copyright Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93) |
9
|
|
|
* @license https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël |
10
|
|
|
* @version Release: 1.0.0 |
11
|
|
|
* @filesource https://github.com/las93/venus2 |
12
|
|
|
* @link https://github.com/las93 |
13
|
|
|
* @since 1.0.2 |
14
|
|
|
*/ |
15
|
|
|
namespace Venus\lib; |
16
|
|
|
|
17
|
|
|
use \Venus\core\Config as Config; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Ldap library |
21
|
|
|
* |
22
|
|
|
* @category core |
23
|
|
|
* @author Judicaël Paquet <[email protected]> |
24
|
|
|
* @copyright Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93) |
25
|
|
|
* @license https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël |
26
|
|
|
* @version Release: 1.0.0 |
27
|
|
|
* @filesource https://github.com/las93/venus2 |
28
|
|
|
* @link https://github.com/las93 |
29
|
|
|
* @since 1.0.2 |
30
|
|
|
*/ |
31
|
|
|
class Ldap |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* connection at ldap server |
35
|
|
|
* |
36
|
|
|
* @access private |
37
|
|
|
* @var resource |
38
|
|
|
*/ |
39
|
|
|
private $_rConnect; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* the databse to connect |
43
|
|
|
* |
44
|
|
|
* @access private |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $_sBase; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* if the user is connected or not |
51
|
|
|
* |
52
|
|
|
* @access private |
53
|
|
|
* @var bool |
54
|
|
|
*/ |
55
|
|
|
private $_bConnected = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* constructor of class |
59
|
|
|
* |
60
|
|
|
* @access public |
61
|
|
|
* @return \Venus\lib\Ldap |
|
|
|
|
62
|
|
|
*/ |
63
|
|
|
public function __construct() |
64
|
|
|
{ |
65
|
|
|
$oDbConf = Config::get('Ldap')->configuration; |
66
|
|
|
|
67
|
|
|
$this->_sBase = $oDbConf->base; |
68
|
|
|
|
69
|
|
|
$this->_rConnect = ldap_connect($oDbConf->host, $oDbConf->port); |
70
|
|
|
|
71
|
|
|
$this->set_option(LDAP_OPT_REFERRALS, 0); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get groups |
76
|
|
|
* |
77
|
|
|
* @access public |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getGroups() : array |
81
|
|
|
{ |
82
|
|
|
$rSearch = ldap_search( $this->_rConnect , $this->_sBase , "objectclass=group" , array("cn") ); |
83
|
|
|
$aEntries = ldap_get_entries($this->_rConnect, $rSearch); |
84
|
|
|
$aGroups = array(); |
85
|
|
|
|
86
|
|
|
for ( $i = 0 ; $i < $aEntries["count"] ; $i++ ) { |
87
|
|
|
|
88
|
|
|
$aGroups[] = utf8_encode($aEntries[$i]["dn"]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $aGroups; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Authentification in Ldap |
96
|
|
|
* |
97
|
|
|
* @access public |
98
|
|
|
* @param string $sUser |
99
|
|
|
* @param string $sPassword |
100
|
|
|
* @return \Venus\lib\Ldap |
101
|
|
|
*/ |
102
|
|
|
public function bind($sUser, $sPassword) : Ldap |
103
|
|
|
{ |
104
|
|
|
return $this->_bConnected = ldap_bind($this->_rConnect, $sUser, $sPassword); |
105
|
|
|
return $this; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Close authentification in Ldap |
110
|
|
|
* |
111
|
|
|
* @access public |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function unbind() : bool |
115
|
|
|
{ |
116
|
|
|
if ($this->_bConnected) { return $this->_bConnected = ldap_unbind($this->_rConnect); } |
117
|
|
|
else { return true; } |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* destructor of the class |
122
|
|
|
* |
123
|
|
|
* @access public |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function __destruct() |
127
|
|
|
{ |
128
|
|
|
$this->close(); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Call a classic ldap method. You have to ignore the ldap_ part |
133
|
|
|
* You put all parameters without the connector |
134
|
|
|
* |
135
|
|
|
* @access public |
136
|
|
|
* @param string $sFunctionName |
137
|
|
|
* @param array $aArgv |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
public function __call(string $sFunctionName, array $aArgv) |
141
|
|
|
{ |
142
|
|
|
array_unshift($argv, $this->_rConnect); |
143
|
|
|
return call_user_func_array('ldap_'.$sFunctionName, $aArgv); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* get in Ldap |
148
|
|
|
* |
149
|
|
|
* @access public |
150
|
|
|
* @param string $sFilter |
151
|
|
|
* @param array $aAttributes |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
public function get(string $sFilter, array $aAttributes) |
155
|
|
|
{ |
156
|
|
|
$res = $this->search($sFilter, $aAttributes); |
157
|
|
|
|
158
|
|
|
return $this->getEntries($res, $aAttributes); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* search in Ldap |
163
|
|
|
* |
164
|
|
|
* @access public |
165
|
|
|
* @param string $sFilter |
166
|
|
|
* @param array $aAttributes |
167
|
|
|
* @return resource |
168
|
|
|
*/ |
169
|
|
|
public function search(string $sFilter, array $aAttributes) |
170
|
|
|
{ |
171
|
|
|
return ldap_search($this->_rConnect, $this->_sBase, $sFilter, $aAttributes); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* get in Ldap |
176
|
|
|
* |
177
|
|
|
* @access public |
178
|
|
|
* @param resource $rResultIdentifier |
179
|
|
|
* @param array $aAttributes |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
|
|
public function getEntries($rResultIdentifier, array $aAttributes) : array |
183
|
|
|
{ |
184
|
|
|
$aEntries = ldap_get_entries($this->_rConnect, $rResultIdentifier); |
185
|
|
|
|
186
|
|
|
$aMask = array_flip($aAttributes); |
187
|
|
|
|
188
|
|
|
$aResultSet = array(); |
189
|
|
|
|
190
|
|
|
for ($i = 0, $count = $aEntries['count']; $i < $count; ++$i) { |
191
|
|
|
|
192
|
|
|
$aResultSet[$i] = array_intersect_key($aEntries[$i], $aMask); |
193
|
|
|
|
194
|
|
|
foreach($aResultSet[$i] as &$aValues) { |
195
|
|
|
|
196
|
|
|
unset($aValues['count']); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $aResultSet; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.