version 1.1, 2015/12/03 20:40:27
|
version 1.12, 2023/08/23 22:57:39
|
Line 28
|
Line 28
|
### |
### |
|
|
package Apache::daxeopen; |
package Apache::daxeopen; |
|
use strict; |
|
|
use Apache::Constants; |
use Apache::Constants qw(:common); |
use DateTime; |
use DateTime; |
use Try::Tiny; |
use Try::Tiny; |
use File::stat; |
use File::stat; |
use Fcntl ':mode'; |
use Fcntl ':mode'; |
|
|
|
use LONCAPA qw(:match); |
use Apache::loncommon; |
use Apache::loncommon; |
use Apache::lonnet; |
use Apache::lonnet; |
use Apache::pre_xml; |
use Apache::pre_xml; |
use Apache::html_to_xml; |
use Apache::html_to_xml; |
use Apache::post_xml; |
use Apache::post_xml; |
|
use Apache::lonlocal; |
|
|
sub handler { |
sub handler { |
my $request = shift; |
my $request = shift; |
my $uri = $request->uri; |
my $uri = $request->uri; |
$uri =~ s/^\/daxeopen//; |
$uri =~ s{^/daxeopen}{}; |
&Apache::loncommon::no_cache($request); |
&Apache::loncommon::no_cache($request); |
if ($uri =~ /\/$/) { |
if ($uri =~ m{/$}) { |
return directory_listing($uri, $request); |
return directory_listing($uri, $request); |
} elsif ($uri =~ /\.(task|problem|exam|quiz|assess|survey|library)$/) { |
} elsif ($uri =~ m{^/priv/.*\.(task|problem|exam|quiz|assess|survey|library|xml|html|htm|xhtml|xhtm)$}) { |
return convert_problem($uri, $request); |
return convert_problem($uri, $request); |
} else { |
} else { |
# Apache should send other files directly |
# Apache should send other files directly |
return HTTP_NOT_ACCEPTABLE; |
$request->status(406); |
|
return OK; |
} |
} |
} |
} |
|
|
sub convert_problem { |
sub convert_problem { |
my ($uri, $request) = @_; |
my ($uri, $request) = @_; |
|
if ($uri =~ m{^/priv/$match_domain/$match_username/}) { |
|
unless (&has_priv_access($uri)) { |
|
$request->content_type('text/plain'); |
|
$request->print(&mt('Forbidden URI: [_1]',$uri)); |
|
$request->status(403); |
|
return OK; |
|
} |
|
} |
my $file = &Apache::lonnet::filelocation('', $uri); |
my $file = &Apache::lonnet::filelocation('', $uri); |
&Apache::lonnet::repcopy($file); |
&Apache::lonnet::repcopy($file); |
if (! -e $file) { |
if (! -e $file) { |
return HTTP_NOT_FOUND; |
$request->status(404); |
|
return OK; |
} |
} |
try { |
try { |
my $warnings = 0; # no warning printed |
my $warnings = 0; # no warning printed |
my $textref = &Apache::pre_xml::pre_xml($file, $warnings); |
my $textref = &Apache::pre_xml::pre_xml($file, $warnings); |
$textref = &Apache::html_to_xml::html_to_xml($textref, $warnings); |
my $case_sensitive; |
my $text = &Apache::post_xml::post_xml($textref, $file, $warnings); |
if ($uri =~ /\.(task)$/) { |
|
$case_sensitive = 1; |
|
} else { |
|
$case_sensitive = 0; |
|
} |
|
$textref = &Apache::html_to_xml::html_to_xml($textref, $warnings, $case_sensitive); |
|
my $text = &Apache::post_xml::post_xml($textref, $file, $Apache::lonnet::perlvar{'lonDocRoot'}, $warnings); |
&Apache::loncommon::content_type($request, 'text/xml', 'utf-8'); |
&Apache::loncommon::content_type($request, 'text/xml', 'utf-8'); |
$request->print($text); |
$request->print($text); |
return OK; |
return OK; |
} catch { |
} catch { |
die "convert failed for $file: $_"; |
$request->content_type('text/plain'); |
#$request->print('<?xml version="1.0" encoding="UTF-8"?>'."\n"); |
$request->print(&mt('convert failed for [_1]:',$file)." $_"); |
#$request->print("<problem>\n"); |
$request->status(406); |
#$request->print("convert failed for $file: $_"); |
return OK; |
#$request->print("</problem>\n"); |
|
#return OK; |
|
}; |
}; |
} |
} |
|
|
sub directory_listing { |
sub directory_listing { |
my ($uri, $request) = @_; |
my ($uri, $request) = @_; |
my $dirpath = &Apache::lonnet::filelocation('', $uri); |
|
if (! -e $dirpath) { |
|
return HTTP_NOT_FOUND; |
|
} |
|
$dirpath =~ s/\/$//; |
|
opendir my $dir, $dirpath or die "Cannot open directory: $dirpath"; |
|
my @files = readdir $dir; |
|
closedir $dir; |
|
my $res = '<?xml version="1.0" encoding="UTF-8"?>'."\n"; |
my $res = '<?xml version="1.0" encoding="UTF-8"?>'."\n"; |
my $dirname = $dirpath; |
if ($uri eq '/') { |
$dirname =~ s/^.*\/([^\/]*)$/$1/; |
# root: let users browse /res |
$res .= "<directory name=\"$dirname\">\n"; |
$res .= "<directory name=\"/\">\n"; |
foreach my $name (@files) { |
$res .= "<directory name=\"priv\"/>\n"; |
if ($name eq '.' || $name eq '..') { |
$res .= "<directory name=\"res\"/>\n"; |
next; |
} elsif ($uri !~ m{^/(priv|res)/}) { |
} |
$request->content_type('text/plain'); |
if ($name =~ /\.(bak|log|meta|save)$/) { |
$request->print(&mt('Not found: [_1]',$uri)); |
next; |
$request->status(404); |
} |
return OK; |
$sb = stat($dirpath.'/'.$name); |
} elsif ($uri =~ m{^/res/}) { |
my $mode = $sb->mode; |
# NOTE: dirlist does not return an error for /res/idontexist/ |
if (S_ISDIR($mode)) { |
my ($listref, $listerror) = &Apache::lonnet::dirlist($uri); |
$res .= "<directory name=\"$name\"/>\n"; |
if ($listerror) { |
|
$request->content_type('text/plain'); |
|
$request->print(&mt('listing error: [_1]',$listerror)); |
|
$request->status(406); |
|
return OK; |
|
} elsif ($uri =~ m{^/res/[^/]+/$} && scalar(@{$listref}) == 0) { |
|
$request->content_type('text/plain'); |
|
$request->print(&mt('Not found: [_1]',$uri)); |
|
$request->status(404); |
|
return OK; |
|
} |
|
my $dirname = $uri; |
|
$dirname =~ s{^.*/([^/]*)$}{$1}; |
|
$res .= "<directory name=\"$dirname/\">\n"; |
|
my (%is_course,%is_courseauthor); |
|
if (ref($listref) eq 'ARRAY') { |
|
my @lines = @{$listref}; |
|
foreach my $line (@lines) { |
|
my ($path, $dom, undef, $testdir, undef, undef, undef, undef, $size, undef, $mtime, undef, undef, undef, $obs, undef) = split(/\&/, $line, 16); |
|
my $isdir = ($testdir & 16384) || $dom =~ /^(user|domain)$/; |
|
$path =~ s{^/home/httpd/html/res/}{}; |
|
next if $path eq '.' || $path eq '..'; |
|
next if $path =~ /\.meta$/ || $obs || $path =~ /\.\d+\.[^.]+$/; |
|
if ($dom ne 'domain') { |
|
my ($udom,$uname); |
|
if ($dom eq 'user') { |
|
($udom) = ($uri =~ m{^/res/($match_domain)}); |
|
$uname = $path; |
|
} else { |
|
($udom, $uname) = ($uri =~ m{^/res/($match_domain)/($match_courseid)}); |
|
} |
|
if ($udom ne '' && $uname ne '') { |
|
my $key = $udom.':'.$uname; |
|
if (exists($is_course{$key})) { |
|
if ($is_course{$key}) { |
|
next unless ($is_courseauthor{$key}); |
|
} |
|
} else { |
|
if (&Apache::lonnet::is_course($udom, $uname)) { |
|
$is_course{$key} = 1; |
|
if ($env{'request.course.id'}) { |
|
my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'}; |
|
my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'}; |
|
if (($cdom eq $udom) && ($cnum eq $uname)) { |
|
if (&Apache::lonnet::allowed('mdc', $env{'request.course.id'})) { |
|
$is_courseauthor{$key} = 1; |
|
} |
|
} |
|
} |
|
# remove courses from the list |
|
next unless ($is_courseauthor{$key}); |
|
} else { |
|
$is_course{$key} = 0; |
|
} |
|
} |
|
} |
|
} |
|
$path =~ s{/$}{}; |
|
my $name = $path; |
|
if ($isdir) { |
|
$res .= "<directory name=\"$name\"/>\n"; |
|
} else { |
|
my $dt = DateTime->from_epoch(epoch => $mtime); |
|
my $modified = $dt->iso8601().'Z'; |
|
$res .= "<file name=\"$name\" size=\"$size\" modified=\"$modified\"/>\n"; |
|
} |
|
} |
|
} |
|
} elsif ($uri eq '/priv/') { |
|
my $referrer = $request->headers_in->{'Referer'}; |
|
my $defdom = &get_defdom($referrer); |
|
if (!defined $defdom) { |
|
$request->content_type('text/plain'); |
|
$request->print(&mt('Forbidden URI: [_1]',$uri)); |
|
$request->status(403); |
|
return OK; |
|
} |
|
$res .= "<directory name=\"priv\">\n"; |
|
$res .= "<directory name=\"$defdom\"/>\n"; |
|
} elsif ($uri =~ m{^/priv/($match_domain)/$}) { |
|
my $domain = $1; |
|
my $referrer = $request->headers_in->{'Referer'}; |
|
my $defdom = &get_defdom($referrer); |
|
if ($domain ne $defdom) { |
|
$request->content_type('text/plain'); |
|
$request->print(&mt('Forbidden URI: [_1]',$uri)); |
|
$request->status(403); |
|
return OK; |
|
} |
|
my $defname = &get_defname($domain,$referrer); |
|
$res .= "<directory name=\"$domain\">\n"; |
|
$res .= "<directory name=\"$defname\"/>\n"; |
|
} elsif ($uri =~ m{^/priv/($match_domain)\/($match_username)/}) { |
|
unless (&has_priv_access($uri)) { |
|
$request->content_type('text/plain'); |
|
$request->print(&mt('Forbidden URI: [_1]',$uri)); |
|
$request->status(403); |
|
return OK; |
|
} |
|
my $dirpath = &Apache::lonnet::filelocation('', $uri); |
|
if (! -e $dirpath) { |
|
$request->content_type('text/plain'); |
|
$request->print(&mt('Not found: [_1]',$uri)); |
|
$request->status(404); |
|
return OK; |
|
} |
|
$dirpath =~ s{/$}{}; |
|
my @files; |
|
if (opendir(my $dir, $dirpath)) { |
|
@files = readdir($dir); |
|
closedir($dir); |
} else { |
} else { |
$res .= "<file name=\"$name\""; |
$request->content_type('text/plain'); |
my $size = $sb->size; # total size of file, in bytes |
$request->print(&mt('Error opening directory: [_1]',$dirpath)); |
$res .= " size=\"$size\""; |
$request->status(403); |
my $mtime = $sb->mtime; # last modify time in seconds since the epoch |
return OK; |
my $dt = DateTime->from_epoch(epoch => $mtime); |
|
my $modified = $dt->iso8601().'Z'; |
|
$res .= " modified=\"$modified\""; |
|
$res .= "/>\n"; |
|
} |
} |
|
my $dirname = $dirpath; |
|
$dirname =~ s{^.*/([^/]*)$}{$1}; |
|
$res .= "<directory name=\"$dirname\">\n"; |
|
foreach my $name (@files) { |
|
if ($name eq '.' || $name eq '..') { |
|
next; |
|
} |
|
if ($name =~ /\.(bak|log|meta|save)$/) { |
|
next; |
|
} |
|
my $sb = stat($dirpath.'/'.$name); |
|
my $mode = $sb->mode; |
|
if (S_ISDIR($mode)) { |
|
$res .= "<directory name=\"$name\"/>\n"; |
|
} else { |
|
$res .= "<file name=\"$name\""; |
|
my $size = $sb->size; # total size of file, in bytes |
|
$res .= " size=\"$size\""; |
|
my $mtime = $sb->mtime; # last modify time in seconds since the epoch |
|
my $dt = DateTime->from_epoch(epoch => $mtime); |
|
my $modified = $dt->iso8601().'Z'; |
|
$res .= " modified=\"$modified\""; |
|
$res .= "/>\n"; |
|
} |
|
} |
|
} else { |
|
$request->content_type('text/plain'); |
|
$request->print(&mt('Not found: [_1]',$uri)); |
|
$request->status(404); |
|
return OK; |
} |
} |
$res .= "</directory>\n"; |
$res .= "</directory>\n"; |
&Apache::loncommon::content_type($request, 'text/xml', 'utf-8'); |
&Apache::loncommon::content_type($request, 'text/xml', 'utf-8'); |
Line 125 sub directory_listing {
|
Line 268 sub directory_listing {
|
return OK; |
return OK; |
} |
} |
|
|
# NOTE: binaries should be sent directly be Apache |
sub has_priv_access { |
# sub send_binary { |
my ($uri) = @_; |
# my ($request, $filepath) = @_; |
my ($ownername,$ownerdom,$ownerhome) = |
# |
&Apache::lonnet::constructaccess($uri); |
# $buffer = ''; |
my $allowed; |
# if (!open(FILE, "<", $filepath)) { |
if (($ownername ne '') && ($ownerdom ne '') && ($ownerhome ne '')) { |
# return HTTP_NOT_FOUND; |
unless ($ownerhome eq 'no_host') { |
# } |
my @hosts = &Apache::lonnet::current_machine_ids(); |
# binmode(FILE); |
if (grep(/^\Q$ownerhome\E$/,@hosts)) { |
# |
$allowed = 1; |
# # Read file in 32K blocks |
} |
# while ((read(FILE, $buffer, 32768)) != 0) { |
} |
# $request->print($buffer); |
} |
# } |
return $allowed; |
# |
} |
# if (!close(FILE)) { |
|
# &Apache::lonnet::logthis("Error closing the file $filepath"); |
sub get_defdom { |
# } |
my ($referrer) = @_; |
# return OK; |
my $defdom; |
# } |
if ($env{'request.role'} =~ m{^au\./($match_domain)/$}) { |
|
$defdom = $1; |
|
} elsif ($env{'request.role'} =~ m{^(?:ca|aa)\.($match_domain)/($match_username)$}) { |
|
$defdom = $1; |
|
} elsif ($env{'request.course.id'}) { |
|
if ($referrer =~ m{\Qfile=/daxeopen/priv/\E($match_domain)/($match_username)/}) { |
|
my ($possdom,$possuname) = ($1,$2); |
|
if (&Apache::lonnet::is_course($possdom,$possuname)) { |
|
my $crsurl = &Apache::lonnet::courseid_to_courseurl($env{'request.course.id'}); |
|
if ($crsurl eq "/$possdom/$possuname") { |
|
$defdom = $possdom; |
|
} |
|
} else { |
|
if (&Apache::lonnet::domain($possdom) ne '') { |
|
$defdom = $possdom; |
|
} |
|
} |
|
} |
|
} |
|
if ($defdom eq '') { |
|
my ($is_adv,$is_author) = &Apache::lonnet::is_advanced_user($env{'user.domain'},$env{'user.name'}); |
|
if ($is_author) { |
|
$defdom = $env{'user.domain'}; |
|
} |
|
} |
|
return $defdom; |
|
} |
|
|
|
sub get_defname { |
|
my ($domain,$referrer) = @_; |
|
my $defname; |
|
if ($env{'request.role'} eq "au./$domain/") { |
|
$defname = $env{'user.name'}; |
|
} elsif ($env{'request.role'} =~ m{^(?:ca|aa)\./\Q$domain\E/($match_username)$}) { |
|
$defname = $1; |
|
} elsif ($env{'request.course.id'}) { |
|
if ($referrer =~ m{\Qfile=/daxeopen/priv/\E($match_domain)/($match_username)/}) { |
|
my ($possdom,$possuname) = ($1,$2); |
|
if ($domain eq $possdom) { |
|
if (&Apache::lonnet::is_course($possdom,$possuname)) { |
|
my $crsurl = &Apache::lonnet::courseid_to_courseurl($env{'request.course.id'}); |
|
if ($crsurl eq "/$possdom/$possuname") { |
|
$defname = $possuname; |
|
} |
|
} else { |
|
unless (&Apache::lonnet::homeserver($possuname,$possdom) eq 'no_host') { |
|
$defname = $possuname; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
if ($defname eq '') { |
|
my ($is_adv,$is_author) = &Apache::lonnet::is_advanced_user($domain,$env{'user.name'}); |
|
if ($is_author) { |
|
$defname = $env{'user.name'}; |
|
} |
|
} |
|
return $defname; |
|
} |
|
|
1; |
1; |
__END__ |
__END__ |