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
{
31 /** @var string backend type */
33 /** @var string backend name */
34 var $bname = 'local_file';
36 /** @var string file used to store data */
38 /** @var object file handle */
40 /** @var bool create file if it is not present */
42 /** @var string umask of the file */
45 /* ========================== Private ======================= */
49 * @param array $param backend options
52 function abook_local_file($param) {
53 $this->sname
= _("Personal address book");
54 $this->umask
= Umask();
56 if(is_array($param)) {
57 if(empty($param['filename'])) {
58 return $this->set_error('Invalid parameters');
60 if(!is_string($param['filename'])) {
61 return $this->set_error($param['filename'] . ': '.
62 _("Not a file name"));
65 $this->filename
= $param['filename'];
67 if(isset($param['create'])) {
68 $this->create
= $param['create'];
70 if(isset($param['umask'])) {
71 $this->umask
= $param['umask'];
73 if(!empty($param['name'])) {
74 $this->sname
= $param['name'];
79 $this->set_error('Invalid argument to constructor');
84 * Open the addressbook file and store the file pointer.
85 * Use $file as the file to open, or the class' own
86 * filename property. If $param is empty and file is
88 * @param bool $new is file already opened
91 function open($new = false) {
93 $file = $this->filename
;
94 $create = $this->create
;
96 /* Return true is file is open and $new is unset */
97 if($this->filehandle
&& !$new) {
101 /* Check that new file exitsts */
102 if((!(file_exists($file) && is_readable($file))) && !$create) {
103 return $this->set_error("$file: " . _("No such file or directory"));
106 /* Close old file, if any */
107 if($this->filehandle
) { $this->close(); }
109 /* Open file. First try to open for reading and writing,
110 * but fall back to read only. */
112 $fh = @fopen
($file, 'a+');
114 $this->filehandle
= &$fh;
115 $this->filename
= $file;
116 $this->writeable
= true;
118 $fh = @fopen
($file, 'r');
120 $this->filehandle
= &$fh;
121 $this->filename
= $file;
122 $this->writeable
= false;
124 return $this->set_error("$file: " . _("Open failed"));
130 /** Close the file and forget the filehandle */
132 @fclose
($this->filehandle
);
133 $this->filehandle
= 0;
134 $this->filename
= '';
135 $this->writable
= false;
138 /** Lock the datafile - try 20 times in 5 seconds */
140 for($i = 0 ; $i < 20 ; $i++
) {
141 if(flock($this->filehandle
, 2 +
4))
149 /** Unlock the datafile */
151 return flock($this->filehandle
, 3);
155 * Overwrite the file with data from $rows
156 * NOTE! Previous locks are broken by this function
157 * @param array $rows new data
160 function overwrite(&$rows) {
162 $newfh = @fopen
($this->filename
.'.tmp', 'w');
165 return $this->set_error($this->filename
. '.tmp:' . _("Open failed"));
168 for($i = 0, $cnt=sizeof($rows) ; $i < $cnt ; $i++
) {
169 if(is_array($rows[$i])) {
170 for($j = 0, $cnt_part=count($rows[$i]) ; $j < $cnt_part ; $j++
) {
171 $rows[$i][$j] = $this->quotevalue($rows[$i][$j]);
173 $tmpwrite = sq_fwrite($newfh, join('|', $rows[$i]) . "\n");
174 if ($tmpwrite === FALSE) {
175 return $this->set_error($this->filename
. '.tmp:' . _("Write failed"));
181 if (!@copy
($this->filename
. '.tmp' , $this->filename
)) {
182 return $this->set_error($this->filename
. ':' . _("Unable to update"));
184 @unlink
($this->filename
. '.tmp');
190 /* ========================== Public ======================== */
194 * @param string $expr search expression
195 * @return array search results
197 function search($expr) {
199 /* To be replaced by advanded search expression parsing */
200 if(is_array($expr)) { return; }
202 /* Make regexp from glob'ed expression
203 * May want to quote other special characters like (, ), -, [, ], etc. */
204 $expr = str_replace('?', '.', $expr);
205 $expr = str_replace('*', '.*', $expr);
211 @rewind
($this->filehandle
);
213 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
214 $line = join(' ', $row);
215 if(eregi($expr, $line)) {
216 array_push($res, array('nickname' => $row[0],
217 'name' => $row[1] . ' ' . $row[2],
218 'firstname' => $row[1],
219 'lastname' => $row[2],
222 'backend' => $this->bnum
,
223 'source' => &$this->sname
));
232 * @param string $alias alias
233 * @return array search results
235 function lookup($alias) {
240 $alias = strtolower($alias);
243 @rewind
($this->filehandle
);
245 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
246 if(strtolower($row[0]) == $alias) {
247 return array('nickname' => $row[0],
248 'name' => $row[1] . ' ' . $row[2],
249 'firstname' => $row[1],
250 'lastname' => $row[2],
253 'backend' => $this->bnum
,
254 'source' => &$this->sname
);
263 * @return array list of all addresses
265 function list_addr() {
268 @rewind
($this->filehandle
);
270 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
271 array_push($res, array('nickname' => $row[0],
272 'name' => $row[1] . ' ' . $row[2],
273 'firstname' => $row[1],
274 'lastname' => $row[2],
277 'backend' => $this->bnum
,
278 'source' => &$this->sname
));
285 * @param array $userdata new data
288 function add($userdata) {
289 if(!$this->writeable
) {
290 return $this->set_error(_("Addressbook is read-only"));
292 /* See if user exists already */
293 $ret = $this->lookup($userdata['nickname']);
295 return $this->set_error(sprintf(_("User '%s' already exist"),
299 /* Here is the data to write */
300 $data = $this->quotevalue($userdata['nickname']) . '|' .
301 $this->quotevalue($userdata['firstname']) . '|' .
302 $this->quotevalue($userdata['lastname']) . '|' .
303 $this->quotevalue($userdata['email']) . '|' .
304 $this->quotevalue($userdata['label']);
306 /* Strip linefeeds */
307 $data = ereg_replace("[\r\n]", ' ', $data);
308 /* Add linefeed at end */
309 $data = $data . "\n";
311 /* Reopen file, just to be sure */
313 if(!$this->writeable
) {
314 return $this->set_error(_("Addressbook is read-only"));
319 return $this->set_error(_("Could not lock datafile"));
323 $r = sq_fwrite($this->filehandle
, $data);
328 /* Test write result */
331 $this->set_error(_("Write to addressbook failed"));
340 * @param string $alias alias that has to be deleted
343 function remove($alias) {
344 if(!$this->writeable
) {
345 return $this->set_error(_("Addressbook is read-only"));
348 /* Lock the file to make sure we're the only process working
351 return $this->set_error(_("Could not lock datafile"));
354 /* Read file into memory, ignoring nicknames to delete */
355 @rewind
($this->filehandle
);
358 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
359 if(!in_array($row[0], $alias)) {
364 /* Write data back */
365 if(!$this->overwrite($rows)) {
376 * @param string $alias modified alias
377 * @param array $userdata new data
378 * @return bool true, if operation successful
380 function modify($alias, $userdata) {
381 if(!$this->writeable
) {
382 return $this->set_error(_("Addressbook is read-only"));
385 /* See if user exists */
386 $ret = $this->lookup($alias);
388 return $this->set_error(sprintf(_("User '%s' does not exist"),
392 /* Lock the file to make sure we're the only process working
395 return $this->set_error(_("Could not lock datafile"));
398 /* Read file into memory, modifying the data for the
399 * user identified by $alias */
401 @rewind
($this->filehandle
);
404 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
405 if(strtolower($row[0]) != strtolower($alias)) {
408 $rows[$i++
] = array(0 => $userdata['nickname'],
409 1 => $userdata['firstname'],
410 2 => $userdata['lastname'],
411 3 => $userdata['email'],
412 4 => $userdata['label']);
416 /* Write data back */
417 if(!$this->overwrite($rows)) {
427 * Function for quoting values before saving
428 * @param string $value string that has to be quoted
429 * @param string quoted string
431 function quotevalue($value) {
432 /* Quote the field if it contains | or ". Double quotes need to
433 * be replaced with "" */
434 if(ereg("[|\"]", $value)) {
435 $value = '"' . str_replace('"', '""', $value) . '"';
440 } /* End of class abook_local_file */