File:  [LON-CAPA] / loncom / homework / daxeopen.pm
Revision 1.8: download - view: text, annotated - select for diffs
Wed Aug 23 20:39:06 2023 UTC (8 months, 4 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Coding style.  use strict

    1: # The LearningOnline Network
    2: # Opening converted problems and directory listings for Daxe
    3: #
    4: # $Id: daxeopen.pm,v 1.8 2023/08/23 20:39:06 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: 
   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:     
   66:     if ($uri =~ m{^/priv/([^/]+)/([^/]+)/}) {
   67:         my ($domain, $user) = ($1, $2);
   68:         my ($uname, $udom) = ($env{'user.name'}, $env{'user.domain'});
   69:         if (!defined $uname || !defined $udom || $domain ne $udom || $user ne $uname) {
   70:             $request->content_type('text/plain');
   71:             $request->print("Forbidden URI: $uri");
   72:             $request->status(403);
   73:             return OK;
   74:         }
   75:     }
   76:     my $file = &Apache::lonnet::filelocation('', $uri);
   77:     &Apache::lonnet::repcopy($file);
   78:     if (! -e $file) {
   79:         $request->status(404);
   80:         return OK;
   81:     }
   82:     try {
   83:         my $warnings = 0; # no warning printed
   84:         my $textref = &Apache::pre_xml::pre_xml($file, $warnings);
   85:         my $case_sensitive;
   86:         if ($uri =~ /\.(task)$/) {
   87:           $case_sensitive = 1;
   88:         } else {
   89:           $case_sensitive = 0;
   90:         }
   91:         $textref = &Apache::html_to_xml::html_to_xml($textref, $warnings, $case_sensitive);
   92:         my $text = &Apache::post_xml::post_xml($textref, $file, $Apache::lonnet::perlvar{'lonDocRoot'}, $warnings);
   93:         &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
   94:         $request->print($text);
   95:         return OK;
   96:     } catch {
   97:         $request->content_type('text/plain');
   98:         $request->print("convert failed for $file: $_");
   99:         $request->status(406);
  100:         return OK;
  101:     };
  102: }
  103: 
  104: sub directory_listing {
  105:     my ($uri, $request) = @_;
  106:     my $res = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
  107:     if ($uri eq '/') {
  108:         # root: let users browse /res
  109:         $res .= "<directory name=\"/\">\n";
  110:         $res .= "<directory name=\"priv\"/>\n";
  111:         $res .= "<directory name=\"res\"/>\n";
  112:     } elsif ($uri !~ /^\/(priv|res)\//) {
  113:         $request->content_type('text/plain');
  114:         $request->print("Not found: $uri");
  115:         $request->status(404);
  116:         return OK;
  117:     } elsif ($uri =~ m{^/res/}) {
  118:         # NOTE: dirlist does not return an error for /res/idontexist/
  119: 	my ($listref, $listerror) = &Apache::lonnet::dirlist($uri);
  120: 	if ($listerror) {
  121:             $request->content_type('text/plain');
  122:             $request->print("listing error: $listerror");
  123:             $request->status(406);
  124:             return OK;
  125: 	} elsif ($uri =~ m{^/res/[^/]+/$} && scalar(@{$listref}) == 0) {
  126:             $request->content_type('text/plain');
  127:             $request->print("Not found: $uri");
  128:             $request->status(404);
  129:             return OK;
  130: 	}
  131:         my $dirname = $uri;
  132:         $dirname =~ s{^.*/([^/]*)$}{$1};
  133:         $res .= "<directory name=\"$dirname/\">\n";
  134:         if (ref($listref) eq 'ARRAY') {
  135:             my @lines = @{$listref};
  136:             foreach my $line (@lines) {
  137:                 my ($path, $dom, undef, $testdir, undef, undef, undef, undef, $size, undef, $mtime, undef, undef, undef, $obs, undef) = split(/\&/, $line, 16);
  138:                 my $isdir = ($testdir & 16384) || $dom =~ /^(user|domain)$/;
  139:                 $path =~ s{^/home/httpd/html/res/}{};
  140:                 next if $path eq '.' || $path eq '..';
  141:                 next if $path =~ /\.meta$/ || $obs || $path =~ /\.\d+\.[^.]+$/;
  142:                 if ($dom ne 'domain') {
  143:                     my ($udom,$uname);
  144:                     if ($dom eq 'user') {
  145:                         ($udom) = ($uri =~ m{^/res/($match_domain)});
  146:                         $uname = $path;
  147:                     } else {
  148:                         ($udom, $uname) = ($uri =~ m{^/res/($match_domain)/($match_courseid)});
  149:                     }
  150:                     if ($udom ne '' && $uname ne '') {
  151:                         # remove courses from the list
  152:                         next if (&Apache::lonnet::is_course($udom, $uname));
  153:                     }
  154:                 }
  155:                 $path =~ s{/$}{};
  156:                 my $name = $path;
  157:                 if ($isdir) {
  158:                     $res .= "<directory name=\"$name\"/>\n";
  159:                 } else {
  160:                     my $dt = DateTime->from_epoch(epoch => $mtime);
  161:                     my $modified = $dt->iso8601().'Z';
  162:                     $res .= "<file name=\"$name\" size=\"$size\" modified=\"$modified\"/>\n";
  163:                 }
  164:             }
  165:         }
  166:     } elsif ($uri eq '/priv/') {
  167:         my $udom = $env{'user.domain'};
  168:         if (!defined $udom) {
  169:             $request->content_type('text/plain');
  170:             $request->print("Forbidden URI: $uri");
  171:             $request->status(403);
  172:             return OK;
  173:         }
  174:         $res .= "<directory name=\"priv\">\n";
  175:         $res .= "<directory name=\"$udom\"/>\n";
  176:     } elsif ($uri =~ m{^/priv/([^/]+)/$}) {
  177:         my $domain = $1;
  178:         my ($uname, $udom) = ($env{'user.name'}, $env{'user.domain'});
  179:         if (!defined $uname || !defined $udom || $domain ne $udom) {
  180:             $request->content_type('text/plain');
  181:             $request->print("Forbidden URI: $uri");
  182:             $request->status(403);
  183:             return OK;
  184:         }
  185:         $res .= "<directory name=\"$domain\">\n";
  186:         $res .= "<directory name=\"$uname\"/>\n";
  187:     } elsif ($uri =~ m{^/priv/([^/]+)/([^/]+)/}) {
  188:         my ($domain, $user) = ($1, $2);
  189:         my ($uname, $udom) = ($env{'user.name'}, $env{'user.domain'});
  190:         if (!defined $uname || !defined $udom || $domain ne $udom || $user ne $uname) {
  191:             $request->content_type('text/plain');
  192:             $request->print("Forbidden URI: $uri");
  193:             $request->status(403);
  194:             return OK;
  195:         }
  196:         my $dirpath = &Apache::lonnet::filelocation('', $uri);
  197:         if (! -e $dirpath) {
  198:             $request->content_type('text/plain');
  199:             $request->print("Not found: $uri");
  200:             $request->status(404);
  201:             return OK;
  202:         }
  203:         $dirpath =~ s{/$}{};
  204:         opendir my $dir, $dirpath or die "Cannot open directory: $dirpath";
  205:         my @files = readdir $dir;
  206:         closedir $dir;
  207:         my $dirname = $dirpath;
  208:         $dirname =~ s{^.*/([^/]*)$}{$1};
  209:         $res .= "<directory name=\"$dirname\">\n";
  210:         foreach my $name (@files) {
  211:             if ($name eq '.' || $name eq '..') {
  212:                 next;
  213:             }
  214:             if ($name =~ /\.(bak|log|meta|save)$/) {
  215:                 next;
  216:             }
  217:             my $sb = stat($dirpath.'/'.$name);
  218:             my $mode = $sb->mode;
  219:             if (S_ISDIR($mode)) {
  220:                 $res .= "<directory name=\"$name\"/>\n";
  221:             } else {
  222:                 $res .= "<file name=\"$name\"";
  223:                 my $size = $sb->size; # total size of file, in bytes
  224:                 $res .= " size=\"$size\"";
  225:                 my $mtime = $sb->mtime; # last modify time in seconds since the epoch
  226:                 my $dt = DateTime->from_epoch(epoch => $mtime);
  227:                 my $modified = $dt->iso8601().'Z';
  228:                 $res .= " modified=\"$modified\"";
  229:                 $res .= "/>\n";
  230:             }
  231:         }
  232:     } else {
  233:         $request->content_type('text/plain');
  234:         $request->print("Not found: $uri");
  235:         $request->status(404);
  236:         return OK;
  237:     }
  238:     $res .= "</directory>\n";
  239:     &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
  240:     $request->print($res);
  241:     return OK;
  242: }
  243: 
  244: 1;
  245: __END__

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