File: abook_ldap_server.php

package info (click to toggle)
squirrelmail 1%3A1.2.6-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,300 kB
  • ctags: 4,949
  • sloc: php: 21,297; perl: 2,538; sh: 350; ansic: 122; makefile: 69
file content (267 lines) | stat: -rw-r--r-- 8,946 bytes parent folder | download
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php

/**
 * abook_ldap_server.php
 *
 * Copyright (c) 1999-2002 The Squirrelmail Project Team
 * Licensed under the GNU GPL. For full terms see the file COPYING.
 *
 * Address book backend for LDAP server
 *
 * An array with the following elements must be passed to
 * the class constructor (elements marked ? are optional):
 *
 *    host      => LDAP server hostname/IP-address
 *    base      => LDAP server root (base dn). Empty string allowed.
 *  ? port      => LDAP server TCP port number (default: 389)
 *  ? charset   => LDAP server charset (default: utf-8)
 *  ? name      => Name for LDAP server (default "LDAP: hostname")
 *                 Used to tag the result data
 *  ? maxrows   => Maximum # of rows in search result
 *  ? timeout   => Timeout for LDAP operations (in seconds, default: 30)
 *                 Might not work for all LDAP libraries or servers.
 *
 * NOTE. This class should not be used directly. Use the
 *       "AddressBook" class instead.
 *
 * $Id: abook_ldap_server.php,v 1.16 2002/01/28 20:47:26 kink Exp $
 */

class abook_ldap_server extends addressbook_backend {
    var $btype = 'remote';
    var $bname = 'ldap_server';

    /* Parameters changed by class */
    var $sname   = 'LDAP';       /* Service name */
    var $server  = '';           /* LDAP server name */
    var $port    = 389;          /* LDAP server port */
    var $basedn  = '';           /* LDAP base DN  */
    var $charset = 'utf-8';      /* LDAP server charset */
    var $linkid  = false;        /* PHP LDAP link ID */
    var $bound   = false;        /* True if LDAP server is bound */
    var $maxrows = 250;          /* Max rows in result */
    var $timeout = 30;           /* Timeout for LDAP operations (in seconds) */

    /* Constructor. Connects to database */
    function abook_ldap_server($param) {
        if(!function_exists('ldap_connect')) {
            $this->set_error('LDAP support missing from PHP');
            return;
        }
        if(is_array($param)) {
            $this->server = $param['host'];
            $this->basedn = $param['base'];
            if(!empty($param['port'])) {
                $this->port = $param['port'];
            }
            if(!empty($param['charset'])) {
                $this->charset = strtolower($param['charset']);
            }
            if(isset($param['maxrows'])) {
                $this->maxrows = $param['maxrows'];
            }
            if(isset($param['timeout'])) {
                $this->timeout = $param['timeout'];
            }
            if(empty($param['name'])) {
                $this->sname = 'LDAP: ' . $param['host'];
            }
            else {
                $this->sname = $param['name'];
            }
            
            $this->open(true);
        } else {
            $this->set_error('Invalid argument to constructor');
        }
    }


    /* Open the LDAP server. New connection if $new is true */
    function open($new = false) {
        $this->error = '';
  
        /* Connection is already open */
        if($this->linkid != false && !$new) {
            return true;
        }
  
        $this->linkid = @ldap_connect($this->server, $this->port);
        if(!$this->linkid) {
            if(function_exists('ldap_error')) {
                return $this->set_error(ldap_error($this->linkid)); 
            } else {
                return $this->set_error('ldap_connect failed');
            }
        }
        
        if(!@ldap_bind($this->linkid)) {
            if(function_exists('ldap_error')) {
                return $this->set_error(ldap_error($this->linkid)); 
            } else {
                return $this->set_error('ldap_bind failed');
            }
        }
        
        $this->bound = true;
        
        return true;
    }


    /* Encode iso8859-1 string to the charset used by this LDAP server */
    function charset_encode($str) {
        if($this->charset == 'utf-8') {
            if(function_exists('utf8_encode')) {
                return utf8_encode($str);
            } else {
                return $str;
            }
        } else {
            return $str;
        }
    }


