8b3198d29bd5709d102d800ab9324bc7f07a82b9
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 if (!@copy
($this->filename
. '.tmp' , $this->filename
)) {
154 return $this->set_error($file->filename
.':' . _("Unable to update"));
156 @unlink
( $this->filename
.'.tmp');
162 /* ========================== Public ======================== */
164 /* Search the file */
165 function search($expr) {
167 /* To be replaced by advanded search expression parsing */
168 if(is_array($expr)) { return; }
170 /* Make regexp from glob'ed expression
171 * May want to quote other special characters like (, ), -, [, ], etc. */
172 $expr = str_replace('?', '.', $expr);
173 $expr = str_replace('*', '.*', $expr);
179 @rewind
($this->filehandle
);
181 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
182 $line = join(' ', $row);
183 if(eregi($expr, $line)) {
184 array_push($res, array('nickname' => $row[0],
185 'name' => $row[1] . ' ' . $row[2],
186 'firstname' => $row[1],
187 'lastname' => $row[2],
190 'backend' => $this->bnum
,
191 'source' => &$this->sname
));
199 function lookup($alias) {
204 $alias = strtolower($alias);
207 @rewind
($this->filehandle
);
209 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
210 if(strtolower($row[0]) == $alias) {
211 return array('nickname' => $row[0],
212 'name' => $row[1] . ' ' . $row[2],
213 'firstname' => $row[1],
214 'lastname' => $row[2],
217 'backend' => $this->bnum
,
218 'source' => &$this->sname
);
225 /* List all addresses */
226 function list_addr() {
229 @rewind
($this->filehandle
);
231 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
232 array_push($res, array('nickname' => $row[0],
233 'name' => $row[1] . ' ' . $row[2],
234 'firstname' => $row[1],
235 'lastname' => $row[2],
238 'backend' => $this->bnum
,
239 'source' => &$this->sname
));
245 function add($userdata) {
246 if(!$this->writeable
) {
247 return $this->set_error(_("Addressbook is read-only"));
249 /* See if user exists already */
250 $ret = $this->lookup($userdata['nickname']);
252 return $this->set_error(sprintf(_("User '%s' already exist"),
256 /* Here is the data to write */
257 $data = $this->quotevalue($userdata['nickname']) . '|' .
258 $this->quotevalue($userdata['firstname']) . '|' .
259 $this->quotevalue($userdata['lastname']) . '|' .
260 $this->quotevalue($userdata['email']) . '|' .
261 $this->quotevalue($userdata['label']);
263 /* Strip linefeeds */
264 $data = ereg_replace("[\r\n]", ' ', $data);
265 /* Add linefeed at end */
266 $data = $data . "\n";
268 /* Reopen file, just to be sure */
270 if(!$this->writeable
) {
271 return $this->set_error(_("Addressbook is read-only"));
276 return $this->set_error(_("Could not lock datafile"));
280 $r = fwrite($this->filehandle
, $data);
285 /* Test write result and exit if OK */
286 if($r > 0) return true;
289 $this->set_error(_("Write to addressbook failed"));
294 function remove($alias) {
295 if(!$this->writeable
) {
296 return $this->set_error(_("Addressbook is read-only"));
299 /* Lock the file to make sure we're the only process working
302 return $this->set_error(_("Could not lock datafile"));
305 /* Read file into memory, ignoring nicknames to delete */
306 @rewind
($this->filehandle
);
309 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
310 if(!in_array($row[0], $alias)) {
315 /* Write data back */
316 if(!$this->overwrite($rows)) {
326 function modify($alias, $userdata) {
327 if(!$this->writeable
) {
328 return $this->set_error(_("Addressbook is read-only"));
331 /* See if user exists */
332 $ret = $this->lookup($alias);
334 return $this->set_error(sprintf(_("User '%s' does not exist"),
338 /* Lock the file to make sure we're the only process working
341 return $this->set_error(_("Could not lock datafile"));
344 /* Read file into memory, modifying the data for the
345 * user identified by $alias */
347 @rewind
($this->filehandle
);
350 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
351 if(strtolower($row[0]) != strtolower($alias)) {
354 $rows[$i++
] = array(0 => $userdata['nickname'],
355 1 => $userdata['firstname'],
356 2 => $userdata['lastname'],
357 3 => $userdata['email'],
358 4 => $userdata['label']);
362 /* Write data back */
363 if(!$this->overwrite($rows)) {
372 /* Function for quoting values before saving */
373 function quotevalue($value) {
374 /* Quote the field if it contains | or ". Double quotes need to
375 * be replaced with "" */
376 if(ereg("[|\"]", $value)) {
377 $value = '"' . str_replace('"', '""', $value) . '"';
382 } /* End of class abook_local_file */