Annotation of loncom/homework/daxesave.pm, revision 1.6
1.1 damieng 1: # The LearningOnline Network
2: # Convert and save a problem from Daxe.
3: #
1.6 ! raeburn 4: # $Id: daxesave.pm,v 1.5 2016/12/13 21:37:35 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;
1.6 ! raeburn 31: use strict;
1.1 damieng 32:
1.6 ! raeburn 33: use Apache::Constants qw(:common);
1.1 damieng 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:
1.2 damieng 45: $request->content_type('text/plain');
46:
1.3 damieng 47: # path should be in the form "/daxeopen/priv/..."
1.4 damieng 48: # or ^/daxeopen/uploaded/[^/]+/[^/]+/.*html?$
1.3 damieng 49: my $path = $env{'form.path'};
1.1 damieng 50: $path =~ s/^\/daxeopen//;
51:
1.3 damieng 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: }
1.4 damieng 64: } elsif ($path =~ m|^/uploaded/[^/]+/[^/]+/|) {
1.3 damieng 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'};
1.4 damieng 68: if ($path =~ m|^/uploaded/\Q$cdom\E/\Q$cnum\E/| && $path !~ /\.\./) {
1.3 damieng 69: if (&Apache::lonnet::allowed('mdc', $env{'request.course.id'})) {
70: $allowed = 1;
71: }
1.1 damieng 72: }
73: }
74: }
75: unless ($allowed) {
1.2 damieng 76: $request->log_reason("Unauthorized path: $path", $path);
77: $request->print("error\nUnauthorized path: $path");
78: $request->status(403);
79: return OK;
1.1 damieng 80: }
81:
82: my $newpath = &Apache::lonnet::filelocation('', $path);
83:
84: my $contents = $env{'form.file'};
85:
1.5 damieng 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: }
1.1 damieng 98:
99: my $filebak = $newpath.".bak";
100: if (-e $newpath) {
101: copy($newpath, $filebak); # errors ignored
102: }
1.6 ! raeburn 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: }
1.1 damieng 110: return OK;
111: }
112:
113: 1;
114: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>