commit | author | age
|
cc97ea
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
|
5 |
| program/include/rcube_addressbook.php | |
|
6 |
| | |
e019f2
|
7 |
| This file is part of the Roundcube Webmail client | |
0501b6
|
8 |
| Copyright (C) 2006-2011, The Roundcube Dev Team | |
cc97ea
|
9 |
| Licensed under the GNU GPL | |
T |
10 |
| | |
|
11 |
| PURPOSE: | |
|
12 |
| Interface to the local address book database | |
|
13 |
| | |
|
14 |
+-----------------------------------------------------------------------+ |
|
15 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
|
16 |
+-----------------------------------------------------------------------+ |
|
17 |
|
1d786c
|
18 |
$Id$ |
cc97ea
|
19 |
|
T |
20 |
*/ |
|
21 |
|
|
22 |
|
|
23 |
/** |
|
24 |
* Abstract skeleton of an address book/repository |
|
25 |
* |
|
26 |
* @package Addressbook |
|
27 |
*/ |
|
28 |
abstract class rcube_addressbook |
|
29 |
{ |
0501b6
|
30 |
/** constants for error reporting **/ |
T |
31 |
const ERROR_READ_ONLY = 1; |
|
32 |
const ERROR_NO_CONNECTION = 2; |
|
33 |
const ERROR_INCOMPLETE = 3; |
|
34 |
const ERROR_SAVING = 4; |
3e2637
|
35 |
const ERROR_SEARCH = 5; |
cc90ed
|
36 |
|
0501b6
|
37 |
/** public properties (mandatory) */ |
T |
38 |
public $primary_key; |
|
39 |
public $groups = false; |
|
40 |
public $readonly = true; |
7f5a84
|
41 |
public $undelete = false; |
0501b6
|
42 |
public $ready = false; |
06670e
|
43 |
public $group_id = null; |
0501b6
|
44 |
public $list_page = 1; |
T |
45 |
public $page_size = 10; |
|
46 |
public $coltypes = array('name' => array('limit'=>1), 'firstname' => array('limit'=>1), 'surname' => array('limit'=>1), 'email' => array('limit'=>1)); |
cc90ed
|
47 |
|
0501b6
|
48 |
protected $error; |
cc90ed
|
49 |
|
A |
50 |
/** |
|
51 |
* Returns addressbook name (e.g. for addressbooks listing) |
|
52 |
*/ |
|
53 |
abstract function get_name(); |
cc97ea
|
54 |
|
T |
55 |
/** |
|
56 |
* Save a search string for future listings |
|
57 |
* |
|
58 |
* @param mixed Search params to use in listing method, obtained by get_search_set() |
|
59 |
*/ |
|
60 |
abstract function set_search_set($filter); |
|
61 |
|
|
62 |
/** |
|
63 |
* Getter for saved search properties |
|
64 |
* |
|
65 |
* @return mixed Search properties used by this class |
|
66 |
*/ |
|
67 |
abstract function get_search_set(); |
|
68 |
|
|
69 |
/** |
|
70 |
* Reset saved results and search parameters |
|
71 |
*/ |
|
72 |
abstract function reset(); |
|
73 |
|
|
74 |
/** |
0501b6
|
75 |
* Refresh saved search set after data has changed |
T |
76 |
* |
|
77 |
* @return mixed New search set |
|
78 |
*/ |
|
79 |
function refresh_search() |
|
80 |
{ |
|
81 |
return $this->get_search_set(); |
|
82 |
} |
|
83 |
|
|
84 |
/** |
cc97ea
|
85 |
* List the current set of contact records |
T |
86 |
* |
|
87 |
* @param array List of cols to show |
|
88 |
* @param int Only return this number of records, use negative values for tail |
|
89 |
* @return array Indexed list of contact records, each a hash array |
|
90 |
*/ |
|
91 |
abstract function list_records($cols=null, $subset=0); |
a61bbb
|
92 |
|
T |
93 |
/** |
cc97ea
|
94 |
* Search records |
T |
95 |
* |
|
96 |
* @param array List of fields to search in |
|
97 |
* @param string Search value |
|
98 |
* @param boolean True if results are requested, False if count only |
0501b6
|
99 |
* @param boolean True to skip the count query (select only) |
T |
100 |
* @param array List of fields that cannot be empty |
|
101 |
* @return object rcube_result_set List of contact records and 'count' value |
cc97ea
|
102 |
*/ |
0501b6
|
103 |
abstract function search($fields, $value, $strict=false, $select=true, $nocount=false, $required=array()); |
cc97ea
|
104 |
|
T |
105 |
/** |
|
106 |
* Count number of available contacts in database |
|
107 |
* |
5c461b
|
108 |
* @return rcube_result_set Result set with values for 'count' and 'first' |
cc97ea
|
109 |
*/ |
T |
110 |
abstract function count(); |
|
111 |
|
|
112 |
/** |
|
113 |
* Return the last result set |
|
114 |
* |
5c461b
|
115 |
* @return rcube_result_set Current result set or NULL if nothing selected yet |
cc97ea
|
116 |
*/ |
T |
117 |
abstract function get_result(); |
|
118 |
|
|
119 |
/** |
|
120 |
* Get a specific contact record |
|
121 |
* |
|
122 |
* @param mixed record identifier(s) |
|
123 |
* @param boolean True to return record as associative array, otherwise a result set is returned |
b393e5
|
124 |
* |
cc97ea
|
125 |
* @return mixed Result object with all record fields or False if not found |
T |
126 |
*/ |
|
127 |
abstract function get_record($id, $assoc=false); |
0501b6
|
128 |
|
T |
129 |
/** |
|
130 |
* Returns the last error occured (e.g. when updating/inserting failed) |
|
131 |
* |
|
132 |
* @return array Hash array with the following fields: type, message |
|
133 |
*/ |
|
134 |
function get_error() |
|
135 |
{ |
|
136 |
return $this->error; |
|
137 |
} |
cc90ed
|
138 |
|
0501b6
|
139 |
/** |
T |
140 |
* Setter for errors for internal use |
|
141 |
* |
|
142 |
* @param int Error type (one of this class' error constants) |
|
143 |
* @param string Error message (name of a text label) |
|
144 |
*/ |
|
145 |
protected function set_error($type, $message) |
|
146 |
{ |
|
147 |
$this->error = array('type' => $type, 'message' => $message); |
|
148 |
} |
cc97ea
|
149 |
|
T |
150 |
/** |
|
151 |
* Close connection to source |
|
152 |
* Called on script shutdown |
|
153 |
*/ |
|
154 |
function close() { } |
|
155 |
|
|
156 |
/** |
|
157 |
* Set internal list page |
|
158 |
* |
|
159 |
* @param number Page number to list |
|
160 |
* @access public |
|
161 |
*/ |
|
162 |
function set_page($page) |
|
163 |
{ |
2eb794
|
164 |
$this->list_page = (int)$page; |
cc97ea
|
165 |
} |
T |
166 |
|
|
167 |
/** |
|
168 |
* Set internal page size |
|
169 |
* |
|
170 |
* @param number Number of messages to display on one page |
|
171 |
* @access public |
|
172 |
*/ |
|
173 |
function set_pagesize($size) |
|
174 |
{ |
2eb794
|
175 |
$this->page_size = (int)$size; |
cc97ea
|
176 |
} |
a61bbb
|
177 |
|
07b95d
|
178 |
|
T |
179 |
/** |
|
180 |
* Check the given data before saving. |
e84818
|
181 |
* If input isn't valid, the message to display can be fetched using get_error() |
07b95d
|
182 |
* |
T |
183 |
* @param array Assoziative array with data to save |
|
184 |
* @return boolean True if input is valid, False if not. |
|
185 |
*/ |
|
186 |
public function validate($save_data) |
|
187 |
{ |
|
188 |
// check validity of email addresses |
|
189 |
foreach ($this->get_col_values('email', $save_data, true) as $email) { |
|
190 |
if (strlen($email)) { |
|
191 |
if (!check_email(rcube_idn_to_ascii($email))) { |
|
192 |
$this->set_error('warning', rcube_label(array('name' => 'emailformaterror', 'vars' => array('email' => $email)))); |
|
193 |
return false; |
|
194 |
} |
|
195 |
} |
|
196 |
} |
|
197 |
|
|
198 |
return true; |
|
199 |
} |
|
200 |
|
|
201 |
|
a61bbb
|
202 |
/** |
cc97ea
|
203 |
* Create a new contact record |
T |
204 |
* |
|
205 |
* @param array Assoziative array with save data |
0501b6
|
206 |
* Keys: Field name with optional section in the form FIELD:SECTION |
T |
207 |
* Values: Field value. Can be either a string or an array of strings for multiple values |
cc97ea
|
208 |
* @param boolean True to check for duplicates first |
5c461b
|
209 |
* @return mixed The created record ID on success, False on error |
cc97ea
|
210 |
*/ |
T |
211 |
function insert($save_data, $check=false) |
|
212 |
{ |
2eb794
|
213 |
/* empty for read-only address books */ |
cc97ea
|
214 |
} |
T |
215 |
|
|
216 |
/** |
0501b6
|
217 |
* Create new contact records for every item in the record set |
T |
218 |
* |
|
219 |
* @param object rcube_result_set Recordset to insert |
|
220 |
* @param boolean True to check for duplicates first |
|
221 |
* @return array List of created record IDs |
|
222 |
*/ |
|
223 |
function insertMultiple($recset, $check=false) |
|
224 |
{ |
|
225 |
$ids = array(); |
|
226 |
if (is_object($recset) && is_a($recset, rcube_result_set)) { |
|
227 |
while ($row = $recset->next()) { |
|
228 |
if ($insert = $this->insert($row, $check)) |
|
229 |
$ids[] = $insert; |
|
230 |
} |
|
231 |
} |
|
232 |
return $ids; |
|
233 |
} |
|
234 |
|
|
235 |
/** |
cc97ea
|
236 |
* Update a specific contact record |
T |
237 |
* |
|
238 |
* @param mixed Record identifier |
|
239 |
* @param array Assoziative array with save data |
0501b6
|
240 |
* Keys: Field name with optional section in the form FIELD:SECTION |
T |
241 |
* Values: Field value. Can be either a string or an array of strings for multiple values |
5c461b
|
242 |
* @return boolean True on success, False on error |
cc97ea
|
243 |
*/ |
T |
244 |
function update($id, $save_cols) |
|
245 |
{ |
2eb794
|
246 |
/* empty for read-only address books */ |
cc97ea
|
247 |
} |
T |
248 |
|
|
249 |
/** |
|
250 |
* Mark one or more contact records as deleted |
|
251 |
* |
|
252 |
* @param array Record identifiers |
63fda8
|
253 |
* @param bool Remove records irreversible (see self::undelete) |
cc97ea
|
254 |
*/ |
63fda8
|
255 |
function delete($ids, $force=true) |
cc97ea
|
256 |
{ |
2eb794
|
257 |
/* empty for read-only address books */ |
cc97ea
|
258 |
} |
T |
259 |
|
|
260 |
/** |
7f5a84
|
261 |
* Unmark delete flag on contact record(s) |
A |
262 |
* |
|
263 |
* @param array Record identifiers |
|
264 |
*/ |
|
265 |
function undelete($ids) |
|
266 |
{ |
|
267 |
/* empty for read-only address books */ |
|
268 |
} |
|
269 |
|
|
270 |
/** |
|
271 |
* Mark all records in database as deleted |
cc97ea
|
272 |
*/ |
T |
273 |
function delete_all() |
|
274 |
{ |
2eb794
|
275 |
/* empty for read-only address books */ |
cc97ea
|
276 |
} |
T |
277 |
|
04adaa
|
278 |
/** |
b393e5
|
279 |
* Setter for the current group |
A |
280 |
* (empty, has to be re-implemented by extending class) |
|
281 |
*/ |
|
282 |
function set_group($gid) { } |
|
283 |
|
|
284 |
/** |
|
285 |
* List all active contact groups of this source |
|
286 |
* |
0501b6
|
287 |
* @param string Optional search string to match group name |
b393e5
|
288 |
* @return array Indexed list of contact groups, each a hash array |
A |
289 |
*/ |
0501b6
|
290 |
function list_groups($search = null) |
b393e5
|
291 |
{ |
A |
292 |
/* empty for address books don't supporting groups */ |
|
293 |
return array(); |
|
294 |
} |
|
295 |
|
|
296 |
/** |
04adaa
|
297 |
* Create a contact group with the given name |
T |
298 |
* |
|
299 |
* @param string The group name |
5c461b
|
300 |
* @return mixed False on error, array with record props in success |
04adaa
|
301 |
*/ |
T |
302 |
function create_group($name) |
|
303 |
{ |
2eb794
|
304 |
/* empty for address books don't supporting groups */ |
A |
305 |
return false; |
04adaa
|
306 |
} |
b393e5
|
307 |
|
04adaa
|
308 |
/** |
T |
309 |
* Delete the given group and all linked group members |
|
310 |
* |
|
311 |
* @param string Group identifier |
|
312 |
* @return boolean True on success, false if no data was changed |
|
313 |
*/ |
|
314 |
function delete_group($gid) |
|
315 |
{ |
2eb794
|
316 |
/* empty for address books don't supporting groups */ |
A |
317 |
return false; |
04adaa
|
318 |
} |
b393e5
|
319 |
|
04adaa
|
320 |
/** |
T |
321 |
* Rename a specific contact group |
|
322 |
* |
|
323 |
* @param string Group identifier |
|
324 |
* @param string New name to set for this group |
360bd3
|
325 |
* @param string New group identifier (if changed, otherwise don't set) |
04adaa
|
326 |
* @return boolean New name on success, false if no data was changed |
T |
327 |
*/ |
360bd3
|
328 |
function rename_group($gid, $newname, &$newid) |
04adaa
|
329 |
{ |
2eb794
|
330 |
/* empty for address books don't supporting groups */ |
A |
331 |
return false; |
04adaa
|
332 |
} |
b393e5
|
333 |
|
04adaa
|
334 |
/** |
T |
335 |
* Add the given contact records the a certain group |
|
336 |
* |
|
337 |
* @param string Group identifier |
|
338 |
* @param array List of contact identifiers to be added |
b393e5
|
339 |
* @return int Number of contacts added |
04adaa
|
340 |
*/ |
T |
341 |
function add_to_group($group_id, $ids) |
|
342 |
{ |
2eb794
|
343 |
/* empty for address books don't supporting groups */ |
A |
344 |
return 0; |
04adaa
|
345 |
} |
b393e5
|
346 |
|
04adaa
|
347 |
/** |
T |
348 |
* Remove the given contact records from a certain group |
|
349 |
* |
|
350 |
* @param string Group identifier |
|
351 |
* @param array List of contact identifiers to be removed |
|
352 |
* @return int Number of deleted group members |
|
353 |
*/ |
|
354 |
function remove_from_group($group_id, $ids) |
|
355 |
{ |
2eb794
|
356 |
/* empty for address books don't supporting groups */ |
A |
357 |
return 0; |
04adaa
|
358 |
} |
b393e5
|
359 |
|
A |
360 |
/** |
|
361 |
* Get group assignments of a specific contact record |
|
362 |
* |
|
363 |
* @param mixed Record identifier |
|
364 |
* |
|
365 |
* @return array List of assigned groups as ID=>Name pairs |
|
366 |
* @since 0.5-beta |
|
367 |
*/ |
|
368 |
function get_record_groups($id) |
|
369 |
{ |
|
370 |
/* empty for address books don't supporting groups */ |
|
371 |
return array(); |
|
372 |
} |
0501b6
|
373 |
|
T |
374 |
|
|
375 |
/** |
|
376 |
* Utility function to return all values of a certain data column |
|
377 |
* either as flat list or grouped by subtype |
|
378 |
* |
|
379 |
* @param string Col name |
|
380 |
* @param array Record data array as used for saving |
|
381 |
* @param boolean True to return one array with all values, False for hash array with values grouped by type |
|
382 |
* @return array List of column values |
|
383 |
*/ |
|
384 |
function get_col_values($col, $data, $flat = false) |
|
385 |
{ |
|
386 |
$out = array(); |
|
387 |
foreach ($data as $c => $values) { |
|
388 |
if (strpos($c, $col) === 0) { |
|
389 |
if ($flat) { |
|
390 |
$out = array_merge($out, (array)$values); |
|
391 |
} |
|
392 |
else { |
|
393 |
list($f, $type) = explode(':', $c); |
|
394 |
$out[$type] = array_merge((array)$out[$type], (array)$values); |
|
395 |
} |
|
396 |
} |
|
397 |
} |
cc90ed
|
398 |
|
0501b6
|
399 |
return $out; |
T |
400 |
} |
3e2637
|
401 |
|
T |
402 |
|
|
403 |
/** |
|
404 |
* Normalize the given string for fulltext search. |
|
405 |
* Currently only optimized for Latin-1 characters; to be extended |
|
406 |
* |
|
407 |
* @param string Input string (UTF-8) |
|
408 |
* @return string Normalized string |
|
409 |
*/ |
|
410 |
protected static function normalize_string($str) |
|
411 |
{ |
12dac4
|
412 |
// split by words |
T |
413 |
$arr = explode(" ", preg_replace( |
|
414 |
array('/[\s;\+\-\/]+/i', '/(\d)[-.\s]+(\d)/', '/\s\w{1,3}\s/'), |
3e2637
|
415 |
array(' ', '\\1\\2', ' '), |
12dac4
|
416 |
$str)); |
cc90ed
|
417 |
|
12dac4
|
418 |
foreach ($arr as $i => $part) { |
T |
419 |
if (utf8_encode(utf8_decode($part)) == $part) { // is latin-1 ? |
e5e1eb
|
420 |
$arr[$i] = utf8_encode(strtr(strtolower(strtr(utf8_decode($part), |
12dac4
|
421 |
'ÇçäâàåéêëèïîìÅÉöôòüûùÿøØáíóúñÑÁÂÀãÃÊËÈÍÎÏÓÔõÕÚÛÙýÝ', |
T |
422 |
'ccaaaaeeeeiiiaeooouuuyooaiounnaaaaaeeeiiioooouuuyy')), |
e5e1eb
|
423 |
array('ß' => 'ss', 'ae' => 'a', 'oe' => 'o', 'ue' => 'u'))); |
12dac4
|
424 |
} |
T |
425 |
else |
34d728
|
426 |
$arr[$i] = mb_strtolower($part); |
12dac4
|
427 |
} |
cc90ed
|
428 |
|
12dac4
|
429 |
return join(" ", $arr); |
3e2637
|
430 |
} |
e84818
|
431 |
|
T |
432 |
|
|
433 |
/** |
|
434 |
* Compose a valid display name from the given structured contact data |
|
435 |
* |
|
436 |
* @param array Hash array with contact data as key-value pairs |
71e8cc
|
437 |
* @param bool The name will be used on the list |
A |
438 |
* |
e84818
|
439 |
* @return string Display name |
T |
440 |
*/ |
71e8cc
|
441 |
public static function compose_display_name($contact, $list_mode = false) |
e84818
|
442 |
{ |
T |
443 |
$contact = rcmail::get_instance()->plugins->exec_hook('contact_displayname', $contact); |
|
444 |
$fn = $contact['name']; |
|
445 |
|
|
446 |
if (!$fn) |
|
447 |
$fn = join(' ', array_filter(array($contact['prefix'], $contact['firstname'], $contact['middlename'], $contact['surname'], $contact['suffix']))); |
|
448 |
|
|
449 |
// use email address part for name |
|
450 |
$email = is_array($contact['email']) ? $contact['email'][0] : $contact['email']; |
71e8cc
|
451 |
|
e84818
|
452 |
if ($email && (empty($fn) || $fn == $email)) { |
71e8cc
|
453 |
// Use full email address on contacts list |
A |
454 |
if ($list_mode) |
|
455 |
return $email; |
|
456 |
|
e84818
|
457 |
list($emailname) = explode('@', $email); |
T |
458 |
if (preg_match('/(.*)[\.\-\_](.*)/', $emailname, $match)) |
|
459 |
$fn = trim(ucfirst($match[1]).' '.ucfirst($match[2])); |
|
460 |
else |
|
461 |
$fn = ucfirst($emailname); |
|
462 |
} |
|
463 |
|
|
464 |
return $fn; |
|
465 |
} |
|
466 |
|
cc97ea
|
467 |
} |
b393e5
|
468 |
|