File:  [LON-CAPA] / loncom / homework / daxeopen.pm
Revision 1.11: download - view: text, annotated - select for diffs
Wed Aug 23 22:34:48 2023 UTC (8 months, 3 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Coding style: eliminate leaning toothpicks.

    1: # The LearningOnline Network
    2: # Opening converted problems and directory listings for Daxe
    3: #
    4: # $Id: daxeopen.pm,v 1.11 2023/08/23 22:34:48 raeburn Exp $
    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: use strict;
   32: 
   33: use Apache::Constants qw(:common);
   34: use DateTime;
   35: use Try::Tiny;
   36: use File::stat;
   37: use Fcntl ':mode';
   38: 
   39: use LONCAPA qw(:match);
   40: use Apache::loncommon;
   41: use Apache::lonnet;
   42: use Apache::pre_xml;
   43: use Apache::html_to_xml;
   44: use Apache::post_xml;
   45: use Apache::lonlocal;
   46: 
   47: sub handler {
   48:     my $request = shift;
   49:     my $uri = $request->uri;
   50:     $uri =~ s{^/daxeopen}{};
   51:     &Apache::loncommon::no_cache($request);
   52:     if ($uri =~ m{/$}) {
   53:         return directory_listing($uri, $request);
   54:     } elsif ($uri =~ m{^/priv/.*\.(task|problem|exam|quiz|assess|survey|library|xml|html|htm|xhtml|xhtm)$}) {
   55:         return convert_problem($uri, $request);
   56:     } else {
   57:         # Apache should send other files directly
   58:         $request->status(406);
   59:         return OK;
   60:     }
   61: }
   62: 
   63: sub convert_problem {
   64:     my ($uri, $request) = @_;
   65:     if ($uri =~ m{^/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(&mt('Forbidden URI: [_1]',$uri));
   71:             $request->status(403);
   72:             return OK;
   73:         }
   74:     }
   75:     my $file = &Apache::lonnet::filelocation('', $uri);
   76:     &Apache::lonnet::repcopy($file);
   77:     if (! -e $file) {
   78:         $request->status(404);
   79:         return OK;
   80:     }
   81:     try {
   82:         my $warnings = 0; # no warning printed
   83:         my $textref = &Apache::pre_xml::pre_xml($file, $warnings);
   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);
   91:         my $text = &Apache::post_xml::post_xml($textref, $file, $Apache::lonnet::perlvar{'lonDocRoot'}, $warnings);
   92:         &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
   93:         $request->print($text);
   94:         return OK;
   95:     } catch {
   96:         $request->content_type('text/plain');
   97:         $request->print(&mt('convert failed for [_1]:',$file)." $_");
   98:         $request->status(406);
   99:         return OK;
  100:     };
  101: }
  102: 
  103: sub directory_listing {
  104:     my ($uri, $request) = @_;
  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";
  109:         $res .= "<directory name=\"priv\"/>\n";
  110:         $res .= "<directory name=\"res\"/>\n";
  111:     } elsif ($uri !~ m{^/(priv|res)/}) {
  112:         $request->content_type('text/plain');
  113:         $request->print(&mt('Not found: [_1]',$uri));
  114:         $request->status(404);
  115:         return OK;
  116:     } elsif ($uri =~ m{^/res/}) {
  117:         # NOTE: dirlist does not return an error for /res/idontexist/
  118: 	my ($listref, $listerror) = &Apache::lonnet::dirlist($uri);
  119: 	if ($listerror) {
  120:             $request->content_type('text/plain');
  121:             $request->print(&mt('listing error: [_1]',$listerror));
  122:             $request->status(406);
  123:             return OK;
  124: 	} elsif ($uri =~ m{^/res/[^/]+/$} && scalar(@{$listref}) == 0) {
  125:             $request->content_type('text/plain');
  126:             $request->print(&mt('Not found: [_1]',$uri));
  127:             $request->status(404);
  128:             return OK;
  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) {
  136:                 my ($path, $dom, undef, $testdir, undef, undef, undef, undef, $size, undef, $mtime, undef, undef, undef, $obs, undef) = split(/\&/, $line, 16);
  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 {
  159:                     my $dt = DateTime->from_epoch(epoch => $mtime);
  160:                     my $modified = $dt->iso8601().'Z';
  161:                     $res .= "<file name=\"$name\" size=\"$size\" modified=\"$modified\"/>\n";
  162:                 }
  163:             }
  164:         }
  165:     } elsif ($uri eq '/priv/') {
  166:         my $udom = $env{'user.domain'};
  167:         if (!defined $udom) {
  168:             $request->content_type('text/plain');
  169:             $request->print(&mt('Forbidden URI: [_1]',$uri));
  170:             $request->status(403);
  171:             return OK;
  172:         }
  173:         $res .= "<directory name=\"priv\">\n";
  174:         $res .= "<directory name=\"$udom\"/>\n";
  175:     } elsif ($uri =~ m{^/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(&mt('Forbidden URI: [_1]',$uri));
  181:             $request->status(403);
  182:             return OK;
  183:         }
  184:         $res .= "<directory name=\"$domain\">\n";
  185:         $res .= "<directory name=\"$uname\"/>\n";
  186:     } elsif ($uri =~ m{^/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(&mt('Forbidden URI: [_1]',$uri));
  192:             $request->status(403);
  193:             return OK;
  194:         }
  195:         my $dirpath = &Apache::lonnet::filelocation('', $uri);
  196:         if (! -e $dirpath) {
  197:             $request->content_type('text/plain');
  198:             $request->print(&mt('Not found: [_1]',$uri));
  199:             $request->status(404);
  200:             return OK;
  201:         }
  202:         $dirpath =~ s{/$}{};
  203:         my @files;
  204:         if (opendir(my $dir, $dirpath)) {
  205:             @files = readdir($dir);
  206:             closedir($dir);
  207:         } else {
  208:             $request->content_type('text/plain');
  209:             $request->print(&mt('Error opening directory: [_1]',$dirpath));
  210:             $request->status(403);
  211:             return OK;
  212:         }
  213:         my $dirname = $dirpath;
  214:         $dirname =~ s{^.*/([^/]*)$}{$1};
  215:         $res .= "<directory name=\"$dirname\">\n";
  216:         foreach my $name (@files) {
  217:             if ($name eq '.' || $name eq '..') {
  218:                 next;
  219:             }
  220:             if ($name =~ /\.(bak|log|meta|save)$/) {
  221:                 next;
  222:             }
  223:             my $sb = stat($dirpath.'/'.$name);
  224:             my $mode = $sb->mode;
  225:             if (S_ISDIR($mode)) {
  226:                 $res .= "<directory name=\"$name\"/>\n";
  227:             } else {
  228:                 $res .= "<file name=\"$name\"";
  229:                 my $size = $sb->size; # total size of file, in bytes
  230:                 $res .= " size=\"$size\"";
  231:                 my $mtime = $sb->mtime; # last modify time in seconds since the epoch
  232:                 my $dt = DateTime->from_epoch(epoch => $mtime);
  233:                 my $modified = $dt->iso8601().'Z';
  234:                 $res .= " modified=\"$modified\"";
  235:                 $res .= "/>\n";
  236:             }
  237:         }
  238:     } else {
  239:         $request->content_type('text/plain');
  240:         $request->print(&mt('Not found: [_1]',$uri));
  241:         $request->status(404);
  242:         return OK;
  243:     }
  244:     $res .= "</directory>\n";
  245:     &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
  246:     $request->print($res);
  247:     return OK;
  248: }
  249: 
  250: 1;
  251: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>