File:  [LON-CAPA] / loncom / interface / lonindexer.pm
Revision 1.47: download - view: text, annotated - select for diffs
Tue Jul 30 19:47:13 2002 UTC (21 years, 10 months ago) by ng
Branches: MAIN
CVS tags: HEAD
add a filter so that files a user does not have permission to view are not
listed.

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>