Merging two strings and making external links open in a new window
[squirrelmail.git] / functions / addressbook.php
index 4432751b2fa0ba4a2b61e43fa1353a13e007c878..9bce2351bab9a360eb51c219d31a2520e33aa8e1 100644 (file)
@@ -1,17 +1,18 @@
 <?php
-
 /**
  * addressbook.php
  *
- * Copyright (c) 1999-2002 The SquirrelMail Project Team
+ * Copyright (c) 1999-2004 The SquirrelMail Project Team
  * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * Functions and classes for the addressbook system.
  *
- * $Id$
+ * @version $Id$
+ * @package squirrelmail
+ * @subpackage addressbook
  */
 
-/*
+/**
    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"
    The global addressbook is unmodifiable by anyone.  You must actually
    use a shell script or whatnot to modify the contents.
 
-  global $data_dir;
+  global $data_dir, $address_book_global_filename;
   $address_book_global_filename = "$data_dir/global.abook";
 
-  Include backends here.
 */
 
-require_once('../functions/abook_local_file.php');
-require_once('../functions/abook_ldap_server.php');
-
-global $addrbook_dsn;
-
-/* Use this if you wanna have a global address book */
-if (isset($address_book_global_filename)) {
-    include_once('../functions/abook_global_file.php');
-}
+global $addrbook_dsn, $addrbook_global_dsn;
 
-/* Only load database backend if database is configured */
-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, $address_book_global_filename;
-    global $addrbook_dsn;
+    global $addrbook_dsn, $addrbook_table;
+    global $addrbook_global_dsn, $addrbook_global_table, $addrbook_global_writeable, $addrbook_global_listing;
 
     /* Create a new addressbook object */
     $abook = new AddressBook;
@@ -63,9 +51,12 @@ function addressbook_init($showerr = true, $onlylocal = false) {
     */
     if (isset($addrbook_dsn) && !empty($addrbook_dsn)) {
         /* Database */
+        if (!isset($addrbook_table) || empty($addrbook_table)) {
+            $addrbook_table = 'address';
+        }
         $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
                             'owner' => $username,
-                            'table' => 'address'));
+                            'table' => $addrbook_table));
         if (!$r && $showerr) {
             echo _("Error initializing addressbook database.");
             exit;
@@ -91,6 +82,30 @@ function addressbook_init($showerr = true, $onlylocal = false) {
         }
     }
 
+    /* Load global addressbook from SQL if configured */
+    if (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) {
+      /* Database configured */
+      if (!isset($addrbook_global_table) || empty($addrbook_global_table)) {
+       $addrbook_global_table = 'global_abook';
+      }
+      $r = $abook->add_backend('database',
+                              Array('dsn' => $addrbook_global_dsn,
+                                    'owner' => 'global',
+                                    'name' => _("Global address book"),
+                                    'writeable' => $addrbook_global_writeable,
+                                    'listing' => $addrbook_global_listing,
+                                    'table' => $addrbook_global_table));
+    }
+
+    /*
+     * hook allows to include different address book backends.
+     * plugins should extract $abook and $r from arguments
+     * and use same add_backend commands as above functions.
+     */
+    $hookReturn = do_hook('abook_init', $abook, $r);
+    $abook = $hookReturn[1];
+    $r = $hookReturn[2];
+    
     if ($onlylocal) {
         return $abook;
     }
