b5b2e0017aa7267649f02bcc7d36ee88ffdb2772
6 * Copyright (c) 1999-2001 The Squirrelmail Development 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.
33 require_once('DB.php');
35 class abook_database
extends addressbook_backend
{
37 var $bname = 'database';
44 var $writeable = true;
46 // ========================== Private =======================
49 function abook_database($param) {
50 $this->sname
= _("Personal address book");
52 if(is_array($param)) {
53 if(empty($param['dsn']) ||
54 empty($param['table']) ||
55 empty($param['owner']))
56 return $this->set_error('Invalid parameters');
58 $this->dsn
= $param['dsn'];
59 $this->table
= $param['table'];
60 $this->owner
= $param['owner'];
62 if(!empty($param['name']))
63 $this->sname
= $param['name'];
65 if(isset($param['writeable']))
66 $this->writeable
= $param['writeable'];
70 return $this->set_error('Invalid argument to constructor');
75 // Open the database. New connection if $new is true
76 function open($new = false) {
79 // Return true is file is open and $new is unset
80 if($this->dbh
&& !$new)
83 // Close old file, if any
84 if($this->dbh
) $this->close();
86 $dbh = DB
::connect($this->dsn
, true);
88 if(DB
::isError($dbh) || DB
::isWarning($dbh))
89 return $this->set_error(sprintf(_("Database error: %s"),
90 DB
::errorMessage($dbh)));
96 // Close the file and forget the filehandle
98 $this->dbh
->disconnect();
102 // ========================== Public ========================
105 function &search($expr) {
110 // To be replaced by advanded search expression parsing
111 if(is_array($expr)) return;
113 // Make regexp from glob'ed expression
114 $expr = str_replace('?', '_', $expr);
115 $expr = str_replace('*', '%', $expr);
116 $expr = $this->dbh
->quoteString($expr);
119 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
120 "(firstname LIKE '%s' OR lastname LIKE '%s')",
121 $this->table
, $this->owner
, $expr, $expr);
122 $res = $this->dbh
->query($query);
124 if(DB
::isError($res))
125 return $this->set_error(sprintf(_("Database error: %s"),
126 DB
::errorMessage($res)));
128 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
129 array_push($ret, array('nickname' => $row['nickname'],
130 'name' => "$row[firstname] $row[lastname]",
131 'firstname' => $row['firstname'],
132 'lastname' => $row['lastname'],
133 'email' => $row['email'],
134 'label' => $row['label'],
135 'backend' => $this->bnum
,
136 'source' => &$this->sname
));
142 function &lookup($alias) {
146 $alias = strtolower($alias);
151 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND nickname='%s'",
152 $this->table
, $this->owner
, $alias);
154 $res = $this->dbh
->query($query);
156 if(DB
::isError($res))
157 return $this->set_error(sprintf(_("Database error: %s"),
158 DB
::errorMessage($res)));
160 if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
161 return 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 // List all addresses
175 function &list_addr() {
180 $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
181 $this->table
, $this->owner
);
183 $res = $this->dbh
->query($query);
185 if(DB
::isError($res))
186 return $this->set_error(sprintf(_("Database error: %s"),
187 DB
::errorMessage($res)));
189 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
190 array_push($ret, array('nickname' => $row['nickname'],
191 'name' => "$row[firstname] $row[lastname]",
192 'firstname' => $row['firstname'],
193 'lastname' => $row['lastname'],
194 'email' => $row['email'],
195 'label' => $row['label'],
196 'backend' => $this->bnum
,
197 'source' => &$this->sname
));
203 function add($userdata) {
204 if(!$this->writeable
)
205 return $this->set_error(_("Addressbook is read-only"));
210 // See if user exist already
211 $ret = $this->lookup($userdata['nickname']);
213 return $this->set_error(sprintf(_("User '%s' already exist"),
217 $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
218 "lastname, email, label) VALUES('%s','%s','%s'," .
220 $this->table
, $this->owner
,
221 $this->dbh
->quoteString($userdata['nickname']),
222 $this->dbh
->quoteString($userdata['firstname']),
223 $this->dbh
->quoteString($userdata['lastname']),
224 $this->dbh
->quoteString($userdata['email']),
225 $this->dbh
->quoteString($userdata['label']) );
228 $r = $this->dbh
->simpleQuery($query);
229 if($r == DB_OK
) return true;
232 return $this->set_error(sprintf(_("Database error: %s"),
233 DB
::errorMessage($r)));
237 function remove($alias) {
238 if(!$this->writeable
)
239 return $this->set_error(_("Addressbook is read-only"));
245 $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
246 $this->table
, $this->owner
);
249 while(list($undef, $nickname) = each($alias)) {
250 $query .= sprintf("%s nickname='%s' ", $sepstr,
251 $this->dbh
->quoteString($nickname));
257 $r = $this->dbh
->simpleQuery($query);
258 if($r == DB_OK
) return true;
261 return $this->set_error(sprintf(_("Database error: %s"),
262 DB
::errorMessage($r)));
266 function modify($alias, $userdata) {
267 if(!$this->writeable
)
268 return $this->set_error(_("Addressbook is read-only"));
274 $ret = $this->lookup($alias);
276 return $this->set_error(sprintf(_("User '%s' does not exist"),
280 $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
281 "lastname='%s', email='%s', label='%s' ".
282 "WHERE owner='%s' AND nickname='%s'",
284 $this->dbh
->quoteString($userdata['nickname']),
285 $this->dbh
->quoteString($userdata['firstname']),
286 $this->dbh
->quoteString($userdata['lastname']),
287 $this->dbh
->quoteString($userdata['email']),
288 $this->dbh
->quoteString($userdata['label']),
290 $this->dbh
->quoteString($alias) );
293 $r = $this->dbh
->simpleQuery($query);
294 if($r == DB_OK
) return true;
297 return $this->set_error(sprintf(_("Database error: %s"),
298 DB
::errorMessage($r)));
300 } // End of class abook_database