Annotation of loncom/homework/daxeopen.pm, revision 1.5
1.1 damieng 1: # The LearningOnline Network
2: # Opening converted problems and directory listings for Daxe
3: #
1.5 ! damieng 4: # $Id: daxeopen.pm,v 1.4 2016/01/06 16:44:30 damieng Exp $
1.1 damieng 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:
32: use Apache::Constants;
33: use DateTime;
34: use Try::Tiny;
35: use File::stat;
36: use Fcntl ':mode';
37:
1.5 ! damieng 38: use LONCAPA qw(:match);
1.1 damieng 39: use Apache::loncommon;
40: use Apache::lonnet;
41: use Apache::pre_xml;
42: use Apache::html_to_xml;
43: use Apache::post_xml;
44:
45:
46: sub handler {
47: my $request = shift;
48: my $uri = $request->uri;
49: $uri =~ s/^\/daxeopen//;
50: &Apache::loncommon::no_cache($request);
51: if ($uri =~ /\/$/) {
52: return directory_listing($uri, $request);
1.4 damieng 53: } elsif ($uri =~ /\.(task|problem|exam|quiz|assess|survey|library|xml|html|htm|xhtml|xhtm)$/) {
1.1 damieng 54: return convert_problem($uri, $request);
55: } else {
56: # Apache should send other files directly
1.2 damieng 57: $request->status(406);
58: return OK;
1.1 damieng 59: }
60: }
61:
62: sub convert_problem {
63: my ($uri, $request) = @_;
64:
65: my $file = &Apache::lonnet::filelocation('', $uri);
66: &Apache::lonnet::repcopy($file);
67: if (! -e $file) {
1.2 damieng 68: $request->status(404);
69: return OK;
1.1 damieng 70: }
71: try {
72: my $warnings = 0; # no warning printed
73: my $textref = &Apache::pre_xml::pre_xml($file, $warnings);
1.4 damieng 74: my $case_sensitive;
75: if ($uri =~ /\.(task)$/) {
76: $case_sensitive = 1;
77: } else {
78: $case_sensitive = 0;
79: }
80: $textref = &Apache::html_to_xml::html_to_xml($textref, $warnings, $case_sensitive);
1.3 damieng 81: my $text = &Apache::post_xml::post_xml($textref, $file, $perlvar{'lonDocRoot'}, $warnings);
1.1 damieng 82: &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
83: $request->print($text);
84: return OK;
85: } catch {
1.2 damieng 86: $request->content_type('text/plain');
87: $request->print("convert failed for $file: $_");
88: $request->status(406);
89: return OK;
1.1 damieng 90: };
91: }
92:
93: sub directory_listing {
94: my ($uri, $request) = @_;
1.5 ! damieng 95: my $res = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
! 96: if ($uri eq '/') {
! 97: # root: let users browse /res
! 98: $res .= "<directory name=\"/\">\n";
! 99: $res .= "<directory name=\"res\"/>\n";
! 100: } elsif ($uri !~ /^\/(priv|res)\//) {
1.2 damieng 101: $request->status(404);
102: return OK;
1.5 ! damieng 103: } elsif ($uri =~ /^\/res\//) {
! 104: (my $listref, $listerror) = &Apache::lonnet::dirlist($uri);
! 105: if ($listerror) {
! 106: $request->content_type('text/plain');
! 107: $request->print("listing error: $listerror");
! 108: $request->status(406);
! 109: return OK;
! 110: }
! 111: my $dirname = $uri;
! 112: $dirname =~ s/^.*\/([^\/]*)$/$1/;
! 113: $res .= "<directory name=\"$dirname/\">\n";
! 114: if (ref($listref) eq 'ARRAY') {
! 115: my @lines = @{$listref};
! 116: foreach my $line (@lines) {
! 117: my ($path, $dom, undef, $testdir, undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, $obs, undef) = split(/\&/, $line, 16);
! 118: my $isdir = ($testdir & 16384) || $dom =~ /^(user|domain)$/;
! 119: $path =~ s/^\/home\/httpd\/html\/res\///;
! 120: next if $path eq '.' || $path eq '..';
! 121: next if $path =~ /\.meta$/ || $obs || $path =~ /\.\d+\.[^.]+$/;
! 122: if ($dom ne 'domain') {
! 123: my ($udom,$uname);
! 124: if ($dom eq 'user') {
! 125: ($udom) = ($uri =~ m{^/res/($match_domain)});
! 126: $uname = $path;
! 127: } else {
! 128: ($udom, $uname) = ($uri =~ m{^/res/($match_domain)/($match_courseid)});
! 129: }
! 130: if ($udom ne '' && $uname ne '') {
! 131: # remove courses from the list
! 132: next if (&Apache::lonnet::is_course($udom, $uname));
! 133: }
! 134: }
! 135: $path =~ s/\/$//;
! 136: my $name = $path;
! 137: if ($isdir) {
! 138: $res .= "<directory name=\"$name\"/>\n";
! 139: } else {
! 140: $res .= "<file name=\"$name\"/>\n";
! 141: }
! 142: }
1.1 damieng 143: }
1.5 ! damieng 144: } else {
! 145: my $dirpath = &Apache::lonnet::filelocation('', $uri);
! 146: if (! -e $dirpath) {
! 147: $request->status(404);
! 148: return OK;
1.1 damieng 149: }
1.5 ! damieng 150: $dirpath =~ s/\/$//;
! 151: opendir my $dir, $dirpath or die "Cannot open directory: $dirpath";
! 152: my @files = readdir $dir;
! 153: closedir $dir;
! 154: my $dirname = $dirpath;
! 155: $dirname =~ s/^.*\/([^\/]*)$/$1/;
! 156: $res .= "<directory name=\"$dirname\">\n";
! 157: foreach my $name (@files) {
! 158: if ($name eq '.' || $name eq '..') {
! 159: next;
! 160: }
! 161: if ($name =~ /\.(bak|log|meta|save)$/) {
! 162: next;
! 163: }
! 164: $sb = stat($dirpath.'/'.$name);
! 165: my $mode = $sb->mode;
! 166: if (S_ISDIR($mode)) {
! 167: $res .= "<directory name=\"$name\"/>\n";
! 168: } else {
! 169: $res .= "<file name=\"$name\"";
! 170: my $size = $sb->size; # total size of file, in bytes
! 171: $res .= " size=\"$size\"";
! 172: my $mtime = $sb->mtime; # last modify time in seconds since the epoch
! 173: my $dt = DateTime->from_epoch(epoch => $mtime);
! 174: my $modified = $dt->iso8601().'Z';
! 175: $res .= " modified=\"$modified\"";
! 176: $res .= "/>\n";
! 177: }
1.1 damieng 178: }
179: }
180: $res .= "</directory>\n";
181: &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
182: $request->print($res);
183: return OK;
184: }
185:
186: 1;
187: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>