* $Id$
*/
-/*****************************************************************/
-/*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
-/*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
-/*** + Base level indent should begin at left margin, as ***/
-/*** the first line of the class definition below. ***/
-/*** + All identation should consist of four space blocks ***/
-/*** + Tab characters are evil. ***/
-/*** + all comments should use "slash-star ... star-slash" ***/
-/*** style -- no pound characters, no slash-slash style ***/
-/*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
-/*** ALWAYS USE { AND } CHARACTERS!!! ***/
-/*** + Please use ' instead of ", when possible. Note " ***/
-/*** should always be used in _( ) function calls. ***/
-/*** Thank you for your help making the SM code more readable. ***/
-/*****************************************************************/
-
class abook_global_file extends addressbook_backend {
var $btype = 'local';
var $bname = 'global_file';
- var $filehandle = 0;
-
- // ========================== Private =======================
-
- // Constructor
- function abook_global_file() {
- global $address_book_global_filename;
- $this->global_filename = $address_book_global_filename;
-
- $this->sname = _("Global address book");
-
- $this->open(true);
- }
-
- // 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
- // open, do nothing.
- function open($new = false) {
- $this->error = '';
-
- // Return true is file is open and $new is unset
- if($this->filehandle && !$new)
- return true;
-
- // Check that new file exists
- if (! file_exists($this->global_filename) ||
- ! is_readable($this->global_filename))
- return $this->set_error($this->global_filename . ': ' .
- _("No such file or directory"));
-
- // Close old file, if any
- if ($this->filehandle) $this->close();
-
- // Open file, read only.
- $fh = @fopen($this->global_filename, 'r');
- $this->writeable = false;
- if(! $fh)
- return $this->set_error($this->global_filename . ': ' .
- _("Open failed"));
-
- $this->filehandle = &$fh;
- return true;
- }
-
- // Close the file and forget the filehandle
- function close() {
- @fclose($this->filehandle);
- $this->filehandle = 0;
- $this->global_filename = '';
- $this->writable = false;
- }
-
- // ========================== Public ========================
-
- // Search the file
- function search($expr) {
-
- // To be replaced by advanded search expression parsing
- if(is_array($expr)) return;
-
- // Make regexp from glob'ed expression
- // May want to quote other special characters like (, ), -, [, ], etc.
- $expr = str_replace('?', '.', $expr);
- $expr = str_replace('*', '.*', $expr);
-
- $res = array();
- if(!$this->open())
- return false;
-
- @rewind($this->filehandle);
-
- while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
- $line = join(' ', $row);
- if (eregi($expr, $line)) {
- $res[] = array('nickname' => $row[0],
- 'name' => $row[1] . ' ' . $row[2],
- 'firstname' => $row[1],
- 'lastname' => $row[2],
- 'email' => $row[3],
- 'label' => $row[4],
- 'backend' => $this->bnum,
- 'source' => &$this->sname);
- }
- }
-
- return $res;
- }
-
- // Lookup alias
- function lookup($alias) {
- if (empty($alias))
- return array();
-
- $alias = strtolower($alias);
-
- $this->open();
- @rewind($this->filehandle);
-
- while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
- if (strtolower($row[0]) == $alias) {
- return array('nickname' => $row[0],
- 'name' => $row[1] . ' ' . $row[2],
- 'firstname' => $row[1],
- 'lastname' => $row[2],
- 'email' => $row[3],
- 'label' => $row[4],
- 'backend' => $this->bnum,
- 'source' => &$this->sname);
- }
- }
-
- return array();
- }
-
- // List all addresses
- function list_addr() {
- $res = array();
- $this->open();
- @rewind($this->filehandle);
-
- while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
- $res[] = array('nickname' => $row[0],
- 'name' => $row[1] . ' ' . $row[2],
- 'firstname' => $row[1],
- 'lastname' => $row[2],
- 'email' => $row[3],
- 'label' => $row[4],
- 'backend' => $this->bnum,
- 'source' => &$this->sname);
- }
- return $res;
- }
-
- // Add address
- function add($userdata) {
- $this->set_error(_("Can not modify global address book"));
- return false;
- }
-
- // Delete address
- function remove($alias) {
- $this->set_error(_("Can not modify global address book"));
- return false;
- }
-
- // Modify address
- function modify($alias, $userdata) {
- $this->set_error(_("Can not modify global address book"));
- return false;
- }
+ var $filehandle = 0;
+
+ /* ========================== Private ======================= */
+
+ /* Constructor */
+ function abook_global_file() {
+ global $address_book_global_filename;
+ $this->global_filename = $address_book_global_filename;
+
+ $this->sname = _("Global address book");
+
+ $this->open(true);
+ }
+
+ /* 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
+ * open, do nothing. */
+ function open($new = false) {
+ $this->error = '';
+
+ /* Return true is file is open and $new is unset */
+ if($this->filehandle && !$new) {
+ return true;
+ }
+
+ /* Check that new file exists */
+ if (! file_exists($this->global_filename) ||
+ ! is_readable($this->global_filename)) {
+ return $this->set_error($this->global_filename . ': ' .
+ _("No such file or directory"));
+ }
+
+ /* Close old file, if any */
+ if ($this->filehandle) {
+ $this->close();
+ }
+
+ /* Open file, read only. */
+ $fh = @fopen($this->global_filename, 'r');
+ $this->writeable = false;
+ if(! $fh) {
+ return $this->set_error($this->global_filename . ': ' .
+ _("Open failed"));
+ }
+
+ $this->filehandle = &$fh;
+ return true;
+ }
+
+ /* Close the file and forget the filehandle */
+ function close() {
+ @fclose($this->filehandle);
+ $this->filehandle = 0;
+ $this->global_filename = '';
+ $this->writable = false;
+ }
+
+ /* ========================== Public ======================== */
+
+ /* Search the file */
+ function search($expr) {
+
+ /* To be replaced by advanded search expression parsing */
+ if(is_array($expr)) {
+ return;
+ }
+
+ /* Make regexp from glob'ed expression
+ * May want to quote other special characters like (, ), -, [, ], etc. */
+ $expr = str_replace('?', '.', $expr);
+ $expr = str_replace('*', '.*', $expr);
+
+ $res = array();
+ if(!$this->open()) {
+ return false;
+ }
+
+ @rewind($this->filehandle);
+
+ while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
+ $line = join(' ', $row);
+ if (eregi($expr, $line)) {
+ $res[] = array('nickname' => $row[0],
+ 'name' => $row[1] . ' ' . $row[2],
+ 'firstname' => $row[1],
+ 'lastname' => $row[2],
+ 'email' => $row[3],
+ 'label' => $row[4],
+ 'backend' => $this->bnum,
+ 'source' => &$this->sname);
+ }
+ }
+
+ return $res;
+ }
+
+ /* Lookup alias */
+ function lookup($alias) {
+ if (empty($alias)) {
+ return array();
+ }
+
+ $alias = strtolower($alias);
+
+ $this->open();
+ @rewind($this->filehandle);
+
+ while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
+ if (strtolower($row[0]) == $alias) {
+ return array('nickname' => $row[0],
+ 'name' => $row[1] . ' ' . $row[2],
+ 'firstname' => $row[1],
+ 'lastname' => $row[2],
+ 'email' => $row[3],
+ 'label' => $row[4],
+ 'backend' => $this->bnum,
+ 'source' => &$this->sname);
+ }
+ }
+
+ return array();
+ }
+
+ /* List all addresses */
+ function list_addr() {
+ $res = array();
+ $this->open();
+ @rewind($this->filehandle);
+
+ while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
+ $res[] = array('nickname' => $row[0],
+ 'name' => $row[1] . ' ' . $row[2],
+ 'firstname' => $row[1],
+ 'lastname' => $row[2],
+ 'email' => $row[3],
+ 'label' => $row[4],
+ 'backend' => $this->bnum,
+ 'source' => &$this->sname);
+ }
+ return $res;
+ }
+
+ /* Add address */
+ function add($userdata) {
+ $this->set_error(_("Can not modify global address book"));
+ return false;
+ }
+
+ /* Delete address */
+ function remove($alias) {
+ $this->set_error(_("Can not modify global address book"));
+ return false;
+ }
+
+ /* Modify address */
+ function modify($alias, $userdata) {
+ $this->set_error(_("Can not modify global address book"));
+ return false;
+ }
- } // End of class abook_local_file
+} /* End of class abook_local_file */
?>
* $Id$
*/
-/*****************************************************************/
-/*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
-/*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
-/*** + Base level indent should begin at left margin, as ***/
-/*** the first line of the class definition below. ***/
-/*** + All identation should consist of four space blocks ***/
-/*** + Tab characters are evil. ***/
-/*** + all comments should use "slash-star ... star-slash" ***/
-/*** style -- no pound characters, no slash-slash style ***/
-/*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
-/*** ALWAYS USE { AND } CHARACTERS!!! ***/
-/*** + Please use ' instead of ", when possible. Note " ***/
-/*** should always be used in _( ) function calls. ***/
-/*** Thank you for your help making the SM code more readable. ***/
-/*****************************************************************/
-
class abook_ldap_server extends addressbook_backend {
- var $btype = 'remote';
- var $bname = 'ldap_server';
-
- // Parameters changed by class
- var $sname = 'LDAP'; // Service name
- var $server = ''; // LDAP server name
- var $port = 389; // LDAP server port
- var $basedn = ''; // LDAP base DN
- var $charset = 'utf-8'; // LDAP server charset
- var $linkid = false; // PHP LDAP link ID
- var $bound = false; // True if LDAP server is bound
- var $maxrows = 250; // Max rows in result
- var $timeout = 30; // Timeout for LDAP operations (in seconds)
-
- // Constructor. Connects to database
- function abook_ldap_server($param) {
- if(!function_exists('ldap_connect')) {
- $this->set_error('LDAP support missing from PHP');
- return;
- }
- if(is_array($param)) {
- $this->server = $param['host'];
- $this->basedn = $param['base'];
- if(!empty($param['port']))
- $this->port = $param['port'];
- if(!empty($param['charset']))
- $this->charset = strtolower($param['charset']);
- if(isset($param['maxrows']))
- $this->maxrows = $param['maxrows'];
- if(isset($param['timeout']))
- $this->timeout = $param['timeout'];
- if(empty($param['name']))
- $this->sname = 'LDAP: ' . $param['host'];
- else
- $this->sname = $param['name'];
-
- $this->open(true);
- } else {
- $this->set_error('Invalid argument to constructor');
- }
- }
-
-
- // Open the LDAP server. New connection if $new is true
- function open($new = false) {
- $this->error = '';
-
- // Connection is already open
- if($this->linkid != false && !$new)
- return true;
-
- $this->linkid = @ldap_connect($this->server, $this->port);
- if(!$this->linkid)
- if(function_exists('ldap_error'))
- return $this->set_error(ldap_error($this->linkid));
- else
- return $this->set_error('ldap_connect failed');
-
- if(!@ldap_bind($this->linkid))
- if(function_exists('ldap_error'))
- return $this->set_error(ldap_error($this->linkid));
- else
- return $this->set_error('ldap_bind failed');
-
- $this->bound = true;
-
- return true;
- }
-
-
- // Encode iso8859-1 string to the charset used by this LDAP server
- function charset_encode($str) {
- if($this->charset == 'utf-8') {
- if(function_exists('utf8_encode'))
- return utf8_encode($str);
- else
- return $str;
- } else {
- return $str;
- }
- }
-
-
- // Decode from charset used by this LDAP server to iso8859-1
- function charset_decode($str) {
- if($this->charset == 'utf-8') {
- if(function_exists('utf8_decode'))
- return utf8_decode($str);
- else
- return $str;
- } else {
- return $str;
- }
- }
-
-
- // ========================== Public ========================
-
- // Search the LDAP server
- function search($expr) {
-
- // To be replaced by advanded search expression parsing
- if(is_array($expr)) return false;
-
- // Encode the expression
- $expr = $this->charset_encode($expr);
- if(strstr($expr, "*") === false)
- $expr = "*$expr*";
- $expression = "cn=$expr";
-
- // Make sure connection is there
- if(!$this->open())
- return false;
-
- // Do the search. Use improved ldap_search() if PHP version is
- // 4.0.2 or newer.
- if(sqCheckPHPVersion(4, 0, 2)) {
- $sret = @ldap_search($this->linkid, $this->basedn, $expression,
- array('dn', 'o', 'ou', 'sn', 'givenname',
- 'cn', 'mail', 'telephonenumber'),
- 0, $this->maxrows, $this->timeout);
- } else {
- $sret = @ldap_search($this->linkid, $this->basedn, $expression,
- array('dn', 'o', 'ou', 'sn', 'givenname',
- 'cn', 'mail', 'telephonenumber'));
- }
-
- // Should get error from server using the ldap_error() function,
- // but it only exist in the PHP LDAP documentation.
- if(!$sret)
- if(function_exists('ldap_error'))
- return $this->set_error(ldap_error($this->linkid));
- else
- return $this->set_error('ldap_search failed');
-
- if(@ldap_count_entries($this->linkid, $sret) <= 0)
- return array();
-
- // Get results
- $ret = array();
- $returned_rows = 0;
- $res = @ldap_get_entries($this->linkid, $sret);
- for($i = 0 ; $i < $res['count'] ; $i++) {
- $row = $res[$i];
-
- // Extract data common for all e-mail addresses
- // of an object. Use only the first name
- $nickname = $this->charset_decode($row['dn']);
- $fullname = $this->charset_decode($row['cn'][0]);
-
- if(empty($row['telephonenumber'][0])) $phone = '';
- else $phone = $this->charset_decode($row['telephonenumber'][0]);
-
- if(!empty($row['ou'][0]))
- $label = $this->charset_decode($row['ou'][0]);
- else if(!empty($row['o'][0]))
- $label = $this->charset_decode($row['o'][0]);
- else
- $label = '';
-
- if(empty($row['givenname'][0])) $firstname = '';
- else $firstname = $this->charset_decode($row['givenname'][0]);
-
- if(empty($row['sn'][0])) $surname = '';
- else $surname = $this->charset_decode($row['sn'][0]);
-
- // Add one row to result for each e-mail address
- if(isset($row['mail']['count'])) {
- for($j = 0 ; $j < $row['mail']['count'] ; $j++) {
- array_push($ret, array('nickname' => $nickname,
- 'name' => $fullname,
- 'firstname' => $firstname,
- 'lastname' => $surname,
- 'email' => $row['mail'][$j],
- 'label' => $label,
- 'phone' => $phone,
- 'backend' => $this->bnum,
- 'source' => &$this->sname));
-
- // Limit number of hits
- $returned_rows++;
- if(($returned_rows >= $this->maxrows) &&
- ($this->maxrows > 0) ) {
- ldap_free_result($sret);
- return $ret;
- }
-
- } // for($j ...)
-
- } // isset($row['mail']['count'])
-
- }
-
- ldap_free_result($sret);
- return $ret;
- } // end search()
-
-
- // If you run a tiny LDAP server and you want the "List All" button
- // to show EVERYONE, then uncomment this tiny block of code:
- //
- // function list_addr() {
- // return $this->search("*");
- // }
- //
- // Careful with this -- it could get quite large for big sites.
- }
-?>
+ var $btype = 'remote';
+ var $bname = 'ldap_server';
+
+ /* Parameters changed by class */
+ var $sname = 'LDAP'; /* Service name */
+ var $server = ''; /* LDAP server name */
+ var $port = 389; /* LDAP server port */
+ var $basedn = ''; /* LDAP base DN */
+ var $charset = 'utf-8'; /* LDAP server charset */
+ var $linkid = false; /* PHP LDAP link ID */
+ var $bound = false; /* True if LDAP server is bound */
+ var $maxrows = 250; /* Max rows in result */
+ var $timeout = 30; /* Timeout for LDAP operations (in seconds) */
+
+ /* Constructor. Connects to database */
+ function abook_ldap_server($param) {
+ if(!function_exists('ldap_connect')) {
+ $this->set_error('LDAP support missing from PHP');
+ return;
+ }
+ if(is_array($param)) {
+ $this->server = $param['host'];
+ $this->basedn = $param['base'];
+ if(!empty($param['port'])) {
+ $this->port = $param['port'];
+ }
+ if(!empty($param['charset'])) {
+ $this->charset = strtolower($param['charset']);
+ }
+ if(isset($param['maxrows'])) {
+ $this->maxrows = $param['maxrows'];
+ }
+ if(isset($param['timeout'])) {
+ $this->timeout = $param['timeout'];
+ }
+ if(empty($param['name'])) {
+ $this->sname = 'LDAP: ' . $param['host'];
+ }
+ else {
+ $this->sname = $param['name'];
+ }
+
+ $this->open(true);
+ } else {
+ $this->set_error('Invalid argument to constructor');
+ }
+ }
+
+
+ /* Open the LDAP server. New connection if $new is true */
+ function open($new = false) {
+ $this->error = '';
+
+ /* Connection is already open */
+ if($this->linkid != false && !$new) {
+ return true;
+ }
+
+ $this->linkid = @ldap_connect($this->server, $this->port);
+ if(!$this->linkid) {
+ if(function_exists('ldap_error')) {
+ return $this->set_error(ldap_error($this->linkid));
+ } else {
+ return $this->set_error('ldap_connect failed');
+ }
+ }
+
+ if(!@ldap_bind($this->linkid)) {
+ if(function_exists('ldap_error')) {
+ return $this->set_error(ldap_error($this->linkid));
+ } else {
+ return $this->set_error('ldap_bind failed');
+ }
+ }
+
+ $this->bound = true;
+
+ return true;
+ }
+
+
+ /* Encode iso8859-1 string to the charset used by this LDAP server */
+ function charset_encode($str) {
+ if($this->charset == 'utf-8') {
+ if(function_exists('utf8_encode')) {
+ return utf8_encode($str);
+ } else {
+ return $str;
+ }
+ } else {
+ return $str;
+ }
+ }
+
+
+ /* Decode from charset used by this LDAP server to iso8859-1 */
+ function charset_decode($str) {
+ if($this->charset == 'utf-8') {
+ if(function_exists('utf8_decode')) {
+ return utf8_decode($str);
+ } else
+ return $str;
+ }
+ } else {
+ return $str;
+ }
+ }
+
+
+ /* ========================== Public ======================== */
+
+ /* Search the LDAP server */
+ function search($expr) {
+
+ /* To be replaced by advanded search expression parsing */
+ if(is_array($expr)) return false;
+
+ /* Encode the expression */
+ $expr = $this->charset_encode($expr);
+ if(strstr($expr, '*') === false) {
+ $expr = "*$expr*";
+ }
+ $expression = "cn=$expr";
+
+ /* Make sure connection is there */
+ if(!$this->open()) {
+ return false;
+ }
+
+ /* Do the search. Use improved ldap_search() if PHP version is
+ * 4.0.2 or newer. */
+ if(sqCheckPHPVersion(4, 0, 2)) {
+ $sret = @ldap_search($this->linkid, $this->basedn, $expression,
+ array('dn', 'o', 'ou', 'sn', 'givenname',
+ 'cn', 'mail', 'telephonenumber'),
+ 0, $this->maxrows, $this->timeout);
+ } else {
+ $sret = @ldap_search($this->linkid, $this->basedn, $expression,
+ array('dn', 'o', 'ou', 'sn', 'givenname',
+ 'cn', 'mail', 'telephonenumber'));
+ }
+
+ /* Should get error from server using the ldap_error() function,
+ * but it only exist in the PHP LDAP documentation. */
+ if(!$sret) {
+ if(function_exists('ldap_error')) {
+ return $this->set_error(ldap_error($this->linkid));
+ } else {
+ return $this->set_error('ldap_search failed');
+ }
+ }
+
+ if(@ldap_count_entries($this->linkid, $sret) <= 0) {
+ return array();
+ }
+
+ /* Get results */
+ $ret = array();
+ $returned_rows = 0;
+ $res = @ldap_get_entries($this->linkid, $sret);
+ for($i = 0 ; $i < $res['count'] ; $i++) {
+ $row = $res[$i];
+
+ /* Extract data common for all e-mail addresses
+ * of an object. Use only the first name */
+ $nickname = $this->charset_decode($row['dn']);
+ $fullname = $this->charset_decode($row['cn'][0]);
+
+ if(empty($row['telephonenumber'][0])) {
+ $phone = '';
+ } else {
+ $phone = $this->charset_decode($row['telephonenumber'][0]);
+ }
+
+ if(!empty($row['ou'][0])) {
+ $label = $this->charset_decode($row['ou'][0]);
+ }
+ else if(!empty($row['o'][0])) {
+ $label = $this->charset_decode($row['o'][0]);
+ } else {
+ $label = '';
+ }
+
+ if(empty($row['givenname'][0])) {
+ $firstname = '';
+ } else {
+ $firstname = $this->charset_decode($row['givenname'][0]);
+ }
+
+ if(empty($row['sn'][0])) {
+ $surname = '';
+ } else {
+ $surname = $this->charset_decode($row['sn'][0]);
+ }
+
+ /* Add one row to result for each e-mail address */
+ if(isset($row['mail']['count'])) {
+ for($j = 0 ; $j < $row['mail']['count'] ; $j++) {
+ array_push($ret, array('nickname' => $nickname,
+ 'name' => $fullname,
+ 'firstname' => $firstname,
+ 'lastname' => $surname,
+ 'email' => $row['mail'][$j],
+ 'label' => $label,
+ 'phone' => $phone,
+ 'backend' => $this->bnum,
+ 'source' => &$this->sname));
+
+ // Limit number of hits
+ $returned_rows++;
+ if(($returned_rows >= $this->maxrows) &&
+ ($this->maxrows > 0) ) {
+ ldap_free_result($sret);
+ return $ret;
+ }
+
+ } // for($j ...)
+
+ } // isset($row['mail']['count'])
+
+ }
+
+ ldap_free_result($sret);
+ return $ret;
+ } /* end search() */
+
+
+ /* If you run a tiny LDAP server and you want the "List All" button
+ * to show EVERYONE, then uncomment this tiny block of code:
+ *
+ * function list_addr() {
+ * return $this->search('*');
+ * }
+ *
+ * Careful with this -- it could get quite large for big sites. */
+}
+?>
\ No newline at end of file
* $Id$
*/
-/*****************************************************************/
-/*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
-/*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
-/*** + Base level indent should begin at left margin, as ***/
-/*** the first line of the class definition below. ***/
-/*** + All identation should consist of four space blocks ***/
-/*** + Tab characters are evil. ***/
-/*** + all comments should use "slash-star ... star-slash" ***/
-/*** style -- no pound characters, no slash-slash style ***/
-/*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
-/*** ALWAYS USE { AND } CHARACTERS!!! ***/
-/*** + Please use ' instead of ", when possible. Note " ***/
-/*** should always be used in _( ) function calls. ***/
-/*** Thank you for your help making the SM code more readable. ***/
-/*****************************************************************/
-
class abook_local_file extends addressbook_backend {
- var $btype = 'local';
- var $bname = 'local_file';
-
- var $filename = '';
- var $filehandle = 0;
- var $create = false;
- var $umask;
-
- // ========================== Private =======================
-
- // Constructor
- function abook_local_file($param) {
- $this->sname = _("Personal address book");
- $this->umask = Umask();
-
- if(is_array($param)) {
- if(empty($param['filename']))
- return $this->set_error('Invalid parameters');
- if(!is_string($param['filename']))
- return $this->set_error($param['filename'] . ': '.
- _("Not a file name"));
-
- $this->filename = $param['filename'];
-
- if($param['create'])
- $this->create = true;
- if(isset($param['umask']))
- $this->umask = $param['umask'];
-
- if(!empty($param['name']))
- $this->sname = $param['name'];
-
- $this->open(true);
- } else {
- $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
- // open, do nothing.
- function open($new = false) {
- $this->error = '';
- $file = $this->filename;
- $create = $this->create;
-
- // Return true is file is open and $new is unset
- if($this->filehandle && !$new)
- return true;
-
- // Check that new file exitsts
- if((!(file_exists($file) && is_readable($file))) && !$create)
- return $this->set_error("$file: " . _("No such file or directory"));
-
- // Close old file, if any
- if($this->filehandle) $this->close();
-
- // Open file. First try to open for reading and writing,
- // but fall back to read only.
- umask($this->umask);
- $fh = @fopen($file, 'a+');
- if($fh) {
- $this->filehandle = &$fh;
- $this->filename = $file;
- $this->writeable = true;
- } else {
- $fh = @fopen($file, 'r');
- if($fh) {
- $this->filehandle = &$fh;
- $this->filename = $file;
- $this->writeable = false;
- } else {
- return $this->set_error("$file: " . _("Open failed"));
- }
- }
-
- return true;
- }
-
- // Close the file and forget the filehandle
- function close() {
- @fclose($this->filehandle);
- $this->filehandle = 0;
- $this->filename = '';
- $this->writable = false;
- }
-
- // Lock the datafile - try 20 times in 5 seconds
- function lock() {
- for($i = 0 ; $i < 20 ; $i++) {
- if(flock($this->filehandle, 2 + 4))
- return true;
- else
- usleep(250000);
- }
- return false;
- }
-
- // Lock the datafile
- function unlock() {
- return flock($this->filehandle, 3);
- }
-
- // Overwrite the file with data from $rows
- // NOTE! Previous locks are broken by this function
- function overwrite(&$rows) {
- $newfh = @fopen($this->filename, 'w');
- if(!$newfh)
- return $this->set_error("$file: " . _("Open failed"));
-
- for($i = 0 ; $i < sizeof($rows) ; $i++) {
- if(is_array($rows[$i]))
- fwrite($newfh, join('|', $rows[$i]) . "\n");
- }
-
- fclose($newfh);
- $this->unlock();
- $this->open(true);
- return true;
- }
-
- // ========================== Public ========================
-
- // Search the file
- function search($expr) {
-
- // To be replaced by advanded search expression parsing
- if(is_array($expr)) return;
-
- // Make regexp from glob'ed expression
- // May want to quote other special characters like (, ), -, [, ], etc.
- $expr = str_replace('?', '.', $expr);
- $expr = str_replace('*', '.*', $expr);
-
- $res = array();
- if(!$this->open())
- return false;
-
- @rewind($this->filehandle);
-
- while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
- $line = join(' ', $row);
- if(eregi($expr, $line)) {
- array_push($res, array('nickname' => $row[0],
- 'name' => $row[1] . ' ' . $row[2],
- 'firstname' => $row[1],
- 'lastname' => $row[2],
- 'email' => $row[3],
- 'label' => $row[4],
- 'backend' => $this->bnum,
- 'source' => &$this->sname));
- }
- }
-
- return $res;
- }
-
- // Lookup alias
- function lookup($alias) {
- if(empty($alias))
- return array();
-
- $alias = strtolower($alias);
-
- $this->open();
- @rewind($this->filehandle);
-
- while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
- if(strtolower($row[0]) == $alias) {
- return array('nickname' => $row[0],
- 'name' => $row[1] . ' ' . $row[2],
- 'firstname' => $row[1],
- 'lastname' => $row[2],
- 'email' => $row[3],
- 'label' => $row[4],
- 'backend' => $this->bnum,
- 'source' => &$this->sname);
- }
- }
-
- return array();
- }
-
- // List all addresses
- function list_addr() {
- $res = array();
- $this->open();
- @rewind($this->filehandle);
-
- while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
- array_push($res, array('nickname' => $row[0],
- 'name' => $row[1] . ' ' . $row[2],
- 'firstname' => $row[1],
- 'lastname' => $row[2],
- 'email' => $row[3],
- 'label' => $row[4],
- 'backend' => $this->bnum,
- 'source' => &$this->sname));
- }
- return $res;
- }
-
- // Add address
- function add($userdata) {
- if(!$this->writeable)
- return $this->set_error(_("Addressbook is read-only"));
-
- // See if user exist already
- $ret = $this->lookup($userdata['nickname']);
- if(!empty($ret))
- return $this->set_error(sprintf(_("User '%s' already exist"),
- $ret['nickname']));
-
- // Here is the data to write
- $data = $userdata['nickname'] . '|' . $userdata['firstname'] . '|' .
- $userdata['lastname'] . '|' . $userdata['email'] . '|' .
- $userdata['label'];
- // Strip linefeeds
- $data = ereg_replace("[\r\n]", ' ', $data);
- // Add linefeed at end
- $data = $data . "\n";
-
- // Reopen file, just to be sure
- $this->open(true);
- if(!$this->writeable)
- return $this->set_error(_("Addressbook is read-only"));
-
- // Lock the file
- if(!$this->lock())
- return $this->set_error(_("Could not lock datafile"));
-
- // Write
- $r = fwrite($this->filehandle, $data);
-
- // Unlock file
- $this->unlock();
-
- // Test write result and exit if OK
- if($r > 0) return true;
-
- // Fail
- $this->set_error(_("Write to addressbook failed"));
- return false;
- }
-
- // Delete address
- function remove($alias) {
- if(!$this->writeable)
- return $this->set_error(_("Addressbook is read-only"));
-
- // Lock the file to make sure we're the only process working
- // on it.
- if(!$this->lock())
- return $this->set_error(_("Could not lock datafile"));
-
- // Read file into memory, ignoring nicknames to delete
- @rewind($this->filehandle);
- $i = 0;
- $rows = array();
- while($row = @fgetcsv($this->filehandle, 2048, '|')) {
- if(!in_array($row[0], $alias))
- $rows[$i++] = $row;
- }
-
- // Write data back
- if(!$this->overwrite($rows)) {
- $this->unlock();
- return false;
- }
-
- $this->unlock();
- return true;
- }
-
- // Modify address
- function modify($alias, $userdata) {
- if(!$this->writeable)
- return $this->set_error(_("Addressbook is read-only"));
-
- // See if user exist
- $ret = $this->lookup($alias);
- if(empty($ret))
- return $this->set_error(sprintf(_("User '%s' does not exist"),
- $alias));
-
- // Lock the file to make sure we're the only process working
- // on it.
- if(!$this->lock())
- return $this->set_error(_("Could not lock datafile"));
-
- // Read file into memory, modifying the data for the
- // user identifyed by $alias
- $this->open(true);
- @rewind($this->filehandle);
- $i = 0;
- $rows = array();
- while($row = @fgetcsv($this->filehandle, 2048, '|')) {
- if(strtolower($row[0]) != strtolower($alias)) {
- $rows[$i++] = $row;
- } else {
- $rows[$i++] = array(0 => $userdata['nickname'],
- 1 => $userdata['firstname'],
- 2 => $userdata['lastname'],
- 3 => $userdata['email'],
- 4 => $userdata['label']);
- }
- }
-
- // Write data back
- if(!$this->overwrite($rows)) {
- $this->unlock();
- return false;
- }
-
- $this->unlock();
- return true;
- }
+ var $btype = 'local';
+ var $bname = 'local_file';
+
+ var $filename = '';
+ var $filehandle = 0;
+ var $create = false;
+ var $umask;
+
+ /* ========================== Private ======================= */
+
+ /* Constructor */
+ function abook_local_file($param) {
+ $this->sname = _("Personal address book");
+ $this->umask = Umask();
+
+ if(is_array($param)) {
+ if(empty($param['filename'])) {
+ return $this->set_error('Invalid parameters');
+ }
+ if(!is_string($param['filename'])) {
+ return $this->set_error($param['filename'] . ': '.
+ _("Not a file name"));
+ }
+
+ $this->filename = $param['filename'];
+
+ if($param['create']) {
+ $this->create = true;
+ }
+ if(isset($param['umask'])) {
+ $this->umask = $param['umask'];
+ }
+ if(!empty($param['name'])) {
+ $this->sname = $param['name'];
+ }
+
+ $this->open(true);
+ } else {
+ $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
+ * open, do nothing. */
+ function open($new = false) {
+ $this->error = '';
+ $file = $this->filename;
+ $create = $this->create;
+
+ /* Return true is file is open and $new is unset */
+ if($this->filehandle && !$new) {
+ return true;
+ }
+
+ /* Check that new file exitsts */
+ if((!(file_exists($file) && is_readable($file))) && !$create) {
+ return $this->set_error("$file: " . _("No such file or directory"));
+ }
+
+ /* Close old file, if any */
+ if($this->filehandle) { $this->close(); }
+
+ /* Open file. First try to open for reading and writing,
+ * but fall back to read only. */
+ umask($this->umask);
+ $fh = @fopen($file, 'a+');
+ if($fh) {
+ $this->filehandle = &$fh;
+ $this->filename = $file;
+ $this->writeable = true;
+ } else {
+ $fh = @fopen($file, 'r');
+ if($fh) {
+ $this->filehandle = &$fh;
+ $this->filename = $file;
+ $this->writeable = false;
+ } else {
+ return $this->set_error("$file: " . _("Open failed"));
+ }
+ }
+ return true;
+ }
+
+ /* Close the file and forget the filehandle */
+ function close() {
+ @fclose($this->filehandle);
+ $this->filehandle = 0;
+ $this->filename = '';
+ $this->writable = false;
+ }
+
+ /* Lock the datafile - try 20 times in 5 seconds */
+ function lock() {
+ for($i = 0 ; $i < 20 ; $i++) {
+ if(flock($this->filehandle, 2 + 4))
+ return true;
+ else
+ usleep(250000);
+ }
+ return false;
+ }
+
+ /* Lock the datafile */
+ function unlock() {
+ return flock($this->filehandle, 3);
+ }
+
+ /* Overwrite the file with data from $rows
+ * NOTE! Previous locks are broken by this function */
+ function overwrite(&$rows) {
+ $newfh = @fopen($this->filename, 'w');
+ if(!$newfh) {
+ return $this->set_error("$file: " . _("Open failed"));
+ }
+
+ for($i = 0 ; $i < sizeof($rows) ; $i++) {
+ if(is_array($rows[$i])) {
+ fwrite($newfh, join('|', $rows[$i]) . "\n");
+ }
+ }
+
+ fclose($newfh);
+ $this->unlock();
+ $this->open(true);
+ return true;
+ }
+
+ /* ========================== Public ======================== */
+
+ /* Search the file */
+ function search($expr) {
+
+ /* To be replaced by advanded search expression parsing */
+ if(is_array($expr)) { return; }
+
+ /* Make regexp from glob'ed expression
+ * May want to quote other special characters like (, ), -, [, ], etc. */
+ $expr = str_replace('?', '.', $expr);
+ $expr = str_replace('*', '.*', $expr);
+
+ $res = array();
+ if(!$this->open()) {
+ return false;
+ }
+ @rewind($this->filehandle);
+
+ while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
+ $line = join(' ', $row);
+ if(eregi($expr, $line)) {
+ array_push($res, array('nickname' => $row[0],
+ 'name' => $row[1] . ' ' . $row[2],
+ 'firstname' => $row[1],
+ 'lastname' => $row[2],
+ 'email' => $row[3],
+ 'label' => $row[4],
+ 'backend' => $this->bnum,
+ 'source' => &$this->sname));
+ }
+ }
+
+ return $res;
+ }
+
+ /* Lookup alias */
+ function lookup($alias) {
+ if(empty($alias)) {
+ return array();
+ }
+
+ $alias = strtolower($alias);
+
+ $this->open();
+ @rewind($this->filehandle);
+
+ while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
+ if(strtolower($row[0]) == $alias) {
+ return array('nickname' => $row[0],
+ 'name' => $row[1] . ' ' . $row[2],
+ 'firstname' => $row[1],
+ 'lastname' => $row[2],
+ 'email' => $row[3],
+ 'label' => $row[4],
+ 'backend' => $this->bnum,
+ 'source' => &$this->sname);
+ }
+ }
+
+ return array();
+ }
+
+ /* List all addresses */
+ function list_addr() {
+ $res = array();
+ $this->open();
+ @rewind($this->filehandle);
+
+ while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
+ array_push($res, array('nickname' => $row[0],
+ 'name' => $row[1] . ' ' . $row[2],
+ 'firstname' => $row[1],
+ 'lastname' => $row[2],
+ 'email' => $row[3],
+ 'label' => $row[4],
+ 'backend' => $this->bnum,
+ 'source' => &$this->sname));
+ }
+ return $res;
+ }
+
+ /* Add address */
+ function add($userdata) {
+ if(!$this->writeable) {
+ return $this->set_error(_("Addressbook is read-only"));
+ }
+ /* See if user exists already */
+ $ret = $this->lookup($userdata['nickname']);
+ if(!empty($ret)) {
+ return $this->set_error(sprintf(_("User '%s' already exist"),
+ $ret['nickname']));
+ }
+
+ /* Here is the data to write */
+ $data = $userdata['nickname'] . '|' . $userdata['firstname'] . '|' .
+ $userdata['lastname'] . '|' . $userdata['email'] . '|' .
+ $userdata['label'];
+ /* Strip linefeeds */
+ $data = ereg_replace("[\r\n]", ' ', $data);
+ /* Add linefeed at end */
+ $data = $data . "\n";
+
+ /* Reopen file, just to be sure */
+ $this->open(true);
+ if(!$this->writeable) {
+ return $this->set_error(_("Addressbook is read-only"));
+ }
+
+ /* Lock the file */
+ if(!$this->lock()) {
+ return $this->set_error(_("Could not lock datafile"));
+ }
+
+ /* Write */
+ $r = fwrite($this->filehandle, $data);
+
+ /* Unlock file */
+ $this->unlock();
+
+ /* Test write result and exit if OK */
+ if($r > 0) return true;
+
+ /* Fail */
+ $this->set_error(_("Write to addressbook failed"));
+ return false;
+ }
+
+ /* Delete address */
+ function remove($alias) {
+ if(!$this->writeable) {
+ return $this->set_error(_("Addressbook is read-only"));
+ }
+
+ /* Lock the file to make sure we're the only process working
+ * on it. */
+ if(!$this->lock()) {
+ return $this->set_error(_("Could not lock datafile"));
+ }
+
+ /* Read file into memory, ignoring nicknames to delete */
+ @rewind($this->filehandle);
+ $i = 0;
+ $rows = array();
+ while($row = @fgetcsv($this->filehandle, 2048, '|')) {
+ if(!in_array($row[0], $alias)) {
+ $rows[$i++] = $row;
+ }
+ }
+
+ /* Write data back */
+ if(!$this->overwrite($rows)) {
+ $this->unlock();
+ return false;
+ }
+
+ $this->unlock();
+ return true;
+ }
+
+ /* Modify address */
+ function modify($alias, $userdata) {
+ if(!$this->writeable) {
+ return $this->set_error(_("Addressbook is read-only"));
+ }
+
+ /* See if user exists */
+ $ret = $this->lookup($alias);
+ if(empty($ret)) {
+ return $this->set_error(sprintf(_("User '%s' does not exist"),
+ $alias));
+ }
+
+ /* Lock the file to make sure we're the only process working
+ * on it. */
+ if(!$this->lock()) {
+ return $this->set_error(_("Could not lock datafile"));
+ }
+
+ /* Read file into memory, modifying the data for the
+ * user identified by $alias */
+ $this->open(true);
+ @rewind($this->filehandle);
+ $i = 0;
+ $rows = array();
+ while($row = @fgetcsv($this->filehandle, 2048, '|')) {
+ if(strtolower($row[0]) != strtolower($alias)) {
+ $rows[$i++] = $row;
+ } else {
+ $rows[$i++] = array(0 => $userdata['nickname'],
+ 1 => $userdata['firstname'],
+ 2 => $userdata['lastname'],
+ 3 => $userdata['email'],
+ 4 => $userdata['label']);
+ }
+ }
+
+ /* Write data back */
+ if(!$this->overwrite($rows)) {
+ $this->unlock();
+ return false;
+ }
+
+ $this->unlock();
+ return true;
+ }
- } // End of class abook_local_file
-?>
+} /* End of class abook_local_file */
+?>
\ No newline at end of file