diff --git a/core.php b/core.php index 807ae72..6eccbe5 100644 --- a/core.php +++ b/core.php @@ -1,138 +1,175 @@ block * * @param mixed $variable the variable to dump */ function dprint_r ($variable) { echo '
'; print_r($variable); echo ''; } /** * Prints human-readable information about a variable, wrapped in a
block * then dies * * @param mixed $variable the variable to dump */ function dieprint_r ($variable) { dprint_r($variable); die; }; /// /// Client information /// /** * Returns the full header or the IP part of it * * @param string $value The header value * @return string the IP part */ function extract_client_ip_from_header ($value) { if (strpos($value, ',') !== false) { //Header contains 'clientIP, proxyIP, anotherProxyIP' //The first value is so the one to return. //See draft-ietf-appsawg-http-forwarded-10. $ips = explode(',', $value, 2); return trim($ips[0]); } return $value; } /** * Gets remote IP address. * * This is intended as a drop-in replacement for $_SERVER['REMOTE_ADDR'], * which takes in consideration proxy values. */ function get_remote_addr () { $candidates = [ //Standard header provided by draft-ietf-appsawg-http-forwarded-10 'HTTP_X_FORWARDED_FOR', //Legacy headers 'HTTP_CLIENT_IP', 'HTTP_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_X_FORWARDED', //Default header if no proxy information could be detected 'REMOTE_ADDR', ]; foreach ($candidates as $candidate) { if (array_key_exists($candidate, $_SERVER)) { return extract_client_ip_from_header($_SERVER[$candidate]); } } return ''; } diff --git a/tests/CoreTest.php b/tests/CoreTest.php index dcef8b6..47e9fe0 100644 --- a/tests/CoreTest.php +++ b/tests/CoreTest.php @@ -1,81 +1,98 @@ assertEquals('àèòàFOOàèòà', mb_str_pad("FOO", 11, "àèò", STR_PAD_BOTH, "UTF-8")); $this->assertEquals('àèòFOOàèòà', mb_str_pad("FOO", 10, "àèò", STR_PAD_BOTH, "UTF-8")); $this->assertEquals('àèòBAAZàèòà', mb_str_pad("BAAZ", 11, "àèò", STR_PAD_BOTH, "UTF-8")); $this->assertEquals('àèòBAAZàèò', mb_str_pad("BAAZ", 10, "àèò", STR_PAD_BOTH, "UTF-8")); $this->assertEquals('FOOBAR', mb_str_pad("FOOBAR", 6, "àèò", STR_PAD_BOTH, "UTF-8")); $this->assertEquals('FOOBAR', mb_str_pad("FOOBAR", 1, "àèò", STR_PAD_BOTH, "UTF-8")); $this->assertEquals('FOOBAR', mb_str_pad("FOOBAR", 0, "àèò", STR_PAD_BOTH, "UTF-8")); $this->assertEquals('FOOBAR', mb_str_pad("FOOBAR", -10, "àèò", STR_PAD_BOTH, "UTF-8")); $this->assertEquals('àèòàèòàèFOO', mb_str_pad("FOO", 11, "àèò", STR_PAD_LEFT, "UTF-8")); $this->assertEquals('àèòàèòàFOO', mb_str_pad("FOO", 10, "àèò", STR_PAD_LEFT, "UTF-8")); $this->assertEquals('àèòàèòàBAAZ', mb_str_pad("BAAZ", 11, "àèò", STR_PAD_LEFT, "UTF-8")); $this->assertEquals('àèòàèòBAAZ', mb_str_pad("BAAZ", 10, "àèò", STR_PAD_LEFT, "UTF-8")); $this->assertEquals('FOOBAR', mb_str_pad("FOOBAR", 6, "àèò", STR_PAD_LEFT, "UTF-8")); $this->assertEquals('FOOBAR', mb_str_pad("FOOBAR", 1, "àèò", STR_PAD_LEFT, "UTF-8")); $this->assertEquals('FOOBAR', mb_str_pad("FOOBAR", 0, "àèò", STR_PAD_LEFT, "UTF-8")); $this->assertEquals('FOOBAR', mb_str_pad("FOOBAR", -10, "àèò", STR_PAD_LEFT, "UTF-8")); $this->assertEquals('FOOàèòàèòàè', mb_str_pad("FOO", 11, "àèò", STR_PAD_RIGHT, "UTF-8")); $this->assertEquals('FOOàèòàèòà', mb_str_pad("FOO", 10, "àèò", STR_PAD_RIGHT, "UTF-8")); $this->assertEquals('BAAZàèòàèòà', mb_str_pad("BAAZ", 11, "àèò", STR_PAD_RIGHT, "UTF-8")); $this->assertEquals('BAAZàèòàèò', mb_str_pad("BAAZ", 10, "àèò", STR_PAD_RIGHT, "UTF-8")); $this->assertEquals('FOOBAR', mb_str_pad("FOOBAR", 6, "àèò", STR_PAD_RIGHT, "UTF-8")); $this->assertEquals('FOOBAR', mb_str_pad("FOOBAR", 1, "àèò", STR_PAD_RIGHT, "UTF-8")); $this->assertEquals('FOOBAR', mb_str_pad("FOOBAR", 0, "àèò", STR_PAD_RIGHT, "UTF-8")); $this->assertEquals('FOOBAR', mb_str_pad("FOOBAR", -10, "àèò", STR_PAD_RIGHT, "UTF-8")); } + /// + /// Identifiers + /// + + function test_uuid () { + $uuid = uuid(); + $this->assertEquals(36, strlen($uuid)); + for ($i = 0 ; $i < 36 ; $i++) { + if ($i == 8 | $i == 13 || $i == 18 || $i == 23) { + $this->assertEquals("-", $uuid[$i], "Dash were expected."); + continue; + } + + $this->assertRegExp('/[0-9a-f]/', $uuid[$i], "Lowercase hexadecimal digit were expected."); + } + } + /// /// Client information /// function test_extract_client_ip_from_header () { $values = [ //Each value should return 10.0.0.3 '10.0.0.3', '10.0.0.3,10.0.0.4', '10.0.0.3, 10.0.0.4', '10.0.0.3, 10.0.0.4, lorem ipsum dolor', ]; foreach ($values as $value) { $this->assertEquals( '10.0.0.3', extract_client_ip_from_header($value) ); } $this->assertEmpty( extract_client_ip_from_header('') ); } function test_get_remote_addr () { $this->assertEmpty(get_remote_addr()); $_SERVER = [ 'REMOTE_ADDR' => '10.0.0.2' ]; $this->assertEquals('10.0.0.2', get_remote_addr()); $_SERVER += [ 'HTTP_X_FORWARDED_FOR' => '10.0.0.3', 'HTTP_CLIENT_IP' => '10.0.0.4', ]; $this->assertEquals('10.0.0.3', get_remote_addr(), "HTTP_X_FORWARDED_FOR must be prioritized."); } }