bugfix
[squirrelmail.git] / functions / abook_local_file.php
CommitLineData
5100704d 1<?php
2
35586184 3/**
4 * abook_local_file.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Backend for addressbook as a pipe separated file
10 *
11 * An array with the following elements must be passed to
12 * the class constructor (elements marked ? are optional):
13 *
14 * filename => path to addressbook file
15 * ? create => if true: file is created if it does not exist.
16 * ? umask => umask set before opening file.
17 *
18 * NOTE. This class should not be used directly. Use the
19 * "AddressBook" class instead.
20 *
21 * $Id$
22 */
5100704d 23
35586184 24class abook_local_file extends addressbook_backend {
06b4facd 25 var $btype = 'local';
26 var $bname = 'local_file';
27
28 var $filename = '';
29 var $filehandle = 0;
30 var $create = false;
31 var $umask;
32
33 /* ========================== Private ======================= */
34
35 /* Constructor */
36 function abook_local_file($param) {
37 $this->sname = _("Personal address book");
38 $this->umask = Umask();
39
40 if(is_array($param)) {
41 if(empty($param['filename'])) {
42 return $this->set_error('Invalid parameters');
43 }
44 if(!is_string($param['filename'])) {
45 return $this->set_error($param['filename'] . ': '.
46 _("Not a file name"));
47 }
48
49 $this->filename = $param['filename'];
50
51 if($param['create']) {
52 $this->create = true;
53 }
54 if(isset($param['umask'])) {
55 $this->umask = $param['umask'];
56 }
57 if(!empty($param['name'])) {
58 $this->sname = $param['name'];
59 }
60
61 $this->open(true);
62 } else {
63 $this->set_error('Invalid argument to constructor');
64 }
65 }
66
67 /* Open the addressbook file and store the file pointer.
68 * Use $file as the file to open, or the class' own
69 * filename property. If $param is empty and file is
70 * open, do nothing. */
71 function open($new = false) {
72 $this->error = '';
73 $file = $this->filename;
74 $create = $this->create;
75
76 /* Return true is file is open and $new is unset */
77 if($this->filehandle && !$new) {
78 return true;
79 }
80
81 /* Check that new file exitsts */
82 if((!(file_exists($file) && is_readable($file))) && !$create) {
83 return $this->set_error("$file: " . _("No such file or directory"));
84 }
85
86 /* Close old file, if any */
87 if($this->filehandle) { $this->close(); }
88
89 /* Open file. First try to open for reading and writing,
90 * but fall back to read only. */
91 umask($this->umask);
92 $fh = @fopen($file, 'a+');
93 if($fh) {
94 $this->filehandle = &$fh;
95 $this->filename = $file;
96 $this->writeable = true;
97 } else {
98 $fh = @fopen($file, 'r');
99 if($fh) {
100 $this->filehandle = &$fh;
101 $this->filename = $file;
102 $this->writeable = false;
103 } else {
104 return $this->set_error("$file: " . _("Open failed"));
105 }
106 }
107 return true;
108 }
109
110 /* Close the file and forget the filehandle */
111 function close() {
112 @fclose($this->filehandle);
113 $this->filehandle = 0;
114 $this->filename = '';
115 $this->writable = false;
116 }
117
118 /* Lock the datafile - try 20 times in 5 seconds */
119 function lock() {
120 for($i = 0 ; $i < 20 ; $i++) {
121 if(flock($this->filehandle, 2 + 4))
122 return true;
123 else
124 usleep(250000);
125 }
126 return false;
127 }
128
129 /* Lock the datafile */
130 function unlock() {
131 return flock($this->filehandle, 3);
132 }
133
134 /* Overwrite the file with data from $rows
135 * NOTE! Previous locks are broken by this function */
136 function overwrite(&$rows) {
01265fba 137 $this->unlock();
06b4facd 138 $newfh = @fopen($this->filename, 'w');
139 if(!$newfh) {
140 return $this->set_error("$file: " . _("Open failed"));
141 }
142
143 for($i = 0 ; $i < sizeof($rows) ; $i++) {
144 if(is_array($rows[$i])) {
77ec28e9 145 for($j = 0 ; $j < count($rows[$i]) ; $j++) {
146 $rows[$i][$j] = $this->quotevalue($rows[$i][$j]);
147 }
06b4facd 148 fwrite($newfh, join('|', $rows[$i]) . "\n");
149 }
150 }
151
152 fclose($newfh);
153 $this->unlock();
154 $this->open(true);
155 return true;
156 }
157
158 /* ========================== Public ======================== */
159
160 /* Search the file */
161 function search($expr) {
162
163 /* To be replaced by advanded search expression parsing */
164 if(is_array($expr)) { return; }
165
166 /* Make regexp from glob'ed expression
167 * May want to quote other special characters like (, ), -, [, ], etc. */
168 $expr = str_replace('?', '.', $expr);
169 $expr = str_replace('*', '.*', $expr);
170
171 $res = array();
172 if(!$this->open()) {
173 return false;
174 }
175 @rewind($this->filehandle);
176
177 while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
178 $line = join(' ', $row);
179 if(eregi($expr, $line)) {
180 array_push($res, array('nickname' => $row[0],
181 'name' => $row[1] . ' ' . $row[2],
182 'firstname' => $row[1],
183 'lastname' => $row[2],
184 'email' => $row[3],
185 'label' => $row[4],
186 'backend' => $this->bnum,
187 'source' => &$this->sname));
188 }
189 }
190
191 return $res;
192 }
193
194 /* Lookup alias */
195 function lookup($alias) {
196 if(empty($alias)) {
197 return array();
198 }
199
200 $alias = strtolower($alias);
201
202 $this->open();
203 @rewind($this->filehandle);
204
205 while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
206 if(strtolower($row[0]) == $alias) {
207 return array('nickname' => $row[0],
208 'name' => $row[1] . ' ' . $row[2],
209 'firstname' => $row[1],
210 'lastname' => $row[2],
211 'email' => $row[3],
212 'label' => $row[4],
213 'backend' => $this->bnum,
214 'source' => &$this->sname);
215 }
216 }
217
218 return array();
219 }
220
221 /* List all addresses */
222 function list_addr() {
223 $res = array();
224 $this->open();
225 @rewind($this->filehandle);
226
227 while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
228 array_push($res, array('nickname' => $row[0],
229 'name' => $row[1] . ' ' . $row[2],
230 'firstname' => $row[1],
231 'lastname' => $row[2],
232 'email' => $row[3],
233 'label' => $row[4],
234 'backend' => $this->bnum,
235 'source' => &$this->sname));
236 }
237 return $res;
238 }
239
240 /* Add address */
241 function add($userdata) {
242 if(!$this->writeable) {
243 return $this->set_error(_("Addressbook is read-only"));
244 }
245 /* See if user exists already */
246 $ret = $this->lookup($userdata['nickname']);
247 if(!empty($ret)) {
248 return $this->set_error(sprintf(_("User '%s' already exist"),
249 $ret['nickname']));
250 }
251
252 /* Here is the data to write */
77ec28e9 253 $data = $this->quotevalue($userdata['nickname']) . '|' .
254 $this->quotevalue($userdata['firstname']) . '|' .
255 $this->quotevalue($userdata['lastname']) . '|' .
256 $this->quotevalue($userdata['email']) . '|' .
257 $this->quotevalue($userdata['label']);
258
06b4facd 259 /* Strip linefeeds */
260 $data = ereg_replace("[\r\n]", ' ', $data);
261 /* Add linefeed at end */
262 $data = $data . "\n";
263
264 /* Reopen file, just to be sure */
265 $this->open(true);
266 if(!$this->writeable) {
267 return $this->set_error(_("Addressbook is read-only"));
268 }
269
270 /* Lock the file */
271 if(!$this->lock()) {
272 return $this->set_error(_("Could not lock datafile"));
273 }
274
275 /* Write */
276 $r = fwrite($this->filehandle, $data);
277
278 /* Unlock file */
279 $this->unlock();
280
281 /* Test write result and exit if OK */
282 if($r > 0) return true;
283
284 /* Fail */
285 $this->set_error(_("Write to addressbook failed"));
286 return false;
287 }
288
289 /* Delete address */
290 function remove($alias) {
291 if(!$this->writeable) {
292 return $this->set_error(_("Addressbook is read-only"));
293 }
294
295 /* Lock the file to make sure we're the only process working
296 * on it. */
297 if(!$this->lock()) {
298 return $this->set_error(_("Could not lock datafile"));
299 }
300
301 /* Read file into memory, ignoring nicknames to delete */
302 @rewind($this->filehandle);
303 $i = 0;
304 $rows = array();
305 while($row = @fgetcsv($this->filehandle, 2048, '|')) {
306 if(!in_array($row[0], $alias)) {
307 $rows[$i++] = $row;
308 }
309 }
310
311 /* Write data back */
312 if(!$this->overwrite($rows)) {
313 $this->unlock();
314 return false;
315 }
316
317 $this->unlock();
318 return true;
319 }
320
321 /* Modify address */
322 function modify($alias, $userdata) {
323 if(!$this->writeable) {
324 return $this->set_error(_("Addressbook is read-only"));
325 }
326
327 /* See if user exists */
328 $ret = $this->lookup($alias);
329 if(empty($ret)) {
330 return $this->set_error(sprintf(_("User '%s' does not exist"),
331 $alias));
332 }
333
334 /* Lock the file to make sure we're the only process working
335 * on it. */
336 if(!$this->lock()) {
337 return $this->set_error(_("Could not lock datafile"));
338 }
339
340 /* Read file into memory, modifying the data for the
341 * user identified by $alias */
342 $this->open(true);
343 @rewind($this->filehandle);
344 $i = 0;
345 $rows = array();
346 while($row = @fgetcsv($this->filehandle, 2048, '|')) {
347 if(strtolower($row[0]) != strtolower($alias)) {
348 $rows[$i++] = $row;
349 } else {
350 $rows[$i++] = array(0 => $userdata['nickname'],
351 1 => $userdata['firstname'],
352 2 => $userdata['lastname'],
353 3 => $userdata['email'],
354 4 => $userdata['label']);
355 }
356 }
357
358 /* Write data back */
359 if(!$this->overwrite($rows)) {
360 $this->unlock();
361 return false;
362 }
363
364 $this->unlock();
365 return true;
366 }
d50a8c48 367
77ec28e9 368 /* Function for quoting values before saving */
369 function quotevalue($value) {
370 /* Quote the field if it contains | or ". Double quotes need to
371 * be replaced with "" */
372 if(ereg("[|\"]", $value)) {
373 $value = '"' . str_replace('"', '""', $value) . '"';
374 }
375 return $value;
376 }
377
06b4facd 378} /* End of class abook_local_file */
379?>