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

1.1       www         1: # The LearningOnline Network with CAPA
1.24      harris41    2: # Directory Indexer
                      3: #
1.31    ! harris41    4: # $Id: lonindexer.pm,v 1.30 2001/12/13 13:59:22 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
1.1       www        80: 
1.23      harris41   81: # ----------------------------- Handling routine called via Apache and mod_perl
1.1       www        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;
1.9       harris41   87:     $fnum=0;
1.16      harris41   88:     $dnum=0;
1.22      harris41   89:     untie %hash;
1.17      harris41   90: 
1.23      harris41   91: # ------------------------------------- read in machine configuration variables
1.10      harris41   92:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
1.1       www        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: 
1.7       harris41  101:     my $fileclr='#ffffe6';
1.15      harris41  102:     my $line;
                    103:     my (@attrchk,@openpath);
                    104:     my $uri=$r->uri;
                    105: 
1.7       harris41  106: # -------------------------------------- see if called from an interactive mode
1.17      harris41  107:     &get_unprocessed_cgi();
1.7       harris41  108: 
1.17      harris41  109:     my $closebutton='';
1.7       harris41  110:     my $groupimportbutton='';
                    111:     my $colspan=''; 
1.14      harris41  112: 
                    113:     $extrafield='';
1.17      harris41  114:     my $diropendb = 
                    115: 	"/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_indexer.db";
1.15      harris41  116: 
                    117:     if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
                    118: 	if ($ENV{'form.launch'} eq '1') {
1.17      harris41  119: 	    &start_fresh_session();
1.15      harris41  120: 	}
1.17      harris41  121: 
                    122: # -------------------- refresh environment with user database values (in %hash)
1.15      harris41  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: 
1.17      harris41  130: # --------------------- define extra fields and buttons in case of special mode
1.15      harris41  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"'.
1.17      harris41  135: 		' border="0" /></td>';
1.15      harris41  136: 	    $colspan=" colspan='2' ";
                    137:             $closebutton=<<END;
1.7       harris41  138: <input type="button" name="close" value='CLOSE' onClick="self.close()">
                    139: END
1.16      harris41  140:         }
1.15      harris41  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"'.
1.17      harris41  145: 		' border="0" /></td>';
1.15      harris41  146: 	    $colspan=" colspan='2' ";
                    147:             $closebutton=<<END;
1.7       harris41  148: <input type="button" name="close" value='CLOSE' onClick="self.close()">
                    149: END
1.15      harris41  150:             $groupimportbutton=<<END;
1.17      harris41  151: <input type="button" name="groupimport" value='GROUP IMPORT'
                    152: onClick="javascript:select_group()">
1.7       harris41  153: END
1.15      harris41  154:         }
1.7       harris41  155: 
1.17      harris41  156: # ------ set catalogmodefunctions to have extra needed javascript functionality
1.15      harris41  157: 	my $catalogmodefunctions='';
                    158: 	if ($ENV{'form.catalogmode'} eq 'interactive' or
                    159: 	    $ENV{'form.catalogmode'} eq 'groupimport') {
                    160: 	    $catalogmodefunctions=<<END;
1.7       harris41  161: function select_data(title,url) {
                    162:     changeTitle(title);
                    163:     changeURL(url);
1.8       harris41  164:     self.close();
                    165: }
                    166: function select_group() {
1.20      harris41  167:     window.location="/adm/groupsort?catalogmode=groupimport&acts="+document.forms.fileattr.acts.value;
1.16      harris41  168: }
1.7       harris41  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
1.15      harris41  180:         }
1.16      harris41  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: 	}
1.17      harris41  201: 
1.1       www       202: # ---------------------------------------------------------------- Print Header
1.15      harris41  203: 	$r->print(<<ENDHEADER);
1.1       www       204: <html>
                    205: <head>
1.2       harris41  206: <title>The LearningOnline Network With CAPA Directory Browser</title>
1.3       harris41  207: 
1.19      harris41  208: <script type="text/javascript">
1.7       harris41  209: $catalogmodefunctions
1.2       harris41  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: }
1.16      harris41  217: function gothere(val) {
                    218:     window.location=val+'?acts='+document.forms.fileattr.acts.value;
                    219: }
