Annotation of loncom/interface/lonindexer.pm, revision 1.2

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Directory Indexer
                      3: # (Login Screen
                      4: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14 Gerd Kortemeyer)
                      5: # 11/23 Gerd Kortemeyer
                      6: # 07/20-08/04 H.K. Ng
                      7: #
1.2     ! harris41    8: # 05/9-05/19/2001 H. K. Ng
        !             9: #
1.1       www        10: package Apache::lonindexer;
                     11: 
                     12: use strict;
                     13: use Apache::lonnet();
                     14: use Apache::Constants qw(:common);
1.2     ! harris41   15: use Apache::File;
        !            16: use GDBM_File;
        !            17: 
        !            18: my %dirs;
        !            19: my %language;
        !            20: 
        !            21: sub BEGIN {
        !            22:     my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.'/language.tab');
        !            23:     map {
        !            24: 	$_=~/(\w+)\s+([\w\s\-]+)/;
        !            25: 	$language{$1}=$2;
        !            26:     } <$fh>;
        !            27: }
1.1       www        28: 
                     29: sub handler {
                     30:     my $r = shift;
                     31:     $r->content_type('text/html');
                     32:     $r->send_http_header;
                     33:     return OK if $r->header_only;
                     34: 
                     35:     my $iconpath= $r->dir_config('lonIconsURL');
                     36:     my $domain  = $r->dir_config('lonDefDomain');
                     37:     my $role    = $r->dir_config('lonRole');
                     38:     my $loadlim = $r->dir_config('lonLoadLim');
                     39:     my $servadm = $r->dir_config('lonAdmEMail');
                     40:     my $sysadm  = $r->dir_config('lonSysEMail');
                     41:     my $lonhost = $r->dir_config('lonHostID');
                     42:     my $tabdir  = $r->dir_config('lonTabDir');
                     43: 
                     44: # ---------------------------------------------------------------- Print Header
                     45:     $r->print(<<ENDHEADER);
                     46: <html>
                     47: <head>
1.2     ! harris41   48: <title>The LearningOnline Network With CAPA Directory Browser</title>
        !            49: <SCRIPT language="javascript">
        !            50: function openWindow(url, wdwName, w, h, toolbar,scrollbar) {
        !            51:     var options = "width=" + w + ",height=" + h + ",";
        !            52:     options += "resizable=yes,scrollbars="+scrollbar+",status=no,";
        !            53:     options += "menubar=no,toolbar="+toolbar+",location=no,directories=no";
        !            54:     var newWin = window.open(url, wdwName, options);
        !            55:     newWin.focus();
        !            56: }
        !            57: </SCRIPT>
1.1       www        58: </head>
                     59: <body bgcolor="#FFFFFF">
                     60: ENDHEADER
                     61: 
                     62:     my $line;
                     63:     my (@attrchk,@openpath);
                     64:     my $uri=$r->uri;
                     65:     my $iconpath="/res/adm/pages/indexericons/";
                     66: 
1.2     ! harris41   67:     $r->print("<h2><font color=\"\#888888\">The LearningOnline With CAPA Network Directory Browser</font></h2>\n");
1.1       www        68: 
                     69:     for (my $i=0; $i<=5; $i++) {
                     70: 	$attrchk[$i] = "checked" if $ENV{'form.attr'.$i} == 1;
                     71:     }
                     72:     $r->print(<<END);
1.2     ! harris41   73: <b><font color="#666666">Display file attributes</font></b><br>
1.1       www        74: <form method="post" name="fileattr" action="$uri" enctype="application/x-www-form-urlencoded">
                     75: <table border=0><tr>
1.2     ! harris41   76: <td><input type=checkbox name=attr0 value="1" $attrchk[0]> Size</td>
        !            77: <td><input type=checkbox name=attr1 value="1" $attrchk[1]> Last access</td>
        !            78: <td><input type=checkbox name=attr2 value="1" $attrchk[2]> Last modified</td>
1.1       www        79: </tr><tr>
1.2     ! harris41   80: <td><input type=checkbox name=attr3 value="1" $attrchk[3]> Author</td>
        !            81: <td><input type=checkbox name=attr4 value="1" $attrchk[4]> Keywords</td>
        !            82: <td><input type=checkbox name=attr5 value="1" $attrchk[5]> Language</td>
1.1       www        83: </tr></table>
1.2     ! harris41   84: <input type="submit" name="dirlistattr" value="Review">&nbsp;
1.1       www        85: <input type="submit" name="dirlistattr" value="Refresh">
                     86: </form>
                     87: END
                     88: 
1.2     ! harris41   89:     my $diropen = "/home/httpd/perl/tmp/$domain$ENV{'user.name'}_diropen.db";
1.1       www        90: 
1.2     ! harris41   91:     if (tie(%dirs,'GDBM_File',$diropen,&GDBM_WRCREAT,0640)) {
        !            92: 	my $titleclr="#ddffff";
        !            93: #	my $fileclr="#ffffdd";
        !            94: 	$r->print("<table border=0><tr><td bgcolor=#eeeeee>\n");
        !            95: 	$r->print("<table border=0><tr>\n");
        !            96: 	$r->print("<td bgcolor=$titleclr><b>Name</b></td>\n");
        !            97: 	$r->print("<td bgcolor=$titleclr align=right><b>Size (bytes) </b></td>\n") if ($ENV{'form.attr0'} == 1);
        !            98: 	$r->print("<td bgcolor=$titleclr><b>Last accessed</b></td>\n") if ($ENV{'form.attr1'} == 1);
        !            99: 	$r->print("<td bgcolor=$titleclr><b>Last modified</b></td>\n") if ($ENV{'form.attr2'} == 1);
        !           100: 	$r->print("<td bgcolor=$titleclr><b>Author(s)</b></td>\n") if ($ENV{'form.attr3'} == 1);
        !           101: 	$r->print("<td bgcolor=$titleclr><b>Keywords</b></td>\n") if ($ENV{'form.attr4'} == 1);
        !           102: 	$r->print("<td bgcolor=$titleclr><b>Language</b></td>\n") if ($ENV{'form.attr5'} == 1);
        !           103: 	$r->print("</tr>");
        !           104: 
        !           105: 	if ($ENV{'form.openuri'}) {  # take care of review and refresh options
        !           106: 	    my $uri=$ENV{'form.openuri'};
        !           107: 	    if (exists($dirs{$uri})) {
        !           108: 		my $cursta = $dirs{$uri};
        !           109: 		$dirs{$uri} = 'open';
        !           110: 		$dirs{$uri} = 'closed' if $cursta eq 'open';
        !           111: 	    } else {
        !           112: 		$dirs{$uri} = 'open';
        !           113: 	    }
        !           114: 	}
1.1       www       115: 
1.2     ! harris41  116: 	sort keys %dirs;
1.1       www       117: 
1.2     ! harris41  118: 	my $toplevel = "/res/";
        !           119: 	my $indent = -1;
        !           120: 	&scanDir ($r,$toplevel,$indent);
        !           121: 
        !           122: 	$r->print("</table>");
        !           123: 	$r->print("</td></tr></table>");
        !           124: 	$r->print("</body></html>\n");
        !           125: 	untie(%dirs);
        !           126:     } else {
        !           127: 	$r->print("Unable to tie hash to db file");
        !           128:     }
1.1       www       129:     return OK;
                    130: }
