Diff for /loncom/publisher/lonupload.pm between versions 1.36 and 1.37

version 1.36, 2008/01/15 16:04:38 version 1.37, 2008/06/17 02:20:08
Line 71  sub upfile_store { Line 71  sub upfile_store {
     return $datatoken;      return $datatoken;
 }  }
   
   
 sub phaseone {  sub phaseone {
     my ($r,$fn,$uname,$udom,$mode)=@_;      my ($r,$fn,$uname,$udom,$mode)=@_;
     my $action = '/adm/upload';      my $action = '/adm/upload';
Line 136  sub phaseone { Line 135  sub phaseone {
   
 sub phasetwo {  sub phasetwo {
     my ($r,$tfn,$uname,$udom,$mode)=@_;      my ($r,$tfn,$uname,$udom,$mode)=@_;
       my $output;
     my $action = '/adm/upload';      my $action = '/adm/upload';
     my $returnflag = '';      my $returnflag = '';
     if ($mode eq 'testbank') {      if ($mode eq 'testbank') {
Line 162  sub phasetwo { Line 162  sub phasetwo {
                 my $source=$r->dir_config('lonDaemons').'/tmp/'.$datatoken.'.tmp';                  my $source=$r->dir_config('lonDaemons').'/tmp/'.$datatoken.'.tmp';
                 my $dirpath=$path.'/';                  my $dirpath=$path.'/';
                 $dirpath=~s/\/+/\//g;                  $dirpath=~s/\/+/\//g;
                 $r->print(&mt('Upload cancelled.').'<br /><font size="+2"><a href="'.$dirpath.'">'.                  $output .= &mt('Upload cancelled.').'<br /><font size="+2"><a href="'.$dirpath.'">'.
                           &mt('Back to Directory').'</a></font>');                            &mt('Back to Directory').'</a></font>';
     } elsif ((-e $target) && (!$env{'form.override'})) {      } elsif ((-e $target) && (!$env{'form.override'})) {
  $r->print('<form action="'.$action.'" method="post">'.   $output .= '<form action="'.$action.'" method="post">'.
   &mt('File [_1] exists. Overwrite?','<span class="LC_filename">'.$fn.'</span>').    &mt('File [_1] exists. Overwrite?','<span class="LC_filename">'.$fn.'</span>').
   '<input type="hidden" name="phase" value="two" />'.    '<input type="hidden" name="phase" value="two" />'.
   '<input type="hidden" name="filename" value="'."$url".'" />'.    '<input type="hidden" name="filename" value="'.$url.'" />'.
   '<input type="hidden" name="datatoken" value="'.$datatoken.'" />'.    '<input type="hidden" name="datatoken" value="'.$datatoken.'" />'.
   '<input type="submit" name="override" value="'.&mt('Yes').'" />'.    '<input type="submit" name="override" value="'.&mt('Yes').'" />'.
                           '<input type="submit" name="cancel" value="'.&mt('Cancel').'" />'.                            '<input type="submit" name="cancel" value="'.&mt('Cancel').'" />'.
                           '</form>');                            '</form>';
             } else {              } else {
  my $source=$r->dir_config('lonDaemons').'/tmp/'.$datatoken.'.tmp';   my $source=$r->dir_config('lonDaemons').'/tmp/'.$datatoken.'.tmp';
  my $dirpath=$path.'/';   my $dirpath=$path.'/';
  $dirpath=~s/\/+/\//g;   $dirpath=~s/\/+/\//g;
  # Check for bad extension and disallow upload   # Check for bad extension and disallow upload
  if ($fn=~/\.(\w+)$/ &&                   my $result;
     (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {                  ($result,$returnflag) = &check_extension($fn,$mode,$source,$target,$action,$dirpath,$url);
     $r->print(&mt('File').' <span class="LC_filename">'.$fn.'</span> '.                  $output .= $result;
       &mt('could not be copied.').'<br />'.  
       '<span class="LC_error">'.  
       &mt('The extension on this file is reserved internally by LON-CAPA.').  
       '</span>');  
     $r->print('<br /><font size=+2><a href="'.$dirpath.'">'.  
       &mt('Back to Directory').'</a></font>');  
  } elsif ($fn=~/\.(\w+)$/ &&   
  !defined(&Apache::loncommon::fileembstyle($1))) {  
     $r->print(&mt('File').' <span class="LC_filename">'.$fn.'</span> '.  
       &mt('could not be copied.').'<br />'.  
       '<span class="LC_error">'.  
       &mt('The extension on this file is not recognized by LON-CAPA.').  
       '</span>');  
     $r->print('<br /><font size="+2"><a href="'.$dirpath.'">'.  
       &mt('Back to Directory').'</a></font>');  
  } elsif (-d $target) {  
     $r->print('File <span class="LC_filename">'.$fn.'</span> could not be copied.<br />'.  
       '<span class="LC_error">'.  
       &mt('The target is an existing directory.').  
       '</span><br />');  
     $r->print('<font size="+2"><a href="'.$dirpath.'">'.  
       &mt('Back to Directory').'</a></font>');  
  } elsif (copy($source,$target)) {  
     chmod(0660, $target); # Set permissions to rw-rw---.  
                     if ($mode eq 'testbank' || $mode eq 'imsimport') {  
                         $r->print(&mt("Your file - $fn - was uploaded successfully")."<br /><br />");  
                         $returnflag = 'ok';  
                     } else {  
                         $r->print(&mt('File copied.'));  
         $r->print('<br /><font size="+2"><a href="'.$url.'">'.  
       &mt('View file').'</a></font>');  
         $r->print('<br /><font size="+2"><a href="'.$dirpath.'">'.  
       &mt('Back to Directory').'</a></font><br />');  
                     }  
  } else {  
     $r->print('Failed to copy: '.$!);  
     $r->print('<br /><font size="+2"><a href="'.$path.'">'.  
       &mt('Back to Directory').'</a></font>');  
  }  
     }      }
  } else {   } else {
     $r->print('<span class="LC_error">'.      $output .= '<span class="LC_error">'.
       &mt('Please use browser "Back" button and pick a filename').        &mt('Please use browser "Back" button and pick a filename').
       '</span><br />');        '</span><br />';
  }   }
     } else {      } else {
  $r->print('<span class="LC_error">'.   $output .= '<span class="LC_error">'.
   &mt('Please use browser "Back" button and pick a filename').     &mt('Please use browser "Back" button and pick a filename').
   '</span><br />>');     '</span><br />';
       }
       return ($output,$returnflag);
   }
   
   sub check_extension {
       my ($fn,$mode,$source,$target,$action,$dirpath,$url) = @_;
       my ($result,$returnflag);
       # Check for bad extension and disallow upload
       if ($fn=~/\.(\w+)$/ &&
           (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
           $result .= &mt('File [_1] could not be copied.',
                         '<span class="LC_filename">'.$fn.'</span> ').
                     '<br /><span class="LC_error">'.
                     &mt('The extension on this file is reserved internally by LON-CAPA.').
                     '</span>';
       } elsif ($fn=~/\.(\w+)$/ &&
                !defined(&Apache::loncommon::fileembstyle($1))) {
           $result .= &mt('File [_1] could not be copied.',
                         '<span class="LC_filename">'.$fn.'</span> ').
                     '<br /><span class="LC_error">'.
                     &mt('The extension on this file is not recognized by LON-CAPA.').
                     '</span>';
       } elsif (-d $target) {
           $result .= &mt('File [_1] could not be copied.',
                         '<span class="LC_filename">'.$fn.'</span>').
                     '<br /><span class="LC_error">'.
                     &mt('The target is an existing directory.').
                     '</span>';
       } elsif (copy($source,$target)) {
           chmod(0660, $target); # Set permissions to rw-rw---.
           if ($mode eq 'testbank' || $mode eq 'imsimport') {
               $returnflag = 'ok';
               $result .= &mt('Your file - [_1] - was uploaded successfully',$fn).'<br /><br />';
           } else {
               $result .= &mt('File copied.').'<br />';
           }
           # Check for embedded objects.
           my (%allfiles,%codebase);
           my ($text,$header,$css,$js);
           if (($mode ne 'imsimport') && ($target =~ /\.(htm|html|shtml)$/i)) {
               my (%allfiles,%codebase);
               &Apache::lonnet::extract_embedded_items($target,\%allfiles,\%codebase);
               if (keys(%allfiles) > 0) {
                   my $state = <<STATE;
       <input type="hidden" name="action"      value="upload_embedded" />
       <input type="hidden" name="currentpath" value="$env{'form.currentpath'}" />
       <input type="hidden" name="mode"        value="$mode" />
       <input type="hidden" name="phase"       value="three" />
       <input type="hidden" name="filename" value="$url" />
   STATE
                   $result .= "<h3>".&mt("Reference Warning")."</h3>".
                              "<p>".&mt("Completed upload of the file. This file contained references to other files.")."</p>".
                             "<p>".&mt("Please select the locations from which the referenced files are to be uploaded.")."</p>".
                             &Apache::loncommon::ask_for_embedded_content($action,$state,\%allfiles,\%codebase,
                                         {'error_on_invalid_names'   => 1,
                                          'ignore_remote_references' => 1,});
                   if ($mode eq 'testbank') {
                       $returnflag = 'embedded';
                       $result .=  '<p>'.&mt('Or [_1]continue[_2] the testbank import without these files','<a href="javascript:document.testbankForm.submit();">','</a>').'</p>';
                   }
               }
           }
           if (($mode ne 'imsimport') && ($mode ne 'testbank')) {
               $result .= '<br /><font size="+2"><a href="'.$url.'">'.
                           &mt('View file').'</a></font>';
           }
       } else {
           $result .= &mt('Failed to copy: [_1].',$!);
       }
       if ($mode ne 'imsimport' && $mode ne 'testbank') {
           $result .= '<br /><font size="+2"><a href="'.$dirpath.'">'.
                      &mt('Back to Directory').'</a></font><br />';
       }
       return ($result,$returnflag);
   }
   
   sub phasethree {
       my ($r,$fn,$uname,$udom,$mode) = @_;
       my $result;
       my $dir_root = '/home/'.$uname.'/public_html';
       my $url_root = '/priv/'.$uname;
       my $base = &File::Basename::basename($fn);
       my $path = &File::Basename::dirname($fn);
       $result = &Apache::loncommon::upload_embedded($mode,$path,$uname,$udom,
                                                     $dir_root,$url_root);
       if ($mode ne 'imsimport' && $mode ne 'testbank') {
           $result = '<br /><font size="+2"><a href="'.$url_root.$fn.'">'.
                     &mt('View main file').'</a></font>'.
                     '<br /><font size="+2"><a href="'.$url_root.$path.'">'.
                     &mt('Back to Directory').'</a></font><br />';
     }      }
     return $returnflag;      return $result;
 }  }
   
 # ---------------------------------------------------------------- Main Handler  # ---------------------------------------------------------------- Main Handler
Line 251  sub handler { Line 302  sub handler {
     }      }
   
     unless ($env{'form.phase'} eq 'two') {      unless ($env{'form.phase'} eq 'two') {
  my %body_layout = ('rightmargin'  => "0",  
    'leftmargin'   => "0",  
    'marginwidth'  => "0",  
    'topmargin'    => "0",  
    'marginheight' => "0");  
  my $start_page =   
     &Apache::loncommon::start_page('Importing a Testbank file into LON-CAPA',  
    undef,  
    {'only_body'   => 1,  
     'add_entries' => \%body_layout,  
     'js_ready'    => 1,});  
  my $end_page =   
     &Apache::loncommon::end_page({'js_ready' => 1,});  
   
         $javascript = qq|          $javascript = qq|
 function verifyForm() {  function verifyForm() {
     var mode = document.fileupload.filetype.options[document.fileupload.filetype.selectedIndex].value      var mode = document.fileupload.filetype.options[document.fileupload.filetype.selectedIndex].value
Line 284  function verifyForm() { Line 321  function verifyForm() {
     ($uname,$udom)=      ($uname,$udom)=
  &Apache::loncacc::constructaccess($env{'form.filename'},   &Apache::loncacc::constructaccess($env{'form.filename'},
   $r->dir_config('lonDefDomain'));    $r->dir_config('lonDefDomain'));
   
     unless (($uname) && ($udom)) {      unless (($uname) && ($udom)) {
  $r->log_reason($uname.' at '.$udom.   $r->log_reason($uname.' at '.$udom.
        ' trying to publish file '.$env{'form.filename'}.         ' trying to publish file '.$env{'form.filename'}.
Line 318  function verifyForm() { Line 356  function verifyForm() {
      $javascript));       $javascript));
       
     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {      if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
  $r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.   $r->print('<h3><span class="LC_error">'.&mt('Co-Author').': '.$uname.
   &mt(' at ').$udom.'</font></h3>');    &mt(' at ').$udom.'</span></h3>');
     }      }
   
     if ($env{'form.phase'} eq 'two') {      if ($env{'form.phase'} eq 'three') {
  &phasetwo($r,$fn,$uname,$udom);          my $output = &phasethree($r,$fn,$uname,$udom,'author');
           $r->print($output);
       } elsif ($env{'form.phase'} eq 'two') {
    my ($output,$returnflag) = &phasetwo($r,$fn,$uname,$udom);
           $r->print($output);
     } else {      } else {
  &phaseone($r,$fn,$uname,$udom);   &phaseone($r,$fn,$uname,$udom);
     }      }
Line 377  Start page output Line 419  Start page output
   
 =item *  =item *
   
 output relevant interface phase (phaseone or phasetwo)  output relevant interface phase (phaseone or phasetwo or phasethree)
   
 =item *  =item *
   
 (phase one is to specify upload file; phase two is to handle conditions  (phase one is to specify upload file; phase two is to handle conditions
 subsequent to specification--like overwriting an existing file)  subsequent to specification--like overwriting an existing file; phase three
   is to handle processing of secondary uploads - of embedded objects in an
   html file).
   
 =back  =back
   
Line 401  as overwriting an existing file). Line 445  as overwriting an existing file).
   
 =item *  =item *
   
   phasethree() : Interface for handling secondary uploads of embedded objects
   in an html file.
   
   =item *
   
 upfile_store() : Store contents of uploaded file into temporary space.  Invoked  upfile_store() : Store contents of uploaded file into temporary space.  Invoked
 by phaseone subroutine.  by phaseone subroutine.
   
   = item *
   
   check_extension() : Checks if filename extension is permitted and checks type
    of file - if html file, calls parser to check for embedded objects.
    Invoked by phasetwo subroutine.
   
 =back  =back
   
 =cut  =cut

Removed from v.1.36  
changed lines
  Added in v.1.37


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