Skip to content

Commit

Permalink
Merge branch 'release-1.6.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Lockhart committed Apr 21, 2012
2 parents b3024df + ea6e77f commit f2444b3
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 10 deletions.
5 changes: 3 additions & 2 deletions Slim/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private function __construct( $settings = null ) {
if ( strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0 ) {
$env['SCRIPT_NAME'] = $_SERVER['SCRIPT_NAME']; //Without URL rewrite
} else {
$env['SCRIPT_NAME'] = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME); //With URL rewrite
$env['SCRIPT_NAME'] = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']) ); //With URL rewrite
}
$env['PATH_INFO'] = substr_replace($_SERVER['REQUEST_URI'], '', 0, strlen($env['SCRIPT_NAME']));
if ( strpos($env['PATH_INFO'], '?') !== false ) {
Expand All @@ -158,6 +158,7 @@ private function __construct( $settings = null ) {
//HTTP request headers
$specialHeaders = array('CONTENT_TYPE', 'CONTENT_LENGTH', 'PHP_AUTH_USER', 'PHP_AUTH_PW', 'PHP_AUTH_DIGEST', 'AUTH_TYPE');
foreach ( $_SERVER as $key => $value ) {
$value = is_string($value) ? trim($value) : $value;
if ( strpos($key, 'HTTP_') === 0 ) {
$env[substr($key, 5)] = $value;
} else if ( strpos($key, 'X_') === 0 || in_array($key, $specialHeaders) ) {
Expand Down Expand Up @@ -222,4 +223,4 @@ public function offsetUnset( $offset ) {
public function getIterator() {
return new ArrayIterator($this->properties);
}
}
}
20 changes: 20 additions & 0 deletions Slim/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ public function post( $key = null ) {
parse_str($this->env['slim.input'], $output);
}
$this->env['slim.request.form_hash'] = Slim_Http_Util::stripSlashesIfMagicQuotes($output);
} else {
$this->env['slim.request.form_hash'] = Slim_Http_Util::stripSlashesIfMagicQuotes($_POST);
}
}
if ( $key ) {
Expand All @@ -245,6 +247,15 @@ public function put( $key = null ) {
return $this->post($key);
}

/**
* Fetch DELETE data (alias for Slim_Http_Request::post)
* @param string $key
* @return array|mixed|null
*/
public function delete( $key = null ) {
return $this->post($key);
}

/**
* Fetch COOKIE data
*
Expand Down Expand Up @@ -393,6 +404,10 @@ public function getContentLength() {
*/
public function getHost() {
if ( isset($this->env['HOST']) ) {
if ( strpos($this->env['HOST'], ':') !== false ) {
$hostParts = explode(':', $this->env['HOST']);
return $hostParts[0];
}
return $this->env['HOST'];
} else {
return $this->env['SERVER_NAME'];
Expand Down Expand Up @@ -480,6 +495,11 @@ public function getUrl() {
* @return string
*/
public function getIp() {
if ( isset($this->env['X_FORWARDED_FOR']) ) {
return $this->env['X_FORWARDED_FOR'];
} else if ( isset($this->env['CLIENT_IP']) ) {
return $this->env['CLIENT_IP'];
}
return $this->env['REMOTE_ADDR'];
}

Expand Down
21 changes: 19 additions & 2 deletions Slim/Http/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public static function decrypt( $data, $key, $iv, $settings = array() ) {
*/
public static function encodeSecureCookie( $value, $expires, $secret, $algorithm, $mode ) {
$key = hash_hmac('sha1', $expires, $secret);
$iv = md5($expires);
$iv = self::get_iv($expires, $secret);
$secureString = base64_encode(self::encrypt($value, $key, $iv, array(
'algorithm' => $algorithm,
'mode' => $mode
Expand Down Expand Up @@ -211,7 +211,7 @@ public static function decodeSecureCookie( $value, $secret, $algorithm, $mode )
$value = explode('|', $value);
if ( count($value) === 3 && ( (int)$value[0] === 0 || (int)$value[0] > time() ) ) {
$key = hash_hmac('sha1', $value[0], $secret);
$iv = md5($value[0]);
$iv = self::get_iv($value[0], $secret);
$data = self::decrypt(base64_decode($value[1]), $key, $iv, array(
'algorithm' => $algorithm,
'mode' => $mode
Expand Down Expand Up @@ -361,4 +361,21 @@ public static function parseCookieHeader( $header ) {
}
return $cookies;
}

/**
* Generate a random IV
*
* This method will generate a non-predictable IV for use with
* the cookie encryption
*
* @param int $expires The UNIX timestamp at which this cookie will expire
* @param string $secret The secret key used to hash the cookie value
* @return binary string with length 40
*/
private static function get_iv($expires, $secret) {
$data1 = hash_hmac('sha1', 'a'.$expires.'b', $secret);
$data2 = hash_hmac('sha1', 'z'.$expires.'y', $secret);
return pack("h*", $data1.$data2);
}

}
2 changes: 1 addition & 1 deletion Slim/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function fatal( $object ) {
*/
protected function log( $object, $level ) {
if ( $this->enabled && $this->writer && $level <= $this->level ) {
return $this->writer->write($object);
return $this->writer->write($object, $level);
} else {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion Slim/LogFileWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ public function __construct( $resource ) {
/**
* Write message
* @param mixed $message
* @param int $level
* @return int|false
*/
public function write( $message ) {
public function write( $message, $level = null ) {
return fwrite($this->resource, (string)$message . PHP_EOL);
}
}
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
</head>
<body>
<header>
<a href="http://www.slimframework.com"><img src="logo.png" alt="Slim"/></a>
<a href="http://www.slimframework.com"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHIAAAA6CAYAAABs1g18AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAABRhJREFUeNrsXY+VsjAMR98twAo6Ao4gI+gIOIKOgCPICDoCjCAjXFdgha+5C3dcv/QfFB5i8h5PD21Bfk3yS9L2VpGnlGW5kS9wJMTHNRxpmjYRy6SycgRvL18OeMQOTYQ8HvIoJKiiz43hgHkq1zvK/h6e/TyJQXeV/VyWBOSHA4C5RvtMAiCc4ZB9FPjgRI8+YuKcrySO515a1hoAY3nc4G2AH52BZsn+MjaAEwIJICKAIR889HljMCcyrR0QE4v/q/BVB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y8L3GvKjE96HpweQoy7iwVQclVutLOEKJxA8gaRCjSzgNI2zhh3bQhzBCQQPIHGaHaUd96GJbZz3Smmjy16u6j3FuKyNxcBarxqWWfYFE0tVVO1Rl3t1Mb05V00MQCJ71YHpNaMcsjWAfkQvPPkaNC7LqTG7JAhGXTKYf+VDeXAX9IvURoAwtTFHvyYIxtnd5tPkywrPafcwbeSuGVwFau3b76NO7SHQrvqhfFE8kM0Wvpv8gVYiYBlxL+fW/34bgP6bIC7JR7YPDubcHCPzIp4+cum7U6NlhZgK7lua3KGLeFwE2m+HblDYWSHG2SAfINuwBBfxbJEIuWZbBH4fAExD7cvaGVyXyH0dhiAYc92z3ZDfUVv+jgb8HrHy7WVO/8BFcy9vuTz+nwADAGnOR39Yg/QkAAAAAElFTkSuQmCC" alt="Slim"/></a>
</header>
<h1>Welcome to Slim!</h1>
<p>
Expand Down
Binary file removed logo.png
Binary file not shown.
86 changes: 86 additions & 0 deletions tests/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,23 @@ public function testPostWithoutInput() {
$req->post('foo');
}

/**
* Test fetch POST params even if multipart/form-data request
*/
public function testPostWithMultipartRequest() {
$_POST = array('foo' => 'bar'); //<-- Set by PHP
$env = Slim_Environment::mock(array(
'REQUEST_METHOD' => 'POST',
'slim.input' => '', //<-- "php://input" is empty for multipart/form-data requests
'CONTENT_TYPE' => 'multipart/form-data',
'CONTENT_LENGTH' => 0
));
$req = new Slim_Http_Request($env);
$this->assertEquals(1, count($req->post()));
$this->assertEquals('bar', $req->post('foo'));
$this->assertNull($req->post('xyz'));
}

/**
* Test fetch PUT params
*/
Expand All @@ -303,9 +320,27 @@ public function testPut() {
$req = new Slim_Http_Request($env);
$this->assertEquals(2, count($req->put()));
$this->assertEquals('bar', $req->put('foo'));
$this->assertEquals('bar', $req->params('foo'));
$this->assertNull($req->put('xyz'));
}

/**
* Test fetch DELETE params
*/
public function testDelete() {
$env = Slim_Environment::mock(array(
'REQUEST_METHOD' => 'DELETE',
'slim.input' => 'foo=bar&abc=123',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'CONTENT_LENGTH' => 15
));
$req = new Slim_Http_Request($env);
$this->assertEquals(2, count($req->delete()));
$this->assertEquals('bar', $req->delete('foo'));
$this->assertEquals('bar', $req->params('foo'));
$this->assertNull($req->delete('xyz'));
}

/**
* Test fetch COOKIE params
*/
Expand Down Expand Up @@ -566,6 +601,18 @@ public function testGetHost() {
$this->assertEquals('slimframework.com', $req->getHost()); //Uses HTTP_HOST if available
}

/**
* Test get host when it has a port number
*/
public function testGetHostAndStripPort() {
$env = Slim_Environment::mock(array(
'SERVER_NAME' => 'slim',
'HOST' => 'slimframework.com:80'
));
$req = new Slim_Http_Request($env);
$this->assertEquals('slimframework.com', $req->getHost()); //Uses HTTP_HOST if available
}

/**
* Test get host
*/
Expand Down Expand Up @@ -593,6 +640,20 @@ public function testGetHostWithPort() {
$this->assertEquals('slimframework.com:80', $req->getHostWithPort());
}

/**
* Test get host with port doesn't dulplicate port numbers
*/
public function testGetHostDoesntDulplicatePort() {
$env = Slim_Environment::mock(array(
'HOST' => 'slimframework.com:80',
'SERVER_NAME' => 'slim',
'SERVER_PORT' => 80,
'slim.url_scheme' => 'http'
));
$req = new Slim_Http_Request($env);
$this->assertEquals('slimframework.com:80', $req->getHostWithPort());
}

/**
* Test get port
*/
Expand Down Expand Up @@ -744,6 +805,31 @@ public function testGetIp() {
$this->assertEquals('127.0.0.1', $req->getIp());
}

/**
* Test get IP with proxy server and Client-Ip header
*/
public function testGetIpWithClientIp() {
$env = Slim_Environment::mock(array(
'REMOTE_ADDR' => '127.0.0.1',
'CLIENT_IP' => '127.0.0.2'
));
$req = new Slim_Http_Request($env);
$this->assertEquals('127.0.0.2', $req->getIp());
}

/**
* Test get IP with proxy server and X-Forwarded-For header
*/
public function testGetIpWithForwardedFor() {
$env = Slim_Environment::mock(array(
'REMOTE_ADDR' => '127.0.0.1',
'CLIENT_IP' => '127.0.0.2',
'X_FORWARDED_FOR' => '127.0.0.3'
));
$req = new Slim_Http_Request($env);
$this->assertEquals('127.0.0.3', $req->getIp());
}

/**
* Test get refererer
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/LogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
require 'Slim/Log.php';

class MyWriter {
public function write( $object ) {
public function write( $object, $level ) {
echo (string)$object;
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Middleware/SessionCookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function testSessionIsPopulatedFromCookie() {
Slim_Environment::mock(array(
'SCRIPT_NAME' => '/index.php',
'PATH_INFO' => '/foo',
'COOKIE' => 'slim_session=1639490378%7CqWbI5R%2Bf%2B%2F1KfHQQ9cANqEEdK5aNhf%2FQy2WX%2FCFOG5Y%3D%7Ce207c55544e1f7889a357ab39700f9cbb3836ea3',
'COOKIE' => 'slim_session=1644004961%7CLKkYPwqKIMvBK7MWl6D%2BxeuhLuMaW4quN%2F512ZAaVIY%3D%7Ce0f007fa852c7101e8224bb529e26be4d0dfbd63',
));
$app = new Slim();
$app->get('/foo', function () {
Expand Down Expand Up @@ -107,4 +107,4 @@ public function testSessionIsPopulatedAsEmptyIfNoCookie() {
$mw->call();
$this->assertEquals(array(), $_SESSION);
}
}
}

0 comments on commit f2444b3

Please sign in to comment.