49f82845ad31d39e1235666382d08cb2a48f85ff
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.
22 * @package squirrelmail
26 * Store the addressbook in a local file
28 class abook_local_file
extends addressbook_backend
{
30 var $bname = 'local_file';
37 /* ========================== Private ======================= */
40 function abook_local_file($param) {
41 $this->sname
= _("Personal address book");
42 $this->umask
= Umask();
44 if(is_array($param)) {
45 if(empty($param['filename'])) {
46 return $this->set_error('Invalid parameters');
48 if(!is_string($param['filename'])) {
49 return $this->set_error($param['filename'] . ': '.
50 _("Not a file name"));
53 $this->filename
= $param['filename'];
55 if($param['create']) {
58 if(isset($param['umask'])) {
59 $this->umask
= $param['umask'];
61 if(!empty($param['name'])) {
62 $this->sname
= $param['name'];
67 $this->set_error('Invalid argument to constructor');
71 /* Open the addressbook file and store the file pointer.
72 * Use $file as the file to open, or the class' own
73 * filename property. If $param is empty and file is
74 * open, do nothing. */
75 function open($new = false) {
77 $file = $this->filename
;
78 $create = $this->create
;
80 /* Return true is file is open and $new is unset */
81 if($this->filehandle
&& !$new) {
85 /* Check that new file exitsts */
86 if((!(file_exists($file) && is_readable($file))) && !$create) {
87 return $this->set_error("$file: " . _("No such file or directory"));
90 /* Close old file, if any */
91 if($this->filehandle
) { $this->close(); }
93 /* Open file. First try to open for reading and writing,
94 * but fall back to read only. */
96 $fh = @fopen
($file, 'a+');
98 $this->filehandle
= &$fh;
99 $this->filename
= $file;
100 $this->writeable
= true;
102 $fh = @fopen
($file, 'r');
104 $this->filehandle
= &$fh;
105 $this->filename
= $file;
106 $this->writeable
= false;
108 return $this->set_error("$file: " . _("Open failed"));
114 /* Close the file and forget the filehandle */
116 @fclose
($this->filehandle
);
117 $this->filehandle
= 0;
118 $this->filename
= '';
119 $this->writable
= false;
122 /* Lock the datafile - try 20 times in 5 seconds */
124 for($i = 0 ; $i < 20 ; $i++
) {
125 if(flock($this->filehandle
, 2 +
4))
133 /* Lock the datafile */
135 return flock($this->filehandle
, 3);
138 /* Overwrite the file with data from $rows
139 * NOTE! Previous locks are broken by this function */
140 function overwrite(&$rows) {
142 $newfh = @fopen
($this->filename
.'.tmp', 'w');
145 return $this->set_error($this->filename
. '.tmp:' . _("Open failed"));
148 for($i = 0, $cnt=sizeof($rows) ; $i < $cnt ; $i++
) {
149 if(is_array($rows[$i])) {
150 for($j = 0, $cnt_part=count($rows[$i]) ; $j < $cnt_part ; $j++
) {
151 $rows[$i][$j] = $this->quotevalue($rows[$i][$j]);
153 $tmpwrite = @fwrite
($newfh, join('|', $rows[$i]) . "\n");
154 if ($tmpwrite == -1) {
155 return $this->set_error($this->filename
. '.tmp:' . _("Write failed"));
161 if (!@copy
($this->filename
. '.tmp' , $this->filename
)) {
162 return $this->set_error($this->filename
. ':' . _("Unable to update"));
164 @unlink
($this->filename
. '.tmp');
170 /* ========================== Public ======================== */
172 /* Search the file */
173 function search($expr) {
175 /* To be replaced by advanded search expression parsing */
176 if(is_array($expr)) { return; }
178 /* Make regexp from glob'ed expression
179 * May want to quote other special characters like (, ), -, [, ], etc. */
180 $expr = str_replace('?', '.', $expr);
181 $expr = str_replace('*', '.*', $expr);
187 @rewind
($this->filehandle
);
189 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
190 $line = join(' ', $row);
191 if(eregi($expr, $line)) {
192 array_push($res, array('nickname' => $row[0],
193 'name' => $row[1] . ' ' . $row[2],
194 'firstname' => $row[1],
195 'lastname' => $row[2],
198 'backend' => $this->bnum
,
199 'source' => &$this->sname
));
207 function lookup($alias) {
212 $alias = strtolower($alias);
215 @rewind
($this->filehandle
);
217 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
218 if(strtolower($row[0]) == $alias) {
219 return array('nickname' => $row[0],
220 'name' => $row[1] . ' ' . $row[2],
221 'firstname' => $row[1],
222 'lastname' => $row[2],
225 'backend' => $this->bnum
,
226 'source' => &$this->sname
);
233 /* List all addresses */
234 function list_addr() {
237 @rewind
($this->filehandle
);
239 while ($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
240 array_push($res, array('nickname' => $row[0],
241 'name' => $row[1] . ' ' . $row[2],
242 'firstname' => $row[1],
243 'lastname' => $row[2],
246 'backend' => $this->bnum
,
247 'source' => &$this->sname
));
253 function add($userdata) {
254 if(!$this->writeable
) {
255 return $this->set_error(_("Addressbook is read-only"));
257 /* See if user exists already */
258 $ret = $this->lookup($userdata['nickname']);
260 return $this->set_error(sprintf(_("User '%s' already exist"),
264 /* Here is the data to write */
265 $data = $this->quotevalue($userdata['nickname']) . '|' .
266 $this->quotevalue($userdata['firstname']) . '|' .
267 $this->quotevalue($userdata['lastname']) . '|' .
268 $this->quotevalue($userdata['email']) . '|' .
269 $this->quotevalue($userdata['label']);
271 /* Strip linefeeds */
272 $data = ereg_replace("[\r\n]", ' ', $data);
273 /* Add linefeed at end */
274 $data = $data . "\n";
276 /* Reopen file, just to be sure */
278 if(!$this->writeable
) {
279 return $this->set_error(_("Addressbook is read-only"));
284 return $this->set_error(_("Could not lock datafile"));
288 $r = fwrite($this->filehandle
, $data);
293 /* Test write result and exit if OK */
294 if($r > 0) return true;
297 $this->set_error(_("Write to addressbook failed"));
302 function remove($alias) {
303 if(!$this->writeable
) {
304 return $this->set_error(_("Addressbook is read-only"));
307 /* Lock the file to make sure we're the only process working
310 return $this->set_error(_("Could not lock datafile"));
313 /* Read file into memory, ignoring nicknames to delete */
314 @rewind
($this->filehandle
);
317 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
318 if(!in_array($row[0], $alias)) {
323 /* Write data back */
324 if(!$this->overwrite($rows)) {
334 function modify($alias, $userdata) {
335 if(!$this->writeable
) {
336 return $this->set_error(_("Addressbook is read-only"));
339 /* See if user exists */
340 $ret = $this->lookup($alias);
342 return $this->set_error(sprintf(_("User '%s' does not exist"),
346 /* Lock the file to make sure we're the only process working
349 return $this->set_error(_("Could not lock datafile"));
352 /* Read file into memory, modifying the data for the
353 * user identified by $alias */
355 @rewind
($this->filehandle
);
358 while($row = @fgetcsv
($this->filehandle
, 2048, '|')) {
359 if(strtolower($row[0]) != strtolower($alias)) {
362 $rows[$i++
] = array(0 => $userdata['nickname'],
363 1 => $userdata['firstname'],
364 2 => $userdata['lastname'],
365 3 => $userdata['email'],
366 4 => $userdata['label']);
370 /* Write data back */
371 if(!$this->overwrite($rows)) {
380 /* Function for quoting values before saving */
381 function quotevalue($value) {
382 /* Quote the field if it contains | or ". Double quotes need to
383 * be replaced with "" */
384 if(ereg("[|\"]", $value)) {
385 $value = '"' . str_replace('"', '""', $value) . '"';
390 } /* End of class abook_local_file */