Commit 03b93908 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Refactorisation et ajout d'options au MaintenanceListener: 'message' (message...

Refactorisation et ajout d'options au MaintenanceListener: 'message' (message à afficher) et 'include_cli' (mode console aussi concerné?).
parent 1489f9d8
Loading
Loading
Loading
Loading
Loading
+85 −46
Changes for src/UnicaenApp/Mvc/Listener/MaintenanceListener.php: 85 added lines, 46 removed lines.
Original line number Diff line number Diff line
@@ -16,19 +16,28 @@ class MaintenanceListener implements ListenerAggregateInterface
    /**
     * @var array
     */
    protected $config = [
        // activation (true: activé, false: désactivé)
    private $defaultConfig = [
        // activation (TRUE: activé, FALSE: désactivé)
        'enable' => false,
        // message à afficher
        'message' => "L'application est temporairement indisponible pour des raisons de maintenance, veuillez nous excuser pour la gêne occasionnée.",
        // le mode console est-il aussi concerné (TRUE: oui, FALSE: non)
        'include_cli' => false,
        // liste blanche des adresses IP clientes à laisser passer
        'white_list' => [
            // Formats possibles : [ REMOTE_ADDR ] ou [ REMOTE_ADDR, HTTP_X_FORWARDED_FOR ]
            // Exemples :
            // exemples :
            // ['127.0.0.1'], // localhost
            // ['172.17.0.1'], // Docker container
            // ['195.220.135.97', '194.199.107.33'], // Via proxy
        ],
    ];

    /**
     * @var array
     */
    private $config = [];

    /**
     * MaintenanceListener constructor.
     *
@@ -36,60 +45,40 @@ class MaintenanceListener implements ListenerAggregateInterface
     */
    public function __construct(array $config = [])
    {
        $this->config = ArrayUtils::merge($this->config, $config);
        $this->config = ArrayUtils::merge($this->defaultConfig, $config);
    }

    /**
     * @return bool
     * Attach one or more listeners
     *
     * Implementors may add an optional $priority argument; the EventManager
     * implementation will pass this to the aggregate.
     *
     * @param EventManagerInterface $events
     */
    protected function isMaintenanceMode()
    public function attach(EventManagerInterface $events)
    {
        if (!array_key_exists('enable', $this->config)) {
            return false;
        }

        $enable = in_array($this->config['enable'], [1, true, '1']);
        if (! $enable) {
            return false;
        }

        if (! array_key_exists('white_list', $this->config)) {
            return true;
        }

        $whiteList = $this->config['white_list'];
        $passed = false;
        foreach ($whiteList as $ip) {
            $passed = $ip[0] === $_SERVER['REMOTE_ADDR'];
            if ($passed && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $passed = isset($ip[1]) && $ip[1] === $_SERVER['HTTP_X_FORWARDED_FOR'];
            }
            if ($passed) break;
        }
        if ($passed) {
            return false;
        }

        return true;
        $events->attach(MvcEvent::EVENT_ROUTE, [$this, 'beforeRoute']);
        $events->attach(MvcEvent::EVENT_RENDER, [$this, 'beforeRendering']);
    }

    /**
     * @param MvcEvent $e
     */
    public function intercept(MvcEvent $e)
    public function beforeRoute(MvcEvent $e)
    {
        if (! $this->isMaintenanceMode()) {
        if (! $this->isMaintenanceEnabled()) {
            return;
        }
        if ($this->isMaintenanceBypassed()) {
            return;
        }

        // mode maintenance en mode console = exit
        if (php_sapi_name() === 'cli') {
            exit(0);
        }

//        if (in_array($e->getRouteMatch()->getMatchedRouteName(), ['apropos','contact','plan','mentions-legales','il'])) {
//            return;
//        }

        $routeMatch = new RouteMatch([
            'controller' => 'UnicaenApp\Controller\Application',
            'action'     => 'maintenance',
@@ -100,17 +89,67 @@ class MaintenanceListener implements ListenerAggregateInterface
    }

    /**
     * Attach one or more listeners
     * @param MvcEvent $e
     */
    public function beforeRendering(MvcEvent $e)
    {
        if (! $this->isMaintenanceEnabled()) {
            return;
        }

        $e->getViewModel()->setVariable('message', $this->config['message']);
    }

    /**
     * Indique si le mode maintenance est activé dans la config.
     *
     * Implementors may add an optional $priority argument; the EventManager
     * implementation will pass this to the aggregate.
     * @return bool
     */
    private function isMaintenanceEnabled()
    {
        return in_array(
            $this->config['enable'],
            [1, true, '1']
        );
    }

    /**
     * Détermine si le mode maintenance doit être outrepassé.
     *
     * @param EventManagerInterface $events
     * @return bool
     */
    private function isMaintenanceBypassed()
    {
        // traitement particulier pour le mode console.
        if (php_sapi_name() === 'cli') {
            return ! $this->isCliIncluded();
        }

        $whiteList = $this->config['white_list'];

        foreach ($whiteList as $ip) {
            $bypassed = $ip[0] === $_SERVER['REMOTE_ADDR'];
            if ($bypassed && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $bypassed = isset($ip[1]) && $ip[1] === $_SERVER['HTTP_X_FORWARDED_FOR'];
            }
            if ($bypassed) {
                return true;
            }
        }

        return false;
    }

    /**
     * Indique si le mode console est aussi concerné.
     *
     * @return void
     * @return bool
     */
    public function attach(EventManagerInterface $events)
    private function isCliIncluded()
    {
        $events->attach(MvcEvent::EVENT_ROUTE, [$this, 'intercept']);
        return in_array(
            $this->config['include_cli'],
            [1, true, '1']
        );
    }
}
 No newline at end of file
+1 −2
Changes for view/layout/maintenance.phtml: 1 added line, 2 removed lines.
Original line number Diff line number Diff line
@@ -40,8 +40,7 @@
            <tr class="text-danger" style="font-size: 150%; line-height: 100%">
                <td><span class="glyphicon glyphicon-exclamation-sign" style="font-size: 200%; vertical-align: -40%; margin-right: 10px;"></span></td>
                <td>
                    <span style="font-weight: 500;">L'application est temporairement indisponible.</span><br>
                    <span style="font-size: 80%">Veuillez nous excuser pour la gêne occasionnée.</span>
                    <span style="font-weight: 500;"><?php echo $message ?></span>
                </td>
            </tr>
        </table>