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