Port the old system to a more advanced setup.
This commit is contained in:
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
*.lock
|
||||
vendor
|
||||
node_modules
|
||||
.php_cs.cache
|
||||
12
.php_cs
Normal file
12
.php_cs
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
$finder = PhpCsFixer\Finder::create()
|
||||
->exclude('vendor')
|
||||
->in(__DIR__);
|
||||
|
||||
return PhpCsFixer\Config::create()
|
||||
->setUsingCache(true)
|
||||
->setRules([
|
||||
'@PSR2' => true,
|
||||
'single_quote' => true,
|
||||
])->setFinder($finder);
|
||||
24
composer.json
Normal file
24
composer.json
Normal file
@@ -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/"
|
||||
}
|
||||
}
|
||||
}
|
||||
15
config/config.sample.php
Normal file
15
config/config.sample.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
/**
|
||||
* The security works by checking if the request is made from an authorized IP-address.
|
||||
*
|
||||
* The address is configured as an IP-address and a subnet mask. In order to verify the entire IP-address, specify
|
||||
* 32 for the mask. This option only supports IPv4 for obvious reasons.
|
||||
*/
|
||||
'security' => [
|
||||
'mask' => 8,
|
||||
'address' => '127.0.0.1',
|
||||
],
|
||||
'state_file' => __DIR__ . '/open.flag',
|
||||
];
|
||||
6
grumphp.yml
Normal file
6
grumphp.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
parameters:
|
||||
git_dir: .
|
||||
bin_dir: vendor/bin
|
||||
tasks:
|
||||
phpcsfixer2:
|
||||
config: .php_cs
|
||||
@@ -1,7 +0,0 @@
|
||||
<?php
|
||||
if(file_exists("open.flag")) {
|
||||
require "isOpen.php";
|
||||
} else {
|
||||
require "isGesloten.php";
|
||||
}
|
||||
?>
|
||||
8
public/index.php
Normal file
8
public/index.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
require_once '../vendor/autoload.php';
|
||||
|
||||
if (\FooBar\Configuration::loadConfig()->isOpen()) {
|
||||
require 'isOpen.php';
|
||||
} else {
|
||||
require 'isGesloten.php';
|
||||
}
|
||||
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 24 KiB |
31
public/toggle.php
Normal file
31
public/toggle.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
require_once '../vendor/autoload.php';
|
||||
|
||||
$configuration = \FooBar\Configuration::loadConfig();
|
||||
|
||||
if (isset($_POST['toggle']) && $configuration->isAuthorized()) {
|
||||
$file = $configuration->stateFile();
|
||||
if ($configuration->isOpen()) {
|
||||
unlink($file);
|
||||
} else {
|
||||
touch($file);
|
||||
}
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Verander de status van de foobar</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>De foobar is nu
|
||||
<?php
|
||||
echo $configuration->isOpen() ? ' ' : ' niet ';
|
||||
?>
|
||||
open.</h1>
|
||||
<form method="post">
|
||||
<input type="submit" name="toggle" value="Verander dit">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
68
src/Configuration.php
Normal file
68
src/Configuration.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace FooBar;
|
||||
|
||||
/**
|
||||
* Main configuration class.
|
||||
*
|
||||
* Reads the app configuration from an array (or optionally a file via loadConfig) and provide easy access.
|
||||
*/
|
||||
class Configuration
|
||||
{
|
||||
private $address;
|
||||
private $mask;
|
||||
private $stateFile;
|
||||
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
26
toggle.php
26
toggle.php
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
if(isset($_POST['toggle'])) {
|
||||
if(file_exists("open.flag")) {
|
||||
unlink("open.flag");
|
||||
} else {
|
||||
touch("open.flag");
|
||||
}
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Verander de status van de foobar</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>De foobar is nu
|
||||
<?php
|
||||
echo file_exists("open.flag") ? " " : " niet ";
|
||||
?>
|
||||
open.</h1>
|
||||
<form method="post">
|
||||
<input type="submit" name="toggle" value="Verander dit">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user