Zend

Zend framework officially provides two types of skeletons: Zend MVC and Zend Expressive.

No matter which skeleton you are using, this guide might give you some ideas on how to implement Shieldon Firewall. Please note that I am not sure which way is considered best practice in Zend, so you can choose the method you prefer.

Firewall in Zend Framework

Installation

Use PHP Composer:

composer require shieldon/shieldon

This will also install dependencies required for Shieldon:

Implementing

Zend Expressive

This is an example that shows you how to use a PSR-15 Middleware in Zend Expressive skeleton.

1. Register a Middleware.

There is a integration class ready for Zend Expressive.

In your pipeline.php, add this line:

Example:

$app->pipe(\Shieldon\Firewall\Integration\ZendPsr15:class);

2. Define a Handler

Let's go to App/src/Handler directory and create a PHP file named FirewallPanelHandler.

Copy the text below and paste it into that file.

Example:

<?php

declare(strict_types=1);

namespace App\Handler;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response;

/**
 * Firewall Panel Handler
 * If you have CSRF enabled, make sure to pass the csrf token to the control panel.
 */
class FirewallPanelHandler implements RequestHandlerInterface
{
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $panel = new \Shieldon\Firewall\Panel();
        $panel->entry();

        return new Response();
    }
}

3. Define a Route for the Firewall Panel

In your route.php, add this line:

Example:


// Begin - Shieldon Firewall

$app->route('/firewall/panel/', App\Handler\FirewallPanelHandler::class, ['GET', 'POST']);

foreach(\Shieldon\Firewall\Panel::getRoutes() as $route) {
    $app->route("/firewall/panel/$route/", App\Handler\FirewallPanelHandler::class, ['GET', 'POST']);
}

// End - Shieldon Firewall

That's it.

Zend MVC

I am not sure which version of the Zend framework you are using, so I will provide instructions that work with most versions of Zend.

1. Before Initializing Core

In your public/index.php under this line:

include __DIR__ . '/../vendor/autoload.php';

Add the following code:

/*
|--------------------------------------------------------------------------
| Run The Shieldon Firewall
|--------------------------------------------------------------------------
|
| Shieldon Firewall will watch all HTTP requests coming to your website.
|
*/
if (isset($_SERVER['REQUEST_URI'])) {

    // This directory must be writable.
    $storage = dirname($_SERVER['SCRIPT_FILENAME']) . '/../shieldon_firewall';

    $firewall = new \Shieldon\Firewall\Firewall();
    $firewall->configure($storage);
    $firewall->controlPanel('/firewall/panel');
    $response = $firewall->run();

    if ($response->getStatusCode() !== 200) {
        $httpResolver = new \Shieldon\Firewall\HttpResolver();
        $httpResolver($response);
    }
}

The next step is to create a controller for the control panel.

2. Define a Controller

Let's create a controller named FirewallController.

<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class FirewallController extends AbstractActionController
{
    /**
     * The entry point of the Firewall Panel.
     */
    public function panelAction()
    {
        $panel = new \Shieldon\Firewall\Panel();
        $panel->entry();
    }
}

3. Define a Route for the Firewall Panel

Open the module.config.php file located at:

module/Application/config/module.config.php

(3-1) Inside the ['router']['routes'] array, add the following code:

Example:

'firewallpanel' => [
    'type' => Segment::class,
    'options' => [
        'route'    => '/firewall/panel[:slug]',
        'constraints' => [
            'slug' => '[a-zA-Z0-9\/]*',
        ],
        'defaults' => [
            'controller' => Controller\FirewallController::class,
            'action'     => 'panel',
        ],
    ],
],

(3-2) Inside the ['controllers']['factories'] array, add the following code:

Controller\FirewallController::class => InvokableFactory::class,

That's it.

Control Panel

You can access the Firewall Panel by visiting /firewall/panel/. To see the page, go to this URL in your browser.

https://yourwebsite.com/firewall/panel

The default login is shieldon_user, and the password is shieldon_pass. After logging into the Firewall Panel, the first thing you should do is change the login and password.

If Shieldon Firewall is enabled in the Daemon setting section, it will start monitoring your website. Make sure you have correctly set up the settings.