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