File:  [LON-CAPA] / loncom / homework / daxesave.pm
Revision 1.1: download - view: text, annotated - select for diffs
Thu Dec 3 20:40:27 2015 UTC (8 years, 4 months ago) by damieng
Branches: MAIN
CVS tags: HEAD
integrated Daxe, opening in a separate window for now

# The LearningOnline Network
# Convert and save a problem from Daxe.
#
# $Id: daxesave.pm,v 1.1 2015/12/03 20:40:27 damieng Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
###

package Apache::daxesave;

use Apache::Constants;
use Apache::lonnet;
use Try::Tiny;
use File::Copy;

use Apache::lonacc;
use Apache::loncommon;
use Apache::xml_to_loncapa;

sub handler {
    my $request = shift;
    
    my $path = $env{'form.path'}; # should be in the form "/daxeopen/priv/..."
    $path =~ s/^\/daxeopen//;
    
    my $allowed;
    my ($ownername,$ownerdom,$ownerhome) = 
        &Apache::lonnet::constructaccess($path, 'setpriv');
    if (($ownername ne '') && ($ownerdom ne '') && ($ownerhome ne '')) {
        unless ($ownerhome eq 'no_host') {
            my @hosts = &Apache::lonnet::current_machine_ids();
            if (grep(/^\Q$ownerhome\E$/,@hosts)) {
                $allowed = 1;
            }
        }
    }
    unless ($allowed) {
        $request->log_reason("Unauthorized path: $path", $path); 
        return HTTP_NOT_ACCEPTABLE;
    }

    my $newpath = &Apache::lonnet::filelocation('', $path);

    my $contents = $env{'form.file'};
    
    try {
        $contents = &Apache::xml_to_loncapa::convert_file($contents);
    } catch {
        $request->print("error\nconvert failed for $path: $_");
        return OK;
    };
    
    my $filebak = $newpath.".bak";
    if (-e $newpath) {
        copy($newpath, $filebak); # errors ignored
    }
    open my $out, '>:encoding(UTF-8)', $newpath;
    print $out $contents;
    close $out;
    
    $request->print("ok\n");
    return OK;
}

1;
__END__

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