File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.45: download - view: text, annotated - select for diffs
Tue Nov 18 04:08:15 2003 UTC (20 years, 6 months ago) by taceyjo1
Branches: MAIN
CVS tags: HEAD
New backend to make decompress go, shouldn't break anything.......

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

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