Annotation of loncom/publisher/lonpubdir.pm, revision 1.55

1.1       www         1: # The LearningOnline Network with CAPA
1.32      www         2: # Construction Space Directory Lister
1.16      albertel    3: #
1.55    ! www         4: # $Id: lonpubdir.pm,v 1.54 2004/01/03 15:57:11 www Exp $
1.16      albertel    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.
                     14: #
                     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.1       www        27: #
1.17      harris41   28: ###
1.1       www        29: 
                     30: package Apache::lonpubdir;
                     31: 
                     32: use strict;
                     33: use Apache::File;
                     34: use File::Copy;
                     35: use Apache::Constants qw(:common :http :methods);
1.6       www        36: use Apache::loncacc;
1.17      harris41   37: use Apache::loncommon();
1.48      www        38: use Apache::lonhtmlcommon();
1.39      www        39: use Apache::lonlocal;
1.50      www        40: use Apache::lonmsg;
1.1       www        41: 
                     42: sub handler {
                     43: 
                     44:   my $r=shift;
                     45: 
                     46:   my $fn;
                     47: 
1.23      foxr       48: 
                     49: 
                     50:   $fn = getEffectiveUrl($r);
                     51: 
                     52:   # Validate access to the construction space and get username@domain.
1.6       www        53: 
                     54:   my $uname;
                     55:   my $udom;
                     56: 
1.9       www        57:   ($uname,$udom)=
1.6       www        58:     &Apache::loncacc::constructaccess(
1.9       www        59:              $fn,$r->dir_config('lonDefDomain')); 
                     60:   unless (($uname) && ($udom)) {
1.6       www        61:      $r->log_reason($uname.' at '.$udom.
1.32      www        62:          ' trying to list directory '.$ENV{'form.filename'}.
1.6       www        63:          ' ('.$fn.') - not authorized', 
                     64:          $r->filename); 
                     65:      return HTTP_NOT_ACCEPTABLE;
                     66:   }
1.23      foxr       67: 
1.32      www        68:   # Remove trailing / from directory name.
1.23      foxr       69: 
1.3       www        70:   $fn=~s/\/$//;
1.1       www        71: 
                     72:   unless ($fn) { 
                     73:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1.2       www        74:          ' trying to list empty directory', $r->filename); 
1.1       www        75:      return HTTP_NOT_FOUND;
                     76:   } 
                     77: 
                     78: # ----------------------------------------------------------- Start page output
                     79: 
1.23      foxr       80:   my $thisdisfn=$fn;
                     81:   $thisdisfn=~s/^\/home\/$uname\/public_html//;	# subdirectory part of
                     82:                                                 # construction space. 
                     83:   my $docroot=$r->dir_config('lonDocRoot');     # Apache  londocument root.
1.1       www        84: 
1.23      foxr       85:   my $resdir=$docroot.'/res/'.$udom.'/'.$uname.$thisdisfn; # Resource directory
                     86:   my $targetdir=$udom.'/'.$uname.$thisdisfn; # Publiction target directory.
1.25      www        87:   my $linkdir='/priv/'.$uname.$thisdisfn;      # Full URL name of constr space.
1.1       www        88: 
1.50      www        89:   my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
1.1       www        90: 
1.26      www        91:   &startpage($r, $uname, $udom, $thisdisfn);   # Put out the start of page.
1.6       www        92:   
1.50      www        93:   # Start off the directory table.
1.1       www        94: 
1.53      www        95:   $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
                     96: 	    '<tr><th>&nbsp;</th><th>'.&mt('Actions').'</th><th>'.&mt('Name').'</th><th>'.
1.39      www        97: 	    &mt('Title').'</th>'.
                     98: 	    '<th>'.&mt('Status').'</th><th>'.&mt('Last Modified').
                     99: 	    '</th></tr>');
