File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.49: download - view: text, annotated - select for diffs
Mon Dec 29 19:01:27 2003 UTC (20 years, 4 months ago) by www
Branches: MAIN
CVS tags: HEAD
Correct language headers in loncfile
Improved filename crumbs
Better prettyprint

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

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