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
34 /** Needs the DB functions */
35 require_once('DB.php');
38 * Undocumented class - stores the addressbook in a sql database
39 * @package squirrelmail
41 class abook_database
extends addressbook_backend
{
43 var $bname = 'database';
50 var $writeable = true;
52 /* ========================== Private ======================= */
55 function abook_database($param) {
56 $this->sname
= _("Personal address book");
58 if (is_array($param)) {
59 if (empty($param['dsn']) ||
60 empty($param['table']) ||
61 empty($param['owner'])) {
62 return $this->set_error('Invalid parameters');
65 $this->dsn
= $param['dsn'];
66 $this->table
= $param['table'];
67 $this->owner
= $param['owner'];
69 if (!empty($param['name'])) {
70 $this->sname
= $param['name'];
73 if (isset($param['writeable'])) {
74 $this->writeable
= $param['writeable'];
77 if (isset($param['listing'])) {
78 $this->listing
= $param['listing'];
84 return $this->set_error('Invalid argument to constructor');
89 /* Open the database. New connection if $new is true */
90 function open($new = false) {
93 /* Return true is file is open and $new is unset */
94 if ($this->dbh
&& !$new) {
98 /* Close old file, if any */
103 $dbh = DB
::connect($this->dsn
, true);
105 if (DB
::isError($dbh)) {
106 return $this->set_error(sprintf(_("Database error: %s"),
107 DB
::errorMessage($dbh)));
114 /* Close the file and forget the filehandle */
116 $this->dbh
->disconnect();
120 /* ========================== Public ======================== */
122 /* Search the file */
123 function &search($expr) {
129 /* To be replaced by advanded search expression parsing */
130 if (is_array($expr)) {
134 /* Make regexp from glob'ed expression */
135 $expr = str_replace('?', '_', $expr);
136 $expr = str_replace('*', '%', $expr);
137 $expr = $this->dbh
->quoteString($expr);
140 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
141 "(firstname LIKE '%s' OR lastname LIKE '%s')",
142 $this->table
, $this->owner
, $expr, $expr);
143 $res = $this->dbh
->query($query);
145 if (DB
::isError($res)) {
146 return $this->set_error(sprintf(_("Database error: %s"),
147 DB
::errorMessage($res)));
150 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
151 array_push($ret, array('nickname' => $row['nickname'],
152 'name' => "$row[firstname] $row[lastname]",
153 'firstname' => $row['firstname'],
154 'lastname' => $row['lastname'],
155 'email' => $row['email'],
156 'label' => $row['label'],
157 'backend' => $this->bnum
,
158 'source' => &$this->sname
));
164 function &lookup($alias) {
169 $alias = strtolower($alias);
171 if (!$this->open()) {
175 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND nickname='%s'",
176 $this->table
, $this->owner
, $alias);
178 $res = $this->dbh
->query($query);
180 if (DB
::isError($res)) {
181 return $this->set_error(sprintf(_("Database error: %s"),
182 DB
::errorMessage($res)));
185 if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
186 return array('nickname' => $row['nickname'],
187 'name' => "$row[firstname] $row[lastname]",
188 'firstname' => $row['firstname'],
189 'lastname' => $row['lastname'],
190 'email' => $row['email'],
191 'label' => $row['label'],
192 'backend' => $this->bnum
,
193 'source' => &$this->sname
);
198 /* List all addresses */
199 function &list_addr() {
201 if (!$this->open()) {
205 if(isset($this->listing
) && !$this->listing
) {
210 $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
211 $this->table
, $this->owner
);
213 $res = $this->dbh
->query($query);
215 if (DB
::isError($res)) {
216 return $this->set_error(sprintf(_("Database error: %s"),
217 DB
::errorMessage($res)));
220 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
221 array_push($ret, array('nickname' => $row['nickname'],
222 'name' => "$row[firstname] $row[lastname]",
223 'firstname' => $row['firstname'],
224 'lastname' => $row['lastname'],
225 'email' => $row['email'],
226 'label' => $row['label'],
227 'backend' => $this->bnum
,
228 'source' => &$this->sname
));
234 function add($userdata) {
235 if (!$this->writeable
) {
236 return $this->set_error(_("Addressbook is read-only"));
239 if (!$this->open()) {
243 /* See if user exist already */
244 $ret = $this->lookup($userdata['nickname']);
246 return $this->set_error(sprintf(_("User '%s' already exist"),
251 $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
252 "lastname, email, label) VALUES('%s','%s','%s'," .
254 $this->table
, $this->owner
,
255 $this->dbh
->quoteString($userdata['nickname']),
256 $this->dbh
->quoteString($userdata['firstname']),
257 $this->dbh
->quoteString($userdata['lastname']),
258 $this->dbh
->quoteString($userdata['email']),
259 $this->dbh
->quoteString($userdata['label']) );
262 $r = $this->dbh
->simpleQuery($query);
268 return $this->set_error(sprintf(_("Database error: %s"),
269 DB
::errorMessage($r)));
273 function remove($alias) {
274 if (!$this->writeable
) {
275 return $this->set_error(_("Addressbook is read-only"));
278 if (!$this->open()) {
283 $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
284 $this->table
, $this->owner
);
287 while (list($undef, $nickname) = each($alias)) {
288 $query .= sprintf("%s nickname='%s' ", $sepstr,
289 $this->dbh
->quoteString($nickname));
295 $r = $this->dbh
->simpleQuery($query);
301 return $this->set_error(sprintf(_("Database error: %s"),
302 DB
::errorMessage($r)));
306 function modify($alias, $userdata) {
307 if (!$this->writeable
) {
308 return $this->set_error(_("Addressbook is read-only"));
311 if (!$this->open()) {
315 /* See if user exist */
316 $ret = $this->lookup($alias);
318 return $this->set_error(sprintf(_("User '%s' does not exist"),
323 $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
324 "lastname='%s', email='%s', label='%s' ".
325 "WHERE owner='%s' AND nickname='%s'",
327 $this->dbh
->quoteString($userdata['nickname']),
328 $this->dbh
->quoteString($userdata['firstname']),
329 $this->dbh
->quoteString($userdata['lastname']),
330 $this->dbh
->quoteString($userdata['email']),
331 $this->dbh
->quoteString($userdata['label']),
333 $this->dbh
->quoteString($alias) );
336 $r = $this->dbh
->simpleQuery($query);
342 return $this->set_error(sprintf(_("Database error: %s"),
343 DB
::errorMessage($r)));
345 } /* End of class abook_database */