home *** CD-ROM | disk | FTP | other *** search
/ synchro.net / synchro.net.tar / synchro.net / Synchronet_Archive / 310IBETA.ZIP / ftp-html.js < prev    next >
Encoding:
JavaScript  |  2002-03-25  |  13.6 KB  |  429 lines

  1. // JavaScript HTML Index for Synchronet FTP Server
  2. // $id$
  3.  
  4. var start=new Date();
  5. var time_stamp=start.valueOf().toString(36);    // Used to defeat caching browsers
  6.  
  7. load("sbbsdefs.js");    // Synchronet constants
  8.  
  9. /* Utility Functions */
  10.  
  11. function writeln(str)
  12. {
  13.     write(str + "\r\n");
  14. }
  15.  
  16. function date(time)
  17. {
  18.     var mon=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
  19.     var d=new Date(time*1000);
  20.  
  21.     return(format("%s %02d %d %02d:%02d"
  22.             ,mon[d.getUTCMonth()]
  23.             ,d.getUTCDate()
  24.             ,d.getUTCFullYear()
  25.             ,d.getUTCHours()
  26.             ,d.getUTCMinutes()));
  27. }
  28.  
  29. function kbytes(nbytes)
  30. {
  31.     return(Math.round(nbytes/1024)+"k");
  32. }
  33.  
  34. function secstr(sec)
  35. {
  36.     return(format("%02u:%02u",sec/60,sec%60));
  37. }
  38.  
  39. var title = system.name + " BBS - FTP Server";
  40. var font_face = "<font face=Arial,Helvetica,sans-serif>";
  41. var font_size = 2;    // Change base font size here
  42.  
  43. writeln('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">');
  44. writeln("<html>");
  45. writeln("<head>");
  46. writeln("<title>");
  47. writeln(title);
  48. if (ftp.curdir.name!=undefined)
  49.     write(" - " + ftp.curdir.name);
  50. else if(ftp.curlib.description!=undefined)
  51.     write(" - " + ftp.curlib.description);
  52. writeln("</title>");
  53.  
  54. /* META TAGS */
  55. writeln("<meta name='GENERATOR' content='" + system.version + "'>");
  56. // The following line is necessary for IBM extended-ASCII in descriptions 
  57. writeln("<meta http-equiv='Content-Type' content='text/html; charset=IBM437'>");
  58.  
  59. writeln("</head>");
  60. writeln("<body bgcolor=teal text=white link=yellow vlink=lime alink=white>");
  61. writeln(font_face);
  62.  
  63. /* Go To Select Box */
  64. writeln("<table width=100%>");
  65. writeln("<td>");
  66. writeln("<h1>" + font_face + "<font color=lime>" + title.italics() + "</font></h1>");
  67. writeln("<td align=right>");
  68. writeln("<form>");    // Netscape requires this to be in a form <sigh>
  69. writeln(format(
  70.     "<select " +
  71.     "onChange='if(selectedIndex>0) location=options[selectedIndex].value + \"%s\";'>"
  72.     ,"?$" + time_stamp));
  73. writeln("<option>Go To...</option>");
  74. writeln(format("<option value=/%s>Root</option>",html_index_file));
  75. for(l in file_area.lib_list) {
  76.     writeln(format("<optgroup label=%s>",file_area.lib_list[l].name));
  77.     writeln(format("<option value=%s> [%s]"
  78.         ,file_area.lib_list[l].link
  79.         ,file_area.lib_list[l].name));
  80.     for(d in file_area.lib_list[l].dir_list) {
  81.         writeln(format("<option value=%s>     %s"
  82.             ,file_area.lib_list[l].dir_list[d].link
  83.             ,file_area.lib_list[l].dir_list[d].name));
  84.     }
  85.     writeln("</optgroup>");
  86. }
  87. writeln("</select>");
  88. writeln("</form>");
  89. writeln("</table>");
  90.  
  91. var prevdir;
  92.  
  93. var hdr_font="<font color=silver>";
  94. var dat_font="<font color=white>";
  95.  
  96. if(!(user.security.restrictions&UFLAG_G)) {    /* !Guest or Anonymous */
  97.     /* Logout button */
  98.     writeln("<table align=right>");
  99.     writeln("<form>");
  100.     writeln("<input type=button value=Logout onclick='location=\"ftp://" 
  101.         + format("%s/%s?$%s",system.host_name,html_index_file,time_stamp)
  102.         + "\";'>");
  103.     writeln("</form>");
  104.     writeln("</table>");
  105.  
  106.     /* User Info */
  107.     writeln("<table nowrap align=left>");
  108.     writeln(font_face);
  109.     writeln("<tr><th align=right>"+hdr_font+"User:<th align=left>"+dat_font+user.alias);
  110.     writeln("<tr><th align=right>"+hdr_font+"Address:<th align=left width=150>"+dat_font+user.ip_address);
  111.     write("<tr><th align=right>"+hdr_font+"Credits:<th align=left>"+dat_font);
  112.     if(user.security.exemptions&UFLAG_D)
  113.         writeln("Exempt");
  114.     else
  115.         writeln(kbytes(user.security.credits+user.security.free_credits));
  116.     write("<tr><th align=right>"+hdr_font+"Time left:<th align=left>"+dat_font);
  117.     if(user.security.exemptions&UFLAG_T)
  118.         writeln("Exempt");
  119.     else
  120.         writeln(secstr(ftp.time_left));
  121.     writeln("</table>");
  122.  
  123.     /* User Stats */
  124.  
  125.     writeln("<table nowrap>");
  126.     writeln(font_face);
  127.     writeln("<tr><th align=right>"+hdr_font+"Logons:<th align=left>"+dat_font+user.stats.total_logons);
  128.     writeln("<tr><th align=right>"+hdr_font+"Last on:<th align=left>"+date(user.stats.laston_date));
  129.      writeln("<tr><th align=right>"+hdr_font+"Uploaded:<th align=left>"+dat_font);
  130.     writeln(format("%s bytes in %u files"
  131.         ,kbytes(user.stats.bytes_uploaded),user.stats.files_uploaded));
  132.     writeln("<tr><th align=right>"+hdr_font+"Downloaded:<th align=left>"+dat_font);
  133.     writeln(format("%s bytes in %u files",kbytes(user.stats.bytes_downloaded)
  134.         ,user.stats.files_downloaded));
  135.     writeln("</table>");
  136.     writeln("<br>");
  137. } else if(ftp.curlib.name==undefined) {    /* Login */
  138.     writeln("<table align=right>");
  139.     writeln("<td><input type=button value='New User' onClick='location=\"telnet://" 
  140.         + system.host_name + "\";'>");
  141.     writeln("</table>");
  142.  
  143.     writeln("<form name='login'>");
  144.     writeln("<table border=1 frame=box rules=none cellpadding=3>");
  145.     writeln(font_face);
  146.     writeln("<tr><th valign=top align=left>"+hdr_font+"Name");
  147.     writeln("<td colspan=2><input type=text name='username' size=25 maxlength=25>");
  148.     writeln("<tr><th valign=top align=left>"+hdr_font+"Password");
  149.     writeln("<td><input type=password name='password' size=10 maxlength=25>");
  150.     writeln("<td align=right><input type=button name='LoginButton' value='Login' onClick='login_event();'>");
  151.  
  152.     /* Client-Side Script */
  153.     writeln("<SCRIPT language='JavaScript'>");
  154.         writeln("<!--");
  155.         writeln("function login_event() {");
  156.         write("var url='ftp://'"); 
  157.         write("+ escape(document.login.username.value) + ':'");
  158.         write("+ escape(document.login.password.value) + '@'");
  159.         write(format("+ '%s/%s?$%s'\r\n",system.host_name,html_index_file,time_stamp));
  160. //        writeln("alert(url);");
  161.         writeln("location = url;");
  162.         writeln("}");
  163.         writeln("// -->");
  164.         writeln("</script>");
  165.  
  166.     writeln("</table>");
  167.     writeln("</form>");
  168. }
  169.  
  170. /* Virtual Path */
  171. writeln("<h3>" + hdr_font + "Path: ");
  172. if(ftp.curlib.name==undefined) 
  173.     writeln(dat_font + "Root");
  174. else
  175.     writeln("Root".link(format("/%s?$%s",html_index_file,time_stamp)));
  176. if(ftp.curlib.name!=undefined) {
  177.     if(ftp.curdir.name==undefined)
  178.         writeln(" / " + dat_font + ftp.curlib.description);
  179.     else
  180.         writeln(" / " + ftp.curlib.description.link(format("/%s/%s?$%s"
  181.             ,ftp.curlib.name,html_index_file,time_stamp)));
  182. }        
  183. if(ftp.curdir.name!=undefined) 
  184.     writeln(" / " + dat_font + ftp.curdir.description);
  185. if(ftp.curdir.settings!=undefined && ftp.curdir.settings&DIR_FREE)
  186.     write(hdr_font+" - FREE");
  187. writeln("</h3>");
  188.  
  189.  
  190. /* Table Attributes */
  191. var hdr_background="white";
  192. var hdr_font=format("<font size=%d color=black>",font_size-1);
  193. var dat_font=format("<font size=%d>",font_size);
  194. var cell_spacing=""; //"cellspacing=2 cellpadding=2";
  195.  
  196. /* Directory Listing */
  197. if(ftp.dir_list.length) {
  198.  
  199.     writeln("<table " + cell_spacing + " width=33%>");
  200.     writeln(font_face);
  201.  
  202.     /* header */
  203.     writeln("<thead>");
  204.     writeln("<tr bgcolor=" + hdr_background + ">");
  205.     writeln("<th>" + hdr_font + "Directory");
  206.     if(ftp.curlib.name!=undefined) 
  207.         writeln("<th>" + hdr_font + "Files");
  208.     writeln("</thead>");
  209.  
  210.     /* body */
  211.     writeln("<tbody>");
  212.     for(i in ftp.dir_list) {
  213.         writeln("<tr>");
  214.     
  215.         /* filename */
  216.         writeln("<th nowrap align=left>" + dat_font 
  217.             + ftp.dir_list[i].description.link(ftp.dir_list[i].link + "?$" + time_stamp));
  218.  
  219.         if(ftp.curlib.name!=undefined) {
  220.             writeln("<td align=right><font color=black>" + dat_font + ftp.dir_list[i].size);
  221.             writeln("<th>" + dat_font + (ftp.dir_list[i].settings&DIR_FREE ? "FREE":""));
  222.         }
  223.     }    
  224.     writeln("</table>");
  225.     if(ftp.file_list.length)
  226.         writeln("<br>"); 
  227. }
  228.  
  229. /* File Listing */
  230. if(ftp.file_list.length) {
  231.  
  232.     /* Sort the list? */
  233.     switch(ftp.sort) {
  234.         case "uploader":
  235.             ftp.file_list.sort(function(a,b) 
  236.                 {     if(a.uploader>b.uploader)
  237.                         return(1);
  238.                     if(a.uploader<b.uploader)
  239.                         return(-1);
  240.                     return(0); }
  241.                 );
  242.             break;
  243.         case "size":
  244.             ftp.file_list.sort(function(a,b) 
  245.                 { return(a.size-b.size); }
  246.                 );
  247.             break;
  248.         case "credits":
  249.             ftp.file_list.sort(function(a,b) 
  250.                 { return(a.credits-b.credits); }
  251.                 );
  252.             break;
  253.         case "time":
  254.             ftp.file_list.sort(function(a,b) 
  255.                 { return(a.time.valueOf()-b.time.valueOf()); }
  256.                 );
  257.             break;
  258.         case "hits":
  259.             ftp.file_list.sort(function(a,b) 
  260.                 { return(a.times_downloaded-b.times_downloaded); }
  261.                 );
  262.             break;
  263.     }
  264.     if(ftp.reverse)
  265.         ftp.file_list.reverse();
  266.  
  267.     var show_ext_desc;            /* show extended descriptions */
  268.     var total_bytes=0;
  269.     var total_downloads=0;
  270.     var most_recent=0;
  271.  
  272.     if (ftp.curdir.name==undefined)
  273.         show_ext_desc=false;    /* aliased files have no ext desc */
  274.     else
  275.         show_ext_desc=user.settings&USER_EXTDESC;
  276.  
  277.     writeln("<table " + cell_spacing + " width=100%>");
  278.     writeln(font_face);
  279.     
  280.     /* header */
  281.     writeln("<thead>");
  282.     writeln("<tr bgcolor=" + hdr_background + ">");
  283.  
  284.     /* File */
  285.     writeln(format("<th><a href=%s?sort=name%s$%s>%sFile</a>"
  286.         ,html_index_file
  287.         ,(ftp.sort=="name" && !ftp.reverse) ? "&reverse":"", time_stamp, hdr_font));
  288.  
  289.     /* Credits or Size */
  290.     if(ftp.curdir.settings!=undefined && !(ftp.curdir.settings&DIR_FREE))
  291.         writeln(format("<th><a href=%s?sort=credits%s$%s>%sCredits</a>"
  292.             ,html_index_file
  293.             ,(ftp.sort=="credits" && !ftp.reverse) ? "&reverse" : "", time_stamp, hdr_font));
  294.     else
  295.         writeln(format("<th><a href=%s?sort=size%s$%s>%sSize</a>"
  296.             ,html_index_file
  297.             ,(ftp.sort=="size" && !ftp.reverse) ? "&reverse" : "", time_stamp, hdr_font));
  298.  
  299.     /* Description */
  300.     write("<th>" + hdr_font + "Description");
  301.     if(!(user.security.restrictions&UFLAG_G) && ftp.curdir.settings!=undefined) {
  302.         if(user.settings&USER_EXTDESC)
  303.             writeln(format(" [%s]"
  304.                 ,(hdr_font+"short").link(format("%s?ext=off$%s",html_index_file, time_stamp))));
  305.         else
  306.             writeln(format(" [%s]"
  307.                 ,(hdr_font+"extended").link(format("%s?ext=on$%s",html_index_file, time_stamp))));
  308.     }
  309.  
  310.     /* Date/Time */
  311.     writeln(format("<th><a href=%s?sort=time%s$%s>%sDate/Time</a>"
  312.         ,html_index_file
  313.         ,(ftp.sort=="time" && !ftp.reverse) ? "&reverse" : "", time_stamp, hdr_font));
  314.  
  315.     /* Uploader and Hits (downloads) */
  316.     if(ftp.curdir.name!=undefined) {    /* not valid for aliased files in root */
  317.         writeln(format("<th><a href=%s?sort=uploader%s$%s>%sUploader</a>"
  318.             ,html_index_file
  319.             ,(ftp.sort=="uploader" && !ftp.reverse) ? "&reverse" : "", time_stamp, hdr_font));
  320.         writeln(format("<th><a href=%s?sort=hits%s$%s>%sHits</a>"
  321.             ,html_index_file
  322.             ,(ftp.sort=="hits" && !ftp.reverse) ? "&reverse" : "", time_stamp, hdr_font));
  323.     }
  324.     writeln("</thead>");
  325.  
  326.     /* body */
  327.     writeln("<tbody>");
  328.     for(i in ftp.file_list) {
  329.  
  330.         total_downloads+=ftp.file_list[i].times_downloaded;
  331.         if(ftp.file_list[i].time>most_recent)
  332.             most_recent=ftp.file_list[i].time;
  333.  
  334.         writeln("<tr valign=top>");
  335.  
  336.         /* filename */
  337.         if(user.security.restrictions&UFLAG_D
  338.             || (ftp.curdir.settings!=undefined 
  339.                 && !(ftp.curdir.settings&DIR_FREE)
  340.                 && !(user.security.exemptions&UFLAG_D)
  341.                 && ftp.file_list[i].credits > (user.security.credits+user.security.free_credits))
  342.             ) {
  343.             write("<td align=left>" + dat_font);
  344.             writeln(ftp.file_list[i].name.link(
  345.                 "javascript:alert('Sorry, you do not have enough credits to download this file.');"));
  346.         } else {
  347.             write("<th align=left>" + dat_font);
  348.             writeln(ftp.file_list[i].name.link(ftp.file_list[i].link));
  349.         }
  350.  
  351.         /* size */
  352.         write("<td align=right>" + dat_font + "<font color=black>");
  353.         if(ftp.curdir.settings!=undefined && !(ftp.curdir.settings&DIR_FREE)) {
  354.             if(!ftp.file_list[i].credits)
  355.                 writeln("<font color=white><b>FREE");
  356.             else
  357.                 writeln(kbytes(ftp.file_list[i].credits)); 
  358.             total_bytes+=ftp.file_list[i].credits;
  359.         } else {
  360.             writeln(kbytes(ftp.file_list[i].size)); 
  361.             total_bytes+=ftp.file_list[i].size;
  362.         }
  363.  
  364.         /* description */
  365.         write("<td>" + dat_font);
  366.         if (show_ext_desc) {
  367.             if(ftp.file_list[i].settings&FILE_EXTDESC)
  368.                 writeln("<pre>" + ftp.file_list[i].extended_description);
  369.             else
  370.                 writeln("<tt>" + ftp.file_list[i].description);
  371.         } else
  372.             writeln(dat_font + ftp.file_list[i].description);
  373.  
  374.         /* date/time */
  375.         writeln("<td align=center nowrap>" + dat_font + "<font color=black>" 
  376.             + "<tt>"+ date(ftp.file_list[i].time));
  377.  
  378.         if(ftp.curdir.name!=undefined) {    /* not valid for aliased files in root */
  379.             /* uploader */
  380.             var uploader=ftp.file_list[i].uploader;
  381.             if (ftp.file_list[i].settings&FILE_ANON)
  382.                 uploader="Anonymous";
  383.             else if (uploader == "-> ADDFILES <-")
  384.                 uploader="Sysop".link("mailto:sysop@"+system.inetaddr);
  385.             else if (!(user.security.restrictions&UFLAG_G))    /* ! Guest/Anonymous */
  386.                 uploader=uploader.link("mailto:" + uploader + "@" + system.inetaddr);
  387.             writeln("<td nowrap>" + dat_font + uploader);
  388.  
  389.             /* download count */
  390.             writeln("<td align=right>" + dat_font + "<font color=black>" 
  391.                 + ftp.file_list[i].times_downloaded);
  392.         }
  393.     }
  394.  
  395.     /* Footer (with totals) */
  396.     writeln("<tfoot>");
  397.     writeln(format("<tr bgcolor=%s><th>%s%lu files" +
  398.         "<th align=right>%s%s<th>%s-<th>%s<font color=black><tt>%s"
  399.         ,hdr_background
  400.         ,hdr_font, ftp.file_list.length
  401.         ,hdr_font, kbytes(total_bytes)
  402.         ,hdr_font
  403.         ,dat_font, date(most_recent)
  404.         ));
  405.  
  406.     if(ftp.curdir.name!=undefined)     /* not valid for aliased files in root */
  407.         writeln(format("<th>%s-<th align=right>%s%lu"
  408.             ,hdr_font
  409.             ,hdr_font, total_downloads
  410.             ));
  411.  
  412.     writeln("</table>");
  413. }
  414.  
  415. if(!ftp.file_list.length && !ftp.dir_list.length)
  416.     writeln("<br><b>No Files.</b><br>");
  417.  
  418. /* Footer */
  419. write(format("<br><font size=%d color=silver>Problems? Ask ",font_size-1));
  420. write(format("<a href=mailto:sysop@%s>%s</a>.",system.inetaddr,system.operator));
  421.  
  422. write(format("<br><font size=%d>Dynamically generated ",font_size-1));
  423. write(format("in %lu milliseconds ", new Date().valueOf()-start.valueOf()));
  424. write("by <a href=http://www.synchro.net>" + server.version + "</a>");
  425. writeln("<br>" + Date() + "</font>");
  426. writeln("</body>");
  427. writeln("</html>");
  428.  
  429. /* End of ftp-html.js */