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