82ddba9dcd5ba708b9a2f829748d54fc430c43f4
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 .= _("Is PEAR installed, and is the include path set correctly to find <tt>DB.php</tt>?") . "<br />\n";
41 $error .= _("Please contact your system administrator and report this error.");
42 error_box($error, $color);
47 * Undocumented class - stores the addressbook in a sql database
48 * @package squirrelmail
50 class abook_database
extends addressbook_backend
{
52 var $bname = 'database';
59 var $writeable = true;
61 /* ========================== Private ======================= */
64 function abook_database($param) {
65 $this->sname
= _("Personal address book");
67 if (is_array($param)) {
68 if (empty($param['dsn']) ||
69 empty($param['table']) ||
70 empty($param['owner'])) {
71 return $this->set_error('Invalid parameters');
74 $this->dsn
= $param['dsn'];
75 $this->table
= $param['table'];
76 $this->owner
= $param['owner'];
78 if (!empty($param['name'])) {
79 $this->sname
= $param['name'];
82 if (isset($param['writeable'])) {
83 $this->writeable
= $param['writeable'];
86 if (isset($param['listing'])) {
87 $this->listing
= $param['listing'];
93 return $this->set_error('Invalid argument to constructor');
98 /* Open the database. New connection if $new is true */
99 function open($new = false) {
102 /* Return true is file is open and $new is unset */
103 if ($this->dbh
&& !$new) {
107 /* Close old file, if any */
112 $dbh = DB
::connect($this->dsn
, true);
114 if (DB
::isError($dbh)) {
115 return $this->set_error(sprintf(_("Database error: %s"),
116 DB
::errorMessage($dbh)));
123 /* Close the file and forget the filehandle */
125 $this->dbh
->disconnect();
129 /* ========================== Public ======================== */
131 /* Search the file */
132 function &search($expr) {
138 /* To be replaced by advanded search expression parsing */
139 if (is_array($expr)) {
143 /* Make regexp from glob'ed expression */
144 $expr = str_replace('?', '_', $expr);
145 $expr = str_replace('*', '%', $expr);
146 $expr = $this->dbh
->quoteString($expr);
149 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
150 "(firstname LIKE '%s' OR lastname LIKE '%s')",
151 $this->table
, $this->owner
, $expr, $expr);
152 $res = $this->dbh
->query($query);
154 if (DB
::isError($res)) {
155 return $this->set_error(sprintf(_("Database error: %s"),
156 DB
::errorMessage($res)));
159 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
160 array_push($ret, array('nickname' => $row['nickname'],
161 'name' => "$row[firstname] $row[lastname]",
162 'firstname' => $row['firstname'],
163 'lastname' => $row['lastname'],
164 'email' => $row['email'],
165 'label' => $row['label'],
166 'backend' => $this->bnum
,
167 'source' => &$this->sname
));
173 function &lookup($alias) {
178 $alias = strtolower($alias);
180 if (!$this->open()) {
184 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND LOWER(nickname)='%s'",
185 $this->table
, $this->owner
, $this->dbh
->quoteString($alias));
187 $res = $this->dbh
->query($query);
189 if (DB
::isError($res)) {
190 return $this->set_error(sprintf(_("Database error: %s"),
191 DB
::errorMessage($res)));
194 if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
195 return array('nickname' => $row['nickname'],
196 'name' => "$row[firstname] $row[lastname]",
197 'firstname' => $row['firstname'],
198 'lastname' => $row['lastname'],
199 'email' => $row['email'],
200 'label' => $row['label'],
201 'backend' => $this->bnum
,
202 'source' => &$this->sname
);
207 /* List all addresses */
208 function &list_addr() {
210 if (!$this->open()) {
214 if(isset($this->listing
) && !$this->listing
) {
219 $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
220 $this->table
, $this->owner
);
222 $res = $this->dbh
->query($query);
224 if (DB
::isError($res)) {
225 return $this->set_error(sprintf(_("Database error: %s"),
226 DB
::errorMessage($res)));
229 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
230 array_push($ret, array('nickname' => $row['nickname'],
231 'name' => "$row[firstname] $row[lastname]",
232 'firstname' => $row['firstname'],
233 'lastname' => $row['lastname'],
234 'email' => $row['email'],
235 'label' => $row['label'],
236 'backend' => $this->bnum
,
237 'source' => &$this->sname
));
243 function add($userdata) {
244 if (!$this->writeable
) {
245 return $this->set_error(_("Addressbook is read-only"));
248 if (!$this->open()) {
252 /* See if user exist already */
253 $ret = $this->lookup($userdata['nickname']);
255 return $this->set_error(sprintf(_("User '%s' already exist"),
260 $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
261 "lastname, email, label) VALUES('%s','%s','%s'," .
263 $this->table
, $this->owner
,
264 $this->dbh
->quoteString($userdata['nickname']),
265 $this->dbh
->quoteString($userdata['firstname']),
266 $this->dbh
->quoteString($userdata['lastname']),
267 $this->dbh
->quoteString($userdata['email']),
268 $this->dbh
->quoteString($userdata['label']) );
271 $r = $this->dbh
->simpleQuery($query);
277 return $this->set_error(sprintf(_("Database error: %s"),
278 DB
::errorMessage($r)));
282 function remove($alias) {
283 if (!$this->writeable
) {
284 return $this->set_error(_("Addressbook is read-only"));
287 if (!$this->open()) {
292 $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
293 $this->table
, $this->owner
);
296 while (list($undef, $nickname) = each($alias)) {
297 $query .= sprintf("%s nickname='%s' ", $sepstr,
298 $this->dbh
->quoteString($nickname));
304 $r = $this->dbh
->simpleQuery($query);
310 return $this->set_error(sprintf(_("Database error: %s"),
311 DB
::errorMessage($r)));
315 function modify($alias, $userdata) {
316 if (!$this->writeable
) {
317 return $this->set_error(_("Addressbook is read-only"));
320 if (!$this->open()) {
324 /* See if user exist */
325 $ret = $this->lookup($alias);
327 return $this->set_error(sprintf(_("User '%s' does not exist"),
332 $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
333 "lastname='%s', email='%s', label='%s' ".
334 "WHERE owner='%s' AND nickname='%s'",
336 $this->dbh
->quoteString($userdata['nickname']),
337 $this->dbh
->quoteString($userdata['firstname']),
338 $this->dbh
->quoteString($userdata['lastname']),
339 $this->dbh
->quoteString($userdata['email']),
340 $this->dbh
->quoteString($userdata['label']),
342 $this->dbh
->quoteString($alias) );
345 $r = $this->dbh
->simpleQuery($query);
351 return $this->set_error(sprintf(_("Database error: %s"),
352 DB
::errorMessage($r)));
354 } /* End of class abook_database */