File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.14: download - view: text, annotated - select for diffs
Mon Aug 20 13:18:41 2001 UTC (22 years, 10 months ago) by www
Branches: MAIN
CVS tags: stable_2001_fall, HEAD
Can do retrieve directly from lonpubdir in case original file damaged

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

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