1.2     ! harris41  131: 
        !           132: # --------------------recursive scan of a directory
        !           133: sub scanDir {
        !           134:     my ($r,$startdir,$indent)=@_;
        !           135:     my $compuri;
1.1       www       136:     $indent++;
                    137: 
1.2     ! harris41  138:     my %dupdirs = %dirs;
        !           139:     sort keys %dupdirs;
        !           140:     my @list=&get_list($r,$startdir);
        !           141:     foreach my $line (@list) {
        !           142: 	my ($strip,$domusr,$foo,$testdir,$foo)=split(/\&/,$line,5); 
        !           143: 	if ($domusr eq "domain") {
        !           144: 	    $compuri=join('',$strip,"/");  # domain list has /res/<domain name>
        !           145: 	} else {
        !           146: 	    $compuri = join('',$startdir,$strip,"/"); # user, dir & file having name only, i.e., w/o path
        !           147: 	}
        !           148: 	my $diropen = 0;
        !           149: 	&display_line($r,$diropen,$line,$indent,$strip."/") if $domusr eq "domain";
        !           150: 	while (my ($key,$val)= each %dupdirs) {
        !           151: 	    $diropen = 1 if ($key eq $compuri and $val eq "open");
1.1       www       152: 	}
1.2     ! harris41  153: 	&display_line($r,$diropen,$line,$indent,$startdir) if ($domusr ne "domain");
        !           154: 	&scanDir ($r,$compuri,$indent) if $diropen == 1;
1.1       www       155:     }
                    156:     $indent--;
                    157: }
                    158: 
