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

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

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