Annotation of loncom/publisher/lonretrieve.pm, revision 1.29

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to retrieve an old version of a file
                      3: #
1.29    ! albertel    4: # $Id: lonretrieve.pm,v 1.28 2005/12/12 21:59:12 albertel Exp $
1.15      albertel    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: #
1.16      harris41   29: ###
1.1       www        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);
1.10      www        37: use Apache::loncacc;
1.16      harris41   38: use Apache::loncommon();
1.23      www        39: use Apache::lonlocal;
1.27      albertel   40: use Apache::lonnet;
1.1       www        41: 
1.16      harris41   42: # ------------------------------------ Interface for selecting previous version
1.2       www        43: sub phaseone {
                     44:     my ($r,$fn,$uname,$udom)=@_;
                     45:     my $docroot=$r->dir_config('lonDocRoot');
                     46: 
1.3       www        47:     my $urldir='/res/'.$udom.'/'.$uname.$fn;
                     48:     $urldir=~s/\/[^\/]+$/\//;
                     49: 
                     50:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
                     51:     my $resdir=$resfn;
1.2       www        52:     $resdir=~s/\/[^\/]+$/\//;
                     53: 
1.6       www        54:     $fn=~/\/([^\/]+)\.(\w+)$/;
1.2       www        55:     my $main=$1;
                     56:     my $suffix=$2;
1.6       www        57: 
                     58:     if (-e $resfn) {  
1.3       www        59:     $r->print('<form action=/adm/retrieve method=post>'.
1.12      www        60: 	      '<input type=hidden name=filename value="/~'.$uname.$fn.'">'.
1.3       www        61:               '<input type=hidden name=phase value=two>'.
1.23      www        62:               '<table border=2><tr><th>'.&mt('Select').'</th><th>'.
                     63: 	      &mt('Version').'</th>'.
1.26      www        64:               '<th>'.&mt('Published on ...').'</th>'.
1.23      www        65:               '<th>'.&mt('Metadata').'</th></tr>');
1.28      albertel   66: 
1.2       www        67:     opendir(DIR,$resdir);
1.28      albertel   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) {
1.22      albertel   77:         if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
1.3       www        78: 	   my $version=$1;
1.26      www        79:            my $rmtime=&Apache::lonnet::metadata($resdir.'/'.$filename,'lastrevisiondate');
1.3       www        80:            $r->print('<tr><td><input type=radio name=version value="'.
1.26      www        81:                      $version.'"></td><td>'.&mt('Previously published version').' '.$version.'</td><td>'.
1.3       www        82:                      localtime($rmtime).'</td><td>'.
                     83:                      '<a href="'.$urldir.$filename.'.meta" target=cat>'.
1.23      www        84:                      &mt('Metadata Version').' '.$version.'</a>');
1.16      harris41   85:            if (&Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
1.8       www        86:                $r->print(
1.11      www        87:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename=/~'.
                     88:                         $uname.$fn.
1.24      albertel   89:                         '&versiontwo=priv&versionone='.$version.
1.23      www        90:                         '">'.&mt('Diffs with Version').' '.$version.'</a>');
1.8       www        91:            }
                     92:            $r->print('</a></td></tr>');
1.2       www        93:         }
                     94:     }
                     95:     closedir(DIR);
1.26      www        96:     my $rmtime=&Apache::lonnet::metadata($resfn,'lastrevisiondate');
1.3       www        97:     $r->print('<tr><td><input type=radio name=version value="new"></td>'.
1.29    ! albertel   98:               '<th>'.&mt('Currently published version').'</th><td>'.localtime($rmtime).
1.3       www        99:            '</td><td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target=cat>'.
1.23      www       100:               &mt('Metadata current version').'</a>');           
1.16      harris41  101:            if (&Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
1.9       www       102:                $r->print(
1.11      www       103:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename=/~'.
                    104:                         $uname.$fn.
1.20      albertel  105:                         '&versiontwo=priv'.
1.23      www       106:                         '">'.&mt('Diffs with current Version').'</a>');
1.9       www       107:            }
                    108:            $r->print('</td></tr></table><p>'.
1.23      www       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>');
1.6       www       112: } else {
1.23      www       113:     $r->print('<h3>'.&mt('No previous versions published.').'</h3>');
1.6       www       114: }
1.25      albertel  115:     $r->print('<p><a href="/priv/'.$uname.$fn.'">'.&mt('Back to').' '.$fn.
                    116: 	      '</a></p>'); 
