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

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