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

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

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