Annotation of loncom/homework/daxeopen.pm, revision 1.6
1.1 damieng 1: # The LearningOnline Network
2: # Opening converted problems and directory listings for Daxe
3: #
1.6 ! damieng 4: # $Id: daxeopen.pm,v 1.5 2017/02/23 21:32:08 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::daxeopen;
31:
32: use Apache::Constants;
33: use DateTime;
34: use Try::Tiny;
35: use File::stat;
36: use Fcntl ':mode';
37:
1.5 damieng 38: use LONCAPA qw(:match);
1.1 damieng 39: use Apache::loncommon;
40: use Apache::lonnet;
41: use Apache::pre_xml;
42: use Apache::html_to_xml;
43: use Apache::post_xml;
44:
45:
46: sub handler {
47: my $request = shift;
48: my $uri = $request->uri;
49: $uri =~ s/^\/daxeopen//;
50: &Apache::loncommon::no_cache($request);
51: if ($uri =~ /\/$/) {
52: return directory_listing($uri, $request);
1.6 ! damieng 53: } elsif ($uri =~ /^\/priv\/.*\.(task|problem|exam|quiz|assess|survey|library|xml|html|htm|xhtml|xhtm)$/) {
1.1 damieng 54: return convert_problem($uri, $request);
55: } else {
56: # Apache should send other files directly
1.2 damieng 57: $request->status(406);
58: return OK;
1.1 damieng 59: }
60: }
61:
62: sub convert_problem {
63: my ($uri, $request) = @_;
64:
1.6 ! damieng 65: if ($uri =~ /^\/priv\/([^\/]+)\/([^\/]+)\//) {
! 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');
! 70: $request->print("Forbidden URI: $uri");
! 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.3 damieng 91: my $text = &Apache::post_xml::post_xml($textref, $file, $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');
97: $request->print("convert failed for $file: $_");
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');
! 113: $request->print("Not found: $uri");
1.2 damieng 114: $request->status(404);
115: return OK;
1.5 damieng 116: } elsif ($uri =~ /^\/res\//) {
1.6 ! damieng 117: # NOTE: dirlist does not return an error for /res/idontexist/
1.5 damieng 118: (my $listref, $listerror) = &Apache::lonnet::dirlist($uri);
119: if ($listerror) {
120: $request->content_type('text/plain');
121: $request->print("listing error: $listerror");
122: $request->status(406);
123: return OK;
1.6 ! damieng 124: } elsif ($uri =~ /^\/res\/[^\/]+\/$/ && scalar(@{$listref}) == 0) {
! 125: $request->content_type('text/plain');
! 126: $request->print("Not found: $uri");
! 127: $request->status(404);
! 128: return OK;
1.5 damieng 129: }
130: my $dirname = $uri;
131: $dirname =~ s/^.*\/([^\/]*)$/$1/;
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)$/;
138: $path =~ s/^\/home\/httpd\/html\/res\///;
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: }
154: $path =~ s/\/$//;
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');
! 169: $request->print("Forbidden URI: $uri");
! 170: $request->status(403);
! 171: return OK;
! 172: }
! 173: $res .= "<directory name=\"priv\">\n";
! 174: $res .= "<directory name=\"$udom\"/>\n";
! 175: } elsif ($uri =~ /^\/priv\/([^\/]+)\/$/) {
! 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');
! 180: $request->print("Forbidden URI: $uri");
! 181: $request->status(403);
! 182: return OK;
! 183: }
! 184: $res .= "<directory name=\"$domain\">\n";
! 185: $res .= "<directory name=\"$uname\"/>\n";
! 186: } elsif ($uri =~ /^\/priv\/([^\/]+)\/([^\/]+)\//) {
! 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');
! 191: $request->print("Forbidden URI: $uri");
! 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');
! 198: $request->print("Not found: $uri");
1.5 damieng 199: $request->status(404);
200: return OK;
1.1 damieng 201: }
1.5 damieng 202: $dirpath =~ s/\/$//;
203: opendir my $dir, $dirpath or die "Cannot open directory: $dirpath";
204: my @files = readdir $dir;
205: closedir $dir;
206: my $dirname = $dirpath;
207: $dirname =~ s/^.*\/([^\/]*)$/$1/;
208: $res .= "<directory name=\"$dirname\">\n";
209: foreach my $name (@files) {
210: if ($name eq '.' || $name eq '..') {
211: next;
212: }
213: if ($name =~ /\.(bak|log|meta|save)$/) {
214: next;
215: }
216: $sb = stat($dirpath.'/'.$name);
217: my $mode = $sb->mode;
218: if (S_ISDIR($mode)) {
219: $res .= "<directory name=\"$name\"/>\n";
220: } else {
221: $res .= "<file name=\"$name\"";
222: my $size = $sb->size; # total size of file, in bytes
223: $res .= " size=\"$size\"";
224: my $mtime = $sb->mtime; # last modify time in seconds since the epoch
225: my $dt = DateTime->from_epoch(epoch => $mtime);
226: my $modified = $dt->iso8601().'Z';
227: $res .= " modified=\"$modified\"";
228: $res .= "/>\n";
229: }
1.1 damieng 230: }
1.6 ! damieng 231: } else {
! 232: $request->content_type('text/plain');
! 233: $request->print("Not found: $uri");
! 234: $request->status(404);
! 235: return OK;
1.1 damieng 236: }
237: $res .= "</directory>\n";
238: &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
239: $request->print($res);
240: return OK;
241: }
242:
243: 1;
244: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>