Happy New Year
[squirrelmail.git] / src / search.php
index 6390cf5bb84eed500e3723fac79bf6faccc1a669..d75b372c7b9a2c42db82c39eb66c92e19b28ba22 100644 (file)
@@ -8,7 +8,7 @@
  * 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-2009 The SquirrelMail Project Team
+ * @copyright 1999-2018 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package squirrelmail
@@ -580,7 +580,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 = ' <span class="mailbox">' . htmlspecialchars(asearch_get_mailbox_display($cur_mailbox)) . '</span>';
+                $mailbox_display = ' <span class="mailbox">' . sm_encode_html_special_chars(asearch_get_mailbox_display($cur_mailbox)) . '</span>';
                 if ($query_display == '')
                     $biop_display = _("In");
                 else
@@ -606,7 +606,7 @@ function asearch_get_query_display(&$color, &$mailbox_array, &$biop_array, &$uno
                     if ($what_type == 'adate')
                         $what_display = asearch_get_date_display($what);
                     else
-                        $what_display = htmlspecialchars($what);
+                        $what_display = sm_encode_html_special_chars($what);
                     $what_display = ' <span class="value">' . $what_display . '</span>';
                 }
             }
@@ -674,13 +674,13 @@ function asearch_print_query_array(&$boxes, &$query_array, &$query_keys, &$actio
     $oTemplate->assign('expand_collapse_toggle', '../src/search.php?'.$show_pref.'='.($show_flag==1 ? 0 : 1));
     $oTemplate->assign('query_list', $a);
     
-    $oTemplate->assign('save_recent', '../src/search.php?submit=save_recent&rownum=');
-    $oTemplate->assign('do_recent', '../src/search.php?submit=search_recent&rownum=');
-    $oTemplate->assign('forget_recent', '../src/search.php?submit=forget_recent&rownum=');
+    $oTemplate->assign('save_recent', '../src/search.php?submit=save_recent&smtoken=' . sm_generate_security_token() . '&rownum=');
+    $oTemplate->assign('do_recent', '../src/search.php?submit=search_recent&smtoken=' . sm_generate_security_token() . '&rownum=');
+    $oTemplate->assign('forget_recent', '../src/search.php?submit=forget_recent&smtoken=' . sm_generate_security_token() . '&rownum=');
     
-    $oTemplate->assign('edit_saved', '../src/search.php?submit=edit_saved&rownum=');
-    $oTemplate->assign('do_saved', '../src/search.php?submit=search_saved&rownum=');
-    $oTemplate->assign('delete_saved', '../src/search.php?submit=delete_saved&rownum=');
+    $oTemplate->assign('edit_saved', '../src/search.php?submit=edit_saved&smtoken=' . sm_generate_security_token() . '&rownum=');
+    $oTemplate->assign('do_saved', '../src/search.php?submit=search_saved&smtoken=' . sm_generate_security_token() . '&rownum=');
+    $oTemplate->assign('delete_saved', '../src/search.php?submit=delete_saved&smtoken=' . sm_generate_security_token() . '&rownum=');
     
     $oTemplate->display('search_list.tpl');
 }
