Annotation of loncom/homework/lonr.pm, revision 1.3

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

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