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

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

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