Diff for /loncom/publisher/lonupload.pm between versions 1.1 and 1.8

version 1.1, 2001/04/05 19:35:15 version 1.8, 2001/11/29 21:51:40
Line 1 Line 1
 # The LearningOnline Network with CAPA  # The LearningOnline Network with CAPA
 # Handler to upload files into construction space  # Handler to upload files into construction space
 #  #
   # $Id$
   #
   # Copyright Michigan State University Board of Trustees
   #
   # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   #
   # LON-CAPA is free software; you can redistribute it and/or modify
   # it under the terms of the GNU General Public License as published by
   # the Free Software Foundation; either version 2 of the License, or
   # (at your option) any later version.
   #
   # LON-CAPA is distributed in the hope that it will be useful,
   # but WITHOUT ANY WARRANTY; without even the implied warranty of
   # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   # GNU General Public License for more details.
   #
   # You should have received a copy of the GNU General Public License
   # along with LON-CAPA; if not, write to the Free Software
   # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   #
   # /home/httpd/html/adm/gpl.txt
   #
   # http://www.lon-capa.org/
   #
 # (Handler to retrieve an old version of a file  # (Handler to retrieve an old version of a file
 #  #
 # (Publication Handler  # (Publication Handler
Line 15 Line 39
 #  #
 # 03/31,04/03 Gerd Kortemeyer)  # 03/31,04/03 Gerd Kortemeyer)
 #  #
 # 04/05 Gerd Kortemeyer  # 04/05,04/09,05/25,06/23,06/24,08/22 Gerd Kortemeyer
   # 11/29 Matthew Hall
   
 package Apache::lonupload;  package Apache::lonupload;
   
