Allow lookups by fields other than nickname; LDAP backend needs to have this implemen...
[squirrelmail.git] / functions / abook_local_file.php
CommitLineData
5100704d 1<?php
4b4abf93 2
35586184 3/**
4 * abook_local_file.php
5 *
4b5049de 6 * @copyright &copy; 1999-2007 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;
7311c377 84 /**
85 * Sets max entry size (number of bytes used for all address book fields
86 * (including escapes) + 4 delimiters + 1 linefeed)
87 * @var integer
88 * @since 1.5.2
89 */
90 var $line_length = 2048;
06b4facd 91
92 /* ========================== Private ======================= */
93
147e5af3 94 /**
95 * Constructor
96 * @param array $param backend options
97 * @return bool
98 */
06b4facd 99 function abook_local_file($param) {
232fb714 100 $this->sname = _("Personal Address Book");
06b4facd 101 $this->umask = Umask();
102
103 if(is_array($param)) {
104 if(empty($param['filename'])) {
105 return $this->set_error('Invalid parameters');
106 }
107 if(!is_string($param['filename'])) {
108 return $this->set_error($param['filename'] . ': '.
109 _("Not a file name"));
110 }
111
112 $this->filename = $param['filename'];
113
147e5af3 114 if(isset($param['create'])) {
115 $this->create = $param['create'];
06b4facd 116 }
117 if(isset($param['umask'])) {
118 $this->umask = $param['umask'];
119 }
4272758c 120 if(isset($param['name'])) {
06b4facd 121 $this->sname = $param['name'];
122 }
4272758c 123 if(isset($param['detect_writeable'])) {
124 $this->detect_writeable = $param['detect_writeable'];
125 }
126 if(!empty($param['writeable'])) {
127 $this->writeable = $param['writeable'];
128 }
e59a9c41 129 if(isset($param['listing'])) {
130 $this->listing = $param['listing'];
131 }
7311c377 132 if(isset($param['line_length']) && ! empty($param['line_length'])) {
133 $this->line_length = (int) $param['line_length'];
134 }
62f7daa5 135
06b4facd 136 $this->open(true);
137 } else {
138 $this->set_error('Invalid argument to constructor');
139 }
140 }
141
147e5af3 142 /**
143 * Open the addressbook file and store the file pointer.
62f7daa5 144 * Use $file as the file to open, or the class' own
145 * filename property. If $param is empty and file is
147e5af3 146 * open, do nothing.
147 * @param bool $new is file already opened
148 * @return bool
149 */
06b4facd 150 function open($new = false) {
151 $this->error = '';
152 $file = $this->filename;
153 $create = $this->create;
d5d43e57 154 $fopenmode = (($this->writeable && is_writable($file)) ? 'a+' : 'r');
62f7daa5 155
06b4facd 156 /* Return true is file is open and $new is unset */
157 if($this->filehandle && !$new) {
158 return true;
159 }
62f7daa5 160
06b4facd 161 /* Check that new file exitsts */
162 if((!(file_exists($file) && is_readable($file))) && !$create) {
163 return $this->set_error("$file: " . _("No such file or directory"));
164 }
62f7daa5 165
06b4facd 166 /* Close old file, if any */
167 if($this->filehandle) { $this->close(); }
62f7daa5 168
06b4facd 169 umask($this->umask);
4272758c 170 if (! $this->detect_writeable) {
171 $fh = @fopen($file,$fopenmode);
172 if ($fh) {
173 $this->filehandle = &$fh;
174 $this->filename = $file;
175 } else {
176 return $this->set_error("$file: " . _("Open failed"));
177 }
06b4facd 178 } else {
4272758c 179 /* Open file. First try to open for reading and writing,
180 * but fall back to read only. */
181 $fh = @fopen($file, 'a+');
06b4facd 182 if($fh) {
183 $this->filehandle = &$fh;
184 $this->filename = $file;
4272758c 185 $this->writeable = true;
06b4facd 186 } else {
4272758c 187 $fh = @fopen($file, 'r');
188 if($fh) {
189 $this->filehandle = &$fh;
190 $this->filename = $file;
191 $this->writeable = false;
192 } else {
193 return $this->set_error("$file: " . _("Open failed"));
194 }
06b4facd 195 }
196 }
197 return true;
198 }
199
147e5af3 200 /** Close the file and forget the filehandle */
06b4facd 201 function close() {
202 @fclose($this->filehandle);
203 $this->filehandle = 0;
204 $this->filename = '';
205 $this->writable = false;
206 }
207
147e5af3 208 /** Lock the datafile - try 20 times in 5 seconds */
06b4facd 209 function lock() {
210 for($i = 0 ; $i < 20 ; $i++) {
62f7daa5 211 if(flock($this->filehandle, 2 + 4))
06b4facd 212 return true;
213 else
214 usleep(250000);
215 }
216 return false;
217 }
218
147e5af3 219 /** Unlock the datafile */
06b4facd 220 function unlock() {
221 return flock($this->filehandle, 3);
222 }
223
147e5af3 224 /**
225 * Overwrite the file with data from $rows
226 * NOTE! Previous locks are broken by this function
227 * @param array $rows new data
228 * @return bool
229 */
06b4facd 230 function overwrite(&$rows) {
01265fba 231 $this->unlock();
dabef6fd 232 $newfh = @fopen($this->filename.'.tmp', 'w');
233
06b4facd 234 if(!$newfh) {
dabef6fd 235 return $this->set_error($this->filename. '.tmp:' . _("Open failed"));
06b4facd 236 }
62f7daa5 237
dabef6fd 238 for($i = 0, $cnt=sizeof($rows) ; $i < $cnt ; $i++) {
06b4facd 239 if(is_array($rows[$i])) {
dabef6fd 240 for($j = 0, $cnt_part=count($rows[$i]) ; $j < $cnt_part ; $j++) {
77ec28e9 241 $rows[$i][$j] = $this->quotevalue($rows[$i][$j]);
242 }
3ecad5e6 243 $tmpwrite = sq_fwrite($newfh, join('|', $rows[$i]) . "\n");
244 if ($tmpwrite === FALSE) {
dabef6fd 245 return $this->set_error($this->filename . '.tmp:' . _("Write failed"));
246 }
06b4facd 247 }
62f7daa5 248 }
06b4facd 249
250 fclose($newfh);
baa59994 251 if (!@copy($this->filename . '.tmp' , $this->filename)) {
dabef6fd 252 return $this->set_error($this->filename . ':' . _("Unable to update"));
baa59994 253 }
dabef6fd 254 @unlink($this->filename . '.tmp');
06b4facd 255 $this->unlock();
256 $this->open(true);
257 return true;
258 }
62f7daa5 259
06b4facd 260 /* ========================== Public ======================== */
62f7daa5 261
147e5af3 262 /**
263 * Search the file
264 * @param string $expr search expression
265 * @return array search results
266 */
06b4facd 267 function search($expr) {
268
269 /* To be replaced by advanded search expression parsing */
270 if(is_array($expr)) { return; }
62f7daa5 271
327e2d96 272 // don't allow wide search when listing is disabled.
273 if ($expr=='*' && ! $this->listing)
274 return array();
275
06b4facd 276 /* Make regexp from glob'ed expression
277 * May want to quote other special characters like (, ), -, [, ], etc. */
278 $expr = str_replace('?', '.', $expr);
279 $expr = str_replace('*', '.*', $expr);
62f7daa5 280
06b4facd 281 $res = array();
282 if(!$this->open()) {
283 return false;
284 }
285 @rewind($this->filehandle);
62f7daa5 286
7311c377 287 while ($row = @fgetcsv($this->filehandle, $this->line_length, '|')) {
288 if (count($row)<5) {
289 /**
290 * address book is corrupted.
291 */
292 global $oTemplate;
293 error_box(_("Address book is corrupted. Required fields are missing."));
294 $oTemplate->display('footer.tpl');
295 die();
296 } else {
297 $line = join(' ', $row);
298 /**
299 * TODO: regexp search is supported only in local_file backend.
300 * Do we check format of regexp or ignore errors?
301 */
302 // errors on eregi call are suppressed in order to prevent display of regexp compilation errors
303 if(@eregi($expr, $line)) {
304 array_push($res, array('nickname' => $row[0],
305 'name' => $this->fullname($row[1], $row[2]),
306 'firstname' => $row[1],
307 'lastname' => $row[2],
308 'email' => $row[3],
309 'label' => $row[4],
310 'backend' => $this->bnum,
311 'source' => &$this->sname));
312 }
06b4facd 313 }
314 }
62f7daa5 315
06b4facd 316 return $res;
317 }
62f7daa5 318
147e5af3 319 /**
503c7650 320 * Lookup an address by the indicated field.
321 *
322 * @param string $value The value to look up
323 * @param integer $field The field to look in, should be one
324 * of the SM_ABOOK_FIELD_* constants
325 * defined in include/constants.php
326 * (OPTIONAL; defaults to nickname field)
327 *
328 * @return array Array with lookup results when the value
329 * was found, an empty array if the value was
330 * not found.
331 *
147e5af3 332 */
503c7650 333 function lookup($value, $field=SM_ABOOK_FIELD_NICKNAME) {
334 if(empty($value)) {
06b4facd 335 return array();
336 }
337
503c7650 338 $value = strtolower($value);
62f7daa5 339
06b4facd 340 $this->open();
341 @rewind($this->filehandle);
62f7daa5 342
7311c377 343 while ($row = @fgetcsv($this->filehandle, $this->line_length, '|')) {
344 if (count($row)<5) {
345 /**
346 * address book is corrupted.
347 */
348 global $oTemplate;
349 error_box(_("Address book is corrupted. Required fields are missing."));
350 $oTemplate->display('footer.tpl');
351 die();
352 } else {
503c7650 353 if(strtolower($row[$field]) == $value) {
7311c377 354 return array('nickname' => $row[0],
355 'name' => $this->fullname($row[1], $row[2]),
356 'firstname' => $row[1],
357 'lastname' => $row[2],
358 'email' => $row[3],
359 'label' => $row[4],
360 'backend' => $this->bnum,
361 'source' => &$this->sname);
362 }
06b4facd 363 }
364 }
62f7daa5 365
06b4facd 366 return array();
367 }
368
147e5af3 369 /**
370 * List all addresses
371 * @return array list of all addresses
372 */
06b4facd 373 function list_addr() {
374 $res = array();
e59a9c41 375
376 if(isset($this->listing) && !$this->listing) {
377 return array();
378 }
379
06b4facd 380 $this->open();
381 @rewind($this->filehandle);
62f7daa5 382
7311c377 383 while ($row = @fgetcsv($this->filehandle, $this->line_length, '|')) {
384 if (count($row)<5) {
385 /**
386 * address book is corrupted. Don't be nice to people that
387 * violate address book formating.
388 */
389 global $oTemplate;
390 error_box(_("Address book is corrupted. Required fields are missing."));
391 $oTemplate->display('footer.tpl');
392 die();
393 } else {
394 array_push($res, array('nickname' => $row[0],
395 'name' => $this->fullname($row[1], $row[2]),
396 'firstname' => $row[1],
397 'lastname' => $row[2],
398 'email' => $row[3],
399 'label' => $row[4],
400 'backend' => $this->bnum,
401 'source' => &$this->sname));
402 }
06b4facd 403 }
404 return $res;
405 }
406
147e5af3 407 /**
408 * Add address
409 * @param array $userdata new data
410 * @return bool
411 */
06b4facd 412 function add($userdata) {
413 if(!$this->writeable) {
35235328 414 return $this->set_error(_("Address book is read-only"));
06b4facd 415 }
416 /* See if user exists already */
417 $ret = $this->lookup($userdata['nickname']);
418 if(!empty($ret)) {
2706a0b1 419 // i18n: don't use html formating in translation
420 return $this->set_error(sprintf(_("User \"%s\" already exists"),$ret['nickname']));
06b4facd 421 }
62f7daa5 422
06b4facd 423 /* Here is the data to write */
77ec28e9 424 $data = $this->quotevalue($userdata['nickname']) . '|' .
425 $this->quotevalue($userdata['firstname']) . '|' .
8419c13b 426 $this->quotevalue((!empty($userdata['lastname'])?$userdata['lastname']:'')) . '|' .
77ec28e9 427 $this->quotevalue($userdata['email']) . '|' .
8419c13b 428 $this->quotevalue((!empty($userdata['label'])?$userdata['label']:''));
77ec28e9 429
06b4facd 430 /* Strip linefeeds */
431 $data = ereg_replace("[\r\n]", ' ', $data);
7311c377 432
433 /**
434 * Make sure that entry fits into allocated record space.
435 * One byte is reserved for linefeed
436 */
437 if (strlen($data) >= $this->line_length) {
438 return $this->set_error(_("Address book entry is too big"));
439 }
440
06b4facd 441 /* Add linefeed at end */
442 $data = $data . "\n";
62f7daa5 443
06b4facd 444 /* Reopen file, just to be sure */
445 $this->open(true);
446 if(!$this->writeable) {
35235328 447 return $this->set_error(_("Address book is read-only"));
06b4facd 448 }
62f7daa5 449
06b4facd 450 /* Lock the file */
451 if(!$this->lock()) {
452 return $this->set_error(_("Could not lock datafile"));
453 }
62f7daa5 454
06b4facd 455 /* Write */
3ecad5e6 456 $r = sq_fwrite($this->filehandle, $data);
62f7daa5 457
06b4facd 458 /* Unlock file */
459 $this->unlock();
62f7daa5 460
3ecad5e6 461 /* Test write result */
462 if($r === FALSE) {
147e5af3 463 /* Fail */
35235328 464 $this->set_error(_("Write to address book failed"));
147e5af3 465 return FALSE;
466 }
62f7daa5 467
3ecad5e6 468 return TRUE;
06b4facd 469 }
470
147e5af3 471 /**
472 * Delete address
473 * @param string $alias alias that has to be deleted
474 * @return bool
475 */
06b4facd 476 function remove($alias) {
477 if(!$this->writeable) {
35235328 478 return $this->set_error(_("Address book is read-only"));
06b4facd 479 }
62f7daa5 480
06b4facd 481 /* Lock the file to make sure we're the only process working
482 * on it. */
483 if(!$this->lock()) {
484 return $this->set_error(_("Could not lock datafile"));
485 }
62f7daa5 486
06b4facd 487 /* Read file into memory, ignoring nicknames to delete */
488 @rewind($this->filehandle);
489 $i = 0;
490 $rows = array();
7311c377 491 while($row = @fgetcsv($this->filehandle, $this->line_length, '|')) {
06b4facd 492 if(!in_array($row[0], $alias)) {
493 $rows[$i++] = $row;
494 }
495 }
62f7daa5 496
06b4facd 497 /* Write data back */
498 if(!$this->overwrite($rows)) {
499 $this->unlock();
500 return false;
501 }
62f7daa5 502
06b4facd 503 $this->unlock();
504 return true;
505 }
506
147e5af3 507 /**
508 * Modify address
509 * @param string $alias modified alias
510 * @param array $userdata new data
511 * @return bool true, if operation successful
512 */
06b4facd 513 function modify($alias, $userdata) {
514 if(!$this->writeable) {
35235328 515 return $this->set_error(_("Address book is read-only"));
06b4facd 516 }
62f7daa5 517
06b4facd 518 /* See if user exists */
519 $ret = $this->lookup($alias);
520 if(empty($ret)) {
2706a0b1 521 // i18n: don't use html formating in translation
522 return $this->set_error(sprintf(_("User \"%s\" does not exist"),$alias));
06b4facd 523 }
849164a1 524
525 /* If the alias changed, see if the new alias exists */
526 if (strtolower($alias) != strtolower($userdata['nickname'])) {
527 $ret = $this->lookup($userdata['nickname']);
528 if (!empty($ret)) {
529 return $this->set_error(sprintf(_("User \"%s\" already exists"), $userdata['nickname']));
530 }
531 }
532
06b4facd 533 /* Lock the file to make sure we're the only process working
534 * on it. */
535 if(!$this->lock()) {
536 return $this->set_error(_("Could not lock datafile"));
537 }
62f7daa5 538
7311c377 539 /* calculate userdata size */
540 $data = $this->quotevalue($userdata['nickname']) . '|'
541 . $this->quotevalue($userdata['firstname']) . '|'
542 . $this->quotevalue((!empty($userdata['lastname'])?$userdata['lastname']:'')) . '|'
543 . $this->quotevalue($userdata['email']) . '|'
544 . $this->quotevalue((!empty($userdata['label'])?$userdata['label']:''));
545 /* make sure that it fits into allocated space */
546 if (strlen($data) >= $this->line_length) {
547 return $this->set_error(_("Address book entry is too big"));
548 }
549
62f7daa5 550 /* Read file into memory, modifying the data for the
06b4facd 551 * user identified by $alias */
552 $this->open(true);
553 @rewind($this->filehandle);
554 $i = 0;
555 $rows = array();
7311c377 556 while($row = @fgetcsv($this->filehandle, $this->line_length, '|')) {
06b4facd 557 if(strtolower($row[0]) != strtolower($alias)) {
558 $rows[$i++] = $row;
559 } else {
560 $rows[$i++] = array(0 => $userdata['nickname'],
561 1 => $userdata['firstname'],
8419c13b 562 2 => (!empty($userdata['lastname'])?$userdata['lastname']:''),
62f7daa5 563 3 => $userdata['email'],
8419c13b 564 4 => (!empty($userdata['label'])?$userdata['label']:''));
06b4facd 565 }
566 }
62f7daa5 567
06b4facd 568 /* Write data back */
569 if(!$this->overwrite($rows)) {
570 $this->unlock();
571 return false;
572 }
62f7daa5 573
06b4facd 574 $this->unlock();
575 return true;
576 }
62f7daa5 577
147e5af3 578 /**
579 * Function for quoting values before saving
580 * @param string $value string that has to be quoted
581 * @param string quoted string
582 */
77ec28e9 583 function quotevalue($value) {
584 /* Quote the field if it contains | or ". Double quotes need to
585 * be replaced with "" */
586 if(ereg("[|\"]", $value)) {
587 $value = '"' . str_replace('"', '""', $value) . '"';
588 }
589 return $value;
590 }
6b0fe53b 591}