commit 09929c7b3aea0da23df86fd7adcae50dde415501 Author: Alexey Skobkin Date: Tue Sep 5 04:28:08 2017 +0300 Initial commit. First version added. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ab27d1e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.idea +/vendor \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..24b7826 --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "skobkin/ip-detect", + "license": "MIT", + "type": "project", + "description": "Minimalistic IP remote IP detector application", + "homepage": "http://ip.skobk.in/", + "authors": [ + { + "name": "Alexey Skobkin", + "email": "skobkin-ru@ya.ru", + "homepage": "https://skobk.in/" + } + ], + "autoload": { + "psr-4": { "App\\": "src/" } + }, + "require": { + "symfony/http-foundation": "^3.3", + "slim/php-view": "^2.2" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..6419455 --- /dev/null +++ b/composer.lock @@ -0,0 +1,229 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "content-hash": "eff3b1001b5ae7db5011252de36831ba", + "packages": [ + { + "name": "psr/http-message", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "time": "2016-08-06T14:39:51+00:00" + }, + { + "name": "slim/php-view", + "version": "2.2.0", + "source": { + "type": "git", + "url": "https://github.com/slimphp/PHP-View.git", + "reference": "122ed121a8d9cf91a94020814d2a3ee6c836754f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/slimphp/PHP-View/zipball/122ed121a8d9cf91a94020814d2a3ee6c836754f", + "reference": "122ed121a8d9cf91a94020814d2a3ee6c836754f", + "shasum": "" + }, + "require": { + "psr/http-message": "^1.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8", + "slim/slim": "^3.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Slim\\Views\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Glenn Eggleton", + "email": "geggleto@gmail.com" + } + ], + "description": "Render PHP view scripts into a PSR-7 Response object.", + "keywords": [ + "framework", + "php", + "phtml", + "renderer", + "slim", + "template", + "view" + ], + "time": "2016-10-11T07:43:08+00:00" + }, + { + "name": "symfony/http-foundation", + "version": "v3.3.8", + "source": { + "type": "git", + "url": "https://github.com/symfony/http-foundation.git", + "reference": "14bacad23a4f075bfd3fd456755236cb261320e3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/http-foundation/zipball/14bacad23a4f075bfd3fd456755236cb261320e3", + "reference": "14bacad23a4f075bfd3fd456755236cb261320e3", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8", + "symfony/polyfill-mbstring": "~1.1" + }, + "require-dev": { + "symfony/expression-language": "~2.8|~3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\HttpFoundation\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony HttpFoundation Component", + "homepage": "https://symfony.com", + "time": "2017-08-10T07:07:06+00:00" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.5.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "7c8fae0ac1d216eb54349e6a8baa57d515fe8803" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/7c8fae0ac1d216eb54349e6a8baa57d515fe8803", + "reference": "7c8fae0ac1d216eb54349e6a8baa57d515fe8803", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.5-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "time": "2017-06-14T15:44:48+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/public/index.php b/public/index.php new file mode 100644 index 0000000..d532adb --- /dev/null +++ b/public/index.php @@ -0,0 +1,11 @@ +setStatusCode($e->getCode()); + $response->setContent($e->getMessage()); + } catch (\Throwable $e) { + $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR); + $response->setContent('Server error'); + } + + $response->send(); + } + + public static function handleRaw(Request $request): Response + { + // It's a kind of routing you know... + if ('/json' === $request->getPathInfo()) { + $format = DetectorController::FORMAT_JSON; + } elseif ('/' === $request->getPathInfo()) { + $format = DetectorController::FORMAT_HTML; + } else { + return new Response('Resource not found', Response::HTTP_NOT_FOUND); + } + + return DetectorController::detect($request, $format); + } +} \ No newline at end of file diff --git a/src/Controller/DetectorController.php b/src/Controller/DetectorController.php new file mode 100644 index 0000000..1083d69 --- /dev/null +++ b/src/Controller/DetectorController.php @@ -0,0 +1,33 @@ + $request->getClientIp(), + 'locale' => $request->getLocale(), + 'preferred_language' => $request->getPreferredLanguage(), + ]; + + if (static::FORMAT_JSON === $format) { + return new JsonResponse($clientData); + } + + $templater = new PhpRenderer(static::TEMPLATES_PATH); + $templater->setAttributes($clientData); + + return new Response($templater->fetch('/client_data.php', $clientData)); + } +} \ No newline at end of file diff --git a/templates/client_data.php b/templates/client_data.php new file mode 100644 index 0000000..0d37cbf --- /dev/null +++ b/templates/client_data.php @@ -0,0 +1,17 @@ + + + + + IP address + + + +

getAttribute('ip_address')); ?>

+ +
+locale: getAttribute('locale')); ?>
+
+preferred language: getAttribute('preferred_language')); ?>
+        
+ + \ No newline at end of file