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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to retrieve an old version of a file
                      3: #
1.25    ! albertel    4: # $Id: lonretrieve.pm,v 1.24 2004/03/11 22:45:06 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.1       www        29: # (Publication Handler
                     30: # 
                     31: # (TeX Content Handler
                     32: #
1.16      harris41   33: # YEAR=2000
1.1       www        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
1.16      harris41   37: # YEAR=2001
1.1       www        38: # 03/23 Guy Albertelli
                     39: # 03/24,03/29 Gerd Kortemeyer)
                     40: #
1.14      www        41: # 03/31,04/03,05/02,05/09,06/23,08/20 Gerd Kortemeyer
1.16      harris41   42: #
                     43: ###
1.1       www        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);
1.10      www        51: use Apache::loncacc;
1.16      harris41   52: use Apache::loncommon();
1.23      www        53: use Apache::lonlocal;
1.1       www        54: 
1.16      harris41   55: # ------------------------------------ Interface for selecting previous version
1.2       www        56: sub phaseone {
                     57:     my ($r,$fn,$uname,$udom)=@_;
                     58:     my $docroot=$r->dir_config('lonDocRoot');
                     59: 
1.3       www        60:     my $urldir='/res/'.$udom.'/'.$uname.$fn;
                     61:     $urldir=~s/\/[^\/]+$/\//;
                     62: 
                     63:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
                     64:     my $resdir=$resfn;
1.2       www        65:     $resdir=~s/\/[^\/]+$/\//;
                     66: 
1.6       www        67:     $fn=~/\/([^\/]+)\.(\w+)$/;
1.2       www        68:     my $main=$1;
                     69:     my $suffix=$2;
1.6       www        70: 
                     71:     if (-e $resfn) {  
1.3       www        72:     $r->print('<form action=/adm/retrieve method=post>'.
1.12      www        73: 	      '<input type=hidden name=filename value="/~'.$uname.$fn.'">'.
1.3       www        74:               '<input type=hidden name=phase value=two>'.
1.23      www        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>');
1.2       www        79:     my $filename;
                     80:     opendir(DIR,$resdir);
                     81:     while ($filename=readdir(DIR)) {
1.22      albertel   82:         if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
1.3       www        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>'.
1.23      www        92:                      &mt('Metadata Version').' '.$version.'</a>');
1.16      harris41   93:            if (&Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
1.8       www        94:                $r->print(
1.11      www        95:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename=/~'.
                     96:                         $uname.$fn.
1.24      albertel   97:                         '&versiontwo=priv&versionone='.$version.
1.23      www        98:                         '">'.&mt('Diffs with Version').' '.$version.'</a>');
1.8       www        99:            }
                    100:            $r->print('</a></td></tr>');
1.2       www       101:         }
                    102:     }
                    103:     closedir(DIR);
1.3       www       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>'.
1.23      www       109:               '<th>'.&mt('Current').'</th><td>'.localtime($rmtime).
1.3       www       110:            '</td><td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target=cat>'.
1.23      www       111:               &mt('Metadata current version').'</a>');           
1.16      harris41  112:            if (&Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
1.9       www       113:                $r->print(
1.11      www       114:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename=/~'.
                    115:                         $uname.$fn.
1.20      albertel  116:                         '&versiontwo=priv'.
1.23      www       117:                         '">'.&mt('Diffs with current Version').'</a>');
1.9       www       118:            }
                    119:            $r->print('</td></tr></table><p>'.
1.23      www       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>');
1.6       www       123: } else {
1.23      www       124:     $r->print('<h3>'.&mt('No previous versions published.').'</h3>');
1.6       www       125: }
1.25    ! albertel  126:     $r->print('<p><a href="/priv/'.$uname.$fn.'">'.&mt('Back to').' '.$fn.
        !           127: 	      '</a></p>'); 
