Source for file abook_database.php
Documentation is available at abook_database.php
* Supported database schema
* owner varchar(128) NOT NULL
* nickname varchar(16) NOT NULL
* email varchar(128) NOT NULL
* PRIMARY KEY (owner,nickname)
* @copyright © 1999-2006 The SquirrelMail Project Team
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version $Id: abook_database.php,v 1.44 2006/07/15 12:00:44 tokul Exp $
* @subpackage addressbook
* Don't display errors here. Error will be set in class constructor function.
* Address book in a database backend
* Backend for personal/shared address book stored in a database,
* accessed using the DB-classes in PEAR.
* IMPORTANT: The PEAR modules must be in the include path
* for this class to work.
* An array with the following elements must be passed to
* the class constructor (elements marked ? are optional):
* dsn => database DNS (see PEAR for syntax)
* table => table to store addresses in (must exist)
* owner => current user (owner of address data)
* ? name => name of address book
* ? writeable => set writeable flag (true/false)
* ? listing => enable/disable listing
* The table used should have the following columns:
* owner, nickname, firstname, lastname, email, label
* The pair (owner,nickname) should be unique (primary key).
* NOTE. This class should not be used directly. Use the
* "AddressBook" class instead.
* @subpackage addressbook
* Data Source Name (connection description)
* Table that stores addresses
* Limits list of database entries visible to end user
* Enable/disable writing into address book
* Enable/disable address book listing
/* ========================== Private ======================= */
* @param array $param address book backend options
$this->sname =
_("Personal address book");
/* test if Pear DB class is available and freak out if it is not */
// same error also in db_prefs.php
$error =
_("Could not include PEAR database functions required for the database backend.") .
"\n";
$error .=
sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
$error .=
_("Please contact your system administrator and report this error.");
if (empty($param['dsn']) ||
empty($param['table']) ||
empty($param['owner'])) {
return $this->set_error('Invalid parameters');
$this->dsn =
$param['dsn'];
$this->table =
$param['table'];
$this->owner =
$param['owner'];
if (!empty($param['name'])) {
$this->sname =
$param['name'];
if (isset
($param['writeable'])) {
if (isset
($param['listing'])) {
$this->listing =
$param['listing'];
return $this->set_error('Invalid argument to constructor');
* @param bool $new new connection if it is true
function open($new =
false) {
/* Return true is file is open and $new is unset */
if ($this->dbh &&
!$new) {
/* Close old file, if any */
$dbh =
DB::connect($this->dsn, true);
DB::errorMessage($dbh)));
* field names are lowercased.
* We use unquoted identifiers and they use upper case in Oracle
$this->dbh->setOption('portability', DB_PORTABILITY_LOWERCASE);
* Close the file and forget the filehandle
$this->dbh->disconnect();
/* ========================== Public ======================== */
* Backend supports only * and ? wildcards. Complex eregs are not supported.
* Search is case insensitive.
* @param string $expr search expression
* @return array search results. boolean false on error
/* To be replaced by advanded search expression parsing */
// don't allow wide search when listing is disabled.
if ($expr==
'*' &&
! $this->listing)
/* lowercase expression in order to make it case insensitive */
/* escape SQL wildcards */
/* Convert wildcards to SQL syntax */
$expr =
$this->dbh->quoteString($expr);
/* create escape expression */
$escape =
'ESCAPE \'' .
$this->dbh->quoteString('\\') .
'\'';
$query =
sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
"(LOWER(firstname) LIKE '%s' %s OR LOWER(lastname) LIKE '%s' %s)",
$this->table, $this->owner, $expr, $escape, $expr, $escape);
$res =
$this->dbh->query($query);
DB::errorMessage($res)));
while ($row =
$res->fetchRow(DB_FETCHMODE_ASSOC)) {
array_push($ret, array('nickname' =>
$row['nickname'],
'name' =>
$this->fullname($row['firstname'], $row['lastname']),
'firstname' =>
$row['firstname'],
'lastname' =>
$row['lastname'],
'email' =>
$row['email'],
'label' =>
$row['label'],
'backend' =>
$this->bnum,
'source' =>
&$this->sname));
* @param string $alias alias
* @return array search results
$query =
sprintf("SELECT * FROM %s WHERE owner='%s' AND LOWER(nickname)='%s'",
$res =
$this->dbh->query($query);
DB::errorMessage($res)));
if ($row =
$res->fetchRow(DB_FETCHMODE_ASSOC)) {
return array('nickname' =>
$row['nickname'],
'name' =>
$this->fullname($row['firstname'], $row['lastname']),
'firstname' =>
$row['firstname'],
'lastname' =>
$row['lastname'],
'email' =>
$row['email'],
'label' =>
$row['label'],
'backend' =>
$this->bnum,
'source' =>
&$this->sname);
* @return array search results
$query =
sprintf("SELECT * FROM %s WHERE owner='%s'",
$res =
$this->dbh->query($query);
DB::errorMessage($res)));
while ($row =
$res->fetchRow(DB_FETCHMODE_ASSOC)) {
array_push($ret, array('nickname' =>
$row['nickname'],
'name' =>
$this->fullname($row['firstname'], $row['lastname']),
'firstname' =>
$row['firstname'],
'lastname' =>
$row['lastname'],
'email' =>
$row['email'],
'label' =>
$row['label'],
'backend' =>
$this->bnum,
'source' =>
&$this->sname));
* @param array $userdata added data
function add($userdata) {
return $this->set_error(_("Address book is read-only"));
/* See if user exist already */
$ret =
$this->lookup($userdata['nickname']);
$query =
sprintf("INSERT INTO %s (owner, nickname, firstname, " .
"lastname, email, label) VALUES('%s','%s','%s'," .
$this->dbh->quoteString($userdata['nickname']),
$this->dbh->quoteString($userdata['firstname']),
$this->dbh->quoteString((!empty($userdata['lastname'])?
$userdata['lastname']:
'')),
$this->dbh->quoteString($userdata['email']),
$this->dbh->quoteString((!empty($userdata['label'])?
$userdata['label']:
'')) );
$r =
$this->dbh->simpleQuery($query);
* Deletes address book entries
* @param array $alias aliases that have to be deleted. numerical
* array with nickname values
return $this->set_error(_("Address book is read-only"));
$query =
sprintf("DELETE FROM %s WHERE owner='%s' AND (",
while (list
($undef, $nickname) =
each($alias)) {
$query .=
sprintf("%s nickname='%s' ", $sepstr,
$this->dbh->quoteString($nickname));
$r =
$this->dbh->simpleQuery($query);
* @param string $alias modified alias
* @param array $userdata new data
function modify($alias, $userdata) {
return $this->set_error(_("Address book is read-only"));
/* make sure that new nickname is not used */
/* same check as in add() */
$ret =
$this->lookup($userdata['nickname']);
$error =
sprintf(_("User '%s' already exist."), $ret['nickname']);
$query =
sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
"lastname='%s', email='%s', label='%s' ".
"WHERE owner='%s' AND nickname='%s'",
$this->dbh->quoteString($userdata['nickname']),
$this->dbh->quoteString($userdata['firstname']),
$this->dbh->quoteString((!empty($userdata['lastname'])?
$userdata['lastname']:
'')),
$this->dbh->quoteString($userdata['email']),
$this->dbh->quoteString((!empty($userdata['label'])?
$userdata['label']:
'')),
$this->dbh->quoteString($alias) );
$r =
$this->dbh->simpleQuery($query);
} /* End of class abook_database */
Documentation generated on Sat, 07 Oct 2006 16:08:24 +0300 by phpDocumentor 1.3.0RC6