File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.18: download - view: text, annotated - select for diffs
Thu May 30 14:08:42 2002 UTC (22 years ago) by www
Branches: MAIN
CVS tags: version_0_5_1, version_0_5, version_0_4, stable_2002_july, STABLE, HEAD
INTERNAL SERVER ERROR!!!

    1: # The LearningOnline Network with CAPA
    2: # Handler to retrieve an old version of a file
    3: #
    4: # $Id: lonretrieve.pm,v 1.18 2002/05/30 14:08:42 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: # (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:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  180: 					  ['filename']);
  181: 
  182:   if ($ENV{'form.filename'}) {
  183:       $fn=$ENV{'form.filename'};
  184:       $fn=~s/^http\:\/\/[^\/]+//;
  185:   } else {
  186:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  187:          ' unspecified filename for retrieval', $r->filename); 
  188:      return HTTP_NOT_FOUND;
  189:   }
  190: 
  191:   unless ($fn) { 
  192:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  193:          ' trying to retrieve non-existing file', $r->filename); 
  194:      return HTTP_NOT_FOUND;
  195:   } 
  196: 
  197: # ----------------------------------------------------------- Start page output
  198:   my $uname;
  199:   my $udom;
  200: 
  201:   ($uname,$udom)=
  202:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
  203:   unless (($uname) && ($udom)) {
  204:      $r->log_reason($uname.' at '.$udom.
  205:          ' trying to publish file '.$ENV{'form.filename'}.
  206:          ' ('.$fn.') - not authorized', 
  207:          $r->filename); 
  208:      return HTTP_NOT_ACCEPTABLE;
  209:   }
  210: 
  211:   $fn=~s/\/\~(\w+)//;
  212: 
  213:   $r->content_type('text/html');
  214:   $r->send_http_header;
  215: 
  216:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
  217: 
  218:   $r->print(
  219:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  220: 
  221:   
  222:   $r->print('<h1>Retrieve previous versions of <tt>'.$fn.'</tt></h1>');
  223:   
  224:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
  225:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
  226:                '</font></h3>');
  227:   }
  228: 
  229: 
  230:   if ($ENV{'form.phase'} eq 'two') {
  231:       &phasetwo($r,$fn,$uname,$udom);
  232:   } else {
  233:       &phaseone($r,$fn,$uname,$udom);
  234:   }
  235: 
  236:   $r->print('</body></html>');
  237:   return OK;  
  238: }
  239: 
  240: 1;
  241: __END__
  242: 
  243: =head1 NAME
  244: 
  245: Apache::lonretrieve - retrieves an old version of a file
  246: 
  247: =head1 SYNOPSIS
  248: 
  249: Invoked by /etc/httpd/conf/srm.conf:
  250: 
  251:  <Location /adm/retrieve>
  252:  PerlAccessHandler       Apache::lonacc
  253:  SetHandler perl-script
  254:  PerlHandler Apache::lonretrieve
  255:  ErrorDocument     403 /adm/login
  256:  ErrorDocument     404 /adm/notfound.html
  257:  ErrorDocument     406 /adm/unauthorized.html
  258:  ErrorDocument	  500 /adm/errorhandler
  259:  </Location>
  260: 
  261: =head1 INTRODUCTION
  262: 
  263: This module retrieves an old published version of a file.
  264: 
  265: This is part of the LearningOnline Network with CAPA project
  266: described at http://www.lon-capa.org.
  267: 
  268: =head1 HANDLER SUBROUTINE
  269: 
  270: This routine is called by Apache and mod_perl.
  271: 
  272: =over 4
  273: 
  274: =item *
  275: 
  276: Get query string for limited number of parameters
  277: 
  278: =item *
  279: 
  280: Start page output
  281: 
  282: =item *
  283: 
  284: print phase relevant output
  285: 
  286: =item *
  287: 
  288: (phase one is to select version; phase two retrieves version)
  289: 
  290: =back
  291: 
  292: =head1 OTHER SUBROUTINES
  293: 
  294: =over 4
  295: 
  296: =item *
  297: 
  298: phaseone() : Interface for selecting previous version.
  299: 
  300: =item *
  301: 
  302: phasetwo() : Interface for presenting specified version.
  303: 
  304: =back
  305: 
  306: =cut

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