6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Backend for addressbook as a pipe separated file
11 * An array with the following elements must be passed to
12 * the class constructor (elements marked ? are optional):
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.
18 * NOTE. This class should not be used directly. Use the
19 * "AddressBook" class instead.
22 * @package squirrelmail
23 * @subpackage addressbook
27 * Store the addressbook in a local file
28 * @package squirrelmail
30 class abook_local_file
extends addressbook_backend
{
32 var $bname = 'local_file';
39 /* ========================== Private ======================= */
42 function abook_local_file($param) {
43 $this->sname
= _("Personal address book");
44 $this->umask
= Umask();
46 if(is_array($param)) {
47 if(empty($param['filename'])) {
48 return $this->set_error('Invalid parameters');
50 if(!is_string($param['filename'])) {
51 return $this->set_error($param['filename'] . ': '.
52 _("Not a file name"));
55 $this->filename
= $param['filename'];
57 if($param['create']) {
60 if(isset($param['umask'])) {
61 $this->umask
= $param['umask'];
63 if(!empty($param['name'])) {
64 $this->sname
= $param['name'];
69 $this->set_error('Invalid argument to constructor');
73 /* Open the addressbook file and store the file pointer.
74 * Use $file as the file to open, or the class' own
75 * filename property. If $param is empty and file is
76 * open, do nothing. */
77 function open($new = false) {
79 $file = $this->filename
;
80 $create = $this->create
;
82 /* Return true is file is open and $new is unset */
83 if($this->filehandle
&& !$new) {
87 /* Check that new file exitsts */
88 if((!(file_exists($file) && is_readable($file))) && !$create) {
89 return $this->set_error("$file: " . _("No such file or directory"));
92 /* Close old file, if any */
93 if($this->filehandle
) { $this->close(); }
95 /* Open file. First try to open for reading and writing,
96 * but fall back to read only. */
98 $fh = @fopen
($file, 'a+');
100 $this->filehandle
= &$fh;
101 $this->filename
= $file;
102 $this->writeable
= true;
104 $fh = @fopen
($file, 'r');
106 $this->filehandle
= &$fh;
107 $this->filename
= $file;
108 $this->writeable
= false;
110 return $this->set_error("$file: " . _("Open failed"));
116 /* Close the file and forget the filehandle */
118 @fclose
($this->filehandle
);
119 $this->filehandle
= 0;
120 $this->filename
= '';
121 $this->writable
= false;
124 /* Lock the datafile - try 20 times in 5 seconds */
126 for($i = 0 ; $i < 20 ; $i++
) {
127 if(flock($this->filehandle
, 2 +
4))
135 /* Lock the datafile */
137 return flock($this->filehandle
, 3);
140 /* Overwrite the file with data from $rows
141 * NOTE! Previous locks are broken by this function */
142 function overwrite(&$rows) {
144 $newfh = @fopen
($this->filename
.'.tmp', 'w');
147 return $this->set_error($this->filename
. '.tmp:' . _("Open failed"));
150 for($i = 0, $cnt=sizeof($rows) ; $i < $cnt ; $i++
) {
151 if(is_array($rows[$i])) {
152 for($j = 0, $cnt_part=count($rows[$i]) ; $j < $cnt_part ; $j++
) {
153 $rows[$i][$j] = $this->quotevalue($rows[$i][$j]);
155 $tmpwrite = sq_fwrite($newfh, join('|', $rows[$i]) . "\n");
156 if ($tmpwrite === FALSE) {
157 return $this->set_error($this->filename
. '.tmp:' . _("Write failed"));
163 if (!@copy
($this->filename
. '.tmp' , $this->filename
)) {
164 return $this->set_error($this->filename
. ':' . _("Unable to update"));
166 @unlink
($this->filename
. '.tmp');
172 /* ========================== Public ======================== */
174 /* Search the file */
175 function search($expr) {
177 /* To be replaced by advanded search expression parsing */
178 if(is_array($expr)) { return; }
180 /* Make regexp from glob'ed expression
181 * May want to quote other special characters like (, ), -, [, ], etc. */
182 $expr = str_replace('?', '.', $expr);
183 $expr = str_replace('*', '.*', $expr);
189 @rewind
($this->filehandle
);
191 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
192 $line = join(' ', $row);
193 if(eregi($expr, $line)) {
194 array_push($res, array('nickname' => $row[0],
195 'name' => $row[1] . ' ' . $row[2],
196 'firstname' => $row[1],
197 'lastname' => $row[2],
200 'backend' => $this->bnum
,
201 'source' => &$this->sname
));
209 function lookup($alias) {
214 $alias = strtolower($alias);
217 @rewind
($this->filehandle
);
219 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
220 if(strtolower($row[0]) == $alias) {
221 return array('nickname' => $row[0],
222 'name' => $row[1] . ' ' . $row[2],
223 'firstname' => $row[1],
224 'lastname' => $row[2],
227 'backend' => $this->bnum
,
228 'source' => &$this->sname
);
235 /* List all addresses */
236 function list_addr() {
239 @rewind
($this->filehandle
);
241 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
242 array_push($res, array('nickname' => $row[0],
243 'name' => $row[1] . ' ' . $row[2],
244 'firstname' => $row[1],
245 'lastname' => $row[2],
248 'backend' => $this->bnum
,
249 'source' => &$this->sname
));
255 function add($userdata) {
256 if(!$this->writeable
) {
257 return $this->set_error(_("Addressbook is read-only"));
259 /* See if user exists already */
260 $ret = $this->lookup($userdata['nickname']);
262 return $this->set_error(sprintf(_("User '%s' already exist"),
266 /* Here is the data to write */
267 $data = $this->quotevalue($userdata['nickname']) . '|' .
268 $this->quotevalue($userdata['firstname']) . '|' .
269 $this->quotevalue($userdata['lastname']) . '|' .
270 $this->quotevalue($userdata['email']) . '|' .
271 $this->quotevalue($userdata['label']);
273 /* Strip linefeeds */
274 $data = ereg_replace("[\r\n]", ' ', $data);
275 /* Add linefeed at end */
276 $data = $data . "\n";
278 /* Reopen file, just to be sure */
280 if(!$this->writeable
) {
281 return $this->set_error(_("Addressbook is read-only"));
286 return $this->set_error(_("Could not lock datafile"));
290 $r = sq_fwrite($this->filehandle
, $data);
295 /* Test write result */
298 $this->set_error(_("Write to addressbook failed"));
306 function remove($alias) {
307 if(!$this->writeable
) {
308 return $this->set_error(_("Addressbook is read-only"));
311 /* Lock the file to make sure we're the only process working
314 return $this->set_error(_("Could not lock datafile"));
317 /* Read file into memory, ignoring nicknames to delete */
318 @rewind
($this->filehandle
);
321 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
322 if(!in_array($row[0], $alias)) {
327 /* Write data back */
328 if(!$this->overwrite($rows)) {
338 function modify($alias, $userdata) {
339 if(!$this->writeable
) {
340 return $this->set_error(_("Addressbook is read-only"));
343 /* See if user exists */
344 $ret = $this->lookup($alias);
346 return $this->set_error(sprintf(_("User '%s' does not exist"),
350 /* Lock the file to make sure we're the only process working
353 return $this->set_error(_("Could not lock datafile"));
356 /* Read file into memory, modifying the data for the
357 * user identified by $alias */
359 @rewind
($this->filehandle
);
362 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
363 if(strtolower($row[0]) != strtolower($alias)) {
366 $rows[$i++
] = array(0 => $userdata['nickname'],
367 1 => $userdata['firstname'],
368 2 => $userdata['lastname'],
369 3 => $userdata['email'],
370 4 => $userdata['label']);
374 /* Write data back */
375 if(!$this->overwrite($rows)) {
384 /* Function for quoting values before saving */
385 function quotevalue($value) {
386 /* Quote the field if it contains | or ". Double quotes need to
387 * be replaced with "" */
388 if(ereg("[|\"]", $value)) {
389 $value = '"' . str_replace('"', '""', $value) . '"';
394 } /* End of class abook_local_file */