| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * abook_local_file.php |
| 5 | * |
| 6 | * @copyright © 1999-2005 The SquirrelMail Project Team |
| 7 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| 8 | * @version $Id$ |
| 9 | * @package squirrelmail |
| 10 | * @subpackage addressbook |
| 11 | */ |
| 12 | |
| 13 | /** |
| 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. |
| 24 | * ? name => name of address book. |
| 25 | * ? detect_writeable => detect address book access permissions by |
| 26 | * checking file permissions. |
| 27 | * ? writeable => allow writing into address book. Used only when |
| 28 | * detect_writeable is set to false. |
| 29 | * ? listing => enable/disable listing |
| 30 | *</pre> |
| 31 | * NOTE. This class should not be used directly. Use the |
| 32 | * "AddressBook" class instead. |
| 33 | * @package squirrelmail |
| 34 | */ |
| 35 | class abook_local_file extends addressbook_backend { |
| 36 | /** |
| 37 | * Backend type |
| 38 | * @var string |
| 39 | */ |
| 40 | var $btype = 'local'; |
| 41 | /** |
| 42 | * Backend name |
| 43 | * @var string |
| 44 | */ |
| 45 | var $bname = 'local_file'; |
| 46 | |
| 47 | /** |
| 48 | * File used to store data |
| 49 | * @var string |
| 50 | */ |
| 51 | var $filename = ''; |
| 52 | /** |
| 53 | * File handle |
| 54 | * @var object |
| 55 | */ |
| 56 | var $filehandle = 0; |
| 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; |
| 74 | /** |
| 75 | * controls listing of address book |
| 76 | * @var bool |
| 77 | */ |
| 78 | var $listing = true; |
| 79 | /** |
| 80 | * Umask of the file |
| 81 | * @var string |
| 82 | */ |
| 83 | var $umask; |
| 84 | |
| 85 | /* ========================== Private ======================= */ |
| 86 | |
| 87 | /** |
| 88 | * Constructor |
| 89 | * @param array $param backend options |
| 90 | * @return bool |
| 91 | */ |
| 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 | |
| 107 | if(isset($param['create'])) { |
| 108 | $this->create = $param['create']; |
| 109 | } |
| 110 | if(isset($param['umask'])) { |
| 111 | $this->umask = $param['umask']; |
| 112 | } |
| 113 | if(isset($param['name'])) { |
| 114 | $this->sname = $param['name']; |
| 115 | } |
| 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 | } |
| 122 | if(isset($param['listing'])) { |
| 123 | $this->listing = $param['listing']; |
| 124 | } |
| 125 | |
| 126 | $this->open(true); |
| 127 | } else { |
| 128 | $this->set_error('Invalid argument to constructor'); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Open the addressbook file and store the file pointer. |
| 134 | * Use $file as the file to open, or the class' own |
| 135 | * filename property. If $param is empty and file is |
| 136 | * open, do nothing. |
| 137 | * @param bool $new is file already opened |
| 138 | * @return bool |
| 139 | */ |
| 140 | function open($new = false) { |
| 141 | $this->error = ''; |
| 142 | $file = $this->filename; |
| 143 | $create = $this->create; |
| 144 | $fopenmode = (($this->writeable && is_writable($file)) ? 'a+' : 'r'); |
| 145 | |
| 146 | /* Return true is file is open and $new is unset */ |
| 147 | if($this->filehandle && !$new) { |
| 148 | return true; |
| 149 | } |
| 150 | |
| 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 | } |
| 155 | |
| 156 | /* Close old file, if any */ |
| 157 | if($this->filehandle) { $this->close(); } |
| 158 | |
| 159 | umask($this->umask); |
| 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 | } |
| 168 | } else { |
| 169 | /* Open file. First try to open for reading and writing, |
| 170 | * but fall back to read only. */ |
| 171 | $fh = @fopen($file, 'a+'); |
| 172 | if($fh) { |
| 173 | $this->filehandle = &$fh; |
| 174 | $this->filename = $file; |
| 175 | $this->writeable = true; |
| 176 | } else { |
| 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 | } |
| 185 | } |
| 186 | } |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | /** Close the file and forget the filehandle */ |
| 191 | function close() { |
| 192 | @fclose($this->filehandle); |
| 193 | $this->filehandle = 0; |
| 194 | $this->filename = ''; |
| 195 | $this->writable = false; |
| 196 | } |
| 197 | |
| 198 | /** Lock the datafile - try 20 times in 5 seconds */ |
| 199 | function lock() { |
| 200 | for($i = 0 ; $i < 20 ; $i++) { |
| 201 | if(flock($this->filehandle, 2 + 4)) |
| 202 | return true; |
| 203 | else |
| 204 | usleep(250000); |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | /** Unlock the datafile */ |
| 210 | function unlock() { |
| 211 | return flock($this->filehandle, 3); |
| 212 | } |
| 213 | |
| 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 | */ |
| 220 | function overwrite(&$rows) { |
| 221 | $this->unlock(); |
| 222 | $newfh = @fopen($this->filename.'.tmp', 'w'); |
| 223 | |
| 224 | if(!$newfh) { |
| 225 | return $this->set_error($this->filename. '.tmp:' . _("Open failed")); |
| 226 | } |
| 227 | |
| 228 | for($i = 0, $cnt=sizeof($rows) ; $i < $cnt ; $i++) { |
| 229 | if(is_array($rows[$i])) { |
| 230 | for($j = 0, $cnt_part=count($rows[$i]) ; $j < $cnt_part ; $j++) { |
| 231 | $rows[$i][$j] = $this->quotevalue($rows[$i][$j]); |
| 232 | } |
| 233 | $tmpwrite = sq_fwrite($newfh, join('|', $rows[$i]) . "\n"); |
| 234 | if ($tmpwrite === FALSE) { |
| 235 | return $this->set_error($this->filename . '.tmp:' . _("Write failed")); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | fclose($newfh); |
| 241 | if (!@copy($this->filename . '.tmp' , $this->filename)) { |
| 242 | return $this->set_error($this->filename . ':' . _("Unable to update")); |
| 243 | } |
| 244 | @unlink($this->filename . '.tmp'); |
| 245 | $this->unlock(); |
| 246 | $this->open(true); |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | /* ========================== Public ======================== */ |
| 251 | |
| 252 | /** |
| 253 | * Search the file |
| 254 | * @param string $expr search expression |
| 255 | * @return array search results |
| 256 | */ |
| 257 | function search($expr) { |
| 258 | |
| 259 | /* To be replaced by advanded search expression parsing */ |
| 260 | if(is_array($expr)) { return; } |
| 261 | |
| 262 | // don't allow wide search when listing is disabled. |
| 263 | if ($expr=='*' && ! $this->listing) |
| 264 | return array(); |
| 265 | |
| 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); |
| 270 | |
| 271 | $res = array(); |
| 272 | if(!$this->open()) { |
| 273 | return false; |
| 274 | } |
| 275 | @rewind($this->filehandle); |
| 276 | |
| 277 | while ($row = @fgetcsv($this->filehandle, 2048, '|')) { |
| 278 | $line = join(' ', $row); |
| 279 | if(eregi($expr, $line)) { |
| 280 | array_push($res, array('nickname' => $row[0], |
| 281 | 'name' => $row[1] . ' ' . $row[2], |
| 282 | 'firstname' => $row[1], |
| 283 | 'lastname' => $row[2], |
| 284 | 'email' => $row[3], |
| 285 | 'label' => $row[4], |
| 286 | 'backend' => $this->bnum, |
| 287 | 'source' => &$this->sname)); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return $res; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Lookup alias |
| 296 | * @param string $alias alias |
| 297 | * @return array search results |
| 298 | */ |
| 299 | function lookup($alias) { |
| 300 | if(empty($alias)) { |
| 301 | return array(); |
| 302 | } |
| 303 | |
| 304 | $alias = strtolower($alias); |
| 305 | |
| 306 | $this->open(); |
| 307 | @rewind($this->filehandle); |
| 308 | |
| 309 | while ($row = @fgetcsv($this->filehandle, 2048, '|')) { |
| 310 | if(strtolower($row[0]) == $alias) { |
| 311 | return array('nickname' => $row[0], |
| 312 | 'name' => $row[1] . ' ' . $row[2], |
| 313 | 'firstname' => $row[1], |
| 314 | 'lastname' => $row[2], |
| 315 | 'email' => $row[3], |
| 316 | 'label' => $row[4], |
| 317 | 'backend' => $this->bnum, |
| 318 | 'source' => &$this->sname); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return array(); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * List all addresses |
| 327 | * @return array list of all addresses |
| 328 | */ |
| 329 | function list_addr() { |
| 330 | $res = array(); |
| 331 | |
| 332 | if(isset($this->listing) && !$this->listing) { |
| 333 | return array(); |
| 334 | } |
| 335 | |
| 336 | $this->open(); |
| 337 | @rewind($this->filehandle); |
| 338 | |
| 339 | while ($row = @fgetcsv($this->filehandle, 2048, '|')) { |
| 340 | array_push($res, array('nickname' => $row[0], |
| 341 | 'name' => $row[1] . ' ' . $row[2], |
| 342 | 'firstname' => $row[1], |
| 343 | 'lastname' => $row[2], |
| 344 | 'email' => $row[3], |
| 345 | 'label' => $row[4], |
| 346 | 'backend' => $this->bnum, |
| 347 | 'source' => &$this->sname)); |
| 348 | } |
| 349 | return $res; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Add address |
| 354 | * @param array $userdata new data |
| 355 | * @return bool |
| 356 | */ |
| 357 | function add($userdata) { |
| 358 | if(!$this->writeable) { |
| 359 | return $this->set_error(_("Addressbook is read-only")); |
| 360 | } |
| 361 | /* See if user exists already */ |
| 362 | $ret = $this->lookup($userdata['nickname']); |
| 363 | if(!empty($ret)) { |
| 364 | // i18n: don't use html formating in translation |
| 365 | return $this->set_error(sprintf(_("User \"%s\" already exists"),$ret['nickname'])); |
| 366 | } |
| 367 | |
| 368 | /* Here is the data to write */ |
| 369 | $data = $this->quotevalue($userdata['nickname']) . '|' . |
| 370 | $this->quotevalue($userdata['firstname']) . '|' . |
| 371 | $this->quotevalue($userdata['lastname']) . '|' . |
| 372 | $this->quotevalue($userdata['email']) . '|' . |
| 373 | $this->quotevalue($userdata['label']); |
| 374 | |
| 375 | /* Strip linefeeds */ |
| 376 | $data = ereg_replace("[\r\n]", ' ', $data); |
| 377 | /* Add linefeed at end */ |
| 378 | $data = $data . "\n"; |
| 379 | |
| 380 | /* Reopen file, just to be sure */ |
| 381 | $this->open(true); |
| 382 | if(!$this->writeable) { |
| 383 | return $this->set_error(_("Addressbook is read-only")); |
| 384 | } |
| 385 | |
| 386 | /* Lock the file */ |
| 387 | if(!$this->lock()) { |
| 388 | return $this->set_error(_("Could not lock datafile")); |
| 389 | } |
| 390 | |
| 391 | /* Write */ |
| 392 | $r = sq_fwrite($this->filehandle, $data); |
| 393 | |
| 394 | /* Unlock file */ |
| 395 | $this->unlock(); |
| 396 | |
| 397 | /* Test write result */ |
| 398 | if($r === FALSE) { |
| 399 | /* Fail */ |
| 400 | $this->set_error(_("Write to addressbook failed")); |
| 401 | return FALSE; |
| 402 | } |
| 403 | |
| 404 | return TRUE; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Delete address |
| 409 | * @param string $alias alias that has to be deleted |
| 410 | * @return bool |
| 411 | */ |
| 412 | function remove($alias) { |
| 413 | if(!$this->writeable) { |
| 414 | return $this->set_error(_("Addressbook is read-only")); |
| 415 | } |
| 416 | |
| 417 | /* Lock the file to make sure we're the only process working |
| 418 | * on it. */ |
| 419 | if(!$this->lock()) { |
| 420 | return $this->set_error(_("Could not lock datafile")); |
| 421 | } |
| 422 | |
| 423 | /* Read file into memory, ignoring nicknames to delete */ |
| 424 | @rewind($this->filehandle); |
| 425 | $i = 0; |
| 426 | $rows = array(); |
| 427 | while($row = @fgetcsv($this->filehandle, 2048, '|')) { |
| 428 | if(!in_array($row[0], $alias)) { |
| 429 | $rows[$i++] = $row; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /* Write data back */ |
| 434 | if(!$this->overwrite($rows)) { |
| 435 | $this->unlock(); |
| 436 | return false; |
| 437 | } |
| 438 | |
| 439 | $this->unlock(); |
| 440 | return true; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Modify address |
| 445 | * @param string $alias modified alias |
| 446 | * @param array $userdata new data |
| 447 | * @return bool true, if operation successful |
| 448 | */ |
| 449 | function modify($alias, $userdata) { |
| 450 | if(!$this->writeable) { |
| 451 | return $this->set_error(_("Addressbook is read-only")); |
| 452 | } |
| 453 | |
| 454 | /* See if user exists */ |
| 455 | $ret = $this->lookup($alias); |
| 456 | if(empty($ret)) { |
| 457 | // i18n: don't use html formating in translation |
| 458 | return $this->set_error(sprintf(_("User \"%s\" does not exist"),$alias)); |
| 459 | } |
| 460 | |
| 461 | /* Lock the file to make sure we're the only process working |
| 462 | * on it. */ |
| 463 | if(!$this->lock()) { |
| 464 | return $this->set_error(_("Could not lock datafile")); |
| 465 | } |
| 466 | |
| 467 | /* Read file into memory, modifying the data for the |
| 468 | * user identified by $alias */ |
| 469 | $this->open(true); |
| 470 | @rewind($this->filehandle); |
| 471 | $i = 0; |
| 472 | $rows = array(); |
| 473 | while($row = @fgetcsv($this->filehandle, 2048, '|')) { |
| 474 | if(strtolower($row[0]) != strtolower($alias)) { |
| 475 | $rows[$i++] = $row; |
| 476 | } else { |
| 477 | $rows[$i++] = array(0 => $userdata['nickname'], |
| 478 | 1 => $userdata['firstname'], |
| 479 | 2 => $userdata['lastname'], |
| 480 | 3 => $userdata['email'], |
| 481 | 4 => $userdata['label']); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /* Write data back */ |
| 486 | if(!$this->overwrite($rows)) { |
| 487 | $this->unlock(); |
| 488 | return false; |
| 489 | } |
| 490 | |
| 491 | $this->unlock(); |
| 492 | return true; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Function for quoting values before saving |
| 497 | * @param string $value string that has to be quoted |
| 498 | * @param string quoted string |
| 499 | */ |
| 500 | function quotevalue($value) { |
| 501 | /* Quote the field if it contains | or ". Double quotes need to |
| 502 | * be replaced with "" */ |
| 503 | if(ereg("[|\"]", $value)) { |
| 504 | $value = '"' . str_replace('"', '""', $value) . '"'; |
| 505 | } |
| 506 | return $value; |
| 507 | } |
| 508 | |
| 509 | } /* End of class abook_local_file */ |
| 510 | ?> |