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

Documentation generated on Wed, 22 May 2013 04:22:40 +0200 by phpDocumentor 1.4.3