Fix capability retrieval for "double" cases in server capability response, for instan...
[squirrelmail.git] / functions / abook_database.php
CommitLineData
9b9474d6 1<?php
4b4abf93 2
35586184 3/**
4 * abook_database.php
5 *
47ccfad4 6 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
4b4abf93 7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
a9d318b0 8 * @version $Id$
d6c32258 9 * @package squirrelmail
a9d318b0 10 * @subpackage addressbook
35586184 11 */
d6c32258 12
6d429ce6 13/**
14 * Needs the DB functions
15 * Don't display errors here. Error will be set in class constructor function.
16 */
17@include_once('DB.php');
d6c32258 18
19/**
0541c5ae 20 * Address book in a database backend
21 *
22 * Backend for personal/shared address book stored in a database,
23 * accessed using the DB-classes in PEAR.
24 *
25 * IMPORTANT: The PEAR modules must be in the include path
26 * for this class to work.
27 *
28 * An array with the following elements must be passed to
29 * the class constructor (elements marked ? are optional):
30 * <pre>
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
37 * </pre>
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).
41 *
42 * NOTE. This class should not be used directly. Use the
43 * "AddressBook" class instead.
d6c32258 44 * @package squirrelmail
0541c5ae 45 * @subpackage addressbook
d6c32258 46 */
9b9474d6 47class abook_database extends addressbook_backend {
0541c5ae 48 /**
49 * Backend type
50 * @var string
51 */
9b9474d6 52 var $btype = 'local';
0541c5ae 53 /**
54 * Backend name
55 * @var string
56 */
9b9474d6 57 var $bname = 'database';
62f7daa5 58
0541c5ae 59 /**
60 * Data Source Name (connection description)
61 * @var string
62 */
9b9474d6 63 var $dsn = '';
0541c5ae 64 /**
65 * Table that stores addresses
66 * @var string
67 */
9b9474d6 68 var $table = '';
0541c5ae 69 /**
70 * Owner name
71 *
72 * Limits list of database entries visible to end user
73 * @var string
74 */
9b9474d6 75 var $owner = '';
0541c5ae 76 /**
77 * Database Handle
78 * @var resource
79 */
9b9474d6 80 var $dbh = false;
0541c5ae 81 /**
82 * Enable/disable writing into address book
83 * @var bool
84 */
9b9474d6 85 var $writeable = true;
0541c5ae 86 /**
87 * Enable/disable address book listing
88 * @var bool
89 */
90 var $listing = true;
62f7daa5 91
9b9474d6 92 /* ========================== Private ======================= */
62f7daa5 93
0541c5ae 94 /**
95 * Constructor
96 * @param array $param address book backend options
97 */
9b9474d6 98 function abook_database($param) {
99 $this->sname = _("Personal address book");
62f7daa5 100
6d429ce6 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);
109 }
110
9b9474d6 111 if (is_array($param)) {
62f7daa5 112 if (empty($param['dsn']) ||
113 empty($param['table']) ||
9b9474d6 114 empty($param['owner'])) {
115 return $this->set_error('Invalid parameters');
116 }
62f7daa5 117
91be2362 118 $this->dsn = $param['dsn'];
119 $this->table = $param['table'];
120 $this->owner = $param['owner'];
62f7daa5 121
9b9474d6 122 if (!empty($param['name'])) {
91be2362 123 $this->sname = $param['name'];
9b9474d6 124 }
7902aca2 125
9b9474d6 126 if (isset($param['writeable'])) {
91be2362 127 $this->writeable = $param['writeable'];
9b9474d6 128 }
7902aca2 129
30e9932c 130 if (isset($param['listing'])) {
131 $this->listing = $param['listing'];
132 }
133
7902aca2 134 $this->open(true);
9b9474d6 135 }
136 else {
91be2362 137 return $this->set_error('Invalid argument to constructor');
9b9474d6 138 }
139 }
62f7daa5 140
141
0541c5ae 142 /**
e50f5ac2 143 * Open the database.
0541c5ae 144 * @param bool $new new connection if it is true
145 * @return bool
146 */
9b9474d6 147 function open($new = false) {
148 $this->error = '';
62f7daa5 149
9b9474d6 150 /* Return true is file is open and $new is unset */
151 if ($this->dbh && !$new) {
7902aca2 152 return true;
9b9474d6 153 }
62f7daa5 154
9b9474d6 155 /* Close old file, if any */
156 if ($this->dbh) {
157 $this->close();
158 }
62f7daa5 159
9b9474d6 160 $dbh = DB::connect($this->dsn, true);
62f7daa5 161
286fe80b 162 if (DB::isError($dbh)) {
701c9c6b 163 return $this->set_error(sprintf(_("Database error: %s"),
7902aca2 164 DB::errorMessage($dbh)));
9b9474d6 165 }
62f7daa5 166
9b9474d6 167 $this->dbh = $dbh;
168 return true;
169 }
7902aca2 170
0541c5ae 171 /**
172 * Close the file and forget the filehandle
173 */
9b9474d6 174 function close() {
175 $this->dbh->disconnect();
176 $this->dbh = false;
177 }
7902aca2 178
9b9474d6 179 /* ========================== Public ======================== */
62f7daa5 180
0541c5ae 181 /**
182 * Search the database
183 * @param string $expr search expression
184 * @return array search results
185 */
aa8c4265 186 function search($expr) {
9b9474d6 187 $ret = array();
188 if(!$this->open()) {
7902aca2 189 return false;
9b9474d6 190 }
30e9932c 191
9b9474d6 192 /* To be replaced by advanded search expression parsing */
193 if (is_array($expr)) {
194 return;
195 }
196
327e2d96 197 // don't allow wide search when listing is disabled.
198 if ($expr=='*' && ! $this->listing)
199 return array();
200
9b9474d6 201 /* Make regexp from glob'ed expression */
202 $expr = str_replace('?', '_', $expr);
203 $expr = str_replace('*', '%', $expr);
204 $expr = $this->dbh->quoteString($expr);
205 $expr = "%$expr%";
206
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);
211
212 if (DB::isError($res)) {
701c9c6b 213 return $this->set_error(sprintf(_("Database error: %s"),
7902aca2 214 DB::errorMessage($res)));
9b9474d6 215 }
7902aca2 216
9b9474d6 217 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
91be2362 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));
9b9474d6 226 }
227 return $ret;
228 }
62f7daa5 229
0541c5ae 230 /**
231 * Lookup alias
232 * @param string $alias alias
233 * @return array search results
234 */
aa8c4265 235 function lookup($alias) {
9b9474d6 236 if (empty($alias)) {
7902aca2 237 return array();
9b9474d6 238 }
62f7daa5 239
9b9474d6 240 $alias = strtolower($alias);
7902aca2 241
9b9474d6 242 if (!$this->open()) {
7902aca2 243 return false;
9b9474d6 244 }
62f7daa5 245
2c90a9f2 246 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND LOWER(nickname)='%s'",
11f2b6ba 247 $this->table, $this->owner, $this->dbh->quoteString($alias));
7902aca2 248
9b9474d6 249 $res = $this->dbh->query($query);
7902aca2 250
9b9474d6 251 if (DB::isError($res)) {
701c9c6b 252 return $this->set_error(sprintf(_("Database error: %s"),
7902aca2 253 DB::errorMessage($res)));
9b9474d6 254 }
7902aca2 255
9b9474d6 256 if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
91be2362 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);
9b9474d6 265 }
266 return array();
267 }
268
0541c5ae 269 /**
e50f5ac2 270 * List all addresses
0541c5ae 271 * @return array search results
272 */
aa8c4265 273 function list_addr() {
9b9474d6 274 $ret = array();
275 if (!$this->open()) {
7902aca2 276 return false;
9b9474d6 277 }
62f7daa5 278
91e0dccc 279 if(isset($this->listing) && !$this->listing) {
280 return array();
281 }
30e9932c 282
7902aca2 283
9b9474d6 284 $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
285 $this->table, $this->owner);
7902aca2 286
9b9474d6 287 $res = $this->dbh->query($query);
62f7daa5 288
9b9474d6 289 if (DB::isError($res)) {
701c9c6b 290 return $this->set_error(sprintf(_("Database error: %s"),
7902aca2 291 DB::errorMessage($res)));
9b9474d6 292 }
7902aca2 293
9b9474d6 294 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
91be2362 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));
9b9474d6 303 }
304 return $ret;
305 }
7902aca2 306
0541c5ae 307 /**
308 * Add address
309 * @param array $userdata added data
310 * @return bool
311 */
9b9474d6 312 function add($userdata) {
313 if (!$this->writeable) {
701c9c6b 314 return $this->set_error(_("Addressbook is read-only"));
9b9474d6 315 }
7902aca2 316
9b9474d6 317 if (!$this->open()) {
7902aca2 318 return false;
9b9474d6 319 }
62f7daa5 320
9b9474d6 321 /* See if user exist already */
322 $ret = $this->lookup($userdata['nickname']);
323 if (!empty($ret)) {
2706a0b1 324 return $this->set_error(sprintf(_("User \"%s\" already exists"),$ret['nickname']));
9b9474d6 325 }
326
327 /* Create query */
328 $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
329 "lastname, email, label) VALUES('%s','%s','%s'," .
330 "'%s','%s','%s')",
331 $this->table, $this->owner,
332 $this->dbh->quoteString($userdata['nickname']),
62f7daa5 333 $this->dbh->quoteString($userdata['firstname']),
8419c13b 334 $this->dbh->quoteString((!empty($userdata['lastname'])?$userdata['lastname']:'')),
62f7daa5 335 $this->dbh->quoteString($userdata['email']),
8419c13b 336 $this->dbh->quoteString((!empty($userdata['label'])?$userdata['label']:'')) );
9b9474d6 337
338 /* Do the insert */
7902aca2 339 $r = $this->dbh->simpleQuery($query);
9b9474d6 340 if ($r == DB_OK) {
341 return true;
342 }
7902aca2 343
9b9474d6 344 /* Fail */
701c9c6b 345 return $this->set_error(sprintf(_("Database error: %s"),
7902aca2 346 DB::errorMessage($r)));
9b9474d6 347 }
7902aca2 348
0541c5ae 349 /**
eee5aa69 350 * Deletes address book entries
351 * @param array $alias aliases that have to be deleted. numerical
352 * array with nickname values
0541c5ae 353 * @return bool
354 */
9b9474d6 355 function remove($alias) {
356 if (!$this->writeable) {
701c9c6b 357 return $this->set_error(_("Addressbook is read-only"));
9b9474d6 358 }
7902aca2 359
9b9474d6 360 if (!$this->open()) {
7902aca2 361 return false;
9b9474d6 362 }
62f7daa5 363
9b9474d6 364 /* Create query */
365 $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
366 $this->table, $this->owner);
7902aca2 367
9b9474d6 368 $sepstr = '';
369 while (list($undef, $nickname) = each($alias)) {
16a973d7 370 $query .= sprintf("%s nickname='%s' ", $sepstr,
7902aca2 371 $this->dbh->quoteString($nickname));
91be2362 372 $sepstr = 'OR';
9b9474d6 373 }
374 $query .= ')';
7902aca2 375
9b9474d6 376 /* Delete entry */
377 $r = $this->dbh->simpleQuery($query);
378 if ($r == DB_OK) {
379 return true;
380 }
7902aca2 381
9b9474d6 382 /* Fail */
383 return $this->set_error(sprintf(_("Database error: %s"),
7902aca2 384 DB::errorMessage($r)));
9b9474d6 385 }
7902aca2 386
0541c5ae 387 /**
388 * Modify address
389 * @param string $alias modified alias
390 * @param array $userdata new data
391 * @return bool
392 */
9b9474d6 393 function modify($alias, $userdata) {
394 if (!$this->writeable) {
701c9c6b 395 return $this->set_error(_("Addressbook is read-only"));
9b9474d6 396 }
7902aca2 397
9b9474d6 398 if (!$this->open()) {
7902aca2 399 return false;
9b9474d6 400 }
62f7daa5 401
9b9474d6 402 /* See if user exist */
403 $ret = $this->lookup($alias);
404 if (empty($ret)) {
2706a0b1 405 return $this->set_error(sprintf(_("User \"%s\" does not exist"),$alias));
9b9474d6 406 }
407
408 /* Create query */
409 $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
410 "lastname='%s', email='%s', label='%s' ".
411 "WHERE owner='%s' AND nickname='%s'",
62f7daa5 412 $this->table,
9b9474d6 413 $this->dbh->quoteString($userdata['nickname']),
62f7daa5 414 $this->dbh->quoteString($userdata['firstname']),
8419c13b 415 $this->dbh->quoteString((!empty($userdata['lastname'])?$userdata['lastname']:'')),
62f7daa5 416 $this->dbh->quoteString($userdata['email']),
8419c13b 417 $this->dbh->quoteString((!empty($userdata['label'])?$userdata['label']:'')),
9b9474d6 418 $this->owner,
419 $this->dbh->quoteString($alias) );
420
421 /* Do the insert */
422 $r = $this->dbh->simpleQuery($query);
423 if ($r == DB_OK) {
424 return true;
425 }
7902aca2 426
9b9474d6 427 /* Fail */
428 return $this->set_error(sprintf(_("Database error: %s"),
429 DB::errorMessage($r)));
430 }
431} /* End of class abook_database */
7902aca2 432
c9fcea56 433// vim: et ts=4
8419c13b 434?>