File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.34: download - view: text, annotated - select for diffs
Wed Jun 18 14:58:39 2003 UTC (20 years, 10 months ago) by www
Branches: MAIN
CVS tags: version_0_99_3, HEAD
As discussed in developer meeting, now shows EditXML for problems.

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

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