File:  [LON-CAPA] / loncom / homework / daxesave.pm
Revision 1.10: download - view: text, annotated - select for diffs
Sun Nov 26 20:47:15 2023 UTC (5 months, 2 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_4_msu, HEAD
- Support checking of available editors when saving in Daxe, when current
  role is a course role.
- Support localization.

    1: # The LearningOnline Network
    2: # Convert and save a problem from Daxe.
    3: #
    4: # $Id: daxesave.pm,v 1.10 2023/11/26 20:47:15 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ###
   29: 
   30: package Apache::daxesave;
   31: use strict;
   32: 
   33: use Apache::Constants qw(:common);
   34: use Apache::lonnet;
   35: use Try::Tiny;
   36: use File::Copy;
   37: 
   38: use Apache::lonacc;
   39: use Apache::loncommon;
   40: use Apache::xml_to_loncapa;
   41: use Apache::lonlocal;
   42: 
   43: sub handler {
   44:     my $request = shift;
   45:     
   46:     $request->content_type('text/plain');
   47: 
   48:     my %editors = &Apache::loncommon::permitted_editors($request->uri);
   49:     unless ($editors{'daxe'}) {
   50:         $request->content_type('text/plain');
   51:         $request->print(&mt('Daxe editor not enabled for this Authoring Space'));
   52:         return OK;
   53:     }
   54: 
   55:     # path should be in the form "/daxeopen/priv/..."
   56:     # or "/daxeopen/uploaded/$cdom/$cnum/(docs|supplemental)/(default|\d+)/\d+/"
   57:     my $path = $env{'form.path'};
   58:     $path =~ s/^\/daxeopen//;
   59:     
   60:     my $allowed = 0;
   61:     my ($cdom,$cnum);
   62:     if ($path =~ m{^/priv/}) {
   63:         my ($ownername,$ownerdom,$ownerhome) = 
   64:             &Apache::lonnet::constructaccess($path);
   65:         if (($ownername ne '') && ($ownerdom ne '') && ($ownerhome ne '')) {
   66:             unless ($ownerhome eq 'no_host') {
   67:                 my @hosts = &Apache::lonnet::current_machine_ids();
   68:                 if (grep(/^\Q$ownerhome\E$/,@hosts)) {
   69:                     $allowed = 1;
   70:                 }
   71:             }
   72:         }
   73:     } elsif ($path =~ m|^/uploaded/|) {
   74:         if ($env{'user.name'} ne '' && $env{'user.domain'} ne '' && $env{'request.course.id'}) {
   75:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
   76:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
   77:             if ($path =~ m|^/uploaded/\Q$cdom\E/\Q$cnum\E/| && $path !~ /\.\./) {
   78:                 if (&Apache::lonnet::allowed('mdc', $env{'request.course.id'})) {
   79:                     $allowed = 1;
   80:                 }
   81:             }
   82:         }
   83:     }
   84:     unless ($allowed) {
   85:         $request->log_reason("Unauthorized path: $path", $path);
   86:         $request->print("error\nUnauthorized path: $path");
   87:         $request->status(403);
   88:         return OK;
   89:     }
   90: 
   91:     if ($path =~ m{^/priv/}) {
   92:         my $newpath = &Apache::lonnet::filelocation('', $path);
   93:         my $contents = $env{'form.file'};
   94:     
   95:         my $mode;
   96:         if ($path =~ /\.(task|problem|exam|quiz|assess|survey|library|xml|html|htm|xhtml|xhtm)$/) {
   97:             try {
   98:                 $contents = &Apache::xml_to_loncapa::convert_file($contents);
   99:             } catch {
  100:                 $request->print("error\nconvert failed for $path: $_");
  101:                 return OK;
  102:             };
  103:             $mode = '>:encoding(UTF-8)';
  104:         } else {
  105:             $mode = '>';
  106:         }
  107:   
  108:         my $filebak = $newpath.".bak";
  109:         if (-e $newpath) {
  110:             copy($newpath, $filebak); # errors ignored
  111:         }
  112:         if (open(my $out, $mode, $newpath)) {
  113:             print $out $contents;
  114:             close($out);
  115:             $request->print("ok\n");
  116:         } else {
  117:             $request->print("error\nFailed to open file to save $path");
  118:         }
  119:     } elsif ($path =~ m{^/uploaded/}) {
  120:         my ($unauthorized,$unsupported);
  121:         if ($path =~ m{^\Q/uploaded/$cdom/$cnum/\E(docs|supplemental)/(default|\d+)/(\d+)/(.+)$}) {
  122:             my ($type,$folder,$rid,$fname) = ($1,$2,$3,$4);
  123:             my $referrer = $request->headers_in->{'Referer'};
  124:             if ($referrer =~ m{\Qfile=/daxeopen/uploaded/$cdom/$cnum/$type/$folder/$rid/\E}) {
  125:                 if ($fname =~ /\.(html|htm|xhtml|xhtm)$/) {
  126:                     try {
  127:                         $env{'form.file'} = &Apache::xml_to_loncapa::convert_file($env{'form.file'});
  128:                     } catch {
  129:                         $request->print("error\nconvert failed for $fname: $_");
  130:                         return OK;
  131:                     }
  132:                 } elsif ($fname =~ /\.(task|problem|exam|quiz|assess|survey|library|xml)$/) {
  133:                     $unsupported = $1;
  134:                 }
  135:                 unless ($unsupported) {
  136:                     my $url = &Apache::lonnet::userfileupload('file','daxesave',"$type/$folder/$rid",
  137:                                                                undef,undef,undef,$cnum,$cdom);
  138:                     if ($url =~ m{^/uploaded/$cdom/$cnum/$type/$folder/$rid/}) {
  139:                         $request->print("ok\n");
  140:                     } else {
  141:                         $request->print("error\nFailed to save uploaded file: $fname");
  142:                     }
  143:                 }
  144:             } else {
  145:                 $unauthorized = 1;
  146:             }
  147:         } else {
  148:             $unauthorized = 1;
  149:         }
  150:         if ($unauthorized) {
  151:             $request->log_reason("Unauthorized path: $path", $path);
  152:             $request->print("error\nUnauthorized path: $path");
  153:             $request->status(403);
  154:         } elsif ($unsupported) {
  155:             $request->log_reason("File extension: $unsupported -- not allowed for upload to course", $path);
  156:             $request->print("error\nFile extension: $unsupported -- not allowed for upload to course");
  157:             $request->status(403);
  158:         }
  159:     }
  160:     return OK;
  161: }
  162: 
  163: 1;
  164: __END__

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