1. Go to this page and download the library: Download kanopi/firewall library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kanopi / firewall example snippets
// Include composer autoloader if not already loaded
'\Kanopi\Firewall\Firewall')) {
\Kanopi\Firewall\Firewall::create([__DIR__ . '/config/firewall.yml'])->evaluate();
}
// Load composer autoloader if not already loaded
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
/firewall.yml';
if (file_exists($firewall_config)) {
\Kanopi\Firewall\Firewall::create([$firewall_config])->evaluate();
}
}
// Firewall integration
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
ig = __DIR__ . '/firewall/config.yml';
if (file_exists($firewall_config)) {
\Kanopi\Firewall\Firewall::create([$firewall_config])->evaluate();
}
}
}
use App\Kernel;
use Kanopi\Firewall\Firewall;
tialize firewall
if (class_exists(Firewall::class)) {
$configPath = dirname(__DIR__) . '/config/firewall.yml';
if (file_exists($configPath)) {
Firewall::create([$configPath])->evaluate();
}
}
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};
// Firewall integration
if (class_exists('\Kanopi\Firewall\Firewall')) {
$firewall_config = __DIR__ . '/../config/firewall.yml';
if (file_exists($firewall_config)) {
\Kanopi\Firewall\Firewall::create([$firewall_config])->evaluate();
}
}
$app =
namespace App\Security\Firewall\Plugins;
use Kanopi\Firewall\Plugins\AbstractPluginBase;
use Symfony\Component\HttpFoundation\Request;
class ApiKeyValidator extends AbstractPluginBase
{
private array $validApiKeys;
public function __construct(array $metadata = [], array $config = [])
{
parent::__construct($metadata, $config);
// Load API keys from configuration or database
$this->validApiKeys = $metadata['api_keys'] ?? [];
}
public function getName(): string
{
return 'API Key Validator';
}
public function getDescription(): string
{
return 'Validates API keys for authenticated endpoints';
}
public function evaluate(Request $request): bool
{
// Only check API endpoints
if (!str_starts_with($request->getPathInfo(), '/api/')) {
return false;
}
// Check for API key in header or query
$apiKey = $request->headers->get('X-API-Key')
?? $request->query->get('api_key');
if (!$apiKey) {
$this->logger?->warning('Missing API key', [
'ip' => $request->getClientIp(),
'path' => $request->getPathInfo(),
]);
return true; // Block request
}
if (!in_array($apiKey, $this->validApiKeys, true)) {
$this->logger?->warning('Invalid API key', [
'ip' => $request->getClientIp(),
'api_key' => substr($apiKey, 0, 8) . '...',
]);
return true; // Block request
}
return false; // Allow request
}
public function getStatusCode(): int
{
return 401; // Unauthorized
}
}
use PHPUnit\Framework\TestCase;
use Kanopi\Firewall\Firewall;
use Symfony\Component\HttpFoundation\Request;
class FirewallTest extends TestCase
{
public function testBlocksMaliciousIp(): void
{
$config = [
'storage' => [
'type' => 'Kanopi\Firewall\Storage\InMemoryStorage'
],
'plugins' => [
[
'plugin' => 'Kanopi\Firewall\Plugins\IpAddress',
'response' => 'block',
'enable' => true,
'config' => ['192.168.1.100'],
],
],
];
$firewall = Firewall::create([$config]);
// Create a request from the blocked IP
$request = Request::create('/', 'GET', [], [], [], [
'REMOTE_ADDR' => '192.168.1.100'
]);
// The firewall should block this request
$this->expectException(\Exception::class);
$firewall->evaluate($request);
}
}
bash
php -S localhost:8000
bash
echo "" > firewall.data
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.