From e816c5a4143f316a9a2a769cb0fed74bcc30e200 Mon Sep 17 00:00:00 2001 From: paramtamtam Date: Sun, 6 Aug 2017 19:01:28 +0500 Subject: [PATCH] Fix domain validation bug + add one feature test --- README.md | 2 ++ app/Services/HostsParser/HostsParser.php | 40 +++++++++++++++--------- composer.json | 10 ++++-- config/app.php | 2 +- nginx.conf.sample | 26 +++++++++++++++ phpunit.xml | 2 +- tests/ExampleTest.php | 19 +++++++++-- 7 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 nginx.conf.sample diff --git a/README.md b/README.md index 9a988f33..4335400e 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ Более подробно о нем можно прочитать по [этой ссылке (хабр)][habr]. +Крайне рекомендую использовать `php` версии 7 и выше по соображениям производительности. + ### Установка Для развертывания приложения достаточно выполнить в терминале: diff --git a/app/Services/HostsParser/HostsParser.php b/app/Services/HostsParser/HostsParser.php index fd0f6549..081eb5ab 100644 --- a/app/Services/HostsParser/HostsParser.php +++ b/app/Services/HostsParser/HostsParser.php @@ -170,16 +170,28 @@ public function addExcludedHosts($hosts_names) */ public function isValidHostname($hostname) { - //static $regexp = '((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*' - // . '([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]))'; - if (is_string($hostname) && ! empty($hostname)) { - if (mb_strpos($hostname, ' ') === false) { // It's faster - //if ((bool) preg_match('/^' . $regexp . '$/', $hostname)) { - return true; + static $regexp = '((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*' + . '([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]))'; + + static $stack = []; + + $hostname = (string) $hostname; + + if (isset($stack[$hostname])) { + return $stack[$hostname]; + } else { + if (is_string($hostname) && ! empty($hostname)) { + if ((bool) preg_match('/^' . $regexp . '$/', $hostname)) { + $stack[$hostname] = true; + + return $stack[$hostname]; + } } + + $stack[$hostname] = false; } - return false; + return $stack[$hostname]; } /** @@ -408,14 +420,14 @@ public function render($limit = 0, $entry_comment = 'ADBlock', $format = 'router $entry_comment = str_replace(' ', '', $entry_comment); foreach ($hosts as $host) { - // $result .= sprintf( -// "add address=%s name=%s comment=%s\n", -// $this->redirect_to, -// $host, -// $entry_comment -// ); + $result .= sprintf( + "add address=%s name=%s comment=%s\n", + $this->redirect_to, + $host, + $entry_comment + ); // It's faster: - $result .= 'add address=' . $this->redirect_to . ' name=' . $host . ' comment=' . $entry_comment . "\n"; + //$result .= 'add address=' . $this->redirect_to . ' name=' . $host . ' comment=' . $entry_comment . "\n"; } } diff --git a/composer.json b/composer.json index 28bcf50d..139b9e31 100644 --- a/composer.json +++ b/composer.json @@ -28,9 +28,13 @@ ] }, "scripts": { - "post-root-package-install": [ - "php -r \"copy('.env.example', '.env');\"" - ] + "post-install-cmd": [ + "php -r 'list($s, $t) = [\"./.env.example\", \"./.env\"]; file_exists($t) || copy($s, $t);'" + ], + "post-update-cmd": [ + "php -r 'list($s, $t) = [\"./.env.example\", \"./.env\"]; file_exists($t) || copy($s, $t);'" + ], + "test": "./vendor/bin/phpunit --no-coverage" }, "minimum-stability": "dev", "prefer-stable": true diff --git a/config/app.php b/config/app.php index f40d99dd..22504db1 100644 --- a/config/app.php +++ b/config/app.php @@ -3,7 +3,7 @@ return [ // Версия - 'version' => '2.0.1', + 'version' => '2.0.2', /* |-------------------------------------------------------------------------- diff --git a/nginx.conf.sample b/nginx.conf.sample new file mode 100644 index 00000000..d920c897 --- /dev/null +++ b/nginx.conf.sample @@ -0,0 +1,26 @@ +server { + listen 80; + server_name your.domain.com; + root /path_to/mikrotik-hosts-parser/public; + access_log off; + error_log off; + + index index.php index.html index.htm; + + location / { + try_files $uri $uri/ /index.php$is_args$args; + } + + location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { + access_log off; + log_not_found off; + expires 1M; + } + + location ~ \.php$ { + include fastcgi_params; + fastcgi_pass php; + } + + location ~ /\.ht {return 444;} +} \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index c703297e..4f608f28 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -16,7 +16,7 @@ - ./app + ./app diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index 998f963c..e113974e 100644 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -4,14 +4,29 @@ class ExampleTest extends TestCase { /** - * A basic test example. + * Test of index page. * * @return void */ - public function testExample() + public function testIndexPage() { $response = $this->get('/'); $response->assertResponseStatus(200); } + + /** + * Test of script source page. + * + * @return void + */ + public function testScriptSourcePage() + { + $response = $this->get('/script/source?format=routeros&version=2.0.2&redirect_to=127.0.0.2&limit=666&sources_urls=https%3A%2F%2Fcdn.rawgit.com%2Ftarampampam%2Fstatic%2Fmaster%2Fhosts%2Fblock_shit.txt&excluded_hosts=localhost'); + + $response->assertResponseOk(); + $this->assertContains('Script generated', $response->response->getContent()); + + $response->assertResponseStatus(200); + } }