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

examples/SafeBrowsing/save-prefixes-db.php (3 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\Common\Exception\YandexException;
12
use Yandex\SafeBrowsing\Adapter\RedisAdapter;
13
use Yandex\SafeBrowsing\SafeBrowsingClient;
14
use Yandex\SafeBrowsing\SafeBrowsingException;
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
        $settings = require_once '../settings.php';
38
39
        if (empty($settings['safebrowsing']['key'])) {
40
            throw new SafeBrowsingException('Empty Safe Browsing key');
41
        }
42
        if (empty($settings['safebrowsing']['redis_dsn'])) {
43
            throw new SafeBrowsingException('Empty Safe Browsing Redis DSN');
44
        }
45
46
        $key = $settings['safebrowsing']['key'];
47
        $redisDsn = $settings['safebrowsing']['redis_dsn'];
48
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
        $shaVarsList = $safeBrowsing->getShavarsList();?>
61
62
        <p>Списки опасных сайтов:</p>
63
        <ul>
64
            <?php
65
            foreach ($shaVarsList as $shaVar) {
66
                ?>
67
                <li><?= $shaVar ?></li>
68
            <?php } ?>
69
        </ul>
70
        <?php
71
        $safeBrowsing->setMalwareShavars($shaVarsList);
72
        $malwaresData = $safeBrowsing->getMalwaresData();
73
74 View Code Duplication
        if (is_array($malwaresData)) {
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...
75
            foreach ($malwaresData as $shaVar => $chunks) {
76
                if (isset($chunks['added'])) {
77
                    foreach ($chunks['added'] as $chunkNum => $hashPrefixes) {
78
                        foreach ($hashPrefixes as $hashPrefix) {
79
                            $redisAdapter->saveHashPrefix($shaVar, $chunkNum, $hashPrefix);
80
                        }
81
                    }
82
                }
83
            }
84
        }
85
        ?>
86
        <div class="alert alert-success">Сохранены префиксы хешей в Redis</div>
87
        <div>
88
            Также можно посмотреть примеры:
89
            <ul>
90
                <li>
91
                    <a href="index.php">Проверить адреса</a>
92
                </li>
93
                <li>
94
                    <a href="local-search.php">Поиск префикса хеша сайта в локальной БД</a>
95
                </li>
96
                <li>
97
                    <a href="lookup.php">Lookup API и Check Adult API</a>
98
                </li>
99
                <li>
100
                    <a href="save-prefixes-db.php">Сохранение базы префиксов хешей вредоносных сайтов (начнется
101
                        автоматически)</a>
102
                </li>
103
            </ul>
104
        </div>
105
    <?php
106
107
    } catch (SafeBrowsingException $e) {
108
        echo "Safe Browsing Exception:<br/>";
109
        echo nl2br($e->getMessage());
110
    } catch (YandexException $e) {
111
        echo "Yandex Library Exception:<br/>";
112
        echo nl2br($e->getMessage());
113
    } catch (\Exception $e) {
114
        echo get_class($e) . "<br/>";
115
        echo nl2br($e->getMessage());
116
    }
117
    ?>
118
</div>
119
120
</body>
121
</html>
122