adding error checking in deliver_sendmail class.
[squirrelmail.git] / src / search.php
index 5b2118632ca8370d5b65f54e4c81c09fc4b061fa..0409561bae646a9c47198e5ce6c6879bf8f6b01d 100644 (file)
@@ -3,17 +3,17 @@
 /**
  * search.php
  *
- * Copyright (c) 1999-2005 The SquirrelMail Project Team
- * Licensed under the GNU GPL. For full terms see the file COPYING.
- *
  * IMAP search page
  *
  * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas!
  *
+ * @author Alex Lemaresquier - Brainstorm <alex at brainstorm.fr>
+ * @copyright &copy; 1999-2005 The SquirrelMail Project Team
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package squirrelmail
+ * @subpackage search
  * @link http://www.ietf.org/rfc/rfc3501.txt
- * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
  */
 
 /**
@@ -25,12 +25,12 @@ define('SM_PATH','../');
 /** SquirrelMail required files.
  */
 require_once(SM_PATH . 'include/validate.php');
-require_once(SM_PATH . 'functions/strings.php');
-require_once(SM_PATH . 'functions/imap_asearch.php');
-require_once(SM_PATH . 'functions/imap_mailbox.php');
-require_once(SM_PATH . 'functions/imap_messages.php');
-require_once(SM_PATH . 'functions/mime.php');
-require_once(SM_PATH . 'functions/mailbox_display.php'); //getButton()...
+include_once(SM_PATH . 'functions/strings.php');
+include_once(SM_PATH . 'functions/imap_asearch.php');
+include_once(SM_PATH . 'functions/imap_mailbox.php');
+include_once(SM_PATH . 'functions/imap_messages.php');
+include_once(SM_PATH . 'functions/mime.php');
+include_once(SM_PATH . 'functions/mailbox_display.php'); //getButton()...
 include_once(SM_PATH . 'class/template/template.class.php');
 
 /** Prefs array ordinals. Must match $recent_prefkeys and $saved_prefkeys
@@ -79,7 +79,9 @@ function asearch_get_link(&$href, $text, $title = '')
  */
 function asearch_get_toggle_link($value, $action, $text_array, $title_array = array())
 {
-    return asearch_get_link(asearch_get_href($action . '=' . (int)$value), $text_array[$value], asearch_nz($title_array[$value]));
+    $asearch_nz=asearch_nz($title_array[$value]);
+    $asearch_get_href=asearch_get_href($action . '=' . (int)$value);
+    return asearch_get_link($asearch_get_href, $text_array[$value], $asearch_nz);
 }
 
 /**
@@ -505,7 +507,7 @@ function asearch_get_query_display(&$color, &$mailbox_array, &$biop_array, &$uno
                 $cur_mailbox = 'INBOX';
             $biop = asearch_nz($biop_array[$crit_num]);
             if (($query_display == '') || ($cur_mailbox != $last_mailbox)) {
-                $mailbox_display = ' <b>' . asearch_get_mailbox_display($cur_mailbox) . '</b>';
+                $mailbox_display = ' <b>' . htmlspecialchars(asearch_get_mailbox_display($cur_mailbox)) . '</b>';
                 if ($query_display == '')
                     $biop_display = _("In");
                 else
@@ -656,7 +658,7 @@ function asearch_print_saved(&$boxes)
     if (isset($saved_array[$saved_prefkeys[0]])) {
         $saved_count = count($saved_array[$saved_prefkeys[0]]);
         if ($saved_count > 0) {
-            $saved_actions = array('edit_saved' => _("edit"), 'search_saved' => _("search"), 'delete_saved' => _("delete"));
+            $saved_actions = array('edit_saved' => _("Edit"), 'search_saved' => _("Search"), 'delete_saved' => _("Delete"));
             asearch_print_query_array($boxes, $saved_array, $saved_prefkeys, $saved_actions, _("Saved Searches"), 'search_show_saved');
         }
     }
@@ -714,7 +716,7 @@ function asearch_mailbox_exists($mailbox, &$boxes)
 function asearch_get_form_mailbox($imapConnection, &$boxes, $mailbox, $row_num = 0)
 {
     if (($mailbox != 'All Folders') && (!asearch_mailbox_exists($mailbox, $boxes))) {
-        $missing = asearch_opt($mailbox, $mailbox, '[' . _("Missing") . '] ' . asearch_get_mailbox_display($mailbox));
+        $missing = asearch_opt($mailbox, $mailbox, '[' . _("Missing") . '] ' . htmlspecialchars(asearch_get_mailbox_display($mailbox)));
     } else {
         $missing = '';
     }
@@ -729,8 +731,7 @@ function asearch_get_form_mailbox($imapConnection, &$boxes, $mailbox, $row_num =
  */
 function asearch_get_form_sub($sub, $row_num = 0)
 {
-    return function_exists('addCheckBox') ? addCheckBox('sub[' . $row_num .']', $sub)
-    : '<input type="checkbox" name="sub[' . $row_num .']"' . ($sub ? ' checked="checked"' : '') . ' />';
+    return addCheckBox('sub[' . $row_num .']', $sub);
 }
 
 /** Build the 2 unop and where selects
@@ -747,16 +748,14 @@ function asearch_get_form_location($unop, $where, $row_num = 0)
  */
 function asearch_get_form_what($what, $row_num = 0)
 {
-    return function_exists('addInput') ? addInput('what[' . $row_num . ']', $what, '35')
-    : '<input type="text" size="35" name="what[' . $row_num . ']" value="' . htmlspecialchars($what) . '" />';
+    return addInput('what[' . $row_num . ']', $what, '35');
 }
 
 /** Build the Exclude criteria checkbox
  */
 function asearch_get_form_exclude($exclude, $row_num = 0)
 {
-    return function_exists('addCheckBox') ? addCheckBox('exclude['.$row_num.']', $exclude)
-    : '<input type="checkbox" name="exclude[' . $row_num .']"' . ($exclude ? ' checked="checked"' : '') . ' />';
+    return addCheckBox('exclude['.$row_num.']', $exclude);
 }
 
 /** Print one advanced form row
@@ -984,9 +983,11 @@ if ($search_advanced) {
  * - 'delete_saved'
  * @global string $submit
  */
