File:  [LON-CAPA] / loncom / publisher / lonupload.pm
Revision 1.1: download - view: text, annotated - select for diffs
Thu Apr 5 19:35:15 2001 UTC (23 years, 1 month ago) by www
Branches: MAIN
CVS tags: HEAD
File upload into construction space

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

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