Allow underscores in nicknames by changing id used in forms, add more labels for...
[squirrelmail.git] / src / addressbook.php
index 42f5d2dd90c735acfd9a3ead024f623b5dc97924..6daf3fd74cefcd95bff6a9829b2a818f79a5ccb1 100644 (file)
@@ -5,13 +5,16 @@
  *
  * Manage personal address book.
  *
- * @copyright © 1999-2006 The SquirrelMail Project Team
+ * @copyright © 1999-2007 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package squirrelmail
  * @subpackage addressbook
  */
 
+/** This is the addressbook page */
+define('PAGE_NAME', 'addressbook');
+
 /**
  * Include the SquirrelMail initialization file.
  */
@@ -20,6 +23,8 @@ include('../include/init.php');
 /** SquirrelMail required files. */
 /* address book functions */
 require_once(SM_PATH . 'functions/addressbook.php');
+include_once(SM_PATH . 'templates/util_addressbook.php');
+
 /* form functions */
 require_once(SM_PATH . 'functions/forms.php');
 
@@ -29,6 +34,7 @@ require_once(SM_PATH . 'functions/forms.php');
 sqgetGlobalVar('addaddr',       $addaddr,       SQ_POST);
 sqgetGlobalVar('editaddr',      $editaddr,      SQ_POST);
 sqgetGlobalVar('deladdr',       $deladdr,       SQ_POST);
+sqgetGlobalVar('compose_to',    $compose_to,    SQ_POST);
 sqgetGlobalVar('sel',           $sel,           SQ_POST);
 sqgetGlobalVar('oldnick',       $oldnick,       SQ_POST);
 sqgetGlobalVar('backend',       $backend,       SQ_POST);
@@ -38,7 +44,7 @@ sqgetGlobalVar('doedit',        $doedit,        SQ_POST);
 $abook_sort_order = get_abook_sort();
 
 /* Create page header before addressbook_init in order to  display error messages correctly. */
-displayPageHeader($color, 'None');
+displayPageHeader($color);
 
 /* Open addressbook with error messages on.
  remote backends (LDAP) are enabled because they can be used. (list_addr function)
@@ -52,7 +58,8 @@ if($abook->localbackend == 0) {
 }
 
 $current_backend = $abook->localbackend;
-if (sqgetGlobalVar('new_bnum',$new_backend,SQ_POST) && array_key_exists($new_backend,$abook->backends)) {
+if (sqgetGlobalVar('new_bnum', $new_backend, SQ_FORM)
+ && array_key_exists($new_backend, $abook->backends)) {
     $current_backend = (int) $new_backend;
 }
 
@@ -109,14 +116,14 @@ if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'P
             $orig_sel = $sel;
             sort($sel);
 
-            /* The selected addresses are identidied by "backend:nickname". *
+            /* The selected addresses are identified by "backend_nickname". *
              * Sort the list and process one backend at the time            */
             $prevback  = -1;
             $subsel    = array();
             $delfailed = false;
 
             for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
-                list($sbackend, $snick) = explode(':', $sel[$i]);
+                list($sbackend, $snick) = explode('_', $sel[$i], 2);
 
                 /* When we get to a new backend, process addresses in *
                  * previous one.                                      */
@@ -150,6 +157,47 @@ if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'P
                 $defselected  = $orig_sel;
             }
 
