Removing HTML tags from strings
[squirrelmail.git] / functions / abook_database.php
1 <?php
2
3 /**
4 * abook_database.php
5 *
6 * Copyright (c) 1999-2004 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 * @version $Id$
31 * @package squirrelmail
32 * @subpackage addressbook
33 */
34
35 /** Needs the DB functions */
36 if (!include_once('DB.php')) {
37 // same error also in db_prefs.php
38 require_once(SM_PATH . 'functions/display_messages.php');
39 $error = _("Could not include PEAR database functions required for the database backend.") . "<br />\n";
40 $error .= sprintf(_("Is PEAR installed, and is the include path set correctly to find %s?"),
41 '<tt>DB.php</tt>') . "<br />\n";
42 $error .= _("Please contact your system administrator and report this error.");
43 error_box($error, $color);
44 exit;
45 }
46
47 /**
48 * Undocumented class - stores the addressbook in a sql database
49 * @package squirrelmail
50 */
51 class abook_database extends addressbook_backend {
52 var $btype = 'local';
53 var $bname = 'database';
54
55 var $dsn = '';
56 var $table = '';
57 var $owner = '';
58 var $dbh = false;
59
60 var $writeable = true;
61
62 /* ========================== Private ======================= */
63
64 /* Constructor */
65 function abook_database($param) {
66 $this->sname = _("Personal address book");
67
68 if (is_array($param)) {
69 if (empty($param['dsn']) ||
70 empty($param['table']) ||
71 empty($param['owner'])) {
72 return $this->set_error('Invalid parameters');
73 }
74
75 $this->dsn = $param['dsn'];
76 $this->table = $param['table'];
77 $this->owner = $param['owner'];
78
79 if (!empty($param['name'])) {
80 $this->sname = $param['name'];
81 }
82
83 if (isset($param['writeable'])) {
84 $this->writeable = $param['writeable'];
85 }
86
87 if (isset($param['listing'])) {
88 $this->listing = $param['listing'];
89 }
90
91 $this->open(true);
92 }
93 else {
94 return $this->set_error('Invalid argument to constructor');
95 }
96 }
97
98
99 /* Open the database. New connection if $new is true */
100 function open($new = false) {
101 $this->error = '';
102
103 /* Return true is file is open and $new is unset */
104 if ($this->dbh && !$new) {
105 return true;
106 }
107
108 /* Close old file, if any */
109 if ($this->dbh) {
110 $this->close();
111 }
112
113 $dbh = DB::connect($this->dsn, true);
114
115 if (DB::isError($dbh)) {
116 return $this->set_error(sprintf(_("Database error: %s"),
117 DB::errorMessage($dbh)));
118 }
119
120 $this->dbh = $dbh;
121 return true;
122 }
123
124 /* Close the file and forget the filehandle */
125 function close() {
126 $this->dbh->disconnect();
127 $this->dbh = false;
128 }
129
130 /* ========================== Public ======================== */
131
132 /* Search the file */
133 function &search($expr) {
134 $ret = array();
135 if(!$this->open()) {
136 return false;
137 }
138
139 /* To be replaced by advanded search expression parsing */
140 if (is_array($expr)) {
141 return;
142 }
143
144 /* Make regexp from glob'ed expression */
145 $expr = str_replace('?', '_', $expr);
146 $expr = str_replace('*', '%', $expr);
147 $expr = $this->dbh->quoteString($expr);
148 $expr = "%$expr%";
149
150 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
151 "(firstname LIKE '%s' OR lastname LIKE '%s')",
152 $this->table, $this->owner, $expr, $expr);
153 $res = $this->dbh->query($query);
154
155 if (DB::isError($res)) {
156 return $this->set_error(sprintf(_("Database error: %s"),
157 DB::errorMessage($res)));
158 }
159
160 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
161 array_push($ret, array('nickname' => $row['nickname'],
162 'name' => "$row[firstname] $row[lastname]",
163 'firstname' => $row['firstname'],
164 'lastname' => $row['lastname'],
165 'email' => $row['email'],
166 'label' => $row['label'],
167 'backend' => $this->bnum,
168 'source' => &$this->sname));
169 }
170 return $ret;
171 }
172
173 /* Lookup alias */
174 function &lookup($alias) {
175 if (empty($alias)) {
176 return array();
177 }
178
179 $alias = strtolower($alias);
180
181 if (!$this->open()) {
182 return false;
183 }
184
185 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND LOWER(nickname)='%s'",
186 $this->table, $this->owner, $this->dbh->quoteString($alias));
187
188 $res = $this->dbh->query($query);
189
190 if (DB::isError($res)) {
191 return $this->set_error(sprintf(_("Database error: %s"),
192 DB::errorMessage($res)));
193 }
194
195 if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
196 return array('nickname' => $row['nickname'],
197 'name' => "$row[firstname] $row[lastname]",
198 'firstname' => $row['firstname'],
199 'lastname' => $row['lastname'],
200 'email' => $row['email'],
201 'label' => $row['label'],
202 'backend' => $this->bnum,
203 'source' => &$this->sname);
204 }
205 return array();
206 }
207
208 /* List all addresses */
209 function &list_addr() {
210 $ret = array();
211 if (!$this->open()) {
212 return false;
213 }
214
215 if(isset($this->listing) && !$this->listing) {
216 return array();
217 }
218
219
220 $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
221 $this->table, $this->owner);
222
223 $res = $this->dbh->query($query);
224
225 if (DB::isError($res)) {
226 return $this->set_error(sprintf(_("Database error: %s"),
227 DB::errorMessage($res)));
228 }
229
230 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
231 array_push($ret, array('nickname' => $row['nickname'],
232 'name' => "$row[firstname] $row[lastname]",
233 'firstname' => $row['firstname'],
234 'lastname' => $row['lastname'],
235 'email' => $row['email'],
236 'label' => $row['label'],
237 'backend' => $this->bnum,
238 'source' => &$this->sname));
239 }
240 return $ret;
241 }
242
243 /* Add address */
244 function add($userdata) {
245 if (!$this->writeable) {
246 return $this->set_error(_("Addressbook is read-only"));
247 }
248
249 if (!$this->open()) {
250 return false;
251 }
252
253 /* See if user exist already */
254 $ret = $this->lookup($userdata['nickname']);
255 if (!empty($ret)) {
256 return $this->set_error(sprintf(_("User '%s' already exist"),
257 $ret['nickname']));
258 }
259
260 /* Create query */
261 $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
262 "lastname, email, label) VALUES('%s','%s','%s'," .
263 "'%s','%s','%s')",
264 $this->table, $this->owner,
265 $this->dbh->quoteString($userdata['nickname']),
266 $this->dbh->quoteString($userdata['firstname']),
267 $this->dbh->quoteString($userdata['lastname']),
268 $this->dbh->quoteString($userdata['email']),
269 $this->dbh->quoteString($userdata['label']) );
270
271 /* Do the insert */
272 $r = $this->dbh->simpleQuery($query);
273 if ($r == DB_OK) {
274 return true;
275 }
276
277 /* Fail */
278 return $this->set_error(sprintf(_("Database error: %s"),
279 DB::errorMessage($r)));
280 }
281
282 /* Delete address */
283 function remove($alias) {
284 if (!$this->writeable) {
285 return $this->set_error(_("Addressbook is read-only"));
286 }
287
288 if (!$this->open()) {
289 return false;
290 }
291
292 /* Create query */
293 $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
294 $this->table, $this->owner);
295
296 $sepstr = '';
297 while (list($undef, $nickname) = each($alias)) {
298 $query .= sprintf("%s nickname='%s' ", $sepstr,
299 $this->dbh->quoteString($nickname));
300 $sepstr = 'OR';
301 }
302 $query .= ')';
303
304 /* Delete entry */
305 $r = $this->dbh->simpleQuery($query);
306 if ($r == DB_OK) {
307 return true;
308 }
309
310 /* Fail */
311 return $this->set_error(sprintf(_("Database error: %s"),
312 DB::errorMessage($r)));
313 }
314
315 /* Modify address */
316 function modify($alias, $userdata) {
317 if (!$this->writeable) {
318 return $this->set_error(_("Addressbook is read-only"));
319 }
320
321 if (!$this->open()) {
322 return false;
323 }
324
325 /* See if user exist */
326 $ret = $this->lookup($alias);
327 if (empty($ret)) {
328 return $this->set_error(sprintf(_("User '%s' does not exist"),
329 $alias));
330 }
331
332 /* Create query */
333 $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
334 "lastname='%s', email='%s', label='%s' ".
335 "WHERE owner='%s' AND nickname='%s'",
336 $this->table,
337 $this->dbh->quoteString($userdata['nickname']),
338 $this->dbh->quoteString($userdata['firstname']),
339 $this->dbh->quoteString($userdata['lastname']),
340 $this->dbh->quoteString($userdata['email']),
341 $this->dbh->quoteString($userdata['label']),
342 $this->owner,
343 $this->dbh->quoteString($alias) );
344
345 /* Do the insert */
346 $r = $this->dbh->simpleQuery($query);
347 if ($r == DB_OK) {
348 return true;
349 }
350
351 /* Fail */
352 return $this->set_error(sprintf(_("Database error: %s"),
353 DB::errorMessage($r)));
354 }
355 } /* End of class abook_database */
356
357
358 // vim: et ts=4
359 ?>