File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.29: download - view: text, annotated - select for diffs
Mon Nov 18 15:26:10 2002 UTC (21 years, 7 months ago) by www
Branches: MAIN
CVS tags: version_0_6_2, version_0_6, HEAD
Bug #952 - shows "Confirm" dialog before publishing directories.

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

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