6 * @copyright © 1999-2006 The SquirrelMail Project Team
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 * @package squirrelmail
10 * @subpackage addressbook
14 * Needs the DB functions
15 * Don't display errors here. Error will be set in class constructor function.
17 @include_once
('DB.php');
20 * Address book in a database backend
22 * Backend for personal/shared address book stored in a database,
23 * accessed using the DB-classes in PEAR.
25 * IMPORTANT: The PEAR modules must be in the include path
26 * for this class to work.
28 * An array with the following elements must be passed to
29 * the class constructor (elements marked ? are optional):
31 * dsn => database DNS (see PEAR for syntax)
32 * table => table to store addresses in (must exist)
33 * owner => current user (owner of address data)
34 * ? name => name of address book
35 * ? writeable => set writeable flag (true/false)
36 * ? listing => enable/disable listing
38 * The table used should have the following columns:
39 * owner, nickname, firstname, lastname, email, label
40 * The pair (owner,nickname) should be unique (primary key).
42 * NOTE. This class should not be used directly. Use the
43 * "AddressBook" class instead.
44 * @package squirrelmail
45 * @subpackage addressbook
47 class abook_database
extends addressbook_backend
{
57 var $bname = 'database';
60 * Data Source Name (connection description)
65 * Table that stores addresses
72 * Limits list of database entries visible to end user
82 * Enable/disable writing into address book
85 var $writeable = true;
87 * Enable/disable address book listing
92 /* ========================== Private ======================= */
96 * @param array $param address book backend options
98 function abook_database($param) {
99 $this->sname
= _("Personal address book");
101 /* test if Pear DB class is available and freak out if it is not */
102 if (! class_exists('DB')) {
103 // same error also in db_prefs.php
104 $error = _("Could not include PEAR database functions required for the database backend.") . "<br />\n";
105 $error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
106 '<tt>DB.php</tt>') . "<br />\n";
107 $error .= _("Please contact your system administrator and report this error.");
108 return $this->set_error($error);
111 if (is_array($param)) {
112 if (empty($param['dsn']) ||
113 empty($param['table']) ||
114 empty($param['owner'])) {
115 return $this->set_error('Invalid parameters');
118 $this->dsn
= $param['dsn'];
119 $this->table
= $param['table'];
120 $this->owner
= $param['owner'];
122 if (!empty($param['name'])) {
123 $this->sname
= $param['name'];
126 if (isset($param['writeable'])) {
127 $this->writeable
= $param['writeable'];
130 if (isset($param['listing'])) {
131 $this->listing
= $param['listing'];
137 return $this->set_error('Invalid argument to constructor');
144 * @param bool $new new connection if it is true
147 function open($new = false) {
150 /* Return true is file is open and $new is unset */
151 if ($this->dbh
&& !$new) {
155 /* Close old file, if any */
160 $dbh = DB
::connect($this->dsn
, true);
162 if (DB
::isError($dbh)) {
163 return $this->set_error(sprintf(_("Database error: %s"),
164 DB
::errorMessage($dbh)));
172 * Close the file and forget the filehandle
175 $this->dbh
->disconnect();
179 /* ========================== Public ======================== */
182 * Search the database
183 * @param string $expr search expression
184 * @return array search results
186 function search($expr) {
192 /* To be replaced by advanded search expression parsing */
193 if (is_array($expr)) {
197 // don't allow wide search when listing is disabled.
198 if ($expr=='*' && ! $this->listing
)
201 /* Make regexp from glob'ed expression */
202 $expr = str_replace('?', '_', $expr);
203 $expr = str_replace('*', '%', $expr);
204 $expr = $this->dbh
->quoteString($expr);
207 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
208 "(firstname LIKE '%s' OR lastname LIKE '%s')",
209 $this->table
, $this->owner
, $expr, $expr);
210 $res = $this->dbh
->query($query);
212 if (DB
::isError($res)) {
213 return $this->set_error(sprintf(_("Database error: %s"),
214 DB
::errorMessage($res)));
217 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
218 array_push($ret, array('nickname' => $row['nickname'],
219 'name' => "$row[firstname] $row[lastname]",
220 'firstname' => $row['firstname'],
221 'lastname' => $row['lastname'],
222 'email' => $row['email'],
223 'label' => $row['label'],
224 'backend' => $this->bnum
,
225 'source' => &$this->sname
));
232 * @param string $alias alias
233 * @return array search results
235 function lookup($alias) {
240 $alias = strtolower($alias);
242 if (!$this->open()) {
246 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND LOWER(nickname)='%s'",
247 $this->table
, $this->owner
, $this->dbh
->quoteString($alias));
249 $res = $this->dbh
->query($query);
251 if (DB
::isError($res)) {
252 return $this->set_error(sprintf(_("Database error: %s"),
253 DB
::errorMessage($res)));
256 if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
257 return array('nickname' => $row['nickname'],
258 'name' => "$row[firstname] $row[lastname]",
259 'firstname' => $row['firstname'],
260 'lastname' => $row['lastname'],
261 'email' => $row['email'],
262 'label' => $row['label'],
263 'backend' => $this->bnum
,
264 'source' => &$this->sname
);
271 * @return array search results
273 function list_addr() {
275 if (!$this->open()) {
279 if(isset($this->listing
) && !$this->listing
) {
284 $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
285 $this->table
, $this->owner
);
287 $res = $this->dbh
->query($query);
289 if (DB
::isError($res)) {
290 return $this->set_error(sprintf(_("Database error: %s"),
291 DB
::errorMessage($res)));
294 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
295 array_push($ret, array('nickname' => $row['nickname'],
296 'name' => "$row[firstname] $row[lastname]",
297 'firstname' => $row['firstname'],
298 'lastname' => $row['lastname'],
299 'email' => $row['email'],
300 'label' => $row['label'],
301 'backend' => $this->bnum
,
302 'source' => &$this->sname
));
309 * @param array $userdata added data
312 function add($userdata) {
313 if (!$this->writeable
) {
314 return $this->set_error(_("Addressbook is read-only"));
317 if (!$this->open()) {
321 /* See if user exist already */
322 $ret = $this->lookup($userdata['nickname']);
324 return $this->set_error(sprintf(_("User \"%s\" already exists"),$ret['nickname']));
328 $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
329 "lastname, email, label) VALUES('%s','%s','%s'," .
331 $this->table
, $this->owner
,
332 $this->dbh
->quoteString($userdata['nickname']),
333 $this->dbh
->quoteString($userdata['firstname']),
334 $this->dbh
->quoteString((!empty($userdata['lastname'])?
$userdata['lastname']:'')),
335 $this->dbh
->quoteString($userdata['email']),
336 $this->dbh
->quoteString((!empty($userdata['label'])?
$userdata['label']:'')) );
339 $r = $this->dbh
->simpleQuery($query);
345 return $this->set_error(sprintf(_("Database error: %s"),
346 DB
::errorMessage($r)));
350 * Deletes address book entries
351 * @param array $alias aliases that have to be deleted. numerical
352 * array with nickname values
355 function remove($alias) {
356 if (!$this->writeable
) {
357 return $this->set_error(_("Addressbook is read-only"));
360 if (!$this->open()) {
365 $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
366 $this->table
, $this->owner
);
369 while (list($undef, $nickname) = each($alias)) {
370 $query .= sprintf("%s nickname='%s' ", $sepstr,
371 $this->dbh
->quoteString($nickname));
377 $r = $this->dbh
->simpleQuery($query);
383 return $this->set_error(sprintf(_("Database error: %s"),
384 DB
::errorMessage($r)));
389 * @param string $alias modified alias
390 * @param array $userdata new data
393 function modify($alias, $userdata) {
394 if (!$this->writeable
) {
395 return $this->set_error(_("Addressbook is read-only"));
398 if (!$this->open()) {
402 /* See if user exist */
403 $ret = $this->lookup($alias);
405 return $this->set_error(sprintf(_("User \"%s\" does not exist"),$alias));
409 $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
410 "lastname='%s', email='%s', label='%s' ".
411 "WHERE owner='%s' AND nickname='%s'",
413 $this->dbh
->quoteString($userdata['nickname']),
414 $this->dbh
->quoteString($userdata['firstname']),
415 $this->dbh
->quoteString((!empty($userdata['lastname'])?
$userdata['lastname']:'')),
416 $this->dbh
->quoteString($userdata['email']),
417 $this->dbh
->quoteString((!empty($userdata['label'])?
$userdata['label']:'')),
419 $this->dbh
->quoteString($alias) );
422 $r = $this->dbh
->simpleQuery($query);
428 return $this->set_error(sprintf(_("Database error: %s"),
429 DB
::errorMessage($r)));
431 } /* End of class abook_database */