From bfea8896def2932a3383d1f527dc2d392b860161 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Sun, 3 Mar 2019 16:16:31 +0100 Subject: [PATCH] Port the old system to a more advanced setup. --- .gitignore | 4 ++ .php_cs | 12 +++++ composer.json | 24 +++++++++ config/config.sample.php | 15 ++++++ grumphp.yml | 6 +++ index.php | 7 --- foobar.css => public/foobar.css | 0 public/index.php | 8 +++ isGesloten.php => public/isGesloten.php | 0 isOpen.php => public/isOpen.php | 0 star.png => public/star.png | Bin public/toggle.php | 31 +++++++++++ src/Configuration.php | 68 ++++++++++++++++++++++++ toggle.php | 26 --------- 14 files changed, 168 insertions(+), 33 deletions(-) create mode 100644 .gitignore create mode 100644 .php_cs create mode 100644 composer.json create mode 100644 config/config.sample.php create mode 100644 grumphp.yml delete mode 100644 index.php rename foobar.css => public/foobar.css (100%) create mode 100644 public/index.php rename isGesloten.php => public/isGesloten.php (100%) rename isOpen.php => public/isOpen.php (100%) rename star.png => public/star.png (100%) create mode 100644 public/toggle.php create mode 100644 src/Configuration.php delete mode 100644 toggle.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..011894d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.lock +vendor +node_modules +.php_cs.cache diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..1e4175b --- /dev/null +++ b/.php_cs @@ -0,0 +1,12 @@ +exclude('vendor') + ->in(__DIR__); + +return PhpCsFixer\Config::create() + ->setUsingCache(true) + ->setRules([ + '@PSR2' => true, + 'single_quote' => true, + ])->setFinder($finder); diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..881afd1 --- /dev/null +++ b/composer.json @@ -0,0 +1,24 @@ +{ + "name": "bert/isdefoobaropen.nl", + "description": "Source code for isdefoobaropen.nl", + "type": "project", + "license": "GPLv3", + "authors": [ + { + "name": "Bert Peters", + "email": "bert@bertptrs.nl" + } + ], + "require": { + "php": "^7.0" + }, + "require-dev": { + "phpro/grumphp": "^0.15.0", + "friendsofphp/php-cs-fixer": "^2.14" + }, + "autoload": { + "psr-4": { + "FooBar\\": "src/" + } + } +} diff --git a/config/config.sample.php b/config/config.sample.php new file mode 100644 index 0000000..960737e --- /dev/null +++ b/config/config.sample.php @@ -0,0 +1,15 @@ + [ + 'mask' => 8, + 'address' => '127.0.0.1', + ], + 'state_file' => __DIR__ . '/open.flag', +]; diff --git a/grumphp.yml b/grumphp.yml new file mode 100644 index 0000000..596a5ef --- /dev/null +++ b/grumphp.yml @@ -0,0 +1,6 @@ +parameters: + git_dir: . + bin_dir: vendor/bin + tasks: + phpcsfixer2: + config: .php_cs \ No newline at end of file diff --git a/index.php b/index.php deleted file mode 100644 index 0e4b7f3..0000000 --- a/index.php +++ /dev/null @@ -1,7 +0,0 @@ - diff --git a/foobar.css b/public/foobar.css similarity index 100% rename from foobar.css rename to public/foobar.css diff --git a/public/index.php b/public/index.php new file mode 100644 index 0000000..a8ec2f8 --- /dev/null +++ b/public/index.php @@ -0,0 +1,8 @@ +isOpen()) { + require 'isOpen.php'; +} else { + require 'isGesloten.php'; +} diff --git a/isGesloten.php b/public/isGesloten.php similarity index 100% rename from isGesloten.php rename to public/isGesloten.php diff --git a/isOpen.php b/public/isOpen.php similarity index 100% rename from isOpen.php rename to public/isOpen.php diff --git a/star.png b/public/star.png similarity index 100% rename from star.png rename to public/star.png diff --git a/public/toggle.php b/public/toggle.php new file mode 100644 index 0000000..6f6d8bb --- /dev/null +++ b/public/toggle.php @@ -0,0 +1,31 @@ +isAuthorized()) { + $file = $configuration->stateFile(); + if ($configuration->isOpen()) { + unlink($file); + } else { + touch($file); + } +} +?> + + + + +Verander de status van de foobar + + +

De foobar is nu +isOpen() ? ' ' : ' niet '; +?> +open.

+
+ +
+ + diff --git a/src/Configuration.php b/src/Configuration.php new file mode 100644 index 0000000..a10434e --- /dev/null +++ b/src/Configuration.php @@ -0,0 +1,68 @@ +address = $config['security']['address']; + $this->mask = $config['security']['mask']; + $this->stateFile = $config['state_file']; + } + + public static function loadConfig(): Configuration + { + $options = [ + dirname(__DIR__) . '/config/config.php', + dirname(__DIR__) . '/config/config.sample.php', + ]; + + foreach ($options as $option) { + if (file_exists($option)) { + return new Configuration(require $option); + } + } + + throw new \RuntimeException('Couldn\'t find config file'); + } + + /** + * Check if the user is authorized to do a toggle. + * + * @param string|null $address An IP-address, or null to default to the clients address. + * @return bool true if the user should be allowed. + */ + public function isAuthorized($address = null): bool + { + $address = ip2long($address ?? $_SERVER['REMOTE_ADDR']); + $allowed = ip2long($this->address); + + if ($this->mask >= 32) { + return $address === $allowed; + } + + $shift = 32 - $this->mask; + + return ($address >> $shift) === ($allowed >> $shift); + } + + public function stateFile(): string + { + return $this->stateFile; + } + + public function isOpen(): bool + { + return file_exists($this->stateFile); + } +} diff --git a/toggle.php b/toggle.php deleted file mode 100644 index b4e2b0f..0000000 --- a/toggle.php +++ /dev/null @@ -1,26 +0,0 @@ - - - - - -Verander de status van de foobar - - -

De foobar is nu - -open.

-
- -
- -