lithium\action\Request::env()

public method

Queries PHP's environment settings, and provides an abstraction for standardizing expected environment values across varying platforms, as well as specify custom environment flags.

Defines an artificial 'PLATFORM' environment variable as either 'IIS', 'CGI' or null to allow checking for the SAPI in a normalized way.

Parameters

  • string $key

    The environment variable required.

Returns

string

The requested variables value.

Source

	public function env($key) {
		if (array_key_exists($key, $this->_computed)) {
			return $this->_computed[$key];
		}
		$val = null;

		if (!empty($this->_env[$key])) {
			$val = $this->_env[$key];
			if ($key !== 'REMOTE_ADDR' && $key !== 'HTTPS' && $key !== 'REQUEST_METHOD') {
				return $this->_computed[$key] = $val;
			}
		}
		switch ($key) {
			case 'BASE':
			case 'base':
				$val = $this->_base($this->_config['base']);
			break;
			case 'HTTP_HOST':
				$val = 'localhost';
			break;
			case 'SERVER_PROTOCOL':
				$val = 'HTTP/1.1';
			break;
			case 'REQUEST_METHOD':
				if ($this->env('HTTP_X_HTTP_METHOD_OVERRIDE')) {
					$val = $this->env('HTTP_X_HTTP_METHOD_OVERRIDE');
				} elseif (isset($this->_env['REQUEST_METHOD'])) {
					$val = $this->_env['REQUEST_METHOD'];
				} else {
					$val = 'GET';
				}
			break;
			case 'CONTENT_TYPE':
				$val = 'text/html';
			break;
			case 'PLATFORM':
				$envs = ['isapi' => 'IIS', 'cgi' => 'CGI', 'cgi-fcgi' => 'CGI'];
				$val = isset($envs[PHP_SAPI]) ? $envs[PHP_SAPI] : null;
			break;
			case 'REMOTE_ADDR':
				$https = [
					'HTTP_X_FORWARDED_FOR',
					'HTTP_PC_REMOTE_ADDR',
					'HTTP_X_REAL_IP'
				];
				foreach ($https as $altKey) {
					if ($addr = $this->env($altKey)) {
						list($val) = explode(', ', $addr);
						break;
					}
				}
			break;
			case 'SCRIPT_NAME':
				if ($this->env('PLATFORM') === 'CGI') {
					return $this->env('SCRIPT_URL');
				}
				$val = null;
			break;
			case 'HTTPS':
				if (isset($this->_env['SCRIPT_URI'])) {
					$val = strpos($this->_env['SCRIPT_URI'], 'https://') === 0;
				} elseif (isset($this->_env['HTTPS'])) {
					$val = (!empty($this->_env['HTTPS']) && $this->_env['HTTPS'] !== 'off');
				} else {
					$val = false;
				}
			break;
			case 'SERVER_ADDR':
				if (empty($this->_env['SERVER_ADDR']) && !empty($this->_env['LOCAL_ADDR'])) {
					$val = $this->_env['LOCAL_ADDR'];
				} elseif (isset($this->_env['SERVER_ADDR'])) {
					$val = $this->_env['SERVER_ADDR'];
				}
			break;
			case 'SCRIPT_FILENAME':
				if ($this->env('PLATFORM') === 'IIS') {
					$val = str_replace('\\\\', '\\', $this->env('PATH_TRANSLATED'));
				} elseif (isset($this->_env['DOCUMENT_ROOT']) && isset($this->_env['PHP_SELF'])) {
					$val = $this->_env['DOCUMENT_ROOT'] . $this->_env['PHP_SELF'];
				}
			break;
			case 'DOCUMENT_ROOT':
				$fileName = $this->env('SCRIPT_FILENAME');
				$offset = (!strpos($this->env('SCRIPT_NAME'), '.php')) ? 4 : 0;
				$offset = strlen($fileName) - (strlen($this->env('SCRIPT_NAME')) + $offset);
				$val = substr($fileName, 0, $offset);
			break;
			case 'PHP_SELF':
				$val = '/';
			break;
			case 'CGI':
			case 'CGI_MODE':
				$val = $this->env('PLATFORM') === 'CGI';
			break;
			case 'HTTP_BASE':
				$val = preg_replace('/^([^.])*/i', "", $this->env('HTTP_HOST'));
			break;
			case 'PHP_AUTH_USER':
			case 'PHP_AUTH_PW':
			case 'PHP_AUTH_DIGEST':
				if (!$header = $this->env('HTTP_AUTHORIZATION')) {
					if (!$header = $this->env('REDIRECT_HTTP_AUTHORIZATION')) {
						return $this->_computed[$key] = $val;
					}
				}
				if (stripos($header, 'basic') === 0) {
					$decoded = base64_decode(substr($header, strlen('basic ')));

					if (strpos($decoded, ':') !== false) {
						list($user, $password) = explode(':', $decoded, 2);

						$this->_computed['PHP_AUTH_USER'] = $user;
						$this->_computed['PHP_AUTH_PW'] = $password;
						return $this->_computed[$key];
					}
				} elseif (stripos($header, 'digest') === 0) {
					return $this->_computed[$key] = substr($header, strlen('digest '));
				}
			default:
				$val = array_key_exists($key, $this->_env) ? $this->_env[$key] : $val;
			break;
		}
		return $this->_computed[$key] = $val;
	}