+        /************************************************
+         * Compose to selected address(es)              *
+         ************************************************/
+        } else if ((!empty($compose_to)) && sizeof($sel) > 0) {
+            $orig_sel = $sel;
+            sort($sel);
+
+            // The selected addresses are identified by "backend_nickname"
+            $lookup_failed = false;
+            $send_to = '';
+
+            for ($i = 0 ; (($i < sizeof($sel)) && !$lookup_failed) ; $i++) {
+                list($sbackend, $snick) = explode('_', $sel[$i], 2);
+
+                $data = $abook->lookup($snick, $sbackend);
+
+                if (!$data) {
+                    $formerror = $abook->error;
+                    $lookup_failed = true;
+                    break;
+                } else {
+                    $addr = $abook->full_address($data);
+                    if (!empty($addr))
+                        $send_to .= $addr . ', ';
+                }
+            }
+
+
+            if ($lookup_failed || empty($send_to)) {
+                $showaddrlist = true;
+                $defselected  = $sel;
+            }
+
+
+            // send off to compose screen
+            else {
+                $send_to = trim($send_to, ', ');
+                header('Location: ' . $base_uri . 'src/compose.php?send_to=' . rawurlencode($send_to));
+                exit;
+            }
+
         } else {
 
             /***********************************************
@@ -164,14 +212,18 @@ if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'P
                         $defselected = $sel;
                     } else {
                         $abortform = true;
-                        list($ebackend, $enick) = explode(':', current($sel));
+                        list($ebackend, $enick) = explode('_', current($sel), 2);
                         $olddata = $abook->lookup($enick, $ebackend);
                         // Test if $olddata really contains anything and return an error message if it doesn't
                         if (!$olddata) {
                             error_box(nl2br(htmlspecialchars($abook->error)));
                         } else {
                             /* Display the "new address" form */
-                            abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
+                            echo abook_create_form($form_url, 'editaddr',
+                                                   _("Update address"),
+                                                   _("Update address"),
+                                                   $current_backend,
+                                                   $olddata);
                             echo addHidden('oldnick', $olddata['nickname']).
                                 addHidden('backend', $olddata['backend']).
                                 addHidden('doedit', '1').
@@ -186,17 +238,14 @@ if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'P
                     /* Handle error messages */
                     if (!$r) {
                         /* Display error */
-                        echo html_tag( 'table',
-                                html_tag( 'tr',
-                                    html_tag( 'td',
-                                        "\n". '<strong><font color="' . $color[2] .
-                                        '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
-                                        'center' )
-                                    ),
-                                'center', '', 'width="100%"' );
+                        plain_error_message( nl2br(htmlspecialchars($abook->error)));
 
                         /* Display the "new address" form again */
-                        abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
+                        echo abook_create_form($form_url, 'editaddr',
+                                               _("Update address"),
+                                               _("Update address"),
+                                               $current_backend,
+                                               $newdata);
                         echo addHidden('oldnick', $oldnick).
                             addHidden('backend', $backend).
                             addHidden('doedit',  '1').
@@ -212,9 +261,10 @@ if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'P
                     $formerror = _("Please select address that you want to edit");
                     $showaddrlist = true;
                 } /* end of edit stage detection */
-            } /* !empty($editaddr)                  - Update/modify address */
-        } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
-    } /* !empty($addaddr['nickname'])               - Add new address */
+            } /* !empty($editaddr)                     - Update/modify address */
+        } /* (!empty($deladdr)) && sizeof($sel) > 0    - Delete address(es) 
+          or (!empty($compose_to)) && sizeof($sel) > 0 - Compose to address(es) */
+    } /* !empty($addaddr['nickname'])                  - Add new address */
 
     // Some times we end output before forms are printed
     if($abortform) {
@@ -245,33 +295,28 @@ while (list($k, $backend) = each ($abook->backends)) {
     $a['Addresses'] = array();
 
     $alist = $abook->list_addr($backend->bnum);
-    usort($alist,'alistcmp');
-    $start = 200;
-    $count = count($alist);
-    if ($start >= $count) $start = 0;
-    $alist = array_slice($alist,$start,15);
-   
-    while(list($undef,$row) = each($alist)) {
-        $contact = array (
-                            'FirstName'     => htmlspecialchars($row['firstname']),
-                            'LastName'      => htmlspecialchars($row['lastname']),
-                            'FullName'      => htmlspecialchars($row['name']),
-                            'NickName'      => htmlspecialchars($row['nickname']),
-                            'Email'         => htmlspecialchars($row['email']),
-                            'FullAddress'   => htmlspecialchars($abook->full_address($row)),
-                            'Info'          => htmlspecialchars($row['label']),
-                            'Extra'         => (isset($row['extra']) ? $row['extra'] : NULL),
-                         );
-        $a['Addresses'][] = $contact;
-    }
+
+    /* check return (array with data or boolean false) */
+    if (is_array($alist)) {
+        usort($alist,'alistcmp');
+
+        $a['Addresses'] = formatAddressList($alist);
   
-    $addresses[$backend->bnum] = $a;
+        $addresses[$backend->bnum] = $a;
+    } else {
+        // list_addr() returns boolean
+        plain_error_message(nl2br(htmlspecialchars($abook->error)));
+    }
 }
 
 
 if ($showaddrlist) {
-    echo addForm($form_url, 'post');
+//FIXME: Remove HTML from here!
+    echo addForm($form_url, 'post', 'address_book_form');
     
+    $oTemplate->assign('compose_new_win', $compose_new_win);
+    $oTemplate->assign('compose_height', $compose_height);
+    $oTemplate->assign('compose_width', $compose_width);
     $oTemplate->assign('addresses', $addresses);
     $oTemplate->assign('current_backend', $current_backend);
     $oTemplate->assign('backends', $list_backends);
@@ -279,18 +324,21 @@ if ($showaddrlist) {
         
     $oTemplate->display('addressbook_list.tpl');
     
+//FIXME: Remove HTML from here!
     echo "</form>\n";
 }
 
 /* Display the "new address" form */
+//FIXME: Remove HTML from here! (echo abook_create_form() is OK, since it is all template based output
 echo '<a name="AddAddress"></a>' . "\n";
-abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
+echo abook_create_form($form_url, 'addaddr',
+                       _("Add to address book"),
+                       _("Add address"),
+                       $current_backend,
+                       $defdata);
 echo "</form>\n";
 
 /* Hook for extra address book blocks */
-echo "<!-- start of addressbook_bottom hook-->\n";
-do_hook('addressbook_bottom');
-echo "\n<!-- end of addressbook_bottom hook-->\n";
+do_hook('addressbook_bottom', $null);
 
 $oTemplate->display('footer.tpl');
-?>
\ No newline at end of file