6 ** Backend for personal addressbook stored in a database,
7 ** accessed using the DB-casses in PEAR.
9 ** IMPORTANT: The PEAR modules must be in the include path
10 ** for this class to work.
12 ** An array with the following elements must be passed to
13 ** the class constructor (elements marked ? are optional):
15 ** dsn => database DNS (see PEAR for syntax)
16 ** table => table to store addresses in (must exist)
17 ** owner => current user (owner of address data)
18 ** ? writeable => set writeable flag (true/false)
20 ** The table used should have the following columns:
21 ** owner, nickname, firstname, lastname, email, label
22 ** The pair (owner,nickname) should be unique (primary key).
24 ** NOTE. This class should not be used directly. Use the
25 ** "AddressBook" class instead.
28 require_once("DB.php");
30 class abook_database
extends addressbook_backend
{
32 var $bname = "database";
39 var $writeable = true;
41 // ========================== Private =======================
44 function abook_database($param) {
45 $this->sname
= _("Personal address book");
47 if(is_array($param)) {
48 if(empty($param["dsn"]) ||
49 empty($param["table"]) ||
50 empty($param["owner"]))
51 return $this->set_error("Invalid parameters");
53 $this->dsn
= $param["dsn"];
54 $this->table
= $param["table"];
55 $this->owner
= $param["owner"];
57 if(!empty($param["name"]))
58 $this->sname
= $param["name"];
60 if(isset($param["writeable"]))
61 $this->writeable
= $param["writeable"];
65 return $this->set_error("Invalid argument to constructor");
70 // Open the database. New connection if $new is true
71 function open($new = false) {
74 // Return true is file is open and $new is unset
75 if($this->dbh
&& !$new)
78 // Close old file, if any
79 if($this->dbh
) $this->close();
81 $dbh = DB
::connect($this->dsn
, true);
83 if(DB
::isError($dbh) || DB
::isWarning($dbh))
84 return $this->set_error(sprintf(_("Database error: %s"),
85 DB
::errorMessage($dbh)));
91 // Close the file and forget the filehandle
93 $this->dbh
->disconnect();
97 // ========================== Public ========================
100 function &search($expr) {
105 // To be replaced by advanded search expression parsing
106 if(is_array($expr)) return;
108 // Make regexp from glob'ed expression
109 $expr = ereg_replace("\?", "_", $expr);
110 $expr = ereg_replace("\*", "%", $expr);
111 $expr = $this->dbh
->quoteString($expr);
114 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND ".
115 "(firstname LIKE '%s' OR lastname LIKE '%s')",
116 $this->table
, $this->owner
, $expr, $expr);
117 $res = $this->dbh
->query($query);
119 if(DB
::isError($res))
120 return $this->set_error(sprintf(_("Database error: %s"),
121 DB
::errorMessage($res)));
123 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
124 array_push($ret, array("nickname" => $row["nickname"],
125 "name" => $row["firstname"] . " " . $row["lastname"],
126 "firstname" => $row["firstname"],
127 "lastname" => $row["lastname"],
128 "email" => $row["email"],
129 "label" => $row["label"],
130 "backend" => $this->bnum
,
131 "source" => &$this->sname
));
137 function &lookup($alias) {
141 $alias = strtolower($alias);
146 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND nickname='%s'",
147 $this->table
, $this->owner
, $alias);
149 $res = $this->dbh
->query($query);
151 if(DB
::isError($res))
152 return $this->set_error(sprintf(_("Database error: %s"),
153 DB
::errorMessage($res)));
155 if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
156 return array("nickname" => $row["nickname"],
157 "name" => $row["firstname"] . " " . $row["lastname"],
158 "firstname" => $row["firstname"],
159 "lastname" => $row["lastname"],
160 "email" => $row["email"],
161 "label" => $row["label"],
162 "backend" => $this->bnum
,
163 "source" => &$this->sname
);
169 // List all addresses
170 function &list_addr() {
175 $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
176 $this->table
, $this->owner
);
178 $res = $this->dbh
->query($query);
180 if(DB
::isError($res))
181 return $this->set_error(sprintf(_("Database error: %s"),
182 DB
::errorMessage($res)));
184 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC
)) {
185 array_push($ret, array("nickname" => $row["nickname"],
186 "name" => $row["firstname"] . " " . $row["lastname"],
187 "firstname" => $row["firstname"],
188 "lastname" => $row["lastname"],
189 "email" => $row["email"],
190 "label" => $row["label"],
191 "backend" => $this->bnum
,
192 "source" => &$this->sname
));
198 function add($userdata) {
199 if(!$this->writeable
)
200 return $this->set_error(_("Addressbook is read-only"));
205 // See if user exist already
206 $ret = $this->lookup($userdata["nickname"]);
208 return $this->set_error(sprintf(_("User '%s' already exist"),
212 $query = sprintf("INSERT INTO %s (owner, nickname, firstname, ".
213 "lastname, email, label) VALUES('%s','%s','%s',".
215 $this->table
, $this->owner
,
216 $this->dbh
->quoteString($userdata["nickname"]),
217 $this->dbh
->quoteString($userdata["firstname"]),
218 $this->dbh
->quoteString($userdata["lastname"]),
219 $this->dbh
->quoteString($userdata["email"]),
220 $this->dbh
->quoteString($userdata["label"]) );
223 $r = $this->dbh
->simpleQuery($query);
224 if($r == DB_OK
) return true;
227 return $this->set_error(sprintf(_("Database error: %s"),
228 DB
::errorMessage($r)));
232 function remove($alias) {
233 if(!$this->writeable
)
234 return $this->set_error(_("Addressbook is read-only"));
240 $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
241 $this->table
, $this->owner
);
244 while(list($undef, $nickname) = each($alias)) {
245 $query .= sprintf("%s nickname='%s' ", $sepstr,
246 $this->dbh
->quoteString($nickname));
252 $r = $this->dbh
->simpleQuery($query);
253 if($r == DB_OK
) return true;
256 return $this->set_error(sprintf(_("Database error: %s"),
257 DB
::errorMessage($r)));
261 function modify($alias, $userdata) {
262 if(!$this->writeable
)
263 return $this->set_error(_("Addressbook is read-only"));
269 $ret = $this->lookup($alias);
271 return $this->set_error(sprintf(_("User '%s' does not exist"),
275 $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
276 "lastname='%s', email='%s', label='%s' ".
277 "WHERE owner='%s' AND nickname='%s'",
279 $this->dbh
->quoteString($userdata["nickname"]),
280 $this->dbh
->quoteString($userdata["firstname"]),
281 $this->dbh
->quoteString($userdata["lastname"]),
282 $this->dbh
->quoteString($userdata["email"]),
283 $this->dbh
->quoteString($userdata["label"]),
285 $this->dbh
->quoteString($alias) );
288 $r = $this->dbh
->simpleQuery($query);
289 if($r == DB_OK
) return true;
292 return $this->set_error(sprintf(_("Database error: %s"),
293 DB
::errorMessage($r)));
295 } // End of class abook_database