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

1.1       www         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: #
1.8       www        14: # 03/31,04/03,05/02 Gerd Kortemeyer
1.1       www        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);
1.10    ! www        22: use Apache::loncacc;
1.1       www        23: 
1.2       www        24: sub phaseone {
                     25:     my ($r,$fn,$uname,$udom)=@_;
                     26:     my $docroot=$r->dir_config('lonDocRoot');
                     27: 
1.3       www        28:     my $urldir='/res/'.$udom.'/'.$uname.$fn;
                     29:     $urldir=~s/\/[^\/]+$/\//;
                     30: 
                     31:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
                     32:     my $resdir=$resfn;
1.2       www        33:     $resdir=~s/\/[^\/]+$/\//;
                     34: 
1.6       www        35:     $fn=~/\/([^\/]+)\.(\w+)$/;
1.2       www        36:     my $main=$1;
                     37:     my $suffix=$2;
1.6       www        38: 
                     39:     if (-e $resfn) {  
1.3       www        40:     $r->print('<form action=/adm/retrieve method=post>'.
                     41: 	      '<input type=hidden name=filename value="'.$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>');
1.2       www        46:     my $filename;
                     47:     opendir(DIR,$resdir);
                     48:     while ($filename=readdir(DIR)) {
                     49:         if ($filename=~/^$main\.(\d+)\.$suffix$/) {
1.3       www        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>'.
1.9       www        59:                      'Metadata Version '.$version.'</a>');
1.8       www        60:            if (&Apache::lonnet::fileembstyle($suffix) eq 'ssi') {
                     61:                $r->print(
                     62:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename='.$fn.
                     63:                         '&versionone=priv&versiontwo='.$version.
                     64:                         '">Diffs with Version '.$version.'</a>');
                     65:            }
                     66:            $r->print('</a></td></tr>');
1.2       www        67:         }
                     68:     }
                     69:     closedir(DIR);
1.3       www        70:     my ($rdev,$rino,$rmode,$rnlink,
                     71:         $ruid,$rgid,$rrdev,$rsize,
                     72:         $ratime,$rmtime,$rctime,
                     73:         $rblksize,$rblocks)=stat($resfn);
                     74:     $r->print('<tr><td><input type=radio name=version value="new"></td>'.
                     75:               '<th>Current</th><td>'.localtime($rmtime).
                     76:            '</td><td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target=cat>'.
1.9       www        77:               'Metadata current version</a>');           
                     78:            if (&Apache::lonnet::fileembstyle($suffix) eq 'ssi') {
                     79:                $r->print(
                     80:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename='.$fn.
                     81:                         '&versionone=priv'.
                     82:                         '">Diffs with current Version</a>');
                     83:            }
                     84:            $r->print('</td></tr></table><p>'.
1.4       www        85:            '<font size=+1 color=red>Retrieval of an old version will '.
                     86:            'overwrite the file currently in construction space</font><p>'.
1.3       www        87:            '<input type=submit value="Retrieve version"></form>');
1.6       www        88: } else {
                     89:     $r->print('<h3>No previous versions published.</h3>');
                     90: }
1.2       www        91: }
1.1       www        92: 
1.4       www        93: sub phasetwo {
                     94:     my ($r,$fn,$uname,$udom)=@_;
                     95:     if ($ENV{'form.version'}) {
                     96:         my $version=$ENV{'form.version'};
                     97: 	if ($version eq 'new') {
                     98: 	    $r->print('<h3>Retrieving current (most recent) version</h3>');
                     99:         } else {
                    100:             $r->print('<h3>Retrieving old version '.$version.'</h3>');
                    101:         }
                    102:         my $logfile;
                    103:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
1.5       www       104:         my $vfn=$fn;
                    105:         if ($version ne 'new') {
                    106: 	    $vfn=~s/\.(\w+)$/\.$version\.$1/;
                    107:         }
                    108:         my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;
1.4       www       109:         unless ($logfile=Apache::File->new('>>'.$ctarget.'.log')) {
                    110: 	  $r->print(
                    111:          '<font color=red>No write permission to user directory, FAIL</font>');
                    112:         }
                    113:         print $logfile 
                    114: "\n\n================= Retrieve ".localtime()." ================\n".
1.5       www       115: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
                    116:         $r->print('<p>Copying file: ');
                    117:         if (copy($csource,$ctarget)) {
                    118: 	    $r->print('ok<p>');
                    119:             print $logfile "Copied sucessfully.\n\n";
                    120:         } else {
                    121:             my $error=$!;
                    122: 	    $r->print('fail, '.$error.'<p>');
                    123:             print $logfile "Copy failed: $error\n\n";
                    124:         }
                    125:         $r->print('<font size=+2><a href="/priv/'.$uname.$fn.
                    126:                   '">Back to '.$fn.'</a></font>'); 
1.4       www       127:     } else {
                    128:        $r->print(
                    129:    '<font size=+1 color=red>Please pick a version to retrieve</font><p>');
                    130:        &phaseone($r,$fn,$uname,$udom);
                    131:     }
                    132: }
                    133: 
1.1       www       134: sub handler {
                    135: 
                    136:   my $r=shift;
                    137: 
                    138:   my $fn;
                    139: 
                    140:   if ($ENV{'form.filename'}) {
                    141:       $fn=$ENV{'form.filename'};
1.10    ! www       142:       $fn=~s/^http\:\/\/[^\/]+//;
1.1       www       143:   } else {
1.2       www       144:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                    145:          ' unspecified filename for retrieval', $r->filename); 
                    146:      return HTTP_NOT_FOUND;
1.1       www       147:   }
                    148: 
                    149:   unless ($fn) { 
                    150:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1.2       www       151:          ' trying to retrieve non-existing file', $r->filename); 
1.1       www       152:      return HTTP_NOT_FOUND;
                    153:   } 
                    154: 
                    155: # ----------------------------------------------------------- Start page output
1.10    ! www       156:   my $uname;
        !           157:   my $udom;
1.1       www       158: 
1.10    ! www       159:   unless (($uname,$udom)=
        !           160:     &Apache::loncacc::constructaccess(
        !           161:              $fn,$r->dir_config('lonDefDomain'))) {
        !           162:      $r->log_reason($uname.' at '.$udom.
        !           163:          ' trying to publish file '.$ENV{'form.filename'}.
        !           164:          ' ('.$fn.') - not authorized', 
        !           165:          $r->filename); 
        !           166:      return HTTP_NOT_ACCEPTABLE;
        !           167:   }
        !           168: 
        !           169:   $fn=~s/\/\~(\w+)//;
1.1       www       170: 
                    171:   $r->content_type('text/html');
                    172:   $r->send_http_header;
                    173: 
                    174:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
                    175: 
                    176:   $r->print(
                    177:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
                    178: 
                    179:   
1.2       www       180:   $r->print('<h1>Retrieve previous versions of <tt>'.$fn.'</tt></h1>');
1.10    ! www       181:   
        !           182:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
        !           183:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
        !           184:                '</font></h3>');
        !           185:   }
        !           186: 
1.1       www       187: 
1.2       www       188:   if ($ENV{'form.phase'} eq 'two') {
1.4       www       189:       &phasetwo($r,$fn,$uname,$udom);
1.2       www       190:   } else {
                    191:       &phaseone($r,$fn,$uname,$udom);
                    192:   }
1.1       www       193: 
                    194:   $r->print('</body></html>');
                    195:   return OK;  
                    196: }
1.7       www       197: 
                    198: 1;
                    199: __END__

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