xref: /PHP-7.4/sapi/cgi/tests/include.inc (revision 902d39a3)
1<?php
2
3function get_cgi_path() /* {{{ */
4{
5	$php = getenv("TEST_PHP_EXECUTABLE");
6
7	$cli = false;
8	$cgi = false;
9
10	if (file_exists($php) && is_executable($php)) {
11		$version = `$php -n -v`;
12		if (strstr($version, "(cli)")) {
13			/* that's cli */
14			$cli = true;
15		} else if (strpos($version, "(cgi")) {
16			/* that's cgi */
17			return $php;
18		}
19	}
20
21	if ($cli) {
22		/* trying to guess ... */
23		$php_path = $php;
24		if (defined("PHP_WINDOWS_VERSION_MAJOR")) {
25			/* On Windows it should be in the same dir as php.exe in most of the cases. */
26			$php_path = dirname($php);
27
28			if (is_dir($php_path) && file_exists("$php_path/php-cgi.exe") && is_executable("$php_path/php-cgi.exe")) {
29				return "$php_path/php-cgi.exe";
30			}
31		} else {
32			for ($i = 0; $i < 2; $i++) {
33				$slash_pos = strrpos($php_path, "/");
34				if ($slash_pos) {
35					$php_path = substr($php_path, 0, $slash_pos);
36				} else {
37					return FALSE;
38				}
39			}
40
41			if ($php_path && is_dir($php_path) && file_exists($php_path."/cgi/php-cgi") && is_executable($php_path."/cgi/php-cgi")) {
42				/* gotcha */
43				return $php_path."/cgi/php-cgi";
44			}
45		}
46		return false;
47	}
48	/* uhm? what's that then? */
49	return false;
50}
51/* }}} */
52
53function reset_env_vars() /* {{{ */
54{
55	putenv("REDIRECT_STATUS");
56	putenv("QUERY_STRING");
57	putenv("PATH_TRANSLATED");
58	putenv("SCRIPT_FILENAME");
59	putenv("SERVER_SOFTWARE");
60	putenv("SERVER_NAME");
61	putenv("GATEWAY_INTERFACE");
62	putenv("REQUEST_METHOD");
63}
64/* }}} */
65
66?>
67