File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.25: download - view: text, annotated - select for diffs
Fri Jul 2 09:41:07 2004 UTC (19 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_1_3_X, version_1_3_3, version_1_3_2, version_1_3_1, version_1_3_0, version_1_2_X, version_1_2_99_1, version_1_2_99_0, version_1_2_1, version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, HEAD
BUG#3061, retreive doesn't need it's own indow.

    1: # The LearningOnline Network with CAPA
    2: # Handler to retrieve an old version of a file
    3: #
    4: # $Id: lonretrieve.pm,v 1.25 2004/07/02 09:41:07 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: 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&versionone='.$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:     $r->print('<p><a href="/priv/'.$uname.$fn.'">'.&mt('Back to').' '.$fn.
  127: 	      '</a></p>'); 
  128: }
  129: 
  130: # ---------------------------------- Interface for presenting specified version
  131: sub phasetwo {
  132:     my ($r,$fn,$uname,$udom)=@_;
  133:     if ($ENV{'form.version'}) {
  134:         my $version=$ENV{'form.version'};
  135: 	if ($version eq 'new') {
  136: 	    $r->print('<h3>'.&mt('Retrieving current (most recent) version').'</h3>');
  137:         } else {
  138:             $r->print('<h3>'.&mt('Retrieving old version').' '.$version.'</h3>');
  139:         }
  140:         my $logfile;
  141:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
  142:         my $vfn=$fn;
  143:         if ($version ne 'new') {
  144: 	    $vfn=~s/\.(\w+)$/\.$version\.$1/;
  145:         }
  146:         my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;
  147:         unless ($logfile=Apache::File->new('>>'.$ctarget.'.log')) {
  148: 	  $r->print(
  149:          '<font color=red>'.&mt('No write permission to user directory, FAIL').'</font>');
  150:         }
  151:         print $logfile 
  152: "\n\n================= Retrieve ".localtime()." ================\n".
  153: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
  154:         $r->print('<p>'.&mt('Copying file').': ');
  155:         if (copy($csource,$ctarget)) {
  156: 	    $r->print('ok<p>');
  157:             print $logfile "Copied sucessfully.\n\n";
  158:         } else {
  159:             my $error=$!;
  160: 	    $r->print('fail, '.$error.'<p>');
  161:             print $logfile "Copy failed: $error\n\n";
  162:         }
  163:         $r->print('<font size=+2><a href="/priv/'.$uname.$fn.
  164:                   '">'.&mt('Back to').' '.$fn.'</a></font>'); 
  165:     } else {
  166:        $r->print(
  167:    '<font size=+1 color=red>'.&mt('Please pick a version to retrieve').'</font><p>');
  168:        &phaseone($r,$fn,$uname,$udom);
  169:     }
  170: }
  171: 
  172: # ---------------------------------------------------------------- Main Handler
  173: sub handler {
  174: 
  175:   my $r=shift;
  176: 
  177:   my $fn;
  178: 
  179: 
  180: # Get query string for limited number of parameters
  181: 
  182:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  183: 					  ['filename']);
  184: 
  185:   if ($ENV{'form.filename'}) {
  186:       $fn=$ENV{'form.filename'};
  187:       $fn=~s/^http\:\/\/[^\/]+//;
  188:   } else {
  189:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  190:          ' unspecified filename for retrieval', $r->filename); 
  191:      return HTTP_NOT_FOUND;
  192:   }
  193: 
  194:   unless ($fn) { 
  195:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  196:          ' trying to retrieve non-existing file', $r->filename); 
  197:      return HTTP_NOT_FOUND;
  198:   } 
  199: 
  200: # ----------------------------------------------------------- Start page output
  201:   my $uname;
  202:   my $udom;
  203: 
  204:   ($uname,$udom)=
  205:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
  206:   unless (($uname) && ($udom)) {
  207:      $r->log_reason($uname.' at '.$udom.
  208:          ' trying to publish file '.$ENV{'form.filename'}.
  209:          ' ('.$fn.') - not authorized', 
  210:          $r->filename); 
  211:      return HTTP_NOT_ACCEPTABLE;
  212:   }
  213: 
  214:   $fn=~s/\/\~(\w+)//;
  215: 
  216:   &Apache::loncommon::content_type($r,'text/html');
  217:   $r->send_http_header;
  218: 
  219:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
  220: 
  221:   $r->print(&Apache::loncommon::bodytag('Retrieve Published Resources'));
  222: 
  223:   
  224:   $r->print('<h1>'.&mt('Retrieve previous versions of').' <tt>'.$fn.'</tt></h1>');
  225:   
  226:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
  227:           $r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.
  228: 		    &mt(' at ').$udom.
  229:                '</font></h3>');
  230:   }
  231: 
  232: 
  233:   if ($ENV{'form.phase'} eq 'two') {
  234:       &phasetwo($r,$fn,$uname,$udom);
  235:   } else {
  236:       &phaseone($r,$fn,$uname,$udom);
  237:   }
  238: 
  239:   $r->print('</body></html>');
  240:   return OK;  
  241: }
  242: 
  243: 1;
  244: __END__
  245: 
  246: =head1 NAME
  247: 
  248: Apache::lonretrieve - retrieves an old version of a file
  249: 
  250: =head1 SYNOPSIS
  251: 
  252: Invoked by /etc/httpd/conf/srm.conf:
  253: 
  254:  <Location /adm/retrieve>
  255:  PerlAccessHandler       Apache::lonacc
  256:  SetHandler perl-script
  257:  PerlHandler Apache::lonretrieve
  258:  ErrorDocument     403 /adm/login
  259:  ErrorDocument     404 /adm/notfound.html
  260:  ErrorDocument     406 /adm/unauthorized.html
  261:  ErrorDocument	  500 /adm/errorhandler
  262:  </Location>
  263: 
  264: =head1 INTRODUCTION
  265: 
  266: This module retrieves an old published version of a file.
  267: 
  268: This is part of the LearningOnline Network with CAPA project
  269: described at http://www.lon-capa.org.
  270: 
  271: =head1 HANDLER SUBROUTINE
  272: 
  273: This routine is called by Apache and mod_perl.
  274: 
  275: =over 4
  276: 
  277: =item *
  278: 
  279: Get query string for limited number of parameters
  280: 
  281: =item *
  282: 
  283: Start page output
  284: 
  285: =item *
  286: 
  287: print phase relevant output
  288: 
  289: =item *
  290: 
  291: (phase one is to select version; phase two retrieves version)
  292: 
  293: =back
  294: 
  295: =head1 OTHER SUBROUTINES
  296: 
  297: =over 4
  298: 
  299: =item *
  300: 
  301: phaseone() : Interface for selecting previous version.
  302: 
  303: =item *
  304: 
  305: phasetwo() : Interface for presenting specified version.
  306: 
  307: =back
  308: 
  309: =cut

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