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) {
137 $newfh = @fopen
($this->filename
, 'w');
139 return $this->set_error("$file: " . _("Open failed"));
142 for($i = 0 ; $i < sizeof($rows) ; $i++
) {
143 if(is_array($rows[$i])) {
144 fwrite($newfh, join('|', $rows[$i]) . "\n");
154 /* ========================== Public ======================== */
156 /* Search the file */
157 function search($expr) {
159 /* To be replaced by advanded search expression parsing */
160 if(is_array($expr)) { return; }
162 /* Make regexp from glob'ed expression
163 * May want to quote other special characters like (, ), -, [, ], etc. */
164 $expr = str_replace('?', '.', $expr);
165 $expr = str_replace('*', '.*', $expr);
171 @rewind
($this->filehandle
);
173 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
174 $line = join(' ', $row);
175 if(eregi($expr, $line)) {
176 array_push($res, array('nickname' => $row[0],
177 'name' => $row[1] . ' ' . $row[2],
178 'firstname' => $row[1],
179 'lastname' => $row[2],
182 'backend' => $this->bnum
,
183 'source' => &$this->sname
));
191 function lookup($alias) {
196 $alias = strtolower($alias);
199 @rewind
($this->filehandle
);
201 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
202 if(strtolower($row[0]) == $alias) {
203 return array('nickname' => $row[0],
204 'name' => $row[1] . ' ' . $row[2],
205 'firstname' => $row[1],
206 'lastname' => $row[2],
209 'backend' => $this->bnum
,
210 'source' => &$this->sname
);
217 /* List all addresses */
218 function list_addr() {
221 @rewind
($this->filehandle
);
223 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
224 array_push($res, array('nickname' => $row[0],
225 'name' => $row[1] . ' ' . $row[2],
226 'firstname' => $row[1],
227 'lastname' => $row[2],
230 'backend' => $this->bnum
,
231 'source' => &$this->sname
));
237 function add($userdata) {
238 if(!$this->writeable
) {
239 return $this->set_error(_("Addressbook is read-only"));
241 /* See if user exists already */
242 $ret = $this->lookup($userdata['nickname']);
244 return $this->set_error(sprintf(_("User '%s' already exist"),
248 /* Here is the data to write */
249 $data = $userdata['nickname'] . '|' . $userdata['firstname'] . '|' .
250 $userdata['lastname'] . '|' . $userdata['email'] . '|' .
252 /* Strip linefeeds */
253 $data = ereg_replace("[\r\n]", ' ', $data);
254 /* Add linefeed at end */
255 $data = $data . "\n";
257 /* Reopen file, just to be sure */
259 if(!$this->writeable
) {
260 return $this->set_error(_("Addressbook is read-only"));
265 return $this->set_error(_("Could not lock datafile"));
269 $r = fwrite($this->filehandle
, $data);
274 /* Test write result and exit if OK */
275 if($r > 0) return true;
278 $this->set_error(_("Write to addressbook failed"));
283 function remove($alias) {
284 if(!$this->writeable
) {
285 return $this->set_error(_("Addressbook is read-only"));
288 /* Lock the file to make sure we're the only process working
291 return $this->set_error(_("Could not lock datafile"));
294 /* Read file into memory, ignoring nicknames to delete */
295 @rewind
($this->filehandle
);
298 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
299 if(!in_array($row[0], $alias)) {
304 /* Write data back */
305 if(!$this->overwrite($rows)) {
315 function modify($alias, $userdata) {
316 if(!$this->writeable
) {
317 return $this->set_error(_("Addressbook is read-only"));
320 /* See if user exists */
321 $ret = $this->lookup($alias);
323 return $this->set_error(sprintf(_("User '%s' does not exist"),
327 /* Lock the file to make sure we're the only process working
330 return $this->set_error(_("Could not lock datafile"));
333 /* Read file into memory, modifying the data for the
334 * user identified by $alias */
336 @rewind
($this->filehandle
);
339 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
340 if(strtolower($row[0]) != strtolower($alias)) {
343 $rows[$i++
] = array(0 => $userdata['nickname'],
344 1 => $userdata['firstname'],
345 2 => $userdata['lastname'],
346 3 => $userdata['email'],
347 4 => $userdata['label']);
351 /* Write data back */
352 if(!$this->overwrite($rows)) {
361 } /* End of class abook_local_file */