File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.16: download - view: text, annotated - select for diffs
Mon Dec 17 00:57:59 2001 UTC (22 years, 6 months ago) by harris41
Branches: MAIN
CVS tags: stable_2002_spring, stable_2002_april, HEAD
adding in POD documentation; changing void context map statements to
foreach statements; removing references to Apache::lonnet::fileembstyle
and using Apache::loncommon::fileembstyle -Scott Harrison

    1: # The LearningOnline Network with CAPA
    2: # Handler to retrieve an old version of a file
    3: #
    4: # $Id: lonretrieve.pm,v 1.16 2001/12/17 00:57:59 harris41 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: # (Publication Handler
   30: # 
   31: # (TeX Content Handler
   32: #
   33: # YEAR=2000
   34: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   35: #
   36: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
   37: # YEAR=2001
   38: # 03/23 Guy Albertelli
   39: # 03/24,03/29 Gerd Kortemeyer)
   40: #
   41: # 03/31,04/03,05/02,05/09,06/23,08/20 Gerd Kortemeyer
   42: # 12/16 Scott Harrison
   43: #
   44: ###
   45: 
   46: package Apache::lonretrieve;
   47: 
   48: use strict;
   49: use Apache::File;
   50: use File::Copy;
   51: use Apache::Constants qw(:common :http :methods);
   52: use Apache::loncacc;
   53: use Apache::loncommon();
   54: 
   55: # ------------------------------------ Interface for selecting previous version
   56: sub phaseone {
   57:     my ($r,$fn,$uname,$udom)=@_;
   58:     my $docroot=$r->dir_config('lonDocRoot');
   59: 
   60:     my $urldir='/res/'.$udom.'/'.$uname.$fn;
   61:     $urldir=~s/\/[^\/]+$/\//;
   62: 
   63:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
   64:     my $resdir=$resfn;
   65:     $resdir=~s/\/[^\/]+$/\//;
   66: 
   67:     $fn=~/\/([^\/]+)\.(\w+)$/;
   68:     my $main=$1;
   69:     my $suffix=$2;
   70: 
   71:     if (-e $resfn) {  
   72:     $r->print('<form action=/adm/retrieve method=post>'.
   73: 	      '<input type=hidden name=filename value="/~'.$uname.$fn.'">'.
   74:               '<input type=hidden name=phase value=two>'.
   75:               '<table border=2><tr><th>Select</th><th>Version</th>'.
   76:               '<th>Became this version on ...</th>'.
   77:               '<th>Metadata</th></tr>');
   78:     my $filename;
   79:     opendir(DIR,$resdir);
   80:     while ($filename=readdir(DIR)) {
   81:         if ($filename=~/^$main\.(\d+)\.$suffix$/) {
   82: 	   my $version=$1;
   83:            my ($rdev,$rino,$rmode,$rnlink,
   84:                 $ruid,$rgid,$rrdev,$rsize,
   85:                 $ratime,$rmtime,$rctime,
   86:                 $rblksize,$rblocks)=stat($resdir.'/'.$filename);
   87:            $r->print('<tr><td><input type=radio name=version value="'.
   88:                      $version.'"></td><th>'.$version.'</th><td>'.
   89:                      localtime($rmtime).'</td><td>'.
   90:                      '<a href="'.$urldir.$filename.'.meta" target=cat>'.
   91:                      'Metadata Version '.$version.'</a>');
   92:            if (&Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
   93:                $r->print(
   94:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename=/~'.
   95:                         $uname.$fn.
   96:                         '&versionone=priv&versiontwo='.$version.
   97:                         '">Diffs with Version '.$version.'</a>');
   98:            }
   99:            $r->print('</a></td></tr>');
  100:         }
  101:     }
  102:     closedir(DIR);
  103:     my ($rdev,$rino,$rmode,$rnlink,
  104:         $ruid,$rgid,$rrdev,$rsize,
  105:         $ratime,$rmtime,$rctime,
  106:         $rblksize,$rblocks)=stat($resfn);
  107:     $r->print('<tr><td><input type=radio name=version value="new"></td>'.
  108:               '<th>Current</th><td>'.localtime($rmtime).
  109:            '</td><td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target=cat>'.
  110:               'Metadata current version</a>');           
  111:            if (&Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
  112:                $r->print(
  113:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename=/~'.
  114:                         $uname.$fn.
  115:                         '&versionone=priv'.
  116:                         '">Diffs with current Version</a>');
  117:            }
  118:            $r->print('</td></tr></table><p>'.
  119:            '<font size=+1 color=red>Retrieval of an old version will '.
  120:            'overwrite the file currently in construction space</font><p>'.
  121:            '<input type=submit value="Retrieve version"></form>');
  122: } else {
  123:     $r->print('<h3>No previous versions published.</h3>');
  124: }
  125: }
  126: 
  127: # ---------------------------------- Interface for presenting specified version
  128: sub phasetwo {
  129:     my ($r,$fn,$uname,$udom)=@_;
  130:     if ($ENV{'form.version'}) {
  131:         my $version=$ENV{'form.version'};
  132: 	if ($version eq 'new') {
  133: 	    $r->print('<h3>Retrieving current (most recent) version</h3>');
  134:         } else {
  135:             $r->print('<h3>Retrieving old version '.$version.'</h3>');
  136:         }
  137:         my $logfile;
  138:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
  139:         my $vfn=$fn;
  140:         if ($version ne 'new') {
  141: 	    $vfn=~s/\.(\w+)$/\.$version\.$1/;
  142:         }
  143:         my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;
  144:         unless ($logfile=Apache::File->new('>>'.$ctarget.'.log')) {
  145: 	  $r->print(
  146:          '<font color=red>No write permission to user directory, FAIL</font>');
  147:         }
  148:         print $logfile 
  149: "\n\n================= Retrieve ".localtime()." ================\n".
  150: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
  151:         $r->print('<p>Copying file: ');
  152:         if (copy($csource,$ctarget)) {
  153: 	    $r->print('ok<p>');
  154:             print $logfile "Copied sucessfully.\n\n";
  155:         } else {
  156:             my $error=$!;
  157: 	    $r->print('fail, '.$error.'<p>');
  158:             print $logfile "Copy failed: $error\n\n";
  159:         }
  160:         $r->print('<font size=+2><a href="/priv/'.$uname.$fn.
  161:                   '">Back to '.$fn.'</a></font>'); 
  162:     } else {
  163:        $r->print(
  164:    '<font size=+1 color=red>Please pick a version to retrieve</font><p>');
  165:        &phaseone($r,$fn,$uname,$udom);
  166:     }
  167: }
  168: 
  169: # ---------------------------------------------------------------- Main Handler
  170: sub handler {
  171: 
  172:   my $r=shift;
  173: 
  174:   my $fn;
  175: 
  176: 
  177: # Get query string for limited number of parameters
  178: 
  179:     foreach (split(/&/,$ENV{'QUERY_STRING'})) {
  180:        my ($name, $value) = split(/=/,$_);
  181:        $value =~ tr/+/ /;
  182:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  183:        if ($name eq 'filename') {
  184:            unless ($ENV{'form.'.$name}) {
  185:               $ENV{'form.'.$name}=$value;
  186: 	   }
  187:        }
  188:     }
  189: 
  190: 
  191:   if ($ENV{'form.filename'}) {
  192:       $fn=$ENV{'form.filename'};
  193:       $fn=~s/^http\:\/\/[^\/]+//;
  194:   } else {
  195:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  196:          ' unspecified filename for retrieval', $r->filename); 
  197:      return HTTP_NOT_FOUND;
  198:   }
  199: 
  200:   unless ($fn) { 
  201:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  202:          ' trying to retrieve non-existing file', $r->filename); 
  203:      return HTTP_NOT_FOUND;
  204:   } 
  205: 
  206: # ----------------------------------------------------------- Start page output
  207:   my $uname;
  208:   my $udom;
  209: 
  210:   ($uname,$udom)=
  211:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
  212:   unless (($uname) && ($udom)) {
  213:      $r->log_reason($uname.' at '.$udom.
  214:          ' trying to publish file '.$ENV{'form.filename'}.
  215:          ' ('.$fn.') - not authorized', 
  216:          $r->filename); 
  217:      return HTTP_NOT_ACCEPTABLE;
  218:   }
  219: 
  220:   $fn=~s/\/\~(\w+)//;
  221: 
  222:   $r->content_type('text/html');
  223:   $r->send_http_header;
  224: 
  225:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
  226: 
  227:   $r->print(
  228:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  229: 
  230:   
  231:   $r->print('<h1>Retrieve previous versions of <tt>'.$fn.'</tt></h1>');
  232:   
  233:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
  234:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
  235:                '</font></h3>');
  236:   }
  237: 
  238: 
  239:   if ($ENV{'form.phase'} eq 'two') {
  240:       &phasetwo($r,$fn,$uname,$udom);
  241:   } else {
  242:       &phaseone($r,$fn,$uname,$udom);
  243:   }
  244: 
  245:   $r->print('</body></html>');
  246:   return OK;  
  247: }
  248: 
  249: 1;
  250: __END__
  251: 
  252: =head1 NAME
  253: 
  254: Apache::lonretrieve - retrieves an old version of a file
  255: 
  256: =head1 SYNOPSIS
  257: 
  258: Invoked by /etc/httpd/conf/srm.conf:
  259: 
  260:  <Location /adm/retrieve>
  261:  PerlAccessHandler       Apache::lonacc
  262:  SetHandler perl-script
  263:  PerlHandler Apache::lonretrieve
  264:  ErrorDocument     403 /adm/login
  265:  ErrorDocument     404 /adm/notfound.html
  266:  ErrorDocument     406 /adm/unauthorized.html
  267:  ErrorDocument	  500 /adm/errorhandler
  268:  </Location>
  269: 
  270: =head1 INTRODUCTION
  271: 
  272: This module retrieves an old published version of a file.
  273: 
  274: This is part of the LearningOnline Network with CAPA project
  275: described at http://www.lon-capa.org.
  276: 
  277: =head1 HANDLER SUBROUTINE
  278: 
  279: This routine is called by Apache and mod_perl.
  280: 
  281: =over 4
  282: 
  283: =item *
  284: 
  285: Get query string for limited number of parameters
  286: 
  287: =item *
  288: 
  289: Start page output
  290: 
  291: =item *
  292: 
  293: print phase relevant output
  294: 
  295: =item *
  296: 
  297: (phase one is to select version; phase two retrieves version)
  298: 
  299: =back
  300: 
  301: =head1 OTHER SUBROUTINES
  302: 
  303: =over 4
  304: 
  305: =item *
  306: 
  307: phaseone() : Interface for selecting previous version.
  308: 
  309: =item *
  310: 
  311: phasetwo() : Interface for presenting specified version.
  312: 
  313: =back
  314: 
  315: =cut

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