Annotation of loncom/homework/daxeopen.pm, revision 1.8
1.1 damieng 1: # The LearningOnline Network
2: # Opening converted problems and directory listings for Daxe
3: #
1.8 ! raeburn 4: # $Id: daxeopen.pm,v 1.7 2023/08/23 20:33:06 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::daxeopen;
1.8 ! raeburn 31: use strict;
1.1 damieng 32:
1.8 ! raeburn 33: use Apache::Constants qw(:common);
1.1 damieng 34: use DateTime;
35: use Try::Tiny;
36: use File::stat;
37: use Fcntl ':mode';
38:
1.5 damieng 39: use LONCAPA qw(:match);
1.1 damieng 40: use Apache::loncommon;
41: use Apache::lonnet;
42: use Apache::pre_xml;
43: use Apache::html_to_xml;
44: use Apache::post_xml;
45:
46:
47: sub handler {
48: my $request = shift;
49: my $uri = $request->uri;
1.7 raeburn 50: $uri =~ s{^/daxeopen}{};
1.1 damieng 51: &Apache::loncommon::no_cache($request);
1.7 raeburn 52: if ($uri =~ m{/$}) {
1.1 damieng 53: return directory_listing($uri, $request);
1.7 raeburn 54: } elsif ($uri =~ m{^/priv/.*\.(task|problem|exam|quiz|assess|survey|library|xml|html|htm|xhtml|xhtm)$}) {
1.1 damieng 55: return convert_problem($uri, $request);
56: } else {
57: # Apache should send other files directly
1.2 damieng 58: $request->status(406);
59: return OK;
1.1 damieng 60: }
61: }
62:
63: sub convert_problem {
64: my ($uri, $request) = @_;
65:
1.7 raeburn 66: if ($uri =~ m{^/priv/([^/]+)/([^/]+)/}) {
1.6 damieng 67: my ($domain, $user) = ($1, $2);
68: my ($uname, $udom) = ($env{'user.name'}, $env{'user.domain'});
69: if (!defined $uname || !defined $udom || $domain ne $udom || $user ne $uname) {
70: $request->content_type('text/plain');
71: $request->print("Forbidden URI: $uri");
72: $request->status(403);
73: return OK;
74: }
75: }
1.1 damieng 76: my $file = &Apache::lonnet::filelocation('', $uri);
77: &Apache::lonnet::repcopy($file);
78: if (! -e $file) {
1.2 damieng 79: $request->status(404);
80: return OK;
1.1 damieng 81: }
82: try {
83: my $warnings = 0; # no warning printed
84: my $textref = &Apache::pre_xml::pre_xml($file, $warnings);
1.4 damieng 85: my $case_sensitive;
86: if ($uri =~ /\.(task)$/) {
87: $case_sensitive = 1;
88: } else {
89: $case_sensitive = 0;
90: }
91: $textref = &Apache::html_to_xml::html_to_xml($textref, $warnings, $case_sensitive);
1.8 ! raeburn 92: my $text = &Apache::post_xml::post_xml($textref, $file, $Apache::lonnet::perlvar{'lonDocRoot'}, $warnings);
1.1 damieng 93: &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
94: $request->print($text);
95: return OK;
96: } catch {
1.2 damieng 97: $request->content_type('text/plain');
98: $request->print("convert failed for $file: $_");
99: $request->status(406);
100: return OK;
1.1 damieng 101: };
102: }
103:
104: sub directory_listing {
105: my ($uri, $request) = @_;
1.5 damieng 106: my $res = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
107: if ($uri eq '/') {
108: # root: let users browse /res
109: $res .= "<directory name=\"/\">\n";
1.6 damieng 110: $res .= "<directory name=\"priv\"/>\n";
1.5 damieng 111: $res .= "<directory name=\"res\"/>\n";
112: } elsif ($uri !~ /^\/(priv|res)\//) {
1.6 damieng 113: $request->content_type('text/plain');
114: $request->print("Not found: $uri");
1.2 damieng 115: $request->status(404);
116: return OK;
1.7 raeburn 117: } elsif ($uri =~ m{^/res/}) {
1.6 damieng 118: # NOTE: dirlist does not return an error for /res/idontexist/
1.8 ! raeburn 119: my ($listref, $listerror) = &Apache::lonnet::dirlist($uri);
1.5 damieng 120: if ($listerror) {
121: $request->content_type('text/plain');
122: $request->print("listing error: $listerror");
123: $request->status(406);
124: return OK;
1.7 raeburn 125: } elsif ($uri =~ m{^/res/[^/]+/$} && scalar(@{$listref}) == 0) {
1.6 damieng 126: $request->content_type('text/plain');
127: $request->print("Not found: $uri");
128: $request->status(404);
129: return OK;
1.5 damieng 130: }
131: my $dirname = $uri;
1.7 raeburn 132: $dirname =~ s{^.*/([^/]*)$}{$1};
1.5 damieng 133: $res .= "<directory name=\"$dirname/\">\n";
134: if (ref($listref) eq 'ARRAY') {
135: my @lines = @{$listref};
136: foreach my $line (@lines) {
1.6 damieng 137: my ($path, $dom, undef, $testdir, undef, undef, undef, undef, $size, undef, $mtime, undef, undef, undef, $obs, undef) = split(/\&/, $line, 16);
1.5 damieng 138: my $isdir = ($testdir & 16384) || $dom =~ /^(user|domain)$/;
1.7 raeburn 139: $path =~ s{^/home/httpd/html/res/}{};
1.5 damieng 140: next if $path eq '.' || $path eq '..';
141: next if $path =~ /\.meta$/ || $obs || $path =~ /\.\d+\.[^.]+$/;
142: if ($dom ne 'domain') {
143: my ($udom,$uname);
144: if ($dom eq 'user') {
145: ($udom) = ($uri =~ m{^/res/($match_domain)});
146: $uname = $path;
147: } else {
148: ($udom, $uname) = ($uri =~ m{^/res/($match_domain)/($match_courseid)});
149: }
150: if ($udom ne '' && $uname ne '') {
151: # remove courses from the list
152: next if (&Apache::lonnet::is_course($udom, $uname));
153: }
154: }
1.7 raeburn 155: $path =~ s{/$}{};
1.5 damieng 156: my $name = $path;
157: if ($isdir) {
158: $res .= "<directory name=\"$name\"/>\n";
159: } else {
1.6 damieng 160: my $dt = DateTime->from_epoch(epoch => $mtime);
161: my $modified = $dt->iso8601().'Z';
162: $res .= "<file name=\"$name\" size=\"$size\" modified=\"$modified\"/>\n";
1.5 damieng 163: }
164: }
1.1 damieng 165: }
1.6 damieng 166: } elsif ($uri eq '/priv/') {
167: my $udom = $env{'user.domain'};
168: if (!defined $udom) {
169: $request->content_type('text/plain');
170: $request->print("Forbidden URI: $uri");
171: $request->status(403);
172: return OK;
173: }
174: $res .= "<directory name=\"priv\">\n";
175: $res .= "<directory name=\"$udom\"/>\n";
1.7 raeburn 176: } elsif ($uri =~ m{^/priv/([^/]+)/$}) {
1.6 damieng 177: my $domain = $1;
178: my ($uname, $udom) = ($env{'user.name'}, $env{'user.domain'});
179: if (!defined $uname || !defined $udom || $domain ne $udom) {
180: $request->content_type('text/plain');
181: $request->print("Forbidden URI: $uri");
182: $request->status(403);
183: return OK;
184: }
185: $res .= "<directory name=\"$domain\">\n";
186: $res .= "<directory name=\"$uname\"/>\n";
1.7 raeburn 187: } elsif ($uri =~ m{^/priv/([^/]+)/([^/]+)/}) {
1.6 damieng 188: my ($domain, $user) = ($1, $2);
189: my ($uname, $udom) = ($env{'user.name'}, $env{'user.domain'});
190: if (!defined $uname || !defined $udom || $domain ne $udom || $user ne $uname) {
191: $request->content_type('text/plain');
192: $request->print("Forbidden URI: $uri");
193: $request->status(403);
194: return OK;
195: }
1.5 damieng 196: my $dirpath = &Apache::lonnet::filelocation('', $uri);
197: if (! -e $dirpath) {
1.6 damieng 198: $request->content_type('text/plain');
199: $request->print("Not found: $uri");
1.5 damieng 200: $request->status(404);
201: return OK;
1.1 damieng 202: }
1.7 raeburn 203: $dirpath =~ s{/$}{};
1.5 damieng 204: opendir my $dir, $dirpath or die "Cannot open directory: $dirpath";
205: my @files = readdir $dir;
206: closedir $dir;
207: my $dirname = $dirpath;
1.7 raeburn 208: $dirname =~ s{^.*/([^/]*)$}{$1};
1.5 damieng 209: $res .= "<directory name=\"$dirname\">\n";
210: foreach my $name (@files) {
211: if ($name eq '.' || $name eq '..') {
212: next;
213: }
214: if ($name =~ /\.(bak|log|meta|save)$/) {
215: next;
216: }
1.8 ! raeburn 217: my $sb = stat($dirpath.'/'.$name);
1.5 damieng 218: my $mode = $sb->mode;
219: if (S_ISDIR($mode)) {
220: $res .= "<directory name=\"$name\"/>\n";
221: } else {
222: $res .= "<file name=\"$name\"";
223: my $size = $sb->size; # total size of file, in bytes
224: $res .= " size=\"$size\"";
225: my $mtime = $sb->mtime; # last modify time in seconds since the epoch
226: my $dt = DateTime->from_epoch(epoch => $mtime);
227: my $modified = $dt->iso8601().'Z';
228: $res .= " modified=\"$modified\"";
229: $res .= "/>\n";
230: }
1.1 damieng 231: }
1.6 damieng 232: } else {
233: $request->content_type('text/plain');
234: $request->print("Not found: $uri");
235: $request->status(404);
236: return OK;
1.1 damieng 237: }
238: $res .= "</directory>\n";
239: &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
240: $request->print($res);
241: return OK;
242: }
243:
244: 1;
245: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>