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