1.2     ! harris41  159: # ----------------- get complete matched list based on the uri ------
1.1       www       160: sub get_list {
                    161:     my ($r,$uri)=@_;
                    162:     my @list;
1.2     ! harris41  163:     my $luri = $uri;
        !           164:     my $domain  = $r->dir_config('lonDefDomain');
        !           165:     $luri =~ s/\//_/g;
        !           166: 
        !           167:     if ($ENV{'form.dirlistattr'} eq "Refresh") {
        !           168: 	my $tmpdir="/home/httpd/perl/tmp";
        !           169: 	my $filename;
        !           170: 	opendir(DIR,$tmpdir);
        !           171: 	while ($filename=readdir(DIR)) {
        !           172: 	    if ($filename=~/^$domain$ENV{'user.name'}_dirlist.*\.tmp$/) {
        !           173: 		unlink($tmpdir.'/'.$filename);
        !           174: 	    }
        !           175: 	}
        !           176: 	closedir(DIR);
        !           177:     }
        !           178: 
        !           179:     my $dirlist = "/home/httpd/perl/tmp/$domain$ENV{'user.name'}_dirlist$luri.tmp";
1.1       www       180:     if (-e $dirlist) {
                    181: 	my $FH = Apache::File->new($dirlist);
                    182: 	@list=<$FH>;
                    183:     } else {
                    184: 	@list=&Apache::lonnet::dirlist($uri);
                    185: 	my $FH = Apache::File->new(">$dirlist");
                    186: 	print $FH join("\n",@list);
                    187:     }
1.2     ! harris41  188:     @list = sort(@list);
1.1       www       189:     return @list=&match_ext($r,@list);
                    190: }
                    191: 
1.2     ! harris41  192: #-------------------------- filters out files based on extensions
1.1       www       193: sub match_ext {
                    194:     my ($r,@packlist)=@_;
                    195:     my @trimlist;
                    196:     my $nextline;
                    197:     my @fileext;
                    198:     my $dirptr=16384;
                    199: 
                    200:     my $tabdir  = $r->dir_config('lonTabDir');
                    201:     my $fn = $tabdir."/filetypes.tab";
                    202:     if (-e $fn) {
                    203: 	my $FH=Apache::File->new($fn);
                    204: 	my @content=<$FH>;
1.2     ! harris41  205: 	foreach my $line (@content) {
1.1       www       206: 	    (my $ext,my $foo) = split /\s+/,$line;
                    207: 	    push @fileext,$ext;
                    208: 	}
                    209:     }
1.2     ! harris41  210:     foreach my $line (@packlist) {
        !           211: 	chomp $line;
        !           212: 	$line =~ s/^\/home\/httpd\/html//;
        !           213: 	my @unpackline = split (/\&/,$line);
        !           214: 	next if ($unpackline[0] eq ".");
        !           215: 	next if ($unpackline[0] eq "..");
        !           216: 	my @filecom = split (/\./,$unpackline[0]);
        !           217: 	my $fext = pop(@filecom);
        !           218: 	next if $fext eq "meta";
        !           219: 	my $fnptr = $unpackline[3]&$dirptr;
        !           220:  	if ($fnptr == 0 and $unpackline[3] ne "") {
        !           221: 	    foreach my $nextline (@fileext) {
        !           222: 		push @trimlist,$line if $nextline eq $fext;
1.1       www       223: 	    }
                    224: 	} else {
1.2     ! harris41  225: 	    push @trimlist,$line;
1.1       www       226: 	}
                    227:     }
                    228:     return @trimlist;
                    229: }
                    230: 
