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

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

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