1.14      harris41  220: </script>
1.3       harris41  221: 
1.1       www       222: </head>
                    223: <body bgcolor="#FFFFFF">
                    224: ENDHEADER
1.17      harris41  225: 
                    226: # - Evaluate actions from previous page (both cumulatively and chronologically)
1.16      harris41  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;
1.17      harris41  233: 	    # some initial hashes for working with data
1.27      harris41  234: 	    foreach (@Acts) {
1.16      harris41  235: 		my ($state,$ref)=split(/a/);
                    236: 		$ahash{$ref}=$state;
                    237: 		$achash{$ref}=$ac;
                    238: 		$ac++;
1.27      harris41  239: 	    }
1.17      harris41  240: 	    # sorting through the actions and changing the tied database hash
1.27      harris41  241: 	    foreach (sort {$achash{$a}<=>$achash{$b}} (keys %ahash)) {
1.16      harris41  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: 		}
1.27      harris41  255: 	    }
1.17      harris41  256: 	    # deleting the previously cached listing
1.27      harris41  257: 	    foreach (keys %hash) {
1.16      harris41  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: 		}
1.27      harris41  265: 	    }
1.16      harris41  266: 	}
                    267: 	
1.23      harris41  268: # ---------------------------------------------------------------- output title
1.17      harris41  269: 	$r->print('<h2><font color="#888888">The LearningOnline With CAPA '.
                    270: 		  'Network Directory Browser</font></h2>'."\n");
1.23      harris41  271: # ---------------------------------- get state of file attributes to be showing
1.6       harris41  272: 	if ($ENV{'form.attrs'} ne "") {
1.18      ng        273: 	    for (my $i=0; $i<=6; $i++) {
1.6       harris41  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 {
1.18      ng        281: 	    for (my $i=0; $i<=6; $i++) {
1.6       harris41  282: 		$attrchk[$i] = "checked" if $hash{'display_attrs_'.$i} == 1;
                    283: 	    }
                    284: 	}
1.23      harris41  285: # ------------------------------- output state of file attributes to be showing
1.15      harris41  286: 	$r->print(<<END);
1.17      harris41  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">
1.1       www       290: <table border=0><tr>
1.16      harris41  291: <td><input type="checkbox" name="attr0" value="1" $attrchk[0] /> Size</td>
1.18      ng        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>
1.1       www       295: </tr><tr>
1.16      harris41  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>
1.18      ng        299: <td>&nbsp;</td>
1.1       www       300: </tr></table>
1.16      harris41  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" />
1.7       harris41  305: $closebutton
                    306: $groupimportbutton
1.1       www       307: </form>
                    308: END
                    309: 
1.23      harris41  310: # ----------------- output starting row to the indexed file/directory hierarchy
1.15      harris41  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");
1.17      harris41  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);
1.15      harris41  327: 	$r->print("</tr>");
1.5       harris41  328: 
1.23      harris41  329: # ----------------- read in what directories have previously been set to "open"
1.27      harris41  330: 	foreach (keys %hash) {
1.4       harris41  331: 	    if ($_ =~ /^diropen_status_/) {
                    332: 		my $key = $_;
                    333: 		$key =~ s/^diropen_status_//;
                    334: 		$dirs{$key} = $hash{$_};
                    335: 	    }
1.27      harris41  336: 	}
1.4       harris41  337: 
1.2       harris41  338: 	if ($ENV{'form.openuri'}) {  # take care of review and refresh options
                    339: 	    my $uri=$ENV{'form.openuri'};
1.4       harris41  340: 	    if (exists($hash{'diropen_status_'.$uri})) {
                    341: 		my $cursta = $hash{'diropen_status_'.$uri};
1.2       harris41  342: 		$dirs{$uri} = 'open';
1.4       harris41  343: 		$hash{'diropen_status_'.$uri} = 'open';
                    344: 		if ($cursta eq 'open') {
                    345: 		    $dirs{$uri} = 'closed';
                    346: 		    $hash{'diropen_status_'.$uri} = 'closed';
                    347: 		}
1.2       harris41  348: 	    } else {
1.4       harris41  349: 		$hash{'diropen_status_'.$uri} = 'open';
1.2       harris41  350: 		$dirs{$uri} = 'open';
                    351: 	    }
                    352: 	}
1.12      ng        353: 	
                    354: 	my $bredir = $ENV{'form.dirPointer'};
                    355: 	my $toplevel;
1.13      ng        356: 	my $indent = 0;
1.12      ng        357: 	$uri = $uri.'/' if $uri !~ /.*\/$/;
1.17      harris41  358: 
1.13      ng        359: 	if ($bredir ne "on") {
1.12      ng        360: 	    $hash{'top.level'} = $uri;
                    361: 	    $toplevel = $uri;
1.13      ng        362: 
                    363: 	} else {
                    364: 	    $toplevel = $hash{'top.level'};
                    365: 	}
1.17      harris41  366: 
1.23      harris41  367: # -------------------------------- if not at top level, provide an uplink arrow
1.13      ng        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;
1.12      ng        375: 	}
1.17      harris41  376: 
1.23      harris41  377: # -------- recursively go through all the directories and output as appropriate
1.16      harris41  378: 	&scanDir ($r,$toplevel,$indent,\%hash);
1.12      ng        379: 	
1.23      harris41  380: # ---------------------------- embed hidden information useful for group import
1.8       harris41  381: 	$r->print("<form name='fnum'>");
                    382: 	$r->print("<input type='hidden' name='fnum' value='$fnum'></form>");