1.1       www       100: 
1.2       www       101:   my $filename;
1.23      foxr      102:   my $dirptr=16384;		# Mask indicating a directory in stat.cmode.
1.1       www       103: 
1.2       www       104:   opendir(DIR,$fn);
1.44      albertel  105:   my @files=sort {uc($a) cmp uc($b)} (readdir(DIR));
1.11      albertel  106:   foreach my $filename (@files) {
1.2       www       107:      my ($cdev,$cino,$cmode,$cnlink,
                    108:          $cuid,$cgid,$crdev,$csize,
                    109:          $catime,$cmtime,$cctime,
                    110:          $cblksize,$cblocks)=stat($fn.'/'.$filename);
1.12      www       111: 
1.10      albertel  112:      my $extension='';
                    113:      if ($filename=~/\.(\w+)$/) { $extension=$1; }
1.15      matthew   114:      if ($cmode&$dirptr) {
1.50      www       115: 	 putdirectory($r, $thisdisfn, $linkdir, $filename, $cmtime,$targetdir,\%bombs);
1.17      harris41  116:      } elsif (&Apache::loncommon::fileembstyle($extension) ne 'hdn') {
1.22      foxr      117: 	 putresource($r, $uname, $filename, $thisdisfn, $resdir, 
1.50      www       118: 		     $targetdir, $linkdir, $cmtime,\%bombs);
1.14      albertel  119:      } else {
1.15      matthew   120: 	# "hidden" extension and not a directory, so hide it away.
1.2       www       121:      }
                    122:   }
                    123:   closedir(DIR);
                    124: 
                    125:   $r->print('</table></body></html>');
1.1       www       126:   return OK;  
                    127: }
1.21      foxr      128: #
1.23      foxr      129: #  Gets the effective URL of the request and returns it:
                    130: #    $effn = getEffectiveUrl($r);
                    131: #       $r  - The Apache Request object.
                    132: sub getEffectiveUrl {
                    133:     my $r = shift;
                    134:     my $fn;
                    135:     
                    136:     if ($ENV{'form.filename'}) {	# If a form filename is defined.
                    137: 	$fn=$ENV{'form.filename'};
                    138: 	#
                    139: 	#   Replace the ~username of the URL with /home/username/public_html
                    140: 	#   so that we don't have to worry about ~ expansion internally.
                    141: 	#
1.32      www       142: 	$fn=~s/^http\:\/\/[^\/]+\///;
                    143:         $fn=~s/^\///;
                    144:         $fn=~s/\~(\w+)/\/home\/$1\/public_html/;
1.23      foxr      145: 	
                    146: 	#  Remove trailing / strings (?) 
                    147: 	
                    148: 	$fn=~s/\/[^\/]+$//;
1.24      albertel  149:     } else {
                    150: 	#   If no form is defined, use request filename.
                    151: 	$fn = $r->filename();
                    152: 	my $lonDocRoot=$r->dir_config('lonDocRoot');
                    153: 	if ( $fn =~ /$lonDocRoot/ ) {
                    154: 	    #internal authentication, needs fixup.
                    155: 	    $fn = $r->uri(); # non users do not get the full path request
                    156:                              # through SCRIPT_FILENAME
                    157: 	    $fn=~s|^/~(\w+)|/home/$1/public_html|;
                    158: 	}
1.23      foxr      159:     }
1.37      www       160:     $fn=~s/\/+/\//g;
1.23      foxr      161:     return $fn;
                    162: }
                    163: #
                    164: #   Output the header of the page.  This includes:
                    165: #   - The HTML header 
                    166: #   - The H1/H3  stuff which includes the directory.
                    167: #
                    168: #     startpage($r, $uame, $udom, $thisdisfn);
                    169: #      $r     - The apache request object.
                    170: #      $uname - User name.
                    171: #      $udom  - Domain name the user is logged in under.
                    172: #      $thisdisfn - Displayable version of the filename.
