Annotation of loncom/interface/lonindexer.pm, revision 1.30

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

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