[R-sig-mediawiki] Revised Rext 0.03 with better error reporting

Alex Brown alex at transitive.com
Tue Sep 26 15:03:44 CEST 2006


Attached is a version of Rext 0.03 with my previous changes plus a  
set of changes to improve error reporting, including:

if iframe images cannot render, you get the program text + the error

if inline R (default output) contains an error, you get the error  
message in red inlined with the wiki page

if inline R (output=display) fails to render, you get the program  
text + the error inlined with the wiki page

In the long run, I plan to unify the 3 different output modes,  
starting with default and display - I have a prototype (not included)  
that does this.

I have attached a patch as well as the complete file.

-Alex

====

patch from 0.03

--- Rext_orig.php       2006-09-26 12:56:55.345076291 +0100
+++ Rext.php_3_with_debug       2006-09-26 13:57:03.412252560 +0100
@@ -36,7 +36,7 @@
defined('convert')   || define('convert',   getCmd('', 'convert', ''));
defined('sudo')      || define('sudo',      getCmd('', 'sudo', ' -u  
' . sudouser . ' '));
defined('chmod')     || define('chmod',     getCmd('', 'chmod', ' 664  
'));
-defined('r_cmd')     || define('r_cmd',     getCmd('', 'R', ' -- 
vanilla'));
+defined('r_cmd')     || define('r_cmd',     getCmd('', 'R', ' -- 
vanilla --quiet'));
defined('r_dir')     || define('r_dir',     getcwd() .  
DIRECTORY_SEPARATOR . 'Rfiles' );
defined('r_url')     || define('r_url',     extractUrl(r_dir));
@@ -103,17 +103,21 @@
    return $ret;
}
-function runRinShell ($cmd, $chmf) {
+function runRinShell ($cmd, $chmf, &$r_exitcode) {
+  $stdout = "";
    if (security>1) {
      $msg = shell_exec (sudo . $cmd);
      if ($chmf!='') {
        $cmd = sudo . chmod . $chmf;
-      shell_exec ($cmd);
+      exec ($cmd, $stdout, $r_exitcode);
#      error ('R', $cmd, $msg);
      }
    } else {
-    $msg = shell_exec ($cmd);
+    exec ($cmd, $stdout, $r_exitcode);
    }
+
+  foreach($stdout as $row) $msg .= $row . "\n";
+
#  error ('R', $cmd, $msg);
    return ($msg);
}
@@ -138,6 +142,8 @@
        $url = r_url . DIRECTORY_SEPARATOR . $sha1 . '.jpg';
        $urlpdf = r_url . DIRECTORY_SEPARATOR . $sha1 . '.pdf';
        $htm = r_dir . DIRECTORY_SEPARATOR . $sha1 . '.html';
+
+      $r_exitcode = 0;
        if (!file_exists($jpg) or onsave) {
          $content = '';
          if ($ws!='') {
@@ -153,38 +159,48 @@
          $fn = r_dir . DIRECTORY_SEPARATOR . $sha1 . '.R';
          $fd = fopen ($fn, 'w') or error ('R', 'can not open file:  
' . $fn, $input);
          fwrite ($fd, $content);
-        fclose ($fd);
-        $err = $err . runRinShell (r_cmd . ' < ' . $fn, '');
-        $err = $err . runShell (convert . ' ' . $pdf . ' ' .  
$convert . ' ' . $jpg);
+        fclose ($fd);
+        $err = $err . runRinShell (r_cmd . ' 2>&1 < ' . $fn, '',  
$r_exitcode);
+        if ($r_exitcode == 0)
+        {
+          $err = $err . runShell (convert . ' ' . $pdf . ' ' .  
$convert . ' ' . $jpg);
+        }
        }
-      if (!file_exists($jpg)) {
-        $jpg = r_dir . DIRECTORY_SEPARATOR . $sha1 . '.jpg.0';
-        if (file_exists($jpg)) {
-          for ($i=0; ; $i++) {
-            $old = r_dir . DIRECTORY_SEPARATOR . $sha1 . '.jpg.' . $i;
-            $new = r_dir . DIRECTORY_SEPARATOR . $sha1 . '-' . $i .  
'.jpg';
-            if (file_exists($old)) {
-              rename ($old, $new);
+      if ($r_exitcode == 0)
+      {
+        if (!file_exists($jpg)) {
+          $jpg = r_dir . DIRECTORY_SEPARATOR . $sha1 . '.jpg.0';
+          if (file_exists($jpg)) {
+            for ($i=0; ; $i++) {
+              $old = r_dir . DIRECTORY_SEPARATOR . $sha1 . '.jpg.' .  
$i;
+              $new = r_dir . DIRECTORY_SEPARATOR . $sha1 . '-' .  
$i . '.jpg';
+              if (file_exists($old)) {
+                rename ($old, $new);
+              } else {
+                break;
+              }
+            }
+          }
+          $jpg = r_dir . DIRECTORY_SEPARATOR . $sha1 . '-0.jpg';
+          file_exists($jpg) or error ('R', 'JPEG file does not  
exist: ' . $jpg, $input . $err);
+          $url = r_url . DIRECTORY_SEPARATOR . $sha1 . '-0.jpg';
+          $ret = '<a href="' . $urlpdf . '"><img src="' . $url . '"  
border="0" style="' . style . '"></a>';
+          for ($i=1; ; $i++) {
+            $jpg = r_dir . DIRECTORY_SEPARATOR . $sha1 . '-'. $i .  
'.jpg';
+            $url = r_url . DIRECTORY_SEPARATOR . $sha1 . '-' . $i .  
'.jpg';
+            if (file_exists($jpg)) {
+              $ret = $ret . '<a href="' . $urlpdf . '"><img src="' .  
$url . '" border="0" style="' . $style . '"></a>';
              } else {
                break;
              }
-          }
+          }
+        } else {
+          $ret = '<a href="' . $urlpdf . '"><img src="' . $url . '"  
border="0" style="' . $style . '"></a>';
          }
-        $jpg = r_dir . DIRECTORY_SEPARATOR . $sha1 . '-0.jpg';
-        file_exists($jpg) or error ('R', 'JPEG file does not exist:  
' . $jpg, $input . $err);
-        $url = r_url . DIRECTORY_SEPARATOR . $sha1 . '-0.jpg';
-        $ret = '<a href="' . $urlpdf . '"><img src="' . $url . '"  
border="0" style="' . style . '"></a>';
-        for ($i=1; ; $i++) {
-          $jpg = r_dir . DIRECTORY_SEPARATOR . $sha1 . '-'. $i .  
'.jpg';
-          $url = r_url . DIRECTORY_SEPARATOR . $sha1 . '-' . $i .  
'.jpg';
-          if (file_exists($jpg)) {
-            $ret = $ret . '<a href="' . $urlpdf . '"><img src="' .  
$url . '" border="0" style="' . $style . '"></a>';
-          } else {
-            break;
-          }
-        }
-      } else {
-        $ret = '<a href="' . $urlpdf . '"><img src="' . $url . '"  
border="0" style="' . $style . '"></a>';
+      }
+      else
+      {
+        $ret = '<pre>' . $err . '</pre>';
        }
        $fd = fopen ($htm, 'w') or error ('R', 'can not open HTML  
file: ' . $htm, $input . $err);
        fwrite ($fd, makeHTML($prg . $ret, $direct));
@@ -211,7 +227,8 @@
          $fd = fopen ($fn, 'w') or error ('R', 'Can not open file:  
' . $fn, $input . $err);
          fwrite ($fd, $content);
          fclose ($fd);
-        $err = $err . runRinShell (r_cmd . ' < ' . $fn, $htm);
+        $r_exitcode = 0;
+        $err = $err . runRinShell (r_cmd . ' < ' . $fn, $htm,  
$r_exitcode);
        }
        file_exists($htm) or error ('R', 'HTML file does not exist:  
' . $htm, $input . $err);
        $cont = file_get_contents ($htm);
@@ -238,14 +255,22 @@
          $fd = fopen ($fn, 'w') or error ('R', 'Can not open file:  
' . $fn, $input . $err);
          fwrite ($fd, $content);
          fclose ($fd);
-        $cmd = renderFilename(r_cmd . ' --slave < ' . $fn . ' > ' .  
$rst);
-        $err = $err . runRinShell ($cmd, $rst);
+        $cmd = renderFilename(r_cmd . ' --slave 2>&1 < ' . $fn . ' >  
' . $rst);
+        $r_exitcode = 0;
+        $err = $err . runRinShell ($cmd, $rst, $r_exitcode);
        }
        file_exists($rst) or error ('R', 'Text file does not exist:  
' . $rst, $input . $err);
        $cont = file_get_contents ($rst);
        if (strpos ($cont, '<html>') === false) {
          $fd = fopen ($rst, 'w') or error ('R', 'can not open file:  
' . $rst, $input . $err);
-        fwrite ($fd, makeHTML($prg . '<pre>' . $cont . '</pre>',  
$direct));
+        if ($r_exitcode == 0)
+        {
+          fwrite ($fd, makeHTML($prg . '<pre>' . $cont . '</pre>',  
$direct));
+        }
+        else
+        {
+          fwrite ($fd, makeHTML($prg . '<pre>' . $cont . '<span  
style="color:red">' . $err . '</span>' . '</pre>', $direct));
+        }
          fclose ($fd);
        }
        $ret = $rst;


====

actual file:

<?php

/*

Plugins for Mediawiki

(C) 2006- Sigbert Klinke (sigbert at wiwi.hu-berlin.de), Markus Cozowicz

This program 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.

This program 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 this program; if not, write to the Free Software  
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA

*/

# for Special::Version:
$wgExtensionCredits['parserhook'][] = array(
         'name' => 'R extension',
         'author' => 'Sigbert Klinke',
         'url' => 'http://mars.wiwi.hu-berlin.de/mediawiki/sk/ 
index.php/R_Plugin_for_MediaWiki',
         'version' => 'v0.03',
);

// global params
if (!defined('extbase')) {
   include ('extbase.php');
}

// security = 0: no checks on code
// security = 1: some R commands are forbidden
// security = 2: sudo
defined('security')  || define('security',  2);
defined('sudouser')  || define('sudouser', 'rd');

defined('convert')   || define('convert',   getCmd('', 'convert', ''));
defined('sudo')      || define('sudo',      getCmd('', 'sudo', ' -u  
' . sudouser . ' '));
defined('chmod')     || define('chmod',     getCmd('', 'chmod', ' 664  
'));
defined('r_cmd')     || define('r_cmd',     getCmd('', 'R', ' -- 
vanilla --quiet'));

defined('r_dir')     || define('r_dir',     getcwd() .  
DIRECTORY_SEPARATOR . 'Rfiles' );
defined('r_url')     || define('r_url',     extractUrl(r_dir));
defined('r_cgi')     || define('r_cgi',     getcwd() .  
DIRECTORY_SEPARATOR . 'R.php');
defined('r_ext')     || define('r_ext',     getcwd() .  
DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR . 'R');


//Add the hook function call to an array defined earlier in the wiki  
code execution.
$wgExtensionFunctions[] = "wfRParse";

//This is the hook function. It adds the tag to the wiki parser and  
tells it what callback function to use.

function wfRParse() {
   global $wgParser;
   # register the extension with the WikiText parser
   $wgParser->setHook("R", "renderR" );
   $wgParser->setHook("Rform", "renderRform" );
}

function makeStyle ($param, $default) {
   $list = explode (';', $default);
   $n    = count($list);
   $arr  = array();
   for ($i=0; $i<$n; $i++) {
     $pair = explode (':', $list[$i]);
     $arr[$pair[0]] = $pair[1];
   }
   $list = explode (';', $param);
   $n    = count($list);
   for ($i=0; $i<$n; $i++) {
     $pair = explode (':', $list[$i]);
     $arr[$pair[0]] = $pair[1];
   }
   $list = array_keys($arr);
   $ret  = '';
   for ($i=0; $i<$n; $i++) {
     if (strlen($list[$i])>0) {
       $ret = $ret . $list[$i] . ':' . $arr[$list[$i]] . ';';
     }
   }
   return $ret;
}

function makeHTML ($input, $direct) {
   if (!direct) {
     $ret = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01  
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
     $ret = $ret . '<html><head><meta http-equiv="expires"  
content="0"></head><body>';
     $ret = $ret . $input;
//  $ret = $ret . '<hr>' . date("d.m.Y") . ' ' . date("H:i:s");
     $ret = $ret . '</body></html>';
   } else {
     $ret = $input;
   }
   return $ret;
}

function renderRform( $input , $params ) {
   array_key_exists('name', $params) or error ('Rform', 'attribute  
"name" required', $input);
   $name = $params['name'];
   $ret  = '<form action="' . extractUrl(r_cgi) . '" method="post"  
target="' . $name . '">';
   $ret  = $ret . '<input type="hidden" name="R" value="' . $name .  
'">';
   $ret  = $ret . $input;
   $ret  = $ret . '</form>';
   return $ret;
}

function runRinShell ($cmd, $chmf, &$r_exitcode) {
   $stdout = "";
   if (security>1) {
     $msg = shell_exec (sudo . $cmd);
     if ($chmf!='') {
       $cmd = sudo . chmod . $chmf;
       exec ($cmd, $stdout, $r_exitcode);
#      error ('R', $cmd, $msg);
     }
   } else {
     exec ($cmd, $stdout, $r_exitcode);
   }

   foreach($stdout as $row) $msg .= $row . "\n";

#  error ('R', $cmd, $msg);
   return ($msg);
}

function runShell ($cmd) {
   $cmd = str_replace ("\n", ' ', $cmd);
   return (shell_exec ($cmd));
}

function runR ($output, $convert, $sha1, $input, $direct, $echo, $ws) {
// Generate a graphics
   $prg = '';
   if ($echo) {
     $prg = '<pre>' . $input . '</pre>';
   }
   $err = "\n";
   $rws = r_dir. DIRECTORY_SEPARATOR . $ws;
   switch ($output) {
     case 'display':
       $jpg = r_dir . DIRECTORY_SEPARATOR . $sha1 . '.jpg';
       $pdf  = r_dir . DIRECTORY_SEPARATOR . $sha1 . '.pdf';
       $url = r_url . DIRECTORY_SEPARATOR . $sha1 . '.jpg';
       $urlpdf = r_url . DIRECTORY_SEPARATOR . $sha1 . '.pdf';
       $htm = r_dir . DIRECTORY_SEPARATOR . $sha1 . '.html';

       $r_exitcode = 0;
       if (!file_exists($jpg) or onsave) {
         $content = '';
         if ($ws!='') {
            $content = $content . 'sys.load.image("' . $rws . '",  
TRUE)' ."\n";
         }
         $content = $content . 'rfiles<-"' . r_dir . '"' . "\n" .  
'rpdf <- "' . $pdf . '"' . "\n";
         $content = $content . 'source("' . r_ext .  
DIRECTORY_SEPARATOR . 'StatWiki.r")' . "\n";
         $content = $content . $input . "\n";
         if ($ws!='') {
            $content = $content . 'sys.save.image("' . $rws .  
'")' ."\n";
         }
         $content = $content . 'q()';
         $fn = r_dir . DIRECTORY_SEPARATOR . $sha1 . '.R';
         $fd = fopen ($fn, 'w') or error ('R', 'can not open file:  
' . $fn, $input);
         fwrite ($fd, $content);
         fclose ($fd);
         $err = $err . runRinShell (r_cmd . ' 2>&1 < ' . $fn, '',  
$r_exitcode);
         if ($r_exitcode == 0)
         {
           $err = $err . runShell (convert . ' ' . $pdf . ' ' .  
$convert . ' ' . $jpg);
         }
       }
       if ($r_exitcode == 0)
       {
         if (!file_exists($jpg)) {
           $jpg = r_dir . DIRECTORY_SEPARATOR . $sha1 . '.jpg.0';
           if (file_exists($jpg)) {
             for ($i=0; ; $i++) {
               $old = r_dir . DIRECTORY_SEPARATOR . $sha1 . '.jpg.' .  
$i;
               $new = r_dir . DIRECTORY_SEPARATOR . $sha1 . '-' .  
$i . '.jpg';
               if (file_exists($old)) {
                 rename ($old, $new);
               } else {
                 break;
               }
             }
           }
           $jpg = r_dir . DIRECTORY_SEPARATOR . $sha1 . '-0.jpg';
           file_exists($jpg) or error ('R', 'JPEG file does not  
exist: ' . $jpg, $input . $err);
           $url = r_url . DIRECTORY_SEPARATOR . $sha1 . '-0.jpg';
           $ret = '<a href="' . $urlpdf . '"><img src="' . $url . '"  
border="0" style="' . style . '"></a>';
           for ($i=1; ; $i++) {
             $jpg = r_dir . DIRECTORY_SEPARATOR . $sha1 . '-'. $i .  
'.jpg';
             $url = r_url . DIRECTORY_SEPARATOR . $sha1 . '-' . $i .  
'.jpg';
             if (file_exists($jpg)) {
               $ret = $ret . '<a href="' . $urlpdf . '"><img src="' .  
$url . '" border="0" style="' . $style . '"></a>';
             } else {
               break;
             }
           }
         } else {
           $ret = '<a href="' . $urlpdf . '"><img src="' . $url . '"  
border="0" style="' . $style . '"></a>';
         }
       }
       else
       {
         $ret = '<pre>' . $err . '</pre>';
       }
       $fd = fopen ($htm, 'w') or error ('R', 'can not open HTML  
file: ' . $htm, $input . $err);
       fwrite ($fd, makeHTML($prg . $ret, $direct));
       fclose ($fd);
       $ret = $htm;
       break;
     case 'html':
       $htm  = r_dir . DIRECTORY_SEPARATOR . $sha1 . '.html';
       if (!file_exists($htm) or onsave) {
         $content = '';
         if ($ws!='') {
            $content = $content . 'sys.load.image("' . $rws . '",  
TRUE)' ."\n";
         }
         $content = $content . 'rfiles<-"' . r_dir . '"' . "\n";
         $content = $content . 'source("' . r_ext .  
DIRECTORY_SEPARATOR . 'StatWiki.r")' . "\n";
         $content = $content . 'rhtml<-"' . $htm . '"' . "\n";
         $content = $content . 'cat("", file=rhtml, append=FALSE)' .  
"\n";
         $content = $content . $input . "\n";
         if ($ws!='') {
            $content = $content . 'sys.save.image("' . $rws .  
'")' ."\n";
         }
         $content = $content . 'q()';
         $fn = r_dir . DIRECTORY_SEPARATOR . $sha1 . '.R';
         $fd = fopen ($fn, 'w') or error ('R', 'Can not open file:  
' . $fn, $input . $err);
         fwrite ($fd, $content);
         fclose ($fd);
         $r_exitcode = 0;
         $err = $err . runRinShell (r_cmd . ' < ' . $fn, $htm,  
$r_exitcode);
       }
       file_exists($htm) or error ('R', 'HTML file does not exist:  
' . $htm, $input . $err);
       $cont = file_get_contents ($htm);
       if (strpos ($cont, '<html>') === false) {
         $fd = fopen ($htm, 'w') or error ('R', 'can not open file:  
' . $htm, $input . $err);
         fwrite ($fd, makeHTML($prg . $cont, $direct));
         fclose ($fd);
       }
       $ret = $htm;
       break;
     default:
       $rst  = r_dir . DIRECTORY_SEPARATOR . $sha1 . '.html';
       if (!file_exists($rst) or onsave) {
         $content = '';
         if ($ws!='') {
            $content = $content . 'sys.load.image("' . $rws . '",  
TRUE)' ."\n";
         }
         $content = $content . 'rfiles<-"' . r_dir . '"' . "\n" .  
$input . "\n";
         if ($ws!='') {
            $content = $content . 'sys.save.image("' . $rws .  
'")' ."\n";
         }
         $content = $content . 'q()';
         $fn = r_dir . '/' . $sha1 . '.R';
         $fd = fopen ($fn, 'w') or error ('R', 'Can not open file:  
' . $fn, $input . $err);
         fwrite ($fd, $content);
         fclose ($fd);
         $cmd = renderFilename(r_cmd . ' --slave 2>&1 < ' . $fn . ' >  
' . $rst);
         $r_exitcode = 0;
         $err = $err . runRinShell ($cmd, $rst, $r_exitcode);
       }
       file_exists($rst) or error ('R', 'Text file does not exist:  
' . $rst, $input . $err);
       $cont = file_get_contents ($rst);
       if (strpos ($cont, '<html>') === false) {
         $fd = fopen ($rst, 'w') or error ('R', 'can not open file:  
' . $rst, $input . $err);
         if ($r_exitcode == 0)
         {
           fwrite ($fd, makeHTML($prg . '<pre>' . $cont . '</pre>',  
$direct));
         }
         else
         {
           fwrite ($fd, makeHTML($prg . '<pre>' . $cont . '<span  
style="color:red">' . $err . '</span>' . '</pre>', $direct));
         }
         fclose ($fd);
       }
       $ret = $rst;
   }
   return $ret;
}

function checkCommands ($input) {
   // Thanks to the R-php people :)
   $banned = array('.C', '.Call', '.Call.graphics', '.External',  
'.External.graphics',
                   '.Fortran', '.readRDS', '.saveRDS', '.Script',  
'.Tcl',
                   '.Tcl.args', '.Tcl.callback', '.Tk.ID',  
'.Tk.newwin', '.Tk.subwin',
                   '.Tkroot', '.Tkwin', 'basename', 'browseURL',  
'bzfile',
                   'capture.output', 'close', 'close.screen',  
'closeAllConnection', 'data.entry',
                   'data.restore', 'dataentry', 'de', 'dev.control',  
'dev.copy2eps',
                   'dev.cur', 'dev.list', 'dev.next', 'dev.prev',  
'dev.print',
                   'dev.set', 'dev2bitmap', 'dget', 'dir', 'dir.create',
                   'dirname', 'do.call', 'download.file', 'dput',  
'dump',
                   'dyn.load', 'edit', 'edit.data.frame', 'emacs',  
'erase.screen',
                   'example', 'fifo', 'file', 'file.access',  
'file.append',
                   'file.choose', 'file.copy', 'file.create',  
'file.exists', 'file.info',
                   'file.path', 'file.remove', 'file.rename',  
'file.show', 'file.symlink',
                   'fix', 'getConnection', 'getwd', 'graphics.off',  
'gzcon',
                   'gzfile', 'INSTALL', 'install.packages', 'jpeg',  
'library.dynam',
                   'list.files', 'loadhistory', 'locator',  
'lookup.xport', 'make.packages.html',
                   'make.socket', 'menu', 'open', 'parent.frame',  
'path.expand',
                   'pico', 'pictex', 'pipe', 'png',
                   'postscript', 'print.socket', 'prompt',  
'promptData', 'quartz',
                   'R.home', 'R.version', 'read.00Index', 'read.dta',  
'read.epiinfo',
                   'read.fwf', 'read.mtp', 'read.socket',  
'read.spss', 'read.ssd',
                   'read.xport', 'readBin', 'readline', 'readLines',  
'remove.packages',
                   'Rprof', 'save', 'savehistory', 'scan', 'screen',
                   'seek', 'setwd', 'showConnection', 'sink',  
'sink.number',
                   'socketConnection', 'source', 'split.screen',  
'stderr', 'stdin',
                   'stdout', 'sys.call', 'sys.calls', 'sys.frame',  
'sys.frames',
                   'sys.function', 'Sys.getenv', 'Sys.getlocale',  
'Sys.info', 'sys.nframe',
                   'sys.on.exit', 'sys.parent', 'sys.parents',  
'Sys.putenv', 'Sys.sleep',
                   'Sys.source', 'sys.source', 'sys.status',  
'Sys.time', 'system',
                   'system.file', 'tempfile', 'textConnection',  
'tkpager', 'tkStartGUI',
                   'unlink', 'unz', 'update.packages', 'url',  
'url.show',
                   'vi', 'write', 'write.dta', 'write.ftable',  
'write.socket',
                   'write.table', 'writeBin', 'writeLines', 'x11',  
'X11',
                   'xedit', 'xemacs', 'xfig', 'zip.file.extract');
# 'pdf',
   $n = count($banned);
   for ($i=0; $i<$n; $i++) {
     if (substr_count($input, $banned[$i])>0) {
     // okay, we found something forbidden, now we need a regular  
expression to check if it is a function call like 'name  (', 'name ='  
or 'name.' !
       $pattern = '/\b' . str_replace ('.', '\.', $banned[$i]) . '[\W] 
*[\(\=\.]+/';
       if (preg_match ($pattern, $input, $match) > 0) { return $banned 
[$i]; }
#      preg_match ($pattern, $input, $match);
#      error('Check', '', print_r($match, true));
     }
   }
   return '';
}

function renderR( $input , $params ) {
   //Eval the code between the tags.
   $style   = array_key_exists('style', $params)   ? ' style="' .  
$params['style'] . '"'  : '';
   $output  = array_key_exists('output', $params)  ? $params 
['output']  : 'text';
   $alt     = array_key_exists('alt', $params)     ? $params 
['alt']     : $input;
   $convert = array_key_exists('convert', $params) ? $params 
['convert'] : '';
   $onsave  = array_key_exists('onsave', $params);
   $echo    = array_key_exists('echo', $params);
   $direct  = !array_key_exists('iframe', $params);
   $sha1    = sha1($input . $output . $style);
   $ws      = array_key_exists('workspace', $params)? $params 
['workspace'] : '';

   // security checks
   if (security>0) {
     $chkres = checkCommands($input);
#    error ('R', $chkres, $input);
     (strlen($chkres)==0) or error ('R', 'security check failed: used  
banned command or parameter "' . $chkres . '"', $input);
     $input = $input . "\n#" . $chkres;
   }
   (preg_match('/\W+/', $ws)==0) or error ('R', 'security check  
failed: invalid workspace name "' . $ws . '"', $input);
   // check if iframe is given, if not then assume direct output
   $iframe = 'width:100%;height:250px;';
   if (!$direct) {
     $iframe  = makeStyle ($params['iframe'], $iframe);
   }
   if (array_key_exists('name', $params)) {
   // we may reuse the program ..., thus save all infos
     $sav = r_dir . DIRECTORY_SEPARATOR . $params['name'] . '.sav';
     $fd = fopen ($sav, 'w') or error ('R', 'can not open file: ' .  
$sav, $input);
     fwrite ($fd, '@output  ' . $output . "\n");
     fwrite ($fd, '@convert ' . $convert . "\n");
     fwrite ($fd, '@sha '     . $sha1 . "\n");
     fwrite ($fd, '@direct '  . $direct . "\n");
     fwrite ($fd, '@echo '    . $echo . "\n");
     fwrite ($fd, '@workspace '. $ws . "\n");
     fwrite ($fd, $input);
     fclose ($fd);
   }

   // execute R program
   $fn   = runR ($output, $convert, $sha1, $input, $direct, $echo, $ws);
   $name = array_key_exists('name', $params) ? $params['name'] : $sha1;

   // now switch between direct output and iframe output
   if ($direct) {
     $ret = file_get_contents ($fn);
   } else {
     $ret = '<iframe name="' . $name . '" style="' . $iframe . '"  
src="' . extractUrl ($fn) . '">Sorry, your browser does not support  
&lt;iframe...&gt;... &lt;/iframe&gt; !</iframe>';
   }

   return ($ret);
}

?>



More information about the R-sig-mediawiki mailing list