source: subversion/trunk/roundcubemail/program/include/rcube_addressbook.php @ 4552

Last change on this file since 4552 was 4552, checked in by thomasb, 2 years ago

Allow group identifiers to be changed upon renaming (used in LDAP)

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