1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <contact@terryl.in>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 * 
10
 * php version 7.1.0
11
 * 
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <contact@terryl.in>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20

21
declare(strict_types=1);
22

23
namespace Shieldon\Firewall\Driver;
24

25
use RuntimeException;
26
use function is_dir;
27
use function mkdir;
28
use function str_replace;
29
use function umask;
30

31
/**
32
 * SQL Driver Trait
33
 */
34
trait FileDriverTrait
35
{
36
    /**
37
     * Create a directory for storing data files.
38
     *
39
     * @return bool
40
     */
41 3
    protected function createDirectory(): bool
42
    {
43 3
        $conA = $conB = $conC = false;
44

45 3
        $originalUmask = umask(0);
46

47 3
        if (!is_dir($this->getDirectory('filter'))) {
48 3
            $conA = mkdir($this->getDirectory('filter'), 0777, true);
49
        }
50

51 3
        if (!is_dir($this->getDirectory('rule'))) {
52 3
            $conB = mkdir($this->getDirectory('rule'), 0777, true);
53
        }
54

55 3
        if (!is_dir($this->getDirectory('session'))) {
56 3
            $conC = mkdir($this->getDirectory('session'), 0777, true);
57
        }
58

59 3
        umask($originalUmask);
60

61 3
        if ($conA && $conB && $conC) {
62 3
            return true;
63
        }
64

65 3
        return false;
66
    }
67

68
    /**
69
     * Check the directory if is writable.
70
     * 
71
     * Not real use in Kernel. only use it in unit tests.
72
     *
73
     * @return bool
74
     */
75 3
    protected function checkDirectory(): bool
76
    {
77
        if (
78 3
            !is_dir($this->directory) || 
79 3
            !is_writable($this->directory)
80
        ) {
81 3
            throw new RuntimeException(
82 3
                'The directory defined by File Driver must be writable. (' . $this->directory . ')'
83
            );
84
        }
85

86 3
        return true;
87
    }
88

89
    /**
90
     * Get filename.
91
     *
92
     * @param string $ip   IP address.
93
     * @param string $type The table name of the data cycle.
94
     *
95
     * @return string
96
     */
97 3
    private function getFilename(string $ip, string $type = 'filter'): string
98
    {
99 3
        $ip = str_replace(':', '-', $ip);
100 3
        $path = [];
101

102 3
        $path['filter']  = $this->directory . '/' . $this->tableFilterLogs . '/' . $ip . '.' . $this->extension;
103 3
        $path['session'] = $this->directory . '/' . $this->tableSessions   . '/' . $ip . '.' . $this->extension;
104 3
        $path['rule']    = $this->directory . '/' . $this->tableRuleList   . '/' . $ip . '.' . $this->extension;
105

106 3
        return $path[$type] ?? '';
107
    }
108

109
    /**
110
     * Get directory.
111
     *
112
     * @param string $type The table name of the data cycle.
113
     *
114
     * @return string
115
     */
116 3
    private function getDirectory(string $type = 'filter'): string
117
    {
118 3
        $path = [];
119

120 3
        $path['filter']  = $this->directory . '/' . $this->tableFilterLogs;
121 3
        $path['session'] = $this->directory . '/' . $this->tableSessions;
122 3
        $path['rule']    = $this->directory . '/' . $this->tableRuleList;
123

124 3
        return $path[$type] ?? '';
125
    }
126
}

Read our documentation on viewing source code .

Loading