1. added non anonymous ldap binding patch. (thanks to Ilyak Kasnacheev <ilyak at...
[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 if (isset($param['listing'])) {
78 $this->listing = $param['listing'];
79 }
80
81 $this->open(true);
82 }
83 else {
84 return $this->set_error('Invalid argument to constructor');
85 }
86 }
87
88
89 /* Open the database. New connection if $new is true */
90 function open($new = false) {
91 $this->error = '';
92
93 /* Return true is file is open and $new is unset */
94 if ($this->dbh && !$new) {
95 return true;
96 }
97
98 /* Close old file, if any */
99 if ($this->dbh) {
100 $this->close();
101 }
102
103 $dbh = DB::connect($this->dsn, true);
104
105 if (DB::isError($dbh)) {
106 return $this->set_error(sprintf(_("Database error: %s"),
107 DB::errorMessage($dbh)));
108 }
109
110 $this->dbh = $dbh;
111 return true;
112 }
113
114 /* Close the file and forget the filehandle */
115 function close() {
116 $this->dbh->disconnect();
117 $this->dbh = false;
118 }
119
120 /* ========================== Public ======================== */
121
122 /* Search the file */
123 function &search($expr) {
124 $ret = array();
125 if(!$this->open()) {
126 return false;
127 }
128
129 /* To be replaced by advanded search expression parsing */
130 if (is_array($expr)) {
131 return;
132 }
133
134 /* Make regexp from glob'ed expression */
135 $expr = str_replace('?', '_', $expr);
136 $expr = str_replace('*', '%', $expr);
137 $expr = $this->dbh->quoteString($expr);
138 $expr = "%$expr%";
139
140 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
141 "(firstname LIKE '%s' OR lastname LIKE '%s')",
142 $this->table, $this->owner, $expr, $expr);
143 $res = $this->dbh->query($query);
144
145 if (DB::isError($res)) {
146 return $this->set_error(sprintf(_("Database error: %s"),
147 DB::errorMessage($res)));
148 }
149
150 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
151 array_push($ret, array('nickname' => $row['nickname'],
152 'name' => "$row[firstname] $row[lastname]",
153 'firstname' => $row['firstname'],
154 'lastname' => $row['lastname'],
155 'email' => $row['email'],
156 'label' => $row['label'],
157 'backend' => $this->bnum,
158 'source' => &$this->sname));
159 }
160 return $ret;
161 }
162
163 /* Lookup alias */
164 function &lookup($alias) {
165 if (empty($alias)) {
166 return array();
167 }
168
169 $alias = strtolower($alias);
170
171 if (!$this->open()) {
172 return false;
173 }
174
175 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND nickname='%s'",
176 $this->table, $this->owner, $alias);
177
178 $res = $this->dbh->query($query);
179
180 if (DB::isError($res)) {
181 return $this->set_error(sprintf(_("Database error: %s"),
182 DB::errorMessage($res)));
183 }
184
185 if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
186 return array('nickname' => $row['nickname'],
187 'name' => "$row[firstname] $row[lastname]",
188 'firstname' => $row['firstname'],
189 'lastname' => $row['lastname'],
190 'email' => $row['email'],
191 'label' => $row['label'],
192 'backend' => $this->bnum,
193 'source' => &$this->sname);
194 }
195 return array();
196 }
197
198 /* List all addresses */
199 function &list_addr() {
200 $ret = array();
201 if (!$this->open()) {
202 return false;
203 }
204
205 if(!$this->listing) {
206 return array();
207 }
208
209
210 $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
211 $this->table, $this->owner);
212
213 $res = $this->dbh->query($query);
214
215 if (DB::isError($res)) {
216 return $this->set_error(sprintf(_("Database error: %s"),
217 DB::errorMessage($res)));
218 }
219
220 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
221 array_push($ret, array('nickname' => $row['nickname'],
222 'name' => "$row[firstname] $row[lastname]",
223 'firstname' => $row['firstname'],
224 'lastname' => $row['lastname'],
225 'email' => $row['email'],
226 'label' => $row['label'],
227 'backend' => $this->bnum,
228 'source' => &$this->sname));
229 }
230 return $ret;
231 }
232
233 /* Add address */
234 function add($userdata) {
235 if (!$this->writeable) {
236 return $this->set_error(_("Addressbook is read-only"));
237 }
238
239 if (!$this->open()) {
240 return false;
241 }
242
243 /* See if user exist already */
244 $ret = $this->lookup($userdata['nickname']);
245 if (!empty($ret)) {
246 return $this->set_error(sprintf(_("User '%s' already exist"),
247 $ret['nickname']));
248 }
249
250 /* Create query */
251 $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
252 "lastname, email, label) VALUES('%s','%s','%s'," .
253 "'%s','%s','%s')",
254 $this->table, $this->owner,
255 $this->dbh->quoteString($userdata['nickname']),
256 $this->dbh->quoteString($userdata['firstname']),
257 $this->dbh->quoteString($userdata['lastname']),
258 $this->dbh->quoteString($userdata['email']),
259 $this->dbh->quoteString($userdata['label']) );
260
261 /* Do the insert */
262 $r = $this->dbh->simpleQuery($query);
263 if ($r == DB_OK) {
264 return true;
265 }
266
267 /* Fail */
268 return $this->set_error(sprintf(_("Database error: %s"),
269 DB::errorMessage($r)));
270 }
271
272 /* Delete address */
273 function remove($alias) {
274 if (!$this->writeable) {
275 return $this->set_error(_("Addressbook is read-only"));
276 }
277
278 if (!$this->open()) {
279 return false;
280 }
281
282 /* Create query */
283 $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
284 $this->table, $this->owner);
285
286 $sepstr = '';
287 while (list($undef, $nickname) = each($alias)) {
288 $query .= sprintf("%s nickname='%s' ", $sepstr,
289 $this->dbh->quoteString($nickname));
290 $sepstr = 'OR';
291 }
292 $query .= ')';
293
294 /* Delete entry */
295 $r = $this->dbh->simpleQuery($query);
296 if ($r == DB_OK) {
297 return true;
298 }
299
300 /* Fail */
301 return $this->set_error(sprintf(_("Database error: %s"),
302 DB::errorMessage($r)));
303 }
304
305 /* Modify address */
306 function modify($alias, $userdata) {
307 if (!$this->writeable) {
308 return $this->set_error(_("Addressbook is read-only"));
309 }
310
311 if (!$this->open()) {
312 return false;
313 }
314
315 /* See if user exist */
316 $ret = $this->lookup($alias);
317 if (empty($ret)) {
318 return $this->set_error(sprintf(_("User '%s' does not exist"),
319 $alias));
320 }
321
322 /* Create query */
323 $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
324 "lastname='%s', email='%s', label='%s' ".
325 "WHERE owner='%s' AND nickname='%s'",
326 $this->table,
327 $this->dbh->quoteString($userdata['nickname']),
328 $this->dbh->quoteString($userdata['firstname']),
329 $this->dbh->quoteString($userdata['lastname']),
330 $this->dbh->quoteString($userdata['email']),
331 $this->dbh->quoteString($userdata['label']),
332 $this->owner,
333 $this->dbh->quoteString($alias) );
334
335 /* Do the insert */
336 $r = $this->dbh->simpleQuery($query);
337 if ($r == DB_OK) {
338 return true;
339 }
340
341 /* Fail */
342 return $this->set_error(sprintf(_("Database error: %s"),
343 DB::errorMessage($r)));
344 }
345 } /* End of class abook_database */
346
347 ?>