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