1.26      www       173: 
1.23      foxr      174: sub startpage {
                    175:     my ($r, $uname, $udom, $thisdisfn) = @_;
                    176:     
1.39      www       177:     &Apache::loncommon::content_type($r,'text/html');
1.23      foxr      178:     $r->send_http_header;
                    179:     
                    180:     $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
                    181:     
1.26      www       182:     $r->print(&Apache::loncommon::bodytag(undef,undef,undef,1));
1.29      www       183:     my $pubdirscript=(<<ENDPUBDIRSCRIPT);
                    184: <script>
1.37      www       185: // Store directory location for menu bar to find
                    186: 
                    187: parent.lastknownpriv='/~$uname/$thisdisfn/';
                    188: 
                    189: // Confirmation dialogues
                    190: 
1.29      www       191:     function pubdir(theform) {
                    192: 	if (confirm('Publish complete directory?')) {
                    193: 	    theform.submit();
                    194:         }
                    195:     }
                    196:    function pubrecdir(theform) {
                    197: 	if (confirm('Publish directory and all subdirectories?')) {
                    198:             theform.pubrec.value='1';
                    199: 	    theform.submit();
                    200:         }
                    201:     }
                    202: </script>
                    203: ENDPUBDIRSCRIPT
                    204: 
1.48      www       205:     $r->print('<h2>'.&mt('Construction Space Directory').'</h2>'.
1.31      albertel  206: 	      '<script type="text/javascript">top.document.title = \''.
                    207: 	      $thisdisfn.'/ - LON-CAPA Construction Space\';</script>'.
                    208: 	      $pubdirscript.
1.26      www       209:               '<form method="post" action="/adm/publish" target="_parent">'.
1.35      www       210:               '<table><tr><td><input type="hidden" name="filename" value="/~'.
1.26      www       211:                $uname.$thisdisfn.'/" />'.
1.40      www       212:               '<input type="button" onClick="pubdir(this.form);" value="'.
                    213: &mt('Publish Directory').'" />'.
1.29      www       214:               '<input type="hidden" name="pubrec" value="" />'.
1.40      www       215:               '<input type="button" onClick="pubrecdir(this.form);" value="'.
                    216: &mt('Publish Directory and Sub Directories').'" /></td><td>'.
1.28      www       217: '<input type="button" onClick="window.location='."'/~".
1.40      www       218:                $uname.$thisdisfn."/default.meta'".'" value="'.
                    219: &mt('Edit Directory Catalog Information').'" /></td></tr><tr><td><input type="checkbox" name="forcerepub" /> '.&mt('Force publication of unmodified files').'.</td><td>&nbsp;</td></tr></table></form>');
1.23      foxr      220:     
                    221:     if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
1.39      www       222: 	$r->print('<h3>'.&mt('Co-Author').': '.$uname.' at '.$udom.
1.23      foxr      223: 		  '</h3>');
                    224:     }
1.48      www       225:     $r->print(
1.53      www       226:        &Apache::lonhtmlcommon::crumbs($uname.'/'.$thisdisfn.'/','_top','/priv').'<br />');
1.23      foxr      227: }
                    228: 
                    229: #
                    230: #   Get the title string or "[untitled]" if the file has no title metadata:
                    231: #   Without the latter substitution, it's impossible to examine metadata for
                    232: #   untitled resources.  Resources may be legitimately untitled, to prevent
                    233: #   searches from locating them.
                    234: #
                    235: #   $str = getTitleString($fullname);
                    236: #       $fullname - Fully qualified filename to check.
                    237: #
                    238: sub getTitleString {
                    239:     my $fullname = shift;
                    240:     my $title    = &Apache::lonnet::metadata($fullname, 'title');
                    241: 
                    242:     unless ($title) {
1.40      www       243: 	$title = "[".&mt('untitled')."]";
1.23      foxr      244:     }
                    245:     return $title;
                    246: }
                    247: 
1.55    ! www       248: sub getCopyRightString {
        !           249:     my $fullname = shift;
        !           250:     return &Apache::lonnet::metadata($fullname, 'copyright');
        !           251: }
