File:  [LON-CAPA] / loncom / interface / lonindexer.pm
Revision 1.31: download - view: text, annotated - select for diffs
Sun Jan 6 01:29:52 2002 UTC (22 years, 5 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
sub BEGIN should be BEGIN

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

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