File:  [LON-CAPA] / loncom / homework / daxesave.pm
Revision 1.6: download - view: text, annotated - select for diffs
Wed Aug 23 20:43:34 2023 UTC (8 months, 2 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Coding style. use strict, check file could be opened for writing.

    1: # The LearningOnline Network
    2: # Convert and save a problem from Daxe.
    3: #
    4: # $Id: daxesave.pm,v 1.6 2023/08/23 20:43:34 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: 
   42: sub handler {
   43:     my $request = shift;
   44:     
   45:     $request->content_type('text/plain');
   46:     
   47:     # path should be in the form "/daxeopen/priv/..."
   48:     # or ^/daxeopen/uploaded/[^/]+/[^/]+/.*html?$
   49:     my $path = $env{'form.path'};
   50:     $path =~ s/^\/daxeopen//;
   51:     
   52:     my $allowed = 0;
   53:     if ($path =~ /^\/priv/) {
   54:         my ($ownername,$ownerdom,$ownerhome) = 
   55:             &Apache::lonnet::constructaccess($path, 'setpriv');
   56:         if (($ownername ne '') && ($ownerdom ne '') && ($ownerhome ne '')) {
   57:             unless ($ownerhome eq 'no_host') {
   58:                 my @hosts = &Apache::lonnet::current_machine_ids();
   59:                 if (grep(/^\Q$ownerhome\E$/,@hosts)) {
   60:                     $allowed = 1;
   61:                 }
   62:             }
   63:         }
   64:     } elsif ($path =~ m|^/uploaded/[^/]+/[^/]+/|) {
   65:         if ($env{'user.name'} ne '' && $env{'user.domain'} ne '' && $env{'request.course.id'}) {
   66:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
   67:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
   68:             if ($path =~ m|^/uploaded/\Q$cdom\E/\Q$cnum\E/| && $path !~ /\.\./) {
   69:                 if (&Apache::lonnet::allowed('mdc', $env{'request.course.id'})) {
   70:                     $allowed = 1;
   71:                 }
   72:             }
   73:         }
   74:     }
   75:     unless ($allowed) {
   76:         $request->log_reason("Unauthorized path: $path", $path);
   77:         $request->print("error\nUnauthorized path: $path");
   78:         $request->status(403);
   79:         return OK;
   80:     }
   81: 
   82:     my $newpath = &Apache::lonnet::filelocation('', $path);
   83: 
   84:     my $contents = $env{'form.file'};
   85:     
   86:     my $mode;
   87:     if ($path =~ /\.(task|problem|exam|quiz|assess|survey|library|xml|html|htm|xhtml|xhtm)$/) {
   88:         try {
   89:             $contents = &Apache::xml_to_loncapa::convert_file($contents);
   90:         } catch {
   91:             $request->print("error\nconvert failed for $path: $_");
   92:             return OK;
   93:         };
   94:         $mode = '>:encoding(UTF-8)';
   95:     } else {
   96:         $mode = '>';
   97:     }
   98:     
   99:     my $filebak = $newpath.".bak";
  100:     if (-e $newpath) {
  101:         copy($newpath, $filebak); # errors ignored
  102:     }
  103:     if (open(my $out, $mode, $newpath)) {
  104:         print $out $contents;
  105:         close $out;
  106:         $request->print("ok\n");
  107:     } else {
  108:         $request->print("error\nFailed to open file to save $path");
  109:     }
  110:     return OK;
  111: }
  112: 
  113: 1;
  114: __END__

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