squirrelmail-webmail  1.4.22
About: SquirrelMail is a standards-based webmail package with strong MIME support, address books, and folder manipulation (written in PHP4).
  Fossies Dox: squirrelmail-webmail-1.4.22.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

abook_ldap_server.php
Go to the documentation of this file.
1 <?php
2 
43  var $btype = 'remote';
47  var $bname = 'ldap_server';
48 
49  /* Parameters changed by class */
53  var $sname = 'LDAP'; /* Service name */
57  var $server = '';
61  var $port = 389;
65  var $basedn = '';
69  var $charset = 'utf-8';
73  var $linkid = false;
77  var $bound = false;
81  var $maxrows = 250;
85  var $timeout = 30;
90  var $binddn = '';
95  var $bindpw = '';
100  var $protocol = '';
101 
106  function abook_ldap_server($param) {
107  if(!function_exists('ldap_connect')) {
108  $this->set_error('LDAP support missing from PHP');
109  return;
110  }
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'];
116  }
117  if(!empty($param['charset'])) {
118  $this->charset = strtolower($param['charset']);
119  }
120  if(isset($param['maxrows'])) {
121  $this->maxrows = $param['maxrows'];
122  }
123  if(isset($param['timeout'])) {
124  $this->timeout = $param['timeout'];
125  }
126  if(isset($param['binddn'])) {
127  $this->binddn = $param['binddn'];
128  }
129  if(isset($param['bindpw'])) {
130  $this->bindpw = $param['bindpw'];
131  }
132  if(isset($param['protocol'])) {
133  $this->protocol = $param['protocol'];
134  }
135  if(empty($param['name'])) {
136  $this->sname = 'LDAP: ' . $param['host'];
137  }
138  else {
139  $this->sname = $param['name'];
140  }
141 
142  $this->open(true);
143  } else {
144  $this->set_error('Invalid argument to constructor');
145  }
146  }
147 
148 
154  function open($new = false) {
155  $this->error = '';
156 
157  /* Connection is already open */
158  if($this->linkid != false && !$new) {
159  return true;
160  }
161 
162  $this->linkid = @ldap_connect($this->server, $this->port);
163  if(!$this->linkid) {
164  if(function_exists('ldap_error')) {
165  return $this->set_error(ldap_error($this->linkid));
166  } else {
167  return $this->set_error('ldap_connect failed');
168  }
169  }
170 
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));
175  } else {
176  return $this->set_error('ldap_set_option failed');
177  }
178  }
179  }
180 
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));
185  } else {
186  return $this->set_error('authenticated ldap_bind failed');
187  }
188  }
189  } else {
190  if(!@ldap_bind($this->linkid)) {
191  if(function_exists('ldap_error')) {
192  return $this->set_error(ldap_error($this->linkid));
193  } else {
194  return $this->set_error('anonymous ldap_bind failed');
195  }
196  }
197  }
198 
199  $this->bound = true;
200 
201  return true;
202  }
203 
209  function charset_encode($str) {
210  global $default_charset;
211  if($this->charset != $default_charset) {
212  return charset_convert($default_charset,$str,$this->charset,false);
213  } else {
214  return $str;
215  }
216  }
217 
225  function charset_decode($str) {
226  global $default_charset;
227  if ($this->charset != $default_charset) {
228  return charset_convert($this->charset,$str,$default_charset,false);
229  } else {
230  return $str;
231  }
232  }
233 
242  function ldapspecialchars($string) {
243  $sanitized=array('\\' => '\5c',
244  '*' => '\2a',
245  '(' => '\28',
246  ')' => '\29',
247  "\x00" => '\00');
248 
249  return str_replace(array_keys($sanitized),array_values($sanitized),$string);
250  }
251 
252  /* ========================== Public ======================== */
253 
259  function search($expr) {
260  /* To be replaced by advanded search expression parsing */
261  if(is_array($expr)) return false;
262 
263  /* Encode the expression */
264  $expr = $this->charset_encode($expr);
265 
266  /*
267  * allow use of one asterisk in search.
268  * Don't allow any ldap special chars if search is different
269  */
270  if($expr!='*') {
271  $expr = '*' . $this->ldapspecialchars($expr) . '*';
272  /* Undo sanitizing of * symbol */
273  $expr = str_replace('\2a','*',$expr);
274  }
275  $expression = "cn=$expr";
276 
277  /* Make sure connection is there */
278  if(!$this->open()) {
279  return false;
280  }
281 
282  $sret = @ldap_search($this->linkid, $this->basedn, $expression,
283  array('dn', 'o', 'ou', 'sn', 'givenname', 'cn', 'mail'),
284  0, $this->maxrows, $this->timeout);
285 
286  /* Should get error from server using the ldap_error() function,
287  * but it only exist in the PHP LDAP documentation. */
288  if(!$sret) {
289  if(function_exists('ldap_error')) {
290  return $this->set_error(ldap_error($this->linkid));
291  } else {
292  return $this->set_error('ldap_search failed');
293  }
294  }
295 
296  if(@ldap_count_entries($this->linkid, $sret) <= 0) {
297  return array();
298  }
299 
300  /* Get results */
301  $ret = array();
302  $returned_rows = 0;
303  $res = @ldap_get_entries($this->linkid, $sret);
304  for($i = 0 ; $i < $res['count'] ; $i++) {
305  $row = $res[$i];
306 
307  /* Extract data common for all e-mail addresses
308  * of an object. Use only the first name */
309  $nickname = $this->charset_decode($row['dn']);
310  $fullname = $this->charset_decode($row['cn'][0]);
311 
312  if(!empty($row['ou'][0])) {
313  $label = $this->charset_decode($row['ou'][0]);
314  }
315  else if(!empty($row['o'][0])) {
316  $label = $this->charset_decode($row['o'][0]);
317  } else {
318  $label = '';
319  }
320 
321  if(empty($row['givenname'][0])) {
322  $firstname = '';
323  } else {
324  $firstname = $this->charset_decode($row['givenname'][0]);
325  }
326 
327  if(empty($row['sn'][0])) {
328  $surname = '';
329  } else {
330  $surname = $this->charset_decode($row['sn'][0]);
331  }
332 
333  /* Add one row to result for each e-mail address */
334  if(isset($row['mail']['count'])) {
335  for($j = 0 ; $j < $row['mail']['count'] ; $j++) {
336  array_push($ret, array('nickname' => $nickname,
337  'name' => $fullname,
338  'firstname' => $firstname,
339  'lastname' => $surname,
340  'email' => $row['mail'][$j],
341  'label' => $label,
342  'backend' => $this->bnum,
343  'source' => &$this->sname));
344 
345  // Limit number of hits
346  $returned_rows++;
347  if(($returned_rows >= $this->maxrows) &&
348  ($this->maxrows > 0) ) {
349  ldap_free_result($sret);
350  return $ret;
351  }
352 
353  } // for($j ...)
354 
355  } // isset($row['mail']['count'])
356 
357  }
358 
359  ldap_free_result($sret);
360  return $ret;
361  } /* end search() */
362 
363 
374  function list_addr() {
375  return array();
376  // return $this->search('*');
377  }
378 }
abook_ldap_server\ldapspecialchars
ldapspecialchars($string)
Definition: abook_ldap_server.php:242
$ret
$ret
Definition: webmail.php:172
abook_ldap_server\$binddn
$binddn
Definition: abook_ldap_server.php:90
abook_ldap_server\$protocol
$protocol
Definition: abook_ldap_server.php:100
abook_ldap_server\$timeout
$timeout
Definition: abook_ldap_server.php:85
addressbook_backend\set_error
set_error($string)
Definition: addressbook.php:656
abook_ldap_server\open
open($new=false)
Definition: abook_ldap_server.php:154
abook_ldap_server\search
search($expr)
Definition: abook_ldap_server.php:259
abook_ldap_server\$server
$server
Definition: abook_ldap_server.php:57
abook_ldap_server\abook_ldap_server
abook_ldap_server($param)
Definition: abook_ldap_server.php:106
abook_ldap_server\list_addr
list_addr()
Definition: abook_ldap_server.php:374
abook_ldap_server\$bname
$bname
Definition: abook_ldap_server.php:47
abook_ldap_server\$charset
$charset
Definition: abook_ldap_server.php:69
abook_ldap_server\$bound
$bound
Definition: abook_ldap_server.php:77
abook_ldap_server\$sname
$sname
Definition: abook_ldap_server.php:53
abook_ldap_server\charset_encode
charset_encode($str)
Definition: abook_ldap_server.php:209
$j
for($i=0; $i< $numboxes; $i++) $j
Definition: empty_trash.php:72
addressbook_backend
Definition: addressbook.php:641
abook_ldap_server\$basedn
$basedn
Definition: abook_ldap_server.php:65
abook_ldap_server\$linkid
$linkid
Definition: abook_ldap_server.php:73
abook_ldap_server
Definition: abook_ldap_server.php:39
abook_ldap_server\$btype
$btype
Definition: abook_ldap_server.php:43
$default_charset
$default_charset
Definition: config_default.php:1027
charset_convert
charset_convert($in_charset, $string, $out_charset, $htmlencode=true)
Definition: i18n.php:264
abook_ldap_server\$bindpw
$bindpw
Definition: abook_ldap_server.php:95
abook_ldap_server\$port
$port
Definition: abook_ldap_server.php:61
abook_ldap_server\$maxrows
$maxrows
Definition: abook_ldap_server.php:81
abook_ldap_server\charset_decode
charset_decode($str)
Definition: abook_ldap_server.php:225