    /* Decode from charset used by this LDAP server to iso8859-1 */
    function charset_decode($str) {
        if($this->charset == 'utf-8') {
            if(function_exists('utf8_decode')) {
                return utf8_decode($str);
            } else {
                return $str;
            }
        } else {
            return $str;
        }
    }

    
    /* ========================== Public ======================== */

    /* Search the LDAP server */
    function search($expr) {
  
        /* To be replaced by advanded search expression parsing */
        if(is_array($expr)) return false;
  
        /* Encode the expression */
        $expr = $this->charset_encode($expr);
        if(strstr($expr, '*') === false) {
            $expr = "*$expr*";
        }
        $expression = "cn=$expr";
  
        /* Make sure connection is there */
        if(!$this->open()) {
            return false;
        }
  
        /* Do the search. Use improved ldap_search() if PHP version is
         * 4.0.2 or newer. */
        if(sqCheckPHPVersion(4, 0, 2)) {
            $sret = @ldap_search($this->linkid, $this->basedn, $expression,
                array('dn', 'o', 'ou', 'sn', 'givenname', 
                'cn', 'mail', 'telephonenumber'),
                0, $this->maxrows, $this->timeout);
        } else {
            $sret = @ldap_search($this->linkid, $this->basedn, $expression,
                array('dn', 'o', 'ou', 'sn', 'givenname', 
                'cn', 'mail', 'telephonenumber'));
        }
  
        /* Should get error from server using the ldap_error() function,
         * but it only exist in the PHP LDAP documentation. */
        if(!$sret) {
            if(function_exists('ldap_error')) {
                return $this->set_error(ldap_error($this->linkid)); 
            } else {
                return $this->set_error('ldap_search failed');       
            }
        }
        
        if(@ldap_count_entries($this->linkid, $sret) <= 0) {
            return array();
        }
  
        /* Get results */
        $ret = array();
        $returned_rows = 0;
        $res = @ldap_get_entries($this->linkid, $sret);
        for($i = 0 ; $i < $res['count'] ; $i++) {
            $row = $res[$i];
           
            /* Extract data common for all e-mail addresses
             * of an object. Use only the first name */
            $nickname = $this->charset_decode($row['dn']);
            $fullname = $this->charset_decode($row['cn'][0]);
           
            if(empty($row['telephonenumber'][0])) {
                $phone = '';
            } else {
                $phone = $this->charset_decode($row['telephonenumber'][0]);
            }
           
            if(!empty($row['ou'][0])) {
                $label = $this->charset_decode($row['ou'][0]);
            }
            else if(!empty($row['o'][0])) {
                $label = $this->charset_decode($row['o'][0]);
            } else {
                $label = '';
            }
           
            if(empty($row['givenname'][0])) {
                $firstname = '';
            } else {
                $firstname = $this->charset_decode($row['givenname'][0]);
            }
           
            if(empty($row['sn'][0])) {
                $surname = '';
            } else {
                $surname = $this->charset_decode($row['sn'][0]);
            }
           
            /* Add one row to result for each e-mail address */
            if(isset($row['mail']['count'])) {
                for($j = 0 ; $j < $row['mail']['count'] ; $j++) {
                    array_push($ret, array('nickname'  => $nickname,
                   'name'      => $fullname,
                   'firstname' => $firstname,
                   'lastname'  => $surname,
                   'email'     => $row['mail'][$j],
                   'label'     => $label,
                   'phone'     => $phone,
                   'backend'   => $this->bnum,
                   'source'    => &$this->sname));
                    
                    // Limit number of hits
                    $returned_rows++;
                    if(($returned_rows >= $this->maxrows) && 
                       ($this->maxrows > 0) ) {
                        ldap_free_result($sret);
                        return $ret;
                    }

                } // for($j ...)

            } // isset($row['mail']['count'])
 
        }
  
        ldap_free_result($sret);
        return $ret;
    } /* end search() */


    /* If you run a tiny LDAP server and you want the "List All" button
     * to show EVERYONE, then uncomment this tiny block of code:
     *
     * function list_addr() {
     *    return $this->search('*');
     * }
     *
     * Careful with this -- it could get quite large for big sites. */
}
?>