5100704d |
1 | <?php |
2 | |
35586184 |
3 | /** |
4 | * abook_ldap_server.php |
5 | * |
76911253 |
6 | * Copyright (c) 1999-2003 The SquirrelMail Project Team |
35586184 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * Address book backend for LDAP server |
10 | * |
11 | * An array with the following elements must be passed to |
12 | * the class constructor (elements marked ? are optional): |
13 | * |
14 | * host => LDAP server hostname/IP-address |
15 | * base => LDAP server root (base dn). Empty string allowed. |
16 | * ? port => LDAP server TCP port number (default: 389) |
17 | * ? charset => LDAP server charset (default: utf-8) |
18 | * ? name => Name for LDAP server (default "LDAP: hostname") |
19 | * Used to tag the result data |
20 | * ? maxrows => Maximum # of rows in search result |
21 | * ? timeout => Timeout for LDAP operations (in seconds, default: 30) |
22 | * Might not work for all LDAP libraries or servers. |
23 | * |
24 | * NOTE. This class should not be used directly. Use the |
25 | * "AddressBook" class instead. |
26 | * |
27 | * $Id$ |
28 | */ |
5100704d |
29 | |
35586184 |
30 | class abook_ldap_server extends addressbook_backend { |
06b4facd |
31 | var $btype = 'remote'; |
32 | var $bname = 'ldap_server'; |
33 | |
34 | /* Parameters changed by class */ |
35 | var $sname = 'LDAP'; /* Service name */ |
36 | var $server = ''; /* LDAP server name */ |
37 | var $port = 389; /* LDAP server port */ |
38 | var $basedn = ''; /* LDAP base DN */ |
39 | var $charset = 'utf-8'; /* LDAP server charset */ |
40 | var $linkid = false; /* PHP LDAP link ID */ |
41 | var $bound = false; /* True if LDAP server is bound */ |
42 | var $maxrows = 250; /* Max rows in result */ |
43 | var $timeout = 30; /* Timeout for LDAP operations (in seconds) */ |
44 | |
45 | /* Constructor. Connects to database */ |
46 | function abook_ldap_server($param) { |
47 | if(!function_exists('ldap_connect')) { |
48 | $this->set_error('LDAP support missing from PHP'); |
49 | return; |
50 | } |
51 | if(is_array($param)) { |
52 | $this->server = $param['host']; |
53 | $this->basedn = $param['base']; |
54 | if(!empty($param['port'])) { |
55 | $this->port = $param['port']; |
56 | } |
57 | if(!empty($param['charset'])) { |
58 | $this->charset = strtolower($param['charset']); |
59 | } |
60 | if(isset($param['maxrows'])) { |
61 | $this->maxrows = $param['maxrows']; |
62 | } |
63 | if(isset($param['timeout'])) { |
64 | $this->timeout = $param['timeout']; |
65 | } |
66 | if(empty($param['name'])) { |
67 | $this->sname = 'LDAP: ' . $param['host']; |
68 | } |
69 | else { |
70 | $this->sname = $param['name']; |
71 | } |
72 | |
73 | $this->open(true); |
74 | } else { |
75 | $this->set_error('Invalid argument to constructor'); |
76 | } |
77 | } |
78 | |
79 | |
80 | /* Open the LDAP server. New connection if $new is true */ |
81 | function open($new = false) { |
82 | $this->error = ''; |
83 | |
84 | /* Connection is already open */ |
85 | if($this->linkid != false && !$new) { |
86 | return true; |
87 | } |
88 | |
89 | $this->linkid = @ldap_connect($this->server, $this->port); |
90 | if(!$this->linkid) { |
91 | if(function_exists('ldap_error')) { |
92 | return $this->set_error(ldap_error($this->linkid)); |
93 | } else { |
94 | return $this->set_error('ldap_connect failed'); |
95 | } |
96 | } |
97 | |
98 | if(!@ldap_bind($this->linkid)) { |
99 | if(function_exists('ldap_error')) { |
100 | return $this->set_error(ldap_error($this->linkid)); |
101 | } else { |
102 | return $this->set_error('ldap_bind failed'); |
103 | } |
104 | } |
105 | |
106 | $this->bound = true; |
107 | |
108 | return true; |
109 | } |
110 | |
111 | |
112 | /* Encode iso8859-1 string to the charset used by this LDAP server */ |
113 | function charset_encode($str) { |
114 | if($this->charset == 'utf-8') { |
115 | if(function_exists('utf8_encode')) { |
116 | return utf8_encode($str); |
117 | } else { |
118 | return $str; |
119 | } |
120 | } else { |
121 | return $str; |
122 | } |
123 | } |
124 | |
125 | |
126 | /* Decode from charset used by this LDAP server to iso8859-1 */ |
127 | function charset_decode($str) { |
128 | if($this->charset == 'utf-8') { |
129 | if(function_exists('utf8_decode')) { |
130 | return utf8_decode($str); |
97445d7d |
131 | } else { |
06b4facd |
132 | return $str; |
133 | } |
134 | } else { |
135 | return $str; |
136 | } |
137 | } |
138 | |
139 | |
140 | /* ========================== Public ======================== */ |
141 | |
142 | /* Search the LDAP server */ |
143 | function search($expr) { |
144 | |
145 | /* To be replaced by advanded search expression parsing */ |
146 | if(is_array($expr)) return false; |
147 | |
148 | /* Encode the expression */ |
149 | $expr = $this->charset_encode($expr); |
150 | if(strstr($expr, '*') === false) { |
151 | $expr = "*$expr*"; |
152 | } |
153 | $expression = "cn=$expr"; |
154 | |
155 | /* Make sure connection is there */ |
156 | if(!$this->open()) { |
157 | return false; |
158 | } |
159 | |
cccfa9c2 |
160 | $sret = @ldap_search($this->linkid, $this->basedn, $expression, |
161 | array('dn', 'o', 'ou', 'sn', 'givenname', |
162 | 'cn', 'mail', 'telephonenumber'), |
163 | 0, $this->maxrows, $this->timeout); |
06b4facd |
164 | |
165 | /* Should get error from server using the ldap_error() function, |
166 | * but it only exist in the PHP LDAP documentation. */ |
167 | if(!$sret) { |
168 | if(function_exists('ldap_error')) { |
169 | return $this->set_error(ldap_error($this->linkid)); |
170 | } else { |
171 | return $this->set_error('ldap_search failed'); |
172 | } |
173 | } |
174 | |
175 | if(@ldap_count_entries($this->linkid, $sret) <= 0) { |
176 | return array(); |
177 | } |
178 | |
179 | /* Get results */ |
180 | $ret = array(); |
181 | $returned_rows = 0; |
182 | $res = @ldap_get_entries($this->linkid, $sret); |
183 | for($i = 0 ; $i < $res['count'] ; $i++) { |
184 | $row = $res[$i]; |
185 | |
186 | /* Extract data common for all e-mail addresses |
187 | * of an object. Use only the first name */ |
188 | $nickname = $this->charset_decode($row['dn']); |
189 | $fullname = $this->charset_decode($row['cn'][0]); |
190 | |
191 | if(empty($row['telephonenumber'][0])) { |
192 | $phone = ''; |
193 | } else { |
194 | $phone = $this->charset_decode($row['telephonenumber'][0]); |
195 | } |
196 | |
197 | if(!empty($row['ou'][0])) { |
198 | $label = $this->charset_decode($row['ou'][0]); |
199 | } |
200 | else if(!empty($row['o'][0])) { |
201 | $label = $this->charset_decode($row['o'][0]); |
202 | } else { |
203 | $label = ''; |
204 | } |
205 | |
206 | if(empty($row['givenname'][0])) { |
207 | $firstname = ''; |
208 | } else { |
209 | $firstname = $this->charset_decode($row['givenname'][0]); |
210 | } |
211 | |
212 | if(empty($row['sn'][0])) { |
213 | $surname = ''; |
214 | } else { |
215 | $surname = $this->charset_decode($row['sn'][0]); |
216 | } |
217 | |
218 | /* Add one row to result for each e-mail address */ |
219 | if(isset($row['mail']['count'])) { |
220 | for($j = 0 ; $j < $row['mail']['count'] ; $j++) { |
221 | array_push($ret, array('nickname' => $nickname, |
222 | 'name' => $fullname, |
223 | 'firstname' => $firstname, |
224 | 'lastname' => $surname, |
225 | 'email' => $row['mail'][$j], |
226 | 'label' => $label, |
227 | 'phone' => $phone, |
228 | 'backend' => $this->bnum, |
229 | 'source' => &$this->sname)); |
230 | |
231 | // Limit number of hits |
232 | $returned_rows++; |
233 | if(($returned_rows >= $this->maxrows) && |
234 | ($this->maxrows > 0) ) { |
235 | ldap_free_result($sret); |
236 | return $ret; |
237 | } |
238 | |
239 | } // for($j ...) |
240 | |
241 | } // isset($row['mail']['count']) |
242 | |
243 | } |
244 | |
245 | ldap_free_result($sret); |
246 | return $ret; |
247 | } /* end search() */ |
248 | |
249 | |
250 | /* If you run a tiny LDAP server and you want the "List All" button |
251 | * to show EVERYONE, then uncomment this tiny block of code: |
252 | * |
253 | * function list_addr() { |
254 | * return $this->search('*'); |
255 | * } |
256 | * |
257 | * Careful with this -- it could get quite large for big sites. */ |
258 | } |
cccfa9c2 |
259 | ?> |