version 1.1, 2015/12/03 20:40:27
|
version 1.5, 2017/02/23 21:32:08
|
Line 35 use Try::Tiny;
|
Line 35 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; |
Line 49 sub handler {
|
Line 50 sub handler {
|
&Apache::loncommon::no_cache($request); |
&Apache::loncommon::no_cache($request); |
if ($uri =~ /\/$/) { |
if ($uri =~ /\/$/) { |
return directory_listing($uri, $request); |
return directory_listing($uri, $request); |
} elsif ($uri =~ /\.(task|problem|exam|quiz|assess|survey|library)$/) { |
} elsif ($uri =~ /\.(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; |
} |
} |
} |
} |
|
|
Line 63 sub convert_problem {
|
Line 65 sub convert_problem {
|
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, $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("convert failed for $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=\"res\"/>\n"; |
if ($name eq '.' || $name eq '..') { |
} elsif ($uri !~ /^\/(priv|res)\//) { |
next; |
$request->status(404); |
|
return OK; |
|
} elsif ($uri =~ /^\/res\//) { |
|
(my $listref, $listerror) = &Apache::lonnet::dirlist($uri); |
|
if ($listerror) { |
|
$request->content_type('text/plain'); |
|
$request->print("listing error: $listerror"); |
|
$request->status(406); |
|
return OK; |
|
} |
|
my $dirname = $uri; |
|
$dirname =~ s/^.*\/([^\/]*)$/$1/; |
|
$res .= "<directory name=\"$dirname/\">\n"; |
|
if (ref($listref) eq 'ARRAY') { |
|
my @lines = @{$listref}; |
|
foreach my $line (@lines) { |
|
my ($path, $dom, undef, $testdir, undef, undef, undef, undef, undef, undef, undef, 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 '') { |
|
# remove courses from the list |
|
next if (&Apache::lonnet::is_course($udom, $uname)); |
|
} |
|
} |
|
$path =~ s/\/$//; |
|
my $name = $path; |
|
if ($isdir) { |
|
$res .= "<directory name=\"$name\"/>\n"; |
|
} else { |
|
$res .= "<file name=\"$name\"/>\n"; |
|
} |
|
} |
} |
} |
if ($name =~ /\.(bak|log|meta|save)$/) { |
} else { |
next; |
my $dirpath = &Apache::lonnet::filelocation('', $uri); |
|
if (! -e $dirpath) { |
|
$request->status(404); |
|
return OK; |
} |
} |
$sb = stat($dirpath.'/'.$name); |
$dirpath =~ s/\/$//; |
my $mode = $sb->mode; |
opendir my $dir, $dirpath or die "Cannot open directory: $dirpath"; |
if (S_ISDIR($mode)) { |
my @files = readdir $dir; |
$res .= "<directory name=\"$name\"/>\n"; |
closedir $dir; |
} else { |
my $dirname = $dirpath; |
$res .= "<file name=\"$name\""; |
$dirname =~ s/^.*\/([^\/]*)$/$1/; |
my $size = $sb->size; # total size of file, in bytes |
$res .= "<directory name=\"$dirname\">\n"; |
$res .= " size=\"$size\""; |
foreach my $name (@files) { |
my $mtime = $sb->mtime; # last modify time in seconds since the epoch |
if ($name eq '.' || $name eq '..') { |
my $dt = DateTime->from_epoch(epoch => $mtime); |
next; |
my $modified = $dt->iso8601().'Z'; |
} |
$res .= " modified=\"$modified\""; |
if ($name =~ /\.(bak|log|meta|save)$/) { |
$res .= "/>\n"; |
next; |
|
} |
|
$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"; |
|
} |
} |
} |
} |
} |
$res .= "</directory>\n"; |
$res .= "</directory>\n"; |
Line 125 sub directory_listing {
|
Line 183 sub directory_listing {
|
return OK; |
return OK; |
} |
} |
|
|
# NOTE: binaries should be sent directly be Apache |
|
# sub send_binary { |
|
# my ($request, $filepath) = @_; |
|
# |
|
# $buffer = ''; |
|
# if (!open(FILE, "<", $filepath)) { |
|
# return HTTP_NOT_FOUND; |
|
# } |
|
# binmode(FILE); |
|
# |
|
# # Read file in 32K blocks |
|
# while ((read(FILE, $buffer, 32768)) != 0) { |
|
# $request->print($buffer); |
|
# } |
|
# |
|
# if (!close(FILE)) { |
|
# &Apache::lonnet::logthis("Error closing the file $filepath"); |
|
# } |
|
# return OK; |
|
# } |
|
|
|
1; |
1; |
__END__ |
__END__ |