Annotation of loncom/homework/daxeopen.pm, revision 1.12
1.1 damieng 1: # The LearningOnline Network
2: # Opening converted problems and directory listings for Daxe
3: #
1.12 ! raeburn 4: # $Id: daxeopen.pm,v 1.11 2023/08/23 22:34:48 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.12 ! raeburn 65: if ($uri =~ m{^/priv/$match_domain/$match_username/}) {
! 66: unless (&has_priv_access($uri)) {
1.6 damieng 67: $request->content_type('text/plain');
1.10 raeburn 68: $request->print(&mt('Forbidden URI: [_1]',$uri));
1.6 damieng 69: $request->status(403);
70: return OK;
71: }
72: }
1.1 damieng 73: my $file = &Apache::lonnet::filelocation('', $uri);
74: &Apache::lonnet::repcopy($file);
75: if (! -e $file) {
1.2 damieng 76: $request->status(404);
77: return OK;
1.1 damieng 78: }
79: try {
80: my $warnings = 0; # no warning printed
81: my $textref = &Apache::pre_xml::pre_xml($file, $warnings);
1.4 damieng 82: my $case_sensitive;
83: if ($uri =~ /\.(task)$/) {
84: $case_sensitive = 1;
85: } else {
86: $case_sensitive = 0;
87: }
88: $textref = &Apache::html_to_xml::html_to_xml($textref, $warnings, $case_sensitive);
1.8 raeburn 89: my $text = &Apache::post_xml::post_xml($textref, $file, $Apache::lonnet::perlvar{'lonDocRoot'}, $warnings);
1.1 damieng 90: &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
91: $request->print($text);
92: return OK;
93: } catch {
1.2 damieng 94: $request->content_type('text/plain');
1.10 raeburn 95: $request->print(&mt('convert failed for [_1]:',$file)." $_");
1.2 damieng 96: $request->status(406);
97: return OK;
1.1 damieng 98: };
99: }
100:
101: sub directory_listing {
102: my ($uri, $request) = @_;
1.5 damieng 103: my $res = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
104: if ($uri eq '/') {
105: # root: let users browse /res
106: $res .= "<directory name=\"/\">\n";
1.6 damieng 107: $res .= "<directory name=\"priv\"/>\n";
1.5 damieng 108: $res .= "<directory name=\"res\"/>\n";
1.11 raeburn 109: } elsif ($uri !~ m{^/(priv|res)/}) {
1.6 damieng 110: $request->content_type('text/plain');
1.10 raeburn 111: $request->print(&mt('Not found: [_1]',$uri));
1.2 damieng 112: $request->status(404);
113: return OK;
1.7 raeburn 114: } elsif ($uri =~ m{^/res/}) {
1.6 damieng 115: # NOTE: dirlist does not return an error for /res/idontexist/
1.8 raeburn 116: my ($listref, $listerror) = &Apache::lonnet::dirlist($uri);
1.5 damieng 117: if ($listerror) {
118: $request->content_type('text/plain');
1.10 raeburn 119: $request->print(&mt('listing error: [_1]',$listerror));
1.5 damieng 120: $request->status(406);
121: return OK;
1.7 raeburn 122: } elsif ($uri =~ m{^/res/[^/]+/$} && scalar(@{$listref}) == 0) {
1.6 damieng 123: $request->content_type('text/plain');
1.10 raeburn 124: $request->print(&mt('Not found: [_1]',$uri));
1.6 damieng 125: $request->status(404);
126: return OK;
1.5 damieng 127: }
128: my $dirname = $uri;
1.7 raeburn 129: $dirname =~ s{^.*/([^/]*)$}{$1};
1.5 damieng 130: $res .= "<directory name=\"$dirname/\">\n";
1.12 ! raeburn 131: my (%is_course,%is_courseauthor);
1.5 damieng 132: if (ref($listref) eq 'ARRAY') {
133: my @lines = @{$listref};
134: foreach my $line (@lines) {
1.6 damieng 135: my ($path, $dom, undef, $testdir, undef, undef, undef, undef, $size, undef, $mtime, undef, undef, undef, $obs, undef) = split(/\&/, $line, 16);
1.5 damieng 136: my $isdir = ($testdir & 16384) || $dom =~ /^(user|domain)$/;
1.7 raeburn 137: $path =~ s{^/home/httpd/html/res/}{};
1.5 damieng 138: next if $path eq '.' || $path eq '..';
139: next if $path =~ /\.meta$/ || $obs || $path =~ /\.\d+\.[^.]+$/;
140: if ($dom ne 'domain') {
141: my ($udom,$uname);
142: if ($dom eq 'user') {
143: ($udom) = ($uri =~ m{^/res/($match_domain)});
144: $uname = $path;
145: } else {
146: ($udom, $uname) = ($uri =~ m{^/res/($match_domain)/($match_courseid)});
147: }
148: if ($udom ne '' && $uname ne '') {
1.12 ! raeburn 149: my $key = $udom.':'.$uname;
! 150: if (exists($is_course{$key})) {
! 151: if ($is_course{$key}) {
! 152: next unless ($is_courseauthor{$key});
! 153: }
! 154: } else {
! 155: if (&Apache::lonnet::is_course($udom, $uname)) {
! 156: $is_course{$key} = 1;
! 157: if ($env{'request.course.id'}) {
! 158: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
! 159: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
! 160: if (($cdom eq $udom) && ($cnum eq $uname)) {
! 161: if (&Apache::lonnet::allowed('mdc', $env{'request.course.id'})) {
! 162: $is_courseauthor{$key} = 1;
! 163: }
! 164: }
! 165: }
! 166: # remove courses from the list
! 167: next unless ($is_courseauthor{$key});
! 168: } else {
! 169: $is_course{$key} = 0;
! 170: }
! 171: }
1.5 damieng 172: }
173: }
1.7 raeburn 174: $path =~ s{/$}{};
1.5 damieng 175: my $name = $path;
176: if ($isdir) {
177: $res .= "<directory name=\"$name\"/>\n";
178: } else {
1.6 damieng 179: my $dt = DateTime->from_epoch(epoch => $mtime);
180: my $modified = $dt->iso8601().'Z';
181: $res .= "<file name=\"$name\" size=\"$size\" modified=\"$modified\"/>\n";
1.5 damieng 182: }
183: }
1.1 damieng 184: }
1.6 damieng 185: } elsif ($uri eq '/priv/') {
1.12 ! raeburn 186: my $referrer = $request->headers_in->{'Referer'};
! 187: my $defdom = &get_defdom($referrer);
! 188: if (!defined $defdom) {
1.6 damieng 189: $request->content_type('text/plain');
1.10 raeburn 190: $request->print(&mt('Forbidden URI: [_1]',$uri));
1.6 damieng 191: $request->status(403);
192: return OK;
193: }
194: $res .= "<directory name=\"priv\">\n";
1.12 ! raeburn 195: $res .= "<directory name=\"$defdom\"/>\n";
! 196: } elsif ($uri =~ m{^/priv/($match_domain)/$}) {
1.6 damieng 197: my $domain = $1;
1.12 ! raeburn 198: my $referrer = $request->headers_in->{'Referer'};
! 199: my $defdom = &get_defdom($referrer);
! 200: if ($domain ne $defdom) {
1.6 damieng 201: $request->content_type('text/plain');
1.10 raeburn 202: $request->print(&mt('Forbidden URI: [_1]',$uri));
1.6 damieng 203: $request->status(403);
204: return OK;
205: }
1.12 ! raeburn 206: my $defname = &get_defname($domain,$referrer);
1.6 damieng 207: $res .= "<directory name=\"$domain\">\n";
1.12 ! raeburn 208: $res .= "<directory name=\"$defname\"/>\n";
! 209: } elsif ($uri =~ m{^/priv/($match_domain)\/($match_username)/}) {
! 210: unless (&has_priv_access($uri)) {
1.6 damieng 211: $request->content_type('text/plain');
1.10 raeburn 212: $request->print(&mt('Forbidden URI: [_1]',$uri));
1.6 damieng 213: $request->status(403);
214: return OK;
215: }
1.5 damieng 216: my $dirpath = &Apache::lonnet::filelocation('', $uri);
217: if (! -e $dirpath) {
1.6 damieng 218: $request->content_type('text/plain');
1.10 raeburn 219: $request->print(&mt('Not found: [_1]',$uri));
1.5 damieng 220: $request->status(404);
221: return OK;
1.1 damieng 222: }
1.7 raeburn 223: $dirpath =~ s{/$}{};
1.9 raeburn 224: my @files;
225: if (opendir(my $dir, $dirpath)) {
226: @files = readdir($dir);
227: closedir($dir);
228: } else {
229: $request->content_type('text/plain');
1.10 raeburn 230: $request->print(&mt('Error opening directory: [_1]',$dirpath));
1.9 raeburn 231: $request->status(403);
232: return OK;
233: }
1.5 damieng 234: my $dirname = $dirpath;
1.7 raeburn 235: $dirname =~ s{^.*/([^/]*)$}{$1};
1.5 damieng 236: $res .= "<directory name=\"$dirname\">\n";
237: foreach my $name (@files) {
238: if ($name eq '.' || $name eq '..') {
239: next;
240: }
241: if ($name =~ /\.(bak|log|meta|save)$/) {
242: next;
243: }
1.8 raeburn 244: my $sb = stat($dirpath.'/'.$name);
1.5 damieng 245: my $mode = $sb->mode;
246: if (S_ISDIR($mode)) {
247: $res .= "<directory name=\"$name\"/>\n";
248: } else {
249: $res .= "<file name=\"$name\"";
250: my $size = $sb->size; # total size of file, in bytes
251: $res .= " size=\"$size\"";
252: my $mtime = $sb->mtime; # last modify time in seconds since the epoch
253: my $dt = DateTime->from_epoch(epoch => $mtime);
254: my $modified = $dt->iso8601().'Z';
255: $res .= " modified=\"$modified\"";
256: $res .= "/>\n";
257: }
1.1 damieng 258: }
1.6 damieng 259: } else {
260: $request->content_type('text/plain');
1.10 raeburn 261: $request->print(&mt('Not found: [_1]',$uri));
1.6 damieng 262: $request->status(404);
263: return OK;
1.1 damieng 264: }
265: $res .= "</directory>\n";
266: &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
267: $request->print($res);
268: return OK;
269: }
270:
1.12 ! raeburn 271: sub has_priv_access {
! 272: my ($uri) = @_;
! 273: my ($ownername,$ownerdom,$ownerhome) =
! 274: &Apache::lonnet::constructaccess($uri);
! 275: my $allowed;
! 276: if (($ownername ne '') && ($ownerdom ne '') && ($ownerhome ne '')) {
! 277: unless ($ownerhome eq 'no_host') {
! 278: my @hosts = &Apache::lonnet::current_machine_ids();
! 279: if (grep(/^\Q$ownerhome\E$/,@hosts)) {
! 280: $allowed = 1;
! 281: }
! 282: }
! 283: }
! 284: return $allowed;
! 285: }
! 286:
! 287: sub get_defdom {
! 288: my ($referrer) = @_;
! 289: my $defdom;
! 290: if ($env{'request.role'} =~ m{^au\./($match_domain)/$}) {
! 291: $defdom = $1;
! 292: } elsif ($env{'request.role'} =~ m{^(?:ca|aa)\.($match_domain)/($match_username)$}) {
! 293: $defdom = $1;
! 294: } elsif ($env{'request.course.id'}) {
! 295: if ($referrer =~ m{\Qfile=/daxeopen/priv/\E($match_domain)/($match_username)/}) {
! 296: my ($possdom,$possuname) = ($1,$2);
! 297: if (&Apache::lonnet::is_course($possdom,$possuname)) {
! 298: my $crsurl = &Apache::lonnet::courseid_to_courseurl($env{'request.course.id'});
! 299: if ($crsurl eq "/$possdom/$possuname") {
! 300: $defdom = $possdom;
! 301: }
! 302: } else {
! 303: if (&Apache::lonnet::domain($possdom) ne '') {
! 304: $defdom = $possdom;
! 305: }
! 306: }
! 307: }
! 308: }
! 309: if ($defdom eq '') {
! 310: my ($is_adv,$is_author) = &Apache::lonnet::is_advanced_user($env{'user.domain'},$env{'user.name'});
! 311: if ($is_author) {
! 312: $defdom = $env{'user.domain'};
! 313: }
! 314: }
! 315: return $defdom;
! 316: }
! 317:
! 318: sub get_defname {
! 319: my ($domain,$referrer) = @_;
! 320: my $defname;
! 321: if ($env{'request.role'} eq "au./$domain/") {
! 322: $defname = $env{'user.name'};
! 323: } elsif ($env{'request.role'} =~ m{^(?:ca|aa)\./\Q$domain\E/($match_username)$}) {
! 324: $defname = $1;
! 325: } elsif ($env{'request.course.id'}) {
! 326: if ($referrer =~ m{\Qfile=/daxeopen/priv/\E($match_domain)/($match_username)/}) {
! 327: my ($possdom,$possuname) = ($1,$2);
! 328: if ($domain eq $possdom) {
! 329: if (&Apache::lonnet::is_course($possdom,$possuname)) {
! 330: my $crsurl = &Apache::lonnet::courseid_to_courseurl($env{'request.course.id'});
! 331: if ($crsurl eq "/$possdom/$possuname") {
! 332: $defname = $possuname;
! 333: }
! 334: } else {
! 335: unless (&Apache::lonnet::homeserver($possuname,$possdom) eq 'no_host') {
! 336: $defname = $possuname;
! 337: }
! 338: }
! 339: }
! 340: }
! 341: }
! 342: if ($defname eq '') {
! 343: my ($is_adv,$is_author) = &Apache::lonnet::is_advanced_user($domain,$env{'user.name'});
! 344: if ($is_author) {
! 345: $defname = $env{'user.name'};
! 346: }
! 347: }
! 348: return $defname;
! 349: }
! 350:
1.1 damieng 351: 1;
352: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>