File:  [LON-CAPA] / loncom / homework / lonr.pm
Revision 1.2: download - view: text, annotated - select for diffs
Sat Apr 18 13:18:58 2009 UTC (15 years ago) by www
Branches: MAIN
CVS tags: HEAD
Command blacklist and library whitelist

    1: # The LearningOnline Network with CAPA
    2: # Interface routines to R CAS
    3: #
    4: # $Id: lonr.pm,v 1.2 2009/04/18 13:18:58 www 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: package Apache::lonr;
   30:  
   31: use strict;
   32: use IO::Socket;
   33: use Apache::lonnet;
   34: use Apache::response();
   35: use LONCAPA;
   36: 
   37: sub connect {
   38:    return IO::Socket::UNIX->new(Peer    => $Apache::lonnet::perlvar{'lonSockDir'}.'/rsock',
   39: 				Type    => SOCK_STREAM,
   40: 				Timeout => 10);
   41: }
   42: 
   43: sub disconnect {
   44:     my ($socket)=@_;
   45:     if ($socket) { close($socket); }
   46: }
   47: 
   48: sub rreply {
   49:     my ($socket,$cmd)=@_;
   50:     if ($socket) {
   51: 	print $socket &escape($cmd)."\n";
   52:         my $reply=<$socket>;
   53:         chomp($reply);
   54:         if ($reply=~/^Incorrect/) { $reply='Error: '.$reply; }
   55:         return &unescape($reply);
   56:     } else {
   57:         return 'Error: no connection.';
   58:     }
   59: }
   60: 
   61: sub blacklisted {
   62:     my ($cmd)=@_;
   63:     foreach my $forbidden (
   64:         'read\.table','scan','plot','X11','windows','quartz',
   65:         'postscript','pdf','png','jpeg',
   66:         'dev\.list','dev\.next','dev\.prev','dev\.set',
   67:         'dev\.off','dev\.copy','dev\.print','graphics\.off',
   68:         'library','package','source','sink','objects'
   69:      ) {
   70: 	if ($cmd=~/$forbidden/s) { return 1; }
   71:     } 
   72:     return 0;
   73: }
   74: 
   75: sub r_allowed_libraries {
   76:    return ('boot','class','cluster','datasets','KernSmooth','MASS',
   77:            'methods','mgcv','nlme','nnet','rpart','spatial',
   78:            'splines','stats','stats4','survival');
   79: }
   80: 
   81: sub r_is_allowed_library {
   82:     my ($library)=@_;
   83:     foreach my $allowed_library (&r_allowed_libraries()) {
   84:        if ($library eq $allowed_library) { return 1; }
   85:     }
   86:     return 0;
   87: }
   88: 
   89: sub runscript {
   90:     my ($socket,$fullscript,$libraries)=@_;
   91:     if (&blacklisted($fullscript)) { return 'Error: blacklisted'; }
   92:     my $reply;
   93:     $fullscript=~s/[\n\r\l]//gs;
   94:     if ($libraries) {
   95:        foreach my $library (split(/\s*\,\s*/,$libraries)) {
   96:           unless ($library=~/\w/) { next; }
   97:           if (&r_is_allowed_library($library)) {
   98:               $reply=&rreply($socket,'library('.$library.');'."\n");
   99:               if ($reply=~/^Error\:/) { return $reply; }
  100:           } else { 
  101:              return 'Error: blacklisted'; 
  102:           }
  103:        }
  104:     }
  105:     foreach my $line (split(/\;/s,$fullscript)) {
  106: 	if ($line=~/\w/) { $reply=&rreply($socket,$line.";\n"); }
  107: 	if ($reply=~/^Error\:/) { return $reply; }
  108:     }
  109:     $reply=~s/^\s*//gs;
  110:     $reply=~s/\s*$//gs;
  111:     &Apache::lonxml::debug("r $fullscript \n reply $reply");
  112:     return $reply;
  113: }
  114: 
  115: sub r_cas_formula_fix {
  116:    my ($expression)=@_;
  117:    return &Apache::response::implicit_multiplication($expression);
  118: }
  119: 
  120: sub r_run {
  121:     my ($script,$submission,$argument,$libraries) = @_;
  122:     my $socket=&connect();
  123:     my @submissionarray=split(/\s*\,\s*/,$submission);
  124:     for (my $i=0;$i<=$#submissionarray;$i++) {
  125:         my $n=$i+1;
  126:         my $fixedsubmission=&r_cas_formula_fix($submissionarray[$i]);
  127:         $script=~s/RESPONSE\[$n\]/$fixedsubmission/gs;
  128:     }
  129:     my @argumentarray=@{$argument};
  130:     for (my $i=0;$i<=$#argumentarray;$i++) {
  131:         my $n=$i+1;
  132:         my $fixedargument=&r_cas_formula_fix($argumentarray[$i]);
  133:         $script=~s/LONCAPALIST\[$n\]/$fixedargument/gs;
  134:     }
  135:     my $reply=&runscript($socket,$script,$libraries);
  136:     &disconnect($socket);
  137:     if ($reply=~/^\s*true\s*$/i) { return 'EXACT_ANS'; }
  138:     if ($reply=~/^\s*false\s*$/i) { return 'INCORRECT'; } 
  139:     return 'BAD_FORMULA';
  140: }
  141: 
  142: sub r_eval {
  143:     my ($script,$libraries) = @_;
  144:     my $socket=&connect();
  145:     my $reply=&runscript($socket,$script,$libraries);
  146:     &disconnect($socket);
  147:     return $reply;
  148: }
  149: 
  150: 
  151: sub compareterms {
  152:     my ($socket,$terma,$termb)=@_;
  153:     my $difference=$terma.'-('.$termb.')';
  154:     if (&blacklisted($difference)) { return 'Error: blacklisted'; }
  155:     my $reply=&rreply($socket,$difference.';');
  156:     if ($reply=~/^\s*0\s*$/) { return 'true'; }
  157:     if ($reply=~/^Error\:/) { return $reply; }
  158:     return 'false';
  159: }
  160: 
  161: sub r_check {
  162:     my ($response,$answer,$reterror) = @_;
  163:     my $socket=&connect();
  164:     my $reply=&compareterms($socket,$response,$answer);
  165:     &disconnect($socket);
  166:     # integer to string mappings come from capaParser.h
  167:     # 1 maps to 'EXACT_ANS'
  168:     if ($reply eq 'true') { return 1; }
  169:     # 11 maps to 'BAD_FORMULA'
  170:     if ($reply=~/^Error\:/) { return 11; }
  171:     # 7 maps to 'INCORRECT'
  172:     return 7;
  173: }
  174:  
  175: 1;
  176: __END__;

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