File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.23: download - view: text, annotated - select for diffs
Mon Sep 22 15:39:53 2003 UTC (20 years, 8 months ago) by www
Branches: MAIN
CVS tags: version_1_1_X, version_1_1_3, version_1_1_2, version_1_1_1, version_1_1_0, version_1_0_99_3, version_1_0_99_2, version_1_0_99_1, version_1_0_99, HEAD
Internationalization.

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

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