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

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

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