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

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

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