Diff for /loncom/interface/loncommon.pm between versions 1.660 and 1.661

version 1.660, 2008/06/09 22:34:55 version 1.661, 2008/06/16 23:34:12
Line 7041  sub ask_for_embedded_content { Line 7041  sub ask_for_embedded_content {
    <form name="upload_embedded" action="'.$actionurl.'"     <form name="upload_embedded" action="'.$actionurl.'"
                   method="post" enctype="multipart/form-data">';                    method="post" enctype="multipart/form-data">';
     $upload_output .= $state;      $upload_output .= $state;
     $upload_output .= '<b>Upload embedded files</b>:<br />'.start_data_table();      $upload_output .= '<b>Upload embedded files</b>:<br />'.&start_data_table();
   
     my $num = 0;      my $num = 0;
     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%{$allfiles})) {      foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%{$allfiles})) {
Line 7056  sub ask_for_embedded_content { Line 7056  sub ask_for_embedded_content {
             $upload_output.='<span class="LC_warning">'.&mt("Invalid characters").'</span>';              $upload_output.='<span class="LC_warning">'.&mt("Invalid characters").'</span>';
   
         } else {          } else {
   
             $upload_output .='              $upload_output .='
            <input name="embedded_item_'.$num.'" type="file" value="bob" />             <input name="embedded_item_'.$num.'" type="file" value="" />
            <input name="embedded_orig_'.$num.'" type="hidden" value="'.&escape($embed_file).'" />';             <input name="embedded_orig_'.$num.'" type="hidden" value="'.&escape($embed_file).'" />';
             my $attrib = join(':',@{$$allfiles{$embed_file}});              my $attrib = join(':',@{$$allfiles{$embed_file}});
             $upload_output .=              $upload_output .=
Line 7083  sub ask_for_embedded_content { Line 7082  sub ask_for_embedded_content {
     return $upload_output;      return $upload_output;
 }  }
   
   sub upload_embedded {
       my ($context,$dirpath,$uname,$udom,$dir_root,$url_root,$group,$disk_quota,
           $current_disk_usage) = @_;
       my $output;
       for (my $i=0; $i<$env{'form.number_embedded_items'}; $i++) {
           next if (!exists($env{'form.embedded_item_'.$i.'.filename'}));
           my $orig_uploaded_filename =
               $env{'form.embedded_item_'.$i.'.filename'};
   
           $env{'form.embedded_orig_'.$i} =
               &unescape($env{'form.embedded_orig_'.$i});
           my ($path,$fname) =
               ($env{'form.embedded_orig_'.$i} =~ m{(.*/)([^/]*)});
           # no path, whole string is fname
           if (!$fname) { $fname = $env{'form.embedded_orig_'.$i} };
   
           $path = $env{'form.currentpath'}.$path;
           $fname = &Apache::lonnet::clean_filename($fname);
           # See if there is anything left
           next if ($fname eq '');
   
           # Check if file already exists as a file or directory.
           my ($state,$msg);
           if ($context eq 'portfolio') {
               my $port_path = $dirpath;
               if ($group ne '') {
                   $port_path = "groups/$group/$port_path";
               }
               ($state,$msg) = &check_for_upload($path,$fname,$group,'embedded_item_'.$i,
                                                 $dir_root,$port_path,$disk_quota,
                                                 $current_disk_usage,$uname,$udom);
               if ($state eq 'will_exceed_quota'
                   || $state eq 'file_locked'
                   || $state eq 'file_exists' ) {
                   $output .= $msg;
                   next;
               }
           } elsif (($context eq 'author') || ($context eq 'testbank')) {
               ($state,$msg) = &check_for_existing($path,$fname,'embedded_item_'.$i);
               if ($state eq 'exists') {
                   $output .= $msg;
                   next;
               }
           }
           # Check if extension is valid
           if (($fname =~ /\.(\w+)$/) &&
               (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
               $output .= &mt('Invalid file extension ([_1]) - reserved for LONCAPA use - rename the file with a different extension and re-upload. ',$1);
               next;
           } elsif (($fname =~ /\.(\w+)$/) &&
                    (!defined(&Apache::loncommon::fileembstyle($1)))) {
               $output .= &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
               next;
           } elsif ($fname=~/\.(\d+)\.(\w+)$/) {
               $output .= &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
               next;
           }
   
           $env{'form.embedded_item_'.$i.'.filename'}=$fname;
           if ($context eq 'portfolio') {
               my $result=
                   &Apache::lonnet::userfileupload('embedded_item_'.$i,'',
                                                   $dirpath.$path);
               if ($result !~ m|^/uploaded/|) {
                   $output .= '<span class="LC_error">'
                         .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
                              ,$result,$orig_uploaded_filename,$env{'form.embedded_orig_'.$i})
                         .'</span><br />';
                   next;
               } else {
                   $output .= '<p>'.&mt('Uploaded [_1]','<span class="LC_filename">'.
                              $path.$fname.'</span>').'</p>';     
               }
           } else {
   # Save the file
               my $target = $env{'form.embedded_item_'.$i};
               my $fullpath = $dir_root.$dirpath.'/'.$path;
               my $dest = $fullpath.$fname;
               my $url = $url_root.$dirpath.'/'.$path.$fname;
               my @parts=split(/\//,$fullpath);
               my $count;
               my $filepath = $dir_root;
               for ($count=4;$count<=$#parts;$count++) {
                   $filepath .= "/$parts[$count]";
                   if ((-e $filepath)!=1) {
                       mkdir($filepath,0770);
                   }
               }
               my $fh;
               if (!open($fh,'>'.$dest)) {
                   &Apache::lonnet::logthis('Failed to create '.$dest);
                   $output .= '<span class="LC_error">'.
                              &mt('An error occurred while trying to upload [_1] for embedded element [_2].',$orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
                              '</span><br />';
               } else {
                   if (!print $fh $env{'form.embedded_item_'.$i}) {
                       &Apache::lonnet::logthis('Failed to write to '.$dest);
                       $output .= '<span class="LC_error">'.
                                 &mt('An error occurred while writing the file [_1] for embedded element [_2].',$orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
                                 '</span><br />';
                   } else {
                       if ($context eq 'testbank') {
                           $output .= &mt('Embedded file uploaded successfully:').
                                      '&nbsp;<a href="'.$url.'">'.
                                      $orig_uploaded_filename.'</a><br />';
                       } else {
                           $output .= '<font size="+2">'.
                                      &mt('View embedded file: [_1]','<a href="'.$url.'">'.
                                      $orig_uploaded_filename.'</a>').'</font><br />';
                       }
                   }
                   close($fh);
               }
           }
       }
       return $output;
   }
   
   sub check_for_existing {
       my ($path,$fname,$element) = @_;
       my ($state,$msg);
       if (-d $path.'/'.$fname) {
           $state = 'exists';
           $msg = &mt('Unable to upload [_1]. A directory by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
       } elsif (-e $path.'/'.$fname) {
           $state = 'exists';
           $msg = &mt('Unable to upload [_1]. A file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
       }
       if ($state eq 'exists') {
           $msg = '<span class="LC_error">'.$msg.'</span><br />';
       }
       return ($state,$msg);
   }
   
   sub check_for_upload {
       my ($path,$fname,$group,$element,$portfolio_root,$port_path,
           $disk_quota,$current_disk_usage,$uname,$udom) = @_;
       my $filesize = (length($env{'form.'.$element})) / 1000; #express in k (1024?)
       my $getpropath = 1;
       my @dir_list = &Apache::lonnet::dirlist($portfolio_root.$path,$udom,$uname,
                                               $getpropath);
       my $found_file = 0;
       my $locked_file = 0;
       foreach my $line (@dir_list) {
           my ($file_name)=split(/\&/,$line,2);
           if ($file_name eq $fname){
               $file_name = $path.$file_name;
               if ($group ne '') {
                   $file_name = $group.$file_name;
               }
               $found_file = 1;
               if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
                   $locked_file = 1;
               }
           }
       }
       my $getpropath = 1;
       if (($current_disk_usage + $filesize) > $disk_quota){
           my $msg = '<span class="LC_error">'.
                   &mt('Unable to upload [_1]. (size = [_2] kilobytes). Disk quota will be exceeded.','<span class="LC_filename">'.$fname.'</span>',$filesize).'</span>'.
                     '<br />'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',$disk_quota,$current_disk_usage);
           return ('will_exceed_quota',$msg);
       } elsif ($found_file) {
           if ($locked_file) {
               my $msg = '<span class="LC_error">';
               $msg .= &mt('Unable to upload [_1]. A locked file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>','<span class="LC_filename">'.$port_path.$env{'form.currentpath'}.'</span>');
               $msg .= '</span><br />';
               $msg .= &mt('You will be able to rename or delete existing [_1] after a grade has been assigned.','<span class="LC_filename">'.$fname.'</span>');
               return ('file_locked',$msg);
           } else {
               my $msg = '<span class="LC_error">';
               $msg .= &mt('Unable to upload [_1]. A file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$port_path.$env{'form.currentpath'});
               $msg .= '</span>';
               $msg .= '<br />';
               $msg .= &mt('To upload, rename or delete existing [_1] in [_2].','<span class="LC_filename">'.$fname.'</span>', $port_path.$env{'form.currentpath'});
               return ('file_exists',$msg);
           }
       }
   }
   
   
 =pod  =pod
   

Removed from v.1.660  
changed lines
  Added in v.1.661


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