@@ -119,6 +134,7 @@ function addressbook_init($showerr = true, $onlylocal = false) {
 /*
  *   Had to move this function outside of the Addressbook Class
  *   PHP 4.0.4 Seemed to be having problems with inline functions.
+ *   Note: this can return now since we don't support 4.0.4 anymore.
  */    
 function addressbook_cmp($a,$b) {
 
@@ -133,10 +149,10 @@ function addressbook_cmp($a,$b) {
 }
 
 
-/*
+/**
  * This is the main address book class that connect all the
  * backends and provide services to the functions above.
- *
+ * @package squirrelmail
  */
 
 class AddressBook {
@@ -198,6 +214,24 @@ class AddressBook {
     }
 
 
+    /*
+     * This function takes a $row array as returned by the addressbook 
+     * search and returns an e-mail address with the full name or
+     * nickname optionally prepended.
+     */
+
+    function full_address($row) {
+        global $addrsrch_fullname, $data_dir, $username;
+        $prefix = getPref($data_dir, $username, 'addrsrch_fullname');
+        if (($prefix != "" || (isset($addrsrch_fullname) &&
+            $prefix == $addrsrch_fullname)) && $prefix != 'noprefix') {
+            $name = ($prefix == 'nickname' ? $row['nickname'] : $row['name']);
+            return $name . ' <' . trim($row['email']) . '>';
+        } else {
+            return trim($row['email']);
+        }
+    }
+
     /*
         Return a list of addresses matching expression in
         all backends of a given type.
@@ -378,7 +412,7 @@ class AddressBook {
             $alias = array(0 => $alias);
         }
         
-        /* Check that specified backend is writable */
+        /* Check that specified backend is writeable */
         if (!$this->backends[$bnum]->writeable) {
             $this->error = _("Addressbook is read-only");
             return false;
@@ -431,7 +465,7 @@ class AddressBook {
             $userdata['nickname'] = $userdata['email'];
         }
         
-        /* Check that specified backend is writable */
+        /* Check that specified backend is writeable */
         if (!$this->backends[$bnum]->writeable) {
             $this->error = _("Addressbook is read-only");;
             return false;
@@ -452,8 +486,9 @@ class AddressBook {
     
 } /* End of class Addressbook */
 
-/*
+/**
  * Generic backend that all other backends extend
+ * @package squirrelmail
  */
 class addressbook_backend {
 
@@ -510,8 +545,32 @@ class addressbook_backend {
 
 }
 
-/* Sort array by the key "name" */
+/**
+ * Sort array by the key "name"
+ */
 function alistcmp($a,$b) {
+    $abook_sort_order=get_abook_sort();
+
+    switch ($abook_sort_order) {
+    case 0:
+    case 1:
+      $abook_sort='nickname';
+      break;
+    case 4:
+    case 5:
+      $abook_sort='email';
+      break;
+    case 6:
+    case 7:
+      $abook_sort='label';
+      break;
+    case 2:
+    case 3:
+    case 8:
+    default:
+      $abook_sort='name';
+    }
+
     if ($a['backend'] > $b['backend']) {
         return 1;
     } else {
@@ -519,7 +578,95 @@ function alistcmp($a,$b) {
             return -1;
         }
     }
-    return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
+
+    if( (($abook_sort_order+2) % 2) == 1) {
+      return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ? 1 : -1;
+    } else {
+      return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1;
+    }
+}
+
+/**
+ * Address book sorting options
+ *
+ * returns address book sorting order
+ * @return integer book sorting options order
+ */
+function get_abook_sort() {
+    global $data_dir, $username;
+
+    /* get sorting order */
+    if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET)) {
+      $abook_sort_order = (int) $temp;
+
+      if ($abook_sort_order < 0 or $abook_sort_order > 8)
+        $abook_sort_order=8;
+
+      setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order);
+    } else {
+      /* get previous sorting options. default to unsorted */
+      $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8);
+    }
+
+    return $abook_sort_order;
+}
+
+/**
+ * This function shows the address book sort button.
+ *
+ * @param integer $abook_sort_order current sort value
+ * @param string $alt_tag alt tag value (string visible to text only browsers)
+ * @param integer $Down sort value when list is sorted ascending
+ * @param integer $Up sort value when list is sorted descending
+ * @return string html code with sorting images and urls 
+ */
+function show_abook_sort_button($abook_sort_order, $alt_tag, $Down, $Up ) {
+    global $form_url;
+
+     /* Figure out which image we want to use. */
+    if ($abook_sort_order != $Up && $abook_sort_order != $Down) {
+        $img = 'sort_none.png';
+        $which = $Up;
+    } elseif ($abook_sort_order == $Up) {
+        $img = 'up_pointer.png';
+        $which = $Down;
+    } else {
+        $img = 'down_pointer.png';
+        $which = 8;
+    }
+
+      /* Now that we have everything figured out, show the actual button. */
+    return ' <a href="' . $form_url .'?abook_sort_order=' . $which
+         . '"><img src="../images/' . $img
+         . '" border="0" width="12" height="10" alt="' . $alt_tag . '" title="'
+         . _("Click here to change the sorting of the address list") .'"></a>';
+}
+
+/*
+  PHP 5 requires that the class be made first, which seems rather
+  logical, and should have been the way it was generated the first time.
+*/
+
+require_once(SM_PATH . 'functions/abook_local_file.php');
+require_once(SM_PATH . 'functions/abook_ldap_server.php');
+
+/* Use this if you wanna have a global address book */
+if (isset($address_book_global_filename)) {
+    include_once(SM_PATH . 'functions/abook_global_file.php');
+}
+
+/* Only load database backend if database is configured */
+if((isset($addrbook_dsn) && !empty($addrbook_dsn)) || 
+ (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) ) {
+  include_once(SM_PATH . 'functions/abook_database.php');
 }
 
+/*
+ * hook allows adding different address book classes.
+ * class must follow address book class coding standards.
+ *
+ * see addressbook_backend class and functions/abook_*.php files.
+ */
+do_hook('abook_add_class');
+
 ?>