6 * Copyright (c) 1999-2003 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.
24 class abook_local_file
extends addressbook_backend
{
26 var $bname = 'local_file';
33 /* ========================== Private ======================= */
36 function abook_local_file($param) {
37 $this->sname
= _("Personal address book");
38 $this->umask
= Umask();
40 if(is_array($param)) {
41 if(empty($param['filename'])) {
42 return $this->set_error('Invalid parameters');
44 if(!is_string($param['filename'])) {
45 return $this->set_error($param['filename'] . ': '.
46 _("Not a file name"));
49 $this->filename
= $param['filename'];
51 if($param['create']) {
54 if(isset($param['umask'])) {
55 $this->umask
= $param['umask'];
57 if(!empty($param['name'])) {
58 $this->sname
= $param['name'];
63 $this->set_error('Invalid argument to constructor');
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) {
73 $file = $this->filename
;
74 $create = $this->create
;
76 /* Return true is file is open and $new is unset */
77 if($this->filehandle
&& !$new) {
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"));
86 /* Close old file, if any */
87 if($this->filehandle
) { $this->close(); }
89 /* Open file. First try to open for reading and writing,
90 * but fall back to read only. */
92 $fh = @fopen
($file, 'a+');
94 $this->filehandle
= &$fh;
95 $this->filename
= $file;
96 $this->writeable
= true;
98 $fh = @fopen
($file, 'r');
100 $this->filehandle
= &$fh;
101 $this->filename
= $file;
102 $this->writeable
= false;
104 return $this->set_error("$file: " . _("Open failed"));
110 /* Close the file and forget the filehandle */
112 @fclose
($this->filehandle
);
113 $this->filehandle
= 0;
114 $this->filename
= '';
115 $this->writable
= false;
118 /* Lock the datafile - try 20 times in 5 seconds */
120 for($i = 0 ; $i < 20 ; $i++
) {
121 if(flock($this->filehandle
, 2 +
4))
129 /* Lock the datafile */
131 return flock($this->filehandle
, 3);
134 /* Overwrite the file with data from $rows
135 * NOTE! Previous locks are broken by this function */
136 function overwrite(&$rows) {
138 $newfh = @fopen
($this->filename
.'.tmp', 'w');
141 return $this->set_error($this->filename
. '.tmp:' . _("Open failed"));
144 for($i = 0, $cnt=sizeof($rows) ; $i < $cnt ; $i++
) {
145 if(is_array($rows[$i])) {
146 for($j = 0, $cnt_part=count($rows[$i]) ; $j < $cnt_part ; $j++
) {
147 $rows[$i][$j] = $this->quotevalue($rows[$i][$j]);
149 $tmpwrite = @fwrite
($newfh, join('|', $rows[$i]) . "\n");
150 if ($tmpwrite == -1) {
151 return $this->set_error($this->filename
. '.tmp:' . _("Write failed"));
157 if (!@copy
($this->filename
. '.tmp' , $this->filename
)) {
158 return $this->set_error($this->filename
. ':' . _("Unable to update"));
160 @unlink
($this->filename
. '.tmp');
166 /* ========================== Public ======================== */
168 /* Search the file */
169 function search($expr) {
171 /* To be replaced by advanded search expression parsing */
172 if(is_array($expr)) { return; }
174 /* Make regexp from glob'ed expression
175 * May want to quote other special characters like (, ), -, [, ], etc. */
176 $expr = str_replace('?', '.', $expr);
177 $expr = str_replace('*', '.*', $expr);
183 @rewind
($this->filehandle
);
185 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
186 $line = join(' ', $row);
187 if(eregi($expr, $line)) {
188 array_push($res, array('nickname' => $row[0],
189 'name' => $row[1] . ' ' . $row[2],
190 'firstname' => $row[1],
191 'lastname' => $row[2],
194 'backend' => $this->bnum
,
195 'source' => &$this->sname
));
203 function lookup($alias) {
208 $alias = strtolower($alias);
211 @rewind
($this->filehandle
);
213 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
214 if(strtolower($row[0]) == $alias) {
215 return array('nickname' => $row[0],
216 'name' => $row[1] . ' ' . $row[2],
217 'firstname' => $row[1],
218 'lastname' => $row[2],
221 'backend' => $this->bnum
,
222 'source' => &$this->sname
);
229 /* List all addresses */
230 function list_addr() {
233 @rewind
($this->filehandle
);
235 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
236 array_push($res, array('nickname' => $row[0],
237 'name' => $row[1] . ' ' . $row[2],
238 'firstname' => $row[1],
239 'lastname' => $row[2],
242 'backend' => $this->bnum
,
243 'source' => &$this->sname
));
249 function add($userdata) {
250 if(!$this->writeable
) {
251 return $this->set_error(_("Addressbook is read-only"));
253 /* See if user exists already */
254 $ret = $this->lookup($userdata['nickname']);
256 return $this->set_error(sprintf(_("User '%s' already exist"),
260 /* Here is the data to write */
261 $data = $this->quotevalue($userdata['nickname']) . '|' .
262 $this->quotevalue($userdata['firstname']) . '|' .
263 $this->quotevalue($userdata['lastname']) . '|' .
264 $this->quotevalue($userdata['email']) . '|' .
265 $this->quotevalue($userdata['label']);
267 /* Strip linefeeds */
268 $data = ereg_replace("[\r\n]", ' ', $data);
269 /* Add linefeed at end */
270 $data = $data . "\n";
272 /* Reopen file, just to be sure */
274 if(!$this->writeable
) {
275 return $this->set_error(_("Addressbook is read-only"));
280 return $this->set_error(_("Could not lock datafile"));
284 $r = fwrite($this->filehandle
, $data);
289 /* Test write result and exit if OK */
290 if($r > 0) return true;
293 $this->set_error(_("Write to addressbook failed"));
298 function remove($alias) {
299 if(!$this->writeable
) {
300 return $this->set_error(_("Addressbook is read-only"));
303 /* Lock the file to make sure we're the only process working
306 return $this->set_error(_("Could not lock datafile"));
309 /* Read file into memory, ignoring nicknames to delete */
310 @rewind
($this->filehandle
);
313 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
314 if(!in_array($row[0], $alias)) {
319 /* Write data back */
320 if(!$this->overwrite($rows)) {
330 function modify($alias, $userdata) {
331 if(!$this->writeable
) {
332 return $this->set_error(_("Addressbook is read-only"));
335 /* See if user exists */
336 $ret = $this->lookup($alias);
338 return $this->set_error(sprintf(_("User '%s' does not exist"),
342 /* Lock the file to make sure we're the only process working
345 return $this->set_error(_("Could not lock datafile"));
348 /* Read file into memory, modifying the data for the
349 * user identified by $alias */
351 @rewind
($this->filehandle
);
354 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
355 if(strtolower($row[0]) != strtolower($alias)) {
358 $rows[$i++
] = array(0 => $userdata['nickname'],
359 1 => $userdata['firstname'],
360 2 => $userdata['lastname'],
361 3 => $userdata['email'],
362 4 => $userdata['label']);
366 /* Write data back */
367 if(!$this->overwrite($rows)) {
376 /* Function for quoting values before saving */
377 function quotevalue($value) {
378 /* Quote the field if it contains | or ". Double quotes need to
379 * be replaced with "" */
380 if(ereg("[|\"]", $value)) {
381 $value = '"' . str_replace('"', '""', $value) . '"';
386 } /* End of class abook_local_file */