Completed
Push — master ( 1dc1a5...f8d9f6 )
by Anton
10s
created

examples/SafeBrowsing/update-prefixes-db.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Example of usage Yandex\SafeBrowsing package
4
 *
5
 * @author   Alexander Khaylo <[email protected]>
6
 * @created  07.02.14 14:00
7
 */
8
ini_set('memory_limit', '256M');
9
set_time_limit(300);
10
11
use Yandex\SafeBrowsing\SafeBrowsingClient;
12
use Yandex\SafeBrowsing\SafeBrowsingException;
13
use Yandex\Common\Exception\YandexException;
14
use Yandex\SafeBrowsing\Adapter\RedisAdapter;
15
16
?>
17
<!doctype html>
18
<html lang="en-US">
19
<head>
20
    <meta charset="UTF-8">
21
    <title>Yandex PHP Library: Safe Browsing Demo</title>
22
    <link rel="stylesheet" href="//yandex.st/bootstrap/3.0.0/css/bootstrap.min.css">
23
    <link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
24
    <link rel="stylesheet" href="/examples/Disk/css/style.css">
25
</head>
26
<body>
27
28
<div class="container">
29
    <ol class="breadcrumb">
30
        <li><a href="/examples">Examples</a></li>
31
        <li><a href="/examples/SafeBrowsing">SafeBrowsing</a></li>
32
        <li class="active">Обновление локальной базы префиксов хешей вредоносных сайтов</li>
33
    </ol>
34
    <h3>Обновление локальной базы префиксов хешей вредоносных сайтов</h3>
35
    <?php
