File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.30: download - view: text, annotated - select for diffs
Thu Apr 6 22:15:19 2006 UTC (18 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: version_2_2_X, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, HEAD
- start_page

    1: # The LearningOnline Network with CAPA
    2: # Handler to retrieve an old version of a file
    3: #
    4: # $Id: lonretrieve.pm,v 1.30 2006/04/06 22:15:19 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 published 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(&Apache::loncommon::start_page('Retrieve Published Resources'));
  209: 
  210:   
  211:   $r->print('<h1>'.&mt('Retrieve previous versions of').' <tt>'.$fn.'</tt></h1>');
  212:   
  213:   if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
  214:           $r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.
  215: 		    &mt(' at ').$udom.
  216:                '</font></h3>');
  217:   }
  218: 
  219: 
  220:   if ($env{'form.phase'} eq 'two') {
  221:       &phasetwo($r,$fn,$uname,$udom);
  222:   } else {
  223:       &phaseone($r,$fn,$uname,$udom);
  224:   }
  225: 
  226:   $r->print(&Apache::loncommon::end_page());
  227:   return OK;  
  228: }
  229: 
  230: 1;
  231: __END__
  232: 
  233: =head1 NAME
  234: 
  235: Apache::lonretrieve - retrieves an old version of a file
  236: 
  237: =head1 SYNOPSIS
  238: 
  239: Invoked by /etc/httpd/conf/srm.conf:
  240: 
  241:  <Location /adm/retrieve>
  242:  PerlAccessHandler       Apache::lonacc
  243:  SetHandler perl-script
  244:  PerlHandler Apache::lonretrieve
  245:  ErrorDocument     403 /adm/login
  246:  ErrorDocument     404 /adm/notfound.html
  247:  ErrorDocument     406 /adm/unauthorized.html
  248:  ErrorDocument	  500 /adm/errorhandler
  249:  </Location>
  250: 
  251: =head1 INTRODUCTION
  252: 
  253: This module retrieves an old published version of a file.
  254: 
  255: This is part of the LearningOnline Network with CAPA project
  256: described at http://www.lon-capa.org.
  257: 
  258: =head1 HANDLER SUBROUTINE
  259: 
  260: This routine is called by Apache and mod_perl.
  261: 
  262: =over 4
  263: 
  264: =item *
  265: 
  266: Get query string for limited number of parameters
  267: 
  268: =item *
  269: 
  270: Start page output
  271: 
  272: =item *
  273: 
  274: print phase relevant output
  275: 
  276: =item *
  277: 
  278: (phase one is to select version; phase two retrieves version)
  279: 
  280: =back
  281: 
  282: =head1 OTHER SUBROUTINES
  283: 
  284: =over 4
  285: 
  286: =item *
  287: 
  288: phaseone() : Interface for selecting previous version.
  289: 
  290: =item *
  291: 
  292: phasetwo() : Interface for presenting specified version.
  293: 
  294: =back
  295: 
  296: =cut

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