Adding CGI_MODE detection and CGI-mode SCRIPT_NAME emulation to env()

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3451 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2006-09-05 18:10:59 +00:00
parent 213e22b1a8
commit d232f47d72

View file

@ -790,30 +790,56 @@
return $uri; return $uri;
} }
/** /**
* Gets an environment variable from available sources. * Gets an environment variable from available sources, and provides emulation
* Used as a backup if $_SERVER/$_ENV are disabled. * for unsupported or inconsisten environment variables (i.e. DOCUMENT_ROOT on
* IIS, or SCRIPT_NAME in CGI mode). Also exposes some additional custom
* environment information.
* *
* @param string $key Environment variable name. * @param string $key Environment variable name.
* @return string Environment variable setting. * @return string Environment variable setting.
*/ */
function env($key) { function env($key) {
if (isset($_SERVER[$key])) {
return $_SERVER[$key]; if ($key == 'SCRIPT_NAME') {
} elseif (isset($_ENV[$key])) { if (env('CGI_MODE')) {
return $_ENV[$key]; $key = 'SCRIPT_URL';
} elseif (getenv($key) !== false) { }
return getenv($key);
} }
if ($key == 'DOCUMENT_ROOT') { $val = null;
$offset=0; if (isset($_SERVER[$key])) {
$val = $_SERVER[$key];
} elseif (isset($_ENV[$key])) {
$val = $_ENV[$key];
} elseif (getenv($key) !== false) {
$val = getenv($key);
}
if ($key == 'REMOTE_ADDR' && $val == env('SERVER_ADDR')) {
$addr = env('HTTP_PC_REMOTE_ADDR');
if ($addr != null) {
$val = $addr;
}
}
if ($val !== null) {
return $val;
}
switch ($key) {
case 'DOCUMENT_ROOT':
$offset = 0;
if (!strpos(env('SCRIPT_NAME'), '.php')) { if (!strpos(env('SCRIPT_NAME'), '.php')) {
$offset = 4; $offset = 4;
} }
return substr(env('SCRIPT_FILENAME'), 0, strlen(env('SCRIPT_FILENAME')) - (strlen(env('SCRIPT_NAME')) + $offset)); return substr(env('SCRIPT_FILENAME'), 0, strlen(env('SCRIPT_FILENAME')) - (strlen(env('SCRIPT_NAME')) + $offset));
} break;
if ($key == 'PHP_SELF') { case 'PHP_SELF':
return r(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME')); return r(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
break;
case 'CGI_MODE':
return (substr(php_sapi_name(), 0, 3) == 'cgi');
break;
} }
return null; return null;
} }