must have functions/i18n.php available to use $squirrelmail_language further
[squirrelmail.git] / functions / abook_local_file.php
CommitLineData
5100704d 1<?php
4b4abf93 2
35586184 3/**
4 * abook_local_file.php
5 *
47ccfad4 6 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
4b4abf93 7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
a9d318b0 8 * @version $Id$
d6c32258 9 * @package squirrelmail
a9d318b0 10 * @subpackage addressbook
35586184 11 */
5100704d 12
d6c32258 13/**
147e5af3 14 * Backend for address book as a pipe separated file
15 *
16 * Stores the address book in a local file
17 *
18 * An array with the following elements must be passed to
19 * the class constructor (elements marked ? are optional):
20 *<pre>
21 * filename => path to addressbook file
22 * ? create => if true: file is created if it does not exist.
23 * ? umask => umask set before opening file.
675357d2 24 * ? name => name of address book.
91e0dccc 25 * ? detect_writeable => detect address book access permissions by
675357d2 26 * checking file permissions.
27 * ? writeable => allow writing into address book. Used only when
28 * detect_writeable is set to false.
e59a9c41 29 * ? listing => enable/disable listing
147e5af3 30 *</pre>
31 * NOTE. This class should not be used directly. Use the
32 * "AddressBook" class instead.
8f6f9ba5 33 * @package squirrelmail
d6c32258 34 */
35586184 35class abook_local_file extends addressbook_backend {
4272758c 36 /**
37 * Backend type
91e0dccc 38 * @var string
4272758c 39 */
06b4facd 40 var $btype = 'local';
4272758c 41 /**
42 * Backend name
43 * @var string
44 */
06b4facd 45 var $bname = 'local_file';
46
4272758c 47 /**
48 * File used to store data
49 * @var string
50 */
51 var $filename = '';
52 /**
53 * File handle
54 * @var object
55 */
06b4facd 56 var $filehandle = 0;
4272758c 57 /**
58 * Create file, if it not present
59 * @var bool
60 */
61 var $create = false;
62 /**
63 * Detect, if address book is writeable by checking file permisions
64 * @var bool
65 */
66 var $detect_writeable = true;
67 /**
68 * Control write access to address book
69 *
70 * Option does not have any effect, if 'detect_writeable' is 'true'
71 * @var bool
72 */
73 var $writeable = false;
e59a9c41 74 /**
75 * controls listing of address book
76 * @var bool
77 */
78 var $listing = true;
4272758c 79 /**
80 * Umask of the file
81 * @var string
82 */
06b4facd 83 var $umask;
84
85 /* ========================== Private ======================= */
86
147e5af3 87 /**
88 * Constructor
89 * @param array $param backend options
90 * @return bool
91 */
06b4facd 92 function abook_local_file($param) {
93 $this->sname = _("Personal address book");
94 $this->umask = Umask();
95
96 if(is_array($param)) {
97 if(empty($param['filename'])) {
98 return $this->set_error('Invalid parameters');
99 }
100 if(!is_string($param['filename'])) {
101 return $this->set_error($param['filename'] . ': '.
102 _("Not a file name"));
103 }
104
105 $this->filename = $param['filename'];
106
147e5af3 107 if(isset($param['create'])) {
108 $this->create = $param['create'];
06b4facd 109 }
110 if(isset($param['umask'])) {
111 $this->umask = $param['umask'];
112 }
4272758c 113 if(isset($param['name'])) {
06b4facd 114 $this->sname = $param['name'];
115 }
4272758c 116 if(isset($param['detect_writeable'])) {
117 $this->detect_writeable = $param['detect_writeable'];
118 }
119 if(!empty($param['writeable'])) {
120 $this->writeable = $param['writeable'];
121 }
e59a9c41 122 if(isset($param['listing'])) {
123 $this->listing = $param['listing'];
124 }
62f7daa5 125
06b4facd 126 $this->open(true);
127 } else {
128 $this->set_error('Invalid argument to constructor');
129 }
130 }
131
147e5af3 132 /**
133 * Open the addressbook file and store the file pointer.
62f7daa5 134 * Use $file as the file to open, or the class' own
135 * filename property. If $param is empty and file is
147e5af3 136 * open, do nothing.
137 * @param bool $new is file already opened
138 * @return bool
139 */
06b4facd 140 function open($new = false) {
141 $this->error = '';
142 $file = $this->filename;
143 $create = $this->create;
d5d43e57 144 $fopenmode = (($this->writeable && is_writable($file)) ? 'a+' : 'r');
62f7daa5 145
06b4facd 146 /* Return true is file is open and $new is unset */
147 if($this->filehandle && !$new) {
148 return true;
149 }
62f7daa5 150
06b4facd 151 /* Check that new file exitsts */
152 if((!(file_exists($file) && is_readable($file))) && !$create) {
153 return $this->set_error("$file: " . _("No such file or directory"));
154 }
62f7daa5 155
06b4facd 156 /* Close old file, if any */
157 if($this->filehandle) { $this->close(); }
62f7daa5 158
06b4facd 159 umask($this->umask);
4272758c 160 if (! $this->detect_writeable) {
161 $fh = @fopen($file,$fopenmode);
162 if ($fh) {
163 $this->filehandle = &$fh;
164 $this->filename = $file;
165 } else {
166 return $this->set_error("$file: " . _("Open failed"));
167 }
06b4facd 168 } else {
4272758c 169 /* Open file. First try to open for reading and writing,
170 * but fall back to read only. */
171 $fh = @fopen($file, 'a+');
06b4facd 172 if($fh) {
173 $this->filehandle = &$fh;
174 $this->filename = $file;
4272758c 175 $this->writeable = true;
06b4facd 176 } else {
4272758c 177 $fh = @fopen($file, 'r');
178 if($fh) {
179 $this->filehandle = &$fh;
180 $this->filename = $file;
181 $this->writeable = false;
182 } else {
183 return $this->set_error("$file: " . _("Open failed"));
184 }
06b4facd 185 }
186 }
187 return true;
188 }
189
147e5af3 190 /** Close the file and forget the filehandle */
06b4facd 191 function close() {
192 @fclose($this->filehandle);
193 $this->filehandle = 0;
194 $this->filename = '';
195 $this->writable = false;
196 }
197
147e5af3 198 /** Lock the datafile - try 20 times in 5 seconds */
06b4facd 199 function lock() {
200 for($i = 0 ; $i < 20 ; $i++) {
62f7daa5 201 if(flock($this->filehandle, 2 + 4))
06b4facd 202 return true;
203 else
204 usleep(250000);
205 }
206 return false;
207 }
208
147e5af3 209 /** Unlock the datafile */
06b4facd 210 function unlock() {
211 return flock($this->filehandle, 3);
212 }
213
147e5af3 214 /**
215 * Overwrite the file with data from $rows
216 * NOTE! Previous locks are broken by this function
217 * @param array $rows new data
218 * @return bool
219 */
06b4facd 220 function overwrite(&$rows) {
01265fba 221 $this->unlock();
dabef6fd 222 $newfh = @fopen($this->filename.'.tmp', 'w');
223
06b4facd 224 if(!$newfh) {
dabef6fd 225 return $this->set_error($this->filename. '.tmp:' . _("Open failed"));
06b4facd 226 }
62f7daa5 227
dabef6fd 228 for($i = 0, $cnt=sizeof($rows) ; $i < $cnt ; $i++) {
06b4facd 229 if(is_array($rows[$i])) {
dabef6fd 230 for($j = 0, $cnt_part=count($rows[$i]) ; $j < $cnt_part ; $j++) {
77ec28e9 231 $rows[$i][$j] = $this->quotevalue($rows[$i][$j]);
232 }
3ecad5e6 233 $tmpwrite = sq_fwrite($newfh, join('|', $rows[$i]) . "\n");
234 if ($tmpwrite === FALSE) {
dabef6fd 235 return $this->set_error($this->filename . '.tmp:' . _("Write failed"));
236 }
06b4facd 237 }
62f7daa5 238 }
06b4facd 239
240 fclose($newfh);
baa59994 241 if (!@copy($this->filename . '.tmp' , $this->filename)) {
dabef6fd 242 return $this->set_error($this->filename . ':' . _("Unable to update"));
baa59994 243 }
dabef6fd 244 @unlink($this->filename . '.tmp');
06b4facd 245 $this->unlock();
246 $this->open(true);
247 return true;
248 }
62f7daa5 249
06b4facd 250 /* ========================== Public ======================== */
62f7daa5 251
147e5af3 252 /**
253 * Search the file
254 * @param string $expr search expression
255 * @return array search results
256 */
06b4facd 257 function search($expr) {
258
259 /* To be replaced by advanded search expression parsing */
260 if(is_array($expr)) { return; }
62f7daa5 261
327e2d96 262 // don't allow wide search when listing is disabled.
263 if ($expr=='*' && ! $this->listing)
264 return array();
265
06b4facd 266 /* Make regexp from glob'ed expression
267 * May want to quote other special characters like (, ), -, [, ], etc. */
268 $expr = str_replace('?', '.', $expr);
269 $expr = str_replace('*', '.*', $expr);
62f7daa5 270
06b4facd 271 $res = array();
272 if(!$this->open()) {
273 return false;
274 }
275 @rewind($this->filehandle);
62f7daa5 276
06b4facd 277 while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
278 $line = join(' ', $row);
7fc4b14f 279 /**
280 * TODO: regexp search is supported only in local_file backend.
281 * Do we check format of regexp or ignore errors?
282 */
283 // errors on eregi call are suppressed in order to prevent display of regexp compilation errors
284 if(@eregi($expr, $line)) {
06b4facd 285 array_push($res, array('nickname' => $row[0],
286 'name' => $row[1] . ' ' . $row[2],
287 'firstname' => $row[1],
288 'lastname' => $row[2],
289 'email' => $row[3],
290 'label' => $row[4],
291 'backend' => $this->bnum,
292 'source' => &$this->sname));
293 }
294 }
62f7daa5 295
06b4facd 296 return $res;
297 }
62f7daa5 298
147e5af3 299 /**
300 * Lookup alias
301 * @param string $alias alias
302 * @return array search results
303 */
06b4facd 304 function lookup($alias) {
305 if(empty($alias)) {
306 return array();
307 }
308
309 $alias = strtolower($alias);
62f7daa5 310
06b4facd 311 $this->open();
312 @rewind($this->filehandle);
62f7daa5 313
06b4facd 314 while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
315 if(strtolower($row[0]) == $alias) {
316 return array('nickname' => $row[0],
317 'name' => $row[1] . ' ' . $row[2],
318 'firstname' => $row[1],
319 'lastname' => $row[2],
320 'email' => $row[3],
321 'label' => $row[4],
322 'backend' => $this->bnum,
323 'source' => &$this->sname);
324 }
325 }
62f7daa5 326
06b4facd 327 return array();
328 }
329
147e5af3 330 /**
331 * List all addresses
332 * @return array list of all addresses
333 */
06b4facd 334 function list_addr() {
335 $res = array();
e59a9c41 336
337 if(isset($this->listing) && !$this->listing) {
338 return array();
339 }
340
06b4facd 341 $this->open();
342 @rewind($this->filehandle);
62f7daa5 343
06b4facd 344 while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
345 array_push($res, array('nickname' => $row[0],
346 'name' => $row[1] . ' ' . $row[2],
347 'firstname' => $row[1],
348 'lastname' => $row[2],
349 'email' => $row[3],
350 'label' => $row[4],
351 'backend' => $this->bnum,
352 'source' => &$this->sname));
353 }
354 return $res;
355 }
356
147e5af3 357 /**
358 * Add address
359 * @param array $userdata new data
360 * @return bool
361 */
06b4facd 362 function add($userdata) {
363 if(!$this->writeable) {
364 return $this->set_error(_("Addressbook is read-only"));
365 }
366 /* See if user exists already */
367 $ret = $this->lookup($userdata['nickname']);
368 if(!empty($ret)) {
2706a0b1 369 // i18n: don't use html formating in translation
370 return $this->set_error(sprintf(_("User \"%s\" already exists"),$ret['nickname']));
06b4facd 371 }
62f7daa5 372
06b4facd 373 /* Here is the data to write */
77ec28e9 374 $data = $this->quotevalue($userdata['nickname']) . '|' .
375 $this->quotevalue($userdata['firstname']) . '|' .
8419c13b 376 $this->quotevalue((!empty($userdata['lastname'])?$userdata['lastname']:'')) . '|' .
77ec28e9 377 $this->quotevalue($userdata['email']) . '|' .
8419c13b 378 $this->quotevalue((!empty($userdata['label'])?$userdata['label']:''));
77ec28e9 379
06b4facd 380 /* Strip linefeeds */
381 $data = ereg_replace("[\r\n]", ' ', $data);
382 /* Add linefeed at end */
383 $data = $data . "\n";
62f7daa5 384
06b4facd 385 /* Reopen file, just to be sure */
386 $this->open(true);
387 if(!$this->writeable) {
388 return $this->set_error(_("Addressbook is read-only"));
389 }
62f7daa5 390
06b4facd 391 /* Lock the file */
392 if(!$this->lock()) {
393 return $this->set_error(_("Could not lock datafile"));
394 }
62f7daa5 395
06b4facd 396 /* Write */
3ecad5e6 397 $r = sq_fwrite($this->filehandle, $data);
62f7daa5 398
06b4facd 399 /* Unlock file */
400 $this->unlock();
62f7daa5 401
3ecad5e6 402 /* Test write result */
403 if($r === FALSE) {
147e5af3 404 /* Fail */
405 $this->set_error(_("Write to addressbook failed"));
406 return FALSE;
407 }
62f7daa5 408
3ecad5e6 409 return TRUE;
06b4facd 410 }
411
147e5af3 412 /**
413 * Delete address
414 * @param string $alias alias that has to be deleted
415 * @return bool
416 */
06b4facd 417 function remove($alias) {
418 if(!$this->writeable) {
419 return $this->set_error(_("Addressbook is read-only"));
420 }
62f7daa5 421
06b4facd 422 /* Lock the file to make sure we're the only process working
423 * on it. */
424 if(!$this->lock()) {
425 return $this->set_error(_("Could not lock datafile"));
426 }
62f7daa5 427
06b4facd 428 /* Read file into memory, ignoring nicknames to delete */
429 @rewind($this->filehandle);
430 $i = 0;
431 $rows = array();
432 while($row = @fgetcsv($this->filehandle, 2048, '|')) {
433 if(!in_array($row[0], $alias)) {
434 $rows[$i++] = $row;
435 }
436 }
62f7daa5 437
06b4facd 438 /* Write data back */
439 if(!$this->overwrite($rows)) {
440 $this->unlock();
441 return false;
442 }
62f7daa5 443
06b4facd 444 $this->unlock();
445 return true;
446 }
447
147e5af3 448 /**
449 * Modify address
450 * @param string $alias modified alias
451 * @param array $userdata new data
452 * @return bool true, if operation successful
453 */
06b4facd 454 function modify($alias, $userdata) {
455 if(!$this->writeable) {
456 return $this->set_error(_("Addressbook is read-only"));
457 }
62f7daa5 458
06b4facd 459 /* See if user exists */
460 $ret = $this->lookup($alias);
461 if(empty($ret)) {
2706a0b1 462 // i18n: don't use html formating in translation
463 return $this->set_error(sprintf(_("User \"%s\" does not exist"),$alias));
06b4facd 464 }
62f7daa5 465
06b4facd 466 /* Lock the file to make sure we're the only process working
467 * on it. */
468 if(!$this->lock()) {
469 return $this->set_error(_("Could not lock datafile"));
470 }
62f7daa5 471
472 /* Read file into memory, modifying the data for the
06b4facd 473 * user identified by $alias */
474 $this->open(true);
475 @rewind($this->filehandle);
476 $i = 0;
477 $rows = array();
478 while($row = @fgetcsv($this->filehandle, 2048, '|')) {
479 if(strtolower($row[0]) != strtolower($alias)) {
480 $rows[$i++] = $row;
481 } else {
482 $rows[$i++] = array(0 => $userdata['nickname'],
483 1 => $userdata['firstname'],
8419c13b 484 2 => (!empty($userdata['lastname'])?$userdata['lastname']:''),
62f7daa5 485 3 => $userdata['email'],
8419c13b 486 4 => (!empty($userdata['label'])?$userdata['label']:''));
06b4facd 487 }
488 }
62f7daa5 489
06b4facd 490 /* Write data back */
491 if(!$this->overwrite($rows)) {
492 $this->unlock();
493 return false;
494 }
62f7daa5 495
06b4facd 496 $this->unlock();
497 return true;
498 }
62f7daa5 499
147e5af3 500 /**
501 * Function for quoting values before saving
502 * @param string $value string that has to be quoted
503 * @param string quoted string
504 */
77ec28e9 505 function quotevalue($value) {
506 /* Quote the field if it contains | or ". Double quotes need to
507 * be replaced with "" */
508 if(ereg("[|\"]", $value)) {
509 $value = '"' . str_replace('"', '""', $value) . '"';
510 }
511 return $value;
512 }
513
06b4facd 514} /* End of class abook_local_file */
8419c13b 515?>