Skip to content

Commit 0e5d5ce

Browse files
Merge pull request #107 from julienloizelet/feat/twig-templating
Feat/twig templating
2 parents e403b5a + 0eaa601 commit 0e5d5ce

16 files changed

+288
-238
lines changed

.github/workflows/test-suite.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333

3434
- name: Install DDEV
3535
env:
36-
DDEV_VERSION: v1.20.0
36+
DDEV_VERSION: v1.21.1
3737
run: |
3838
# @see https://ddev.readthedocs.io/en/stable/#installationupgrade-script-linux-and-macos-armarm64-and-amd64-architectures
3939
sudo apt-get -qq update

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.31.0](https://github.com/crowdsecurity/php-cs-bouncer/releases/tag/v0.31.0) - 2022-09-23
8+
[_Compare with previous release_](https://github.com/crowdsecurity/php-cs-bouncer/compare/v0.30.0...v0.31.0)
9+
10+
### Changed
11+
- Use Twig as template engine for ban and captcha walls
12+
13+
---
714

815
## [0.30.0](https://github.com/crowdsecurity/php-cs-bouncer/releases/tag/v0.30.0) - 2022-09-22
916
[_Compare with previous release_](https://github.com/crowdsecurity/php-cs-bouncer/compare/v0.29.0...v0.30.0)

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@
3939
}
4040
],
4141
"require": {
42-
"php": "^7.2 || ^8.0",
42+
"php": ">=7.2.5",
4343
"symfony/config": "^4.4.27 || ^5.2 || ^6.0",
4444
"symfony/cache": "^5.4.11 || ^6.0.11",
45+
"twig/twig": "^3.4.2",
4546
"monolog/monolog": "^1.17 || ^2.1",
4647
"gregwar/captcha": "^1.1",
4748
"mlocati/ip-lib": "^1.18",

docs/DEVELOPER.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ For a quick start, follow the below steps.
7272

7373
#### DDEV installation
7474

75-
This project is fully compatible with DDEV 1.20.0, and it is recommended to use this specific version.
75+
This project is fully compatible with DDEV 1.21.1, and it is recommended to use this specific version.
7676
For the DDEV installation, please follow the [official instructions](https://ddev.readthedocs.io/en/stable/#installation).
7777
On a Linux distribution, you can run:
7878
```
7979
sudo apt-get -qq update
8080
sudo apt-get -qq -y install libnss3-tools
8181
curl -LO https://raw.githubusercontent.com/drud/ddev/master/scripts/install_ddev.sh
82-
bash install_ddev.sh v1.20.0
82+
bash install_ddev.sh v1.21.1
8383
rm install_ddev.sh
8484
```
8585

src/Bouncer.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace CrowdSecBouncer;
44

5-
require_once __DIR__ . '/templates/captcha.php';
6-
require_once __DIR__ . '/templates/access-forbidden.php';
7-
85
use CrowdSecBouncer\Fixes\Gregwar\Captcha\CaptchaBuilder;
96
use CrowdSecBouncer\RestClient\AbstractClient;
107
use ErrorException;
@@ -154,7 +151,7 @@ public function getRemediationForIp(string $ip): string
154151
}
155152

156153
/**
157-
* Returns a default "CrowdSec 403" HTML template to display to a web browser using a banned IP.
154+
* Returns a default "CrowdSec 403" HTML template.
158155
* The input $config should match the TemplateConfiguration input format.
159156
*
160157
* @param array $config An array of template configuration parameters
@@ -167,16 +164,20 @@ public static function getAccessForbiddenHtmlTemplate(array $config): string
167164
$configuration = new TemplateConfiguration();
168165
$processor = new Processor();
169166
$config = $processor->processConfiguration($configuration, [$config]);
167+
$template = new Template('ban.html.twig');
170168

171-
ob_start();
172-
displayAccessForbiddenTemplate($config);
173-
174-
return ob_get_clean();
169+
return $template->render($config);
175170
}
176171

177172
/**
178-
* Returns a default "CrowdSec Captcha" HTML template to display to a web browser using a captchable IP.
173+
* Returns a default "CrowdSec Captcha (401)" HTML template.
179174
* The input $config should match the TemplateConfiguration input format.
175+
*
176+
* @param bool $error
177+
* @param string $captchaImageSrc
178+
* @param string $captchaResolutionFormUrl
179+
* @param array $config
180+
* @return string
180181
*/
181182
public static function getCaptchaHtmlTemplate(
182183
bool $error,
@@ -188,11 +189,16 @@ public static function getCaptchaHtmlTemplate(
188189
$configuration = new TemplateConfiguration();
189190
$processor = new Processor();
190191
$config = $processor->processConfiguration($configuration, [$config]);
191-
192-
ob_start();
193-
displayCaptchaTemplate($error, $captchaImageSrc, $captchaResolutionFormUrl, $config);
194-
195-
return ob_get_clean();
192+
$template = new Template('captcha.html.twig');
193+
194+
return $template->render(array_merge(
195+
$config,
196+
[
197+
'error' => $error,
198+
'captcha_img' => $captchaImageSrc,
199+
'captcha_resolution_url' => $captchaResolutionFormUrl
200+
]
201+
));
196202
}
197203

198204
/**

src/Constants.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Constants
2020
public const DEFAULT_LAPI_URL = 'http://localhost:8080';
2121

2222
/** @var string The last version of this library */
23-
public const VERSION = 'v0.30.0';
23+
public const VERSION = 'v0.31.0';
2424

2525
/** @var string The user agent used to send request to LAPI */
2626
public const BASE_USER_AGENT = 'PHP CrowdSec Bouncer/' . self::VERSION;
@@ -105,4 +105,7 @@ class Constants
105105

106106
/** @var string The "disabled" x-forwarded-for setting */
107107
public const X_FORWARDED_DISABLED = 'no_forward';
108+
109+
/** @var string Path for html templates folder (e.g. ban and captcha wall) */
110+
public const TEMPLATES_DIR = __DIR__ . "/templates";
108111
}

src/Template.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace CrowdSecBouncer;
4+
5+
use Twig\Error\LoaderError;
6+
use Twig\Error\RuntimeError;
7+
use Twig\Error\SyntaxError;
8+
use Twig\Loader\FilesystemLoader;
9+
use Twig\Environment;
10+
use Twig\TemplateWrapper;
11+
12+
/**
13+
* The template engine.
14+
*
15+
* @author CrowdSec team
16+
*
17+
* @see https://crowdsec.net CrowdSec Official Website
18+
*
19+
* @copyright Copyright (c) 2020+ CrowdSec
20+
* @license MIT License
21+
*/
22+
class Template
23+
{
24+
/** @var TemplateWrapper */
25+
private $template;
26+
27+
/**
28+
* @param string $path
29+
* @param string $templatesDir
30+
* @param array $options
31+
* @throws LoaderError
32+
* @throws RuntimeError
33+
* @throws SyntaxError
34+
*/
35+
public function __construct(string $path, string $templatesDir = Constants::TEMPLATES_DIR, array $options = [])
36+
{
37+
$loader = new FilesystemLoader($templatesDir);
38+
$env = new Environment($loader, $options);
39+
$this->template = $env->load($path);
40+
}
41+
42+
public function render(array $config = []): string
43+
{
44+
return $this->template->render(['config' => $config]);
45+
}
46+
}

src/templates/_base.php

Lines changed: 0 additions & 148 deletions
This file was deleted.

src/templates/access-forbidden.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/templates/ban.html.twig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
{% include('partials/head.html.twig') with {'tab_title': config.text.ban_wall.tab_title} %}
5+
</head>
6+
<body>
7+
<div class="container">
8+
<div class="main">
9+
<h1>{{ config.text.ban_wall.title | e }}</h1>
10+
<p class="desc">{{ config.text.ban_wall.subtitle }}</p>
11+
{% if config.text.ban_wall.footer is not empty %}
12+
<p class="footer">{{ config.text.ban_wall.footer | e }}</p>
13+
{% endif %}
14+
{% if config.hide_crowdsec_mentions is empty %}
15+
{% include('partials/mentions.html.twig') %}
16+
{% endif %}
17+
</div>
18+
</div>
19+
</body>
20+
</html>

0 commit comments

Comments
 (0)