File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.50: download - view: text, annotated - select for diffs
Tue Dec 30 22:19:18 2003 UTC (20 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #2444: show bombs in construction space.

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

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