1.17      harris41  383: 
1.23      harris41  384: # -------------------------------------------------------------- end the tables
1.2       harris41  385: 	$r->print("</table>");
                    386: 	$r->print("</td></tr></table>");
1.17      harris41  387: 
1.23      harris41  388: # --------------------------------------------------- end the output and return
1.2       harris41  389: 	$r->print("</body></html>\n");
1.4       harris41  390: 	untie(%hash);
1.2       harris41  391:     } else {
1.17      harris41  392: 	$r->print('<html><head></head><body>Unable to tie hash to db '.
                    393: 		  'file</body></html>');
                    394: 	return OK;
1.2       harris41  395:     }
1.1       www       396:     return OK;
                    397: }
1.2       harris41  398: 
1.17      harris41  399: # ----------------------------------------------- recursive scan of a directory
1.2       harris41  400: sub scanDir {
1.16      harris41  401:     my ($r,$startdir,$indent,$hashref)=@_;
1.3       harris41  402:     my ($compuri,$curdir);
                    403:     my $dirptr=16384;
1.1       www       404:     $indent++;
                    405: 
1.2       harris41  406:     my %dupdirs = %dirs;
                    407:     my @list=&get_list($r,$startdir);
                    408:     foreach my $line (@list) {
1.5       harris41  409: 	my ($strip,$dom,$foo,$testdir,$foo)=split(/\&/,$line,5); 
1.4       harris41  410: 	next if $strip =~ /.*\.meta$/;
1.18      ng        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: 
1.5       harris41  421: 	if ($dom eq "domain") {
1.17      harris41  422: 	    $compuri = join('',$strip,"/");  # dom list has /res/<domain name>
1.3       harris41  423: 	    $curdir = $compuri;
1.2       harris41  424: 	} else {
1.17      harris41  425: 	    # user, dir & file have name only, i.e., w/o path
                    426: 	    $compuri = join('',$startdir,$strip,"/");
1.3       harris41  427: 	    $curdir = $startdir;
1.2       harris41  428: 	}
1.11      ng        429: 	my $diropen = "closed";
1.5       harris41  430: 	if (($dirptr&$testdir) or ($dom =~ /^(domain|user)$/)) {
1.3       harris41  431: 	    while (my ($key,$val)= each %dupdirs) {
1.5       harris41  432: 		if ($key eq $compuri and $val eq "open") {
1.11      ng        433: 		    $diropen = "opened";
1.5       harris41  434: 		    delete $dupdirs{key},$dirs{$key};
                    435: 		}
1.3       harris41  436: 	    }
1.1       www       437: 	}
1.16      harris41  438: 	&display_line($r,$diropen,$line,$indent,$curdir,$hashref,@list);
1.11      ng        439: 	&scanDir ($r,$compuri,$indent) if $diropen eq "opened";
1.1       www       440:     }
                    441:     $indent--;
                    442: }
                    443: 
