1: # The LearningOnline Network with CAPA
2: # Directory Indexer
3: #
4: # $Id: lonindexer.pm,v 1.70 2003/07/21 18:35:25 ng Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: # YEAR=1999
29: # 5/21/99, 5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14 Gerd Kortemeyer)
30: # 11/23 Gerd Kortemeyer
31: # YEAR=2000
32: # 07/20-08/04 H.K. Ng
33: # YEAR=2001
34: # 05/9-05/19/2001 H. K. Ng
35: # 05/21/2001 H. K. Ng
36: # 05/23/2001 H. K. Ng
37: # 6/26,7/8 H. K. Ng
38: # 8/14 H. K. Ng
39: # 11/30 Matthew Hall
40: # YEAR=2002
41: # 6/29/2002 H. K. Ng
42: #
43: ###
44:
45: ###############################################################################
46: ## ##
47: ## ORGANIZATION OF THIS PERL MODULE ##
48: ## ##
49: ## 1. Description of functions ##
50: ## 2. Modules used by this module ##
51: ## 3. Choices for different output views (detailed, summary, xml, etc) ##
52: ## 4. BEGIN block (to be run once after compilation) ##
53: ## 5. Handling routine called via Apache and mod_perl ##
54: ## 6. Other subroutines ##
55: ## ##
56: ###############################################################################
57:
58: package Apache::lonindexer;
59:
60: # ------------------------------------------------- modules used by this module
61: use strict;
62: use Apache::lonnet();
63: use Apache::loncommon();
64: use Apache::Constants qw(:common);
65: use Apache::lonmeta;
66: use Apache::File;
67: use GDBM_File;
68:
69: # ---------------------------------------- variables used throughout the module
70: my %hash; # tied to a user-specific gdbm file
71: my %dirs; # keys are directories, values are the open/close status
72: my %language; # has the reference information present in language.tab
73:
74: # ----- Values which are set by the handler subroutine and are accessible to
75: # ----- other methods.
76: my $extrafield; # default extra table cell
77: my $fnum; # file counter
78: my $dnum; # directory counter
79:
80: # ----- Used to include or exclude files with certain extensions.
81: my @Only = ();
82: my @Omit = ();
83:
84:
85: # ----------------------------- Handling routine called via Apache and mod_perl
86: sub handler {
87: my $r = shift;
88: my $c = $r->connection();
89: $r->content_type('text/html');
90: &Apache::loncommon::no_cache($r);
91: $r->send_http_header;
92: return OK if $r->header_only;
93: $fnum=0;
94: $dnum=0;
95:
96: # Deal with stupid global variables (is there a way around making
97: # these global to this package? It is just so wrong....)
98: undef (@Only);
99: undef (@Omit);
100:
101: # ------------------------------------- read in machine configuration variables
102: my $iconpath= $r->dir_config('lonIconsURL') . "/";
103: my $domain = $r->dir_config('lonDefDomain');
104: my $role = $r->dir_config('lonRole');
105: my $loadlim = $r->dir_config('lonLoadLim');
106: my $servadm = $r->dir_config('lonAdmEMail');
107: my $sysadm = $r->dir_config('lonSysEMail');
108: my $lonhost = $r->dir_config('lonHostID');
109: my $tabdir = $r->dir_config('lonTabDir');
110:
111: my $fileclr='#ffffe6';
112: my $line;
113: my (@attrchk,@openpath);
114: my $uri=$r->uri;
115:
116: # -------------------------------------- see if called from an interactive mode
117: # Get the parameters from the query string
118: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
119: ['catalogmode','launch','acts','mode','form','element',
120: 'only','omit']);
121: #-------------------------------------------------------------------
122: my $closebutton='';
123: my $groupimportbutton='';
124: my $colspan='';
125:
126: $extrafield='';
127: my $diropendb =
128: "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_indexer.db";
129: %hash = ();
130: {
131: my %dbfile;
132: if (tie(%dbfile,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
133: while(my($key,$value)=each(%dbfile)) {
134: $hash{$key}=$value;
135: }
136: untie(%dbfile);
137: }
138: }
139: {
140: if ($ENV{'form.launch'} eq '1') {
141: &start_fresh_session();
142: }
143: # -------------------- refresh environment with user database values (in %hash)
144: &setvalues(\%hash,'form.catalogmode',\%ENV,'form.catalogmode' );
145:
146: # --------------------- define extra fields and buttons in case of special mode
147: if ($ENV{'form.catalogmode'} eq 'interactive') {
148: $extrafield='<td bgcolor="'.$fileclr.'" valign="bottom">'.
149: '<a name="$anchor"><img src="'.$iconpath.'whitespace1.gif"'.
150: ' border="0" /></td>';
151: $colspan=" colspan='2' ";
152: $closebutton=<<END;
153: <input type="button" name="close" value='CLOSE' onClick="self.close()">
154: END
155: }
156: elsif ($ENV{'form.catalogmode'} eq 'groupimport') {
157: $extrafield='<td bgcolor="'.$fileclr.'" valign="bottom">'.
158: '<a name="$anchor"><img src="'.$iconpath.'whitespace1.gif"'.
159: ' border="0" /></td>';
160: $colspan=" colspan='2' ";
161: $closebutton=<<END;
162: <input type="button" name="close" value='CLOSE' onClick="self.close()">
163: END
164: $groupimportbutton=<<END;
165: <input type="button" name="groupimport" value='GROUP IMPORT'
166: onClick="javascript:select_group()">
167: END
168: }
169: # Additions made by Matthew to make the browser a little easier to deal
170: # with in the future.
171: #
172: # $mode (at this time) indicates if we are in edit mode.
173: # $form is the name of the form that the URL is placed when the
174: # selection is made.
175: # $element is the name of the element in $formname which receives
176: # the URL.
177: # &Apache::lonxml::debug('Checking mode, form, element');
178: &setvalues(\%hash,'form.mode' ,\%ENV,'form.mode' );
179: &setvalues(\%hash,'form.form' ,\%ENV,'form.form' );
180: &setvalues(\%hash,'form.element',\%ENV,'form.element');
181: &setvalues(\%hash,'form.only' ,\%ENV,'form.only' );
182: &setvalues(\%hash,'form.omit' ,\%ENV,'form.omit' );
183:
184: # Deal with 'omit' and 'only'
185: if (exists $ENV{'form.omit'}) {
186: @Omit = split(',',$ENV{'form.omit'});
187: }
188: if (exists $ENV{'form.only'}) {
189: @Only = split(',',$ENV{'form.only'});
190: }
191:
192: my $mode = $ENV{'form.mode'};
193: my ($form,$element);
194: if ($mode eq 'edit' || $mode eq 'parmset') {
195: $form = $ENV{'form.form'};
196: $element = $ENV{'form.element'};
197: }
198: &Apache::lonxml::debug("mode=$mode form=$form element=$element");
199: # ------ set catalogmodefunctions to have extra needed javascript functionality
200: my $catalogmodefunctions='';
201: if ($ENV{'form.catalogmode'} eq 'interactive' or
202: $ENV{'form.catalogmode'} eq 'groupimport') {
203: # The if statement below sets us up to use the old version
204: # by default (ie. if $mode is undefined). This is the easy
205: # way out. Hopefully in the future I'll find a way to get
206: # the calls dealt with in a more comprehensive manner.
207:
208: #
209: # There is now also mode "simple", which is for the simple version of the rat
210: #
211: #
212: if (!defined($mode) || ($mode ne 'edit' && $mode ne 'parmset')) {
213: my $location = "/adm/groupsort?catalogmode=groupimport&";
214: $location .= "mode=".$mode."&";
215: $location .= "acts=";
216: $catalogmodefunctions=<<"END";
217: function select_data(title,url) {
218: changeTitle(title);
219: changeURL(url);
220: self.close();
221: }
222: function select_group() {
223: window.location="$location"+document.forms.fileattr.acts.value;
224: }
225: function changeTitle(val) {
226: if (opener.inf) {
227: if (opener.inf.document.forms.resinfo.elements.t) {
228: opener.inf.document.forms.resinfo.elements.t.value=val;
229: }
230: }
231: }
232: function changeURL(val) {
233: if (opener.inf) {
234: if (opener.inf.document.forms.resinfo.elements.u) {
235: opener.inf.document.forms.resinfo.elements.u.value=val;
236: }
237: }
238: }
239: END
240: } elsif ($mode eq 'edit') { # we are in 'edit' mode
241: my $location = "/adm/groupsort?catalogmode=interactive&";
242: $location .= "form=$form&element=$element&mode=edit&acts=";
243: $catalogmodefunctions=<<END;
244: // mode = $mode
245: function select_data(title,url) {
246: changeURL(url);
247: self.close();
248: }
249:
250: function select_group() {
251: window.location="$location"+document.forms.fileattr.acts.value;
252: }
253:
254: function changeURL(val) {
255: if (window.opener.document) {
256: window.opener.document.forms["$form"].elements["$element"].value=val;
257: } else {
258: alert("The file you selected is: "+val);
259: }
260: }
261:
262: END
263: } elsif ($mode eq 'parmset') {
264: my $location = "/adm/groupsort?catalogmode=interactive&";
265: $location .= "form=$form&element=$element&mode=parmset&acts=";
266: $catalogmodefunctions=<<END;
267: // mode = $mode
268: function select_data(title,url) {
269: changeURL(url);
270: self.close();
271: }
272:
273: function select_group() {
274: window.location="$location"+document.forms.fileattr.acts.value;
275: }
276:
277: function changeURL(val) {
278: if (window.opener.document) {
279: var elementname = "$element"+"_value";
280: var checkboxname = "$element"+"_setparmval";
281: window.opener.document.forms["$form"].elements[elementname].value=val;
282: window.opener.document.forms["$form"].elements[checkboxname].checked=true;
283: } else {
284: alert("The file you selected is: "+val);
285: }
286: }
287:
288: END
289: }
290: }
291: $catalogmodefunctions.=<<END;
292: var acts='';
293: function rep_dirpath(suffix,val) {
294: eval("document.forms.dirpath"+suffix+".acts.value=val");
295: }
296: END
297: if ($ENV{'form.catalogmode'} eq 'groupimport') {
298: $catalogmodefunctions.=<<END;
299: function queue(val) {
300: if (eval("document.forms."+val+".filelink.checked")) {
301: var l=val.length;
302: var v=val.substring(4,l);
303: document.forms.fileattr.acts.value+='1a'+v+'b';
304: }
305: else {
306: var l=val.length;
307: var v=val.substring(4,l);
308: document.forms.fileattr.acts.value+='0a'+v+'b';
309: }
310: }
311: END
312: }
313:
314: # ---------------------------------------------------------------- Print Header
315: $r->print(<<ENDHEADER);
316: <html>
317: <head>
318: <title>The LearningOnline Network With CAPA Directory Browser</title>
319:
320: <script type="text/javascript">
321: $catalogmodefunctions
322: function openWindow(url, wdwName, w, h, toolbar,scrollbar,locationbar) {
323: var options = "width=" + w + ",height=" + h + ",";
324: options += "resizable=yes,scrollbars="+scrollbar+",status=no,";
325: options += "menubar=no,toolbar="+toolbar+",location="+locationbar+",directories=no";
326: var newWin = window.open(url, wdwName, options);
327: newWin.focus();
328: }
329: function gothere(val) {
330: window.location=val+'?acts='+document.forms.fileattr.acts.value;
331: }
332: </script>
333:
334: </head>
335: ENDHEADER
336: $r->print(&Apache::loncommon::bodytag('Browse Resources'));
337: # - Evaluate actions from previous page (both cumulatively and chronologically)
338: if ($ENV{'form.catalogmode'} eq 'groupimport') {
339: my $acts=$ENV{'form.acts'};
340: my @Acts=split(/b/,$acts);
341: my %ahash;
342: my %achash;
343: my $ac=0;
344: # some initial hashes for working with data
345: foreach (@Acts) {
346: my ($state,$ref)=split(/a/);
347: $ahash{$ref}=$state;
348: $achash{$ref}=$ac;
349: $ac++;
350: }
351: # sorting through the actions and changing the tied database hash
352: foreach (sort {$achash{$a}<=>$achash{$b}} (keys %ahash)) {
353: my $key=$_;
354: if ($ahash{$key} eq '1') {
355: $hash{'store_'.$hash{'pre_'.$key.'_link'}}=
356: $hash{'pre_'.$key.'_title'};
357: $hash{'storectr_'.$hash{'pre_'.$key.'_link'}}=
358: $hash{'storectr'}+0;
359: $hash{'storectr'}++;
360: }
361: if ($ahash{$key} eq '0') {
362: if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
363: delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
364: }
365: }
366: }
367: # deleting the previously cached listing
368: foreach (keys %hash) {
369: if ($_ =~ /^pre_/ && $_ =~/link$/) {
370: my $key = $_;
371: $key =~ s/^pre_//;
372: $key =~ s/_[^_]*$//;
373: delete $hash{'pre_'.$key.'_title'};
374: delete $hash{'pre_'.$key.'_link'};
375: }
376: }
377: }
378:
379: # ---------------------------------- get state of file attributes to be showing
380: if ($ENV{'form.attrs'} ne '') {
381: for (my $i=0; $i<=9; $i++) {
382: delete $hash{'display_attrs_'.$i};
383: if ($ENV{'form.attr'.$i} == 1) {
384: $attrchk[$i] = 'checked';
385: $hash{'display_attrs_'.$i} = 1;
386: }
387: }
388: } else {
389: for (my $i=0; $i<=9; $i++) {
390: $attrchk[$i] = 'checked' if $hash{'display_attrs_'.$i} == 1;
391: }
392: }
393: # ------------------------------- output state of file attributes to be showing
394: $r->print(<<END);
395: <form method="post" name="fileattr" action="$uri"
396: enctype="application/x-www-form-urlencoded">
397: <b><font color="#666666">Display file attributes</font></b><br />
398: <table border=0><tr>
399: <td><input type="checkbox" name="attr0" value="1" $attrchk[0] /> Title</td>
400: <td><input type="checkbox" name="attr1" value="1" $attrchk[1] /> Size</td>
401: <td><input type="checkbox" name="attr2" value="1" $attrchk[2] /> Last access</td>
402: <td><input type="checkbox" name="attr3" value="1" $attrchk[3] /> Last modified</td>
403: <td><input type="checkbox" name="attr8" value="1" $attrchk[8] /> Statistics</td></tr><tr>
404: <td><input type="checkbox" name="attr4" value="1" $attrchk[4] /> Author</td>
405: <td><input type="checkbox" name="attr5" value="1" $attrchk[5] /> Keywords</td>
406: <td><input type="checkbox" name="attr6" value="1" $attrchk[6] /> Language</td>
407: <td><input type="checkbox" name="attr7" value="1" $attrchk[7] /> Show Resource</td>
408: <td><input type="checkbox" name="attr9" value="1" $attrchk[9] /> All versions</td>
409: <td> </td>
410: </tr></table>
411: <input type="hidden" name="dirPointer" value="on" />
412: <input type="hidden" name="acts" value="" />
413: <input type="submit" name="attrs" value="Review" />
414: <input type="submit" name="attrs" value="Refresh" />
415: $closebutton
416: $groupimportbutton
417: </form>
418: END
419:
420: # ----------------- output starting row to the indexed file/directory hierarchy
421: my $titleclr="#ddffff";
422: # $r->print(&initdebug());
423: # $r->print(&writedebug("Omit:@Omit")) if (@Omit);
424: # $r->print(&writedebug("Only:@Only")) if (@Only);
425: $r->print("<table width='100\%' border=0><tr><td bgcolor=#777777>\n");
426: $r->print("<table width='100\%' border=0><tr bgcolor=$titleclr>\n");
427: $r->print("<td $colspan><b>Name</b></td>\n");
428: $r->print("<td><b>Title</b></td>\n")
429: if ($hash{'display_attrs_0'} == 1);
430: $r->print("<td align=right><b>Size (bytes) ".
431: "</b></td>\n") if ($hash{'display_attrs_1'} == 1);
432: $r->print("<td><b>Last accessed</b></td>\n")
433: if ($hash{'display_attrs_2'} == 1);
434: $r->print("<td><b>Last modified</b></td>\n")
435: if ($hash{'display_attrs_3'} == 1);
436: $r->print("<td><b>Author(s)</b></td>\n")
437: if ($hash{'display_attrs_4'} == 1);
438: $r->print("<td><b>Keywords</b></td>\n")
439: if ($hash{'display_attrs_5'} == 1);
440: $r->print("<td><b>Language</b></td>\n")
441: if ($hash{'display_attrs_6'} == 1);
442: $r->print("<td><b>Resource</b></td>\n")
443: if ($hash{'display_attrs_7'} == 1);
444: $r->print("<td><b>Usage Statistics <br />(Courses/Network Hits)</b></td>\n")
445: if ($hash{'display_attrs_8'} == 1);
446: $r->print('</tr>');
447:
448: # ----------------- read in what directories have previously been set to "open"
449: foreach (keys %hash) {
450: if ($_ =~ /^diropen_status_/) {
451: my $key = $_;
452: $key =~ s/^diropen_status_//;
453: $dirs{$key} = $hash{$_};
454: }
455: }
456:
457: if ($ENV{'form.openuri'}) { # take care of review and refresh options
458: my $uri=$ENV{'form.openuri'};
459: if (exists($hash{'diropen_status_'.$uri})) {
460: my $cursta = $hash{'diropen_status_'.$uri};
461: $dirs{$uri} = 'open';
462: $hash{'diropen_status_'.$uri} = 'open';
463: if ($cursta eq 'open') {
464: $dirs{$uri} = 'closed';
465: $hash{'diropen_status_'.$uri} = 'closed';
466: }
467: } else {
468: $hash{'diropen_status_'.$uri} = 'open';
469: $dirs{$uri} = 'open';
470: }
471: }
472:
473: my $bredir = $ENV{'form.dirPointer'};
474: my $toplevel;
475: my $indent = 0;
476: $uri = $uri.'/' if $uri !~ /.*\/$/;
477:
478: if ($bredir ne 'on') {
479: $hash{'top.level'} = $uri;
480: $toplevel = $uri;
481:
482: } else {
483: $toplevel = $hash{'top.level'};
484: }
485:
486: # -------------------------------- if not at top level, provide an uplink arrow
487: if ($toplevel ne '/res/'){
488: my (@uri_com) = split(/\//,$uri);
489: pop @uri_com;
490: my $upone = join('/',@uri_com);
491: my @list = qw (0);
492: &display_line ($r,'opened',$upone.'&viewOneUp',0,$upone,@list);
493: $indent = 1;
494: }
495:
496: # -------- recursively go through all the directories and output as appropriate
497: &scanDir ($r,$toplevel,$indent,\%hash);
498:
499: # ---------------------------- embed hidden information useful for group import
500: $r->print("<form name='fnum'>");
501: $r->print("<input type='hidden' name='fnum' value='$fnum'></form>");
502:
503: # -------------------------------------------------------------- end the tables
504: $r->print('</table>');
505: $r->print('</td></tr></table>');
506:
507: # --------------------------------------------------- end the output and return
508: $r->print('</body></html>'."\n");
509: # } else {
510: # $r->print('<html><head></head><body>Unable to tie hash to db '.
511: # 'file</body></html>');
512: # return OK;
513: }
514: if(! $c->aborted()) {
515: my %dbfile;
516: if (tie(%dbfile,'GDBM_File',$diropendb,&GDBM_NEWDB(),0640)) {
517: while (my($key,$value) = each(%hash)) {
518: $dbfile{$key}=$value;
519: }
520: untie(%dbfile);
521: }
522: }
523:
524: return OK;
525: }
526:
527: # ----------------------------------------------- recursive scan of a directory
528: sub scanDir {
529: my ($r,$startdir,$indent,$hashref)=@_;
530: my $c = $r->connection();
531: my ($compuri,$curdir);
532: my $dirptr=16384;
533: $indent++;
534:
535: my %dupdirs = %dirs;
536: my @list=&get_list($r,$startdir);
537: foreach my $line (@list) {
538: return if ($c->aborted());
539: my ($strip,$dom,undef,$testdir,undef)=split(/\&/,$line,5);
540: next if $strip =~ /.*\.meta$/;
541: my (@fileparts) = split(/\./,$strip);
542: if ($hash{'display_attrs_9'} != 1) {
543: if (scalar(@fileparts) >= 3) {
544: my $fext = pop @fileparts;
545: my $ov = pop @fileparts;
546: my $fname = join ('.',@fileparts,$fext);
547: next if (grep /\Q$fname\E/,@list and $ov =~ /\d+/);
548: }
549: }
550:
551: if ($dom eq 'domain') {
552: $compuri = join('',$strip,'/'); # dom list has /res/<domain name>
553: $curdir = $compuri;
554: } else {
555: # user, dir & file have name only, i.e., w/o path
556: $compuri = join('',$startdir,$strip,'/');
557: $curdir = $startdir;
558: }
559: my $diropen = 'closed';
560: if (($dirptr&$testdir) or ($dom =~ /^(domain|user)$/)) {
561: while (my ($key,$val)= each %dupdirs) {
562: if ($key eq $compuri and $val eq "open") {
563: $diropen = "opened";
564: delete($dupdirs{$key});
565: delete($dirs{$key});
566: }
567: }
568: }
569: &display_line($r,$diropen,$line,$indent,$curdir,$hashref,@list);
570: &scanDir ($r,$compuri,$indent) if $diropen eq 'opened';
571: }
572: $indent--;
573: }
574:
575: # --------------- get complete matched list based on the uri (returns an array)
576: sub get_list {
577: my ($r,$uri)=@_;
578: my @list;
579: (my $luri = $uri) =~ s/\//_/g;
580:
581: if ($ENV{'form.attrs'} eq 'Refresh') {
582: foreach (keys %hash) {
583: delete $hash{$_} if ($_ =~ /^dirlist_files_/);
584: }
585: }
586:
587: if ($hash{'dirlist_files'.$luri}) {
588: @list = split(/\n/,$hash{'dirlist_files_'.$luri});
589: } else {
590: @list = &Apache::lonnet::dirlist($uri);
591: $hash{'dirlist_files_'.$luri} = join('\n',@list);
592: }
593: return @list=&match_ext($r,@list);
594: }
595:
596: sub initdebug {
597: return <<ENDJS;
598: <script>
599: var debugging = true;
600: if (debugging) {
601: var debuggingWindow = window.open('','Debug','width=400,height=300',true);
602: }
603:
604: function output(text) {
605: if (debugging) {
606: debuggingWindow.document.writeln(text);
607: }
608: }
609: output("<html><head><title>Debugging Window</title></head><body><pre>");
610: </script>
611: ENDJS
612: }
613:
614: sub writedebug {
615: my $text = shift;
616: return "<script>output('$text');</script>";
617: }
618:
619: # -------------------- filters out files based on extensions (returns an array)
620: sub match_ext {
621: my ($r,@packlist)=@_;
622: my @trimlist;
623: my $nextline;
624: my @fileext;
625: my $dirptr=16384;
626:
627: foreach my $line (@packlist) {
628: chomp $line;
629: $line =~ s/^\/home\/httpd\/html//;
630: my @unpackline = split (/\&/,$line);
631: next if ($unpackline[0] eq '.');
632: next if ($unpackline[0] eq '..');
633: my @filecom = split (/\./,$unpackline[0]);
634: my $fext = pop(@filecom);
635: my $fnptr = $unpackline[3]&$dirptr;
636: if ($fnptr == 0 and $unpackline[3] ne "") {
637: my $embstyle = &Apache::loncommon::fileembstyle($fext);
638: push @trimlist,$line if (defined($embstyle) &&
639: ($embstyle ne 'hdn' or $fext eq 'meta'));
640: } else {
641: push @trimlist,$line;
642: }
643: }
644: @trimlist = sort (@trimlist);
645: return @trimlist;
646: }
647:
648: # ------------------------------- displays one line in appropriate table format
649: sub display_line {
650: my ($r,$diropen,$line,$indent,$startdir,$hashref,@list)=@_;
651: my (@pathfn, $fndir);
652: my $dirptr=16384;
653: my $fileclr="#ffffe6";
654: my $iconpath= $r->dir_config('lonIconsURL') . '/';
655:
656: my @filecom = split (/\&/,$line);
657: my @pathcom = split (/\//,$filecom[0]);
658: my $listname = $pathcom[scalar(@pathcom)-1];
659: my $fnptr = $filecom[3]&$dirptr;
660: my $msg = 'View '.$filecom[0].' resources';
661: $msg = 'Close '.$filecom[0].' directory' if $diropen eq 'opened';
662:
663: my $tabtag='</td>';
664: my $i=0;
665:
666: while ($i<=8) {
667: $tabtag=join('',$tabtag,"<td> </td>")
668: if $hash{'display_attrs_'.$i} == 1;
669: $i++;
670: }
671:
672: my $valign = ($hash{'display_attrs_7'} == 1 ? 'top' : 'bottom');
673:
674: # display uplink arrow
675: if ($filecom[1] eq 'viewOneUp') {
676: $r->print("<tr valign='$valign' bgcolor=$fileclr>$extrafield");
677: $r->print("<td>\n");
678: $r->print ('<form method="post" name="dirpathUP" action="'.$startdir.
679: '/" '.
680: 'onSubmit="return rep_dirpath(\'UP\','.
681: 'document.forms.fileattr.acts.value)" '.
682: 'enctype="application/x-www-form-urlencoded"'.
683: '>'."\n");
684: $r->print ('<input type=hidden name=openuri value="'.
685: $startdir.'">'."\n");
686: $r->print ('<input type="hidden" name="acts" value="">'."\n");
687: $r->print ('<input src="'.$iconpath.'arrow_up.gif"');
688: $r->print (' name="'.$msg.'" height="22" type="image" border="0">'.
689: "\n");
690: $r->print("Up $tabtag</tr></form>\n");
691: return OK;
692: }
693: # Do we have permission to look at this?
694:
695: return OK if (!&Apache::lonnet::allowed('bre',$startdir.$filecom[0]));
696:
697: # display domain
698: if ($filecom[1] eq 'domain') {
699: $r->print ('<input type="hidden" name="dirPointer" value="on">'."\n")
700: if ($ENV{'form.dirPointer'} eq "on");
701: $r->print("<tr valign='$valign' bgcolor=$fileclr>$extrafield");
702: $r->print("<td>");
703: &begin_form ($r,$filecom[0].'/');
704: my $anchor = $filecom[0].'/';
705: $anchor =~ s/\///g;
706: $r->print ('<a name="'.$anchor.'">');
707: $r->print ('<input type="hidden" name="acts" value="">');
708: $r->print ('<input src="'.$iconpath.'folder_pointer_'.
709: $diropen.'.gif"');
710: $r->print (' name="'.$msg.'" height="22" type="image" border="0">'.
711: "\n");
712: $r->print ('<a href="javascript:gothere(\''.$filecom[0].
713: '/\')"><img src="'.$iconpath.'server.gif"');
714: $r->print (' border="0" /></a>'."\n");
715: $r->print ("Domain - $listname ");
716: if ($Apache::lonnet::domaindescription{$listname}) {
717: $r->print("(".$Apache::lonnet::domaindescription{$listname}.
718: ")");
719: }
720: $r->print (" $tabtag</tr></form>\n");
721: return OK;
722:
723: # display user directory
724: }
725: if ($filecom[1] eq 'user') {
726: $r->print("<tr valign=$valign bgcolor=$fileclr>$extrafield");
727: $r->print("<td nowrap>\n");
728: my $curdir = $startdir.$filecom[0].'/';
729: my $anchor = $curdir;
730: $anchor =~ s/\///g;
731: &begin_form ($r,$curdir);
732: $r->print ('<a name="'.$anchor.'"><img src="'.$iconpath.
733: 'whitespace1.gif" border="0" />'."\n");
734: $r->print ('<input type="hidden" name="acts" value="">');
735: $r->print ('<input src="'.$iconpath.'folder_pointer_'.$diropen.
736: '.gif"');
737: $r->print (' name="'.$msg.'" height="22" type="image" border="0">'.
738: "\n");
739: $r->print ('<a href="javascript:gothere(\''.$curdir.'\')"><img src='.
740: $iconpath.'quill.gif border="0" name="'.$msg.
741: '" height="22" /></a>');
742: my $domain=(split(m|/|,$startdir))[2];
743: my $plainname=&Apache::loncommon::plainname($listname,$domain);
744: $r->print ($listname);
745: if (defined($plainname) && $plainname) { $r->print(" ($plainname) "); }
746: $r->print ($tabtag.'</tr></form>'."\n");
747: return OK;
748: }
749:
750: # display file
751: if ($fnptr == 0 and $filecom[3] ne '') {
752: my $filelink = $startdir.$filecom[0];
753: my @file_ext = split (/\./,$listname);
754: my $curfext = $file_ext[-1];
755: if (@Omit) {
756: foreach (@Omit) { return OK if ($curfext eq $_); }
757: }
758: if (@Only) {
759: my $skip = 1;
760: foreach (@Only) { $skip = 0 if ($curfext eq $_); }
761: return OK if ($skip > 0);
762: }
763: # Set the icon for the file
764: my $iconname = "unknown.gif";
765: my $embstyle = &Apache::loncommon::fileembstyle($curfext);
766: # The unless conditional that follows is a bit of overkill
767: $iconname = $curfext.".gif" unless
768: (!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn');
769: #
770: $r->print("<tr valign='$valign' bgcolor=$fileclr><td nowrap>");
771: my $metafile = grep /^\Q$filecom[0]\E\.meta\&/, @list;
772: my $title;
773: if ($ENV{'form.catalogmode'} eq 'interactive') {
774: $title=$listname;
775: $title = &Apache::lonnet::metadata($filelink,'title')
776: if ($metafile == 1);
777: $title=$listname unless $title;
778: my $titleesc=HTML::Entities::encode($title);
779: $titleesc=~s/\'/\\'/; #' (clean up this spare quote)
780: $r->print("<a href=\"javascript:select_data(\'",
781: $titleesc,"','",$filelink,"')\">");
782: $r->print("<img src='",$iconpath,"select.gif' border='0' /></a>".
783: "\n");
784: $r->print("</td><td nowrap>");
785: }
786: elsif ($ENV{'form.catalogmode'} eq 'groupimport') {
787: $title=$listname;
788: $title = &Apache::lonnet::metadata($filelink,'title')
789: if ($metafile == 1);
790: $title=$listname unless $title;
791: my $titleesc=&HTML::Entities::encode($title);
792: $r->print("<form name='form$fnum'>\n");
793: $r->print("<input type='checkbox' name='filelink"."' ".
794: "value='$filelink' onClick='".
795: "javascript:queue(\"form$fnum\")' ");
796: if ($hash{'store_'.$filelink}) {
797: $r->print("checked");
798: }
799: $r->print(">\n");
800: $r->print("<input type='hidden' name='title"."' ".
801: "value='$titleesc'>\n");
802: $r->print("</form>\n");
803: $r->print("</td><td nowrap>");
804: $hash{"pre_${fnum}_link"}=$filelink;
805: $hash{"pre_${fnum}_title"}=$titleesc;
806: $fnum++;
807: }
808:
809: if ($indent > 0 and $indent < 11) {
810: $r->print("<img src=",$iconpath,"whitespace",$indent,
811: ".gif border='0' />\n");
812: } elsif ($indent >0) {
813: my $ten = int($indent/10.);
814: my $rem = $indent%10.0;
815: my $count = 0;
816: while ($count < $ten) {
817: $r->print("<img src=",$iconpath,
818: "whitespace10.gif border='0' />\n");
819: $count++;
820: }
821: $r->print("<img src=",$iconpath,"whitespace",$rem,
822: ".gif border='0' />\n") if $rem > 0;
823: }
824:
825: $r->print("<img src=$iconpath$iconname border='0' />\n");
826: $r->print (" <a href=\"javascript:openWindow('".$filelink.
827: "', 'previewfile', '450', '500', 'no', 'yes','yes')\";".
828: " TARGET=_self>$listname</a> ");
829:
830: $r->print (" (<a href=\"javascript:openWindow('".$filelink.
831: ".meta', 'metadatafile', '400', '450', 'no', 'yes','no')\"; ".
832: "TARGET=_self>metadata</a>) ") if ($metafile == 1);
833:
834: $r->print("</td>\n");
835: if ($hash{'display_attrs_0'} == 1) {
836: my $title = &Apache::lonnet::gettitle($filelink,'title')
837: if ($metafile == 1);
838: $r->print('<td> '.($title eq '' ? ' ' : $title).
839: ' </td>'."\n");
840: }
841: $r->print('<td align=right> ',
842: $filecom[8]," </td>\n")
843: if $hash{'display_attrs_1'} == 1;
844: $r->print('<td> '.
845: (localtime($filecom[9]))." </td>\n")
846: if $hash{'display_attrs_2'} == 1;
847: $r->print('<td> '.
848: (localtime($filecom[10]))." </td>\n")
849: if $hash{'display_attrs_3'} == 1;
850:
851: if ($hash{'display_attrs_4'} == 1) {
852: my $author = &Apache::lonnet::metadata($filelink,'author')
853: if ($metafile == 1);
854: $r->print('<td> '.($author eq '' ? ' ' : $author).
855: " </td>\n");
856: }
857: if ($hash{'display_attrs_5'} == 1) {
858: my $keywords = &Apache::lonnet::metadata($filelink,'keywords')
859: if ($metafile == 1);
860: # $keywords = ' ' if (!$keywords);
861: $r->print('<td> '.($keywords eq '' ? ' ' : $keywords).
862: " </td>\n");
863: }
864: if ($hash{'display_attrs_6'} == 1) {
865: my $lang = &Apache::lonnet::metadata($filelink,'language')
866: if ($metafile == 1);
867: $lang = &Apache::loncommon::languagedescription($lang);
868: $r->print('<td> '.($lang eq '' ? ' ' : $lang).
869: " </td>\n");
870: }
871: if ($hash{'display_attrs_7'} == 1) {
872: my $output='';
873: my $embstyle=&Apache::loncommon::fileembstyle($curfext);
874: if ($embstyle eq 'ssi') {
875: $output=&Apache::lonnet::ssi_body($filelink);
876: $output='<font size="-2">'.$output.'</font>';
877: } elsif ($embstyle eq 'img') {
878: $output='<img src="'.$filelink.'" />';
879: } elsif ($filelink=~/^\/res\/(\w+)\/(\w+)\//) {
880: $output='<img src="http://'.
881: $Apache::lonnet::hostname{&Apache::lonnet::homeserver($2,$1)}.
882: '/cgi-bin/thumbnail.gif?url='.$filelink.'" />';
883: }
884: $r->print('<td> '.($output eq '' ? ' ':$output).
885: " </td>\n");
886: }
887: if ($hash{'display_attrs_8'} == 1) {
888: my (%stat) = &Apache::lonmeta::dynamicmeta($filelink) if ($metafile == 1);
889: my $stat = ($stat{'course'} eq '' ? '' : $stat{'course'}).
890: (($stat{'course'} ne '' || $stat{'count'} ne '') ? '/' : '').
891: ($stat{'count'} eq '' ? '' : $stat{'count'});
892: $r->print('<td align=center> '.($stat eq '' ? ' ' : $stat).
893: ' </td>'."\n");
894: }
895:
896: $r->print("</tr>\n");
897: }
898:
899: # -- display directory
900: if ($fnptr == $dirptr) {
901: my @file_ext = split (/\./,$listname);
902: my $curfext = $file_ext[scalar(@file_ext)-1];
903: my $curdir = $startdir.$filecom[0].'/';
904: my $anchor = $curdir;
905: $anchor =~ s/\///g;
906: $r->print("<tr bgcolor=$fileclr>$extrafield<td valign=$valign>");
907: &begin_form ($r,$curdir);
908: my $indentm1 = $indent-1;
909: if ($indentm1 < 11 and $indentm1 > 0) {
910: $r->print("<img src=",$iconpath,"whitespace",$indentm1,
911: ".gif border='0' />\n");
912: } else {
913: my $ten = int($indentm1/10.);
914: my $rem = $indentm1%10.0;
915: my $count = 0;
916: while ($count < $ten) {
917: $r->print ("<img src=",$iconpath
918: ,"whitespace10.gif border='0' />\n");
919: $count++;
920: }
921: $r->print ("<img src=",$iconpath,"whitespace",$rem,
922: ".gif border='0' />\n") if $rem > 0;
923: }
924: $r->print ('<input type="hidden" name="acts" value="">');
925: $r->print ('<a name="'.$anchor.'"><input src="'.$iconpath.
926: 'folder_pointer_'.$diropen.'.gif"');
927: $r->print (' name="'.$msg.'" height="22" type="image" border="0">'.
928: "\n");
929: $r->print ('<a href="javascript:gothere(\''.$curdir.'\')"><img src="'.
930: $iconpath.'folder_'.$diropen.'.gif" border="0" /></a>'.
931: "\n");
932: $r->print ("$listname$tabtag</tr></form>\n");
933: }
934:
935: }
936:
937: # ------------------- prints the beginning of a form for directory or file link
938: sub begin_form {
939: my ($r,$uri) = @_;
940: my $anchor = $uri;
941: $anchor =~ s/\///g;
942: $r->print ('<form method="post" name="dirpath'.$dnum.'" action="'.$uri.
943: '#'.$anchor.
944: '" onSubmit="return rep_dirpath(\''.$dnum.'\''.
945: ',document.forms.fileattr.acts.value)" '.
946: 'enctype="application/x-www-form-urlencoded">'."\n");
947: $r->print ('<input type="hidden" name="openuri" value="'.$uri.'">'.
948: "\n");
949: $r->print ('<input type="hidden" name="dirPointer" value="on">'."\n");
950: $dnum++;
951: }
952:
953: # --------- settings whenever the user causes the indexer window to be launched
954: sub start_fresh_session {
955: delete $hash{'form.catalogmode'};
956: delete $hash{'form.mode'};
957: delete $hash{'form.form'};
958: delete $hash{'form.element'};
959: delete $hash{'form.omit'};
960: delete $hash{'form.only'};
961: foreach (keys %hash) {
962: delete $hash{$_} if (/^(pre_|store)/);
963: }
964: }
965:
966: # ------------------------------------------------------------------- setvalues
967: sub setvalues {
968: # setvalues is used in registerurl to synchronize the database
969: # hash and environment hashes
970: my ($H1,$h1key,$H2,$h2key) =@_;
971: #
972: if (exists $H2->{$h2key}) {
973: $H1->{$h1key} = $H2->{$h2key};
974: } elsif (exists $H1->{$h1key}) {
975: $H2->{$h2key} = $H1->{$h1key};
976: }
977: }
978:
979: 1;
980:
981: sub cleanup {
982: if (tied(%hash)){
983: &Apache::lonnet::logthis('Cleanup indexer: hash');
984: }
985: }
986:
987: =head1 NAME
988:
989: Apache::lonindexer - mod_perl module for cross server filesystem browsing
990:
991: =head1 SYNOPSIS
992:
993: Invoked by /etc/httpd/conf/srm.conf:
994:
995: <LocationMatch "^/res.*/$">
996: SetHandler perl-script
997: PerlHandler Apache::lonindexer
998: </LocationMatch>
999:
1000: =head1 INTRODUCTION
1001:
1002: This module enables a scheme of browsing across a cross server.
1003:
1004: This is part of the LearningOnline Network with CAPA project
1005: described at http://www.lon-capa.org.
1006:
1007: =head1 BEGIN SUBROUTINE
1008:
1009: This routine is only run once after compilation.
1010:
1011: =over 4
1012:
1013: =item *
1014:
1015: Initializes %language hash table.
1016:
1017: =back
1018:
1019: =head1 HANDLER SUBROUTINE
1020:
1021: This routine is called by Apache and mod_perl.
1022:
1023: =over 4
1024:
1025: =item *
1026:
1027: read in machine configuration variables
1028:
1029: =item *
1030:
1031: see if called from an interactive mode
1032:
1033: =item *
1034:
1035: refresh environment with user database values (in %hash)
1036:
1037: =item *
1038:
1039: define extra fields and buttons in case of special mode
1040:
1041: =item *
1042:
1043: set catalogmodefunctions to have extra needed javascript functionality
1044:
1045: =item *
1046:
1047: print header
1048:
1049: =item *
1050:
1051: evaluate actions from previous page (both cumulatively and chronologically)
1052:
1053: =item *
1054:
1055: output title
1056:
1057: =item *
1058:
1059: get state of file attributes to be showing
1060:
1061: =item *
1062:
1063: output state of file attributes to be showing
1064:
1065: =item *
1066:
1067: output starting row to the indexed file/directory hierarchy
1068:
1069: =item *
1070:
1071: read in what directories have previously been set to "open"
1072:
1073: =item *
1074:
1075: if not at top level, provide an uplink arrow
1076:
1077: =item *
1078:
1079: recursively go through all the directories and output as appropriate
1080:
1081: =item *
1082:
1083: information useful for group import
1084:
1085: =item *
1086:
1087: end the tables
1088:
1089: =item *
1090:
1091: end the output and return
1092:
1093: =back
1094:
1095: =head1 OTHER SUBROUTINES
1096:
1097: =over 4
1098:
1099: =item *
1100:
1101: scanDir - recursive scan of a directory
1102:
1103: =item *
1104:
1105: get_list - get complete matched list based on the uri (returns an array)
1106:
1107: =item *
1108:
1109: match_ext - filters out files based on extensions (returns an array)
1110:
1111: =item *
1112:
1113: display_line - displays one line in appropriate table format
1114:
1115: =item *
1116:
1117: begin_form - prints the beginning of a form for directory or file link
1118:
1119: =item *
1120:
1121: start_fresh_session - settings whenever the user causes the indexer window
1122: to be launched
1123:
1124: =back
1125:
1126: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>