Annotation of loncom/homework/daxesave.pm, revision 1.4

1.1       damieng     1: # The LearningOnline Network
                      2: # Convert and save a problem from Daxe.
                      3: #
1.4     ! damieng     4: # $Id: daxesave.pm,v 1.3 2016/02/17 00:05:14 damieng Exp $
1.1       damieng     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: 
                     32: use Apache::Constants;
                     33: use Apache::lonnet;
                     34: use Try::Tiny;
                     35: use File::Copy;
                     36: 
                     37: use Apache::lonacc;
                     38: use Apache::loncommon;
                     39: use Apache::xml_to_loncapa;
                     40: 
                     41: sub handler {
                     42:     my $request = shift;
                     43:     
1.2       damieng    44:     $request->content_type('text/plain');
                     45:     
1.3       damieng    46:     # path should be in the form "/daxeopen/priv/..."
1.4     ! damieng    47:     # or ^/daxeopen/uploaded/[^/]+/[^/]+/.*html?$
1.3       damieng    48:     my $path = $env{'form.path'};
1.1       damieng    49:     $path =~ s/^\/daxeopen//;
                     50:     
1.3       damieng    51:     my $allowed = 0;
                     52:     if ($path =~ /^\/priv/) {
                     53:         my ($ownername,$ownerdom,$ownerhome) = 
                     54:             &Apache::lonnet::constructaccess($path, 'setpriv');
                     55:         if (($ownername ne '') && ($ownerdom ne '') && ($ownerhome ne '')) {
                     56:             unless ($ownerhome eq 'no_host') {
                     57:                 my @hosts = &Apache::lonnet::current_machine_ids();
                     58:                 if (grep(/^\Q$ownerhome\E$/,@hosts)) {
                     59:                     $allowed = 1;
                     60:                 }
                     61:             }
                     62:         }
1.4     ! damieng    63:     } elsif ($path =~ m|^/uploaded/[^/]+/[^/]+/|) {
1.3       damieng    64:         if ($env{'user.name'} ne '' && $env{'user.domain'} ne '' && $env{'request.course.id'}) {
                     65:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                     66:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.4     ! damieng    67:             if ($path =~ m|^/uploaded/\Q$cdom\E/\Q$cnum\E/| && $path !~ /\.\./) {
1.3       damieng    68:                 if (&Apache::lonnet::allowed('mdc', $env{'request.course.id'})) {
                     69:                     $allowed = 1;
                     70:                 }
1.1       damieng    71:             }
                     72:         }
                     73:     }
                     74:     unless ($allowed) {
1.2       damieng    75:         $request->log_reason("Unauthorized path: $path", $path);
                     76:         $request->print("error\nUnauthorized path: $path");
                     77:         $request->status(403);
                     78:         return OK;
1.1       damieng    79:     }
                     80: 
                     81:     my $newpath = &Apache::lonnet::filelocation('', $path);
                     82: 
                     83:     my $contents = $env{'form.file'};
                     84:     
                     85:     try {
                     86:         $contents = &Apache::xml_to_loncapa::convert_file($contents);
                     87:     } catch {
                     88:         $request->print("error\nconvert failed for $path: $_");
                     89:         return OK;
                     90:     };
                     91:     
                     92:     my $filebak = $newpath.".bak";
                     93:     if (-e $newpath) {
                     94:         copy($newpath, $filebak); # errors ignored
                     95:     }
                     96:     open my $out, '>:encoding(UTF-8)', $newpath;
                     97:     print $out $contents;
                     98:     close $out;
                     99:     
                    100:     $request->print("ok\n");
                    101:     return OK;
                    102: }
                    103: 
                    104: 1;
                    105: __END__

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