Passed
Push — 2.x ( 3b7a15...f6a0a4 )
by Terry
02:08
created

Bootstrap::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 0
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
12
declare(strict_types=1);
13
14
namespace Shieldon\Firewall\Integration;
15
16
use Shieldon\Firewall\Container;
17
use Shieldon\Firewall\Firewall;
18
use Shieldon\Firewall\Panel;
19
use Shieldon\Firewall\HttpResolver;
20
use function dirname;
21
use function strpos;
22
23
/**
24
 * The easist way to implement Shieldon Firewall in your PHP project.
25
 * 
26
 * [How to use]
27
 * 
28
 * This class is supposed to be used in a very early stage of your code.
29
 * The position is right after Composer autoloader.
30
 * 
31
 * [Example]
32
 *
33
 *     require_once '../vendor/autoload.php';
34
 *
35
 *     $shieldon = new \Shieldon\Firewall\Intergration\Bootstrap();
36
 *     $shieldon->run();
37
 * 
38
 * [Note]
39
 * 
40
 * If you use this approach on a PHP framework, make sure that the route
41
 * supports POST method, otherwise the CAPTCHA form will not work.
42
 */
43
class Bootstrap
44
{
45
    /**
46
     * Constuctor.
47
     *
48
     * @param string $storage    The absolute path of the storage where stores 
49
     *                           Shieldon generated data.
50
     * @param string $requestUri The entry URL of Firewall Panel.
51
     *
52
     * @return void
53
     */
54
    public function __construct(string $storage = '', string $requestUri = '')
55
    {   
56
        // Prevent possible issues occur in CLI command line.
57
        if (isset($_SERVER['REQUEST_URI'])) {
58
59
            $serverRequestUri = $_SERVER['REQUEST_URI'];
60
            $scriptFilename = $_SERVER['SCRIPT_FILENAME'];
61
62
            if (empty($storage)) {
63
64
                // The storage folder should be placed above www-root for best security, 
65
                // this folder must be writable.
66
                $storage = dirname($scriptFilename) . '/../shieldon_firewall';
67
            }
68
69
            $firewall = new Firewall();
70
            $firewall->configure($storage);
71
            $firewall->controlPanel('/firewall/panel/');
72
73
            if (
74
                $requestUri !== '' && 
75
                strpos($serverRequestUri, $requestUri) === 0
76
            ) {
77
                // Get into the Firewall Panel.
78
                $panel = new Panel();
79
                $panel->entry();
80
            }
81
        }
82
    }
83
84
    /**
85
     * Start protecting your site.
86
     *
87
     * @return void
88
     */
89
    public function run(): void
90
    {
91
        $firewall = Container::get('firewall');
92
93
        $response = $firewall->run();
94
95
        if ($response->getStatusCode() !== 200) {
96
            $httpResolver = new HttpResolver();
97
            $httpResolver($response);
98
        }
99
    }
100
}
101