+$searchpressed = false;
 if (isset($_GET['submit'])) {
     $submit = strip_tags($_GET['submit']);
 }
+
 /** Searched mailboxes
  * @global array $mailbox_array
  */
@@ -1354,7 +1355,9 @@ if (isset($aMailbox['FORWARD_SESSION'])) {
         // write the session in order to make sure that the compose window has
         // access to the composemessages array which is stored in the session
         session_write_close();
-        sqsession_is_active();
+        // restart the session. Do not use sqsession_is_active because the session_id
+        // isn't empty after a session_write_close
+        sqsession_start();
 
         if (!preg_match("/^[0-9]{3,4}$/", $compose_width)) {
             $compose_width = '640';
@@ -1382,7 +1385,7 @@ if (isset($aMailbox['FORWARD_SESSION'])) {
 }
 
 if (isset($note)) {
-    echo html_tag( 'div', '<b>' . $note .'</b>', 'center' ) . "<br />\n";
+    echo html_tag( 'div', '<b>' . htmlspecialchars($note) .'</b>', 'center' ) . "<br />\n";
 }
 
 
@@ -1447,7 +1450,7 @@ if ($submit == $search_button_text) {
     echo '</table>' . "\n";
 
     flush();
-
+    $iMsgCnt = 0;
     $query_error = asearch_check_query($where_array, $what_array, $exclude_array);
     if ($query_error != '') {
         echo '<br />' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n";
@@ -1516,8 +1519,8 @@ if ($submit == $search_button_text) {
             $aConfig['max_cache_size'] = count($mboxes_mailbox) +1;
             if (isset($startMessage) && $targetmailbox == $mbx) {
                 $aConfig['offset'] = $startMessage;
-            } else if (isset($aConfig['offset'])) {
-                unset($aConfig['offset']);
+            } else {
+                $aConfig['offset'] = 0;
             }
             if (isset($showall) && $targetmailbox == $mbx) {
                 $aConfig['showall'] = $showall;
@@ -1575,6 +1578,12 @@ if ($submit == $search_button_text) {
 
             $iError = 0;
             $aTemplate = showMessagesForMailbox($imapConnection, $aMailbox,$aProps, $iError);
+
+            // in th future we can make use of multiple message sets, now set it to 1 for search.
+            $iSetIndex = 1;
+            if (isset($aMailbox['UIDSET'][$iSetIndex])) {
+                $iMsgCnt += count($aMailbox['UIDSET'][$iSetIndex]);
+            }
             if ($iError) {
                 // error handling
             } else {
@@ -1586,38 +1595,37 @@ if ($submit == $search_button_text) {
                 $oTemplate = new Template($sTplDir);
 
                 if ($aMailbox['EXISTS'] > 0) {
-                    // $aTemplate =& showMessagesForMailbox($imapConnection,$aMailbox,$aProps,$iError);
                     if ($iError) {
-
-                    }
-                    foreach ($aTemplate as $k => $v) {
-                        $oTemplate->assign($k, $v);
-                    }
-                    $oTemplate->assign('page_selector',  $page_selector);
-                    $oTemplate->assign('page_selector_max', $page_selector_max);
-                    $oTemplate->assign('compact_paginator', $compact_paginator);
-                    $oTemplate->assign('javascript_on', $javascript_on);
-                    $oTemplate->assign('enablesort', (isset($aProps['config']['enablesort'])) ? $aProps['config']['enablesort'] : false);
-                    // Aaaaaahhhhhhh FIX ME DO NOT USE the string "none" for a var when you mean the boolean false or null
-                    $oTemplate->assign('icon_theme', (isset($icon_theme) && $icon_theme !== 'none') ? $icon_theme : false);
-                    $oTemplate->assign('use_icons', (isset($use_icons)) ? $use_icons : false);
-                    $oTemplate->assign('aOrder', array_keys($aColumns));
-                    $oTemplate->assign('alt_index_colors', isset($alt_index_colors) ? $alt_index_colors: false);
-                    $oTemplate->assign('color', $color);
-                    $oTemplate->assign('align', $align);
-                    $oTemplate->assign('showall', $showall);
-
-                    $mailbox_display = asearch_get_mailbox_display($aMailbox['NAME']);
-                    if (strtoupper($mbx) == 'INBOX') {
-                        $mailbox_display = _("INBOX");
+                       // TODO
+                       echo "ERROR occured, errorhandler will be implemented very soon";
                     } else {
-                        $mailbox_display = imap_utf7_decode_local($mbx);
-                    }
-
-                    echo '<br /><b><big>' . _("Folder:") . ' '. $mailbox_display . '&nbsp;</big></b>';
+                        foreach ($aTemplate as $k => $v) {
+                            $oTemplate->assign($k, $v);
+                        }
+                        $oTemplate->assign('page_selector',  $page_selector);
+                        $oTemplate->assign('page_selector_max', $page_selector_max);
+                        $oTemplate->assign('compact_paginator', $compact_paginator);
+                        $oTemplate->assign('javascript_on', $javascript_on);
+                        $oTemplate->assign('enablesort', (isset($aProps['config']['enablesort'])) ? $aProps['config']['enablesort'] : false);
+                        // Aaaaaahhhhhhh FIX ME DO NOT USE the string "none" for a var when you mean the boolean false or null
+                        $oTemplate->assign('icon_theme', (isset($icon_theme) && $icon_theme !== 'none') ? $icon_theme : false);
+                        $oTemplate->assign('use_icons', (isset($use_icons)) ? $use_icons : false);
+                        $oTemplate->assign('aOrder', array_keys($aColumns));
+                        $oTemplate->assign('alt_index_colors', isset($alt_index_colors) ? $alt_index_colors: false);
+                        $oTemplate->assign('color', $color);
+                        $oTemplate->assign('align', $align);
+
+                        $mailbox_display = asearch_get_mailbox_display($aMailbox['NAME']);
+                        if (strtoupper($mbx) == 'INBOX') {
+                            $mailbox_display = _("INBOX");
+                        } else {
+                            $mailbox_display = imap_utf7_decode_local($mbx);
+                        }
 
+                        echo '<br /><b><big>' . _("Folder:") . ' '. htmlspecialchars($mailbox_display) . '&nbsp;</big></b>';
 
-                    $oTemplate->display('message_list.tpl');
+                        $oTemplate->display('message_list.tpl');
+                    }
                 }
             }
 
@@ -1626,7 +1634,7 @@ if ($submit == $search_button_text) {
 
         }
     }
-    if(!$msgsfound) {
+    if(!$iMsgCnt) {
         echo '<br />' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n";
     }
 }
@@ -1635,4 +1643,5 @@ do_hook('search_bottom');
 sqimap_logout($imapConnection);
 echo '</body></html>';
 sqsession_register($mailbox_cache,'mailbox_cache');
-?>
\ No newline at end of file
+
+?>