1.2     ! harris41  231: #------------------- displays one line in appropriate table format
1.1       www       232: sub display_line{
1.2     ! harris41  233:     my ($r,$diropen,$line,$indent,$startdir)=@_;
1.1       www       234:     my (@pathfn, $fndir, $fnptr);
                    235:     my $dirptr=16384;
                    236:     my $fileclr="#ffffe6";
                    237:     my $iconpath="/res/adm/pages/indexericons/";
                    238: 
                    239:     my @filecom = split (/\&/,$line);
                    240:     my @pathcom = split (/\//,$filecom[0]);
                    241:     my $listname = $pathcom[scalar(@pathcom)-1];
                    242:     my $fnptr = $filecom[3]&$dirptr;
                    243: 
                    244:     my $tabtag="</td>";
                    245:     my $i=0;
                    246: 
                    247:     while ($i<=5) {
                    248: 	my $key="form.attr".$i;
                    249: 	$tabtag=join('',$tabtag,"<td bgcolor=",$fileclr,">&nbsp;</td>") if $ENV{$key} == 1;
                    250: 	$i++;
                    251:     }
                    252:     if ($filecom[1] eq "domain") {
                    253: 	$r->print("<tr>");
                    254: 	$r->print("<td bgcolor=$fileclr valign=bottom>");
1.2     ! harris41  255: 	&begin_form ($r,$filecom[0].'/');
1.1       www       256: 	$r->print ("<input src=\"$iconpath");
                    257: 	$r->print ("comp.blue.gif\"");
1.2     ! harris41  258: 	$r->print (" name=\"View $filecom[0]/ resources\" height=\"22\" type=\"image\" border=\"0\">\n");
1.1       www       259: 	$r->print("Domain - $listname $tabtag</tr></form>\n");
                    260: 	return OK;
                    261:     }
                    262:     if ($filecom[1] eq "user") {
                    263: 	$r->print("<tr>");
                    264: 	$r->print("<td bgcolor=$fileclr valign=bottom>\n");
1.2     ! harris41  265: 	my $curdir = $startdir.$filecom[0].'/';
        !           266: 	&begin_form ($r,$curdir);
        !           267: 	$r->print("<img src=",$iconpath,"white_space_20_22.gif border=0>\n");
1.1       www       268: 	$r->print ("<input src=\"$iconpath");
                    269: 	$r->print ("folder_pointer_closed.gif\"") if $diropen == 0;
                    270: 	$r->print ("folder_pointer_opened.gif\"") if $diropen == 1;
1.2     ! harris41  271: 	$r->print (" name=\"View $curdir resources\" height=\"22\" type=\"image\" border=\"0\">\n");
1.1       www       272: 	$r->print("<img src=",$iconpath,"quill.gif border=0>\n");
                    273: 	$r->print("$listname $tabtag</tr></form>\n");
                    274: 	return OK;
                    275:     }
                    276: # display file
                    277:     if ($fnptr == 0 and $filecom[3] ne "") {
                    278: 	my @file_ext = split (/\./,$listname);
                    279: 	my $curfext = $file_ext[scalar(@file_ext)-1];
1.2     ! harris41  280: 	my $filelink = $startdir.$filecom[0];
1.1       www       281: 	my $count = 0;
                    282: 	$r->print("<tr><td bgcolor=$fileclr>");
1.2     ! harris41  283: 	while ($count < $indent) {
1.1       www       284: 	    $r->print("<img src=",$iconpath,"white_space_20_22.gif border=0>\n");
                    285: 	    $count++;
                    286: 	}
                    287: 	$r->print("<img src=",$iconpath,"white_space_20_22.gif border=0>\n");
                    288: 	$r->print("<img src=$iconpath$curfext.gif border=0>\n");
1.2     ! harris41  289: 	$r->print(" <a href=$filelink>",$listname,"</a>");
        !           290: 	my $metafile = '/home/httpd/html'.$filelink.'.meta';
        !           291: 
        !           292: 	$r->print (" (<a href=\"javascript:openWindow('".$filelink.".meta', 'metadata', '400', '450', 'no', 'yes')\"; TARGET=_self>metadata</a>) ") if (-e $metafile);
        !           293: 
        !           294: #	$r->print(" (<a href=$filelink.meta target=cat>metadata</a>)") if (-e $metafile);
        !           295: 	$r=>print("</td>\n");
1.1       www       296: 	$r->print("<td bgcolor=$fileclr align=right valign=bottom> ",$filecom[8]," </td>\n") if $ENV{'form.attr0'} == 1;
                    297: 	$r->print("<td bgcolor=$fileclr valign=bottom> ".(localtime($filecom[9]))." </td>\n") if $ENV{'form.attr1'} == 1;
                    298: 	$r->print("<td bgcolor=$fileclr valign=bottom> ".(localtime($filecom[10]))." </td>\n") if $ENV{'form.attr2'} == 1;
1.2     ! harris41  299: 
        !           300: 	if ($ENV{'form.attr3'} == 1) {
        !           301: 	    my $author = &Apache::lonnet::metadata($filelink,'author');
        !           302: 	    $author = '&nbsp;' if (!$author);
        !           303: 	    $r->print("<td bgcolor=$fileclr valign=bottom> ".$author." </td>\n");
        !           304: 	}
        !           305: 	if ($ENV{'form.attr4'} == 1) {
        !           306: 	    my $keywords = &Apache::lonnet::metadata($filelink,'keywords');
        !           307: 	    $keywords = '&nbsp;' if (!$keywords);
        !           308: 	    $r->print("<td bgcolor=$fileclr valign=bottom> ".$keywords." </td>\n");
        !           309: 	}
        !           310: 	if ($ENV{'form.attr5'} == 1) {
        !           311: 	    my $lang = &Apache::lonnet::metadata($filelink,'language');
        !           312: 	    $lang = $language{$lang};
        !           313: 	    $lang = '&nbsp;' if (!$lang);
        !           314: 	    $r->print("<td bgcolor=$fileclr valign=bottom> ".$lang." </td>\n");
        !           315: 	}
1.1       www       316: 	$r->print("</tr>\n");
                    317:     }
1.2     ! harris41  318: # -- display directory
1.1       www       319:     if ($fnptr == $dirptr) {
                    320: 	my @file_ext = split (/\./,$listname);
                    321: 	my $curfext = $file_ext[scalar(@file_ext)-1];
1.2     ! harris41  322: 	my $curdir = $startdir.$filecom[0].'/';
1.1       www       323: 	$r->print("<tr><td bgcolor=$fileclr valign=bottom>");
1.2     ! harris41  324: 	&begin_form ($r,$curdir);
1.1       www       325: 
                    326: 	my $count = 0;
1.2     ! harris41  327: 	while ($count < $indent) {
1.1       www       328: 	    $r->print("<img src=",$iconpath,"white_space_20_22.gif border=0>\n");
                    329: 	    $count++;
                    330: 	}
                    331: 
                    332: 	$r->print ("<input src=\"$iconpath");
                    333: 	$r->print ("folder_pointer_closed.gif\"") if $diropen == 0;
                    334: 	$r->print ("folder_pointer_opened.gif\"") if $diropen == 1;
1.2     ! harris41  335: 	$r->print (" name=\"View $curdir resources\" height=\"22\" type=\"image\" border=\"0\">\n");
1.1       www       336: 	$r->print("<img src=",$iconpath,"folder_closed.gif border=0>\n") if $diropen == 0;
                    337: 	$r->print("<img src=",$iconpath,"folder_opened.gif border=0>\n") if $diropen == 1;
                    338: 	$r->print("$listname $tabtag</tr></form>\n");
                    339:     }
1.2     ! harris41  340: 
1.1       www       341: }
                    342: 
1.2     ! harris41  343: #---------------------prints the beginning of a form for directory or file link
1.1       www       344: sub begin_form {
                    345:     my ($r,$uri) = @_;
1.2     ! harris41  346: 
        !           347:     $r->print ("<form method=\"post\" name=\"dirpath\" action=\"/res/\" enctype=\"application/x-www-form-urlencoded\">\n");
        !           348:     $r->print ("<input type=hidden name=openuri value=\"$uri\">\n");
1.1       www       349: 
                    350:     for (my $i=0; $i<=5; $i++) {
                    351: 	$r->print ("<input type=hidden name=attr$i value=\"1\">\n") if $ENV{'form.attr'.$i} == 1;
                    352:     }
                    353: }
                    354: 
                    355: 1;
                    356: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>