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