--- loncom/homework/daxeopen.pm 2023/08/23 20:33:06 1.7 +++ loncom/homework/daxeopen.pm 2023/08/23 22:25:48 1.10 @@ -1,7 +1,7 @@ # The LearningOnline Network # Opening converted problems and directory listings for Daxe # -# $Id: daxeopen.pm,v 1.7 2023/08/23 20:33:06 raeburn Exp $ +# $Id: daxeopen.pm,v 1.10 2023/08/23 22:25:48 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -28,8 +28,9 @@ ### package Apache::daxeopen; +use strict; -use Apache::Constants; +use Apache::Constants qw(:common); use DateTime; use Try::Tiny; use File::stat; @@ -41,7 +42,7 @@ use Apache::lonnet; use Apache::pre_xml; use Apache::html_to_xml; use Apache::post_xml; - +use Apache::lonlocal; sub handler { my $request = shift; @@ -61,13 +62,12 @@ sub handler { sub convert_problem { my ($uri, $request) = @_; - if ($uri =~ m{^/priv/([^/]+)/([^/]+)/}) { my ($domain, $user) = ($1, $2); my ($uname, $udom) = ($env{'user.name'}, $env{'user.domain'}); if (!defined $uname || !defined $udom || $domain ne $udom || $user ne $uname) { $request->content_type('text/plain'); - $request->print("Forbidden URI: $uri"); + $request->print(&mt('Forbidden URI: [_1]',$uri)); $request->status(403); return OK; } @@ -88,13 +88,13 @@ sub convert_problem { $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); + my $text = &Apache::post_xml::post_xml($textref, $file, $Apache::lonnet::perlvar{'lonDocRoot'}, $warnings); &Apache::loncommon::content_type($request, 'text/xml', 'utf-8'); $request->print($text); return OK; } catch { $request->content_type('text/plain'); - $request->print("convert failed for $file: $_"); + $request->print(&mt('convert failed for [_1]:',$file)." $_"); $request->status(406); return OK; }; @@ -110,20 +110,20 @@ sub directory_listing { $res .= "\n"; } elsif ($uri !~ /^\/(priv|res)\//) { $request->content_type('text/plain'); - $request->print("Not found: $uri"); + $request->print(&mt('Not found: [_1]',$uri)); $request->status(404); return OK; } elsif ($uri =~ m{^/res/}) { # NOTE: dirlist does not return an error for /res/idontexist/ - (my $listref, $listerror) = &Apache::lonnet::dirlist($uri); + my ($listref, $listerror) = &Apache::lonnet::dirlist($uri); if ($listerror) { $request->content_type('text/plain'); - $request->print("listing error: $listerror"); + $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("Not found: $uri"); + $request->print(&mt('Not found: [_1]',$uri)); $request->status(404); return OK; } @@ -166,7 +166,7 @@ sub directory_listing { my $udom = $env{'user.domain'}; if (!defined $udom) { $request->content_type('text/plain'); - $request->print("Forbidden URI: $uri"); + $request->print(&mt('Forbidden URI: [_1]',$uri)); $request->status(403); return OK; } @@ -177,7 +177,7 @@ sub directory_listing { my ($uname, $udom) = ($env{'user.name'}, $env{'user.domain'}); if (!defined $uname || !defined $udom || $domain ne $udom) { $request->content_type('text/plain'); - $request->print("Forbidden URI: $uri"); + $request->print(&mt('Forbidden URI: [_1]',$uri)); $request->status(403); return OK; } @@ -188,21 +188,28 @@ sub directory_listing { my ($uname, $udom) = ($env{'user.name'}, $env{'user.domain'}); if (!defined $uname || !defined $udom || $domain ne $udom || $user ne $uname) { $request->content_type('text/plain'); - $request->print("Forbidden URI: $uri"); + $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("Not found: $uri"); + $request->print(&mt('Not found: [_1]',$uri)); $request->status(404); return OK; } $dirpath =~ s{/$}{}; - opendir my $dir, $dirpath or die "Cannot open directory: $dirpath"; - my @files = readdir $dir; - closedir $dir; + my @files; + if (opendir(my $dir, $dirpath)) { + @files = readdir($dir); + closedir($dir); + } else { + $request->content_type('text/plain'); + $request->print(&mt('Error opening directory: [_1]',$dirpath)); + $request->status(403); + return OK; + } my $dirname = $dirpath; $dirname =~ s{^.*/([^/]*)$}{$1}; $res .= "\n"; @@ -213,7 +220,7 @@ sub directory_listing { if ($name =~ /\.(bak|log|meta|save)$/) { next; } - $sb = stat($dirpath.'/'.$name); + my $sb = stat($dirpath.'/'.$name); my $mode = $sb->mode; if (S_ISDIR($mode)) { $res .= "\n"; @@ -230,7 +237,7 @@ sub directory_listing { } } else { $request->content_type('text/plain'); - $request->print("Not found: $uri"); + $request->print(&mt('Not found: [_1]',$uri)); $request->status(404); return OK; }