File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.15: download - view: text, annotated - select for diffs
Tue Dec 4 15:34:57 2001 UTC (22 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
 - GPL headers added

    1: # The LearningOnline Network with CAPA
    2: # Handler to retrieve an old version of a file
    3: #
    4: # $Id: lonretrieve.pm,v 1.15 2001/12/04 15:34:57 albertel Exp $
    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: #
   29: # (Publication Handler
   30: # 
   31: # (TeX Content Handler
   32: #
   33: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   34: #
   35: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
   36: # 03/23 Guy Albertelli
   37: # 03/24,03/29 Gerd Kortemeyer)
   38: #
   39: # 03/31,04/03,05/02,05/09,06/23,08/20 Gerd Kortemeyer
   40: 
   41: package Apache::lonretrieve;
   42: 
   43: use strict;
   44: use Apache::File;
   45: use File::Copy;
   46: use Apache::Constants qw(:common :http :methods);
   47: use Apache::loncacc;
   48: 
   49: sub phaseone {
   50:     my ($r,$fn,$uname,$udom)=@_;
   51:     my $docroot=$r->dir_config('lonDocRoot');
   52: 
   53:     my $urldir='/res/'.$udom.'/'.$uname.$fn;
   54:     $urldir=~s/\/[^\/]+$/\//;
   55: 
   56:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
   57:     my $resdir=$resfn;
   58:     $resdir=~s/\/[^\/]+$/\//;
   59: 
   60:     $fn=~/\/([^\/]+)\.(\w+)$/;
   61:     my $main=$1;
   62:     my $suffix=$2;
   63: 
   64:     if (-e $resfn) {  
   65:     $r->print('<form action=/adm/retrieve method=post>'.
   66: 	      '<input type=hidden name=filename value="/~'.$uname.$fn.'">'.
   67:               '<input type=hidden name=phase value=two>'.
   68:               '<table border=2><tr><th>Select</th><th>Version</th>'.
   69:               '<th>Became this version on ...</th>'.
   70:               '<th>Metadata</th></tr>');
   71:     my $filename;
   72:     opendir(DIR,$resdir);
   73:     while ($filename=readdir(DIR)) {
   74:         if ($filename=~/^$main\.(\d+)\.$suffix$/) {
   75: 	   my $version=$1;
   76:            my ($rdev,$rino,$rmode,$rnlink,
   77:                 $ruid,$rgid,$rrdev,$rsize,
   78:                 $ratime,$rmtime,$rctime,
   79:                 $rblksize,$rblocks)=stat($resdir.'/'.$filename);
   80:            $r->print('<tr><td><input type=radio name=version value="'.
   81:                      $version.'"></td><th>'.$version.'</th><td>'.
   82:                      localtime($rmtime).'</td><td>'.
   83:                      '<a href="'.$urldir.$filename.'.meta" target=cat>'.
   84:                      'Metadata Version '.$version.'</a>');
   85:            if (&Apache::lonnet::fileembstyle($suffix) eq 'ssi') {
   86:                $r->print(
   87:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename=/~'.
   88:                         $uname.$fn.
   89:                         '&versionone=priv&versiontwo='.$version.
   90:                         '">Diffs with Version '.$version.'</a>');
   91:            }
   92:            $r->print('</a></td></tr>');
   93:         }
   94:     }
   95:     closedir(DIR);
   96:     my ($rdev,$rino,$rmode,$rnlink,
   97:         $ruid,$rgid,$rrdev,$rsize,
   98:         $ratime,$rmtime,$rctime,
   99:         $rblksize,$rblocks)=stat($resfn);
  100:     $r->print('<tr><td><input type=radio name=version value="new"></td>'.
  101:               '<th>Current</th><td>'.localtime($rmtime).
  102:            '</td><td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target=cat>'.
  103:               'Metadata current version</a>');           
  104:            if (&Apache::lonnet::fileembstyle($suffix) eq 'ssi') {
  105:                $r->print(
  106:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename=/~'.
  107:                         $uname.$fn.
  108:                         '&versionone=priv'.
  109:                         '">Diffs with current Version</a>');
  110:            }
  111:            $r->print('</td></tr></table><p>'.
  112:            '<font size=+1 color=red>Retrieval of an old version will '.
  113:            'overwrite the file currently in construction space</font><p>'.
  114:            '<input type=submit value="Retrieve version"></form>');
  115: } else {
  116:     $r->print('<h3>No previous versions published.</h3>');
  117: }
  118: }
  119: 
  120: sub phasetwo {
  121:     my ($r,$fn,$uname,$udom)=@_;
  122:     if ($ENV{'form.version'}) {
  123:         my $version=$ENV{'form.version'};
  124: 	if ($version eq 'new') {
  125: 	    $r->print('<h3>Retrieving current (most recent) version</h3>');
  126:         } else {
  127:             $r->print('<h3>Retrieving old version '.$version.'</h3>');
  128:         }
  129:         my $logfile;
  130:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
  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;
  136:         unless ($logfile=Apache::File->new('>>'.$ctarget.'.log')) {
  137: 	  $r->print(
  138:          '<font color=red>No write permission to user directory, FAIL</font>');
  139:         }
  140:         print $logfile 
  141: "\n\n================= Retrieve ".localtime()." ================\n".
  142: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
  143:         $r->print('<p>Copying file: ');
  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.
  153:                   '">Back to '.$fn.'</a></font>'); 
  154:     } else {
  155:        $r->print(
  156:    '<font size=+1 color=red>Please pick a version to retrieve</font><p>');
  157:        &phaseone($r,$fn,$uname,$udom);
  158:     }
  159: }
  160: 
  161: sub handler {
  162: 
  163:   my $r=shift;
  164: 
  165:   my $fn;
  166: 
  167: 
  168: # Get query string for limited number of parameters
  169: 
  170:     map {
  171:        my ($name, $value) = split(/=/,$_);
  172:        $value =~ tr/+/ /;
  173:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  174:        if ($name eq 'filename') {
  175:            unless ($ENV{'form.'.$name}) {
  176:               $ENV{'form.'.$name}=$value;
  177: 	   }
  178:        }
  179:     } (split(/&/,$ENV{'QUERY_STRING'}));
  180: 
  181: 
  182:   if ($ENV{'form.filename'}) {
  183:       $fn=$ENV{'form.filename'};
  184:       $fn=~s/^http\:\/\/[^\/]+//;
  185:   } else {
  186:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  187:          ' unspecified filename for retrieval', $r->filename); 
  188:      return HTTP_NOT_FOUND;
  189:   }
  190: 
  191:   unless ($fn) { 
  192:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  193:          ' trying to retrieve non-existing file', $r->filename); 
  194:      return HTTP_NOT_FOUND;
  195:   } 
  196: 
  197: # ----------------------------------------------------------- Start page output
  198:   my $uname;
  199:   my $udom;
  200: 
  201:   ($uname,$udom)=
  202:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
  203:   unless (($uname) && ($udom)) {
  204:      $r->log_reason($uname.' at '.$udom.
  205:          ' trying to publish file '.$ENV{'form.filename'}.
  206:          ' ('.$fn.') - not authorized', 
  207:          $r->filename); 
  208:      return HTTP_NOT_ACCEPTABLE;
  209:   }
  210: 
  211:   $fn=~s/\/\~(\w+)//;
  212: 
  213:   $r->content_type('text/html');
  214:   $r->send_http_header;
  215: 
  216:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
  217: 
  218:   $r->print(
  219:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  220: 
  221:   
  222:   $r->print('<h1>Retrieve previous versions of <tt>'.$fn.'</tt></h1>');
  223:   
  224:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
  225:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
  226:                '</font></h3>');
  227:   }
  228: 
  229: 
  230:   if ($ENV{'form.phase'} eq 'two') {
  231:       &phasetwo($r,$fn,$uname,$udom);
  232:   } else {
  233:       &phaseone($r,$fn,$uname,$udom);
  234:   }
  235: 
  236:   $r->print('</body></html>');
  237:   return OK;  
  238: }
  239: 
  240: 1;
  241: __END__

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