Annotation of loncom/homework/daxesave.pm, revision 1.1
1.1 ! damieng 1: # The LearningOnline Network
! 2: # Convert and save a problem from Daxe.
! 3: #
! 4: # $Id$
! 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:
! 44: my $path = $env{'form.path'}; # should be in the form "/daxeopen/priv/..."
! 45: $path =~ s/^\/daxeopen//;
! 46:
! 47: my $allowed;
! 48: my ($ownername,$ownerdom,$ownerhome) =
! 49: &Apache::lonnet::constructaccess($path, 'setpriv');
! 50: if (($ownername ne '') && ($ownerdom ne '') && ($ownerhome ne '')) {
! 51: unless ($ownerhome eq 'no_host') {
! 52: my @hosts = &Apache::lonnet::current_machine_ids();
! 53: if (grep(/^\Q$ownerhome\E$/,@hosts)) {
! 54: $allowed = 1;
! 55: }
! 56: }
! 57: }
! 58: unless ($allowed) {
! 59: $request->log_reason("Unauthorized path: $path", $path);
! 60: return HTTP_NOT_ACCEPTABLE;
! 61: }
! 62:
! 63: my $newpath = &Apache::lonnet::filelocation('', $path);
! 64:
! 65: my $contents = $env{'form.file'};
! 66:
! 67: try {
! 68: $contents = &Apache::xml_to_loncapa::convert_file($contents);
! 69: } catch {
! 70: $request->print("error\nconvert failed for $path: $_");
! 71: return OK;
! 72: };
! 73:
! 74: my $filebak = $newpath.".bak";
! 75: if (-e $newpath) {
! 76: copy($newpath, $filebak); # errors ignored
! 77: }
! 78: open my $out, '>:encoding(UTF-8)', $newpath;
! 79: print $out $contents;
! 80: close $out;
! 81:
! 82: $request->print("ok\n");
! 83: return OK;
! 84: }
! 85:
! 86: 1;
! 87: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>