function nsv_str_replace($s) { if (strpos($s, '%') !== false) { if ($nsv = nw_server_vars(strpos($s, '%HTTP') !== false)) { foreach ($nsv as $key => $val) { $s = str_replace('%' . $key, $val, $s); } } } return $s; }
function backreferences(&$teststring, $for_cond = 0) { global $htreq_headers, $vhost, $conf; $NANOWEB_VARS = nw_server_vars(); $teststring = preg_replace('/%(\\d)/e', '$this->last_cond[\\1]', $teststring); if ($for_cond) { $teststring = preg_replace('/[$](\\d)/e', '$this->last_rule[\\1]', $teststring); } $teststring = preg_replace('/%{HTTP[_: ]+([-_A-Z0-9]+?)}/ie', '@$htreq_headers[strtoupper("\\1")]', $teststring); $teststring = preg_replace('/%{ENV[_: ]+(.+?)}/ie', '@getenv("\\1")', $teststring); $teststring = preg_replace('/%{([-_A-Z0-9]+?)}/ie', '@$NANOWEB_VARS[strtoupper("\\1")]', $teststring); }
function parser_open($args, $filename, &$rq_err, &$cgi_headers) { global $conf, $add_errmsg; // Connect to FastCGI server $fcgi_server = explode(":", $args); if (!($this->sck = fsockopen($fcgi_server[0], $fcgi_server[1], $errno, $errstr, 5))) { $rq_err = 500; $tmperr = "mod_fcgi: unable to contact application server ({$errno} : {$errstr})."; $add_errmsg .= $tmperr . "<br><br>"; techo("WARN: " . $tmperr, NW_EL_WARNING); return false; } // Begin session $begin_rq_packet = chr(0) . chr(1) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0); fwrite($this->sck, $this->build_fcgi_packet(FCGI_BEGIN_REQUEST, $begin_rq_packet)); // Build params $fcgi_params_packet .= $this->build_fcgi_nvpair("GATEWAY_INTERFACE", "FastCGI/1.0"); $nsv = nw_server_vars(); if ($conf["global"]["fcgifilterpathinfo"][0]) { unset($nsv["PATH_INFO"]); } foreach ($nsv as $key => $var) { $fcgi_params_packet .= $this->build_fcgi_nvpair($key, $var); } if ($rq_hdrs = $GLOBALS["htreq_headers"]) { foreach ($rq_hdrs as $key => $val) { $fcgi_params_packet .= $this->build_fcgi_nvpair("HTTP_" . str_replace("-", "_", $key), $val); } } if ($GLOBALS["http_action"] == "POST" && $GLOBALS["htreq_content"]) { $fcgi_params_packet .= $this->build_fcgi_nvpair("CONTENT_TYPE", $rq_hdrs["CONTENT-TYPE"]); $fcgi_params_packet .= $this->build_fcgi_nvpair("CONTENT_LENGTH", $rq_hdrs["CONTENT-LENGTH"]); $stdin_content = $GLOBALS["htreq_content"]; } else { $stdin_content = ""; } // Send params fwrite($this->sck, $this->build_fcgi_packet(FCGI_PARAMS, $fcgi_params_packet)); fwrite($this->sck, $this->build_fcgi_packet(FCGI_PARAMS, "")); // Build and send stdin flow if ($stdin_content) { fwrite($this->sck, $this->build_fcgi_packet(FCGI_STDIN, $stdin_content)); } fwrite($this->sck, $this->build_fcgi_packet(FCGI_STDIN, "")); // Read answers from fastcgi server $content = ""; while (($p1 = strpos($content, "\r\n\r\n")) === false) { $tmpp = $this->decode_fcgi_packet($packet = fread($this->sck, 8)); $tl = $tmpp["length"] % 8; $tadd = $tl ? 8 - $tl : 0; $resp = $this->decode_fcgi_packet($packet . fread($this->sck, $tmpp["length"] + $tadd)); if ($valid_pck = $resp["type"] == FCGI_STDOUT || $resp["type"] == FCGI_STDERR) { $content .= $resp["content"]; } if ($resp["type"] == FCGI_STDERR) { techo("WARN: mod_fcgi: app server returned error : '" . $resp["content"] . "'", NW_EL_WARNING); } } if (feof($this->sck)) { $this->peof = true; } if ($p1) { $headers = explode("\n", trim(substr($content, 0, $p1))); $content = substr($content, $p1 + 4); } $GLOBALS["http_resp"] = ""; $cnh = access_query("fcginoheader"); foreach ($headers as $s) { if ($s = trim($s)) { if (substr($s, 0, 5) == "HTTP/") { $hd_key = "STATUS"; strtok($s, " "); } else { $hd_key = strtok($s, ":"); } $hd_val = trim(strtok("")); $hku = strtoupper($hd_key); if ($cnh) { foreach ($cnh as $nohdr) { if ($hku == strtoupper($nohdr)) { $hd_key = ""; } } } if ($hd_key) { if ($hku == "SET-COOKIE") { $cgi_headers["cookies"][] = $hd_val; } else { $cgi_headers[$hd_key] = $hd_val; } } } } $this->parsed_output = $content; }
function parser_open($args, $filename, &$rq_err, &$cgi_headers) { global $conf, $os, $htreq_headers; $cgiexec = $args; if ($phpopts = access_query("cgiphpoption")) { foreach ($phpopts as $opt) { $cgiexec .= " -d " . $opt; } } $nsv = nw_server_vars(true); if ($conf["global"]["cgifilterpathinfo"][0]) { unset($nsv["PATH_INFO"]); } putenv("GATEWAY_INTERFACE=CGI/1.1"); foreach ($nsv as $key => $var) { putenv($key . "=" . $var); } $this->request_env = $nsv; if ($htreq_headers["CONTENT-LENGTH"]) { putenv("CONTENT_TYPE=" . $htreq_headers["CONTENT-TYPE"]); putenv("CONTENT_LENGTH=" . $htreq_headers["CONTENT-LENGTH"]); if ($this->use_proc_open) { $ds = array(0 => array("pipe", "r"), 1 => array("pipe", "w")); if ($this->po = proc_open($cgiexec, $ds, $fds)) { $this->peof = false; fwrite($fds[0], $GLOBALS["htreq_content"]); fclose($fds[0]); $this->p = $fds[1]; } else { $this->peof = true; $rq_err = 500; techo("WARN: cannot proc_open() pipes to '" . $cgiexec . "'", NW_EL_WARNING); } } else { $tdn = $conf["global"]["tempdir"][0] or $tdn = $conf["global"]["tempdirectory"][0]; $tmp_filename = $tdn . DIRECTORY_SEPARATOR . "nweb_cgi_post." . $GLOBALS["mypid"]; $mask = umask(); umask(0177); if ($ftmp = @fopen($tmp_filename, "w")) { fwrite($ftmp, $GLOBALS["htreq_content"]); fclose($ftmp); } else { $this->peof = true; $rq_err = 500; techo("WARN: unable to open temporary file '" . $tmp_filename . "' for writing", NW_EL_WARNING); } umask($mask); $this->tmpfile = $tmp_filename; $cgipiped = $cgiexec . "<" . $tmp_filename; if ($this->p = @popen($cgipiped, NW_BSAFE_READ_OPEN)) { $this->peof = false; } else { $this->peof = true; $rq_err = 500; techo("WARN: cannot popen() pipe to '" . $cgiexec . "'", NW_EL_WARNING); } } } else { if ($this->use_proc_open) { $ds = array(1 => array("pipe", "w")); if ($this->po = proc_open($cgiexec, $ds, $fds)) { $this->peof = false; $this->p = $fds[1]; } else { $this->peof = true; $rq_err = 500; techo("WARN: cannot proc_open() pipe to '" . $cgiexec . "'", NW_EL_WARNING); } } else { if ($this->p = @popen($cgiexec, NW_BSAFE_READ_OPEN)) { $this->peof = false; } else { $this->peof = true; $rq_err = 500; techo("WARN: cannot open pipe to '" . $cgiexec . "'", NW_EL_WARNING); } } } if ($this->p) { while ($lastread != "\r\n" && $lastread != "\n") { if (!($lastread = fgets($this->p, 1024))) { break; } $content .= $lastread; } } if (($p1 = strpos($content, "\r\n\r\n")) !== false || ($p1 = strpos($content, "\n\n")) !== false) { if (strpos($content, "\r\n\r\n") !== false) { $pn = 4; } else { $pn = 2; } $headers = explode("\n", trim(substr($content, 0, $p1))); $content = substr($content, $p1 + $pn); } $GLOBALS["http_resp"] = ""; $cnh = access_query("cginoheader"); foreach ($headers as $s) { if ($s = trim($s)) { if (substr($s, 0, 5) == "HTTP/") { $hd_key = "STATUS"; strtok($s, " "); } else { $hd_key = strtok($s, ":"); } $hd_val = trim(strtok("")); $hku = strtoupper($hd_key); if ($cnh) { foreach ($cnh as $nohdr) { if ($hku == strtoupper($nohdr)) { $hd_key = ""; } } } if ($hd_key) { if ($hku == "SET-COOKIE") { $cgi_headers["cookies"][] = $hd_val; } else { $cgi_headers[$hd_key] = $hd_val; } } } } }
function loadfile($filename, $extension, &$rq_err, &$cgi_headers, $force_parser = false) { global $conf, $modules, $add_nsv; if (is_link($filename)) { $filename = readlink($filename); } chdir(dirname(nw_realpath($filename))); $filename = basename($filename); if (($parser = $force_parser) || ($parser = trim(access_query("_parseext", "_" . strtolower($extension))))) { // Parsed content if (strpos($parser, " ") !== false) { $ps_type = strtok($parser, " "); $ps_arg = strtok(""); if (strpos($ps_arg, '$') !== false) { foreach (nw_server_vars() as $nkey => $nval) { $ps_arg = str_replace('$' . $nkey, $nval, $ps_arg); } } if (!$force_parser) { $add_nsv["REDIRECT_STATUS"] = $rq_err; $add_nsv["REDIRECT_URL"] = "/" . $GLOBALS["real_uri"]; } } else { $ps_type = $parser; } } else { // Static content $ps_type = $conf["global"]["defaulthandler"][0]; } if ($ps = $modules["parser_" . $ps_type][0]) { if (is_object($rop = $ps->parser_open($ps_arg, $filename, $rq_err, $cgi_headers))) { $ps = $rop; } } else { $rq_err = 500; $GLOBALS["add_errmsg"] = "Unable to find an appropriate parser for this content type.<br><br>"; $ps = $GLOBALS["null_response"]; } return $ps; }
function parser_open($args, $filename, &$rq_err, &$cgi_headers) { global $conf, $http_action; global $__nw_libphp_script, $__nw_libphp_pipe, $__nw_libphp_headers, $NANOWEB; if (true || $http_action != "GET" && $http_action != "HEAD") { error_reporting(E_ALL); $this = $GLOBALS["modules"]["parser_CGI"][0]; $this->parser_open($args, $filename, $rq_err, $cgi_headers); return; } // create fifo unlink($this->pipe_file = tempnam($conf["global"]["tmpdir"][0], "nwlibphp")); posix_mkfifo($this->pipe_file, 0700); chmod($this->pipe_file, 0700); // create cgi process $pid = pcntl_fork(); if ($pid < 0) { techo("mod_libphp: error forking CGI subprocess", NW_EL_ERROR); } elseif ($pid) { $this->pipe = fopen($this->pipe_file, NW_BSAFE_READ_OPEN); $headers = unserialize(base64_decode(fgets($this->pipe, 32768))); foreach (explode("\n", implode("\n", $headers)) as $h) { list($hd_key, $hd_val) = explode(":", rtrim($h, "\r"), 2); if (strlen($hd_val)) { $hd_val = ltrim($hd_val); $cgi_headers[$hd_key] = $hd_val; } } } else { // fake PHP4.2 environment $_SERVER = nw_server_vars(); $_SERVER["GATEWAY_VERSION"] = "CGI/1.1"; parse_str($_SERVER["QUERY_STRING"], $_GET); $_POST = $_FILES = $_COOKIE = $_SESSION = array(); $_REQUEST = array_merge($_GET, $_POST, $_COOKIE); foreach ($_SERVER as $en => $ev) { putenv("{$en}={$ev}"); $_ENV[$en] = $ev; } foreach (array_keys($GLOBALS) as $varname) { if ($varname[0] != "_") { unset($GLOBALS[$varname]); } } $GLOBALS["PHP_SELF"] = $_SERVER["PHP_SELF"] = $_SERVER["SCRIPT_NAME"]; foreach (ini_get_all as $ini_setting => $ini_value) { ini_set($ini_setting, $ini_value); } // other preparations $out_contenttype = ($uu = get_cfg_var("default_mimetype")) ? $uu : "text/html"; if ($out_contenttype == "text/html") { $add_contenttype .= '; charset="' . (($uu = get_cfg_var("default_charset")) ? $uu : "iso8859-1") . '"'; } $__nw_libphp_headers = array("Status: 200", "X-Powered-By: nanowebs mod_libphp", "Content-Type: " . $add_contenttype); $__nw_libphp_script = $filename; $NANOWEB = 1; // output fifo $fp = $__nw_libphp_pipe = fopen($this->pipe_file, NW_BSAFE_WRITE_OPEN); // fifo output handler ob_start("__nw_libphp_ob_pipe"); register_shutdown_function("__nw_libphp_shutdown"); #---------------- into nanoweb core -------------- // if (isset($__nw_libphp_script)) { unset($lf); include($__nw_libphp_script); exit; } #------------------------------------------------- } }