14 if (!include_once(
'DB.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.");
105 $this->sname =
_(
"Personal address book");
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');
114 $this->dsn = $param[
'dsn'];
115 $this->table = $param[
'table'];
116 $this->owner = $param[
'owner'];
118 if (!empty($param[
'name'])) {
119 $this->sname = $param[
'name'];
122 if (isset($param[
'writeable'])) {
123 $this->writeable = $param[
'writeable'];
126 if (isset($param[
'listing'])) {
127 $this->listing = $param[
'listing'];
133 return $this->
set_error(
'Invalid argument to constructor');
147 if ($this->dbh && !$new) {
156 $dbh = DB::connect($this->dsn,
true);
158 if (DB::isError(
$dbh)) {
160 DB::errorMessage(
$dbh)));
169 $this->dbh->setOption(
'portability', DB_PORTABILITY_LOWERCASE);
178 $this->dbh->disconnect();
224 if (is_array($expr)) {
229 if ($expr==
'*' && ! $this->listing) {
234 $expr = strtolower($expr);
237 $expr = str_replace(
'_',
'\\_', $expr);
238 $expr = str_replace(
'%',
'\\%', $expr);
241 $expr = str_replace(
'?',
'_', $expr);
242 $expr = str_replace(
'*',
'%', $expr);
243 $expr = $this->dbh->quoteString($expr);
247 $escape =
'ESCAPE \'' . $this->dbh->quoteString(
'\\') .
'\'';
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);
254 if (DB::isError($res)) {
256 DB::errorMessage($res)));
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));
293 $value = strtolower($value);
295 if (!$this->
open()) {
299 $query =
sprintf(
"SELECT * FROM %s WHERE owner = '%s' AND LOWER(%s) = '%s'",
301 $this->dbh->quoteString($value));
303 $res = $this->dbh->query($query);
305 if (DB::isError($res)) {
307 DB::errorMessage($res)));
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);
329 if (!$this->
open()) {
333 if(isset($this->listing) && !$this->listing) {
338 $query =
sprintf(
"SELECT * FROM %s WHERE owner='%s'",
339 $this->table, $this->owner);
341 $res = $this->dbh->query($query);
343 if (DB::isError($res)) {
345 DB::errorMessage($res)));
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));
367 if (!$this->writeable) {
368 return $this->
set_error(
_(
"Address book is read-only"));
371 if (!$this->
open()) {
382 $query =
sprintf(
"INSERT INTO %s (owner, nickname, firstname, " .
383 "lastname, email, label) VALUES('%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']:
'')) );
393 $r = $this->dbh->simpleQuery($query);
396 if (DB::isError(
$r)) {
398 DB::errorMessage(
$r)));
409 function remove($alias) {
410 if (!$this->writeable) {
411 return $this->
set_error(
_(
"Address book is read-only"));
414 if (!$this->
open()) {
419 $query =
sprintf(
"DELETE FROM %s WHERE owner='%s' AND (",
420 $this->table, $this->owner);
423 while (list($undef, $nickname) = each($alias)) {
424 $query .=
sprintf(
"%s nickname='%s' ", $sepstr,
425 $this->dbh->quoteString($nickname));
431 $r = $this->dbh->simpleQuery($query);
434 if (DB::isError(
$r)) {
436 DB::errorMessage(
$r)));
448 if (!$this->writeable) {
449 return $this->
set_error(
_(
"Address book is read-only"));
452 if (!$this->
open()) {
463 if (strtolower($alias) != strtolower($userdata[
'nickname'])) {
473 $query =
sprintf(
"UPDATE %s SET nickname='%s', firstname='%s', ".
474 "lastname='%s', email='%s', label='%s' ".
475 "WHERE owner='%s' AND nickname='%s'",
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']:
'')),
483 $this->dbh->quoteString($alias) );
486 $r = $this->dbh->simpleQuery($query);
489 if (DB::isError(
$r)) {
491 DB::errorMessage(
$r)));