Annotation of loncom/homework/daxesave.pm, revision 1.10
1.1 damieng 1: # The LearningOnline Network
2: # Convert and save a problem from Daxe.
3: #
1.10 ! raeburn 4: # $Id: daxesave.pm,v 1.9 2023/11/19 21:28:17 raeburn 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;
1.10 ! raeburn 41: use Apache::lonlocal;
1.1 damieng 42:
43: sub handler {
44: my $request = shift;
45:
1.2 damieng 46: $request->content_type('text/plain');
1.9 raeburn 47:
1.10 ! raeburn 48: my %editors = &Apache::loncommon::permitted_editors($request->uri);
1.9 raeburn 49: unless ($editors{'daxe'}) {
50: $request->content_type('text/plain');
51: $request->print(&mt('Daxe editor not enabled for this Authoring Space'));
52: return OK;
53: }
54:
1.3 damieng 55: # path should be in the form "/daxeopen/priv/..."
1.7 raeburn 56: # or "/daxeopen/uploaded/$cdom/$cnum/(docs|supplemental)/(default|\d+)/\d+/"
1.3 damieng 57: my $path = $env{'form.path'};
1.1 damieng 58: $path =~ s/^\/daxeopen//;
59:
1.3 damieng 60: my $allowed = 0;
1.7 raeburn 61: my ($cdom,$cnum);
62: if ($path =~ m{^/priv/}) {
1.3 damieng 63: my ($ownername,$ownerdom,$ownerhome) =
1.7 raeburn 64: &Apache::lonnet::constructaccess($path);
1.3 damieng 65: if (($ownername ne '') && ($ownerdom ne '') && ($ownerhome ne '')) {
66: unless ($ownerhome eq 'no_host') {
67: my @hosts = &Apache::lonnet::current_machine_ids();
68: if (grep(/^\Q$ownerhome\E$/,@hosts)) {
69: $allowed = 1;
70: }
71: }
72: }
1.7 raeburn 73: } elsif ($path =~ m|^/uploaded/|) {
1.3 damieng 74: if ($env{'user.name'} ne '' && $env{'user.domain'} ne '' && $env{'request.course.id'}) {
75: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
76: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.4 damieng 77: if ($path =~ m|^/uploaded/\Q$cdom\E/\Q$cnum\E/| && $path !~ /\.\./) {
1.3 damieng 78: if (&Apache::lonnet::allowed('mdc', $env{'request.course.id'})) {
79: $allowed = 1;
80: }
1.1 damieng 81: }
82: }
83: }
84: unless ($allowed) {
1.2 damieng 85: $request->log_reason("Unauthorized path: $path", $path);
86: $request->print("error\nUnauthorized path: $path");
87: $request->status(403);
88: return OK;
1.1 damieng 89: }
90:
1.7 raeburn 91: if ($path =~ m{^/priv/}) {
92: my $newpath = &Apache::lonnet::filelocation('', $path);
93: my $contents = $env{'form.file'};
1.1 damieng 94:
1.7 raeburn 95: my $mode;
96: if ($path =~ /\.(task|problem|exam|quiz|assess|survey|library|xml|html|htm|xhtml|xhtm)$/) {
97: try {
98: $contents = &Apache::xml_to_loncapa::convert_file($contents);
99: } catch {
100: $request->print("error\nconvert failed for $path: $_");
101: return OK;
102: };
103: $mode = '>:encoding(UTF-8)';
104: } else {
105: $mode = '>';
106: }
107:
108: my $filebak = $newpath.".bak";
109: if (-e $newpath) {
110: copy($newpath, $filebak); # errors ignored
111: }
112: if (open(my $out, $mode, $newpath)) {
113: print $out $contents;
114: close($out);
115: $request->print("ok\n");
116: } else {
117: $request->print("error\nFailed to open file to save $path");
118: }
119: } elsif ($path =~ m{^/uploaded/}) {
120: my ($unauthorized,$unsupported);
121: if ($path =~ m{^\Q/uploaded/$cdom/$cnum/\E(docs|supplemental)/(default|\d+)/(\d+)/(.+)$}) {
122: my ($type,$folder,$rid,$fname) = ($1,$2,$3,$4);
123: my $referrer = $request->headers_in->{'Referer'};
124: if ($referrer =~ m{\Qfile=/daxeopen/uploaded/$cdom/$cnum/$type/$folder/$rid/\E}) {
125: if ($fname =~ /\.(html|htm|xhtml|xhtm)$/) {
126: try {
127: $env{'form.file'} = &Apache::xml_to_loncapa::convert_file($env{'form.file'});
128: } catch {
129: $request->print("error\nconvert failed for $fname: $_");
130: return OK;
131: }
132: } elsif ($fname =~ /\.(task|problem|exam|quiz|assess|survey|library|xml)$/) {
133: $unsupported = $1;
134: }
135: unless ($unsupported) {
1.8 raeburn 136: my $url = &Apache::lonnet::userfileupload('file','daxesave',"$type/$folder/$rid",
137: undef,undef,undef,$cnum,$cdom);
1.7 raeburn 138: if ($url =~ m{^/uploaded/$cdom/$cnum/$type/$folder/$rid/}) {
139: $request->print("ok\n");
140: } else {
141: $request->print("error\nFailed to save uploaded file: $fname");
142: }
143: }
144: } else {
145: $unauthorized = 1;
146: }
147: } else {
148: $unauthorized = 1;
149: }
150: if ($unauthorized) {
151: $request->log_reason("Unauthorized path: $path", $path);
152: $request->print("error\nUnauthorized path: $path");
153: $request->status(403);
154: } elsif ($unsupported) {
155: $request->log_reason("File extension: $unsupported -- not allowed for upload to course", $path);
156: $request->print("error\nFile extension: $unsupported -- not allowed for upload to course");
157: $request->status(403);
158: }
1.6 raeburn 159: }
1.1 damieng 160: return OK;
161: }
162:
163: 1;
164: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>