1.2       www       128: }
1.1       www       129: 
1.16      harris41  130: # ---------------------------------- Interface for presenting specified version
1.4       www       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') {
1.23      www       136: 	    $r->print('<h3>'.&mt('Retrieving current (most recent) version').'</h3>');
1.4       www       137:         } else {
1.23      www       138:             $r->print('<h3>'.&mt('Retrieving old version').' '.$version.'</h3>');
1.4       www       139:         }
                    140:         my $logfile;
                    141:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
1.5       www       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;
1.4       www       147:         unless ($logfile=Apache::File->new('>>'.$ctarget.'.log')) {
                    148: 	  $r->print(
1.23      www       149:          '<font color=red>'.&mt('No write permission to user directory, FAIL').'</font>');
1.4       www       150:         }
                    151:         print $logfile 
                    152: "\n\n================= Retrieve ".localtime()." ================\n".
1.5       www       153: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
1.23      www       154:         $r->print('<p>'.&mt('Copying file').': ');
1.5       www       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.
1.23      www       164:                   '">'.&mt('Back to').' '.$fn.'</a></font>'); 
1.4       www       165:     } else {
                    166:        $r->print(
1.23      www       167:    '<font size=+1 color=red>'.&mt('Please pick a version to retrieve').'</font><p>');
1.4       www       168:        &phaseone($r,$fn,$uname,$udom);
                    169:     }
                    170: }
                    171: 
1.16      harris41  172: # ---------------------------------------------------------------- Main Handler
1.1       www       173: sub handler {
                    174: 
                    175:   my $r=shift;
                    176: 
                    177:   my $fn;
1.14      www       178: 
                    179: 
                    180: # Get query string for limited number of parameters
                    181: 
1.17      stredwic  182:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.18      www       183: 					  ['filename']);
1.1       www       184: 
                    185:   if ($ENV{'form.filename'}) {
                    186:       $fn=$ENV{'form.filename'};
1.10      www       187:       $fn=~s/^http\:\/\/[^\/]+//;
1.1       www       188:   } else {
1.2       www       189:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                    190:          ' unspecified filename for retrieval', $r->filename); 
                    191:      return HTTP_NOT_FOUND;
1.1       www       192:   }
                    193: 
                    194:   unless ($fn) { 
                    195:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1.2       www       196:          ' trying to retrieve non-existing file', $r->filename); 
1.1       www       197:      return HTTP_NOT_FOUND;
                    198:   } 
                    199: 
                    200: # ----------------------------------------------------------- Start page output
1.10      www       201:   my $uname;
                    202:   my $udom;
1.1       www       203: 
1.13      www       204:   ($uname,$udom)=
                    205:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
                    206:   unless (($uname) && ($udom)) {
1.10      www       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+)//;
1.1       www       215: 
1.23      www       216:   &Apache::loncommon::content_type($r,'text/html');
1.1       www       217:   $r->send_http_header;
                    218: 
                    219:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
                    220: 
1.19      www       221:   $r->print(&Apache::loncommon::bodytag('Retrieve Published Resources'));
1.1       www       222: 
                    223:   
1.23      www       224:   $r->print('<h1>'.&mt('Retrieve previous versions of').' <tt>'.$fn.'</tt></h1>');
1.10      www       225:   
                    226:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
1.23      www       227:           $r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.
                    228: 		    &mt(' at ').$udom.
1.10      www       229:                '</font></h3>');
                    230:   }
                    231: 
1.1       www       232: 
1.2       www       233:   if ($ENV{'form.phase'} eq 'two') {
1.4       www       234:       &phasetwo($r,$fn,$uname,$udom);
1.2       www       235:   } else {
                    236:       &phaseone($r,$fn,$uname,$udom);
                    237:   }
1.1       www       238: 
                    239:   $r->print('</body></html>');
                    240:   return OK;  
                    241: }
1.7       www       242: 
                    243: 1;
                    244: __END__
1.16      harris41  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>