36
    try {
37
38
        $settings = require_once '../settings.php';
39
40
        if (empty($settings["safebrowsing"]["key"])) {
41
            throw new SafeBrowsingException('Empty Safe Browsing key');
42
        }
43
        if (empty($settings['safebrowsing']['redis_dsn'])) {
44
            throw new SafeBrowsingException('Empty Safe Browsing Redis DSN');
45
        }
46
47
        $key = $settings["safebrowsing"]["key"];
48
        $redisDsn = $settings['safebrowsing']['redis_dsn'];
49
        $redisOptions = [];
50 View Code Duplication
        if (isset($settings['safebrowsing']['redis_database'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
            $redisOptions['parameters']['database'] = $settings['safebrowsing']['redis_database'];
52
        }
53 View Code Duplication
        if (isset($settings['safebrowsing']['redis_password'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
            $redisOptions['parameters']['password'] = $settings['safebrowsing']['redis_password'];
55
        }
56
57
        $safeBrowsing = new SafeBrowsingClient($key);
58
        $redisAdapter = new RedisAdapter($redisDsn, $redisOptions);
59
60
        $localShaVars = $redisAdapter->getShaVars();
61
        $localChunkNums = [];
62
63
        foreach ($localShaVars as $shaVar) {
64
            $localChunkNums[$shaVar] = $redisAdapter->getChunkNums($shaVar);
65
        }
66
67
68
69
        /**
70
         * Example:
71
         */
72
        //$savedChunks['ydx-malware-shavar'] = [
73
        //    'added' => [
74
        //        'min' => 1,
75
        //        'max' => 30000
76
        //    ],
77
        //    'removed' => [
78
        //        'min' => 1,
79
        //        'max' => 30000
80
        //    ]
81
        //
82
        //];
83
        $savedChunks = [];
84
85
        foreach ($localChunkNums as $shaVar => $chunkNums) {
86
            $minChunkNum = false;
87
            $maxChunkNum = false;
88
89
            foreach ($chunkNums as $chunkNum) {
90
                if (!$maxChunkNum && !$minChunkNum) {
91
                    $minChunkNum = $chunkNum;
92
                    $maxChunkNum = $chunkNum;
93
                } elseif ($chunkNum > $maxChunkNum) {
94
                    $maxChunkNum = $chunkNum;
95
                } elseif ($chunkNum < $minChunkNum) {
96
                    $minChunkNum = $chunkNum;
97
                }
98
            }
99
100
            if ($minChunkNum && $maxChunkNum) {
101
                $savedChunks[$shaVar]['added'] = [
102
                    'min' => $minChunkNum,
103
                    'max' => $maxChunkNum
104
                ];
105
            }
106
        }
107
108
        $malwaresData = $safeBrowsing->getMalwaresData($savedChunks);
109
110
        if (is_string($malwaresData) && $malwaresData === 'pleasereset') {
111
            ?>
112
            <div class="alert alert-info">Нужно сбросить БД</div>
113
        <?php
114
        } else {
115
            $newPrefixes = [];
116
            $removedPrefixes = [];
117
            $newChunks = 0;
118
            $removedChunks = 0;
119
            if (is_array($malwaresData)) {
120
                foreach ($malwaresData as $shaVar => $chunks) {
121
122
                    //Need add new malwares hash prefixes
123 View Code Duplication
                    if (isset($chunks['added'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
                        foreach ($chunks['added'] as $chunkNum => $hashPrefixes) {
125
                            foreach ($hashPrefixes as $hashPrefix) {
126
                                if (!$redisAdapter->getHashPrefix($hashPrefix)) {
127
                                    $redisAdapter->saveHashPrefix($shaVar, $chunkNum, $hashPrefix);
128
                                    $newChunks++;
129
                                }
130
                            }
131
                        }
132
                    }
133
134
                    //Need remove chunks
135 View Code Duplication
                    if (isset($chunks['removed'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
                        foreach ($chunks['removed'] as $chunkNum => $hashPrefixes) {
137
                            foreach ($hashPrefixes as $hashPrefix) {
138
                                if ($redisAdapter->getHashPrefix($hashPrefix)) {
139
                                    $redisAdapter->removeHashPrefix($shaVar, $chunkNum, $hashPrefix);
140
                                    $removedChunks++;
141
                                }
142
                            }
143
                        }
144
                    }
145
146
                    //Need remove chunks range
147
                    if (isset($chunks['delete_added_ranges'])) {
148
                        foreach ($chunks['delete_added_ranges'] as $range) {
149
                            for ($i = $range['min']; $i <= $range['max']; $i++) {
150
                                $redisAdapter->removeChunkNum($shaVar, $chunkNum);
151
                                $removedChunks++;
152
                            }
153
                        }
154
                    }
155
                }
156
            }
157
            ?>
158
            <div class="alert alert-info">Новых кусков: <?= $newChunks ?></div>
159
            <div class="alert alert-info">Кусков, в которых содержаться более не опасные
160
                сайты: <?= $removedChunks ?></div>
161
            <div class="alert alert-success">Локальная БД обновлена успешно.</div>
162
            <div>
163
                Также можно посмотреть примеры:
164
                <ul>
165
                    <li>
166
                        <a href="index.php">Проверить адреса</a>
167
                    </li>
168
                    <li>
169
                        <a href="local-search.php">Поиск префикса хеша сайта в локальной БД</a>
170
                    </li>
171
                    <li>
172
                        <a href="lookup.php">Lookup API и Check Adult API</a>
173
                    </li>
174
                    <li>
175
                        <a href="update-prefixes-db.php">Обновить локальную базу префиксов хешей вредоносных сайтов
176
                            (начнется автоматически)</a>
177
                    </li>
178
                </ul>
179
            </div>
180
        <?php
181
        }
182
183
    } catch (SafeBrowsingException $e) {
184
        echo "Safe Browsing Exception:<br/>";
185
        echo nl2br($e->getMessage());
186
    } catch (YandexException $e) {
187
        echo "Yandex Library Exception:<br/>";
188
        echo nl2br($e->getMessage());
189
    } catch (\Exception $e) {
190
        echo get_class($e) . "<br/>";
191
        echo nl2br($e->getMessage());
192
    }
193
    ?>
194
</div>
195
196
</body>
197
</html>
198