File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.39: download - view: text, annotated - select for diffs
Mon Sep 22 03:02:06 2003 UTC (20 years, 8 months ago) by www
Branches: MAIN
CVS tags: HEAD
Internationalizing.

    1: # The LearningOnline Network with CAPA
    2: # Construction Space Directory Lister
    3: #
    4: # $Id: lonpubdir.pm,v 1.39 2003/09/22 03:02:06 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/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(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="Publish Directory" />'.
  225:               '<input type="hidden" name="pubrec" value="" />'.
  226:               '<input type="button" onClick="pubrecdir(this.form);" value="Publish Directory and Sub Directories" /></td><td>'.
  227: '<input type="button" onClick="window.location='."'/~".
  228:                $uname.$thisdisfn."/default.meta'".'" value="Edit Directory Catalog Information" /></td></tr><tr><td><input type="checkbox" name="forcerepub" /> Force publication of unmodified files.</td><td>&nbsp;</td></tr></table></form>');
  229:     
  230:     if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
  231: 	$r->print('<h3>'.&mt('Co-Author').': '.$uname.' at '.$udom.
  232: 		  '</h3>');
  233:     }
  234: }
  235: 
  236: #
  237: #   Get the title string or "[untitled]" if the file has no title metadata:
  238: #   Without the latter substitution, it's impossible to examine metadata for
  239: #   untitled resources.  Resources may be legitimately untitled, to prevent
  240: #   searches from locating them.
  241: #
  242: #   $str = getTitleString($fullname);
  243: #       $fullname - Fully qualified filename to check.
  244: #
  245: sub getTitleString {
  246:     my $fullname = shift;
  247:     my $title    = &Apache::lonnet::metadata($fullname, 'title');
  248: 
  249:     unless ($title) {
  250: 	$title = "[untitled]";
  251:     }
  252:     return $title;
  253: }
  254: 
  255: 
  256: #
  257: #  Put out a directory table row:
  258: #    putdirectory(r, base, here, dirname, modtime)
  259: #      r       - Apache request object.
  260: #      reqfile - File in request.
  261: #      here    - Where we are in directory tree.
  262: #      dirname - Name of directory special file.
  263: #      modtime - Encoded modification time.
  264: # 
  265: sub putdirectory {
  266:     my ($r, $reqfile, $here, $dirname, $modtime) = @_;
  267:   
  268:     # construct the display filename: the directory name unless ..:
  269:     
  270:     my $disfilename = $dirname;
  271:     if ($dirname eq '..') {
  272: 	$disfilename = '<i>'.&mt('Parent Directory').'</i>';
  273:     }
  274:     unless (( ($dirname eq '..') && ($reqfile eq '')) ||
  275: 	    ($dirname eq '.')) {
  276: 	$r->print('<tr bgcolor="#CCCCFF">'.
  277: 		  '<td>Go to ...</td>'.
  278: 		  '<td><a href="'.$here.'/'.$dirname.'/" target="_top">'.
  279: 		  $disfilename.'</a></td>'.
  280: 		        '<td>&nbsp;</td>'.
  281: 		  '<td>&nbsp;</td>'.
  282: 		  '<td>'.localtime($modtime).'</td>'.
  283: 		  "</tr>\n");
  284:     }	
  285:     return OK;
  286: }
  287: #
  288: #   Put a table row for a file resource.
  289: #
  290: sub putresource {
  291:     my ($r, $uname, $filename, $thisdisfn, 
  292: 	$resdir, $targetdir, $linkdir,
  293: 	$cmtime) = @_;
  294: 
  295:     my $status='Unpublished';
  296:     my $bgcolor='#FFCCCC';
  297:     my $title='&nbsp;';
  298:     if (-e $resdir.'/'.$filename) {
  299: 	my ($rdev,$rino,$rmode,$rnlink,
  300: 	    $ruid,$rgid,$rrdev,$rsize,
  301: 	    $ratime,$rmtime,$rctime,
  302: 	    $rblksize,$rblocks)=stat($resdir.'/'.$filename);
  303: 	if ($rmtime>=$cmtime) {
  304: 	    $status='Published';
  305:             $bgcolor='#CCFFCC';
  306: 	    $title='<a href="/res/'.$targetdir.'/'.$filename.
  307: 		'.meta" target=cat>'.
  308: 		getTitleString($targetdir.'/'.$filename, 'title').'</a>';
  309: 	} else {
  310: 	    $status='Modified';
  311:             $bgcolor='#FFFFCC';
  312: 	    $title='<a href="/res/'.$targetdir.'/'.$filename.'.meta" target=cat>'.
  313: 		getTitleString($targetdir.'/'.$filename,'title').'</a>';
  314: 	    if (&Apache::loncommon::fileembstyle(($filename=~/\.(\w+)$/)) eq 'ssi') {
  315: 		$status.='<br><a href="/adm/diff?filename=/~'.$uname.
  316: 		    $thisdisfn.'/'.$filename.
  317: 		    '&versiontwo=priv" target=cat>Diffs</a>';
  318: 	    }
  319: 	}   
  320: 	$status.='<br><a href="/adm/retrieve?filename=/~'.$uname.
  321: 	    $thisdisfn.'/'.$filename.'" target=cat>Retrieve</a>';
  322:     }
  323:     my $editlink='';
  324:     my $editlink2='';
  325:     if ($filename=~/\.(xml|html|htm|xhtml|xhtm|sty)$/) {
  326: 	$editlink=' (<a href="'.$linkdir.'/'.$filename.'?forceedit=1" target="_top">'.&mt('Edit').'</a>)';
  327:     }
  328:     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library)$/) {
  329: 	$editlink=' (<a href="'.$linkdir.'/'.$filename.'?forceedit=1" target="_top">'.&mt('EditXML').'</a>)';
  330: 	$editlink2=' (<a href="'.$linkdir.'/'.$filename.'?forceColoredit=1" target="_top">'.&mt('Edit').'</a>)';
  331:     }
  332:     $r->print('<tr bgcolor="'.$bgcolor.'">'.
  333: 	      '<td><a target="_parent" href="/adm/publish?filename=/~'.
  334: 	      $uname.$thisdisfn.'/'.$filename.'">'.'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>'.localtime($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>