File:  [LON-CAPA] / loncom / interface / lonindexer.pm
Revision 1.38: download - view: text, annotated - select for diffs
Thu Mar 14 21:37:04 2002 UTC (22 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Fix javascript error, bug #174

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

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