File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.22: download - view: text, annotated - select for diffs
Tue Feb 5 22:25:00 2002 UTC (22 years, 3 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Break out putresource as a sub for file resources.

    1: # The LearningOnline Network with CAPA
    2: # (Publication Handler
    3: #
    4: # $Id: lonpubdir.pm,v 1.22 2002/02/05 22:25:00 foxr 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:   if ($ENV{'form.filename'}) {
   60:       $fn=$ENV{'form.filename'};
   61:       $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
   62:       $fn=~s/\/[^\/]+$//;
   63:   } else {
   64:       $fn=$r->filename();
   65:   }
   66: 
   67:   my $uname;
   68:   my $udom;
   69: 
   70:   ($uname,$udom)=
   71:     &Apache::loncacc::constructaccess(
   72:              $fn,$r->dir_config('lonDefDomain')); 
   73:   unless (($uname) && ($udom)) {
   74:      $r->log_reason($uname.' at '.$udom.
   75:          ' trying to publish file '.$ENV{'form.filename'}.
   76:          ' ('.$fn.') - not authorized', 
   77:          $r->filename); 
   78:      return HTTP_NOT_ACCEPTABLE;
   79:   }
   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: 
   92:   $r->content_type('text/html');
   93:   $r->send_http_header;
   94: 
   95:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
   96: 
   97:   $r->print(
   98:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
   99: 
  100:   my $thisdisfn=$fn;
  101:   $thisdisfn=~s/^\/home\/$uname\/public_html//;
  102:   
  103:   $r->print('<h1>Construction Space Directory <tt>'.$thisdisfn.'/</tt></h1>');
  104:   
  105:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
  106:           $r->print('<h3>Co-Author: '.$uname.' at '.$udom.
  107:                '</h3>');
  108:   }
  109: 
  110: 
  111:   my $docroot=$r->dir_config('lonDocRoot');
  112: 
  113:   my $resdir=$docroot.'/res/'.$udom.'/'.$uname.$thisdisfn;
  114:   my $targetdir=$udom.'/'.$uname.$thisdisfn;
  115:   my $linkdir='/~'.$uname.$thisdisfn;
  116: 
  117:   $r->print('<table border=2>'.
  118:  '<tr><th>Actions</th><th>Name</th><th>Title</th>'.
  119:  '<th>Status</th><th>Last Modified</th></tr>');
  120: 
  121:   my $filename;
  122:   my $dirptr=16384;
  123: 
  124:   opendir(DIR,$fn);
  125:   my @files=sort(readdir(DIR));
  126:   foreach my $filename (@files) {
  127:      my ($cdev,$cino,$cmode,$cnlink,
  128:          $cuid,$cgid,$crdev,$csize,
  129:          $catime,$cmtime,$cctime,
  130:          $cblksize,$cblocks)=stat($fn.'/'.$filename);
  131: 
  132:      my $extension='';
  133:      if ($filename=~/\.(\w+)$/) { $extension=$1; }
  134:      if ($cmode&$dirptr) {
  135: 	 putdirectory($r, $thisdisfn, $linkdir, $filename, $cmtime);
  136:      } elsif (&Apache::loncommon::fileembstyle($extension) ne 'hdn') {
  137: 	 putresource($r, $uname, $filename, $thisdisfn, $resdir, 
  138: 		     $targetdir, $linkdir, $cmtime);
  139:      } else {
  140: 	# "hidden" extension and not a directory, so hide it away.
  141:      }
  142:   }
  143:   closedir(DIR);
  144: 
  145:   $r->print('</table></body></html>');
  146:   return OK;  
  147: }
  148: #
  149: #  Put out a directory table row:
  150: #    putdirectory(r, base, here, dirname, modtime)
  151: #      r       - Apache request object.
  152: #      reqfile - File in request.
  153: #      here    - Where we are in directory tree.
  154: #      dirname - Name of directory special file.
  155: #      modtime - Encoded modification time.
  156: # 
  157: sub putdirectory {
  158:     my ($r, $reqfile, $here, $dirname, $modtime) = @_;
  159:   
  160:     # construct the display filename: the directory name unless ..:
  161:     
  162:     my $disfilename = $dirname;
  163:     if ($dirname eq '..') {
  164: 	$disfilename = '<i>Parent Directory</i>';
  165:     }
  166:     unless (( ($dirname eq '..') && ($reqfile eq '')) ||
  167: 	    ($dirname eq '.')) {
  168: 	$r->print('<tr>'.
  169: 		  '<td>Click to cwd</td>'.
  170: 		  '<td><a href="'.$here.'/'.$dirname.'/">'.
  171: 		  $disfilename.'</a></td>'.
  172: 		        '<td>&nbsp;</td>'.
  173: 		  '<td>&nbsp;</td>'.
  174: 		  '<td>'.localtime($modtime).'</td>'.
  175: 		  '</tr>');
  176:     }	
  177:     return OK;
  178: }
  179: #
  180: #   Put a table row for a file resource.
  181: #
  182: sub putresource {
  183:     my ($r, $uname, $filename, $thisdisfn, 
  184: 	$resdir, $targetdir, $linkdir,
  185: 	$cmtime) = @_;
  186: 
  187:     my $status='Unpublished';
  188:     my $title='&nbsp;';
  189:     if (-e $resdir.'/'.$filename) {
  190: 	my ($rdev,$rino,$rmode,$rnlink,
  191: 	    $ruid,$rgid,$rrdev,$rsize,
  192: 	    $ratime,$rmtime,$rctime,
  193: 	    $rblksize,$rblocks)=stat($resdir.'/'.$filename);
  194: 	if ($rmtime>=$cmtime) {
  195: 	    $status='Published';
  196: 	    $title='<a href="/res/'.$targetdir.'/'.$filename.'.meta" target=cat>'.
  197: 		&Apache::lonnet::metadata($targetdir.'/'.$filename,'title').'</a>';
  198: 	} else {
  199: 	    $status='Modified';
  200: 	    $title='<a href="/res/'.$targetdir.'/'.$filename.'.meta" target=cat>'.
  201: 		&Apache::lonnet::metadata($targetdir.'/'.$filename,'title').'</a>';
  202: 	    if (&Apache::loncommon::fileembstyle(($filename=~/\.(\w+)$/)) eq 'ssi') {
  203: 		$status.='<br><a href="/adm/diff?filename=/~'.$uname.
  204: 		    $thisdisfn.'/'.$filename.
  205: 		    '&versionone=priv" target=cat>Diffs</a>';
  206: 	    }
  207: 	}   
  208: 	$status.='<br><a href="/adm/retrieve?filename=/~'.$uname.
  209: 	    $thisdisfn.'/'.$filename.'" target=cat>Retrieve</a>';
  210:     }
  211:     $r->print('<tr>'.
  212: 	      '<td><a target="_parent" href="/adm/publish?filename=/~'.
  213: 	      $uname.$thisdisfn.'/'.$filename.'">'.'Publish</a>'.
  214: 	      '</td>'.
  215: 	      '<td>'.
  216: 	      '<a href="'.$linkdir.'/'.$filename.'">'.$filename.'</a>'.
  217: 	      '</td>'.
  218: 	      '<td>'.$title.'</td>'.
  219: 	      '<td>'.$status.'</td>'.
  220: 	      '<td>'.localtime($cmtime).'</td>'.
  221: 	      '</tr>');
  222:     return OK;
  223: }
  224: 
  225: 1;
  226: __END__
  227: 
  228: =head1 NAME
  229: 
  230: Apache::lonpubdir - Publication Handler for Directories
  231: 
  232: =head1 SYNOPSIS
  233: 
  234: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
  235: 
  236:  <LocationMatch "^/\~.*/$">
  237:  PerlAccessHandler       Apache::loncacc
  238:  SetHandler perl-script
  239:  PerlHandler Apache::lonpubdir
  240:  ErrorDocument     403 /adm/login
  241:  ErrorDocument     404 /adm/notfound.html
  242:  ErrorDocument     406 /adm/unauthorized.html
  243:  ErrorDocument	  500 /adm/errorhandler
  244:  </LocationMatch>
  245: 
  246:  <Location /adm/pubdir>
  247:  PerlAccessHandler       Apache::lonacc
  248:  SetHandler perl-script
  249:  PerlHandler Apache::lonpubdir
  250:  ErrorDocument     403 /adm/login
  251:  ErrorDocument     404 /adm/notfound.html
  252:  ErrorDocument     406 /adm/unauthorized.html
  253:  ErrorDocument	  500 /adm/errorhandler
  254:  </Location>
  255: 
  256: =head1 INTRODUCTION
  257: 
  258: This module publishes a directory of files.
  259: 
  260: This is part of the LearningOnline Network with CAPA project
  261: described at http://www.lon-capa.org.
  262: 
  263: =head1 HANDLER SUBROUTINE
  264: 
  265: This routine is called by Apache and mod_perl.
  266: 
  267: =over 4
  268: 
  269: =item *
  270: 
  271: read in information
  272: 
  273: =item *
  274: 
  275: start page output
  276: 
  277: =item *
  278: 
  279: run through list of files and attempt to publish unhidden files
  280: 
  281: =back
  282: 
  283: =cut

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