6 * Copyright (c) 1999-2002 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');
140 return $this->set_error($this->filename
.'.tmp: '. _("Open failed"));
143 for($i = 0 ; $i < sizeof($rows) ; $i++
) {
144 if(is_array($rows[$i])) {
145 for($j = 0 ; $j < count($rows[$i]) ; $j++
) {
146 $rows[$i][$j] = $this->quotevalue($rows[$i][$j]);
148 fwrite($newfh, join('|', $rows[$i]) . "\n");
153 copy( $this->filename
.'.tmp' , $this->filename
);
154 unlink( $this->filename
.'.tmp');
160 /* ========================== Public ======================== */
162 /* Search the file */
163 function search($expr) {
165 /* To be replaced by advanded search expression parsing */
166 if(is_array($expr)) { return; }
168 /* Make regexp from glob'ed expression
169 * May want to quote other special characters like (, ), -, [, ], etc. */
170 $expr = str_replace('?', '.', $expr);
171 $expr = str_replace('*', '.*', $expr);
177 @rewind
($this->filehandle
);
179 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
180 $line = join(' ', $row);
181 if(eregi($expr, $line)) {
182 array_push($res, array('nickname' => $row[0],
183 'name' => $row[1] . ' ' . $row[2],
184 'firstname' => $row[1],
185 'lastname' => $row[2],
188 'backend' => $this->bnum
,
189 'source' => &$this->sname
));
197 function lookup($alias) {
202 $alias = strtolower($alias);
205 @rewind
($this->filehandle
);
207 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
208 if(strtolower($row[0]) == $alias) {
209 return array('nickname' => $row[0],
210 'name' => $row[1] . ' ' . $row[2],
211 'firstname' => $row[1],
212 'lastname' => $row[2],
215 'backend' => $this->bnum
,
216 'source' => &$this->sname
);
223 /* List all addresses */
224 function list_addr() {
227 @rewind
($this->filehandle
);
229 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
230 array_push($res, array('nickname' => $row[0],
231 'name' => $row[1] . ' ' . $row[2],
232 'firstname' => $row[1],
233 'lastname' => $row[2],
236 'backend' => $this->bnum
,
237 'source' => &$this->sname
));
243 function add($userdata) {
244 if(!$this->writeable
) {
245 return $this->set_error(_("Addressbook is read-only"));
247 /* See if user exists already */
248 $ret = $this->lookup($userdata['nickname']);
250 return $this->set_error(sprintf(_("User '%s' already exist"),
254 /* Here is the data to write */
255 $data = $this->quotevalue($userdata['nickname']) . '|' .
256 $this->quotevalue($userdata['firstname']) . '|' .
257 $this->quotevalue($userdata['lastname']) . '|' .
258 $this->quotevalue($userdata['email']) . '|' .
259 $this->quotevalue($userdata['label']);
261 /* Strip linefeeds */
262 $data = ereg_replace("[\r\n]", ' ', $data);
263 /* Add linefeed at end */
264 $data = $data . "\n";
266 /* Reopen file, just to be sure */
268 if(!$this->writeable
) {
269 return $this->set_error(_("Addressbook is read-only"));
274 return $this->set_error(_("Could not lock datafile"));
278 $r = fwrite($this->filehandle
, $data);
283 /* Test write result and exit if OK */
284 if($r > 0) return true;
287 $this->set_error(_("Write to addressbook failed"));
292 function remove($alias) {
293 if(!$this->writeable
) {
294 return $this->set_error(_("Addressbook is read-only"));
297 /* Lock the file to make sure we're the only process working
300 return $this->set_error(_("Could not lock datafile"));
303 /* Read file into memory, ignoring nicknames to delete */
304 @rewind
($this->filehandle
);
307 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
308 if(!in_array($row[0], $alias)) {
313 /* Write data back */
314 if(!$this->overwrite($rows)) {
324 function modify($alias, $userdata) {
325 if(!$this->writeable
) {
326 return $this->set_error(_("Addressbook is read-only"));
329 /* See if user exists */
330 $ret = $this->lookup($alias);
332 return $this->set_error(sprintf(_("User '%s' does not exist"),
336 /* Lock the file to make sure we're the only process working
339 return $this->set_error(_("Could not lock datafile"));
342 /* Read file into memory, modifying the data for the
343 * user identified by $alias */
345 @rewind
($this->filehandle
);
348 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
349 if(strtolower($row[0]) != strtolower($alias)) {
352 $rows[$i++
] = array(0 => $userdata['nickname'],
353 1 => $userdata['firstname'],
354 2 => $userdata['lastname'],
355 3 => $userdata['email'],
356 4 => $userdata['label']);
360 /* Write data back */
361 if(!$this->overwrite($rows)) {
370 /* Function for quoting values before saving */
371 function quotevalue($value) {
372 /* Quote the field if it contains | or ". Double quotes need to
373 * be replaced with "" */
374 if(ereg("[|\"]", $value)) {
375 $value = '"' . str_replace('"', '""', $value) . '"';
380 } /* End of class abook_local_file */