squirrelmail-webmail  1.4.22
About: SquirrelMail is a standards-based webmail package with strong MIME support, address books, and folder manipulation (written in PHP4).
  Fossies Dox: squirrelmail-webmail-1.4.22.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

abook_database.php
Go to the documentation of this file.
1 <?php
2 
14 if (!include_once('DB.php')) {
15  // same error also in db_prefs.php
16  require_once(SM_PATH . 'functions/display_messages.php');
17  $error = _("Could not include PEAR database functions required for the database backend.") . "<br />\n";
18  $error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
19  '<tt>DB.php</tt>') . "<br />\n";
20  $error .= _("Please contact your system administrator and report this error.");
21  error_box($error, $color);
22  exit;
23 }
24 
58  var $btype = 'local';
63  var $bname = 'database';
64 
69  var $dsn = '';
74  var $table = '';
81  var $owner = '';
86  var $dbh = false;
91  var $writeable = true;
96  var $listing = true;
97 
98  /* ========================== Private ======================= */
99 
104  function abook_database($param) {
105  $this->sname = _("Personal address book");
106 
107  if (is_array($param)) {
108  if (empty($param['dsn']) ||
109  empty($param['table']) ||
110  empty($param['owner'])) {
111  return $this->set_error('Invalid parameters');
112  }
113 
114  $this->dsn = $param['dsn'];
115  $this->table = $param['table'];
116  $this->owner = $param['owner'];
117 
118  if (!empty($param['name'])) {
119  $this->sname = $param['name'];
120  }
121 
122  if (isset($param['writeable'])) {
123  $this->writeable = $param['writeable'];
124  }
125 
126  if (isset($param['listing'])) {
127  $this->listing = $param['listing'];
128  }
129 
130  $this->open(true);
131  }
132  else {
133  return $this->set_error('Invalid argument to constructor');
134  }
135  }
136 
137 
143  function open($new = false) {
144  $this->error = '';
145 
146  /* Return true is file is open and $new is unset */
147  if ($this->dbh && !$new) {
148  return true;
149  }
150 
151  /* Close old file, if any */
152  if ($this->dbh) {
153  $this->close();
154  }
155 
156  $dbh = DB::connect($this->dsn, true);
157 
158  if (DB::isError($dbh)) {
159  return $this->set_error(sprintf(_("Database error: %s"),
160  DB::errorMessage($dbh)));
161  }
162 
163  $this->dbh = $dbh;
164 
169  $this->dbh->setOption('portability', DB_PORTABILITY_LOWERCASE);
170 
171  return true;
172  }
173 
177  function close() {
178  $this->dbh->disconnect();
179  $this->dbh = false;
180  }
181 
193  function get_field_name($field) {
194  switch ($field) {
196  return 'nickname';
198  return 'firstname';
200  return 'lastname';
202  return 'email';
204  return 'label';
205  default:
206  return 'ERROR';
207  }
208  }
209 
210  /* ========================== Public ======================== */
211 
217  function search($expr) {
218  $ret = array();
219  if(!$this->open()) {
220  return false;
221  }
222 
223  /* To be replaced by advanded search expression parsing */
224  if (is_array($expr)) {
225  return;
226  }
227 
228  // don't allow wide search when listing is disabled.
229  if ($expr=='*' && ! $this->listing) {
230  return array();
231  }
232 
233  /* lowercase expression in order to make it case insensitive */
234  $expr = strtolower($expr);
235 
236  /* escape SQL wildcards */
237  $expr = str_replace('_', '\\_', $expr);
238  $expr = str_replace('%', '\\%', $expr);
239 
240  /* Convert wildcards to SQL syntax */
241  $expr = str_replace('?', '_', $expr);
242  $expr = str_replace('*', '%', $expr);
243  $expr = $this->dbh->quoteString($expr);
244  $expr = "%$expr%";
245 
246  /* create escape expression */
247  $escape = 'ESCAPE \'' . $this->dbh->quoteString('\\') . '\'';
248 
249  $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
250  "(LOWER(firstname) LIKE '%s' %s OR LOWER(lastname) LIKE '%s' %s)",
251  $this->table, $this->owner, $expr, $escape, $expr, $escape);
252  $res = $this->dbh->query($query);
253 
254  if (DB::isError($res)) {
255  return $this->set_error(sprintf(_("Database error: %s"),
256  DB::errorMessage($res)));
257  }
258 
259  while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
260  array_push($ret, array('nickname' => $row['nickname'],
261  'name' => "$row[firstname] $row[lastname]",
262  'firstname' => $row['firstname'],
263  'lastname' => $row['lastname'],
264  'email' => $row['email'],
265  'label' => $row['label'],
266  'backend' => $this->bnum,
267  'source' => &$this->sname));
268  }
269  return $ret;
270  }
271 
288  function lookup($value, $field=SM_ABOOK_FIELD_NICKNAME) {
289  if (empty($value)) {
290  return array();
291  }
292 
293  $value = strtolower($value);
294 
295  if (!$this->open()) {
296  return false;
297  }
298 
299  $query = sprintf("SELECT * FROM %s WHERE owner = '%s' AND LOWER(%s) = '%s'",
300  $this->table, $this->owner, $this->get_field_name($field),
301  $this->dbh->quoteString($value));
302 
303  $res = $this->dbh->query($query);
304 
305  if (DB::isError($res)) {
306  return $this->set_error(sprintf(_("Database error: %s"),
307  DB::errorMessage($res)));
308  }
309 
310  if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
311  return array('nickname' => $row['nickname'],
312  'name' => "$row[firstname] $row[lastname]",
313  'firstname' => $row['firstname'],
314  'lastname' => $row['lastname'],
315  'email' => $row['email'],
316  'label' => $row['label'],
317  'backend' => $this->bnum,
318  'source' => &$this->sname);
319  }
320  return array();
321  }
322 
327  function list_addr() {
328  $ret = array();
329  if (!$this->open()) {
330  return false;
331  }
332 
333  if(isset($this->listing) && !$this->listing) {
334  return array();
335  }
336 
337 
338  $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
339  $this->table, $this->owner);
340 
341  $res = $this->dbh->query($query);
342 
343  if (DB::isError($res)) {
344  return $this->set_error(sprintf(_("Database error: %s"),
345  DB::errorMessage($res)));
346  }
347 
348  while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
349  array_push($ret, array('nickname' => $row['nickname'],
350  'name' => "$row[firstname] $row[lastname]",
351  'firstname' => $row['firstname'],
352  'lastname' => $row['lastname'],
353  'email' => $row['email'],
354  'label' => $row['label'],
355  'backend' => $this->bnum,
356  'source' => &$this->sname));
357  }
358  return $ret;
359  }
360 
366  function add($userdata) {
367  if (!$this->writeable) {
368  return $this->set_error(_("Address book is read-only"));
369  }
370 
371  if (!$this->open()) {
372  return false;
373  }
374 
375  /* See if user exist already */
376  $ret = $this->lookup($userdata['nickname']);
377  if (!empty($ret)) {
378  return $this->set_error(sprintf(_("User \"%s\" already exists"), $ret['nickname']));
379  }
380 
381  /* Create query */
382  $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
383  "lastname, email, label) VALUES('%s','%s','%s'," .
384  "'%s','%s','%s')",
385  $this->table, $this->owner,
386  $this->dbh->quoteString($userdata['nickname']),
387  $this->dbh->quoteString($userdata['firstname']),
388  $this->dbh->quoteString((!empty($userdata['lastname'])?$userdata['lastname']:'')),
389  $this->dbh->quoteString($userdata['email']),
390  $this->dbh->quoteString((!empty($userdata['label'])?$userdata['label']:'')) );
391 
392  /* Do the insert */
393  $r = $this->dbh->simpleQuery($query);
394 
395  /* Check for errors */
396  if (DB::isError($r)) {
397  return $this->set_error(sprintf(_("Database error: %s"),
398  DB::errorMessage($r)));
399  }
400 
401  return true;
402  }
403 
409  function remove($alias) {
410  if (!$this->writeable) {
411  return $this->set_error(_("Address book is read-only"));
412  }
413 
414  if (!$this->open()) {
415  return false;
416  }
417 
418  /* Create query */
419  $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
420  $this->table, $this->owner);
421 
422  $sepstr = '';
423  while (list($undef, $nickname) = each($alias)) {
424  $query .= sprintf("%s nickname='%s' ", $sepstr,
425  $this->dbh->quoteString($nickname));
426  $sepstr = 'OR';
427  }
428  $query .= ')';
429 
430  /* Delete entry */
431  $r = $this->dbh->simpleQuery($query);
432 
433  /* Check for errors */
434  if (DB::isError($r)) {
435  return $this->set_error(sprintf(_("Database error: %s"),
436  DB::errorMessage($r)));
437  }
438  return true;
439  }
440 
447  function modify($alias, $userdata) {
448  if (!$this->writeable) {
449  return $this->set_error(_("Address book is read-only"));
450  }
451 
452  if (!$this->open()) {
453  return false;
454  }
455 
456  /* See if user exist */
457  $ret = $this->lookup($alias);
458  if (empty($ret)) {
459  return $this->set_error(sprintf(_("User \"%s\" does not exist"), $alias));
460  }
461 
462  /* make sure that new nickname is not used */
463  if (strtolower($alias) != strtolower($userdata['nickname'])) {
464  /* same check as in add() */
465  $ret = $this->lookup($userdata['nickname']);
466  if (!empty($ret)) {
467  $error = sprintf(_("User '%s' already exist."), $ret['nickname']);
468  return $this->set_error($error);
469  }
470  }
471 
472  /* Create query */
473  $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
474  "lastname='%s', email='%s', label='%s' ".
475  "WHERE owner='%s' AND nickname='%s'",
476  $this->table,
477  $this->dbh->quoteString($userdata['nickname']),
478  $this->dbh->quoteString($userdata['firstname']),
479  $this->dbh->quoteString((!empty($userdata['lastname'])?$userdata['lastname']:'')),
480  $this->dbh->quoteString($userdata['email']),
481  $this->dbh->quoteString((!empty($userdata['label'])?$userdata['label']:'')),
482  $this->owner,
483  $this->dbh->quoteString($alias) );
484 
485  /* Do the insert */
486  $r = $this->dbh->simpleQuery($query);
487 
488  /* Check for errors */
489  if (DB::isError($r)) {
490  return $this->set_error(sprintf(_("Database error: %s"),
491  DB::errorMessage($r)));
492  }
493  return true;
494  }
495 } /* End of class abook_database */
496 
497 // vim: et ts=4
SM_ABOOK_FIELD_NICKNAME
const SM_ABOOK_FIELD_NICKNAME
Definition: constants.php:57
$ret
$ret
Definition: webmail.php:172
abook_database\$listing
$listing
Definition: abook_database.php:96
SM_ABOOK_FIELD_FIRSTNAME
const SM_ABOOK_FIELD_FIRSTNAME
Definition: constants.php:58
abook_database\modify
modify($alias, $userdata)
Definition: abook_database.php:447
abook_database\$btype
$btype
Definition: abook_database.php:58
sprintf
powered by Systran sprintf(_("Number of supported language pairs: %s"), '36').' '
Definition: options.php:107
addressbook_backend\set_error
set_error($string)
Definition: addressbook.php:656
SM_ABOOK_FIELD_LASTNAME
const SM_ABOOK_FIELD_LASTNAME
Definition: constants.php:59
abook_database\$table
$table
Definition: abook_database.php:74
abook_database\list_addr
list_addr()
Definition: abook_database.php:327
abook_database\close
close()
Definition: abook_database.php:177
addressbook_backend\$error
$error
Definition: addressbook.php:653
abook_database\$dsn
$dsn
Definition: abook_database.php:69
abook_database\$writeable
$writeable
Definition: abook_database.php:91
abook_database\open
open($new=false)
Definition: abook_database.php:143
abook_database\abook_database
abook_database($param)
Definition: abook_database.php:104
$r
while(count($Left) > 0) $r
Definition: darkness.php:103
addressbook_backend
Definition: addressbook.php:641
abook_database\get_field_name
get_field_name($field)
Definition: abook_database.php:193
SM_ABOOK_FIELD_EMAIL
const SM_ABOOK_FIELD_EMAIL
Definition: constants.php:60
exit
exit
Definition: help.php:128
SM_PATH
const SM_PATH
Definition: decrypt_headers.php:16
_
_($str)
Definition: gettext.php:160
abook_database\$bname
$bname
Definition: abook_database.php:63
abook_database\lookup
lookup($value, $field=SM_ABOOK_FIELD_NICKNAME)
Definition: abook_database.php:288
abook_database
Definition: abook_database.php:53
abook_database\$dbh
$dbh
Definition: abook_database.php:86
$color
$color
Definition: load_prefs.php:28
abook_database\add
add($userdata)
Definition: abook_database.php:366
abook_database\$owner
$owner
Definition: abook_database.php:81
SM_ABOOK_FIELD_LABEL
const SM_ABOOK_FIELD_LABEL
Definition: constants.php:61
abook_database\search
search($expr)
Definition: abook_database.php:217
error_box
error_box($string, $color)
Definition: display_messages.php:116