replaced "foo" with 'foo' while doing other
[squirrelmail.git] / functions / addressbook.php
1 <?php
2
3 /**
4 ** addressbook.php
5 **
6 ** Functions and classes for the addressbook system.
7 **
8 ** $Id$
9 **/
10
11 $addressbook_php = true;
12
13 // Include backends here.
14 include('../functions/abook_local_file.php');
15 include('../functions/abook_ldap_server.php');
16
17 // Un-comment if you're using database backend
18 // include('../functions/abook_database.php');
19
20
21 // Create and initialize an addressbook object.
22 // Returns the created object
23 function addressbook_init($showerr = true, $onlylocal = false) {
24 global $data_dir, $username, $ldap_server;
25
26 // Create a new addressbook object
27 $abook = new AddressBook;
28
29 // Always add a local backend
30
31 // Use *either* file-based *or* database addressbook. Remove
32 // and insert comments to enable the one you want.
33
34 // ------ BEGIN Initialize file-based personal addressbook ------
35 $filename = sprintf('%s%s.abook', $data_dir, $username);
36 $r = $abook->add_backend('local_file', Array('filename' => $filename,
37 'create' => true));
38
39 if(!$r && $showerr) {
40 printf(_("Error opening file %s"), $filename);
41 exit;
42 }
43 // ------ END Initialize file-based personal addressbook ------
44
45 // ------ BEGIN Initialize database-based personal addressbook ------
46 // $r = $abook->add_backend('database', Array('dsn' => 'mysql://dbuser@host/dbname',
47 // 'owner' => $username,
48 // 'table' => 'address'));
49 // if(!$r && $showerr) {
50 // printf(_("Error initializing addressbook: %s"), $filename);
51 // exit;
52 // }
53 // ------ END Initialize database-based personal addressbook ------
54
55 if($onlylocal)
56 return $abook;
57
58 // Load configured LDAP servers (if PHP has LDAP support)
59 if(isset($ldap_server) && is_array($ldap_server) &&
60 function_exists('ldap_connect')) {
61 reset($ldap_server);
62 while(list($undef,$param) = each($ldap_server)) {
63 if(is_array($param)) {
64 $r = $abook->add_backend('ldap_server', $param);
65 if(!$r && $showerr) {
66 printf('&nbsp;' . _("Error initializing LDAP server %s:") .
67 "<BR>\n", $param['host']);
68 print('&nbsp;' . $abook->error);
69 exit;
70 }
71 }
72 }
73 }
74
75 // Return the initialized object
76 return $abook;
77 }
78
79
80 // Had to move this function outside of the Addressbook Class
81 // PHP 4.0.4 Seemed to be having problems with inline functions.
82 function addressbook_cmp($a,$b) {
83 if($a['backend'] > $b['backend'])
84 return 1;
85 else if($a['backend'] < $b['backend'])
86 return -1;
87 return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
88 }
89
90
91 /**
92 ** This is the main address book class that connect all the
93 ** backends and provide services to the functions above.
94 **
95 **/
96
97 class AddressBook {
98 var $backends = array();
99 var $numbackends = 0;
100 var $error = '';
101 var $localbackend = 0;
102 var $localbackendname = '';
103
104 // Constructor function.
105 function AddressBook() {
106 $localbackendname = _("Personal address book");
107 }
108
109 // Return an array of backends of a given type,
110 // or all backends if no type is specified.
111 function get_backend_list($type = '') {
112 $ret = array();
113 for($i = 1 ; $i <= $this->numbackends ; $i++) {
114 if(empty($type) || $type == $this->backends[$i]->btype) {
115 $ret[] = &$this->backends[$i];
116 }
117 }
118 return $ret;
119 }
120
121
122 // ========================== Public ========================
123
124 // Add a new backend. $backend is the name of a backend
125 // (without the abook_ prefix), and $param is an optional
126 // mixed variable that is passed to the backend constructor.
127 // See each of the backend classes for valid parameters.
128 function add_backend($backend, $param = '') {
129 $backend_name = 'abook_' . $backend;
130 eval('$newback = new ' . $backend_name . '($param);');
131 if(!empty($newback->error)) {
132 $this->error = $newback->error;
133 return false;
134 }
135
136 $this->numbackends++;
137
138 $newback->bnum = $this->numbackends;
139 $this->backends[$this->numbackends] = $newback;
140
141 // Store ID of first local backend added
142 if($this->localbackend == 0 && $newback->btype == 'local') {
143 $this->localbackend = $this->numbackends;
144 $this->localbackendname = $newback->sname;
145 }
146
147 return $this->numbackends;
148 }
149
150
151 // Return a list of addresses matching expression in
152 // all backends of a given type.
153 function search($expression, $bnum = -1) {
154 $ret = array();
155 $this->error = '';
156
157 // Search all backends
158 if($bnum == -1) {
159 $sel = $this->get_backend_list('');
160 $failed = 0;
161 for($i = 0 ; $i < sizeof($sel) ; $i++) {
162 $backend = &$sel[$i];
163 $backend->error = '';
164 $res = $backend->search($expression);
165 if(is_array($res)) {
166 $ret = array_merge($ret, $res);
167 } else {
168 $this->error .= "<br>\n" . $backend->error;
169 $failed++;
170 }
171 }
172
173 // Only fail if all backends failed
174 if($failed >= sizeof($sel))
175 return false;
176
177 }
178
179 // Search only one backend
180 else {
181 $ret = $this->backends[$bnum]->search($expression);
182 if(!is_array($ret)) {
183 $this->error .= "<br>\n" . $this->backends[$bnum]->error;
184 return false;
185 }
186 }
187
188 return $ret;
189 }
190
191
192 // Return a sorted search
193 function s_search($expression, $bnum = -1) {
194
195 $ret = $this->search($expression, $bnum);
196 if(!is_array($ret))
197 return $ret;
198 usort($ret, 'addressbook_cmp');
199 return $ret;
200 }
201
202
203 // Lookup an address by alias. Only possible in
204 // local backends.
205 function lookup($alias, $bnum = -1) {
206 $ret = array();
207
208 if($bnum > -1) {
209 $res = $this->backends[$bnum]->lookup($alias);
210 if(is_array($res)) {
211 return $res;
212 } else {
213 $this->error = $backend->error;
214 return false;
215 }
216 }
217
218 $sel = $this->get_backend_list('local');
219 for($i = 0 ; $i < sizeof($sel) ; $i++) {
220 $backend = &$sel[$i];
221 $backend->error = '';
222 $res = $backend->lookup($alias);
223 if(is_array($res)) {
224 if(!empty($res))
225 return $res;
226 } else {
227 $this->error = $backend->error;
228 return false;
229 }
230 }
231
232 return $ret;
233 }
234
235
236 // Return all addresses
237 function list_addr($bnum = -1) {
238 $ret = array();
239
240 if($bnum == -1)
241 $sel = $this->get_backend_list('local');
242 else
243 $sel = array(0 => &$this->backends[$bnum]);
244
245 for($i = 0 ; $i < sizeof($sel) ; $i++) {
246 $backend = &$sel[$i];
247 $backend->error = '';
248 $res = $backend->list_addr();
249 if(is_array($res)) {
250 $ret = array_merge($ret, $res);
251 } else {
252 $this->error = $backend->error;
253 return false;
254 }
255 }
256
257 return $ret;
258 }
259
260
261 // Create a new address from $userdata, in backend $bnum.
262 // Return the backend number that the/ address was added
263 // to, or false if it failed.
264 function add($userdata, $bnum) {
265
266 // Validate data
267 if(!is_array($userdata)) {
268 $this->error = _("Invalid input data");
269 return false;
270 }
271 if(empty($userdata['firstname']) &&
272 empty($userdata['lastname'])) {
273 $this->error = _("Name is missing");
274 return false;
275 }
276 if(empty($userdata['email'])) {
277 $this->error = _("E-mail address is missing");
278 return false;
279 }
280 if(empty($userdata['nickname'])) {
281 $userdata['nickname'] = $userdata['email'];
282 }
283
284 if(eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
285 $this->error = _("Nickname contain illegal characters");
286 return false;
287 }
288
289 // Check that specified backend accept new entries
290 if(!$this->backends[$bnum]->writeable) {
291 $this->error = _("Addressbook is read-only");
292 return false;
293 }
294
295 // Add address to backend
296 $res = $this->backends[$bnum]->add($userdata);
297 if($res) {
298 return $bnum;
299 } else {
300 $this->error = $this->backends[$bnum]->error;
301 return false;
302 }
303
304 return false; // Not reached
305 } // end of add()
306
307
308 // Remove the user identified by $alias from backend $bnum
309 // If $alias is an array, all users in the array are removed.
310 function remove($alias, $bnum) {
311
312 // Check input
313 if(empty($alias))
314 return true;
315
316 // Convert string to single element array
317 if(!is_array($alias))
318 $alias = array(0 => $alias);
319
320 // Check that specified backend is writable
321 if(!$this->backends[$bnum]->writeable) {
322 $this->error = _("Addressbook is read-only");
323 return false;
324 }
325
326 // Remove user from backend
327 $res = $this->backends[$bnum]->remove($alias);
328 if($res) {
329 return $bnum;
330 } else {
331 $this->error = $this->backends[$bnum]->error;
332 return false;
333 }
334
335 return false; // Not reached
336 } // end of remove()
337
338
339 // Remove the user identified by $alias from backend $bnum
340 // If $alias is an array, all users in the array are removed.
341 function modify($alias, $userdata, $bnum) {
342
343 // Check input
344 if(empty($alias) || !is_string($alias))
345 return true;
346
347 // Validate data
348 if(!is_array($userdata)) {
349 $this->error = _("Invalid input data");
350 return false;
351 }
352 if(empty($userdata['firstname']) &&
353 empty($userdata['lastname'])) {
354 $this->error = _("Name is missing");
355 return false;
356 }
357 if(empty($userdata['email'])) {
358 $this->error = _("E-mail address is missing");
359 return false;
360 }
361
362 if(eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
363 $this->error = _("Nickname contain illegal characters");
364 return false;
365 }
366
367 if(empty($userdata['nickname'])) {
368 $userdata['nickname'] = $userdata['email'];
369 }
370
371 // Check that specified backend is writable
372 if(!$this->backends[$bnum]->writeable) {
373 $this->error = _("Addressbook is read-only");;
374 return false;
375 }
376
377 // Modify user in backend
378 $res = $this->backends[$bnum]->modify($alias, $userdata);
379 if($res) {
380 return $bnum;
381 } else {
382 $this->error = $this->backends[$bnum]->error;
383 return false;
384 }
385
386 return false; // Not reached
387 } // end of modify()
388
389
390 } // End of class Addressbook
391
392 /**
393 ** Generic backend that all other backends extend
394 **/
395 class addressbook_backend {
396
397 // Variables that all backends must provide.
398 var $btype = 'dummy';
399 var $bname = 'dummy';
400 var $sname = 'Dummy backend';
401
402 // Variables common for all backends, but that
403 // should not be changed by the backends.
404 var $bnum = -1;
405 var $error = '';
406 var $writeable = false;
407
408 function set_error($string) {
409 $this->error = '[' . $this->sname . '] ' . $string;
410 return false;
411 }
412
413
414 // ========================== Public ========================
415
416 function search($expression) {
417 $this->set_error('search not implemented');
418 return false;
419 }
420
421 function lookup($alias) {
422 $this->set_error('lookup not implemented');
423 return false;
424 }
425
426 function list_addr() {
427 $this->set_error('list_addr not implemented');
428 return false;
429 }
430
431 function add($userdata) {
432 $this->set_error('add not implemented');
433 return false;
434 }
435
436 function remove($alias) {
437 $this->set_error('delete not implemented');
438 return false;
439 }
440
441 function modify($alias, $newuserdata) {
442 $this->set_error('modify not implemented');
443 return false;
444 }
445
446 }
447
448 ?>