5 * Copyright (c) 1999-2004 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
26 * NOTE. This class should not be used directly. Use the
27 * "AddressBook" class instead.
28 * @package squirrelmail
30 class abook_local_file
extends addressbook_backend
{
40 var $bname = 'local_file';
43 * File used to store data
53 * Create file, if it not present
58 * Detect, if address book is writeable by checking file permisions
61 var $detect_writeable = true;
63 * Control write access to address book
65 * Option does not have any effect, if 'detect_writeable' is 'true'
68 var $writeable = false;
75 /* ========================== Private ======================= */
79 * @param array $param backend options
82 function abook_local_file($param) {
83 $this->sname
= _("Personal address book");
84 $this->umask
= Umask();
86 if(is_array($param)) {
87 if(empty($param['filename'])) {
88 return $this->set_error('Invalid parameters');
90 if(!is_string($param['filename'])) {
91 return $this->set_error($param['filename'] . ': '.
92 _("Not a file name"));
95 $this->filename
= $param['filename'];
97 if(isset($param['create'])) {
98 $this->create
= $param['create'];
100 if(isset($param['umask'])) {
101 $this->umask
= $param['umask'];
103 if(isset($param['name'])) {
104 $this->sname
= $param['name'];
106 if(isset($param['detect_writeable'])) {
107 $this->detect_writeable
= $param['detect_writeable'];
109 if(!empty($param['writeable'])) {
110 $this->writeable
= $param['writeable'];
115 $this->set_error('Invalid argument to constructor');
120 * Open the addressbook file and store the file pointer.
121 * Use $file as the file to open, or the class' own
122 * filename property. If $param is empty and file is
124 * @param bool $new is file already opened
127 function open($new = false) {
129 $file = $this->filename
;
130 $create = $this->create
;
131 $fopenmode = ($this->writeable ?
'a+' : 'r');
133 /* Return true is file is open and $new is unset */
134 if($this->filehandle
&& !$new) {
138 /* Check that new file exitsts */
139 if((!(file_exists($file) && is_readable($file))) && !$create) {
140 return $this->set_error("$file: " . _("No such file or directory"));
143 /* Close old file, if any */
144 if($this->filehandle
) { $this->close(); }
147 if (! $this->detect_writeable
) {
148 $fh = @fopen
($file,$fopenmode);
150 $this->filehandle
= &$fh;
151 $this->filename
= $file;
153 return $this->set_error("$file: " . _("Open failed"));
156 /* Open file. First try to open for reading and writing,
157 * but fall back to read only. */
158 $fh = @fopen
($file, 'a+');
160 $this->filehandle
= &$fh;
161 $this->filename
= $file;
162 $this->writeable
= true;
164 $fh = @fopen
($file, 'r');
166 $this->filehandle
= &$fh;
167 $this->filename
= $file;
168 $this->writeable
= false;
170 return $this->set_error("$file: " . _("Open failed"));
177 /** Close the file and forget the filehandle */
179 @fclose
($this->filehandle
);
180 $this->filehandle
= 0;
181 $this->filename
= '';
182 $this->writable
= false;
185 /** Lock the datafile - try 20 times in 5 seconds */
187 for($i = 0 ; $i < 20 ; $i++
) {
188 if(flock($this->filehandle
, 2 +
4))
196 /** Unlock the datafile */
198 return flock($this->filehandle
, 3);
202 * Overwrite the file with data from $rows
203 * NOTE! Previous locks are broken by this function
204 * @param array $rows new data
207 function overwrite(&$rows) {
209 $newfh = @fopen
($this->filename
.'.tmp', 'w');
212 return $this->set_error($this->filename
. '.tmp:' . _("Open failed"));
215 for($i = 0, $cnt=sizeof($rows) ; $i < $cnt ; $i++
) {
216 if(is_array($rows[$i])) {
217 for($j = 0, $cnt_part=count($rows[$i]) ; $j < $cnt_part ; $j++
) {
218 $rows[$i][$j] = $this->quotevalue($rows[$i][$j]);
220 $tmpwrite = sq_fwrite($newfh, join('|', $rows[$i]) . "\n");
221 if ($tmpwrite === FALSE) {
222 return $this->set_error($this->filename
. '.tmp:' . _("Write failed"));
228 if (!@copy
($this->filename
. '.tmp' , $this->filename
)) {
229 return $this->set_error($this->filename
. ':' . _("Unable to update"));
231 @unlink
($this->filename
. '.tmp');
237 /* ========================== Public ======================== */
241 * @param string $expr search expression
242 * @return array search results
244 function search($expr) {
246 /* To be replaced by advanded search expression parsing */
247 if(is_array($expr)) { return; }
249 /* Make regexp from glob'ed expression
250 * May want to quote other special characters like (, ), -, [, ], etc. */
251 $expr = str_replace('?', '.', $expr);
252 $expr = str_replace('*', '.*', $expr);
258 @rewind
($this->filehandle
);
260 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
261 $line = join(' ', $row);
262 if(eregi($expr, $line)) {
263 array_push($res, array('nickname' => $row[0],
264 'name' => $row[1] . ' ' . $row[2],
265 'firstname' => $row[1],
266 'lastname' => $row[2],
269 'backend' => $this->bnum
,
270 'source' => &$this->sname
));
279 * @param string $alias alias
280 * @return array search results
282 function lookup($alias) {
287 $alias = strtolower($alias);
290 @rewind
($this->filehandle
);
292 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
293 if(strtolower($row[0]) == $alias) {
294 return array('nickname' => $row[0],
295 'name' => $row[1] . ' ' . $row[2],
296 'firstname' => $row[1],
297 'lastname' => $row[2],
300 'backend' => $this->bnum
,
301 'source' => &$this->sname
);
310 * @return array list of all addresses
312 function list_addr() {
315 @rewind
($this->filehandle
);
317 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
318 array_push($res, array('nickname' => $row[0],
319 'name' => $row[1] . ' ' . $row[2],
320 'firstname' => $row[1],
321 'lastname' => $row[2],
324 'backend' => $this->bnum
,
325 'source' => &$this->sname
));
332 * @param array $userdata new data
335 function add($userdata) {
336 if(!$this->writeable
) {
337 return $this->set_error(_("Addressbook is read-only"));
339 /* See if user exists already */
340 $ret = $this->lookup($userdata['nickname']);
342 return $this->set_error(sprintf(_("User '%s' already exist"),
346 /* Here is the data to write */
347 $data = $this->quotevalue($userdata['nickname']) . '|' .
348 $this->quotevalue($userdata['firstname']) . '|' .
349 $this->quotevalue($userdata['lastname']) . '|' .
350 $this->quotevalue($userdata['email']) . '|' .
351 $this->quotevalue($userdata['label']);
353 /* Strip linefeeds */
354 $data = ereg_replace("[\r\n]", ' ', $data);
355 /* Add linefeed at end */
356 $data = $data . "\n";
358 /* Reopen file, just to be sure */
360 if(!$this->writeable
) {
361 return $this->set_error(_("Addressbook is read-only"));
366 return $this->set_error(_("Could not lock datafile"));
370 $r = sq_fwrite($this->filehandle
, $data);
375 /* Test write result */
378 $this->set_error(_("Write to addressbook failed"));
387 * @param string $alias alias that has to be deleted
390 function remove($alias) {
391 if(!$this->writeable
) {
392 return $this->set_error(_("Addressbook is read-only"));
395 /* Lock the file to make sure we're the only process working
398 return $this->set_error(_("Could not lock datafile"));
401 /* Read file into memory, ignoring nicknames to delete */
402 @rewind
($this->filehandle
);
405 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
406 if(!in_array($row[0], $alias)) {
411 /* Write data back */
412 if(!$this->overwrite($rows)) {
423 * @param string $alias modified alias
424 * @param array $userdata new data
425 * @return bool true, if operation successful
427 function modify($alias, $userdata) {
428 if(!$this->writeable
) {
429 return $this->set_error(_("Addressbook is read-only"));
432 /* See if user exists */
433 $ret = $this->lookup($alias);
435 return $this->set_error(sprintf(_("User '%s' does not exist"),
439 /* Lock the file to make sure we're the only process working
442 return $this->set_error(_("Could not lock datafile"));
445 /* Read file into memory, modifying the data for the
446 * user identified by $alias */
448 @rewind
($this->filehandle
);
451 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
452 if(strtolower($row[0]) != strtolower($alias)) {
455 $rows[$i++
] = array(0 => $userdata['nickname'],
456 1 => $userdata['firstname'],
457 2 => $userdata['lastname'],
458 3 => $userdata['email'],
459 4 => $userdata['label']);
463 /* Write data back */
464 if(!$this->overwrite($rows)) {
474 * Function for quoting values before saving
475 * @param string $value string that has to be quoted
476 * @param string quoted string
478 function quotevalue($value) {
479 /* Quote the field if it contains | or ". Double quotes need to
480 * be replaced with "" */
481 if(ereg("[|\"]", $value)) {
482 $value = '"' . str_replace('"', '""', $value) . '"';
487 } /* End of class abook_local_file */