1.2       www       117: }
1.1       www       118: 
1.16      harris41  119: # ---------------------------------- Interface for presenting specified version
1.4       www       120: sub phasetwo {
                    121:     my ($r,$fn,$uname,$udom)=@_;
1.27      albertel  122:     if ($env{'form.version'}) {
                    123:         my $version=$env{'form.version'};
1.4       www       124: 	if ($version eq 'new') {
1.23      www       125: 	    $r->print('<h3>'.&mt('Retrieving current (most recent) version').'</h3>');
1.4       www       126:         } else {
1.23      www       127:             $r->print('<h3>'.&mt('Retrieving old version').' '.$version.'</h3>');
1.4       www       128:         }
                    129:         my $logfile;
                    130:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
1.5       www       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;
1.4       www       136:         unless ($logfile=Apache::File->new('>>'.$ctarget.'.log')) {
                    137: 	  $r->print(
1.23      www       138:          '<font color=red>'.&mt('No write permission to user directory, FAIL').'</font>');
1.4       www       139:         }
                    140:         print $logfile 
                    141: "\n\n================= Retrieve ".localtime()." ================\n".
1.5       www       142: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
1.23      www       143:         $r->print('<p>'.&mt('Copying file').': ');
1.5       www       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.
1.23      www       153:                   '">'.&mt('Back to').' '.$fn.'</a></font>'); 
1.4       www       154:     } else {
                    155:        $r->print(
1.23      www       156:    '<font size=+1 color=red>'.&mt('Please pick a version to retrieve').'</font><p>');
1.4       www       157:        &phaseone($r,$fn,$uname,$udom);
                    158:     }
                    159: }
                    160: 
1.16      harris41  161: # ---------------------------------------------------------------- Main Handler
1.1       www       162: sub handler {
                    163: 
                    164:   my $r=shift;
                    165: 
                    166:   my $fn;
1.14      www       167: 
                    168: 
                    169: # Get query string for limited number of parameters
                    170: 
1.17      stredwic  171:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.18      www       172: 					  ['filename']);
1.1       www       173: 
1.27      albertel  174:   if ($env{'form.filename'}) {
                    175:       $fn=$env{'form.filename'};
1.10      www       176:       $fn=~s/^http\:\/\/[^\/]+//;
1.1       www       177:   } else {
1.27      albertel  178:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.2       www       179:          ' unspecified filename for retrieval', $r->filename); 
                    180:      return HTTP_NOT_FOUND;
1.1       www       181:   }
                    182: 
                    183:   unless ($fn) { 
1.27      albertel  184:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.2       www       185:          ' trying to retrieve non-existing file', $r->filename); 
1.1       www       186:      return HTTP_NOT_FOUND;
                    187:   } 
                    188: 
                    189: # ----------------------------------------------------------- Start page output
1.10      www       190:   my $uname;
                    191:   my $udom;
1.1       www       192: 
1.13      www       193:   ($uname,$udom)=
                    194:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
                    195:   unless (($uname) && ($udom)) {
1.10      www       196:      $r->log_reason($uname.' at '.$udom.
1.27      albertel  197:          ' trying to publish file '.$env{'form.filename'}.
1.10      www       198:          ' ('.$fn.') - not authorized', 
                    199:          $r->filename); 
                    200:      return HTTP_NOT_ACCEPTABLE;
                    201:   }
                    202: 
                    203:   $fn=~s/\/\~(\w+)//;
1.1       www       204: 
1.23      www       205:   &Apache::loncommon::content_type($r,'text/html');
1.1       www       206:   $r->send_http_header;
                    207: 
                    208:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
                    209: 
1.19      www       210:   $r->print(&Apache::loncommon::bodytag('Retrieve Published Resources'));
1.1       www       211: 
                    212:   
1.23      www       213:   $r->print('<h1>'.&mt('Retrieve previous versions of').' <tt>'.$fn.'</tt></h1>');
1.10      www       214:   
1.27      albertel  215:   if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.23      www       216:           $r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.
                    217: 		    &mt(' at ').$udom.
1.10      www       218:                '</font></h3>');
                    219:   }
                    220: 
1.1       www       221: 
1.27      albertel  222:   if ($env{'form.phase'} eq 'two') {
1.4       www       223:       &phasetwo($r,$fn,$uname,$udom);
1.2       www       224:   } else {
                    225:       &phaseone($r,$fn,$uname,$udom);
                    226:   }
1.1       www       227: 
                    228:   $r->print('</body></html>');
                    229:   return OK;  
                    230: }
1.7       www       231: 
                    232: 1;
                    233: __END__
1.16      harris41  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>