[LON-CAPA-cvs] cvs: loncom /publisher lonupload.pm

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Tue, 17 Jun 2008 02:20:09 -0000


This is a MIME encoded message

--raeburn1213669209
Content-Type: text/plain

raeburn		Mon Jun 16 22:20:09 2008 EDT

  Modified files:              
    /loncom/publisher	lonupload.pm 
  Log:
  - When web pages are uploaded to CSTR, user will be prompted to upload any embedded objects (e.g., image files).    
  - Checking for existing files and valid extension for files uploaded to CSTR moved to &check_extension() subroutine.
  - &phasethree() added to handle uploading of embedded objects.
  
  
--raeburn1213669209
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20080616222009.txt"

Index: loncom/publisher/lonupload.pm
diff -u loncom/publisher/lonupload.pm:1.36 loncom/publisher/lonupload.pm:1.37
--- loncom/publisher/lonupload.pm:1.36	Tue Jan 15 11:04:38 2008
+++ loncom/publisher/lonupload.pm	Mon Jun 16 22:20:08 2008
@@ -2,7 +2,7 @@
 # The LearningOnline Network with CAPA
 # Handler to upload files into construction space
 #
-# $Id: lonupload.pm,v 1.36 2008/01/15 16:04:38 www Exp $
+# $Id: lonupload.pm,v 1.37 2008/06/17 02:20:08 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -71,7 +71,6 @@
     return $datatoken;
 }
 
-
 sub phaseone {
     my ($r,$fn,$uname,$udom,$mode)=@_;
     my $action = '/adm/upload';
@@ -136,6 +135,7 @@
 
 sub phasetwo {
     my ($r,$tfn,$uname,$udom,$mode)=@_;
+    my $output;
     my $action = '/adm/upload';
     my $returnflag = '';
     if ($mode eq 'testbank') {
@@ -162,76 +162,127 @@
                 my $source=$r->dir_config('lonDaemons').'/tmp/'.$datatoken.'.tmp';
                 my $dirpath=$path.'/';
                 $dirpath=~s/\/+/\//g;
-                $r->print(&mt('Upload cancelled.').'<br /><font size="+2"><a href="'.$dirpath.'">'.
-                          &mt('Back to Directory').'</a></font>');
+                $output .= &mt('Upload cancelled.').'<br /><font size="+2"><a href="'.$dirpath.'">'.
+                          &mt('Back to Directory').'</a></font>';
 	    } 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>').
 			  '<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="submit" name="override" value="'.&mt('Yes').'" />'.
                           '<input type="submit" name="cancel" value="'.&mt('Cancel').'" />'.
-                          '</form>');
+                          '</form>';
             } else {
 		my $source=$r->dir_config('lonDaemons').'/tmp/'.$datatoken.'.tmp';
 		my $dirpath=$path.'/';
 		$dirpath=~s/\/+/\//g;
 		# Check for bad extension and disallow upload
-		if ($fn=~/\.(\w+)$/ && 
-		    (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
-		    $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 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>');
-		}
+                my $result;
+                ($result,$returnflag) = &check_extension($fn,$mode,$source,$target,$action,$dirpath,$url);
+                $output .= $result;
 	    }
 	} else {
-	    $r->print('<span class="LC_error">'.
+	    $output .= '<span class="LC_error">'.
 		      &mt('Please use browser "Back" button and pick a filename').
-		      '</span><br />');
+		      '</span><br />';
 	}
     } else {
-	$r->print('<span class="LC_error">'.
-		  &mt('Please use browser "Back" button and pick a filename').
-		  '</span><br />>');
+	$output .= '<span class="LC_error">'.
+		   &mt('Please use browser "Back" button and pick a filename').
+		   '</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
@@ -251,20 +302,6 @@
     }
 
     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|
 function verifyForm() {
     var mode = document.fileupload.filetype.options[document.fileupload.filetype.selectedIndex].value
@@ -284,6 +321,7 @@
     ($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'}.
@@ -318,12 +356,16 @@
 					     $javascript));
   
     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
-	$r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.
-		  &mt(' at ').$udom.'</font></h3>');
+	$r->print('<h3><span class="LC_error">'.&mt('Co-Author').': '.$uname.
+		  &mt(' at ').$udom.'</span></h3>');
     }
 
-    if ($env{'form.phase'} eq 'two') {
-	&phasetwo($r,$fn,$uname,$udom);
+    if ($env{'form.phase'} eq 'three') {
+        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 {
 	&phaseone($r,$fn,$uname,$udom);
     }
@@ -377,12 +419,14 @@
 
 =item *
 
-output relevant interface phase (phaseone or phasetwo)
+output relevant interface phase (phaseone or phasetwo or phasethree)
 
 =item *
 
 (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
 
@@ -401,9 +445,20 @@
 
 =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
 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
 
 =cut

--raeburn1213669209--