Passed
Push — 2.x ( 958536...e12ed9 )
by Terry
02:44 queued 25s
created

Firewall::setCronJob()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 32
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 32
rs 9.4555
1
<?php
2
/*
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\Firewall;
14
15
use Psr\Http\Message\ServerRequestInterface;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Server\MiddlewareInterface;
18
use Shieldon\Firewall\Kernel;
19
20
21
use Shieldon\Firewall\Middleware as Middleware;
22
23
use Shieldon\Firewall\Utils\Container;
24
use Shieldon\Firewall\Log\ActionLogger;
25
use Shieldon\Firewall\FirewallTrait;
26
use Shieldon\Firewall\MessengerTrait;
0 ignored issues
show
Bug introduced by
The type Shieldon\Firewall\MessengerTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Shieldon\Firewall\Firewall\XssProtectionTrait;
28
use Shieldon\Psr15\RequestHandler;
29
use function Shieldon\Firewall\get_request;
30
31
use RuntimeException;
32
33
use function array_column;
34
use function defined;
35
use function file_exists;
36
use function file_get_contents;
37
use function file_put_contents;
38
use function is_dir;
39
use function json_decode;
40
use function json_encode;
41
use function mkdir;
42
use function rtrim;
43
use function strpos;
44
use function umask;
45
use function time;
46
use function strtotime;
47
use function date;
48
49
/**
50
 * Managed Firewall.
51
 */
52
class Firewall
53
{
54
    use FirewallTrait;
55
    use MainTrait;
0 ignored issues
show
Bug introduced by
The type Shieldon\Firewall\MainTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
    use XssProtectionTrait;
57
    use MessengerTrait;
58
59
    /**
60
     * Collection of PSR-7 or PSR-15 middlewares.
61
     *
62
     * @var array
63
     */
64
    protected $middlewares = [];
65
66
    /**
67
     * Constructor.
68
     */
69
    public function __construct(?ServerRequestInterface $request = null, ?ResponseInterface $response = null)
70
    {
71
        Container::set('firewall', $this);
72
73
        $this->kernel = new Kernel($request, $response);
74
    }
75
76
    /**
77
     * Set up the path of the configuration file.
78
     *
79
     * @param string $source The path.
80
     * @param string $type   The type.
81
     * 
82
     * @return void
83
     */
84
    public function configure(string $source, string $type = 'json')
85
    {
86
        if ($type === 'json') {
87
            $this->directory = rtrim($source, '\\/');
88
            $configFilePath = $this->directory . '/' . $this->filename;
89
90
            if (file_exists($configFilePath)) {
91
                $jsonString = file_get_contents($configFilePath);
92
93
            } else {
94
                $jsonString = file_get_contents(__DIR__ . '/../../config.json');
95
96
                if (defined('PHP_UNIT_TEST')) {
97
                    $jsonString = file_get_contents(__DIR__ . '/../../tests/config.json');
98
                }
99
            }
100
101
            $this->configuration = json_decode($jsonString, true);
102
            $this->kernel->managedBy('managed');
103
104
        } elseif ($type === 'php') {
105
            $this->configuration = require $source;
106
            $this->kernel->managedBy('config');
107
        }
108
109
        $this->setup();
110
    }
111
112
    /**
113
     * Add middlewares and use them before going into Shieldon kernal.
114
     *
115
     * @param MiddlewareInterface $middleware A PSR-15 middlewares.
116
     *
117
     * @return void
118
     */
119
    public function add(MiddlewareInterface $middleware)
120
    {
121
        $this->middlewares[] = $middleware;
122
    }
123
124
    /**
125
     * Setup everything we need.
126
     *
127
     * @return void
128
     */
129
    public function setup(): void
130
    {
131
        $this->status = $this->getOption('daemon');
132
133
        $this->setDriver();
134
135
        $this->setChannel();
136
137
        $this->setIpSource();
138
139
        $this->setLogger();
140
141
        $this->setSessionLimit();
142
143
        $this->setCronJob();
144
145
        $this->setExcludedUrls();
146
147
        $this->setXssProtection();
148
149
        $this->setPageAuthentication();
150
151
        $this->setDialogUserInterface();
152
153
        $this->setMessengers();
154
155
        $this->setMessageEvents();
156
157
        $this->setDenyTooManyAttempts();
158
159
        $this->setIptablesBridgeDirectory();
160
    }
161
162
    /**
163
     * Just, run!
164
     *
165
     * @return ResponseInterface
166
     */
167
    public function run(): ResponseInterface
168
    {
169
        // If settings are ready, let's start monitoring requests.
170
        if ($this->status) {
171
172
            $response = get_request();
173
174
            // PSR-15 request handler.
175
            $requestHandler = new RequestHandler();
176
177
            foreach ($this->middlewares as $middleware) {
178
                $requestHandler->add($middleware);
179
            }
180
181
            $response = $requestHandler->handle($response);
182
183
            // Something is detected by Middlewares, return.
184
            if ($response->getStatusCode() !== 200) {
185
                return $response;
186
            }
187
188
            $result = $this->kernel->run();
189
190
            if ($result !== $this->kernel::RESPONSE_ALLOW) {
191
192
                if ($this->kernel->captchaResponse()) {
193
                    $this->kernel->unban();
194
195
                    $response = $response->withHeader('Location', $this->kernel->getCurrentUrl());
196
                    $response = $response->withStatus(303);
197
198
                    return $response;
199
                }
200
            }
201
        }
202
203
        return $this->kernel->respond();
204
    }
205
}
206