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
, 'w');
140 return $this->set_error("$file: " . _("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");
158 /* ========================== Public ======================== */
160 /* Search the file */
161 function search($expr) {
163 /* To be replaced by advanded search expression parsing */
164 if(is_array($expr)) { return; }
166 /* Make regexp from glob'ed expression
167 * May want to quote other special characters like (, ), -, [, ], etc. */
168 $expr = str_replace('?', '.', $expr);
169 $expr = str_replace('*', '.*', $expr);
175 @rewind
($this->filehandle
);
177 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
178 $line = join(' ', $row);
179 if(eregi($expr, $line)) {
180 array_push($res, array('nickname' => $row[0],
181 'name' => $row[1] . ' ' . $row[2],
182 'firstname' => $row[1],
183 'lastname' => $row[2],
186 'backend' => $this->bnum
,
187 'source' => &$this->sname
));
195 function lookup($alias) {
200 $alias = strtolower($alias);
203 @rewind
($this->filehandle
);
205 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
206 if(strtolower($row[0]) == $alias) {
207 return array('nickname' => $row[0],
208 'name' => $row[1] . ' ' . $row[2],
209 'firstname' => $row[1],
210 'lastname' => $row[2],
213 'backend' => $this->bnum
,
214 'source' => &$this->sname
);
221 /* List all addresses */
222 function list_addr() {
225 @rewind
($this->filehandle
);
227 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
228 array_push($res, array('nickname' => $row[0],
229 'name' => $row[1] . ' ' . $row[2],
230 'firstname' => $row[1],
231 'lastname' => $row[2],
234 'backend' => $this->bnum
,
235 'source' => &$this->sname
));
241 function add($userdata) {
242 if(!$this->writeable
) {
243 return $this->set_error(_("Addressbook is read-only"));
245 /* See if user exists already */
246 $ret = $this->lookup($userdata['nickname']);
248 return $this->set_error(sprintf(_("User '%s' already exist"),
252 /* Here is the data to write */
253 $data = $this->quotevalue($userdata['nickname']) . '|' .
254 $this->quotevalue($userdata['firstname']) . '|' .
255 $this->quotevalue($userdata['lastname']) . '|' .
256 $this->quotevalue($userdata['email']) . '|' .
257 $this->quotevalue($userdata['label']);
259 /* Strip linefeeds */
260 $data = ereg_replace("[\r\n]", ' ', $data);
261 /* Add linefeed at end */
262 $data = $data . "\n";
264 /* Reopen file, just to be sure */
266 if(!$this->writeable
) {
267 return $this->set_error(_("Addressbook is read-only"));
272 return $this->set_error(_("Could not lock datafile"));
276 $r = fwrite($this->filehandle
, $data);
281 /* Test write result and exit if OK */
282 if($r > 0) return true;
285 $this->set_error(_("Write to addressbook failed"));
290 function remove($alias) {
291 if(!$this->writeable
) {
292 return $this->set_error(_("Addressbook is read-only"));
295 /* Lock the file to make sure we're the only process working
298 return $this->set_error(_("Could not lock datafile"));
301 /* Read file into memory, ignoring nicknames to delete */
302 @rewind
($this->filehandle
);
305 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
306 if(!in_array($row[0], $alias)) {
311 /* Write data back */
312 if(!$this->overwrite($rows)) {
322 function modify($alias, $userdata) {
323 if(!$this->writeable
) {
324 return $this->set_error(_("Addressbook is read-only"));
327 /* See if user exists */
328 $ret = $this->lookup($alias);
330 return $this->set_error(sprintf(_("User '%s' does not exist"),
334 /* Lock the file to make sure we're the only process working
337 return $this->set_error(_("Could not lock datafile"));
340 /* Read file into memory, modifying the data for the
341 * user identified by $alias */
343 @rewind
($this->filehandle
);
346 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
347 if(strtolower($row[0]) != strtolower($alias)) {
350 $rows[$i++
] = array(0 => $userdata['nickname'],
351 1 => $userdata['firstname'],
352 2 => $userdata['lastname'],
353 3 => $userdata['email'],
354 4 => $userdata['label']);
358 /* Write data back */
359 if(!$this->overwrite($rows)) {
368 /* Function for quoting values before saving */
369 function quotevalue($value) {
370 /* Quote the field if it contains | or ". Double quotes need to
371 * be replaced with "" */
372 if(ereg("[|\"]", $value)) {
373 $value = '"' . str_replace('"', '""', $value) . '"';
378 } /* End of class abook_local_file */