File:  [LON-CAPA] / loncom / interface / lonindexer.pm
Revision 1.44: download - view: text, annotated - select for diffs
Thu Jun 20 21:22:20 2002 UTC (21 years, 11 months ago) by ng
Branches: MAIN
CVS tags: version_0_4, stable_2002_july, STABLE, HEAD
Change outline color for table listing of resources.

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

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