Line 23  use strict; Line 48  use strict;
 use Apache::File;  use Apache::File;
 use File::Copy;  use File::Copy;
 use Apache::Constants qw(:common :http :methods);  use Apache::Constants qw(:common :http :methods);
   use Apache::loncacc;
   use Apache::lonnet;
   
 sub phaseone {  sub upfile_store {
     my ($r,$fn,$uname,$udom)=@_;      my $r=shift;
     my $docroot=$r->dir_config('lonDocRoot');  
       my $fname=$ENV{'form.upfile.filename'};
     my $urldir='/res/'.$udom.'/'.$uname.$fn;      $fname=~s/\W//g;
     $urldir=~s/\/[^\/]+$/\//;      
       chop($ENV{'form.upfile'});
     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;  
     my $resdir=$resfn;  
     $resdir=~s/\/[^\/]+$/\//;  
   
     $fn=~/^\/(.+)\.(\w+)$/;  
     my $main=$1;  
     my $suffix=$2;  
       
     $r->print('<form action=/adm/retrieve method=post>'.      my $datatoken=$ENV{'user.name'}.'_'.$ENV{'user.domain'}.
       '<input type=hidden name=filename value="'.$fn.'">'.    '_upload_'.$fname.'_'.time.'_'.$$;
               '<input type=hidden name=phase value=two>'.      {
               '<table border=2><tr><th>Select</th><th>Version</th>'.         my $fh=Apache::File->new('>'.$r->dir_config('lonDaemons').
               '<th>Became this version on ...</th>'.                                     '/tmp/'.$datatoken.'.tmp');
               '<th>Metadata</th></tr>');         print $fh $ENV{'form.upfile'};
     my $filename;  
     opendir(DIR,$resdir);  
     while ($filename=readdir(DIR)) {  
         if ($filename=~/^$main\.(\d+)\.$suffix$/) {  
    my $version=$1;  
            my ($rdev,$rino,$rmode,$rnlink,  
                 $ruid,$rgid,$rrdev,$rsize,  
                 $ratime,$rmtime,$rctime,  
                 $rblksize,$rblocks)=stat($resdir.'/'.$filename);  
            $r->print('<tr><td><input type=radio name=version value="'.  
                      $version.'"></td><th>'.$version.'</th><td>'.  
                      localtime($rmtime).'</td><td>'.  
                      '<a href="'.$urldir.$filename.'.meta" target=cat>'.  
                      'Metadata Version '.$version.'</a></td></tr>');  
         }  
     }      }
     closedir(DIR);      return $datatoken;
     my ($rdev,$rino,$rmode,$rnlink,  
         $ruid,$rgid,$rrdev,$rsize,  
         $ratime,$rmtime,$rctime,  
         $rblksize,$rblocks)=stat($resfn);  
     $r->print('<tr><td><input type=radio name=version value="new"></td>'.  
               '<th>Current</th><td>'.localtime($rmtime).  
            '</td><td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target=cat>'.  
               'Metadata current version</a></td></tr></table><p>'.  
            '<font size=+1 color=red>Retrieval of an old version will '.  
            'overwrite the file currently in construction space</font><p>'.  
            '<input type=submit value="Retrieve version"></form>');  
 }  }
   
 sub phasetwo {  
     my ($r,$fn,$uname,$udom)=@_;  sub phaseone {
     if ($ENV{'form.version'}) {     my ($r,$fn,$uname,$udom)=@_;
         my $version=$ENV{'form.version'};     $ENV{'form.upfile.filename'}=~s/\\/\//g;
  if ($version eq 'new') {     $ENV{'form.upfile.filename'}=~s/^.*\/([^\/]+)$/$1/;
     $r->print('<h3>Retrieving current (most recent) version</h3>');     if ($ENV{'form.upfile.filename'}) {
         } else {      $fn=~s/\/[^\/]+$//;
             $r->print('<h3>Retrieving old version '.$version.'</h3>');      $fn=~s/([^\/])$/$1\//;
         }      $fn.=$ENV{'form.upfile.filename'};
         my $logfile;      $fn=~s/^\///;
         my $ctarget='/home/'.$uname.'/public_html'.$fn;      $fn=~s/(\/)+/\//g;
         my $vfn=$fn;  
         if ($version ne 'new') {      if (($fn) && ($fn!~/\/$/)) {
     $vfn=~s/\.(\w+)$/\.$version\.$1/;        $r->print(
         }   '<form action=/adm/upload method=post>'.
         my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;   '<input type=hidden name=phase value=two>'.
         unless ($logfile=Apache::File->new('>>'.$ctarget.'.log')) {   '<input type=hidden name=datatoken value="'.&upfile_store.'">'.
    'Store uploaded file as '.
    '<input type=text size=50 name=filename value="/priv/'.
     $uname.'/'.$fn.'"><br>'.
    '<input type=submit value="Store"></form>');
         # Check for bad extension
         if ($fn=~/\.(\w+)$/ && 
     (&Apache::lonnet::fileembstyle($1) eq 'hdn')) {
   $r->print(    $r->print(
          '<font color=red>No write permission to user directory, FAIL</font>');   '<font color=red>'.
         }   'The extension on this file, "'.$1.
         print $logfile    '", is reserved internally by LON-CAPA. <br \>'.
 "\n\n================= Retrieve ".localtime()." ================\n".   'Please change the extension.'.
 "Version: $version\nSource: $csource\nTarget: $ctarget\n";   '</font>');
         $r->print('<p>Copying file: ');        }  
         if (copy($csource,$ctarget)) {    } else {
     $r->print('ok<p>');        $r->print('<font color=red>Illegal filename.</font>');
             print $logfile "Copied sucessfully.\n\n";    }
         } else {   } else {
             my $error=$!;       $r->print('<font color=red>No upload file specified.</font>');
     $r->print('fail, '.$error.'<p>');   }
             print $logfile "Copy failed: $error\n\n";  }
         }  
         $r->print('<font size=+2><a href="/priv/'.$uname.$fn.  sub phasetwo {
                   '">Back to '.$fn.'</a></font>');      my ($r,$fn,$uname,$udom)=@_;
      if ($fn=~/^\/priv\/$uname\//) { 
       my $tfn=$fn;
       $tfn=~s/^\/(\~|priv)\/(\w+)//;
       my $target='/home/'.$uname.'/public_html'.$tfn;
       my $datatoken=$ENV{'form.datatoken'};
       if (($fn) && ($datatoken)) {
    if ((-e $target) && ($ENV{'form.override'} ne 'Yes')) {
              $r->print(
    '<form action=/adm/upload method=post>'.
    'File <tt>'.$fn.'</tt> exists. Overwrite? '.
    '<input type=hidden name=phase value=two>'.
    '<input type=hidden name=filename value="'.$fn.'">'.
    '<input type=hidden name=datatoken value="'.$datatoken.'">'.
    '<input type=submit name=override value="Yes"></form>');
          } else {
              my $source=$r->dir_config('lonDaemons').
                                '/tmp/'.$datatoken.'.tmp';
              # Check for bad extension
      if ($fn=~/\.(\w+)$/ && 
          (&Apache::lonnet::fileembstyle($1) eq 'hdn')) {
          $r->print(
    'File <tt>'.$fn.'</tt> could not be copied.<br />'.
    '<font color=red>'.
    'The extension on this file is reserved internally by LON-CAPA.'.
    '</font>');
      } elsif (copy($source,$target)) {
         $r->print('File copied.');
                 $r->print('<p><font size=+2><a href="'.$fn.
                           '">View file</a></font>');
      } else {
                 $r->print('Failed to copy: '.$!);
      }
          }
     } else {      } else {
        $r->print(         $r->print(
    '<font size=+1 color=red>Please pick a version to retrieve</font><p>');     '<font size=+1 color=red>Please pick a filename</font><p>');
        &phaseone($r,$fn,$uname,$udom);         &phaseone($r,$fn,$uname,$udom);
     }      }
     } else {
       $r->print(
      '<font size=+1 color=red>Please pick a filename</font><p>');
       &phaseone($r,$fn,$uname,$udom);
     }
 }  }
   
 sub handler {  sub handler {
   
   my $r=shift;    my $r=shift;
   
     my $uname;
     my $udom;
   
     ($uname,$udom)=
       &Apache::loncacc::constructaccess(
    $ENV{'form.filename'},$r->dir_config('lonDefDomain'));
     unless (($uname) && ($udom)) {
        $r->log_reason($uname.' at '.$udom.
            ' trying to publish file '.$ENV{'form.filename'}.
            ' - not authorized', 
            $r->filename); 
        return HTTP_NOT_ACCEPTABLE;
     }
   
   my $fn;    my $fn;
   
   if ($ENV{'form.filename'}) {    if ($ENV{'form.filename'}) {
       $fn=$ENV{'form.filename'};        $fn=$ENV{'form.filename'};
       $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)//;        $fn=~s/^http\:\/\/[^\/]+\/(\~|priv\/)(\w+)//;
   } else {    } else {
      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.       $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
          ' unspecified filename for retrieval', $r->filename);            ' unspecified filename for upload', $r->filename); 
      return HTTP_NOT_FOUND;       return HTTP_NOT_FOUND;
   }    }
   
   unless ($fn) {   
      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.  
          ' trying to retrieve non-existing file', $r->filename);   
      return HTTP_NOT_FOUND;  
   }   
   
 # ----------------------------------------------------------- Start page output  # ----------------------------------------------------------- Start page output
   
   my $uname=$ENV{'user.name'};  
   my $udom=$ENV{'user.domain'};  
   
   $r->content_type('text/html');    $r->content_type('text/html');
   $r->send_http_header;    $r->send_http_header;
Line 151  sub handler { Line 196  sub handler {
    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');     '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
   
       
   $r->print('<h1>Retrieve previous versions of <tt>'.$fn.'</tt></h1>');    $r->print('<h1>Upload file to Construction Space</h1>');
     
     if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
             $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
                  '</font></h3>');
     }
   
   
   if ($ENV{'form.phase'} eq 'two') {    if ($ENV{'form.phase'} eq 'two') {
       &phasetwo($r,$fn,$uname,$udom);        &phasetwo($r,$fn,$uname,$udom);
Line 162  sub handler { Line 213  sub handler {
   $r->print('</body></html>');    $r->print('</body></html>');
   return OK;      return OK;  
 }  }
   
   1;
   __END__

Removed from v.1.1  
changed lines
  Added in v.1.8


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