Source for file search.php

Documentation is available at search.php

  1. <?php
  2.  
  3. /**
  4.  * search.php
  5.  *
  6.  * IMAP search page
  7.  *
  8.  * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  9.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.  * @version $Id: search.php,v 1.92.2.18 2006/07/27 18:58:48 tokul Exp $
  11.  * @package squirrelmail
  12.  * @subpackage search
  13.  */
  14.  
  15. /**
  16.  * Path for SquirrelMail required files.
  17.  * @ignore
  18.  */
  19. define('SM_PATH','../');
  20.  
  21. /** SquirrelMail required files.
  22.  */
  23. require_once(SM_PATH 'include/validate.php');
  24. require_once(SM_PATH 'functions/imap.php');
  25. require_once(SM_PATH 'functions/imap_search.php');
  26. require_once(SM_PATH 'functions/imap_mailbox.php');
  27. require_once(SM_PATH 'functions/strings.php');
  28.  
  29. global $allow_thread_sort;
  30.  
  31. /* get globals we may need */
  32.  
  33. sqgetGlobalVar('username'$usernameSQ_SESSION);
  34. sqgetGlobalVar('key'$keySQ_COOKIE);
  35. sqgetGlobalVar('delimiter'$delimiterSQ_SESSION);
  36. sqgetGlobalVar('onetimepad'$onetimepadSQ_SESSION);
  37. sqgetGlobalVar('composenew' $composenewSQ_FORM);
  38. sqgetGlobalVar('composesession' $composesession SQ_SESSION);
  39.  
  40. if (!sqgetGlobalVar('mailbox',$mailbox,SQ_GET)) {
  41.     unset($mailbox);
  42. }
  43. if (!sqgetGlobalVar('submit',$submit,SQ_GET)) {
  44.     $submit '';
  45. }
  46. if (!sqgetGlobalVar('what',$what,SQ_GET)) {
  47.     $what='';
  48. }
  49. if (sqgetGlobalVar('where',$where,SQ_GET||
  50.     in_array$wherearray('BODY','TEXT','SUBJECT','FROM','CC','TO'))) {
  51.     // make sure that 'where' is one if standard IMAP SEARCH keywords
  52.         $where 'FROM';
  53. }
  54. // FIXME: what is this?
  55. if (!sqgetGlobalVar('checkall',$checkall,SQ_GET)) {
  56.     unset($checkall);
  57. }
  58. if (sqgetGlobalVar('count',$count,SQ_GET)) {
  59.     $count = (int) $count;
  60. else {
  61.     unset($count);
  62. }
  63. /* end of get globals */
  64.  
  65. /*  here are some functions, could go in imap_search.php
  66.     this was here, pretty handy  */
  67. function s_opt$val$sel$tit {
  68.     echo "            <option value=\"$val\"";
  69.     if $sel == $val {
  70.         echo ' selected="selected"';
  71.     }
  72.     echo  ">$tit</option>\n";
  73. }
  74.  
  75. /*  function to get the recent searches and put them in the attributes array  */
  76. function get_recent($username$data_dir{
  77.     $attributes array();
  78.     $types array('search_what''search_where''search_folder');
  79.     $recent_count getPref($data_dir$username'search_memory'0);
  80.     for ($x=1;$x<=$recent_count;$x++{
  81.         reset($types);
  82.         foreach ($types as $key{
  83.             $attributes[$key][$xgetPref($data_dir$username$key.$x"");
  84.         }
  85.     }
  86.     return $attributes;
  87. }
  88.  
  89. /*  function to get the saved searches and put them in the saved_attributes array  */
  90. function get_saved($username$data_dir{
  91.     $saved_attributes array();
  92.     $types array('saved_what''saved_where''saved_folder');
  93.     foreach ($types as $key{
  94.         for ($x=1;;$x++{
  95.             $prefval getPref($data_dir$username$key."$x""");
  96.             if ($prefval == ""{
  97.                 break;
  98.             else {
  99.                 $saved_attributes[$key][$x$prefval;
  100.             }
  101.         }
  102.     }
  103.     return $saved_attributes;
  104. }
  105.  
  106. /*  function to update recent pref arrays  */
  107. function update_recent($what$where$mailbox$username$data_dir{
  108.     $attributes array();
  109.     $types array('search_what''search_where''search_folder');
  110.     $input array($what$where$mailbox);
  111.     $attributes get_recent$username$data_dir);
  112.     reset($types);
  113.     $dupe 'no';
  114.     for ($i=1;$i<=count($attributes['search_what']);$i++{
  115.         if (isset($attributes['search_what'][$i])) {
  116.             if ($what == $attributes['search_what'][$i&&
  117.                 $where == $attributes['search_where'][$i&&
  118.                 $mailbox == $attributes['search_folder'][$i]{
  119.                     $dupe 'yes';
  120.             }
  121.         }
  122.     }
  123.     if ($dupe == 'no'{
  124.         $i 0;
  125.         foreach ($types as $key{
  126.             array_push ($attributes[$key]$input[$i]);
  127.             array_shift ($attributes[$key]);
  128.             $i++;
  129.         }
  130.         $recent_count getPref($data_dir$username'search_memory'0);
  131.         $n=0;
  132.         for ($i=1;$i<=$recent_count;$i++{
  133.             reset($types);
  134.             foreach ($types as $key{
  135.                 setPref($data_dir$username$key.$i$attributes[$key][$n]);
  136.             }
  137.             $n++;
  138.         }
  139.     }
  140. }
  141.  
  142. /*  function to forget a recent search  */
  143. function forget_recent($forget_index$username$data_dir{
  144.     $attributes array();
  145.     $types array('search_what''search_where''search_folder');
  146.     $attributes get_recent$username$data_dir);
  147.     reset($types);
  148.     foreach ($types as $key{
  149.         array_splice($attributes[$key]$forget_index 11);
  150.         array_unshift($attributes[$key]'');
  151.     }
  152.     reset($types);
  153.     $recent_count getPref($data_dir$username'search_memory'0);
  154.     $n=0;
  155.     for ($i=1;$i<=$recent_count;$i++{
  156.         reset($types);
  157.         foreach ($types as $key{
  158.             setPref($data_dir$username$key.$i$attributes[$key][$n]);
  159.         }
  160.         $n++;
  161.     }
  162. }
  163.  
  164. /*  function to delete a saved search  */
  165. function delete_saved($delete_index$username$data_dir{
  166.     $types array('saved_what''saved_where''saved_folder');
  167.     $attributes get_saved($username$data_dir);
  168.     foreach ($types as $key{
  169.         array_splice($attributes[$key]$delete_index1);
  170.     }
  171.     reset($types);
  172.     $n=0;
  173.     $saved_count count($attributes['saved_what']);
  174.     $last_element $saved_count 1;
  175.         for ($i=1;$i<=$saved_count;$i++{
  176.             reset($types);
  177.             foreach ($types as $key{
  178.                 setPref($data_dir$username$key.$i$attributes[$key][$n]);
  179.             }
  180.         $n++;
  181.         }
  182.     reset($types);
  183.     foreach($types as $key{
  184.     removePref($data_dir$username$key.$last_element);
  185.     }
  186. }
  187.  
  188. /*  function to save a search from recent to saved  */
  189. function save_recent($save_index$username$data_dir{
  190.     $attributes array();
  191.     $types array('search_what''search_where''search_folder');
  192.     $saved_types array(=> 'saved_what'=> 'saved_where'=> 'saved_folder');
  193.     $saved_array get_saved($username$data_dir);
  194.     $save_index $save_index -1;
  195.     if (isset($saved_array['saved_what'])) {
  196.         $saved_count (count($saved_array['saved_what']1);
  197.     else {
  198.         // there are no saved searches. Function is used to save first search
  199.         $saved_count 1;
  200.     }
  201.     $attributes get_recent ($username$data_dir);
  202.     $n 0;
  203.     foreach ($types as $key{
  204.         $slice array_slice($attributes[$key]$save_index1);
  205.         $name $saved_types[$n];
  206.         setPref($data_dir$username$name.$saved_count$slice[0]);
  207.         $n++;
  208.     }
  209. }
  210.  
  211. function printSearchMessages($msgs,$mailbox$cnt$imapConnection$where$what$usecache false$newsort false{
  212.     global $sort$color$allow_server_sort$allow_server_thread;
  213.     
  214.     if ($cnt 0{
  215.         if ((!empty($allow_server_sort&& $allow_server_sort|| (!empty($allow_server_thread&& $allow_server_thread)) {
  216.             $msort $msgs;
  217.         else {
  218.             $msort calc_msort($msgs$sort);
  219.         }
  220.  
  221.         if $mailbox == 'INBOX' {
  222.             $showbox _("INBOX");
  223.         else {
  224.             $showbox imap_utf7_decode_local($mailbox);
  225.         }
  226.         echo html_tag'div''<b><big>' _("Folder:"' '
  227.             htmlspecialchars($showbox.'</big></b>','center'"\n";
  228.  
  229.         $msg_cnt_str get_msgcnt_str(1$cnt$cnt);
  230.         $toggle_all get_selectall_link(1$sort);
  231.  
  232.         echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
  233.         echo '<tr><td>';
  234.  
  235.         mail_message_listing_beginning($imapConnection$mailbox$sort
  236.                                        $msg_cnt_str$toggle_all1);
  237.  
  238.         echo '</td></tr>';
  239.         echo '<tr><td height="5" bgcolor="'.$color[4].'"></td></tr>';  
  240.         echo '<tr><td>';
  241.         echo '    <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
  242.         echo '     <tr><td>';
  243.         echo '       <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
  244.         echo '<tr><td>';
  245.  
  246.         printHeader($mailbox6$colorfalse);
  247.  
  248.         displayMessageArray($imapConnection$cnt1
  249.             $msort$mailbox$sort$color$cnt$where$what);
  250.  
  251.         echo '</td></tr></table></td></tr></table>';
  252.         mail_message_listing_end($cnt''$msg_cnt_str$color)
  253.         echo '</td></tr></table>';
  254.     }
  255. }
  256.  
  257. /* ------------------------ main ------------------------ */
  258.  
  259. /*  reset these arrays on each page load just in case  */
  260. $attributes array ();
  261. $saved_attributes array ();
  262. $search_all 'none';
  263. $perbox_count array ();
  264. $recent_count getPref($data_dir$username'search_memory'0);
  265.  
  266. /*  get mailbox names  */
  267. $imapConnection sqimap_login($username$key$imapServerAddress$imapPort0);
  268. $boxes sqimap_mailbox_list($imapConnection);
  269.  
  270. /*  set current mailbox to INBOX if none was selected or if page
  271.     was called to search all folders.  */
  272. if !isset($mailbox|| $mailbox == 'None' || $mailbox == '' {
  273.     $mailbox $boxes[0]['unformatted'];
  274. }
  275. if ($mailbox == 'All Folders'{
  276.     $search_all 'all';
  277. }
  278.  
  279. if (isset($composenew&& $composenew{
  280.     $comp_uri "../src/compose.php?mailbox="urlencode($mailbox).
  281.         "&session=$composesession&attachedmessages=true&amp";
  282.     displayPageHeader($color$mailbox"comp_in_new('$comp_uri');"false);
  283. else {
  284.     displayPageHeader($color$mailbox);
  285. }
  286. /*  See how the page was called and fire off correct function  */
  287. if (empty($submit&& !empty($what)) {
  288.     $submit _("Search");
  289. }
  290.  
  291. if ($submit == _("Search"&& !empty($what)) {
  292.     if ($recent_count 0{
  293.         update_recent($what$where$mailbox$username$data_dir);
  294.     }
  295. }
  296. elseif ($submit == 'forget' && isset($count)) {
  297.     forget_recent($count$username$data_dir);
  298. }
  299. elseif ($submit == 'save' && isset($count)) {
  300.     save_recent($count$username$data_dir);
  301. }
  302. elseif ($submit == 'delete' && isset($count)) {
  303.     delete_saved($count$username$data_dir);
  304. }
  305.  
  306. do_hook('search_before_form');
  307.  
  308. echo html_tag'table',
  309.          html_tag'tr'"\n" .
  310.              html_tag'td''<b>' _("Search"'</b>''center'$color[0)
  311.          ,
  312.      '''''width="100%"'"\n";
  313.  
  314. /*  update the recent and saved searches from the pref files  */
  315. $attributes get_recent($username$data_dir);
  316. $saved_attributes get_saved($username$data_dir);
  317. if (isset($saved_attributes['saved_what'])) {
  318.     $saved_count count($saved_attributes['saved_what']);
  319. else {
  320.     $saved_count 0;
  321. }
  322. $count_all 0;
  323.  
  324. /* Saved Search Table */
  325. if ($saved_count 0{
  326.     echo "<br />\n"
  327.     . html_tag'table''''center'$color[9]'width="95%" cellpadding="1" cellspacing="1" border="0"' )
  328.     . html_tag'tr',
  329.           html_tag'td''<b>'._("Saved Searches"'</b>''center' )
  330.       )
  331.     . html_tag'tr' )
  332.     . html_tag'td' )
  333.     . html_tag'table''''center''''width="100%" cellpadding="2" cellspacing="2" border="0"' );
  334.     for ($i=0$i $saved_count++$i{
  335.         if ($i 2{
  336.             echo html_tag'tr'''''$color[0);
  337.         else {
  338.             echo html_tag'tr'''''$color[4);
  339.         }
  340.         echo html_tag'td'htmlspecialchars(imap_utf7_decode_local($saved_attributes['saved_folder'][$i 1]))'left''''width="35%"' )
  341.         . html_tag'td'htmlspecialchars($saved_attributes['saved_what'][$i 1])'left' )
  342.         . html_tag'td'htmlspecialchars($saved_attributes['saved_where'][$i 1])'center' )
  343.         . html_tag'td''''right' )
  344.         .   '<a href="search.php'
  345.         .     '?mailbox=' urlencode($saved_attributes['saved_folder'][$i 1])
  346.         .     '&amp;what=' urlencode($saved_attributes['saved_what'][$i 1])
  347.         .     '&amp;where=' urlencode($saved_attributes['saved_where'][$i 1])
  348.         .   '">' _("edit"'</a>'
  349.         .   '&nbsp;|&nbsp;'
  350.         .   '<a href="search.php'
  351.         .     '?mailbox=' urlencode($saved_attributes['saved_folder'][$i 1])
  352.         .     '&amp;what=' urlencode($saved_attributes['saved_what'][$i 1])
  353.         .     '&amp;where=' urlencode($saved_attributes['saved_where'][$i 1])
  354.         .     '&amp;submit=Search_no_update'
  355.         .   '">' _("search"'</a>'
  356.         .   '&nbsp;|&nbsp;'
  357.         .   "<a href=\"search.php?count=$i&amp;submit=delete\">"
  358.         .     _("delete")
  359.         .   '</a>'
  360.         . '</td></tr>';
  361.     }
  362.     echo "</table></td></tr></table>\n";
  363. }
  364.  
  365. if ($recent_count 0{
  366.     echo "<br />\n"
  367.        . html_tag'table''''center'$color[9]'width="95%" cellpadding="1" cellspacing="1" border="0"' )
  368.        . html_tag'tr',
  369.              html_tag'td''<b>' _("Recent Searches"'</b>''center' )
  370.          )
  371.        . html_tag'tr' )
  372.        . html_tag'td' )
  373.        . html_tag'table''''center''''width="100%" cellpadding="0" cellspacing="0" border="0"' );
  374.     for ($i=1$i <= $recent_count++$i{
  375.             if (isset($attributes['search_folder'][$i])) 
  376.             if ($attributes['search_folder'][$i== ""{
  377.                 $attributes['search_folder'][$i"INBOX";
  378.             }
  379.             }
  380.             if ($i 2{
  381.                 echo html_tag'tr'''''$color[0);
  382.             else {
  383.                 echo html_tag'tr'''''$color[0);
  384.             }
  385.             if (isset($attributes['search_what'][$i]&&
  386.                 !empty($attributes['search_what'][$i])) {
  387.             echo html_tag'td'htmlspecialchars(imap_utf7_decode_local($attributes['search_folder'][$i]))'left''''width="35%"' )
  388.                . html_tag'td'htmlspecialchars($attributes['search_what'][$i])'left' )
  389.                . html_tag'td'htmlspecialchars($attributes['search_where'][$i])'center' )
  390.                . html_tag'td''''right' )
  391.                .   "<a href=\"search.php?count=$i&amp;submit=save\">"
  392.                .     _("save")
  393.                .   '</a>'
  394.                .   '&nbsp;|&nbsp;'
  395.                .   '<a href="search.php'
  396.                .     '?mailbox=' urlencode($attributes['search_folder'][$i])
  397.                .     '&amp;what=' urlencode($attributes['search_what'][$i])
  398.                .     '&amp;where=' urlencode($attributes['search_where'][$i])
  399.                .     '&amp;submit=Search_no_update'
  400.                .   '">' _("search"'</a>'
  401.                .   '&nbsp;|&nbsp;'
  402.                .   "<a href=\"search.php?count=$i&amp;submit=forget\">"
  403.                .     _("forget")
  404.                .   '</a>'
  405.                . '</td></tr>';
  406.         }
  407.         }
  408.     echo '</table></td></tr></table><br />';
  409. }
  410.  
  411. /** FIXME: remove or fix it. $newsort is not set and not extracted from request
  412. if (isset($newsort)) {
  413.     $sort = $newsort;
  414.     sqsession_register($sort, 'sort');
  415. }*/
  416.  
  417. /*******************************************************************
  418.  * Check to see if we can use cache or not. Currently the only time  *
  419.  * when you will not use it is when a link on the left hand frame is *
  420.  * used. Also check to make sure we actually have the array in the   *
  421.  * registered session data.  :)                                      *
  422.  *********************************************************************/
  423.  
  424.  
  425. /** FIXME: remove or fix it. $use_mailbox_cache is not set and not extracted from request
  426. if (! isset($use_mailbox_cache)) {
  427.     $use_mailbox_cache = 0;
  428. }*/
  429.  
  430. /* There is a problem with registered vars in 4.1 */
  431. /*
  432. if( substr( phpversion(), 0, 3 ) == '4.1'  ) {
  433.     $use_mailbox_cache = FALSE;
  434. }
  435. */
  436.  
  437. /* Search Form */
  438. echo html_tag'div''<b>' _("Current Search"'</b>''left' "\n"
  439.    . '<form action="search.php" name="s">'
  440.    . html_tag'table''''''''width="95%" cellpadding="0" cellspacing="0" border="0"' )
  441.    . html_tag'tr' )
  442.    . html_tag'td''''left' )
  443.    . '<select name="mailbox">'
  444.    . '<option value="All Folders"';
  445.    if ($mailbox == 'All Folders'{
  446.        echo ' selected="selected"';
  447.    }
  448.    echo '>[ ' _("All Folders"" ]</option>\n";
  449.  
  450.    $show_selected array(strtolower($mailbox));
  451.    echo sqimap_mailbox_option_list($imapConnection$show_selected0$boxes);
  452.  
  453.    echo '         </select>'.
  454.         "       </td>\n";
  455.  
  456. // FIXME: explain all str_replace calls.
  457. $what_disp str_replace(','' '$what);
  458. $what_disp str_replace('\\\\''\\'$what_disp);
  459. $what_disp str_replace('\\"''"'$what_disp);
  460. $what_disp str_replace('"''&quot;'$what_disp);
  461. echo html_tag'td''<input type="text" size="35" name="what" value="' $what_disp '" />' "\n"'center' )
  462.      . html_tag'td''''right' )
  463.      . "<select name=\"where\">";
  464. s_opt'BODY'$where_("Body") );
  465. s_opt'TEXT'$where_("Everywhere") );
  466. s_opt'SUBJECT'$where_("Subject") );
  467. s_opt'FROM'$where_("From") );
  468. s_opt'CC'$where_("Cc") );
  469. s_opt'TO'$where_("To") );
  470. echo "         </select>\n" .
  471.      "        </td>\n".
  472.      html_tag'td''<input type="submit" name="submit" value="' _("Search"'" />' "\n"'center''''colspan="3"' .
  473.      "     </tr>\n".
  474.      "</form>\n".
  475.      "   </table>\n".
  476.      "</td></tr></table>\n";
  477.  
  478.  
  479. do_hook('search_after_form');
  480.  
  481.  
  482. /*
  483.     search all folders option still in the works. returns a table for each
  484.     folder it finds a match in.
  485. */
  486.  
  487. $old_value 0;
  488. if ($allow_thread_sort == TRUE{
  489.     $old_value $allow_thread_sort;
  490.     $allow_thread_sort FALSE;
  491. }
  492.  
  493. if ($search_all == 'all'{
  494.     $mailbox == '';
  495.     $boxcount count($boxes);
  496.     echo '<br /><center><b>' .
  497.          _("Search Results".
  498.          "</b></center><br />\n";
  499.     for ($x=0;$x<$boxcount;$x++{
  500.         if (!in_array('noselect'$boxes[$x]['flags'])) {
  501.             $mailbox $boxes[$x]['unformatted'];
  502.         }
  503.         if (($submit == _("Search"|| $submit == 'Search_no_update'&& !empty($what)) {
  504.             sqimap_mailbox_select($imapConnection$mailbox);
  505.             $msgs sqimap_search($imapConnection$where$what$mailbox$color0$search_all$count_all);
  506.             $count_all count($msgs);
  507.             printSearchMessages($msgs$mailbox$count_all$imapConnection
  508.                                 $where$whatfalsefalse);
  509.             array_push($perbox_count$count_all);
  510.         }
  511.     }
  512.     for ($i=0;$i<count($perbox_count);$i++{
  513.         if ($perbox_count[$i]{
  514.             $count_all true;
  515.             break;
  516.         }
  517.     }
  518.     if (!$count_all{
  519.        echo '<br /><center>' _("No Messages Found"'</center>';
  520.     }
  521. }
  522.  
  523. /*  search one folder option  */
  524. else {
  525.     if (($submit == _("Search"|| $submit == 'Search_no_update'&& !empty($what)) {
  526.         echo '<br />'
  527.         . html_tag'div''<b>' _("Search Results"'</b>''center' "\n";
  528.         sqimap_mailbox_select($imapConnection$mailbox);
  529.         $msgs sqimap_search($imapConnection$where$what$mailbox$color0$search_all$count_all);
  530.         if (count($msgs)) {
  531.             printSearchMessages($msgs$mailboxcount($msgs)$imapConnection,
  532.                                 $where$whatfalsefalse);
  533.         else {
  534.             echo '<br /><center>' _("No Messages Found"'</center>';
  535.         }
  536.     }
  537. }
  538.  
  539. /*  must have search terms to search  */
  540. if ($submit == _("Search"&& empty($what)) {
  541.         echo '<br />'
  542.         . html_tag'div''<b>' _("Please enter something to search for"'</b>''center' "\n";
  543. }
  544.  
  545. $allow_thread_sort $old_value;
  546.  
  547.  
  548. do_hook('search_bottom');
  549. sqimap_logout($imapConnection);
  550. echo '</body></html>';
  551. ?>

Documentation generated on Sat, 07 Oct 2006 16:33:20 +0300 by phpDocumentor 1.3.0RC6