File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.28: download - view: text, annotated - select for diffs
Mon Dec 12 21:59:12 2005 UTC (18 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_99_1, HEAD
- put past reviosns in revision order

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

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