1.23      foxr      252: #
1.21      foxr      253: #  Put out a directory table row:
                    254: #    putdirectory(r, base, here, dirname, modtime)
                    255: #      r       - Apache request object.
                    256: #      reqfile - File in request.
                    257: #      here    - Where we are in directory tree.
                    258: #      dirname - Name of directory special file.
                    259: #      modtime - Encoded modification time.
                    260: # 
                    261: sub putdirectory {
1.50      www       262:     my ($r, $reqfile, $here, $dirname, $modtime, $resdir, $bombs) = @_;
1.21      foxr      263:   
                    264:     # construct the display filename: the directory name unless ..:
                    265:     
                    266:     my $disfilename = $dirname;
                    267:     if ($dirname eq '..') {
1.39      www       268: 	$disfilename = '<i>'.&mt('Parent Directory').'</i>';
1.21      foxr      269:     }
                    270:     unless (( ($dirname eq '..') && ($reqfile eq '')) ||
                    271: 	    ($dirname eq '.')) {
1.50      www       272: 	my $kaputt=0;
                    273: 	foreach (keys %{$bombs}) {
                    274: 	    if ($_=~/^$resdir\/$disfilename\//) { $kaputt=1; last; }
                    275: 	}
1.52      www       276: 	%Apache::lonpublisher::metadatafields=();
                    277: 	%Apache::lonpublisher::metadatakeys=();
                    278: 	my $construct=$here;
                    279: 	$construct=~s/^\/priv\/(\w+)$/\/home\/$1\/public_html/;
                    280: 	&Apache::lonpublisher::metaeval(&Apache::lonnet::getfile(
                    281:        				 $construct.'/'.$dirname.'/default.meta'
                    282: 								 ));
1.25      www       283: 	$r->print('<tr bgcolor="#CCCCFF">'.
1.53      www       284: 		  '<td><img src="'.
                    285: 		  $Apache::lonnet::perlvar{'lonIconsURL'}.'/folder_closed.gif" /></td>'.
1.47      sakharuk  286: 		  '<td>'.&mt('Go to ...').'</td>'.
1.54      www       287: 		  '<td><font size="+1" face="arial"><a href="'.$here.'/'.$dirname.'/" target="_top">'.
                    288: 		  $disfilename.'</a></font></td>'.
1.53      www       289: 		        '<td colspan="2">'.($kaputt?'<img src="/adm/lonMisc/bomb.gif" />':'').$Apache::lonpublisher::metadatafields{'title'}.' <i>'.
                    290: 		  $Apache::lonpublisher::metadatafields{'subject'}.'</i> '.
1.52      www       291: 		  $Apache::lonpublisher::metadatafields{'keywords'}.'</td>'.
1.42      www       292: 		  '<td>'.&Apache::lonlocal::locallocaltime($modtime).'</td>'.
1.25      www       293: 		  "</tr>\n");
1.21      foxr      294:     }	
                    295:     return OK;
                    296: }
1.22      foxr      297: #
                    298: #   Put a table row for a file resource.
                    299: #
                    300: sub putresource {
                    301:     my ($r, $uname, $filename, $thisdisfn, 
                    302: 	$resdir, $targetdir, $linkdir,
1.50      www       303: 	$cmtime,$bombs) = @_;
1.22      foxr      304: 
1.47      sakharuk  305:     my $status=&mt('Unpublished');
1.53      www       306:     my $bgcolor='#FFAA99';
1.22      foxr      307:     my $title='&nbsp;';
                    308:     if (-e $resdir.'/'.$filename) {
                    309: 	my ($rdev,$rino,$rmode,$rnlink,
                    310: 	    $ruid,$rgid,$rrdev,$rsize,
                    311: 	    $ratime,$rmtime,$rctime,
                    312: 	    $rblksize,$rblocks)=stat($resdir.'/'.$filename);
                    313: 	if ($rmtime>=$cmtime) {
1.55    ! www       314: 	    $status=&mt('Published').'<br />'.
        !           315: 		&mt(&getCopyRightString($targetdir.'/'.$filename));
1.53      www       316:             $bgcolor='#CCFF88';
1.40      www       317: 	    if (&Apache::lonnet::metadata($targetdir.'/'.$filename,'obsolete')) {
1.41      www       318: 		$status=&mt('Obsolete');
1.40      www       319:                 $bgcolor='#AAAAAA';
                    320: 	    }
1.23      foxr      321: 	    $title='<a href="/res/'.$targetdir.'/'.$filename.
                    322: 		'.meta" target=cat>'.
1.55    ! www       323: 		&getTitleString($targetdir.'/'.$filename).'</a>';
1.22      foxr      324: 	} else {
1.55    ! www       325: 	    $status=&mt('Modified').'<br />'.
        !           326: 		&mt(&getCopyRightString($targetdir.'/'.$filename));
1.53      www       327:             $bgcolor='#FFFF77';
1.22      foxr      328: 	    $title='<a href="/res/'.$targetdir.'/'.$filename.'.meta" target=cat>'.
1.55    ! www       329: 		&getTitleString($targetdir.'/'.$filename).'</a>';
1.22      foxr      330: 	    if (&Apache::loncommon::fileembstyle(($filename=~/\.(\w+)$/)) eq 'ssi') {
                    331: 		$status.='<br><a href="/adm/diff?filename=/~'.$uname.
                    332: 		    $thisdisfn.'/'.$filename.
1.41      www       333: 		    '&versiontwo=priv" target=cat>'.&mt('Diffs').'</a>';
1.22      foxr      334: 	    }
1.51      www       335: 	}
                    336: 	$title.='<br /><a href="/~'.$uname.$thisdisfn.'/'.$filename.'.meta">'. 
1.53      www       337: 	    ($$bombs{$targetdir.'/'.$filename}?'<img src="/adm/lonMisc/bomb.gif" border="0" />':'Edit Metadata').'</a>';
1.22      foxr      338: 	$status.='<br><a href="/adm/retrieve?filename=/~'.$uname.
1.41      www       339: 	    $thisdisfn.'/'.$filename.'" target=cat>'.&mt('Retrieve').'</a>';
1.22      foxr      340:     }
1.33      www       341:     my $editlink='';
1.38      taceyjo1  342:     my $editlink2='';
1.36      www       343:     if ($filename=~/\.(xml|html|htm|xhtml|xhtm|sty)$/) {
1.54      www       344: 	$editlink=' <br />(<a href="'.$linkdir.'/'.$filename.'?forceedit=1" target="_top">'.&mt('Edit').'</a>)';
1.34      www       345:     }
                    346:     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library)$/) {
1.39      www       347: 	$editlink=' (<a href="'.$linkdir.'/'.$filename.'?forceedit=1" target="_top">'.&mt('EditXML').'</a>)';
1.54      www       348: 	$editlink2=' <br />(<a href="'.$linkdir.'/'.$filename.'?forceColoredit=1" target="_top">'.&mt('Edit').'</a>)';
1.43      taceyjo1  349:     }
                    350:     if ($filename=~/\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/) {
1.46      taceyjo1  351: 	$editlink=' (<a target="_parent" href="/adm/cfile?decompress=/~'.
                    352: 	      $uname.$thisdisfn.'/'.$filename.'">'.&mt('Decompress').'</a>)';
1.33      www       353:     }
1.25      www       354:     $r->print('<tr bgcolor="'.$bgcolor.'">'.
1.53      www       355: 	      '<td>'.($filename=~/[\#\~]$/?'&nbsp;':
                    356: 		      '<img src="'.&Apache::loncommon::icon($filename).'" /></td>').
1.22      foxr      357: 	      '<td><a target="_parent" href="/adm/publish?filename=/~'.
1.40      www       358: 	      $uname.$thisdisfn.'/'.$filename.'">'.&mt('Publish').'</a>'.
1.22      foxr      359: 	      '</td>'.
1.54      www       360: 	      '<td><font size="+1" face="arial">'.
1.25      www       361: 	      '<a href="'.$linkdir.'/'.$filename.'" target="_top">'.
1.54      www       362:                $filename.'</a></font>'.$editlink2.$editlink.
1.22      foxr      363: 	      '</td>'.
                    364: 	      '<td>'.$title.'</td>'.
1.41      www       365: 	      '<td>'.$status.'</td>'.
1.42      www       366: 	      '<td>'.&Apache::lonlocal::locallocaltime($cmtime).'</td>'.
1.25      www       367: 	      "</tr>\n");
1.22      foxr      368:     return OK;
1.23      foxr      369: }
                    370: #
                    371: #   Categorize files in the directory.
                    372: #   For each file in a list of files in a file directory, 
                    373: #   the  file categorized as one of:
                    374: #    - directory  
                    375: #    - sequence
                    376: #    - problem 
                    377: #    - Other resource.
                    378: #
                    379: #   For each file the modification date is determined as well.
                    380: #   Returned is a list of sublists:
                    381: #    (directories, sequences, problems, other)
                    382: #   each of the sublists contains entries of the following form (sorted by
                    383: #   filename):
                    384: #     (filename, typecode, lastmodtime)
                    385: #
                    386: #   $list = CategorizeFiles($location, $files)
                    387: #       $location   - Directory in which the files live (relative to our
                    388: #                     execution.
                    389: #       $files      - list of files.
                    390: #
                    391: sub CategorizeFiles {
                    392:     my $location = shift;
                    393:     my $files    = shift;
1.22      foxr      394: }
                    395: 