1.17      harris41  444: # --------------- get complete matched list based on the uri (returns an array)
1.1       www       445: sub get_list {
                    446:     my ($r,$uri)=@_;
                    447:     my @list;
1.2       harris41  448:     my $luri = $uri;
                    449:     $luri =~ s/\//_/g;
                    450: 
1.6       harris41  451:     if ($ENV{'form.attrs'} eq "Refresh") {
1.27      harris41  452: 	foreach (keys %hash) {
1.4       harris41  453: 	    delete $hash{$_} if ($_ =~ /^dirlist_files_/);
1.27      harris41  454: 	    }
1.2       harris41  455:     }
                    456: 
1.4       harris41  457:     if ($hash{'dirlist_files'.$luri}) {
                    458: 	@list = split(/\n/,$hash{'dirlist_files_'.$luri});
1.1       www       459:     } else {
1.4       harris41  460: 	@list = &Apache::lonnet::dirlist($uri);
                    461: 	$hash{'dirlist_files_'.$luri} = join('\n',@list);
1.1       www       462:     }
                    463:     return @list=&match_ext($r,@list);
                    464: }
                    465: 
1.17      harris41  466: # -------------------- filters out files based on extensions (returns an array)
1.1       www       467: sub match_ext {
                    468:     my ($r,@packlist)=@_;
                    469:     my @trimlist;
                    470:     my $nextline;
                    471:     my @fileext;
                    472:     my $dirptr=16384;
                    473: 
1.2       harris41  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 "") {
1.28      harris41  484: 	    my $embstyle = &Apache::loncommon::fileembstyle($fext);
1.25      matthew   485:             push @trimlist,$line if (defined($embstyle) && 
                    486: 				     $embstyle ne 'hdn' );
1.1       www       487: 	} else {
1.2       harris41  488: 	    push @trimlist,$line;
1.1       www       489: 	}
                    490:     }
1.4       harris41  491:     @trimlist = sort (@trimlist);
1.1       www       492:     return @trimlist;
                    493: }
                    494: 
