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: #
8: # 05/9-05/19/2001 H. K. Ng
9: # 05/21/2001 H. K. Ng
10: #
11: package Apache::lonindexer;
12:
13: use strict;
14: use Apache::lonnet();
15: use Apache::Constants qw(:common);
16: use Apache::File;
17: use GDBM_File;
18:
19: my %hash;
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: }
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>
50: <title>The LearningOnline Network With CAPA Directory Browser</title>
51:
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>
61:
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:
71: $r->print("<h2><font color=\"\#888888\">The LearningOnline With CAPA Network Directory Browser</font></h2>\n");
72:
73: for (my $i=0; $i<=5; $i++) {
74: $attrchk[$i] = "checked" if $ENV{'form.attr'.$i} == 1;
75: }
76: $r->print(<<END);
77: <b><font color="#666666">Display file attributes</font></b><br>
78: <form method="post" name="fileattr" action="$uri" enctype="application/x-www-form-urlencoded">
79: <table border=0><tr>
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>
83: </tr><tr>
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>
87: </tr></table>
88: <input type="submit" name="dirlistattr" value="Review">
89: <input type="submit" name="dirlistattr" value="Refresh">
90: </form>
91: END
92:
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";
106:
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:
116: if ($ENV{'form.openuri'}) { # take care of review and refresh options
117: my $uri=$ENV{'form.openuri'};
118: if (exists($hash{'diropen_status_'.$uri})) {
119: my $cursta = $hash{'diropen_status_'.$uri};
120: $dirs{$uri} = 'open';
121: $hash{'diropen_status_'.$uri} = 'open';
122: if ($cursta eq 'open') {
123: $dirs{$uri} = 'closed';
124: $hash{'diropen_status_'.$uri} = 'closed';
125: }
126: } else {
127: $hash{'diropen_status_'.$uri} = 'open';
128: $dirs{$uri} = 'open';
129: }
130: }
131:
132: my $toplevel = "/res/";
133: my $indent = 0;
134: &scanDir ($r,$toplevel,$indent);
135:
136: $r->print("</table>");
137: $r->print("</td></tr></table>");
138: $r->print("</body></html>\n");
139: untie(%hash);
140: } else {
141: $r->print("Unable to tie hash to db file");
142: }
143: return OK;
144: }
145:
146: # --------------------recursive scan of a directory
147: sub scanDir {
148: my ($r,$startdir,$indent)=@_;
149: my ($compuri,$curdir);
150: my $dirptr=16384;
151: $indent++;
152:
153: my %dupdirs = %dirs;
154: my @list=&get_list($r,$startdir);
155: foreach my $line (@list) {
156: my ($strip,$dom,$foo,$testdir,$foo)=split(/\&/,$line,5);
157: next if $strip =~ /.*\.meta$/;
158: if ($dom eq "domain") {
159: $compuri = join('',$strip,"/"); # domain list has /res/<domain name>
160: $curdir = $compuri;
161: } else {
162: $compuri = join('',$startdir,$strip,"/"); # user, dir & file having name only, i.e., w/o path
163: $curdir = $startdir;
164: }
165: my $diropen = 0;
166: if (($dirptr&$testdir) or ($dom =~ /^(domain|user)$/)) {
167: while (my ($key,$val)= each %dupdirs) {
168: if ($key eq $compuri and $val eq "open") {
169: $diropen = 1;
170: delete $dupdirs{key},$dirs{$key};
171: }
172: }
173: }
174: &display_line($r,$diropen,$line,$indent,$curdir,@list);
175: &scanDir ($r,$compuri,$indent) if $diropen == 1;
176: }
177: $indent--;
178: }
179:
180: # ----------------- get complete matched list based on the uri ------
181: sub get_list {
182: my ($r,$uri)=@_;
183: my @list;
184: my $luri = $uri;
185: $luri =~ s/\//_/g;
186:
187: if ($ENV{'form.dirlistattr'} eq "Refresh") {
188: map {
189: delete $hash{$_} if ($_ =~ /^dirlist_files_/);
190: } keys %hash;
191: }
192:
193: if ($hash{'dirlist_files'.$luri}) {
194: @list = split(/\n/,$hash{'dirlist_files_'.$luri});
195: } else {
196: @list = &Apache::lonnet::dirlist($uri);
197: $hash{'dirlist_files_'.$luri} = join('\n',@list);
198: }
199: return @list=&match_ext($r,@list);
200: }
201:
202: #-------------------------- filters out files based on extensions
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>;
215: foreach my $line (@content) {
216: (my $ext,my $foo) = split /\s+/,$line;
217: push @fileext,$ext;
218: }
219: }
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;
232: }
233: } else {
234: push @trimlist,$line;
235: }
236: }
237: @trimlist = sort (@trimlist);
238: return @trimlist;
239: }
240:
241: #------------------- displays one line in appropriate table format
242: sub display_line{
243: my ($r,$diropen,$line,$indent,$startdir,@list)=@_;
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;
253: my $msg = 'View '.$filecom[0].' resources';
254: $msg = 'Close '.$filecom[0].' directory' if $diropen == 1;
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,"> </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>");
267: &begin_form ($r,$filecom[0].'/');
268: my $anchor = $filecom[0].'/';
269: $anchor =~ s/\///g;
270: $r->print ("<a name=\"".$anchor."\">\n<input src=\"".$iconpath."comp.blue.gif\"");
271: $r->print (" name=\"$msg\" height=\"22\" type=\"image\" border=\"0\">\n");
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");
278: my $curdir = $startdir.$filecom[0].'/';
279: &begin_form ($r,$curdir);
280: my $anchor = $curdir;
281: $anchor =~ s/\///g;
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");
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;
287: $r->print (" name=\"$msg\" height=\"22\" type=\"image\" border=\"0\">\n");
288: $r->print ("<img src=",$iconpath,"quill.gif border=0>\n");
289: $r->print ("$listname $tabtag</tr></form>\n");
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];
296: my $filelink = $startdir.$filecom[0];
297: $r->print("<tr><td bgcolor=$fileclr>");
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");
307: $count++;
308: }
309: $r->print("<img src=",$iconpath,"whitespace",$rem,".gif border=0>\n") if $rem > 0;
310: }
311:
312: $r->print("<img src=$iconpath$curfext.gif border=0>\n");
313: $r->print(" <a href=$filelink>",$listname,"</a>\n");
314: my $metafile = grep /^$filecom[0]\.meta\&/, @list;
315:
316: $r->print (" (<a href=\"javascript:openWindow('".$filelink.".meta', 'metadatafile', '400', '450', 'no', 'yes')\"; TARGET=_self>metadata</a>) ") if ($metafile == 1);
317:
318: $r=>print("</td>\n");
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;
322:
323: if ($ENV{'form.attr3'} == 1) {
324: my $author = &Apache::lonnet::metadata($filelink,'author') if ($metafile == 1);
325: $author = ' ' if (!$author);
326: $r->print("<td bgcolor=$fileclr valign=bottom> ".$author." </td>\n");
327: }
328: if ($ENV{'form.attr4'} == 1) {
329: my $keywords = &Apache::lonnet::metadata($filelink,'keywords') if ($metafile == 1);
330: $keywords = ' ' if (!$keywords);
331: $r->print("<td bgcolor=$fileclr valign=bottom> ".$keywords." </td>\n");
332: }
333: if ($ENV{'form.attr5'} == 1) {
334: my $lang = &Apache::lonnet::metadata($filelink,'language') if ($metafile == 1);
335: $lang = $language{$lang};
336: $lang = ' ' if (!$lang);
337: $r->print("<td bgcolor=$fileclr valign=bottom> ".$lang." </td>\n");
338: }
339: $r->print("</tr>\n");
340: }
341: # -- display directory
342: if ($fnptr == $dirptr) {
343: my @file_ext = split (/\./,$listname);
344: my $curfext = $file_ext[scalar(@file_ext)-1];
345: my $curdir = $startdir.$filecom[0].'/';
346: my $anchor = $curdir;
347: $anchor =~ s/\///g;
348: $r->print("<tr><td bgcolor=$fileclr valign=bottom>");
349: &begin_form ($r,$curdir);
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");
359: $count++;
360: }
361: $r->print("<img src=",$iconpath,"whitespace",$rem,".gif border=0>\n") if $rem > 0;
362: }
363: $r->print ("<a name=\"$anchor\">\n<input src=\"$iconpath");
364: $r->print ("folder_pointer_closed.gif\"") if $diropen == 0;
365: $r->print ("folder_pointer_opened.gif\"") if $diropen == 1;
366: $r->print (" name=\"$msg\" height=\"22\" type=\"image\" border=\"0\">\n");
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: }
371:
372: }
373:
374: #---------------------prints the beginning of a form for directory or file link
375: sub begin_form {
376: my ($r,$uri) = @_;
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");
380: $r->print ("<input type=hidden name=openuri value=\"$uri\">\n");
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>