Diff for /loncom/homework/daxesave.pm between versions 1.1 and 1.6

version 1.1, 2015/12/03 20:40:27 version 1.6, 2023/08/23 20:43:34
Line 28 Line 28
 ###  ###
   
 package Apache::daxesave;  package Apache::daxesave;
   use strict;
   
 use Apache::Constants;  use Apache::Constants qw(:common);
 use Apache::lonnet;  use Apache::lonnet;
 use Try::Tiny;  use Try::Tiny;
 use File::Copy;  use File::Copy;
Line 41  use Apache::xml_to_loncapa; Line 42  use Apache::xml_to_loncapa;
 sub handler {  sub handler {
     my $request = shift;      my $request = shift;
           
     my $path = $env{'form.path'}; # should be in the form "/daxeopen/priv/..."      $request->content_type('text/plain');
       
       # path should be in the form "/daxeopen/priv/..."
       # or ^/daxeopen/uploaded/[^/]+/[^/]+/.*html?$
       my $path = $env{'form.path'};
     $path =~ s/^\/daxeopen//;      $path =~ s/^\/daxeopen//;
           
     my $allowed;      my $allowed = 0;
     my ($ownername,$ownerdom,$ownerhome) =       if ($path =~ /^\/priv/) {
         &Apache::lonnet::constructaccess($path, 'setpriv');          my ($ownername,$ownerdom,$ownerhome) = 
     if (($ownername ne '') && ($ownerdom ne '') && ($ownerhome ne '')) {              &Apache::lonnet::constructaccess($path, 'setpriv');
         unless ($ownerhome eq 'no_host') {          if (($ownername ne '') && ($ownerdom ne '') && ($ownerhome ne '')) {
             my @hosts = &Apache::lonnet::current_machine_ids();              unless ($ownerhome eq 'no_host') {
             if (grep(/^\Q$ownerhome\E$/,@hosts)) {                  my @hosts = &Apache::lonnet::current_machine_ids();
                 $allowed = 1;                  if (grep(/^\Q$ownerhome\E$/,@hosts)) {
                       $allowed = 1;
                   }
               }
           }
       } elsif ($path =~ m|^/uploaded/[^/]+/[^/]+/|) {
           if ($env{'user.name'} ne '' && $env{'user.domain'} ne '' && $env{'request.course.id'}) {
               $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
               $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
               if ($path =~ m|^/uploaded/\Q$cdom\E/\Q$cnum\E/| && $path !~ /\.\./) {
                   if (&Apache::lonnet::allowed('mdc', $env{'request.course.id'})) {
                       $allowed = 1;
                   }
             }              }
         }          }
     }      }
     unless ($allowed) {      unless ($allowed) {
         $request->log_reason("Unauthorized path: $path", $path);           $request->log_reason("Unauthorized path: $path", $path);
         return HTTP_NOT_ACCEPTABLE;          $request->print("error\nUnauthorized path: $path");
           $request->status(403);
           return OK;
     }      }
   
     my $newpath = &Apache::lonnet::filelocation('', $path);      my $newpath = &Apache::lonnet::filelocation('', $path);
   
     my $contents = $env{'form.file'};      my $contents = $env{'form.file'};
           
     try {      my $mode;
         $contents = &Apache::xml_to_loncapa::convert_file($contents);      if ($path =~ /\.(task|problem|exam|quiz|assess|survey|library|xml|html|htm|xhtml|xhtm)$/) {
     } catch {          try {
         $request->print("error\nconvert failed for $path: $_");              $contents = &Apache::xml_to_loncapa::convert_file($contents);
         return OK;          } catch {
     };              $request->print("error\nconvert failed for $path: $_");
               return OK;
           };
           $mode = '>:encoding(UTF-8)';
       } else {
           $mode = '>';
       }
           
     my $filebak = $newpath.".bak";      my $filebak = $newpath.".bak";
     if (-e $newpath) {      if (-e $newpath) {
         copy($newpath, $filebak); # errors ignored          copy($newpath, $filebak); # errors ignored
     }      }
     open my $out, '>:encoding(UTF-8)', $newpath;      if (open(my $out, $mode, $newpath)) {
     print $out $contents;          print $out $contents;
     close $out;          close $out;
               $request->print("ok\n");
     $request->print("ok\n");      } else {
           $request->print("error\nFailed to open file to save $path");
       }
     return OK;      return OK;
 }  }
   

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


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