Bläddra i källkod

* Added global address book support (disabled by default)

Tyler Akins 24 år sedan
förälder
incheckning
634f3e27dc
3 ändrade filer med 218 tillägg och 1 borttagningar
  1. 176 0
      functions/abook_global_file.php
  2. 32 1
      functions/addressbook.php
  3. 10 0
      src/addressbook.php

+ 176 - 0
functions/abook_global_file.php

@@ -0,0 +1,176 @@
+<?php
+
+  /**
+   **  abook_local_file.php
+   **
+   **  Backend for addressbook as a pipe separated file
+   **
+   **  An array with the following elements must be passed to
+   **  the class constructor (elements marked ? are optional):
+   **
+   **  NOTE. This class should not be used directly. Use the
+   **        "AddressBook" class instead.
+   **
+   **  Make sure you configure this before using it!
+   **
+   ** $Id$
+   **/
+
+   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;
+     }
+     
+   } // End of class abook_local_file
+?>

+ 32 - 1
functions/addressbook.php

@@ -12,10 +12,31 @@
       return; 
    define ('addressbook_php', true); 
 
+   // This is the path to the global site-wide addressbook.
+   // It looks and feels just like a user's .abook file
+   // If this is in the data directory, use "$data_dir/global.abook"
+   // If not, specify the path as though it was accessed from the
+   // src/ directory ("../global.abook" -> in main directory)
+   //
+   // If you don't want a global site-wide addressbook, comment these
+   // two lines out.  (They are disabled by default.)
+   //
+   // The global addressbook is unmodifiable by anyone.  You must actually
+   // use a shell script or whatnot to modify the contents.
+   //
+   //global $data_dir;
+   //$address_book_global_filename = "$data_dir/global.abook";
+
+
+
    // Include backends here.
    include('../functions/abook_local_file.php');
    include('../functions/abook_ldap_server.php');
 
+   // Use this if you wanna have a global address book
+   if (isset($address_book_global_filename))
+      include('../functions/abook_global_file.php');
+
    // Only load database backend if database is configured
    global $addrbook_dsn;
    if(isset($addrbook_dsn))
@@ -25,7 +46,7 @@
    // Create and initialize an addressbook object. 
    // Returns the created object
    function addressbook_init($showerr = true, $onlylocal = false) {
-      global $data_dir, $username, $ldap_server;
+      global $data_dir, $username, $ldap_server, $address_book_global_filename;
       global $addrbook_dsn;
       
       // Create a new addressbook object
@@ -52,6 +73,16 @@
 	    printf(_("Error opening file %s"), $filename);
 	    exit;
 	 }
+	 
+      }
+
+      // This would be for the global addressbook
+      if (isset($address_book_global_filename)) {
+         $r = $abook->add_backend('global_file');
+         if (!$r && $showerr) {
+             printf(_("Error initializing global addressbook."));
+	     exit;
+         }
       }
 
       if($onlylocal)

+ 10 - 0
src/addressbook.php

@@ -280,6 +280,9 @@
       $prevbackend = -1;
       $headerprinted = false;
 
+      echo "<p align=center><a href=\"#AddAddress\">" .
+         _("Add address") . "</a></p>\n";
+
       // List addresses
       printf("<FORM ACTION=\"%s\" METHOD=\"POST\">\n", $PHP_SELF);
       while(list($undef,$row) = each($alist)) {
@@ -287,6 +290,12 @@
 	 // New table header for each backend
 	 if($prevbackend != $row["backend"]) {
 	    if($prevbackend >= 0) {
+	       print "<TR><TD COLSPAN=5 ALIGN=center>\n";
+	       printf("<INPUT TYPE=submit NAME=editaddr VALUE=\"%s\">\n",
+	          _("Edit selected"));
+	       printf("<INPUT TYPE=submit NAME=deladdr VALUE=\"%s\">\n",
+	          _("Delete selected"));
+	       echo "</tr>\n";
 	       print '<TR><TD COLSPAN="5" ALIGN=center>';
 	       print "&nbsp;<BR></TD></TR></TABLE>\n";
 	    }
@@ -349,6 +358,7 @@
 
 
    // Display the "new address" form
+   echo "<a name=\"AddAddress\"></a>\n";
    printf("<FORM ACTION=\"%s\" NAME=f_add METHOD=\"POST\">\n", $PHP_SELF);
    print "<TABLE WIDTH=100% COLS=1 ALIGN=CENTER>\n";
    print "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER>\n<STRONG>";