1.17      harris41  495: # ------------------------------- displays one line in appropriate table format
1.23      harris41  496: sub display_line {
1.16      harris41  497:     my ($r,$diropen,$line,$indent,$startdir,$hashref,@list)=@_;
1.1       www       498:     my (@pathfn, $fndir, $fnptr);
                    499:     my $dirptr=16384;
                    500:     my $fileclr="#ffffe6";
1.10      harris41  501:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
1.1       www       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;
1.4       harris41  507:     my $msg = 'View '.$filecom[0].' resources';
1.11      ng        508:     $msg = 'Close '.$filecom[0].' directory' if $diropen eq "opened";
1.1       www       509: 
                    510:     my $tabtag="</td>";
                    511:     my $i=0;
                    512: 
                    513:     while ($i<=5) {
1.17      harris41  514: 	$tabtag=join('',$tabtag,"<td bgcolor=",$fileclr,">&nbsp;</td>")
                    515: 	    if $hash{'display_attrs_'.$i} == 1;
1.1       www       516: 	$i++;
                    517:     }
1.17      harris41  518: 
                    519: # display uplink arrow
1.13      ng        520:     if ($filecom[1] eq "viewOneUp") {
                    521: 	$r->print("<tr>$extrafield");
                    522: 	$r->print("<td bgcolor=$fileclr valign=bottom>\n");
1.16      harris41  523: 	$r->print ('<form method="post" name="dirpathUP" action="'.$startdir.
                    524: 		   '/" '.
1.17      harris41  525: 		   'onSubmit="return rep_dirpath(\'UP\','.
                    526: 		   'document.forms.fileattr.acts.value)" '.
1.16      harris41  527: 		   'enctype="application/x-www-form-urlencoded"'.
                    528:                    '>'."\n");
1.17      harris41  529: 	$r->print ('<input type=hidden name=openuri value="'.
                    530: 		   $startdir.'">'."\n");
1.16      harris41  531: 	$r->print ('<input type="hidden" name="acts" value="">'."\n");
1.13      ng        532: 	$r->print ('<input src="'.$iconpath.'arrow_up.gif"');
1.17      harris41  533: 	$r->print (' name="'.$msg.'" height="22" type="image" border="0">'.
                    534: 		   "\n");
1.13      ng        535: 	$r->print("Up $tabtag</tr></form>\n");
                    536: 	return OK;
                    537:     }
1.17      harris41  538: 
                    539: # display domain
1.1       www       540:     if ($filecom[1] eq "domain") {
1.17      harris41  541: 	$r->print ('<input type="hidden" name="dirPointer" value="on">'."\n")
                    542: 	    if ($ENV{'form.dirPointer'} eq "on");
1.7       harris41  543: 	$r->print("<tr>$extrafield");
1.1       www       544: 	$r->print("<td bgcolor=$fileclr valign=bottom>");
1.2       harris41  545: 	&begin_form ($r,$filecom[0].'/');
1.3       harris41  546: 	my $anchor = $filecom[0].'/';
                    547: 	$anchor =~ s/\///g;
1.13      ng        548: 	$r->print ('<a name="'.$anchor.'">');
1.16      harris41  549: 	$r->print ('<input type="hidden" name="acts" value="">');
1.17      harris41  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");
1.1       www       557: 	$r->print("Domain - $listname $tabtag</tr></form>\n");
                    558: 	return OK;
1.17      harris41  559: 
                    560: # display user directory
1.1       www       561:     }
                    562:     if ($filecom[1] eq "user") {
1.7       harris41  563: 	$r->print("<tr>$extrafield");
1.13      ng        564: 	$r->print("<td bgcolor=$fileclr valign=bottom nowrap>\n");
1.2       harris41  565: 	my $curdir = $startdir.$filecom[0].'/';
1.3       harris41  566: 	my $anchor = $curdir;
                    567: 	$anchor =~ s/\///g;
1.13      ng        568: 	&begin_form ($r,$curdir);
1.17      harris41  569: 	$r->print ('<a name="'.$anchor.'"><img src="'.$iconpath.
                    570: 		   'whitespace1.gif" border="0" />'."\n");
1.16      harris41  571: 	$r->print ('<input type="hidden" name="acts" value="">');
1.17      harris41  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>');
1.13      ng        579: 	$r->print ($listname.$tabtag.'</tr></form>'."\n");
1.1       www       580: 	return OK;
                    581:     }
1.17      harris41  582: 
1.1       www       583: # display file
                    584:     if ($fnptr == 0 and $filecom[3] ne "") {
                    585: 	my @file_ext = split (/\./,$listname);
1.25      matthew   586: 	my $curfext = $file_ext[-1];
                    587: 	# Set the icon for the file
1.26      matthew   588: 	my $iconname = "unknown.gif";
1.28      harris41  589: 	my $embstyle = &Apache::loncommon::fileembstyle($curfext);
1.25      matthew   590: 	# The unless conditional that follows is a bit of overkill
                    591: 	$iconname = $curfext.".gif" unless
1.26      matthew   592: 	    (!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn');
1.25      matthew   593: 	#
1.2       harris41  594: 	my $filelink = $startdir.$filecom[0];
1.8       harris41  595: 	$r->print("<tr><td nowrap valign='bottom' bgcolor=$fileclr>");
1.7       harris41  596: 	my $metafile = grep /^$filecom[0]\.meta\&/, @list;
                    597: 	my $title;
                    598:         if ($ENV{'form.catalogmode'} eq 'interactive') {
                    599: 	    $title=$listname;
1.8       harris41  600: 	    $title = &Apache::lonnet::metadata($filelink,'title')
                    601: 		if ($metafile == 1);
1.7       harris41  602: 	    $title=$listname unless $title;
                    603: 	    $r->print("<a href='javascript:select_data(\"",
                    604: 	              $title,'","',$filelink,"\")'>");
1.17      harris41  605: 	    $r->print("<img src='",$iconpath,"select.gif' border='0' /></a>".
                    606: 		      "\n");
1.8       harris41  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"."' ".
1.16      harris41  616: 		      "value='$filelink' onClick='".
                    617: 		      "javascript:queue(\"form$fnum\")' ");
                    618: 	    if ($hash{'store_'.$filelink}) {
                    619: 		$r->print("checked");
                    620: 	    }
                    621: 	    $r->print(">\n");
1.8       harris41  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>");
1.16      harris41  626: 	    $hash{"pre_${fnum}_link"}=$filelink;
                    627: 	    $hash{"pre_${fnum}_title"}=$title;
1.8       harris41  628:   	    $fnum++;
1.7       harris41  629: 	}
1.4       harris41  630: 
1.12      ng        631: 	if ($indent > 0 and $indent < 11) {
1.17      harris41  632: 	    $r->print("<img src=",$iconpath,"whitespace",$indent,
                    633: 		      ".gif border='0' />\n");
1.11      ng        634: 	} elsif ($indent >0) {
1.4       harris41  635: 	    my $ten = int($indent/10.);
                    636: 	    my $rem = $indent%10.0;
                    637: 	    my $count = 0;
                    638: 	    while ($count < $ten) {
1.17      harris41  639: 		$r->print("<img src=",$iconpath,
                    640: 			  "whitespace10.gif border='0' />\n");
1.1       www       641: 	    $count++;
1.4       harris41  642: 	    }
1.17      harris41  643: 	    $r->print("<img src=",$iconpath,"whitespace",$rem,
                    644: 		      ".gif border='0' />\n") if $rem > 0;
1.1       www       645: 	}
1.4       harris41  646: 
1.25      matthew   647: 	$r->print("<img src=$iconpath$iconname border='0' />\n");
1.17      harris41  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);
1.2       harris41  655: 
1.7       harris41  656: 	$r->print("</td>\n");
1.17      harris41  657: 	$r->print("<td bgcolor=$fileclr align=right valign=bottom> ",
                    658: 		  $filecom[8]," </td>\n") 
1.11      ng        659: 	    if $hash{'display_attrs_0'} == 1;
1.17      harris41  660: 	$r->print("<td bgcolor=$fileclr valign=bottom> ".
                    661: 		  (localtime($filecom[9]))." </td>\n") 
1.11      ng        662: 	    if $hash{'display_attrs_1'} == 1;
1.17      harris41  663: 	$r->print("<td bgcolor=$fileclr valign=bottom> ".
                    664: 		  (localtime($filecom[10]))." </td>\n") 
1.11      ng        665: 	    if $hash{'display_attrs_2'} == 1;
1.2       harris41  666: 
1.6       harris41  667: 	if ($hash{'display_attrs_3'} == 1) {
1.17      harris41  668: 	    my $author = &Apache::lonnet::metadata($filelink,'author')
                    669: 		if ($metafile == 1);
1.2       harris41  670: 	    $author = '&nbsp;' if (!$author);
1.17      harris41  671: 	    $r->print("<td bgcolor=$fileclr valign=bottom> ".$author.
                    672: 		      " </td>\n");
1.2       harris41  673: 	}
1.6       harris41  674: 	if ($hash{'display_attrs_4'} == 1) {
1.17      harris41  675: 	    my $keywords = &Apache::lonnet::metadata($filelink,'keywords')
                    676: 		if ($metafile == 1);
1.2       harris41  677: 	    $keywords = '&nbsp;' if (!$keywords);
1.17      harris41  678: 	    $r->print("<td bgcolor=$fileclr valign=bottom> ".$keywords.
                    679: 		      " </td>\n");
1.2       harris41  680: 	}
1.6       harris41  681: 	if ($hash{'display_attrs_5'} == 1) {
1.17      harris41  682: 	    my $lang = &Apache::lonnet::metadata($filelink,'language')
                    683: 		if ($metafile == 1);
1.28      harris41  684: 	    $lang = &Apache::loncommon::languagedescription($lang);
1.2       harris41  685: 	    $lang = '&nbsp;' if (!$lang);
1.17      harris41  686: 	    $r->print("<td bgcolor=$fileclr valign=bottom> ".$lang.
                    687: 		      " </td>\n");
1.2       harris41  688: 	}
1.1       www       689: 	$r->print("</tr>\n");
                    690:     }
