home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / admin / folders.php < prev    next >
Encoding:
PHP Script  |  2003-08-24  |  12.4 KB  |  398 lines

  1. <?php
  2. /////////////////////////////////////////////////////////
  3. //    
  4. //    source/folders.php
  5. //
  6. //    (C)Copyright 2000-2003 Ryo Chijiiwa <Ryo@IlohaMail.org>
  7. //
  8. //        This file is part of IlohaMail.
  9. //        IlohaMail is free software released under the GPL 
  10. //        license.  See enclosed file COPYING for details,
  11. //        or see http://www.fsf.org/copyleft/gpl.html
  12. //
  13. /////////////////////////////////////////////////////////
  14.  
  15. /********************************************************
  16.  
  17.     AUTHOR: Ryo Chijiiwa <ryo@ilohamail.org>
  18.     FILE: source/folders.php
  19.     PURPOSE:
  20.         Display a list of folders, with links to main.php (which lists the contents of the folder).
  21.         Also provides link to edit_folders.php (for creating/deleting/renaming folders).
  22.     PRE-CONDITIONS:
  23.         $user - Session ID
  24.     COMMENTS:
  25.         This is the klunkiest hack in all of IlohaMail...it's been rewritten at least half a dozen times.
  26.         Latest revision includes support for expanding/collapsing folders that have sub-folders.
  27.         It's messy as all hell because some IMAP servers (*cough* Netscape *cough*) don't return
  28.         container folders for some reason.  In other words, if you create "folder/sub/subsub", it won't
  29.         return "folder/sub" automatically.
  30.         
  31. ********************************************************/
  32. include("../include/super2global.inc");
  33. include("../include/nocache.inc");
  34.  
  35. function getFolderStates(){
  36.     global $loginID, $host;
  37.     
  38.     $data = cache_read($loginID, $host, "folder_states");
  39.  
  40.     if (!$data) return array("INBOX");
  41.     else{
  42.         return $data;
  43.     }
  44. }
  45.  
  46. function saveFolderStates($folders){
  47.     global $loginID, $host;
  48.     
  49.     $result = cache_write($loginID, $host, "folder_states", $folders, false);
  50.     return $result;
  51. }
  52.  
  53. function removeFolders($array){
  54.     if ((!is_array($array)) || (count($array)==0)) return true;
  55.     
  56.     $current = getFolderStates();
  57.     if (is_array($current)){
  58.         $save = array();
  59.         while ( list($k,$folder)=each($current) ){
  60.             if (!in_array($folder, $array)) $save[]=$folder;
  61.         }
  62.         saveFolderStates($save);
  63.     }
  64. }
  65.  
  66. function addFolders($array){
  67.     if ((!is_array($array)) || (count($array)==0)) return true;
  68.  
  69.     $current = getFolderStates();
  70.     if (is_array($current)){
  71.         $save = array_merge($current, $array);
  72.         sort($save);
  73.         saveFolderStates($save);
  74.     }
  75. }
  76.  
  77. function InArray($array, $item){
  78.     if (!is_array($array)) return false;
  79.     else if (strcasecmp($item, "inbox")==0) return false;
  80.     else return in_array($item, $array);
  81. }
  82.  
  83. function ChildInArray($array, $item){
  84.     if (!is_array($array)) return false;
  85.     reset($array);
  86.     while (list($k,$v)=each($array)){
  87.         $pos = strpos($v, $item);
  88.         if (($pos!==false) && ($pos==0)) return true;
  89.     }
  90.     return false;
  91. }
  92.  
  93. function IndentPath($path, $containers, $delim){
  94.     $containers->reset();
  95.     $pos = strrpos($path, $delim);
  96.     if ($pos>0){
  97.         $folder = substr($path, $pos);
  98.         $path = substr($path, 0, $pos);
  99.     }
  100.     
  101.     do{
  102.         $container = $containers->next();
  103.         if ($container) $path = str_replace($container, "   ", $path);
  104.     }while($container);
  105.     
  106.     return $path.$folder;
  107. }
  108.  
  109. if (isset($user)){
  110.     include("../include/session_auth.inc");
  111.     include("../include/ryosimap.inc");
  112.     include("../include/icl.inc");
  113.     include("../include/stack.inc");
  114.     include("../include/cache.inc");
  115.  
  116.     $linkc = $my_colors["folder_link"];
  117.     $bgc = $my_colors["folder_bg"];
  118.     $textc = $my_colors["folder_link"];
  119.     $font_size = $my_colors["font_size"];
  120.     $bodyString= '<BODY BGCOLOR="'.$bgc.'" TEXT="'.$linkc.'" LINK="'.$linkc.'" ALINK="'.$linkc.'" VLINK="'.$linkc.'">';    
  121. ?>
  122. <HTML>
  123. <HEAD>
  124.     <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=<?php echo $my_prefs["charset"]; ?>">
  125.     <?php
  126.     if ($my_prefs["refresh_folderlist"]){
  127.         $interval = $my_prefs["folderlist_interval"];
  128.         if ($MIN_FOLDERLIST_REFRESH > $my_prefs["folderlist_interval"]) $interval = $MIN_FOLDERLIST_REFRESH;
  129.         echo '<META HTTP-EQUIV="refresh"  CONTENT="'.$interval.';URL=folders.php?user='.$user.'">';
  130.         echo "\n";
  131.     }
  132.     include("../include/css.inc");
  133.     ?>
  134. </HEAD>
  135. <?php
  136.     echo $bodyString;
  137.     
  138.     $conn = iil_Connect($host, $loginID, $password, $AUTH_MODE);
  139.     if (!$conn)
  140.         echo "failed";
  141.     else{
  142.     
  143.     //handle emptry_trash request
  144.     if ($empty_trash && !empty($my_prefs["trash_name"])){
  145.         iil_C_ClearFolder($conn, $my_prefs["trash_name"]);
  146.     }
  147.  
  148.     //show heading
  149.     include("../lang/".$my_prefs["lang"]."folders.inc");
  150.     echo "<p><a href=\"folders.php?user=$user\"><b>".$fl_str["folders"]."</b></a>\n";
  151.     echo "<br>[<a href=\"edit_folders.php?user=".$user."\" target=\"list2\">".$fl_str["manage"]."</a>]";
  152.     echo "<br><br>";
  153.     
  154.     //show quota
  155.     if ($my_prefs["show_quota"]=="f"){
  156.         $quota = iil_C_GetQuota($conn);
  157.         include("../lang/".$my_prefs["lang"]."quota.inc");
  158.         include_once("../lang/".$my_prefs["charset"].".inc");
  159.         echo "<span class=\"small\">\n";
  160.         if ($quota) echo "<b>".$quotaStr["label"]."</b><br>".LangInsertStringsFromAK($quotaStr["full"], $quota);
  161.         else echo "<b>".$quotaStr["label"]."</b>".$quotaStr["unknown"];
  162.         echo "</span>\n";
  163.         echo "<p>\n";
  164.     }
  165.     
  166.     
  167.     //get list of mailboxes
  168.     cache_clear($loginID, $host, "folders");
  169.     if ($my_prefs["hideUnsubscribed"]){
  170.         $folders = iil_C_ListSubscribed($conn, $my_prefs["rootdir"], "*");
  171.     }else{
  172.         $folders = iil_C_ListMailboxes($conn, $my_prefs["rootdir"], "*");
  173.     }
  174.     cache_write($loginID, $host, "folders", $folders);
  175.     
  176.     echo "<!--\n".implode(",\n",$folders)."\n//-->\n";
  177.     
  178.     if (!is_array($folders)){
  179.         echo "Couldn't get folder list from stream $mbox at $host.<br>\n";
  180.     } else {    
  181.         //get hierarchy delimiter, usually '/' or '.'
  182.         $delim = iil_C_GetHierarchyDelimiter($conn);
  183.     
  184.         //get list of container folders, because some IMAP server won't return them
  185.         //e.g.  container of "folder/sub" is "folder"
  186.         $folder_container = array();
  187.         $containers = array();
  188.         reset($folders);
  189.         while ( list($k, $path) = each($folders) ){
  190.             while (false !== ($pos = strrpos($path, $delim))){
  191.                 $container = substr($path, 0, $pos);
  192.                 if ($containers[$container]!=1) $containers[$container]=1;
  193.                 $folder_container[$path] = $container;
  194.                 //echo "<!-- container of $path is ".$folder_container[$path]." //-->\n";
  195.                 $path = substr($path, 0, $pos);
  196.             }
  197.         }
  198.  
  199.         //make sure containers are in folder list
  200.         reset($containers);
  201.         while ( list($container, $v) = each($containers) ){
  202.             if (!InArray($folders,$container)) array_push($folders, $container);
  203.         }
  204.         asort($folders);
  205.  
  206.         //handle subscribe (expand) command
  207.         if ($subscribe){
  208.             //subscribe folder...
  209.             $add_list = array();
  210.             //iil_C_Subscribe($conn, $folder);
  211.             $v_sub[]=$folder;
  212.             $add_list[] = $folder;
  213.             
  214.             //and immediate sub-folders
  215.             $folder.=$delim;
  216.             reset($folders);
  217.             while (list($k,$v)=each($folders)){
  218.                 $pos = strpos($v, $folder);
  219.                 if (($pos!==false) && ($pos==0)){
  220.                     $pos = strrpos($v, $delim);
  221.                     if ($pos <= strlen($folder)){
  222.                         //iil_C_Subscribe($conn, $v);
  223.                         $v_sub[]=$v;
  224.                         $add_list[] = $v;
  225.                     }
  226.                 }
  227.             }
  228.             if (count($add_list)>0) addFolders($add_list);
  229.         }
  230.         
  231.         //handle unsubscribe (collapse) command
  232.         if ($unsubscribe){
  233.             //subscribe folder...
  234.             $remove_list = array();
  235.             //iil_C_UnSubscribe($conn, $folder);
  236.             $remove_list[] = $folder;
  237.             
  238.             //and all sub-folders
  239.             $folder.=$delim;
  240.             reset($folders);
  241.             while (list($k,$v)=each($folders)){
  242.                 $pos = strpos($v, $folder);
  243.                 if (($pos!==false) && ($pos==0)){
  244.                     //$r = iil_C_UnSubscribe($conn, $v);
  245.                     $remove_list[] = $v;
  246.                 }
  247.             }
  248.             if (count($remove_list)>0) removeFolders($remove_list);
  249.         }
  250.                 
  251.         //get list of subscribed (expanded) folders
  252.         $subscribed = getFolderStates();
  253.         
  254.         //make sure they exist (might've been deleted)
  255.         $temp_subs = array();
  256.         reset($subscribed);
  257.         while( list($k,$path)=each($subscribed) ){
  258.             if (in_array($path, $folders)) $temp_subs[] = $path;
  259.         }
  260.         $subscribed = $temp_subs;
  261.         echo "<!-- AFTER UNSUB:\n".implode("\n", $subscribed)."\n//-->\n";
  262.         
  263.         //with some servers, only container folders are ignored, so we need to
  264.         //do it the inefficient way...
  265.         if (is_array($subscribed)){
  266.             //make sure the container of every subscribed folder is also in list
  267.             //curse Netscape!
  268.             reset($subscribed);
  269.             while( list($k,$path)=each($subscribed) ){
  270.                 //make sure every folder in path to subscribed folder is also subscribed.
  271.                 $original_path = $path;
  272.                 while(false !== ($pos = strrpos($path, $delim))){
  273.                        $container = substr($path, 0, $pos);
  274.                     if (!in_array($container, $subscribed)){
  275.                         $v_sub[]=$container;
  276.                     }
  277.                     $path = substr($path, 0, $pos);
  278.                 }
  279.                 
  280.                 //make sure all folder at same level as subscribed folders are subscribed
  281.                 $path = $original_path;
  282.                 if (false !== ($pos = strrpos($path, $delim))){
  283.                     $container = substr($path, 0, $pos);
  284.                     if (!$checked_container[$container]){
  285.                         //echo "<!-- Adding all in: $container //-->\n";
  286.                         reset($folders);
  287.                         while ( list($k2, $folder)=each($folders) ){
  288.                             //is "folder" inside "container"?
  289.                             $pos = strpos($folder, $container);
  290.                             if (($pos!==false) && ($pos==0)){
  291.                                 //is $folder immediately inside $container, or further down?
  292.                                 $pos = strrpos($folder, $delim);
  293.                                 if ($pos <= strlen($container.$delim)){
  294.                                     if (!InArray($subscribed, $folder)){
  295.                                         //*gasp*!  $folder is not subscribed!
  296.                                         $v_sub[]=$folder;
  297.                                     }
  298.                                 }
  299.                             }
  300.                         }
  301.                         $checked_container[$container] = 1;
  302.                     }
  303.                 }
  304.             }
  305.         }
  306.         if (is_array($v_sub)){
  307.             while ( list($k,$v)=each($v_sub) ) if (!in_array($v, $subscribed)) $subscribed[]=$v;
  308.         }
  309.         if (is_array($subscribed)){
  310.             sort($subscribed);
  311.             reset($subscribed);
  312.             //while ( list($k,$v)=each($subscribed) ) echo "<!-- subscribed: $v //-->\n";
  313.         }
  314.         
  315.         natcasesort($folders);
  316.         $c=sizeof($folders);
  317.         echo "<NOBR>";
  318.  
  319.         //show default folders (i.e. Inbox, Sent, Trash)
  320.         $unseen_str = "";
  321.         reset ($defaults);
  322.          while (list($key, $value) = each ($defaults)) {
  323.             if (($value!=".")&&(!empty($key))){
  324.                 if ($my_prefs["showNumUnread"]){
  325.                     $num_unseen = iil_C_CountUnseen($conn, $key);
  326.                     if ( $num_unseen > 0 ) $unseen_str = " (".$num_unseen.")";
  327.                     else $unseen_str = "";
  328.                 }
  329.                 echo "<a href=\"main.php?folder=$key&user=".$user."\" target=\"list2\">$value</a>";
  330.                 echo $unseen_str;
  331.                 if ($key==$my_prefs["trash_name"]){
  332.                     echo " [<a href=\"folders.php?user=".$user."&empty_trash=1\">".$fstr["expunge"]."</a>]";
  333.                 }
  334.                 echo "<br>";
  335.             }
  336.          }
  337.  
  338.         echo "<br>\n";
  339.  
  340.         //indent according to depth
  341.         $result = array();
  342.         reset($folders);
  343.         while ( list($k, $path) = each($folders) ){
  344.             //we're only going to display folders that are in...
  345.             //root level, subscribed, or in "INBOX"
  346.             if (($folder_container[$path]==$my_prefs["rootdir"]) 
  347.                 || (InArray($subscribed, $path))){
  348.             //    || ($folder_container[$path]=="INBOX")){
  349.                 
  350.                 $a = explode($delim, $path);
  351.                 $c = count($a);
  352.                 $folder = $a[$c-1];
  353.                 if (strcmp($a[0], $my_prefs["rootdir"])==0) $c--;
  354.                 if (($path[0]!=".") && ($folder[0]!=".")){
  355.                     for($i=0;$i<($c-1);$i++) $indent[$path].="  ";
  356.                     $result[$path] = $folder;
  357.                 }
  358.             }
  359.         }
  360.  
  361.         flush();
  362.  
  363.         //display folders
  364.         reset($result);
  365.         while ( list($path, $display) = each($result) ){
  366.             if ((!empty($display)) && (($containers[$path])||(empty($defaults[$path])))){
  367.                 $key = $path;
  368.                 if ($containers[$path]){
  369.                     echo "<!-- begins with ".$path.$delim."? //-->\n";
  370.                     $is_sub = ChildInArray($subscribed, $path.$delim);
  371.                     $button = "<a href=\"folders.php?user=$user&".($is_sub?"unsubscribe":"subscribe")."=1&folder=".urlencode($path)."\" target=\"list1\">";
  372.                     $button .= "<tt>".($is_sub?"-":"+")."</tt></a>";
  373.                 }else{
  374.                     $button = "<tt> </tt>";
  375.                 }
  376.                 echo "<span style=\"font-size: ".$my_colors["font_size"]."; color: ".$my_colors["folder_bg"]."\"><tt>".$indent[$key]."</tt></span>";
  377.                 echo $button;
  378.                 
  379.                 $unseen_str="";
  380.                 if ($my_prefs["showNumUnread"]){
  381.                     $num_unseen = iil_C_CountUnseen($conn, $path);
  382.                     if ( $num_unseen > 0 ) $unseen_str = " (".$num_unseen.")";
  383.                 }
  384.                 
  385.                 $path = urlencode($path);                
  386.                 echo "<a href=\"main.php?folder=$path&user=".$user."\" target=\"list2\">".$display.$unseen_str."</a><BR>\n";
  387.                 flush();
  388.             }
  389.         }
  390.     }
  391.     iil_Close($conn);
  392.     }
  393. }else{
  394.     echo "User unspecified.";
  395.     exit;
  396. }
  397. ?>
  398. </BODY></HTML>