Source for file abook_local_file.php
Documentation is available at abook_local_file.php
* @copyright © 1999-2006 The SquirrelMail Project Team
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version $Id: abook_local_file.php,v 1.48 2006/08/15 20:41:20 stevetruckstuff Exp $
* @subpackage addressbook
* Backend for address book as a pipe separated file
* Stores the address book in a local file
* An array with the following elements must be passed to
* the class constructor (elements marked ? are optional):
* filename => path to addressbook file
* ? create => if true: file is created if it does not exist.
* ? umask => umask set before opening file.
* ? name => name of address book.
* ? detect_writeable => detect address book access permissions by
* checking file permissions.
* ? writeable => allow writing into address book. Used only when
* detect_writeable is set to false.
* ? listing => enable/disable listing
* NOTE. This class should not be used directly. Use the
* "AddressBook" class instead.
* File used to store data
* Create file, if it not present
* Detect, if address book is writeable by checking file permisions
* Control write access to address book
* Option does not have any effect, if 'detect_writeable' is 'true'
* controls listing of address book
* Sets max entry size (number of bytes used for all address book fields
* (including escapes) + 4 delimiters + 1 linefeed)
/* ========================== Private ======================= */
* @param array $param backend options
$this->sname =
_("Personal address book");
if(empty($param['filename'])) {
return $this->set_error('Invalid parameters');
return $this->set_error($param['filename'] .
': '.
if(isset
($param['create'])) {
$this->create =
$param['create'];
if(isset
($param['umask'])) {
$this->umask =
$param['umask'];
if(isset
($param['name'])) {
$this->sname =
$param['name'];
if(isset
($param['detect_writeable'])) {
if(!empty($param['writeable'])) {
if(isset
($param['listing'])) {
$this->listing =
$param['listing'];
if(isset
($param['line_length']) &&
! empty($param['line_length'])) {
$this->set_error('Invalid argument to constructor');
* Open the addressbook file and store the file pointer.
* Use $file as the file to open, or the class' own
* filename property. If $param is empty and file is
* @param bool $new is file already opened
function open($new =
false) {
/* Return true is file is open and $new is unset */
/* Check that new file exitsts */
return $this->set_error("$file: " .
_("No such file or directory"));
/* Close old file, if any */
$fh =
@fopen($file,$fopenmode);
return $this->set_error("$file: " .
_("Open failed"));
/* Open file. First try to open for reading and writing,
* but fall back to read only. */
$fh =
@fopen($file, 'a+');
$fh =
@fopen($file, 'r');
return $this->set_error("$file: " .
_("Open failed"));
/** Close the file and forget the filehandle */
/** Lock the datafile - try 20 times in 5 seconds */
for($i =
0 ; $i <
20 ; $i++
) {
/** Unlock the datafile */
* Overwrite the file with data from $rows
* NOTE! Previous locks are broken by this function
* @param array $rows new data
for($i =
0, $cnt=
sizeof($rows) ; $i <
$cnt ; $i++
) {
for($j =
0, $cnt_part=
count($rows[$i]) ; $j <
$cnt_part ; $j++
) {
if ($tmpwrite ===
FALSE) {
/* ========================== Public ======================== */
* @param string $expr search expression
* @return array search results
/* To be replaced by advanded search expression parsing */
// don't allow wide search when listing is disabled.
if ($expr==
'*' &&
! $this->listing)
/* Make regexp from glob'ed expression
* May want to quote other special characters like (, ), -, [, ], etc. */
* address book is corrupted.
error_box(_("Address book is corrupted. Required fields are missing."));
$oTemplate->display('footer.tpl');
* TODO: regexp search is supported only in local_file backend.
* Do we check format of regexp or ignore errors?
// errors on eregi call are suppressed in order to prevent display of regexp compilation errors
if(@eregi($expr, $line)) {
'name' =>
$this->fullname($row[1], $row[2]),
'backend' =>
$this->bnum,
'source' =>
&$this->sname));
* @param string $alias alias
* @return array search results
* address book is corrupted.
error_box(_("Address book is corrupted. Required fields are missing."));
$oTemplate->display('footer.tpl');
return array('nickname' =>
$row[0],
'name' =>
$this->fullname($row[1], $row[2]),
'backend' =>
$this->bnum,
'source' =>
&$this->sname);
* @return array list of all addresses
* address book is corrupted. Don't be nice to people that
* violate address book formating.
error_box(_("Address book is corrupted. Required fields are missing."));
$oTemplate->display('footer.tpl');
'name' =>
$this->fullname($row[1], $row[2]),
'backend' =>
$this->bnum,
'source' =>
&$this->sname));
* @param array $userdata new data
function add($userdata) {
return $this->set_error(_("Address book is read-only"));
/* See if user exists already */
$ret =
$this->lookup($userdata['nickname']);
// i18n: don't use html formating in translation
/* Here is the data to write */
$data =
$this->quotevalue($userdata['nickname']) .
'|' .
$this->quotevalue((!empty($userdata['lastname'])?
$userdata['lastname']:
'')) .
'|' .
$this->quotevalue((!empty($userdata['label'])?
$userdata['label']:
''));
* Make sure that entry fits into allocated record space.
* One byte is reserved for linefeed
return $this->set_error(_("Address book entry is too big"));
/* Add linefeed at end */
/* Reopen file, just to be sure */
return $this->set_error(_("Address book is read-only"));
return $this->set_error(_("Could not lock datafile"));
$this->set_error(_("Write to address book failed"));
* @param string $alias alias that has to be deleted
return $this->set_error(_("Address book is read-only"));
/* Lock the file to make sure we're the only process working
return $this->set_error(_("Could not lock datafile"));
/* Read file into memory, ignoring nicknames to delete */
* @param string $alias modified alias
* @param array $userdata new data
* @return bool true, if operation successful
function modify($alias, $userdata) {
return $this->set_error(_("Address book is read-only"));
// i18n: don't use html formating in translation
/* If the alias changed, see if the new alias exists */
$ret =
$this->lookup($userdata['nickname']);
return $this->set_error(sprintf(_("User \"%s\" already exists"), $userdata['nickname']));
/* Lock the file to make sure we're the only process working
return $this->set_error(_("Could not lock datafile"));
/* calculate userdata size */
$data =
$this->quotevalue($userdata['nickname']) .
'|'
.
$this->quotevalue((!empty($userdata['lastname'])?
$userdata['lastname']:
'')) .
'|'
.
$this->quotevalue((!empty($userdata['label'])?
$userdata['label']:
''));
/* make sure that it fits into allocated space */
return $this->set_error(_("Address book entry is too big"));
/* Read file into memory, modifying the data for the
* user identified by $alias */
$rows[$i++
] =
array(0 =>
$userdata['nickname'],
1 =>
$userdata['firstname'],
2 =>
(!empty($userdata['lastname'])?
$userdata['lastname']:
''),
4 =>
(!empty($userdata['label'])?
$userdata['label']:
''));
* Function for quoting values before saving
* @param string $value string that has to be quoted
* @param string quoted string
/* Quote the field if it contains | or ". Double quotes need to
if(ereg("[|\"]", $value)) {
Documentation generated on Sat, 07 Oct 2006 16:08:37 +0300 by phpDocumentor 1.3.0RC6