Merge pull request #70 from eric1234/noenv

Use $_SERVER instead of getenv.
pull/57/merge
Mike Cao 11 years ago
commit 6a7622d99b

@ -466,7 +466,8 @@ class Engine {
$this->response()->header('ETag', $id); $this->response()->header('ETag', $id);
if ($id === getenv('HTTP_IF_NONE_MATCH')) { if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
$_SERVER['HTTP_IF_NONE_MATCH'] === $id) {
$this->halt(304); $this->halt(304);
} }
} }
@ -479,7 +480,8 @@ class Engine {
public function _lastModified($time) { public function _lastModified($time) {
$this->response()->header('Last-Modified', date(DATE_RFC1123, $time)); $this->response()->header('Last-Modified', date(DATE_RFC1123, $time));
if ($time === strtotime(getenv('HTTP_IF_MODIFIED_SINCE'))) { if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) === $time) {
$this->halt(304); $this->halt(304);
} }
} }

@ -132,23 +132,23 @@ class Request {
// Default properties // Default properties
if (empty($config)) { if (empty($config)) {
$config = array( $config = array(
'url' => getenv('REQUEST_URI') ?: '/', 'url' => $this->server('REQUEST_URI', '/'),
'base' => str_replace(array('\\',' '), array('/','%20'), dirname(getenv('SCRIPT_NAME'))), 'base' => str_replace(array('\\',' '), array('/','%20'), dirname($this->server('SCRIPT_NAME'))),
'method' => getenv('REQUEST_METHOD') ?: 'GET', 'method' => $this->server('REQUEST_METHOD', 'GET'),
'referrer' => getenv('HTTP_REFERER') ?: '', 'referrer' => $this->server('HTTP_REFERER'),
'ip' => getenv('REMOTE_ADDR') ?: '', 'ip' => $this->server('REMOTE_ADDR'),
'ajax' => getenv('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest', 'ajax' => $this->server('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest',
'scheme' => getenv('SERVER_PROTOCOL') ?: 'HTTP/1.1', 'scheme' => $this->server('SERVER_PROTOCOL', 'HTTP/1.1'),
'user_agent' => getenv('HTTP_USER_AGENT') ?: '', 'user_agent' => $this->server('HTTP_USER_AGENT'),
'body' => file_get_contents('php://input'), 'body' => file_get_contents('php://input'),
'type' => getenv('CONTENT_TYPE') ?: '', 'type' => $this->server('CONTENT_TYPE'),
'length' => getenv('CONTENT_LENGTH') ?: 0, 'length' => $this->server('CONTENT_LENGTH', 0),
'query' => new Collection($_GET), 'query' => new Collection($_GET),
'data' => new Collection($_POST), 'data' => new Collection($_POST),
'cookies' => new Collection($_COOKIE), 'cookies' => new Collection($_COOKIE),
'files' => new Collection($_FILES), 'files' => new Collection($_FILES),
'secure' => getenv('HTTPS') && getenv('HTTPS') != 'off', 'secure' => $this->server('HTTPS', 'off') != 'off',
'accept' => getenv('HTTP_ACCEPT') ?: '', 'accept' => $this->server('HTTP_ACCEPT'),
'proxy_ip' => $this->getProxyIpAddress() 'proxy_ip' => $this->getProxyIpAddress()
); );
} }
@ -225,4 +225,11 @@ class Request {
return ''; return '';
} }
/**
* Get variable from $_SERVER using $default if not provided
*/
private function server($var, $default='') {
return isset($_SERVER[$var]) ? $_SERVER[$var] : $default;
}
} }

@ -187,7 +187,7 @@ class Response {
header( header(
sprintf( sprintf(
'%s %d %s', '%s %d %s',
getenv('SERVER_PROTOCOL') ?: 'HTTP/1.1', (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'),
$this->status, $this->status,
self::$codes[$this->status]), self::$codes[$this->status]),
true, true,

@ -25,7 +25,7 @@ class RedirectTest extends PHPUnit_Framework_TestCase
} }
function setUp() { function setUp() {
putenv('SCRIPT_NAME=/subdir/index.php'); $_SERVER['SCRIPT_NAME'] = '/subdir/index.php';
$this->app = new \flight\Engine(); $this->app = new \flight\Engine();
$this->app->set('flight.base_url', '/testdir'); $this->app->set('flight.base_url', '/testdir');

@ -17,13 +17,12 @@ class RequestTest extends PHPUnit_Framework_TestCase
private $request; private $request;
function setUp() { function setUp() {
putenv('REQUEST_URI=/'); $_SERVER['REQUEST_URI'] = '/';
putenv('SCRIPT_NAME=/index.php'); $_SERVER['SCRIPT_NAME'] = '/index.php';
putenv('REQUEST_METHOD=GET'); $_SERVER['REQUEST_METHOD'] = 'GET';
putenv('HTTP_X_REQUESTED_WITH=XMLHttpRequest'); $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
putenv('REQUEST_URI=/'); $_SERVER['REMOTE_ADDR'] = '8.8.8.8';
putenv('REMOTE_ADDR=8.8.8.8'); $_SERVER['HTTPS'] = 'on';
putenv('HTTPS=on');
$_SERVER['HTTP_X_FORWARDED_FOR'] = '32.32.32.32'; $_SERVER['HTTP_X_FORWARDED_FOR'] = '32.32.32.32';
$this->request = new \flight\net\Request(); $this->request = new \flight\net\Request();
@ -48,7 +47,7 @@ class RequestTest extends PHPUnit_Framework_TestCase
} }
function testSubdirectory() { function testSubdirectory() {
putenv('SCRIPT_NAME=/subdir/index.php'); $_SERVER['SCRIPT_NAME'] = '/subdir/index.php';
$request = new \flight\net\Request(); $request = new \flight\net\Request();
@ -56,7 +55,7 @@ class RequestTest extends PHPUnit_Framework_TestCase
} }
function testQueryParameters() { function testQueryParameters() {
putenv('REQUEST_URI=/page?id=1&name=bob'); $_SERVER['REQUEST_URI'] = '/page?id=1&name=bob';
$request = new \flight\net\Request(); $request = new \flight\net\Request();
@ -66,7 +65,7 @@ class RequestTest extends PHPUnit_Framework_TestCase
} }
function testCollections() { function testCollections() {
putenv('REQUEST_URI=/page?id=1'); $_SERVER['REQUEST_URI'] = '/page?id=1';
$_GET['q'] = 1; $_GET['q'] = 1;
$_POST['q'] = 1; $_POST['q'] = 1;

Loading…
Cancel
Save