1.4       www       396: 1;
                    397: __END__
1.17      harris41  398: 
                    399: =head1 NAME
                    400: 
1.32      www       401: Apache::lonpubdir - Construction space directory lister
1.17      harris41  402: 
                    403: =head1 SYNOPSIS
                    404: 
                    405: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
                    406: 
1.18      harris41  407:  <LocationMatch "^/\~.*/$">
                    408:  PerlAccessHandler       Apache::loncacc
                    409:  SetHandler perl-script
                    410:  PerlHandler Apache::lonpubdir
                    411:  ErrorDocument     403 /adm/login
                    412:  ErrorDocument     404 /adm/notfound.html
                    413:  ErrorDocument     406 /adm/unauthorized.html
                    414:  ErrorDocument	  500 /adm/errorhandler
                    415:  </LocationMatch>
                    416: 
                    417:  <Location /adm/pubdir>
                    418:  PerlAccessHandler       Apache::lonacc
                    419:  SetHandler perl-script
                    420:  PerlHandler Apache::lonpubdir
                    421:  ErrorDocument     403 /adm/login
                    422:  ErrorDocument     404 /adm/notfound.html
                    423:  ErrorDocument     406 /adm/unauthorized.html
                    424:  ErrorDocument	  500 /adm/errorhandler
                    425:  </Location>
1.17      harris41  426: 
                    427: =head1 INTRODUCTION
                    428: 
1.18      harris41  429: This module publishes a directory of files.
1.17      harris41  430: 
                    431: This is part of the LearningOnline Network with CAPA project
                    432: described at http://www.lon-capa.org.
                    433: 
                    434: =head1 HANDLER SUBROUTINE
                    435: 
                    436: This routine is called by Apache and mod_perl.
                    437: 
                    438: =over 4
                    439: 
                    440: =item *
                    441: 
                    442: read in information
                    443: 
                    444: =item *
                    445: 
                    446: start page output
                    447: 
                    448: =item *
                    449: 
                    450: run through list of files and attempt to publish unhidden files
                    451: 
                    452: =back
                    453: 
                    454: =cut

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