db7e1a008a1cb823f83d40e388f2b587c91b44fb
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
13 /** Needs the DB functions */
14 if (!include_once('DB.php')) {
15 // same error also in db_prefs.php
16 $error = _("Could not include PEAR database functions required for the database backend.") . "<br />\n";
17 $error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
18 '<tt>DB.php</tt>') . "<br />\n";
19 $error .= _("Please contact your system administrator and report this error.");
20 error_box($error, $color);
25 * Address book in a database backend
27 * Backend for personal/shared address book stored in a database,
28 * accessed using the DB-classes in PEAR.
30 * IMPORTANT: The PEAR modules must be in the include path
31 * for this class to work.
33 * An array with the following elements must be passed to
34 * the class constructor (elements marked ? are optional):
36 * dsn => database DNS (see PEAR for syntax)
37 * table => table to store addresses in (must exist)
38 * owner => current user (owner of address data)
39 * ? name => name of address book
40 * ? writeable => set writeable flag (true/false)
41 * ? listing => enable/disable listing
43 * The table used should have the following columns:
44 * owner, nickname, firstname, lastname, email, label
45 * The pair (owner,nickname) should be unique (primary key).
47 * NOTE. This class should not be used directly. Use the
48 * "AddressBook" class instead.
49 * @package squirrelmail
50 * @subpackage addressbook
52 class abook_database
extends addressbook_backend
{
62 var $bname = 'database';
65 * Data Source Name (connection description)
70 * Table that stores addresses
77 * Limits list of database entries visible to end user
87 * Enable/disable writing into address book
90 var $writeable = true;
92 * Enable/disable address book listing
97 /* ========================== Private ======================= */
101 * @param array $param address book backend options
103 function abook_database($param) {
104 $this->sname
= _("Personal address book");
106 if (is_array($param)) {
107 if (empty($param['dsn']) ||
108 empty($param['table']) ||
109 empty($param['owner'])) {
110 return $this->set_error('Invalid parameters');
113 $this->dsn
= $param['dsn'];
114 $this->table
= $param['table'];
115 $this->owner
= $param['owner'];
117 if (!empty($param['name'])) {
118 $this->sname
= $param['name'];
121 if (isset($param['writeable'])) {
122 $this->writeable
= $param['writeable'];
125 if (isset($param['listing'])) {
126 $this->listing
= $param['listing'];
132 return $this->set_error('Invalid argument to constructor');
139 * @param bool $new new connection if it is true
142 function open($new = false) {
145 /* Return true is file is open and $new is unset */
146 if ($this->dbh
&& !$new) {
150 /* Close old file, if any */
155 $dbh = DB
::connect($this->dsn
, true);
157 if (DB
::isError($dbh)) {
158 return $this->set_error(sprintf(_("Database error: %s"),
159 DB
::errorMessage($dbh)));
167 * Close the file and forget the filehandle
170 $this->dbh
->disconnect();
174 /* ========================== Public ======================== */
177 * Search the database
178 * @param string $expr search expression
179 * @return array search results
181 function search($expr) {
187 /* To be replaced by advanded search expression parsing */
188 if (is_array($expr)) {
192 // don't allow wide search when listing is disabled.
193 if ($expr=='*' && ! $this->listing
)
196 /* Make regexp from glob'ed expression */
197 $expr = str_replace('?', '_', $expr);
198 $expr = str_replace('*', '%', $expr);
199 $expr = $this->dbh
->quoteString($expr);
202 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
203 "(firstname LIKE '%s' OR lastname LIKE '%s')",
204 $this->table
, $this->owner
, $expr, $expr);
205 $res = $this->dbh
->query($query);
207 if (DB
::isError($res)) {
208 return $this->set_error(sprintf(_("Database error: %s"),
209 DB
::errorMessage($res)));
212 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
213 array_push($ret, array('nickname' => $row['nickname'],
214 'name' => "$row[firstname] $row[lastname]",
215 'firstname' => $row['firstname'],
216 'lastname' => $row['lastname'],
217 'email' => $row['email'],
218 'label' => $row['label'],
219 'backend' => $this->bnum
,
220 'source' => &$this->sname
));
227 * @param string $alias alias
228 * @return array search results
230 function lookup($alias) {
235 $alias = strtolower($alias);
237 if (!$this->open()) {
241 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND LOWER(nickname)='%s'",
242 $this->table
, $this->owner
, $this->dbh
->quoteString($alias));
244 $res = $this->dbh
->query($query);
246 if (DB
::isError($res)) {
247 return $this->set_error(sprintf(_("Database error: %s"),
248 DB
::errorMessage($res)));
251 if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
252 return array('nickname' => $row['nickname'],
253 'name' => "$row[firstname] $row[lastname]",
254 'firstname' => $row['firstname'],
255 'lastname' => $row['lastname'],
256 'email' => $row['email'],
257 'label' => $row['label'],
258 'backend' => $this->bnum
,
259 'source' => &$this->sname
);
266 * @return array search results
268 function list_addr() {
270 if (!$this->open()) {
274 if(isset($this->listing
) && !$this->listing
) {
279 $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
280 $this->table
, $this->owner
);
282 $res = $this->dbh
->query($query);
284 if (DB
::isError($res)) {
285 return $this->set_error(sprintf(_("Database error: %s"),
286 DB
::errorMessage($res)));
289 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
290 array_push($ret, array('nickname' => $row['nickname'],
291 'name' => "$row[firstname] $row[lastname]",
292 'firstname' => $row['firstname'],
293 'lastname' => $row['lastname'],
294 'email' => $row['email'],
295 'label' => $row['label'],
296 'backend' => $this->bnum
,
297 'source' => &$this->sname
));
304 * @param array $userdata added data
307 function add($userdata) {
308 if (!$this->writeable
) {
309 return $this->set_error(_("Addressbook is read-only"));
312 if (!$this->open()) {
316 /* See if user exist already */
317 $ret = $this->lookup($userdata['nickname']);
319 return $this->set_error(sprintf(_("User \"%s\" already exists"),$ret['nickname']));
323 $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
324 "lastname, email, label) VALUES('%s','%s','%s'," .
326 $this->table
, $this->owner
,
327 $this->dbh
->quoteString($userdata['nickname']),
328 $this->dbh
->quoteString($userdata['firstname']),
329 $this->dbh
->quoteString((!empty($userdata['lastname'])?
$userdata['lastname']:'')),
330 $this->dbh
->quoteString($userdata['email']),
331 $this->dbh
->quoteString((!empty($userdata['label'])?
$userdata['label']:'')) );
334 $r = $this->dbh
->simpleQuery($query);
340 return $this->set_error(sprintf(_("Database error: %s"),
341 DB
::errorMessage($r)));
346 * @param string $alias alias that has to be deleted
349 function remove($alias) {
350 if (!$this->writeable
) {
351 return $this->set_error(_("Addressbook is read-only"));
354 if (!$this->open()) {
359 $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
360 $this->table
, $this->owner
);
363 while (list($undef, $nickname) = each($alias)) {
364 $query .= sprintf("%s nickname='%s' ", $sepstr,
365 $this->dbh
->quoteString($nickname));
371 $r = $this->dbh
->simpleQuery($query);
377 return $this->set_error(sprintf(_("Database error: %s"),
378 DB
::errorMessage($r)));
383 * @param string $alias modified alias
384 * @param array $userdata new data
387 function modify($alias, $userdata) {
388 if (!$this->writeable
) {
389 return $this->set_error(_("Addressbook is read-only"));
392 if (!$this->open()) {
396 /* See if user exist */
397 $ret = $this->lookup($alias);
399 return $this->set_error(sprintf(_("User \"%s\" does not exist"),$alias));
403 $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
404 "lastname='%s', email='%s', label='%s' ".
405 "WHERE owner='%s' AND nickname='%s'",
407 $this->dbh
->quoteString($userdata['nickname']),
408 $this->dbh
->quoteString($userdata['firstname']),
409 $this->dbh
->quoteString((!empty($userdata['lastname'])?
$userdata['lastname']:'')),
410 $this->dbh
->quoteString($userdata['email']),
411 $this->dbh
->quoteString((!empty($userdata['label'])?
$userdata['label']:'')),
413 $this->dbh
->quoteString($alias) );
416 $r = $this->dbh
->simpleQuery($query);
422 return $this->set_error(sprintf(_("Database error: %s"),
423 DB
::errorMessage($r)));
425 } /* End of class abook_database */