Source for file left_main.php

Documentation is available at left_main.php

  1. <?php
  2.  
  3. /**
  4.  * left_main.php
  5.  *
  6.  * This is the code for the left bar. The left bar shows the folders
  7.  * available, and has cookie information.
  8.  *
  9.  * @copyright 1999-2020 The SquirrelMail Project Team
  10.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11.  * @version $Id: left_main.php 14840 2020-01-07 07:42:38Z pdontthink $
  12.  * @package squirrelmail
  13.  */
  14.  
  15. /** This is the left_main page */
  16. define('PAGE_NAME''left_main');
  17.  
  18. /**
  19.  * Path for SquirrelMail required files.
  20.  * @ignore
  21.  */
  22. define('SM_PATH','../');
  23.  
  24. $script_libs array();
  25.  
  26. /* SquirrelMail required files. */
  27. require_once(SM_PATH 'include/validate.php');
  28. require_once(SM_PATH 'functions/imap.php');
  29. require_once(SM_PATH 'functions/plugin.php');
  30. require_once(SM_PATH 'functions/page_header.php');
  31. require_once(SM_PATH 'functions/html.php');
  32.  
  33. /* These constants are used for folder stuff. */
  34. define('SM_BOX_UNCOLLAPSED'0);
  35. define('SM_BOX_COLLAPSED',   1);
  36.  
  37. /* --------------------- FUNCTIONS ------------------------- */
  38.  
  39. function formatMailboxName($imapConnection$box_array{
  40.  
  41.     global $folder_prefix$trash_folder$sent_folder,
  42.            $color$move_to_sent$move_to_trash,
  43.            $unseen_notify$unseen_type$collapse_folders,
  44.            $draft_folder$save_as_draft,
  45.            $use_special_folder_color;
  46.     $real_box $box_array['unformatted'];
  47.     $mailbox str_replace('&nbsp;','',$box_array['formatted']);
  48.     $mailboxURL urlencode($real_box);
  49.  
  50.     /* Strip down the mailbox name. */
  51.     if (preg_match('/^( *)([^ ]*)$/'$mailbox$regs)) {
  52.         $mailbox $regs[2];
  53.     }
  54.     $unseen 0;
  55.     $status array('','');
  56.     if (($unseen_notify == && $real_box == 'INBOX'||
  57.         $unseen_notify == 3{
  58.             $tmp_status create_unseen_string($real_box$box_array$imapConnection$unseen_type );
  59.             if ($status !== false{
  60.                 $status $tmp_status;
  61.             }
  62.     }
  63.     list($unseen_string$unseen$status;
  64.     $special_color ($use_special_folder_color && isSpecialMailbox($real_box));
  65.  
  66.     /* Start off with a blank line. */
  67.     $line '';
  68.  
  69.     /* If there are unseen message, bold the line. */
  70.     if ($unseen 0$line .= '<b>'}
  71.  
  72.     /* Create the link for this folder. */
  73.     if ($status !== false{
  74.         $line .= '<a href="right_main.php?PG_SHOWALL=0&amp;sort=0&amp;startMessage=1&amp;mailbox='.
  75.                  $mailboxURL.'" target="right" title="' ($mailbox == 'INBOX' _("INBOX"$mailbox'" style="text-decoration:none">';
  76.     }
  77.     if ($special_color{
  78.         $line .= "<font color=\"$color[11]\">";
  79.     }
  80.     if $mailbox == 'INBOX' {
  81.         $line .= _("INBOX");
  82.     else {
  83.         $line .= str_replace(array(' ','<','>'),array('&nbsp;','&lt;','&gt;'),$mailbox);
  84.     }
  85.     if ($special_color == TRUE)
  86.         $line .= '</font>';
  87.     if ($status !== false{
  88.         $line .= '</a>';
  89.     }
  90.  
  91.     /* If there are unseen message, close bolding. */
  92.     if ($unseen 0$line .= "</b>"}
  93.  
  94.     /* Print unseen information. */
  95.     if ($unseen_string != ''{
  96.         $line .= "&nbsp;<small>$unseen_string</small>";
  97.     }
  98.  
  99.     /* If it's the trash folder, show a purge link when needed */
  100.     if (($move_to_trash&& ($real_box == $trash_folder)) {
  101.         if (isset($numMessages)) {
  102.             $numMessages sqimap_get_num_messages($imapConnection$real_box);
  103.         }
  104.  
  105.         if (($numMessages 0or ($box_array['parent'== 1)) {
  106.             $urlMailbox urlencode($real_box);
  107.             $line .= "\n<small>\n" .
  108.                     '&nbsp;&nbsp;(<a href="empty_trash.php?smtoken=' sm_generate_security_token('" style="text-decoration:none">'._("Purge").'</a>)' .
  109.                     '</small>';
  110.         }
  111.     }
  112.  
  113.     $line .= concat_hook_function('left_main_after_each_folder',
  114.                                   array(isset($numMessages$numMessages '',
  115.                                         $real_box$imapConnection));
  116.  
  117.     /* Return the final product. */
  118.     return ($line);
  119. }
  120.  
  121. /**
  122.  * Recursive function that computes the collapsed status and parent
  123.  * (or not parent) status of this box, and the visiblity and collapsed
  124.  * status and parent (or not parent) status for all children boxes.
  125.  */
  126. function compute_folder_children(&$parbox$boxcount{
  127.     global $boxes$data_dir$username$collapse_folders;
  128.     $nextbox $parbox 1;
  129.  
  130.     /* Retreive the name for the parent box. */
  131.     $parbox_name $boxes[$parbox]['unformatted'];
  132.  
  133.     /* 'Initialize' this parent box to childless. */
  134.     $boxes[$parbox]['parent'FALSE;
  135.  
  136.     /* Compute the collapse status for this box. */
  137.     ifisset($collapse_folders&& $collapse_folders {
  138.         $collapse getPref($data_dir$username'collapse_folder_' $parbox_name);
  139.         $collapse ($collapse == '' SM_BOX_UNCOLLAPSED $collapse);
  140.     else {
  141.         $collapse SM_BOX_UNCOLLAPSED;
  142.     }
  143.     $boxes[$parbox]['collapse'$collapse;
  144.  
  145.     /* Otherwise, get the name of the next box. */
  146.     if (isset($boxes[$nextbox]['unformatted'])) {
  147.         $nextbox_name $boxes[$nextbox]['unformatted'];
  148.     else {
  149.         $nextbox_name '';
  150.     }
  151.  
  152.     /* Compute any children boxes for this box. */
  153.     while (($nextbox $boxcount&&
  154.            (is_parent_box($boxes[$nextbox]['unformatted']$parbox_name))) {
  155.  
  156.         /* Note that this 'parent' box has at least one child. */
  157.         $boxes[$parbox]['parent'TRUE;
  158.  
  159.         /* Compute the visiblity of this box. */
  160.         $boxes[$nextbox]['visible'($boxes[$parbox]['visible'&&
  161.                                        ($boxes[$parbox]['collapse'!= SM_BOX_COLLAPSED));
  162.  
  163.         /* Compute the visibility of any child boxes. */
  164.         compute_folder_children($nextbox$boxcount);
  165.     }
  166.  
  167.     /* Set the parent box to the current next box. */
  168.     $parbox $nextbox;
  169. }
  170.  
  171. /**
  172.  * Create the link for a parent folder that will allow that
  173.  * parent folder to either be collapsed or expaned, as is
  174.  * currently appropriate.
  175.  */
  176. function create_collapse_link($boxnum{
  177.     global $boxes$imapConnection$unseen_notify$color;
  178.     $mailbox urlencode($boxes[$boxnum]['unformatted']);
  179.  
  180.     /* Create the link for this collapse link. */
  181.     $link '<a target="left" style="text-decoration:none" ' .
  182.             'href="left_main.php?';
  183.     if ($boxes[$boxnum]['collapse'== SM_BOX_COLLAPSED{
  184.         $link .= "unfold=$mailbox\">+";
  185.     else {
  186.         $link .= "fold=$mailbox\">-";
  187.     }
  188.     $link .= '</a>';
  189.  
  190.     /* Return the finished product. */
  191.     return ($link);
  192. }
  193.  
  194. /**
  195.  * create_unseen_string:
  196.  *
  197.  * Create unseen and total message count for both this folder and
  198.  * it's subfolders.
  199.  *
  200.  * @param string $boxName name of the current mailbox
  201.  * @param array $boxArray array for the current mailbox
  202.  * @param $imapConnection current imap connection in use
  203.  * @return array[0] unseen message string (for display)
  204.  * @return array[1] unseen message count
  205.  */
  206. function create_unseen_string($boxName$boxArray$imapConnection$unseen_type{
  207.     global $boxes$unseen_type$color$unseen_cum;
  208.  
  209.     /* Initialize the return value. */
  210.     $result array(0,0);
  211.  
  212.     /* Initialize the counts for this folder. */
  213.     $boxUnseenCount 0;
  214.     $boxMessageCount 0;
  215.     $totalUnseenCount 0;
  216.     $totalMessageCount 0;
  217.  
  218.     /* Collect the counts for this box alone. */
  219.     $status sqimap_status_messages($imapConnection$boxName);
  220.     $boxUnseenCount $status['UNSEEN'];
  221.     if ($boxUnseenCount === false{
  222.         return false;
  223.     }
  224.     if ($unseen_type == 2{
  225.         $boxMessageCount $status['MESSAGES'];
  226.     }
  227.  
  228.     /* Initialize the total counts. */
  229.  
  230.     if ($boxArray['collapse'== SM_BOX_COLLAPSED && $unseen_cum{
  231.         /* Collect the counts for this boxes subfolders. */
  232.         $curBoxLength strlen($boxName);
  233.         $boxCount count($boxes);
  234.  
  235.         for ($i 0$i $boxCount++$i{
  236.             /* Initialize the counts for this subfolder. */
  237.             $subUnseenCount 0;
  238.             $subMessageCount 0;
  239.  
  240.             /* Collect the counts for this subfolder. */
  241.             if (($boxName != $boxes[$i]['unformatted'])
  242.                     && (substr($boxes[$i]['unformatted']0$curBoxLength== $boxName)
  243.                     && !in_array('noselect'$boxes[$i]['flags'])) {
  244.                 $status sqimap_status_messages($imapConnection$boxes[$i]['unformatted']);
  245.                 $subUnseenCount $status['UNSEEN'];
  246.                 if ($unseen_type == 2{
  247.                     $subMessageCount $status['MESSAGES'];;
  248.                 }
  249.                 /* Add the counts for this subfolder to the total. */
  250.                 $totalUnseenCount += $subUnseenCount;
  251.                 $totalMessageCount += $subMessageCount;
  252.             }
  253.         }
  254.  
  255.         /* Add the counts for all subfolders to that of the box. */
  256.         $boxUnseenCount += $totalUnseenCount;
  257.         $boxMessageCount += $totalMessageCount;
  258.     }
  259.  
  260.     /* And create the magic unseen count string.     */
  261.     /* Really a lot more then just the unseen count. */
  262.     if (($unseen_type == 1&& ($boxUnseenCount 0)) {
  263.         $result[0"($boxUnseenCount)";
  264.     else if ($unseen_type == 2{
  265.         $result[0"($boxUnseenCount/$boxMessageCount)";
  266.         $result[0"<font color=\"$color[11]\">$result[0]</font>";
  267.     }
  268.  
  269.     /* Set the unseen count to return to the outside world. */
  270.     $result[1$boxUnseenCount;
  271.  
  272.     /* Return our happy result. */
  273.     return ($result);
  274. }
  275.  
  276. /**
  277.  * This simple function checks if a box is another box's parent.
  278.  */
  279. function is_parent_box($curbox_name$parbox_name{
  280.     global $delimiter;
  281.  
  282.     /* Extract the name of the parent of the current box. */
  283.     $curparts explode($delimiter$curbox_name);
  284.     $curname array_pop($curparts);
  285.     $actual_parname implode($delimiter$curparts);
  286.     $actual_parname substr($actual_parname,0,strlen($parbox_name));
  287.  
  288.     /* Compare the actual with the given parent name. */
  289.     return ($parbox_name == $actual_parname);
  290. }
  291.  
  292.  
  293. /* -------------------- MAIN ------------------------ */
  294.  
  295. /* get globals */
  296. sqgetGlobalVar('username'$usernameSQ_SESSION);
  297. sqgetGlobalVar('key'$keySQ_COOKIE);
  298. sqgetGlobalVar('delimiter'$delimiterSQ_SESSION);
  299. sqgetGlobalVar('onetimepad'$onetimepadSQ_SESSION);
  300.  
  301. sqgetGlobalVar('fold'$foldSQ_GET);
  302. sqgetGlobalVar('unfold'$unfoldSQ_GET);
  303. sqgetGlobalVar('auto_create_done',$auto_create_done,SQ_SESSION);
  304.  
  305. /* end globals */
  306.  
  307. // Disable browser caching //
  308. header('Cache-Control: no-cache, no-store, must-revalidate');
  309. header('Pragma: no-cache');
  310. header('Expires: Sat, 1 Jan 2000 00:00:00 GMT');
  311.  
  312. // open a connection to the IMAP server
  313. global $imap_stream_options// in case not defined in config
  314. $imapConnection sqimap_login($username$key$imapServerAddress$imapPort10$imap_stream_options)// the 10 is to hide the output
  315.  
  316. /**
  317.  * Using stristr since older preferences may contain "None" and "none".
  318.  */
  319. $xtra '';
  320. if (isset($left_refresh&& ($left_refresh != ''&&
  321.     !stristr($left_refresh'none')){
  322.     if ($javascript_on
  323.      && ($check_mail_mechanism === 'basic' || $check_mail_mechanism === 'advanced')) {
  324.         $script_libs[SM_SCRIPT_LIB_REMOTE_REQUEST;
  325.         $script_libs[SM_SCRIPT_LIB_RELOAD_PAGE;
  326.         // NB: we could use $script_libs instead of $xtra - they both do the same thing for adding free-form extra script tags
  327.         // FIXME: the "advanced" version of the JavaScript check mail feature could be triggered by browser detection (brief testing shows that at least IE 8 doesn't like the advanced version).... but for now, just let admin decide in system settings
  328.         $xtra "\n<script language=\"JavaScript\" type=\"text/javascript\">\n<!--\nvar base_uri = '" $base_uri "';\nvar reload_interval = " $left_refresh ";\nvar use_advanced_page_reload = " ($check_mail_mechanism === 'advanced' "true;\n" "false;\n""// -->\n</script>\n";
  329.     else {
  330.         $xtra =  "\n<meta http-equiv=\"REFRESH\" content=\"$left_refresh;URL=left_main.php\">\n";
  331.     }
  332. }
  333.  
  334. displayHtmlHeader$org_title$xtraTRUE$script_libs );
  335.  
  336. /* If requested and not yet complete, attempt to autocreate folders. */
  337. if ($auto_create_special && !$auto_create_done{
  338.     $autocreate array($sent_folder$trash_folder$draft_folder);
  339.     foreach$autocreate as $folder {
  340.         if (($folder != ''&& ($folder != 'none')) {
  341.             if !sqimap_mailbox_exists($imapConnection$folder)) {
  342.                 sqimap_mailbox_create($imapConnection$folder'');
  343.             else if (!sqimap_mailbox_is_subscribed($imapConnection$folder)) {
  344.                 sqimap_subscribe($imapConnection$folder);
  345.             }
  346.         }
  347.     }
  348.  
  349.     /* Let the world know that autocreation is complete! Hurrah! */
  350.     $auto_create_done TRUE;
  351.     sqsession_register($auto_create_done'auto_create_done');
  352.     /* retrieve the mailboxlist. We do this at a later stage again but if
  353.        the right_frame loads faster then the second call retrieves a cached
  354.        version of the mailboxlist without the newly created folders.
  355.        The second parameter forces a non cached mailboxlist return.
  356.      */
  357.     $boxes sqimap_mailbox_list($imapConnection,true);
  358. }
  359.  
  360. echo "\n<body bgcolor=\"$color[3]\" text=\"$color[6]\" link=\"$color[6]\" vlink=\"$color[6]\" alink=\"$color[6]\">\n";
  361.  
  362. do_hook('left_main_before');
  363.  
  364. echo "\n\n" html_tag'table''''left''''border="0" cellspacing="0" cellpadding="0" width="99%"' .
  365.     html_tag'tr' .
  366.     html_tag'td''''left' .
  367.     html_tag'table''''''''border="0" cellspacing="0" cellpadding="0"' .
  368.     html_tag'tr' .
  369.     html_tag'td''''center' .
  370.     '<font size="4"><b>'_("Folders""</b><br /></font>\n\n";
  371.  
  372. if ($date_format != 6{
  373.     /* First, display the clock. */
  374.     if ($hour_format == 1{
  375.         $hr 'H:i';
  376.         if ($date_format == 4{
  377.             $hr .= ':s';
  378.         }
  379.     else {
  380.         if ($date_format == 4{
  381.             $hr 'g:i:s a';
  382.         else {
  383.             $hr 'g:i a';
  384.         }
  385.     }
  386.  
  387.     switch$date_format {
  388.     case 0:
  389.         $clk date('Y-m-d '.$hr' T'time());
  390.         break;
  391.     case 1:
  392.         $clk date('m/d/y '.$hrtime());
  393.         break;
  394.     case 2:
  395.         $clk date('d/m/y '.$hrtime());
  396.         break;
  397.     case 4:
  398.     case 5:
  399.         $clk date($hrtime());
  400.         break;
  401.     default:
  402.         $clk getDayAbrvdate'w'time() ) ) date', ' $hrtime() );
  403.     }
  404.     $clk str_replace(' ','&nbsp;',$clk);
  405.  
  406.     echo '<small><span style="white-space: nowrap;">'
  407.        . str_replace(' ''&nbsp;'_("Last Refresh"))
  408.        . ":</span><br /><span style=\"white-space: nowrap;\">$clk</span></small><br />";
  409. }
  410.  
  411. /* Next, display the refresh button. */
  412. echo '<small style="white-space: nowrap;">(<a href="../src/left_main.php" target="left">'.
  413.      _("Check mail"'</a>)</small></td></tr></table><br />';
  414.  
  415. /* Lastly, display the folder list. */
  416. if $collapse_folders {
  417.     /* If directed, collapse or uncollapse a folder. */
  418.     if (isset($fold)) {
  419.         setPref($data_dir$username'collapse_folder_' $foldSM_BOX_COLLAPSED);
  420.     else if (isset($unfold)) {
  421.         setPref($data_dir$username'collapse_folder_' $unfoldSM_BOX_UNCOLLAPSED);
  422.     }
  423. }
  424.  
  425. sqgetGlobalVar('force_refresh',$force_refresh,SQ_GET);
  426. if (!isset($boxes)) // auto_create_done
  427.     $boxes sqimap_mailbox_list($imapConnection,$force_refresh);
  428. }
  429. /* Prepare do do out collapsedness and visibility computation. */
  430. $curbox 0;
  431. $boxcount count($boxes);
  432.  
  433. /* Compute the collapsedness and visibility of each box. */
  434.  
  435. while ($curbox $boxcount{
  436.     $boxes[$curbox]['visible'TRUE;
  437.     compute_folder_children($curbox$boxcount);
  438. }
  439.  
  440. for ($i 0$i count($boxes)$i++{
  441.     if $boxes[$i]['visible'{
  442.         $mailbox $boxes[$i]['formatted'];
  443.     // remove folder_prefix using substr so folders aren't indented unnecessarily
  444.         $mblevel substr_count(substr($boxes[$i]['unformatted']strlen($folder_prefix))$delimiter1;
  445.  
  446.         /* Create the prefix for the folder name and link. */
  447.         $prefix str_repeat('  ',$mblevel);
  448.         if (isset($collapse_folders&& $collapse_folders && $boxes[$i]['parent']{
  449.             $prefix str_replace(' ','&nbsp;',substr($prefix,0,strlen($prefix)-2)).
  450.                       create_collapse_link($i'&nbsp;';
  451.         else {
  452.             $prefix str_replace(' ','&nbsp;',$prefix);
  453.         }
  454.         $line "<span style=\"white-space: nowrap;\"><tt>$prefix</tt>";
  455.  
  456.         /* Add the folder name and link. */
  457.         if (isset($color[15])) {
  458.             $color[15$color[6];
  459.         }
  460.  
  461.         if (in_array('noselect'$boxes[$i]['flags'])) {
  462.             ifisSpecialMailbox$boxes[$i]['unformatted']) ) {
  463.                 $line .= "<font color=\"$color[11]\">";
  464.             else {
  465.                 $line .= "<font color=\"$color[15]\">";
  466.             }
  467.             if (preg_match('/^( *)([^ ]*)/'$mailbox$regs)) {
  468.                 $mailbox str_replace('&nbsp;','',$mailbox);
  469.                 $line .= str_replace(' ''&nbsp;'$mailbox);
  470.             }
  471.             $line .= '</font>';
  472.         else {
  473.             $line .= formatMailboxName($imapConnection$boxes[$i]);
  474.         }
  475.  
  476.         /* Put the final touches on our folder line. */
  477.         $line .= "</span><br />\n";
  478.  
  479.         /* Output the line for this folder. */
  480.         echo $line;
  481.     }
  482. }
  483.  
  484. do_hook('left_main_after');
  485. sqimap_logout($imapConnection);
  486.  
  487. ?>
  488. </td></tr></table>
  489. </body></html>

Documentation generated on Mon, 13 Jan 2020 04:24:52 +0100 by phpDocumentor 1.4.3