File:  [LON-CAPA] / loncom / homework / daxeopen.pm
Revision 1.5: download - view: text, annotated - select for diffs
Thu Feb 23 21:32:08 2017 UTC (7 years, 2 months ago) by damieng
Branches: MAIN
CVS tags: HEAD
daxeopen: added support for / and /res directory listing

    1: # The LearningOnline Network
    2: # Opening converted problems and directory listings for Daxe
    3: #
    4: # $Id: daxeopen.pm,v 1.5 2017/02/23 21:32:08 damieng 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: 
   32: use Apache::Constants;
   33: use DateTime;
   34: use Try::Tiny;
   35: use File::stat;
   36: use Fcntl ':mode';
   37: 
   38: use LONCAPA qw(:match);
   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);
   53:     } elsif ($uri =~ /\.(task|problem|exam|quiz|assess|survey|library|xml|html|htm|xhtml|xhtm)$/) {
   54:         return convert_problem($uri, $request);
   55:     } else {
   56:         # Apache should send other files directly
   57:         $request->status(406);
   58:         return OK;
   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) {
   68:         $request->status(404);
   69:         return OK;
   70:     }
   71:     try {
   72:         my $warnings = 0; # no warning printed
   73:         my $textref = &Apache::pre_xml::pre_xml($file, $warnings);
   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);
   81:         my $text = &Apache::post_xml::post_xml($textref, $file, $perlvar{'lonDocRoot'}, $warnings);
   82:         &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
   83:         $request->print($text);
   84:         return OK;
   85:     } catch {
   86:         $request->content_type('text/plain');
   87:         $request->print("convert failed for $file: $_");
   88:         $request->status(406);
   89:         return OK;
   90:     };
   91: }
   92: 
   93: sub directory_listing {
   94:     my ($uri, $request) = @_;
   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)\//) {
  101:         $request->status(404);
  102:         return OK;
  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:             }
  143:         }
  144:     } else {
  145:         my $dirpath = &Apache::lonnet::filelocation('', $uri);
  146:         if (! -e $dirpath) {
  147:             $request->status(404);
  148:             return OK;
  149:         }
  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:             }
  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>