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 .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
41 '<tt>DB.php</tt>') . "<br />\n";
42 $error .= _("Please contact your system administrator and report this error.");
43 error_box($error, $color);
48 * Undocumented class - stores the addressbook in a sql database
49 * @package squirrelmail
51 class abook_database
extends addressbook_backend
{
53 var $bname = 'database';
60 var $writeable = true;
62 /* ========================== Private ======================= */
65 function abook_database($param) {
66 $this->sname
= _("Personal address book");
68 if (is_array($param)) {
69 if (empty($param['dsn']) ||
70 empty($param['table']) ||
71 empty($param['owner'])) {
72 return $this->set_error('Invalid parameters');
75 $this->dsn
= $param['dsn'];
76 $this->table
= $param['table'];
77 $this->owner
= $param['owner'];
79 if (!empty($param['name'])) {
80 $this->sname
= $param['name'];
83 if (isset($param['writeable'])) {
84 $this->writeable
= $param['writeable'];
87 if (isset($param['listing'])) {
88 $this->listing
= $param['listing'];
94 return $this->set_error('Invalid argument to constructor');
99 /* Open the database. New connection if $new is true */
100 function open($new = false) {
103 /* Return true is file is open and $new is unset */
104 if ($this->dbh
&& !$new) {
108 /* Close old file, if any */
113 $dbh = DB
::connect($this->dsn
, true);
115 if (DB
::isError($dbh)) {
116 return $this->set_error(sprintf(_("Database error: %s"),
117 DB
::errorMessage($dbh)));
124 /* Close the file and forget the filehandle */
126 $this->dbh
->disconnect();
130 /* ========================== Public ======================== */
132 /* Search the file */
133 function &search($expr) {
139 /* To be replaced by advanded search expression parsing */
140 if (is_array($expr)) {
144 /* Make regexp from glob'ed expression */
145 $expr = str_replace('?', '_', $expr);
146 $expr = str_replace('*', '%', $expr);
147 $expr = $this->dbh
->quoteString($expr);
150 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
151 "(firstname LIKE '%s' OR lastname LIKE '%s')",
152 $this->table
, $this->owner
, $expr, $expr);
153 $res = $this->dbh
->query($query);
155 if (DB
::isError($res)) {
156 return $this->set_error(sprintf(_("Database error: %s"),
157 DB
::errorMessage($res)));
160 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
161 array_push($ret, array('nickname' => $row['nickname'],
162 'name' => "$row[firstname] $row[lastname]",
163 'firstname' => $row['firstname'],
164 'lastname' => $row['lastname'],
165 'email' => $row['email'],
166 'label' => $row['label'],
167 'backend' => $this->bnum
,
168 'source' => &$this->sname
));
174 function &lookup($alias) {
179 $alias = strtolower($alias);
181 if (!$this->open()) {
185 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND LOWER(nickname)='%s'",
186 $this->table
, $this->owner
, $this->dbh
->quoteString($alias));
188 $res = $this->dbh
->query($query);
190 if (DB
::isError($res)) {
191 return $this->set_error(sprintf(_("Database error: %s"),
192 DB
::errorMessage($res)));
195 if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
196 return array('nickname' => $row['nickname'],
197 'name' => "$row[firstname] $row[lastname]",
198 'firstname' => $row['firstname'],
199 'lastname' => $row['lastname'],
200 'email' => $row['email'],
201 'label' => $row['label'],
202 'backend' => $this->bnum
,
203 'source' => &$this->sname
);
208 /* List all addresses */
209 function &list_addr() {
211 if (!$this->open()) {
215 if(isset($this->listing
) && !$this->listing
) {
220 $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
221 $this->table
, $this->owner
);
223 $res = $this->dbh
->query($query);
225 if (DB
::isError($res)) {
226 return $this->set_error(sprintf(_("Database error: %s"),
227 DB
::errorMessage($res)));
230 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
231 array_push($ret, array('nickname' => $row['nickname'],
232 'name' => "$row[firstname] $row[lastname]",
233 'firstname' => $row['firstname'],
234 'lastname' => $row['lastname'],
235 'email' => $row['email'],
236 'label' => $row['label'],
237 'backend' => $this->bnum
,
238 'source' => &$this->sname
));
244 function add($userdata) {
245 if (!$this->writeable
) {
246 return $this->set_error(_("Addressbook is read-only"));
249 if (!$this->open()) {
253 /* See if user exist already */
254 $ret = $this->lookup($userdata['nickname']);
256 return $this->set_error(sprintf(_("User '%s' already exist"),
261 $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
262 "lastname, email, label) VALUES('%s','%s','%s'," .
264 $this->table
, $this->owner
,
265 $this->dbh
->quoteString($userdata['nickname']),
266 $this->dbh
->quoteString($userdata['firstname']),
267 $this->dbh
->quoteString($userdata['lastname']),
268 $this->dbh
->quoteString($userdata['email']),
269 $this->dbh
->quoteString($userdata['label']) );
272 $r = $this->dbh
->simpleQuery($query);
278 return $this->set_error(sprintf(_("Database error: %s"),
279 DB
::errorMessage($r)));
283 function remove($alias) {
284 if (!$this->writeable
) {
285 return $this->set_error(_("Addressbook is read-only"));
288 if (!$this->open()) {
293 $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
294 $this->table
, $this->owner
);
297 while (list($undef, $nickname) = each($alias)) {
298 $query .= sprintf("%s nickname='%s' ", $sepstr,
299 $this->dbh
->quoteString($nickname));
305 $r = $this->dbh
->simpleQuery($query);
311 return $this->set_error(sprintf(_("Database error: %s"),
312 DB
::errorMessage($r)));
316 function modify($alias, $userdata) {
317 if (!$this->writeable
) {
318 return $this->set_error(_("Addressbook is read-only"));
321 if (!$this->open()) {
325 /* See if user exist */
326 $ret = $this->lookup($alias);
328 return $this->set_error(sprintf(_("User '%s' does not exist"),
333 $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
334 "lastname='%s', email='%s', label='%s' ".
335 "WHERE owner='%s' AND nickname='%s'",
337 $this->dbh
->quoteString($userdata['nickname']),
338 $this->dbh
->quoteString($userdata['firstname']),
339 $this->dbh
->quoteString($userdata['lastname']),
340 $this->dbh
->quoteString($userdata['email']),
341 $this->dbh
->quoteString($userdata['label']),
343 $this->dbh
->quoteString($alias) );
346 $r = $this->dbh
->simpleQuery($query);
352 return $this->set_error(sprintf(_("Database error: %s"),
353 DB
::errorMessage($r)));
355 } /* End of class abook_database */