function process_FastRWeb()
{
    global $root;
    // $req = array_merge($_GET, $_POST);
    $path = $_SERVER['PATH_INFO'];
    if (!isset($path)) {
        echo "No path specified.";
        return FALSE;
    }
    $sp = str_replace("..", "_", $path);
    // sanitize paths
    $script = "{$root}/web.R{$sp}.R";
    if (!file_exists($script)) {
        echo "Script [{$script}] {$sp}.R does not exist.";
        return FALSE;
    }
    // escape dangerous characters
    $script = str_replace("\\", "\\\\", $script);
    $script = str_replace("\"", "\\\"", $script);
    $qs = str_replace("\\", "\\\\", $_SERVER['QUERY_STRING']);
    $qs = str_replace("\"", "\\\"", $qs);
    $s = Rserve_connect();
    $r = Rserve_eval($s, "{ qs<-\"{$qs}\"; setwd('{$root}/tmp'); library(FastRWeb); .out<-''; cmd<-'html'; ct<-'text/html'; hdr<-''; pars<-list(); lapply(strsplit(strsplit(qs,\"&\")[[1]],\"=\"),function(x) pars[[x[1]]]<<-x[2]); if(exists('init') && is.function(init)) init(); as.character(try({source(\"{$script}\"); as.WebResult(do.call(run, pars)) },silent=TRUE))}");
    Rserve_close($s);
    if (!is_array($r)) {
        // this ususally means that an erro rocurred since the returned value is jsut a string
        ob_end_flush();
        echo $r;
        exit(0);
    }
    if (isset($r[2])) {
        header("Content-type: {$r['2']}");
    }
    if ($r[0] == "file" or $r[0] == "tmpfile") {
        $f = fopen($r[1], "rb");
        $contents = '';
        while (!feof($f)) {
            $contents .= fread($f, 8192);
        }
        fclose($f);
        ob_end_clean();
        echo $contents;
        if ($r[0] == "tmpfile") {
            unlink($r[0]);
        }
        exit(0);
    }
    if ($r[0] == "html") {
        ob_end_clean();
        echo is_array($r[1]) ? implode("\n", $r[1]) : $r[1];
        exit(0);
    }
    print_r($r);
    ob_end_flush();
    exit(0);
}
Exemple #2
0
function process_FastRWeb($host, $port)
{
    global $root;
    // $req = array_merge($_GET, $_POST);
    $path = $_SERVER['PATH_INFO'];
    if (!isset($path)) {
        echo "No path specified.";
        return FALSE;
    }
    $path = str_replace("..", "_", $path);
    // sanitize paths
    // We cannot check for the presence of the script, because FastRWeb now
    // supports PATH_INFO *after* the script name.
    // If you don't care but want fast error, uncomment the followign two lines.
    // $script = "$root/web.R$path.R";
    // if (!file_exists($script)) { echo "Script [$script] $path.R does not exist."; return FALSE; }
    $raddr = $_SERVER['REMOTE_ADDR'];
    $method = $_SERVER['REQUEST_METHOD'];
    $ct = $_SERVER['CONTENT_TYPE'];
    $cl = $_SERVER['CONTENT_LENGTH'] + 0;
    // escape as needed
    $uri = str_replace("\"", "\\\"", str_replace("\\", "\\\\", $_SERVER['REQUEST_URI']));
    // NOTE: the C client URI-encodes control characters and quotes -- maybe a good idea?
    $cook = str_replace("\"", "\\\"", str_replace("\\", "\\\\", $_SERVER['HTTP_COOKIE']));
    $qs = str_replace("\"", "\\\"", str_replace("\\", "\\\\", $_SERVER['QUERY_STRING']));
    $path = str_replace("\"", "\\\"", str_replace("\\", "\\\\", $path));
    $s = Rserve_connect($host, $port);
    $bodyval = "NULL";
    if ($ct == "" && $cl == 0) {
        $cl = -1;
    } else {
        $body = file_get_contents("php://input");
        Rserve_assign_raw($s, "body", $body);
        $bodyval = ".GlobalEnv\$body";
    }
    $r = Rserve_eval($s, "{ library(FastRWeb); request<-list(uri=\"{$uri}\", method=\"{$method}\", c.type=\"{$ct}\", c.length={$cl}, body={$bodyval}, client.ip=\"{$raddr}\", query.string=\"{$qs}\", raw.cookies=\"{$cook}\"); FastRWeb:::.run(request,\"{$root}\",\"{$path}\") }");
    Rserve_close($s);
    if (!is_array($r)) {
        // this ususally means that an erro rocurred since the returned value is jsut a string
        ob_end_flush();
        echo $r;
        exit(0);
    }
    if (isset($r[2])) {
        header("Content-type: {$r['2']}");
    }
    if ($r[0] == "file" or $r[0] == "tmpfile") {
        error_log("INFO: {$r['0']}: '{$r['1']}'");
        $contents = file_get_contents($root . "/tmp/" . $r[1]);
        ob_end_clean();
        echo $contents;
        if ($r[0] == "tmpfile") {
            unlink($r[0]);
        }
        exit(0);
    }
    if ($r[0] == "html") {
        ob_end_clean();
        echo is_array($r[1]) ? implode("\n", $r[1]) : $r[1];
        exit(0);
    }
    print_r($r);
    ob_end_flush();
    exit(0);
}