5 * Copyright (c) 1999-2005 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * @package squirrelmail
10 * @subpackage addressbook
13 /** Needs the DB functions */
14 if (!include_once('DB.php')) {
15 // same error also in db_prefs.php
16 require_once(SM_PATH
. 'functions/display_messages.php');
17 $error = _("Could not include PEAR database functions required for the database backend.") . "<br />\n";
18 $error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
19 '<tt>DB.php</tt>') . "<br />\n";
20 $error .= _("Please contact your system administrator and report this error.");
21 error_box($error, $color);
26 * Address book in a database backend
28 * Backend for personal/shared address book stored in a database,
29 * accessed using the DB-classes in PEAR.
31 * IMPORTANT: The PEAR modules must be in the include path
32 * for this class to work.
34 * An array with the following elements must be passed to
35 * the class constructor (elements marked ? are optional):
37 * dsn => database DNS (see PEAR for syntax)
38 * table => table to store addresses in (must exist)
39 * owner => current user (owner of address data)
40 * ? name => name of address book
41 * ? writeable => set writeable flag (true/false)
42 * ? listing => enable/disable listing
44 * The table used should have the following columns:
45 * owner, nickname, firstname, lastname, email, label
46 * The pair (owner,nickname) should be unique (primary key).
48 * NOTE. This class should not be used directly. Use the
49 * "AddressBook" class instead.
50 * @package squirrelmail
51 * @subpackage addressbook
53 class abook_database
extends addressbook_backend
{
63 var $bname = 'database';
66 * Data Source Name (connection description)
71 * Table that stores addresses
78 * Limits list of database entries visible to end user
88 * Enable/disable writing into address book
91 var $writeable = true;
93 * Enable/disable address book listing
98 /* ========================== Private ======================= */
102 * @param array $param address book backend options
104 function abook_database($param) {
105 $this->sname
= _("Personal address book");
107 if (is_array($param)) {
108 if (empty($param['dsn']) ||
109 empty($param['table']) ||
110 empty($param['owner'])) {
111 return $this->set_error('Invalid parameters');
114 $this->dsn
= $param['dsn'];
115 $this->table
= $param['table'];
116 $this->owner
= $param['owner'];
118 if (!empty($param['name'])) {
119 $this->sname
= $param['name'];
122 if (isset($param['writeable'])) {
123 $this->writeable
= $param['writeable'];
126 if (isset($param['listing'])) {
127 $this->listing
= $param['listing'];
133 return $this->set_error('Invalid argument to constructor');
140 * @param bool $new new connection if it is true
143 function open($new = false) {
146 /* Return true is file is open and $new is unset */
147 if ($this->dbh
&& !$new) {
151 /* Close old file, if any */
156 $dbh = DB
::connect($this->dsn
, true);
158 if (DB
::isError($dbh)) {
159 return $this->set_error(sprintf(_("Database error: %s"),
160 DB
::errorMessage($dbh)));
168 * Close the file and forget the filehandle
171 $this->dbh
->disconnect();
175 /* ========================== Public ======================== */
178 * Search the database
179 * @param string $expr search expression
180 * @return array search results
182 function &search($expr) {
188 /* To be replaced by advanded search expression parsing */
189 if (is_array($expr)) {
193 /* Make regexp from glob'ed expression */
194 $expr = str_replace('?', '_', $expr);
195 $expr = str_replace('*', '%', $expr);
196 $expr = $this->dbh
->quoteString($expr);
199 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
200 "(firstname LIKE '%s' OR lastname LIKE '%s')",
201 $this->table
, $this->owner
, $expr, $expr);
202 $res = $this->dbh
->query($query);
204 if (DB
::isError($res)) {
205 return $this->set_error(sprintf(_("Database error: %s"),
206 DB
::errorMessage($res)));
209 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
210 array_push($ret, array('nickname' => $row['nickname'],
211 'name' => "$row[firstname] $row[lastname]",
212 'firstname' => $row['firstname'],
213 'lastname' => $row['lastname'],
214 'email' => $row['email'],
215 'label' => $row['label'],
216 'backend' => $this->bnum
,
217 'source' => &$this->sname
));
224 * @param string $alias alias
225 * @return array search results
227 function &lookup($alias) {
232 $alias = strtolower($alias);
234 if (!$this->open()) {
238 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND LOWER(nickname)='%s'",
239 $this->table
, $this->owner
, $this->dbh
->quoteString($alias));
241 $res = $this->dbh
->query($query);
243 if (DB
::isError($res)) {
244 return $this->set_error(sprintf(_("Database error: %s"),
245 DB
::errorMessage($res)));
248 if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
249 return array('nickname' => $row['nickname'],
250 'name' => "$row[firstname] $row[lastname]",
251 'firstname' => $row['firstname'],
252 'lastname' => $row['lastname'],
253 'email' => $row['email'],
254 'label' => $row['label'],
255 'backend' => $this->bnum
,
256 'source' => &$this->sname
);
263 * @return array search results
265 function &list_addr() {
267 if (!$this->open()) {
271 if(isset($this->listing
) && !$this->listing
) {
276 $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
277 $this->table
, $this->owner
);
279 $res = $this->dbh
->query($query);
281 if (DB
::isError($res)) {
282 return $this->set_error(sprintf(_("Database error: %s"),
283 DB
::errorMessage($res)));
286 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
287 array_push($ret, array('nickname' => $row['nickname'],
288 'name' => "$row[firstname] $row[lastname]",
289 'firstname' => $row['firstname'],
290 'lastname' => $row['lastname'],
291 'email' => $row['email'],
292 'label' => $row['label'],
293 'backend' => $this->bnum
,
294 'source' => &$this->sname
));
301 * @param array $userdata added data
304 function add($userdata) {
305 if (!$this->writeable
) {
306 return $this->set_error(_("Addressbook is read-only"));
309 if (!$this->open()) {
313 /* See if user exist already */
314 $ret = $this->lookup($userdata['nickname']);
316 return $this->set_error(sprintf(_("User '%s' already exist"),
321 $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
322 "lastname, email, label) VALUES('%s','%s','%s'," .
324 $this->table
, $this->owner
,
325 $this->dbh
->quoteString($userdata['nickname']),
326 $this->dbh
->quoteString($userdata['firstname']),
327 $this->dbh
->quoteString($userdata['lastname']),
328 $this->dbh
->quoteString($userdata['email']),
329 $this->dbh
->quoteString($userdata['label']) );
332 $r = $this->dbh
->simpleQuery($query);
338 return $this->set_error(sprintf(_("Database error: %s"),
339 DB
::errorMessage($r)));
344 * @param string $alias alias that has to be deleted
347 function remove($alias) {
348 if (!$this->writeable
) {
349 return $this->set_error(_("Addressbook is read-only"));
352 if (!$this->open()) {
357 $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
358 $this->table
, $this->owner
);
361 while (list($undef, $nickname) = each($alias)) {
362 $query .= sprintf("%s nickname='%s' ", $sepstr,
363 $this->dbh
->quoteString($nickname));
369 $r = $this->dbh
->simpleQuery($query);
375 return $this->set_error(sprintf(_("Database error: %s"),
376 DB
::errorMessage($r)));
381 * @param string $alias modified alias
382 * @param array $userdata new data
385 function modify($alias, $userdata) {
386 if (!$this->writeable
) {
387 return $this->set_error(_("Addressbook is read-only"));
390 if (!$this->open()) {
394 /* See if user exist */
395 $ret = $this->lookup($alias);
397 return $this->set_error(sprintf(_("User '%s' does not exist"),
402 $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
403 "lastname='%s', email='%s', label='%s' ".
404 "WHERE owner='%s' AND nickname='%s'",
406 $this->dbh
->quoteString($userdata['nickname']),
407 $this->dbh
->quoteString($userdata['firstname']),
408 $this->dbh
->quoteString($userdata['lastname']),
409 $this->dbh
->quoteString($userdata['email']),
410 $this->dbh
->quoteString($userdata['label']),
412 $this->dbh
->quoteString($alias) );
415 $r = $this->dbh
->simpleQuery($query);
421 return $this->set_error(sprintf(_("Database error: %s"),
422 DB
::errorMessage($r)));
424 } /* End of class abook_database */