1.17      harris41  691: 
1.2       harris41  692: # -- display directory
1.1       www       693:     if ($fnptr == $dirptr) {
                    694: 	my @file_ext = split (/\./,$listname);
                    695: 	my $curfext = $file_ext[scalar(@file_ext)-1];
1.2       harris41  696: 	my $curdir = $startdir.$filecom[0].'/';
1.3       harris41  697: 	my $anchor = $curdir;
                    698: 	$anchor =~ s/\///g;
1.7       harris41  699: 	$r->print("<tr>$extrafield<td bgcolor=$fileclr valign=bottom>");
1.2       harris41  700: 	&begin_form ($r,$curdir);
1.4       harris41  701: 	my $indentm1 = $indent-1;
1.11      ng        702: 	if ($indentm1 < 11 and $indentm1 > 0) {
1.17      harris41  703: 	    $r->print("<img src=",$iconpath,"whitespace",$indentm1,
                    704: 		      ".gif border='0' />\n");
1.4       harris41  705: 	} else {
                    706: 	    my $ten = int($indentm1/10.);
                    707: 	    my $rem = $indentm1%10.0;
                    708: 	    my $count = 0;
                    709: 	    while ($count < $ten) {
1.17      harris41  710: 		$r->print ("<img src=",$iconpath
                    711: 			   ,"whitespace10.gif border='0' />\n");
1.12      ng        712: 		$count++;
1.4       harris41  713: 	    }
1.17      harris41  714: 	    $r->print ("<img src=",$iconpath,"whitespace",$rem,
                    715: 		       ".gif border='0' />\n") if $rem > 0;
1.1       www       716: 	}
1.16      harris41  717: 	$r->print ('<input type="hidden" name="acts" value="">');
1.17      harris41  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");
1.13      ng        725: 	$r->print ("$listname$tabtag</tr></form>\n");
1.1       www       726:     }
1.2       harris41  727: 
1.1       www       728: }
                    729: 
