linux/conf/nginx/cgi-bin.php

52 lines
1.2 KiB
PHP
Raw Permalink Normal View History

2014-10-10 16:35:33 +02:00
<?php
2015-12-13 23:09:12 +01:00
// Adapted from https://wiki.archlinux.org/index.php/Awstats
2014-10-10 16:35:33 +02:00
2015-05-16 01:41:09 +02:00
// Prepare environment
2014-10-10 16:35:33 +02:00
$newenv = $_SERVER;
2015-12-13 23:09:12 +01:00
$newenv["SCRIPT_FILENAME"] = $_SERVER["X_SCRIPT_FILENAME"];
2015-05-16 01:41:09 +02:00
2015-12-13 23:09:12 +01:00
if (!is_executable($_SERVER["X_SCRIPT_FILENAME"]) || is_dir($_SERVER["X_SCRIPT_FILENAME"]))
2015-09-17 10:41:22 +02:00
error_exit(404, 'Not Found');
2014-10-10 16:35:33 +02:00
2015-05-16 01:41:09 +02:00
// Open process
$descriptorspec = array(
array('pipe', 'r'),
array('pipe', 'w'),
array('pipe', 'w'),
);
$pipes = array();
2015-12-13 23:09:12 +01:00
$process = proc_open($_SERVER["X_SCRIPT_FILENAME"], $descriptorspec, $pipes, null, $newenv);
2014-10-10 16:35:33 +02:00
2015-09-17 10:41:22 +02:00
if (!is_resource($process))
error_exit(500, 'Internal Server Error');
2014-10-10 16:35:33 +02:00
// All ok, send response
while (true) {
$head = fgets($pipes[1]);
2015-12-13 23:09:12 +01:00
if ($head === false || $head === "\r\n" || $head === "\n") break;
2014-10-10 16:35:33 +02:00
header($head);
}
fpassthru($pipes[1]);
2015-05-16 01:41:09 +02:00
// Close
2015-12-13 23:09:12 +01:00
$scriptErrors = trim(stream_get_contents($pipes[2]));
2015-05-16 01:41:09 +02:00
foreach ($pipes as $fh) fclose($fh);
2015-12-13 23:09:12 +01:00
$returnValue = proc_close($process);
2016-03-16 11:58:40 +01:00
if ($scriptErrors) error_exit(500, "Status=$returnValue Error=$scriptErrors");
2015-12-13 23:09:12 +01:00
exit;
2015-09-17 10:41:22 +02:00
// Exit helper
function error_exit($code, $msg)
{
header("$_SERVER[SERVER_PROTOCOL] $code $msg");
echo($msg);
2016-03-16 11:58:40 +01:00
trigger_error("$msg in $_SERVER[X_SCRIPT_FILENAME]", E_USER_ERROR);
2015-12-13 23:09:12 +01:00
exit(1);
2015-09-17 10:41:22 +02:00
}