# The LearningOnline Network # Opening converted problems and directory listings for Daxe # # $Id: daxeopen.pm,v 1.3 2015/12/15 15:00:58 damieng Exp $ # # Copyright Michigan State University Board of Trustees # # This file is part of the LearningOnline Network with CAPA (LON-CAPA). # # LON-CAPA is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # LON-CAPA is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with LON-CAPA; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # /home/httpd/html/adm/gpl.txt # # http://www.lon-capa.org/ # ### package Apache::daxeopen; use Apache::Constants; use DateTime; use Try::Tiny; use File::stat; use Fcntl ':mode'; use Apache::loncommon; use Apache::lonnet; use Apache::pre_xml; use Apache::html_to_xml; use Apache::post_xml; sub handler { my $request = shift; my $uri = $request->uri; $uri =~ s/^\/daxeopen//; &Apache::loncommon::no_cache($request); if ($uri =~ /\/$/) { return directory_listing($uri, $request); } elsif ($uri =~ /\.(task|problem|exam|quiz|assess|survey|library)$/) { return convert_problem($uri, $request); } else { # Apache should send other files directly $request->status(406); return OK; } } sub convert_problem { my ($uri, $request) = @_; my $file = &Apache::lonnet::filelocation('', $uri); &Apache::lonnet::repcopy($file); if (! -e $file) { $request->status(404); return OK; } try { my $warnings = 0; # no warning printed my $textref = &Apache::pre_xml::pre_xml($file, $warnings); $textref = &Apache::html_to_xml::html_to_xml($textref, $warnings); my $text = &Apache::post_xml::post_xml($textref, $file, $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->status(406); return OK; }; } sub directory_listing { my ($uri, $request) = @_; my $dirpath = &Apache::lonnet::filelocation('', $uri); if (! -e $dirpath) { $request->status(404); return OK; } $dirpath =~ s/\/$//; opendir my $dir, $dirpath or die "Cannot open directory: $dirpath"; my @files = readdir $dir; closedir $dir; my $res = ''."\n"; my $dirname = $dirpath; $dirname =~ s/^.*\/([^\/]*)$/$1/; $res .= "\n"; foreach my $name (@files) { if ($name eq '.' || $name eq '..') { next; } if ($name =~ /\.(bak|log|meta|save)$/) { next; } $sb = stat($dirpath.'/'.$name); my $mode = $sb->mode; if (S_ISDIR($mode)) { $res .= "\n"; } else { $res .= "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 .= "\n"; &Apache::loncommon::content_type($request, 'text/xml', 'utf-8'); $request->print($res); return OK; } 1; __END__