107 if(!function_exists(
'ldap_connect')) {
108 $this->
set_error(
'LDAP support missing from PHP');
111 if(is_array($param)) {
112 $this->server = $param[
'host'];
113 $this->basedn = $param[
'base'];
114 if(!empty($param[
'port'])) {
115 $this->port = $param[
'port'];
117 if(!empty($param[
'charset'])) {
118 $this->charset = strtolower($param[
'charset']);
120 if(isset($param[
'maxrows'])) {
121 $this->maxrows = $param[
'maxrows'];
123 if(isset($param[
'timeout'])) {
124 $this->timeout = $param[
'timeout'];
126 if(isset($param[
'binddn'])) {
127 $this->binddn = $param[
'binddn'];
129 if(isset($param[
'bindpw'])) {
130 $this->bindpw = $param[
'bindpw'];
132 if(isset($param[
'protocol'])) {
133 $this->protocol = $param[
'protocol'];
135 if(empty($param[
'name'])) {
136 $this->sname =
'LDAP: ' . $param[
'host'];
139 $this->sname = $param[
'name'];
144 $this->
set_error(
'Invalid argument to constructor');
158 if($this->linkid !=
false && !$new) {
162 $this->linkid = @ldap_connect($this->server, $this->port);
164 if(function_exists(
'ldap_error')) {
165 return $this->
set_error(ldap_error($this->linkid));
167 return $this->
set_error(
'ldap_connect failed');
171 if(!empty($this->protocol)) {
172 if(!@ldap_set_option($this->linkid, LDAP_OPT_PROTOCOL_VERSION, $this->protocol)) {
173 if(function_exists(
'ldap_error')) {
174 return $this->
set_error(ldap_error($this->linkid));
176 return $this->
set_error(
'ldap_set_option failed');
181 if(!empty($this->binddn)) {
182 if(!@ldap_bind($this->linkid, $this->binddn, $this->bindpw)) {
183 if(function_exists(
'ldap_error')) {
184 return $this->
set_error(ldap_error($this->linkid));
186 return $this->
set_error(
'authenticated ldap_bind failed');
190 if(!@ldap_bind($this->linkid)) {
191 if(function_exists(
'ldap_error')) {
192 return $this->
set_error(ldap_error($this->linkid));
194 return $this->
set_error(
'anonymous ldap_bind failed');
243 $sanitized=array(
'\\' =>
'\5c',
249 return str_replace(array_keys($sanitized),array_values($sanitized),$string);
261 if(is_array($expr))
return false;
273 $expr = str_replace(
'\2a',
'*',$expr);
275 $expression =
"cn=$expr";
282 $sret = @ldap_search($this->linkid, $this->basedn, $expression,
283 array(
'dn',
'o',
'ou',
'sn',
'givenname',
'cn',
'mail'),
284 0, $this->maxrows, $this->timeout);
289 if(function_exists(
'ldap_error')) {
290 return $this->
set_error(ldap_error($this->linkid));
292 return $this->
set_error(
'ldap_search failed');
296 if(@ldap_count_entries($this->linkid, $sret) <= 0) {
303 $res = @ldap_get_entries($this->linkid, $sret);
304 for($i = 0 ; $i < $res[
'count'] ; $i++) {
312 if(!empty($row[
'ou'][0])) {
315 else if(!empty($row[
'o'][0])) {
321 if(empty($row[
'givenname'][0])) {
327 if(empty($row[
'sn'][0])) {
334 if(isset($row[
'mail'][
'count'])) {
335 for(
$j = 0 ;
$j < $row[
'mail'][
'count'] ;
$j++) {
336 array_push(
$ret, array(
'nickname' => $nickname,
338 'firstname' => $firstname,
339 'lastname' => $surname,
340 'email' => $row[
'mail'][
$j],
342 'backend' => $this->bnum,
343 'source' => &$this->sname));
347 if(($returned_rows >= $this->maxrows) &&
348 ($this->maxrows > 0) ) {
349 ldap_free_result($sret);
359 ldap_free_result($sret);