ee1584052eb6186f542d78bebe754b955eab0bf7
5 * Copyright (c) 1999-2005 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * @package squirrelmail
10 * @subpackage addressbook
14 * Backend for address book as a pipe separated file
16 * Stores the address book in a local file
18 * An array with the following elements must be passed to
19 * the class constructor (elements marked ? are optional):
21 * filename => path to addressbook file
22 * ? create => if true: file is created if it does not exist.
23 * ? umask => umask set before opening file.
24 * ? name => name of address book.
25 * ? detect_writeable => detect address book access permissions by
26 * checking file permissions.
27 * ? writeable => allow writing into address book. Used only when
28 * detect_writeable is set to false.
30 * NOTE. This class should not be used directly. Use the
31 * "AddressBook" class instead.
32 * @package squirrelmail
34 class abook_local_file
extends addressbook_backend
{
44 var $bname = 'local_file';
47 * File used to store data
57 * Create file, if it not present
62 * Detect, if address book is writeable by checking file permisions
65 var $detect_writeable = true;
67 * Control write access to address book
69 * Option does not have any effect, if 'detect_writeable' is 'true'
72 var $writeable = false;
79 /* ========================== Private ======================= */
83 * @param array $param backend options
86 function abook_local_file($param) {
87 $this->sname
= _("Personal address book");
88 $this->umask
= Umask();
90 if(is_array($param)) {
91 if(empty($param['filename'])) {
92 return $this->set_error('Invalid parameters');
94 if(!is_string($param['filename'])) {
95 return $this->set_error($param['filename'] . ': '.
96 _("Not a file name"));
99 $this->filename
= $param['filename'];
101 if(isset($param['create'])) {
102 $this->create
= $param['create'];
104 if(isset($param['umask'])) {
105 $this->umask
= $param['umask'];
107 if(isset($param['name'])) {
108 $this->sname
= $param['name'];
110 if(isset($param['detect_writeable'])) {
111 $this->detect_writeable
= $param['detect_writeable'];
113 if(!empty($param['writeable'])) {
114 $this->writeable
= $param['writeable'];
119 $this->set_error('Invalid argument to constructor');
124 * Open the addressbook file and store the file pointer.
125 * Use $file as the file to open, or the class' own
126 * filename property. If $param is empty and file is
128 * @param bool $new is file already opened
131 function open($new = false) {
133 $file = $this->filename
;
134 $create = $this->create
;
135 $fopenmode = ($this->writeable ?
'a+' : 'r');
137 /* Return true is file is open and $new is unset */
138 if($this->filehandle
&& !$new) {
142 /* Check that new file exitsts */
143 if((!(file_exists($file) && is_readable($file))) && !$create) {
144 return $this->set_error("$file: " . _("No such file or directory"));
147 /* Close old file, if any */
148 if($this->filehandle
) { $this->close(); }
151 if (! $this->detect_writeable
) {
152 $fh = @fopen
($file,$fopenmode);
154 $this->filehandle
= &$fh;
155 $this->filename
= $file;
157 return $this->set_error("$file: " . _("Open failed"));
160 /* Open file. First try to open for reading and writing,
161 * but fall back to read only. */
162 $fh = @fopen
($file, 'a+');
164 $this->filehandle
= &$fh;
165 $this->filename
= $file;
166 $this->writeable
= true;
168 $fh = @fopen
($file, 'r');
170 $this->filehandle
= &$fh;
171 $this->filename
= $file;
172 $this->writeable
= false;
174 return $this->set_error("$file: " . _("Open failed"));
181 /** Close the file and forget the filehandle */
183 @fclose
($this->filehandle
);
184 $this->filehandle
= 0;
185 $this->filename
= '';
186 $this->writable
= false;
189 /** Lock the datafile - try 20 times in 5 seconds */
191 for($i = 0 ; $i < 20 ; $i++
) {
192 if(flock($this->filehandle
, 2 +
4))
200 /** Unlock the datafile */
202 return flock($this->filehandle
, 3);
206 * Overwrite the file with data from $rows
207 * NOTE! Previous locks are broken by this function
208 * @param array $rows new data
211 function overwrite(&$rows) {
213 $newfh = @fopen
($this->filename
.'.tmp', 'w');
216 return $this->set_error($this->filename
. '.tmp:' . _("Open failed"));
219 for($i = 0, $cnt=sizeof($rows) ; $i < $cnt ; $i++
) {
220 if(is_array($rows[$i])) {
221 for($j = 0, $cnt_part=count($rows[$i]) ; $j < $cnt_part ; $j++
) {
222 $rows[$i][$j] = $this->quotevalue($rows[$i][$j]);
224 $tmpwrite = sq_fwrite($newfh, join('|', $rows[$i]) . "\n");
225 if ($tmpwrite === FALSE) {
226 return $this->set_error($this->filename
. '.tmp:' . _("Write failed"));
232 if (!@copy
($this->filename
. '.tmp' , $this->filename
)) {
233 return $this->set_error($this->filename
. ':' . _("Unable to update"));
235 @unlink
($this->filename
. '.tmp');
241 /* ========================== Public ======================== */
245 * @param string $expr search expression
246 * @return array search results
248 function search($expr) {
250 /* To be replaced by advanded search expression parsing */
251 if(is_array($expr)) { return; }
253 /* Make regexp from glob'ed expression
254 * May want to quote other special characters like (, ), -, [, ], etc. */
255 $expr = str_replace('?', '.', $expr);
256 $expr = str_replace('*', '.*', $expr);
262 @rewind
($this->filehandle
);
264 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
265 $line = join(' ', $row);
266 if(eregi($expr, $line)) {
267 array_push($res, array('nickname' => $row[0],
268 'name' => $row[1] . ' ' . $row[2],
269 'firstname' => $row[1],
270 'lastname' => $row[2],
273 'backend' => $this->bnum
,
274 'source' => &$this->sname
));
283 * @param string $alias alias
284 * @return array search results
286 function lookup($alias) {
291 $alias = strtolower($alias);
294 @rewind
($this->filehandle
);
296 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
297 if(strtolower($row[0]) == $alias) {
298 return array('nickname' => $row[0],
299 'name' => $row[1] . ' ' . $row[2],
300 'firstname' => $row[1],
301 'lastname' => $row[2],
304 'backend' => $this->bnum
,
305 'source' => &$this->sname
);
314 * @return array list of all addresses
316 function list_addr() {
319 @rewind
($this->filehandle
);
321 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
322 array_push($res, array('nickname' => $row[0],
323 'name' => $row[1] . ' ' . $row[2],
324 'firstname' => $row[1],
325 'lastname' => $row[2],
328 'backend' => $this->bnum
,
329 'source' => &$this->sname
));
336 * @param array $userdata new data
339 function add($userdata) {
340 if(!$this->writeable
) {
341 return $this->set_error(_("Addressbook is read-only"));
343 /* See if user exists already */
344 $ret = $this->lookup($userdata['nickname']);
346 return $this->set_error(sprintf(_("User '%s' already exist"),
350 /* Here is the data to write */
351 $data = $this->quotevalue($userdata['nickname']) . '|' .
352 $this->quotevalue($userdata['firstname']) . '|' .
353 $this->quotevalue($userdata['lastname']) . '|' .
354 $this->quotevalue($userdata['email']) . '|' .
355 $this->quotevalue($userdata['label']);
357 /* Strip linefeeds */
358 $data = ereg_replace("[\r\n]", ' ', $data);
359 /* Add linefeed at end */
360 $data = $data . "\n";
362 /* Reopen file, just to be sure */
364 if(!$this->writeable
) {
365 return $this->set_error(_("Addressbook is read-only"));
370 return $this->set_error(_("Could not lock datafile"));
374 $r = sq_fwrite($this->filehandle
, $data);
379 /* Test write result */
382 $this->set_error(_("Write to addressbook failed"));
391 * @param string $alias alias that has to be deleted
394 function remove($alias) {
395 if(!$this->writeable
) {
396 return $this->set_error(_("Addressbook is read-only"));
399 /* Lock the file to make sure we're the only process working
402 return $this->set_error(_("Could not lock datafile"));
405 /* Read file into memory, ignoring nicknames to delete */
406 @rewind
($this->filehandle
);
409 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
410 if(!in_array($row[0], $alias)) {
415 /* Write data back */
416 if(!$this->overwrite($rows)) {
427 * @param string $alias modified alias
428 * @param array $userdata new data
429 * @return bool true, if operation successful
431 function modify($alias, $userdata) {
432 if(!$this->writeable
) {
433 return $this->set_error(_("Addressbook is read-only"));
436 /* See if user exists */
437 $ret = $this->lookup($alias);
439 return $this->set_error(sprintf(_("User '%s' does not exist"),
443 /* Lock the file to make sure we're the only process working
446 return $this->set_error(_("Could not lock datafile"));
449 /* Read file into memory, modifying the data for the
450 * user identified by $alias */
452 @rewind
($this->filehandle
);
455 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
456 if(strtolower($row[0]) != strtolower($alias)) {
459 $rows[$i++
] = array(0 => $userdata['nickname'],
460 1 => $userdata['firstname'],
461 2 => $userdata['lastname'],
462 3 => $userdata['email'],
463 4 => $userdata['label']);
467 /* Write data back */
468 if(!$this->overwrite($rows)) {
478 * Function for quoting values before saving
479 * @param string $value string that has to be quoted
480 * @param string quoted string
482 function quotevalue($value) {
483 /* Quote the field if it contains | or ". Double quotes need to
484 * be replaced with "" */
485 if(ereg("[|\"]", $value)) {
486 $value = '"' . str_replace('"', '""', $value) . '"';
491 } /* End of class abook_local_file */