Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ldap.inc
1 <?php
29 class Ldap
30 {
31 
38  var $host = '';
39 
46  var $port = 0;
47 
53  var $ptr = null;
54 
55 
65  function Ldap($h=null, $p=null, $pass=null, $bdn='')
66  {
67  // if a host has been specified, connect
68  if (!is_null($h) && !empty($h)) {
69  $this->connect($h, $p, $pass, $bdn);
70  $this->bind($bdn, $pass);
71  }
72 
73  }//end constructor
74 
75 
85  function connect($h, $p=null)
86  {
87  // bail out safely if we have no ldap functions
88  if (!extension_loaded('ldap')) {
89  $this->ptr = null;
90  return false;
91  }
92 
93  if ($h != $this->host || $p != $this->port) {
94  // close old connection
95  if ($this->ptr) ldap_close($this->ptr);
96 
97  if (!($this->ptr = ldap_connect($h, $p))) {
98  trigger_error('Unable to connect to LDAP server: '.$h.' on port '.$p, E_USER_WARNING);
99  return false;
100  }
101  $this->host = $h;
102  $this->port = $p;
103  }
104  return true;
105 
106  }//end connect()
107 
108 
118  function bind($bdn='', $pass=null)
119  {
120  if (!$this->ptr) return false;
121 
122  // bind to the server
123  if (is_null($pass)) {
124  // attempting an anonymous bind
125  if (!($r = ldap_bind($this->ptr))) {
126  trigger_error('Unable to Anonomously bind to LDAP server: '.$this->host, E_USER_WARNING);
127  unset($this->ptr);
128  return false;
129  }
130  } else {
131  if (!($r = ldap_bind($this->ptr, $bdn, $pass))) {
132  trigger_error('Unable to bind to LDAP server: '.$this->host.' BDN: '.$bdn.' Using Password: '.(($pass) ? 'YES' : 'NO'), E_USER_WARNING);
133  $this->ptr = null;
134  return false;
135  }
136  }
137 
138  return $r;
139 
140  }//end bind()
141 
142 
149  function disconnect()
150  {
151  if ($this->ptr) {
152  return ldap_close($this->ptr);
153  }
154  return true;
155 
156  }//end disconnect()
157 
158 
168  function connectAsUser($bdn, $pass)
169  {
170  $conn_result = $this->connect($this->host, $this->port);
171  if (!$conn_result) return false;
172  return $this->bind($bdn, $pass);
173 
174  }//end connectAsUser()
175 
176 
189  function search($startdn, $filter, $sort_by=null, $multi_level=true, $multi_result=true, $attributes=Array())
190  {
191  if (!$this->ptr) return 0;
192 
193  if(!empty($attributes)) {
194  if ($multi_level && $multi_result) $sr = @ldap_search($this->ptr, $startdn, $filter, $attributes);
195  else if ($multi_result) $sr = @ldap_list($this->ptr, $startdn, $filter, $attributes);
196  else $sr = @ldap_read($this->ptr, $startdn, $filter, $attributes);
197  } else {
198  if ($multi_level && $multi_result) $sr = @ldap_search($this->ptr, $startdn, $filter);
199  else if ($multi_result) $sr = @ldap_list($this->ptr, $startdn, $filter);
200  else $sr = @ldap_read($this->ptr, $startdn, $filter);
201  }
202 
203  if (!$sr) {
204  return 0;
205  } else {
206  if (!is_null($sort_by)) ldap_sort($this->ptr, $sr, $sort_by);
207  return $sr;
208  }
209 
210  }//end search()
211 
212 
222  function getEntries($result, $binary_attributes = Array())
223  {
224  if (!($info = ldap_get_entries($this->ptr, $result))) {
225  trigger_error('Unable to get entries for LDAP search result ['.$result.'] :'.ldap_error($this->ptr), E_USER_WARNING);
226  return Array();
227  } else {
228  // ldap_get_entries() function can not handle binary data so we use ldap_get_values_len() to
229  // put binary data to the binary attributes of $info
230  if (!empty($binary_attributes) && ($info['count'] > 0)) {
231  // There is no document confirming that the order of entries returned by ldap_get_entries()
232  // is the same with that of ldap_first_entry()/ldap_next_entry() so we use the dn as identifier
233  $entry_order = Array();
234  for ($i = 0; $i < $info['count']; $i++) {
235  $entry_order[$info[$i]['dn']] = $i;
236  }
237  // Set binary data for each entry
238  for ($entry_identifier = ldap_first_entry($this->ptr, $result); $entry_identifier !== FALSE; $entry_identifier = ldap_next_entry($this->ptr, $entry_identifier)) {
239  $dn = ldap_get_dn($this->ptr, $entry_identifier);
240  if (($dn !== FALSE) && isset($entry_order[$dn])) {
241  $entry_index = $entry_order[$dn];
242  foreach ($binary_attributes as $attribute) {
243  // ldap_get_entries() lowercases attribute indexes
244  $lowercased_attribute = strtolower($attribute);
245  // ldap_get_entries() can still get the first few bytes of the binary data, so we can speed this function
246  // a bit by checking if there is a value of this attribute before extracting its actual binary data
247  if (isset($info[$entry_index][$lowercased_attribute])) {
248  // ldap_get_values_len() function throws a warning if there is no value for the attribute so we suppress the warning with @
249  $binary_value = @ldap_get_values_len($this->ptr, $entry_identifier, $attribute);
250  if ($binary_value !== FALSE) {
251  $info[$entry_index][$lowercased_attribute] = $binary_value;
252  }
253  }
254  }
255  }
256  }
257  }
258 
259  return $info;
260  }
261 
262  }//end getEntries()
263 
264 
273  function getNumEntries($result)
274  {
275  if (!$result) return 0;
276  return ldap_count_entries($this->ptr, $result);
277 
278  }//end getNumEntries()
279 
280 
290  function addEntry($dn, $details)
291  {
292  if (!ldap_add($this->ptr, $dn, $details)) {
293  trigger_error('Unable to INSERT entry into LDAP at DN: ['.$dn.'] '.ldap_error($this->ptr), E_USER_WARNING);
294  return false;
295  }
296  return true;
297 
298  }//end addEntry()
299 
300 
310  function modifyEntry($dn, $details)
311  {
312  if (!ldap_modify($this->ptr, $dn, $details)) {
313  trigger_error('Unable to MODIFY entry in LDAP at DN: ['.$dn.'] '.ldap_error($this->ptr), E_USER_WARNING);
314  }
315  return true;
316 
317  }//end modifyEntry()
318 
319 
328  function deleteEntry($dn)
329  {
330  if (!ldap_delete($this->ptr, $dn)) {
331  trigger_error('Unable to DELETE entry from LDAP at DN: ['.$dn.'] '.ldap_error($this->ptr), E_USER_WARNING);
332  }
333  return true;
334 
335  }//end deleteEntry()
336 
337 
346  function escape_filter_value($str){
347 
348  $metaChars = array('\\', '(', ')', '*');
349  $quotedMetaChars = array();
350  foreach ($metaChars as $key => $value) $quotedMetaChars[$key] = '\\'.dechex(ord($value));
351  $str=str_replace($metaChars,$quotedMetaChars,$str); //replace them
352  return ($str);
353 
354  }
355 
356 
357 }//end class
358 
359 ?>