Warning removal.
[squirrelmail.git] / functions / abook_database.php
... / ...
CommitLineData
1<?php {
2
3 /**
4 ** abook_database.php
5 **
6 ** Copyright (c) 1999-2001 The Squirrelmail Development 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 **/
32
33 require_once('DB.php');
34
35 class abook_database extends addressbook_backend {
36 var $btype = 'local';
37 var $bname = 'database';
38
39 var $dsn = '';
40 var $table = '';
41 var $owner = '';
42 var $dbh = false;
43
44 var $writeable = true;
45
46 // ========================== Private =======================
47
48 // Constructor
49 function abook_database($param) {
50 $this->sname = _("Personal address book");
51
52 if(is_array($param)) {
53 if(empty($param['dsn']) ||
54 empty($param['table']) ||
55 empty($param['owner']))
56 return $this->set_error('Invalid parameters');
57
58 $this->dsn = $param['dsn'];
59 $this->table = $param['table'];
60 $this->owner = $param['owner'];
61
62 if(!empty($param['name']))
63 $this->sname = $param['name'];
64
65 if(isset($param['writeable']))
66 $this->writeable = $param['writeable'];
67
68 $this->open(true);
69 } else {
70 return $this->set_error('Invalid argument to constructor');
71 }
72 }
73
74
75 // Open the database. New connection if $new is true
76 function open($new = false) {
77 $this->error = '';
78
79 // Return true is file is open and $new is unset
80 if($this->dbh && !$new)
81 return true;
82
83 // Close old file, if any
84 if($this->dbh) $this->close();
85
86 $dbh = DB::connect($this->dsn, true);
87
88 if(DB::isError($dbh) || DB::isWarning($dbh))
89 return $this->set_error(sprintf(_("Database error: %s"),
90 DB::errorMessage($dbh)));
91
92 $this->dbh = $dbh;
93 return true;
94 }
95
96 // Close the file and forget the filehandle
97 function close() {
98 $this->dbh->disconnect();
99 $this->dbh = false;
100 }
101
102 // ========================== Public ========================
103
104 // Search the file
105 function &search($expr) {
106 $ret = array();
107 if(!$this->open())
108 return false;
109
110 // To be replaced by advanded search expression parsing
111 if(is_array($expr)) return;
112
113 // Make regexp from glob'ed expression
114 $expr = str_replace('?', '_', $expr);
115 $expr = str_replace('*', '%', $expr);
116 $expr = $this->dbh->quoteString($expr);
117 $expr = "%$expr%";
118
119 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
120 "(firstname LIKE '%s' OR lastname LIKE '%s')",
121 $this->table, $this->owner, $expr, $expr);
122 $res = $this->dbh->query($query);
123
124 if(DB::isError($res))
125 return $this->set_error(sprintf(_("Database error: %s"),
126 DB::errorMessage($res)));
127
128 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
129 array_push($ret, array('nickname' => $row['nickname'],
130 'name' => "$row[firstname] $row[lastname]",
131 'firstname' => $row['firstname'],
132 'lastname' => $row['lastname'],
133 'email' => $row['email'],
134 'label' => $row['label'],
135 'backend' => $this->bnum,
136 'source' => &$this->sname));
137 }
138 return $ret;
139 }
140
141 // Lookup alias
142 function &lookup($alias) {
143 if(empty($alias))
144 return array();
145
146 $alias = strtolower($alias);
147
148 if(!$this->open())
149 return false;
150
151 $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND nickname='%s'",
152 $this->table, $this->owner, $alias);
153
154 $res = $this->dbh->query($query);
155
156 if(DB::isError($res))
157 return $this->set_error(sprintf(_("Database error: %s"),
158 DB::errorMessage($res)));
159
160 if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
161 return 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
171 return array();
172 }
173
174 // List all addresses
175 function &list_addr() {
176 $ret = array();
177 if(!$this->open())
178 return false;
179
180 $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
181 $this->table, $this->owner);
182
183 $res = $this->dbh->query($query);
184
185 if(DB::isError($res))
186 return $this->set_error(sprintf(_("Database error: %s"),
187 DB::errorMessage($res)));
188
189 while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
190 array_push($ret, array('nickname' => $row['nickname'],
191 'name' => "$row[firstname] $row[lastname]",
192 'firstname' => $row['firstname'],
193 'lastname' => $row['lastname'],
194 'email' => $row['email'],
195 'label' => $row['label'],
196 'backend' => $this->bnum,
197 'source' => &$this->sname));
198 }
199 return $ret;
200 }
201
202 // Add address
203 function add($userdata) {
204 if(!$this->writeable)
205 return $this->set_error(_("Addressbook is read-only"));
206
207 if(!$this->open())
208 return false;
209
210 // See if user exist already
211 $ret = $this->lookup($userdata['nickname']);
212 if(!empty($ret))
213 return $this->set_error(sprintf(_("User '%s' already exist"),
214 $ret['nickname']));
215
216 // Create query
217 $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
218 "lastname, email, label) VALUES('%s','%s','%s'," .
219 "'%s','%s','%s')",
220 $this->table, $this->owner,
221 $this->dbh->quoteString($userdata['nickname']),
222 $this->dbh->quoteString($userdata['firstname']),
223 $this->dbh->quoteString($userdata['lastname']),
224 $this->dbh->quoteString($userdata['email']),
225 $this->dbh->quoteString($userdata['label']) );
226
227 // Do the insert
228 $r = $this->dbh->simpleQuery($query);
229 if($r == DB_OK) return true;
230
231 // Fail
232 return $this->set_error(sprintf(_("Database error: %s"),
233 DB::errorMessage($r)));
234 }
235
236 // Delete address
237 function remove($alias) {
238 if(!$this->writeable)
239 return $this->set_error(_("Addressbook is read-only"));
240
241 if(!$this->open())
242 return false;
243
244 // Create query
245 $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
246 $this->table, $this->owner);
247
248 $sepstr = '';
249 while(list($undef, $nickname) = each($alias)) {
250 $query .= sprintf("%s nickname='%s' ", $sepstr,
251 $this->dbh->quoteString($nickname));
252 $sepstr = 'OR';
253 }
254 $query .= ')';
255
256 // Delete entry
257 $r = $this->dbh->simpleQuery($query);
258 if($r == DB_OK) return true;
259
260 // Fail
261 return $this->set_error(sprintf(_("Database error: %s"),
262 DB::errorMessage($r)));
263 }
264
265 // Modify address
266 function modify($alias, $userdata) {
267 if(!$this->writeable)
268 return $this->set_error(_("Addressbook is read-only"));
269
270 if(!$this->open())
271 return false;
272
273 // See if user exist
274 $ret = $this->lookup($alias);
275 if(empty($ret))
276 return $this->set_error(sprintf(_("User '%s' does not exist"),
277 $alias));
278
279 // Create query
280 $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
281 "lastname='%s', email='%s', label='%s' ".
282 "WHERE owner='%s' AND nickname='%s'",
283 $this->table,
284 $this->dbh->quoteString($userdata['nickname']),
285 $this->dbh->quoteString($userdata['firstname']),
286 $this->dbh->quoteString($userdata['lastname']),
287 $this->dbh->quoteString($userdata['email']),
288 $this->dbh->quoteString($userdata['label']),
289 $this->owner,
290 $this->dbh->quoteString($alias) );
291
292 // Do the insert
293 $r = $this->dbh->simpleQuery($query);
294 if($r == DB_OK) return true;
295
296 // Fail
297 return $this->set_error(sprintf(_("Database error: %s"),
298 DB::errorMessage($r)));
299 }
300 } // End of class abook_database
301
302} ?>