0a2b09b6af97c119f26192bc5191e701c40f9693
[squirrelmail.git] / functions / abook_database.php
1 <?php
2
3 /**
4 * abook_database.php
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Backend for personal addressbook stored in a database,
10 * accessed using the DB-classes in PEAR.
11 *
12 * IMPORTANT: The PEAR modules must be in the include path
13 * for this class to work.
14 *
15 * An array with the following elements must be passed to
16 * the class constructor (elements marked ? are optional):
17 *
18 * dsn => database DNS (see PEAR for syntax)
19 * table => table to store addresses in (must exist)
20 * owner => current user (owner of address data)
21 * ? writeable => set writeable flag (true/false)
22 *
23 * The table used should have the following columns:
24 * owner, nickname, firstname, lastname, email, label
25 * The pair (owner,nickname) should be unique (primary key).
26 *
27 * NOTE. This class should not be used directly. Use the
28 * "AddressBook" class instead.
29 *
30 * $Id$
31 * @package squirrelmail
32 */
33
34 /** Needs the DB functions */
35 require_once('DB.php');
36
37 /**
38 * Undocumented class - stores the addressbook in a sql database
39 * @package squirrelmail
40 */
41 class abook_database extends addressbook_backend {
42 var $btype = 'local';
43 var $bname = 'database';
44
45 var $dsn = '';
46 var $table = '';
47 var $owner = '';
48 var $dbh = false;
49
50 var $writeable = true;
51
52 /* ========================== Private ======================= */
53
54 /* Constructor */
55 function abook_database($param) {
56 $this->sname = _("Personal address book");
57
58 if (is_array($param)) {
59 if (empty($param['dsn']) ||
60 empty($param['table']) ||
61 empty($param['owner'])) {
62 return $this->set_error('Invalid parameters');
63 }
64
65 $this->dsn = $param['dsn'];
66 $this->table = $param['table'];
67 $this->owner = $param['owner'];
68
69 if (!empty($param['name'])) {
70 $this->sname = $param['name'];
71 }
72
73 if (isset($param['writeable'])) {
74 $this->writeable = $param['writeable'];
75 }
76
77 $this->open(true);
78 }
79 else {
80 return $this->set_error('Invalid argument to constructor');
81 }
82 }
83
84
85 /* Open the database. New connection if $new is true */
86 function open($new = false) {
87 $this->error = '';
88
89 /* Return true is file is open and $new is unset */
90 if ($this->dbh && !$new) {
91 return true;
92 }
93
94 /* Close old file, if any */
95 if ($this->dbh) {
96 $this->close();
97 }
98
99 $dbh = DB::connect($this->dsn, true);
100
101 if (DB::isError($dbh)) {
102 return $this->set_error(sprintf(_("Database error: %s"),
103 DB::errorMessage($dbh)));
104 }
105
106 $this->dbh = $dbh;
107 return true;
108 }
109
110 /* Close the file and forget the filehandle */
111 function close() {
112 $this->dbh->disconnect();
113 $this->dbh = false;
114 }
115
116 /* ========================== Public ======================== */
117
118 /* Search the file */
119 function &search($expr) {
120 $ret = array();
121 if(!$this->open()) {
122 return false;
123 }
124
125 /* To be replaced by advanded search expression parsing */
126 if (is_array($expr)) {
127 return;
128 }
129
130 /* Make regexp from glob'ed expression */
131 $expr = str_replace('?', '_', $expr);
132 $expr = str_replace('*', '%', $expr);
133 $expr = $this->dbh->quoteString($expr);
134 $expr = "%$expr%";
135
136 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
137 "(firstname LIKE '%s' OR lastname LIKE '%s')",
138 $this->table, $this->owner, $expr, $expr);
139 $res = $this->dbh->query($query);
140
141 if (DB::isError($res)) {
142 return $this->set_error(sprintf(_("Database error: %s"),
143 DB::errorMessage($res)));
144 }
145
146 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
147 array_push($ret, array('nickname' => $row['nickname'],
148 'name' => "$row[firstname] $row[lastname]",
149 'firstname' => $row['firstname'],
150 'lastname' => $row['lastname'],
151 'email' => $row['email'],
152 'label' => $row['label'],
153 'backend' => $this->bnum,
154 'source' => &$this->sname));
155 }
156 return $ret;
157 }
158
159 /* Lookup alias */
160 function &lookup($alias) {
161 if (empty($alias)) {
162 return array();
163 }
164
165 $alias = strtolower($alias);
166
167 if (!$this->open()) {
168 return false;
169 }
170
171 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND nickname='%s'",
172 $this->table, $this->owner, $alias);
173
174 $res = $this->dbh->query($query);
175
176 if (DB::isError($res)) {
177 return $this->set_error(sprintf(_("Database error: %s"),
178 DB::errorMessage($res)));
179 }
180
181 if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
182 return array('nickname' => $row['nickname'],
183 'name' => "$row[firstname] $row[lastname]",
184 'firstname' => $row['firstname'],
185 'lastname' => $row['lastname'],
186 'email' => $row['email'],
187 'label' => $row['label'],
188 'backend' => $this->bnum,
189 'source' => &$this->sname);
190 }
191 return array();
192 }
193
194 /* List all addresses */
195 function &list_addr() {
196 $ret = array();
197 if (!$this->open()) {
198 return false;
199 }
200
201 $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
202 $this->table, $this->owner);
203
204 $res = $this->dbh->query($query);
205
206 if (DB::isError($res)) {
207 return $this->set_error(sprintf(_("Database error: %s"),
208 DB::errorMessage($res)));
209 }
210
211 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
212 array_push($ret, array('nickname' => $row['nickname'],
213 'name' => "$row[firstname] $row[lastname]",
214 'firstname' => $row['firstname'],
215 'lastname' => $row['lastname'],
216 'email' => $row['email'],
217 'label' => $row['label'],
218 'backend' => $this->bnum,
219 'source' => &$this->sname));
220 }
221 return $ret;
222 }
223
224 /* Add address */
225 function add($userdata) {
226 if (!$this->writeable) {
227 return $this->set_error(_("Addressbook is read-only"));
228 }
229
230 if (!$this->open()) {
231 return false;
232 }
233
234 /* See if user exist already */
235 $ret = $this->lookup($userdata['nickname']);
236 if (!empty($ret)) {
237 return $this->set_error(sprintf(_("User '%s' already exist"),
238 $ret['nickname']));
239 }
240
241 /* Create query */
242 $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
243 "lastname, email, label) VALUES('%s','%s','%s'," .
244 "'%s','%s','%s')",
245 $this->table, $this->owner,
246 $this->dbh->quoteString($userdata['nickname']),
247 $this->dbh->quoteString($userdata['firstname']),
248 $this->dbh->quoteString($userdata['lastname']),
249 $this->dbh->quoteString($userdata['email']),
250 $this->dbh->quoteString($userdata['label']) );
251
252 /* Do the insert */
253 $r = $this->dbh->simpleQuery($query);
254 if ($r == DB_OK) {
255 return true;
256 }
257
258 /* Fail */
259 return $this->set_error(sprintf(_("Database error: %s"),
260 DB::errorMessage($r)));
261 }
262
263 /* Delete address */
264 function remove($alias) {
265 if (!$this->writeable) {
266 return $this->set_error(_("Addressbook is read-only"));
267 }
268
269 if (!$this->open()) {
270 return false;
271 }
272
273 /* Create query */
274 $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
275 $this->table, $this->owner);
276
277 $sepstr = '';
278 while (list($undef, $nickname) = each($alias)) {
279 $query .= sprintf("%s nickname='%s' ", $sepstr,
280 $this->dbh->quoteString($nickname));
281 $sepstr = 'OR';
282 }
283 $query .= ')';
284
285 /* Delete entry */
286 $r = $this->dbh->simpleQuery($query);
287 if ($r == DB_OK) {
288 return true;
289 }
290
291 /* Fail */
292 return $this->set_error(sprintf(_("Database error: %s"),
293 DB::errorMessage($r)));
294 }
295
296 /* Modify address */
297 function modify($alias, $userdata) {
298 if (!$this->writeable) {
299 return $this->set_error(_("Addressbook is read-only"));
300 }
301
302 if (!$this->open()) {
303 return false;
304 }
305
306 /* See if user exist */
307 $ret = $this->lookup($alias);
308 if (empty($ret)) {
309 return $this->set_error(sprintf(_("User '%s' does not exist"),
310 $alias));
311 }
312
313 /* Create query */
314 $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
315 "lastname='%s', email='%s', label='%s' ".
316 "WHERE owner='%s' AND nickname='%s'",
317 $this->table,
318 $this->dbh->quoteString($userdata['nickname']),
319 $this->dbh->quoteString($userdata['firstname']),
320 $this->dbh->quoteString($userdata['lastname']),
321 $this->dbh->quoteString($userdata['email']),
322 $this->dbh->quoteString($userdata['label']),
323 $this->owner,
324 $this->dbh->quoteString($alias) );
325
326 /* Do the insert */
327 $r = $this->dbh->simpleQuery($query);
328 if ($r == DB_OK) {
329 return true;
330 }
331
332 /* Fail */
333 return $this->set_error(sprintf(_("Database error: %s"),
334 DB::errorMessage($r)));
335 }
336 } /* End of class abook_database */
337
338 ?>