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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to retrieve an old version of a file
                      3: #
1.30    ! albertel    4: # $Id: lonretrieve.pm,v 1.29 2006/01/31 22:37:04 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: 
1.30    ! albertel  208:   $r->print(&Apache::loncommon::start_page('Retrieve Published Resources'));
1.1       www       209: 
                    210:   
1.23      www       211:   $r->print('<h1>'.&mt('Retrieve previous versions of').' <tt>'.$fn.'</tt></h1>');
1.10      www       212:   
1.27      albertel  213:   if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.23      www       214:           $r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.
                    215: 		    &mt(' at ').$udom.
1.10      www       216:                '</font></h3>');
                    217:   }
                    218: 
1.1       www       219: 
1.27      albertel  220:   if ($env{'form.phase'} eq 'two') {
1.4       www       221:       &phasetwo($r,$fn,$uname,$udom);
1.2       www       222:   } else {
                    223:       &phaseone($r,$fn,$uname,$udom);
                    224:   }
1.1       www       225: 
1.30    ! albertel  226:   $r->print(&Apache::loncommon::end_page());
1.1       www       227:   return OK;  
                    228: }
1.7       www       229: 
                    230: 1;
                    231: __END__
1.16      harris41  232: 
                    233: =head1 NAME
                    234: 
                    235: Apache::lonretrieve - retrieves an old version of a file
                    236: 
                    237: =head1 SYNOPSIS
                    238: 
                    239: Invoked by /etc/httpd/conf/srm.conf:
                    240: 
                    241:  <Location /adm/retrieve>
                    242:  PerlAccessHandler       Apache::lonacc
                    243:  SetHandler perl-script
                    244:  PerlHandler Apache::lonretrieve
                    245:  ErrorDocument     403 /adm/login
                    246:  ErrorDocument     404 /adm/notfound.html
                    247:  ErrorDocument     406 /adm/unauthorized.html
                    248:  ErrorDocument	  500 /adm/errorhandler
                    249:  </Location>
                    250: 
                    251: =head1 INTRODUCTION
                    252: 
                    253: This module retrieves an old published version of a file.
                    254: 
                    255: This is part of the LearningOnline Network with CAPA project
                    256: described at http://www.lon-capa.org.
                    257: 
                    258: =head1 HANDLER SUBROUTINE
                    259: 
                    260: This routine is called by Apache and mod_perl.
                    261: 
                    262: =over 4
                    263: 
                    264: =item *
                    265: 
                    266: Get query string for limited number of parameters
                    267: 
                    268: =item *
                    269: 
                    270: Start page output
                    271: 
                    272: =item *
                    273: 
                    274: print phase relevant output
                    275: 
                    276: =item *
                    277: 
                    278: (phase one is to select version; phase two retrieves version)
                    279: 
                    280: =back
                    281: 
                    282: =head1 OTHER SUBROUTINES
                    283: 
                    284: =over 4
                    285: 
                    286: =item *
                    287: 
                    288: phaseone() : Interface for selecting previous version.
                    289: 
                    290: =item *
                    291: 
                    292: phasetwo() : Interface for presenting specified version.
                    293: 
                    294: =back
                    295: 
                    296: =cut

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