9b9474d6 |
1 | <?php |
7902aca2 |
2 | |
35586184 |
3 | /** |
4 | * abook_database.php |
5 | * |
82d304a0 |
6 | * Copyright (c) 1999-2004 The SquirrelMail Project Team |
35586184 |
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$ |
d6c32258 |
31 | * @package squirrelmail |
35586184 |
32 | */ |
d6c32258 |
33 | |
34 | /** Needs the DB functions */ |
35586184 |
35 | require_once('DB.php'); |
d6c32258 |
36 | |
37 | /** |
38 | * Undocumented class - stores the addressbook in a sql database |
39 | * @package squirrelmail |
40 | */ |
9b9474d6 |
41 | class abook_database extends addressbook_backend { |
42 | var $btype = 'local'; |
43 | var $bname = 'database'; |
7902aca2 |
44 | |
9b9474d6 |
45 | var $dsn = ''; |
46 | var $table = ''; |
47 | var $owner = ''; |
48 | var $dbh = false; |
7902aca2 |
49 | |
9b9474d6 |
50 | var $writeable = true; |
7902aca2 |
51 | |
9b9474d6 |
52 | /* ========================== Private ======================= */ |
7902aca2 |
53 | |
9b9474d6 |
54 | /* Constructor */ |
55 | function abook_database($param) { |
56 | $this->sname = _("Personal address book"); |
7902aca2 |
57 | |
9b9474d6 |
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 | } |
7902aca2 |
64 | |
91be2362 |
65 | $this->dsn = $param['dsn']; |
66 | $this->table = $param['table']; |
67 | $this->owner = $param['owner']; |
7902aca2 |
68 | |
9b9474d6 |
69 | if (!empty($param['name'])) { |
91be2362 |
70 | $this->sname = $param['name']; |
9b9474d6 |
71 | } |
7902aca2 |
72 | |
9b9474d6 |
73 | if (isset($param['writeable'])) { |
91be2362 |
74 | $this->writeable = $param['writeable']; |
9b9474d6 |
75 | } |
7902aca2 |
76 | |
30e9932c |
77 | if (isset($param['listing'])) { |
78 | $this->listing = $param['listing']; |
79 | } |
80 | |
7902aca2 |
81 | $this->open(true); |
9b9474d6 |
82 | } |
83 | else { |
91be2362 |
84 | return $this->set_error('Invalid argument to constructor'); |
9b9474d6 |
85 | } |
86 | } |
7902aca2 |
87 | |
88 | |
9b9474d6 |
89 | /* Open the database. New connection if $new is true */ |
90 | function open($new = false) { |
91 | $this->error = ''; |
7902aca2 |
92 | |
9b9474d6 |
93 | /* Return true is file is open and $new is unset */ |
94 | if ($this->dbh && !$new) { |
7902aca2 |
95 | return true; |
9b9474d6 |
96 | } |
7902aca2 |
97 | |
9b9474d6 |
98 | /* Close old file, if any */ |
99 | if ($this->dbh) { |
100 | $this->close(); |
101 | } |
7902aca2 |
102 | |
9b9474d6 |
103 | $dbh = DB::connect($this->dsn, true); |
7902aca2 |
104 | |
286fe80b |
105 | if (DB::isError($dbh)) { |
701c9c6b |
106 | return $this->set_error(sprintf(_("Database error: %s"), |
7902aca2 |
107 | DB::errorMessage($dbh))); |
9b9474d6 |
108 | } |
7902aca2 |
109 | |
9b9474d6 |
110 | $this->dbh = $dbh; |
111 | return true; |
112 | } |
7902aca2 |
113 | |
9b9474d6 |
114 | /* Close the file and forget the filehandle */ |
115 | function close() { |
116 | $this->dbh->disconnect(); |
117 | $this->dbh = false; |
118 | } |
7902aca2 |
119 | |
9b9474d6 |
120 | /* ========================== Public ======================== */ |
7902aca2 |
121 | |
9b9474d6 |
122 | /* Search the file */ |
123 | function &search($expr) { |
124 | $ret = array(); |
125 | if(!$this->open()) { |
7902aca2 |
126 | return false; |
9b9474d6 |
127 | } |
30e9932c |
128 | |
9b9474d6 |
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)) { |
701c9c6b |
146 | return $this->set_error(sprintf(_("Database error: %s"), |
7902aca2 |
147 | DB::errorMessage($res))); |
9b9474d6 |
148 | } |
7902aca2 |
149 | |
9b9474d6 |
150 | while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { |
91be2362 |
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)); |
9b9474d6 |
159 | } |
160 | return $ret; |
161 | } |
7902aca2 |
162 | |
9b9474d6 |
163 | /* Lookup alias */ |
164 | function &lookup($alias) { |
165 | if (empty($alias)) { |
7902aca2 |
166 | return array(); |
9b9474d6 |
167 | } |
7902aca2 |
168 | |
9b9474d6 |
169 | $alias = strtolower($alias); |
7902aca2 |
170 | |
9b9474d6 |
171 | if (!$this->open()) { |
7902aca2 |
172 | return false; |
9b9474d6 |
173 | } |
7902aca2 |
174 | |
9b9474d6 |
175 | $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND nickname='%s'", |
176 | $this->table, $this->owner, $alias); |
7902aca2 |
177 | |
9b9474d6 |
178 | $res = $this->dbh->query($query); |
7902aca2 |
179 | |
9b9474d6 |
180 | if (DB::isError($res)) { |
701c9c6b |
181 | return $this->set_error(sprintf(_("Database error: %s"), |
7902aca2 |
182 | DB::errorMessage($res))); |
9b9474d6 |
183 | } |
7902aca2 |
184 | |
9b9474d6 |
185 | if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { |
91be2362 |
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); |
9b9474d6 |
194 | } |
195 | return array(); |
196 | } |
197 | |
198 | /* List all addresses */ |
199 | function &list_addr() { |
200 | $ret = array(); |
201 | if (!$this->open()) { |
7902aca2 |
202 | return false; |
9b9474d6 |
203 | } |
30e9932c |
204 | |
168c8dd0 |
205 | if(isset($this->listing) && !$this->listing) { |
30e9932c |
206 | return array(); |
207 | } |
208 | |
7902aca2 |
209 | |
9b9474d6 |
210 | $query = sprintf("SELECT * FROM %s WHERE owner='%s'", |
211 | $this->table, $this->owner); |
7902aca2 |
212 | |
9b9474d6 |
213 | $res = $this->dbh->query($query); |
214 | |
215 | if (DB::isError($res)) { |
701c9c6b |
216 | return $this->set_error(sprintf(_("Database error: %s"), |
7902aca2 |
217 | DB::errorMessage($res))); |
9b9474d6 |
218 | } |
7902aca2 |
219 | |
9b9474d6 |
220 | while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { |
91be2362 |
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)); |
9b9474d6 |
229 | } |
230 | return $ret; |
231 | } |
7902aca2 |
232 | |
9b9474d6 |
233 | /* Add address */ |
234 | function add($userdata) { |
235 | if (!$this->writeable) { |
701c9c6b |
236 | return $this->set_error(_("Addressbook is read-only")); |
9b9474d6 |
237 | } |
7902aca2 |
238 | |
9b9474d6 |
239 | if (!$this->open()) { |
7902aca2 |
240 | return false; |
9b9474d6 |
241 | } |
7902aca2 |
242 | |
9b9474d6 |
243 | /* See if user exist already */ |
244 | $ret = $this->lookup($userdata['nickname']); |
245 | if (!empty($ret)) { |
b9bfd165 |
246 | return $this->set_error(sprintf(_("User '%s' already exist"), |
91be2362 |
247 | $ret['nickname'])); |
9b9474d6 |
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 */ |
7902aca2 |
262 | $r = $this->dbh->simpleQuery($query); |
9b9474d6 |
263 | if ($r == DB_OK) { |
264 | return true; |
265 | } |
7902aca2 |
266 | |
9b9474d6 |
267 | /* Fail */ |
701c9c6b |
268 | return $this->set_error(sprintf(_("Database error: %s"), |
7902aca2 |
269 | DB::errorMessage($r))); |
9b9474d6 |
270 | } |
7902aca2 |
271 | |
9b9474d6 |
272 | /* Delete address */ |
273 | function remove($alias) { |
274 | if (!$this->writeable) { |
701c9c6b |
275 | return $this->set_error(_("Addressbook is read-only")); |
9b9474d6 |
276 | } |
7902aca2 |
277 | |
9b9474d6 |
278 | if (!$this->open()) { |
7902aca2 |
279 | return false; |
9b9474d6 |
280 | } |
7902aca2 |
281 | |
9b9474d6 |
282 | /* Create query */ |
283 | $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (", |
284 | $this->table, $this->owner); |
7902aca2 |
285 | |
9b9474d6 |
286 | $sepstr = ''; |
287 | while (list($undef, $nickname) = each($alias)) { |
16a973d7 |
288 | $query .= sprintf("%s nickname='%s' ", $sepstr, |
7902aca2 |
289 | $this->dbh->quoteString($nickname)); |
91be2362 |
290 | $sepstr = 'OR'; |
9b9474d6 |
291 | } |
292 | $query .= ')'; |
7902aca2 |
293 | |
9b9474d6 |
294 | /* Delete entry */ |
295 | $r = $this->dbh->simpleQuery($query); |
296 | if ($r == DB_OK) { |
297 | return true; |
298 | } |
7902aca2 |
299 | |
9b9474d6 |
300 | /* Fail */ |
301 | return $this->set_error(sprintf(_("Database error: %s"), |
7902aca2 |
302 | DB::errorMessage($r))); |
9b9474d6 |
303 | } |
7902aca2 |
304 | |
9b9474d6 |
305 | /* Modify address */ |
306 | function modify($alias, $userdata) { |
307 | if (!$this->writeable) { |
701c9c6b |
308 | return $this->set_error(_("Addressbook is read-only")); |
9b9474d6 |
309 | } |
7902aca2 |
310 | |
9b9474d6 |
311 | if (!$this->open()) { |
7902aca2 |
312 | return false; |
9b9474d6 |
313 | } |
7902aca2 |
314 | |
9b9474d6 |
315 | /* See if user exist */ |
316 | $ret = $this->lookup($alias); |
317 | if (empty($ret)) { |
701c9c6b |
318 | return $this->set_error(sprintf(_("User '%s' does not exist"), |
7902aca2 |
319 | $alias)); |
9b9474d6 |
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 | } |
7902aca2 |
340 | |
9b9474d6 |
341 | /* Fail */ |
342 | return $this->set_error(sprintf(_("Database error: %s"), |
343 | DB::errorMessage($r))); |
344 | } |
345 | } /* End of class abook_database */ |
7902aca2 |
346 | |
9b9474d6 |
347 | ?> |