File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.27: download - view: text, annotated - select for diffs
Thu Apr 7 06:56:27 2005 UTC (19 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: version_2_0_X, version_2_0_2, version_2_0_1, version_2_0_0, version_1_99_3, version_1_99_2, version_1_99_1_tmcc, version_1_99_1, version_1_99_0_tmcc, version_1_99_0, HEAD
- ENV -> env

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

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