@@ -782,7 +782,7 @@ function asearch_print_form($imapConnection, &$boxes, $mailbox_array, $biop_arra
     # Build the mailbox array
     $a = array();
     if (($mailbox != 'All Folders') && (!asearch_mailbox_exists($mailbox, $boxes))) {
-        $a[$mailbox] = '[' . _("Missing") . '] ' . htmlspecialchars(asearch_get_mailbox_display($mailbox));
+        $a[$mailbox] = '[' . _("Missing") . '] ' . sm_encode_html_special_chars(asearch_get_mailbox_display($mailbox));
     }
     $a['All Folders'] = '[' . asearch_get_mailbox_display('All Folders') . ']';
     $a = array_merge($a, sqimap_mailbox_option_array($imapConnection, 0, $boxes, NULL));
@@ -806,7 +806,8 @@ function asearch_print_form($imapConnection, &$boxes, $mailbox_array, $biop_arra
 
     $oTemplate->assign('criteria', $c);
     
-    echo '<form action="../src/search.php" name="form_asearch">' . "\n";
+    echo '<form action="../src/search.php" name="form_asearch">' . "\n"
+       . addHidden('smtoken', sm_generate_security_token()) . "\n";
     $oTemplate->display('search_advanced.tpl');
     echo "</form>\n";
 }
@@ -840,7 +841,7 @@ function asearch_print_form_basic($imapConnection, &$boxes, $mailbox_array, $bio
     # Build the mailbox array
     $a = array();
     if (($mailbox != 'All Folders') && (!asearch_mailbox_exists($mailbox, $boxes))) {
-        $a[$mailbox] = '[' . _("Missing") . '] ' . htmlspecialchars(asearch_get_mailbox_display($mailbox));
+        $a[$mailbox] = '[' . _("Missing") . '] ' . sm_encode_html_special_chars(asearch_get_mailbox_display($mailbox));
     }
     $a['All Folders'] = '[' . asearch_get_mailbox_display('All Folders') . ']';
     $a = array_merge($a, sqimap_mailbox_option_array($imapConnection, 0, $boxes, NULL));
@@ -861,12 +862,13 @@ function asearch_print_form_basic($imapConnection, &$boxes, $mailbox_array, $bio
     $oTemplate->assign('unary_options', $imap_asearch_unops);
     $oTemplate->assign('where_options', $imap_asearch_options);
     
-    $oTemplate->assign('mailbox_sel', strtolower(htmlspecialchars($mailbox)));
+    $oTemplate->assign('mailbox_sel', strtolower(sm_encode_html_special_chars($mailbox)));
     $oTemplate->assign('unary_sel', $unop);
     $oTemplate->assign('where_sel', $where);
     $oTemplate->assign('what_val', $what);
         
-    echo '<form action="../src/search.php" name="form_asearch">' . "\n";
+    echo '<form action="../src/search.php" name="form_asearch">' . "\n"
+       . addHidden('smtoken', sm_generate_security_token()) . "\n";
     $oTemplate->display('search.tpl');
     echo "</form>\n";
 }
@@ -891,6 +893,7 @@ function sqimap_asearch_get_selectable_unformatted_mailboxes(&$boxes)
 
 /* ------------------------ main ------------------------ */
 /* get globals we will need */
+sqgetGlobalVar('smtoken', $submitted_token, SQ_FORM, '');
 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
 
 if (!sqgetGlobalVar('checkall',$checkall,SQ_GET)) {
@@ -1179,6 +1182,10 @@ if ((empty($submit)) && (!empty($where_array))) {
 if (!isset($submit)) {
     $submit = '';
 } else {
+
+    // first validate security token
+    sm_validate_security_token($submitted_token, -1, TRUE);
+
     switch ($submit) {
       case $search_button_text:
         if (asearch_check_query($where_array, $what_array, $exclude_array) == '') {
@@ -1314,7 +1321,8 @@ if ($search_advanced) {
 uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
 
 /* open IMAP connection */
-$imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
+global $imap_stream_options; // in case not defined in config
+$imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0, $imap_stream_options);
 
 
 /* get mailboxes once here */
@@ -1374,16 +1382,20 @@ if (isset($aMailbox['FORWARD_SESSION'])) {
             $compose_height = '550';
         }
         // do not use &amp;, it will break the query string and $session will not be detected!!!
-        $comp_uri = SM_PATH . 'src/compose.php?mailbox='. urlencode($mailbox).
-                    '&session='.$aMailbox['FORWARD_SESSION'];
+        $comp_uri = $base_uri . 'src/compose.php?mailbox='. urlencode($mailbox)
+                  . '&session='.$aMailbox['FORWARD_SESSION']['SESSION_NUMBER']
+                  . '&smaction=forward_as_attachment'
+                  . '&fwduid=' . implode('_', $aMailbox['FORWARD_SESSION']['UIDS']);
         displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri', $compose_width, $compose_height);", false);
     } else {
         // save mailboxstate
         sqsession_register($aMailbox,'aLastSelectedMailbox');
         session_write_close();
         // we have to redirect to the compose page
-        $location = SM_PATH . 'src/compose.php?mailbox='. urlencode($mailbox).
-                    '&session='.$aMailbox['FORWARD_SESSION'];
+        $location = $base_uri . 'src/compose.php?mailbox='. urlencode($mailbox)
+                  . '&session='.$aMailbox['FORWARD_SESSION']['SESSION_NUMBER']
+                  . '&smaction=forward_as_attachment'
+                  . '&fwduid=' . implode('_', $aMailbox['FORWARD_SESSION']['UIDS']);
         header("Location: $location");
         exit;
     }
@@ -1593,7 +1605,7 @@ if ($submit == $search_button_text) {
                  */
                 if ($aMailbox['EXISTS'] > 0) {
                     if ($iError) {
-                       // TODO
+                       // TODO: Implement an error handler in the search page.
                        echo "ERROR occured, errorhandler will be implemented very soon";
                     } else {
                         foreach ($aTemplate as $k => $v) {
@@ -1607,7 +1619,7 @@ if ($submit == $search_button_text) {
                             $mailbox_display = imap_utf7_decode_local($mbx);
                         }
 
-                        $oTemplate->assign('mailbox_name', htmlspecialchars($mailbox_display));
+                        $oTemplate->assign('mailbox_name', sm_encode_html_special_chars($mailbox_display));
                         $oTemplate->display('search_result_mailbox.tpl');
 
                         $oTemplate->assign('page_selector',  $page_selector);