1.14      harris41  730: # ------------------- prints the beginning of a form for directory or file link
1.1       www       731: sub begin_form {
                    732:     my ($r,$uri) = @_;
1.3       harris41  733:     my $anchor = $uri;
                    734:     $anchor =~ s/\///g;
1.17      harris41  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)" '.
1.16      harris41  739: 	       'enctype="application/x-www-form-urlencoded">'."\n");
1.17      harris41  740:     $r->print ('<input type="hidden" name="openuri" value="'.$uri.'">'.
                    741: 	       "\n");
                    742:     $r->print ('<input type="hidden" name="dirPointer" value="on">'."\n");
1.16      harris41  743:     $dnum++;
1.17      harris41  744: }
                    745: 
                    746: # ----------- grab unprocessed CGI variables that may have been appended to URL
                    747: sub get_unprocessed_cgi {
1.27      harris41  748:     foreach (split(/&/,$ENV{'QUERY_STRING'})) {
1.17      harris41  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:        }
1.27      harris41  755:     }
1.17      harris41  756: }
                    757: 
                    758: # --------- settings whenever the user causes the indexer window to be launched
                    759: sub start_fresh_session {
                    760:     delete $hash{'mode_catalog'};
1.27      harris41  761:     foreach (keys %hash) {
1.17      harris41  762: 	if ($_ =~ /^pre_/) {
                    763: 	    delete $hash{$_};
                    764: 	}
                    765: 	if ($_ =~ /^store/) {
                    766: 	    delete $hash{$_};
                    767: 	}
1.27      harris41  768:     }
1.1       www       769: }
                    770: 
                    771: 1;
1.23      harris41  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>