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\Log;
24

25
use function date;
26
use function file_get_contents;
27
use function file_put_contents;
28
use function json_encode;
29
use function rtrim;
30
use function strtotime;
31
use function time;
32
use const JSON_PRETTY_PRINT;
33

34
/**
35
 * Cache the data parsed by ActionLogParser.
36
 */
37
final class ActionLogParsedCache
38
{
39
    /**
40
     * $directory The directory where to store the logs in.
41
     *
42
     * @var string
43
     */
44
    protected $directory = '';
45

46
    /**
47
     * Constructer.
48
     *
49
     * @param string $directory The directory where to store the logs in.
50
     */
51 3
    public function __construct(string $directory = '') 
52
    {
53 3
        $this->directory = $directory;
54
    }
55

56
    /**
57
     * Save the data into a cache file.
58
     *
59
     * @param string $type The period type of action logs.
60
     * @param string $data The parsed data of action logs.
61
     *                     The keys are `time`, `ip_details`, `period_data`.
62
     *
63
     * @return self
64
     */
65 3
    public function save(string $type = 'today', array $data = []): self
66
    {
67 3
        $data['time'] = time();
68

69 3
        $filePath = rtrim($this->directory, '/') . '/cache_' . $type . '.json';
70

71 3
        file_put_contents($filePath, json_encode($data, JSON_PRETTY_PRINT));
72

73 3
        return $this;
74
    }
75

76
    /**
77
     * Get the data from a cache file.
78
     *
79
     * @param string $type The period type of action logs.
80
     *
81
     * @return array
82
     */
83 3
    public function get(string $type = 'today'): array
84
    {
85 3
        $data = [];
86

87 3
        $filePath = rtrim($this->directory, '/') . '/cache_' . $type . '.json';
88

89 3
        if (file_exists($filePath)) {
90 3
            $content = file_get_contents($filePath);
91 3
            $data = json_decode($content, true);
92

93 3
            $cacheTime = $data['time'];
94

95 3
            $keepCache = true;
96

97 2
            switch ($type) {
98

99 3
                case 'yesterday':
100 3
                case 'past_seven_days':
101

102
                    // Update cache file daily.
103 3
                    $endTime = strtotime(date('Y-m-d', strtotime('-1 day')));
104 3
                    $beginTime = strtotime(date('Y-m-d', strtotime('-2 days')));
105 3
                    break;
106

107 3
                case 'last_month':
108

109
                    // Update cache file monthly.
110 3
                    $endTime = strtotime(date('Y-m-d', strtotime('-1 month')));
111 3
                    $beginTime = strtotime(date('Y-m-d', strtotime('-2 months')));
112 3
                    break;
113

114 3
                case 'this_month':
115

116
                    // Update cache file daily.
117 3
                    $endTime = strtotime(date('Y-m-d', strtotime('-1 day')));
118 3
                    $beginTime = strtotime(date('Y-m') . '-01');
119 3
                    break;
120

121 3
                case 'past_seven_hours':
122 3
                case 'today':
123
                default:
124

125
                    // Update cache file hourly.
126 3
                    $endTime = strtotime(date('Y-m-d H:00:00', time()));
127 3
                    $beginTime = strtotime(date('Y-m-d H:00:00', strtotime('-1 hour')));
128 3
                    break;
129
            }
130

131
            // The cacheTime is between beginTime and endTime.
132
            // @codeCoverageIgnoreStart
133
            if (($beginTime < $cacheTime) && ($endTime > $cacheTime)) {
134
                $keepCache = false;
135
            }
136

137
            if (!$keepCache) {
138
                $data = [];
139
            }
140
            // @codeCoverageIgnoreEnd
141
        }
142

143 3
        return $data;
144
    }
145
}

Read our documentation on viewing source code .

Loading