6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Backend for personal addressbook stored in a database,
10 * accessed using the DB-classes in PEAR.
12 * IMPORTANT: The PEAR modules must be in the include path
13 * for this class to work.
15 * An array with the following elements must be passed to
16 * the class constructor (elements marked ? are optional):
18 * dsn => database DNS (see PEAR for syntax)
19 * table => table to store addresses in (must exist)
20 * owner => current user (owner of address data)
21 * ? writeable => set writeable flag (true/false)
23 * The table used should have the following columns:
24 * owner, nickname, firstname, lastname, email, label
25 * The pair (owner,nickname) should be unique (primary key).
27 * NOTE. This class should not be used directly. Use the
28 * "AddressBook" class instead.
31 * @package squirrelmail
32 * @subpackage addressbook
35 /** Needs the DB functions */
36 if (!include_once('DB.php')) {
37 // same error also in db_prefs.php
38 require_once(SM_PATH
. 'functions/display_messages.php');
39 $error = _("Could not include PEAR database functions required for the database backend.") . "<br />\n";
40 $error .= _("Please contact your system administrator and report this error.");
41 error_box($error, $color);
45 * Undocumented class - stores the addressbook in a sql database
46 * @package squirrelmail
48 class abook_database
extends addressbook_backend
{
50 var $bname = 'database';
57 var $writeable = true;
59 /* ========================== Private ======================= */
62 function abook_database($param) {
63 $this->sname
= _("Personal address book");
65 if (is_array($param)) {
66 if (empty($param['dsn']) ||
67 empty($param['table']) ||
68 empty($param['owner'])) {
69 return $this->set_error('Invalid parameters');
72 $this->dsn
= $param['dsn'];
73 $this->table
= $param['table'];
74 $this->owner
= $param['owner'];
76 if (!empty($param['name'])) {
77 $this->sname
= $param['name'];
80 if (isset($param['writeable'])) {
81 $this->writeable
= $param['writeable'];
84 if (isset($param['listing'])) {
85 $this->listing
= $param['listing'];
91 return $this->set_error('Invalid argument to constructor');
96 /* Open the database. New connection if $new is true */
97 function open($new = false) {
100 /* Return true is file is open and $new is unset */
101 if ($this->dbh
&& !$new) {
105 /* Close old file, if any */
110 $dbh = DB
::connect($this->dsn
, true);
112 if (DB
::isError($dbh)) {
113 return $this->set_error(sprintf(_("Database error: %s"),
114 DB
::errorMessage($dbh)));
121 /* Close the file and forget the filehandle */
123 $this->dbh
->disconnect();
127 /* ========================== Public ======================== */
129 /* Search the file */
130 function &search($expr) {
136 /* To be replaced by advanded search expression parsing */
137 if (is_array($expr)) {
141 /* Make regexp from glob'ed expression */
142 $expr = str_replace('?', '_', $expr);
143 $expr = str_replace('*', '%', $expr);
144 $expr = $this->dbh
->quoteString($expr);
147 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
148 "(firstname LIKE '%s' OR lastname LIKE '%s')",
149 $this->table
, $this->owner
, $expr, $expr);
150 $res = $this->dbh
->query($query);
152 if (DB
::isError($res)) {
153 return $this->set_error(sprintf(_("Database error: %s"),
154 DB
::errorMessage($res)));
157 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
158 array_push($ret, array('nickname' => $row['nickname'],
159 'name' => "$row[firstname] $row[lastname]",
160 'firstname' => $row['firstname'],
161 'lastname' => $row['lastname'],
162 'email' => $row['email'],
163 'label' => $row['label'],
164 'backend' => $this->bnum
,
165 'source' => &$this->sname
));
171 function &lookup($alias) {
176 $alias = strtolower($alias);
178 if (!$this->open()) {
182 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND LOWER(nickname)='%s'",
183 $this->table
, $this->owner
, $this->dbh
->quoteString($alias));
185 $res = $this->dbh
->query($query);
187 if (DB
::isError($res)) {
188 return $this->set_error(sprintf(_("Database error: %s"),
189 DB
::errorMessage($res)));
192 if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
193 return array('nickname' => $row['nickname'],
194 'name' => "$row[firstname] $row[lastname]",
195 'firstname' => $row['firstname'],
196 'lastname' => $row['lastname'],
197 'email' => $row['email'],
198 'label' => $row['label'],
199 'backend' => $this->bnum
,
200 'source' => &$this->sname
);
205 /* List all addresses */
206 function &list_addr() {
208 if (!$this->open()) {
212 if(isset($this->listing
) && !$this->listing
) {
217 $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
218 $this->table
, $this->owner
);
220 $res = $this->dbh
->query($query);
222 if (DB
::isError($res)) {
223 return $this->set_error(sprintf(_("Database error: %s"),
224 DB
::errorMessage($res)));
227 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
228 array_push($ret, array('nickname' => $row['nickname'],
229 'name' => "$row[firstname] $row[lastname]",
230 'firstname' => $row['firstname'],
231 'lastname' => $row['lastname'],
232 'email' => $row['email'],
233 'label' => $row['label'],
234 'backend' => $this->bnum
,
235 'source' => &$this->sname
));
241 function add($userdata) {
242 if (!$this->writeable
) {
243 return $this->set_error(_("Addressbook is read-only"));
246 if (!$this->open()) {
250 /* See if user exist already */
251 $ret = $this->lookup($userdata['nickname']);
253 return $this->set_error(sprintf(_("User '%s' already exist"),
258 $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
259 "lastname, email, label) VALUES('%s','%s','%s'," .
261 $this->table
, $this->owner
,
262 $this->dbh
->quoteString($userdata['nickname']),
263 $this->dbh
->quoteString($userdata['firstname']),
264 $this->dbh
->quoteString($userdata['lastname']),
265 $this->dbh
->quoteString($userdata['email']),
266 $this->dbh
->quoteString($userdata['label']) );
269 $r = $this->dbh
->simpleQuery($query);
275 return $this->set_error(sprintf(_("Database error: %s"),
276 DB
::errorMessage($r)));
280 function remove($alias) {
281 if (!$this->writeable
) {
282 return $this->set_error(_("Addressbook is read-only"));
285 if (!$this->open()) {
290 $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
291 $this->table
, $this->owner
);
294 while (list($undef, $nickname) = each($alias)) {
295 $query .= sprintf("%s nickname='%s' ", $sepstr,
296 $this->dbh
->quoteString($nickname));
302 $r = $this->dbh
->simpleQuery($query);
308 return $this->set_error(sprintf(_("Database error: %s"),
309 DB
::errorMessage($r)));
313 function modify($alias, $userdata) {
314 if (!$this->writeable
) {
315 return $this->set_error(_("Addressbook is read-only"));
318 if (!$this->open()) {
322 /* See if user exist */
323 $ret = $this->lookup($alias);
325 return $this->set_error(sprintf(_("User '%s' does not exist"),
330 $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
331 "lastname='%s', email='%s', label='%s' ".
332 "WHERE owner='%s' AND nickname='%s'",
334 $this->dbh
->quoteString($userdata['nickname']),
335 $this->dbh
->quoteString($userdata['firstname']),
336 $this->dbh
->quoteString($userdata['lastname']),
337 $this->dbh
->quoteString($userdata['email']),
338 $this->dbh
->quoteString($userdata['label']),
340 $this->dbh
->quoteString($alias) );
343 $r = $this->dbh
->simpleQuery($query);
349 return $this->set_error(sprintf(_("Database error: %s"),
350 DB
::errorMessage($r)));
352 } /* End of class abook_database */