Added (c) stuff and some formatting.
[squirrelmail.git] / functions / addressbook.php
index c83d4faa524a0e850e221f24149fa7f933b07e3e..08246cd6cf9446d62b145f1872b2cc2f15690d19 100644 (file)
@@ -1,42 +1,96 @@
 <?php
 
-  /**
-   **  addressbook.php
-   **
-   **  Functions and classes for the addressbook system.
-   **
-   **  $Id$
-   **/
-    
-   $addressbook_php = true;
+   /**
+    **  addressbook.php
+    **
+    **  Copyright (c) 1999-2001 The Squirrelmail Development Team
+    **  Licensed under the GNU GPL. For full terms see the file COPYING.
+    **
+    **  Functions and classes for the addressbook system.
+    **
+    **  $Id$
+    **/
+
+
+   // 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');
+   require_once('../functions/abook_local_file.php');
+   require_once('../functions/abook_ldap_server.php');
+
+   // Use this if you wanna have a global address book
+   if (isset($address_book_global_filename))
+      include_once('../functions/abook_global_file.php');
+
+   // Only load database backend if database is configured
+   global $addrbook_dsn;
+   if(isset($addrbook_dsn))
+      include_once('../functions/abook_database.php');
 
 
    // 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
       $abook = new AddressBook;
       
-      // Always add a local backend
-      $filename = sprintf('%s%s.abook', $data_dir, $username);
-      $r = $abook->add_backend('local_file', Array('filename' => $filename,
-                                                  'create'   => true));
-      if(!$r && $showerr) {
-        printf(_("Error opening file %s"), $filename);
-        exit;
+      // Always add a local backend. We use *either* file-based *or* a
+      // database addressbook. If $addrbook_dsn is set, the database
+      // backend is used. If not, addressbooks are stores in files.
+      if(isset($addrbook_dsn) && !empty($addrbook_dsn)) {
+        // Database
+        $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
+                                                   'owner' => $username,
+                                                   'table' => 'address'));
+        if(!$r && $showerr) {
+           printf(_("Error initializing addressbook database."));
+           exit;
+        }
+      } else {
+        // File
+        $filename = sprintf('%s%s.abook', $data_dir, $username);
+        $r = $abook->add_backend('local_file', Array('filename' => $filename,
+                                                     'create'   => true));
+        if(!$r && $showerr) {
+           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)
        return $abook;
 
       // Load configured LDAP servers (if PHP has LDAP support)
-      if(is_array($ldap_server) && function_exists('ldap_connect')) {
+      if(isset($ldap_server) && is_array($ldap_server) && 
+          function_exists('ldap_connect')) {
         reset($ldap_server);
         while(list($undef,$param) = each($ldap_server)) {
            if(is_array($param)) {
    
    // Had to move this function outside of the Addressbook Class
    // PHP 4.0.4 Seemed to be having problems with inline functions.
-   function cmp($a,$b) {   
+   function addressbook_cmp($a,$b) {   
       if($a['backend'] > $b['backend']) 
             return 1;
          else if($a['backend'] < $b['backend']) 
         $ret = array();
         for($i = 1 ; $i <= $this->numbackends ; $i++) {
            if(empty($type) || $type == $this->backends[$i]->btype) {
-              array_push($ret, &$this->backends[$i]);
+              $ret[] = &$this->backends[$i];
            }
         }
         return $ret;
       // See each of the backend classes for valid parameters.
       function add_backend($backend, $param = '') {
         $backend_name = 'abook_' . $backend;
-        eval("\$newback = new $backend_name(\$param);");
+        eval('$newback = new ' . $backend_name . '($param);');
         if(!empty($newback->error)) {
            $this->error = $newback->error;
            return false;
         $ret = $this->search($expression, $bnum);
         if(!is_array($ret))
            return $ret;
-            usort($ret, 'cmp');
+            usort($ret, 'addressbook_cmp');
             return $ret;
       }
 
            $userdata['nickname'] = $userdata['email'];
         }
 
-        if(eregi("[\: \|\#\"\!]", $userdata['nickname'])) {
-           $this->error = _("Nickname contain illegal characters");
+        if(eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
+           $this->error = _("Nickname contains illegal characters");
            return false;
         }
 
            return false;
         }
 
-        if(eregi("[\: \|\#\"\!]", $userdata['nickname'])) {
-           $this->error = _("Nickname contain illegal characters");
+        if(eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
+           $this->error = _("Nickname contains illegal characters");
            return false;
         }