File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.22: download - view: text, annotated - select for diffs
Mon May 19 21:53:03 2003 UTC (20 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: version_1_0_3, version_1_0_2, version_1_0_1, version_1_0_0, version_0_99_5, version_0_99_4, version_0_99_3, version_0_99_2, version_0_99_1, version_0_99_0, conference_2003, HEAD
- didn't like )1.problem

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

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