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

examples/SafeBrowsing/local-search.php (1 issue)

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 13:57
7
 */
8
9
use Yandex\Common\Exception\YandexException;
10
use Yandex\SafeBrowsing\Adapter\RedisAdapter;
11
use Yandex\SafeBrowsing\SafeBrowsingClient;
12
use Yandex\SafeBrowsing\SafeBrowsingException;
13
14
ini_set('memory_limit', '256M');
15
?>
16
<!doctype html>
17
<html lang="en-US">
18
<head>
19
    <meta charset="UTF-8">
20
    <title>Yandex PHP Library: Safe Browsing Demo</title>
21
    <link rel="stylesheet" href="//yandex.st/bootstrap/3.0.0/css/bootstrap.min.css">
22
    <link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
23
    <link rel="stylesheet" href="/examples/Disk/css/style.css">
24
    <style>
25
        .btn {
26
            padding: 6px 12px;
27
        }
28
    </style>
29
</head>
30
<body>
31
32
<div class="container">
33
    <ol class="breadcrumb">
34
        <li><a href="/examples">Examples</a></li>
35
        <li><a href="/examples/SafeBrowsing">SafeBrowsing</a></li>
36
        <li class="active">Поиск префикса хеша сайта в локальной БД</li>
37
    </ol>
38
    <h3>Поиск префикса хеша сайта в локальной БД</h3>
39
    <?php
40
    try {
41
42
        $settings = require_once '../settings.php';
43
44
        if (empty($settings['safebrowsing']['key'])) {
45
            throw new SafeBrowsingException('Empty Safe Browsing key');
46
        }
47
        if (empty($settings['safebrowsing']['redis_dsn'])) {
48
            throw new SafeBrowsingException('Empty Safe Browsing Redis DSN');
49
        }
50
51
        if (isset($_GET['url']) && $_GET['url']) {
52
            $url = $_GET['url'];
53
54
            $key = $settings['safebrowsing']['key'];
55
            $redisDsn = $settings['safebrowsing']['redis_dsn'];
56
            $redisOptions = [];
57
            if (isset($settings['safebrowsing']['redis_database'])) {
58
                $redisOptions['parameters']['database'] = $settings['safebrowsing']['redis_database'];
59
            }
60 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...
61
                $redisOptions['parameters']['password'] = $settings['safebrowsing']['redis_password'];
62
            }
63
64
            $safeBrowsing = new SafeBrowsingClient($key);
65
            $redisAdapter = new RedisAdapter($redisDsn, $redisOptions);
66
67
            $hashes = $safeBrowsing->getHashesByUrl($url);
68
69
            if ($redisAdapter->hasHashes($hashes)) {
70
                ?>
71
                <div class="alert alert-danger">Найден полный хеш для "<?= htmlentities($url) ?>" в списке опасных сайтов</div>
72
            <?php
73
            } else {
74
                ?>
75
                <div class="alert alert-success"><?= htmlentities($url) ?> - не найден в списке опасных сайтов</div>
76
            <?php
77
            }
78
        }
79
        ?>
80
81
        <form method="get">
82
            <div class="input-group">
83
                <input name="url" placeholder="URL" type="text" class="form-control"/>
84
                <span class="input-group-btn">
85
                    <input class="btn btn-primary" type="submit" value="Проверить URL"/>
86
                </span>
87
            </div>
88
        </form>
89
        <p>
90
            Пример: http://www.wmconvirus.narod.ru/
91
        </p>
92
        <div>
93
            Также можно посмотреть примеры:
94
            <ul>
95
                <li>
96
                    <a href="index.php">Проверить адреса</a>
97
                </li>
98
                <li>
99
                    <a href="lookup.php">Lookup API и Check Adult API</a>
100
                </li>
101
                <li>
102
                    <a href="save-prefixes-db.php">Сохранение базы префиксов хешей вредоносных сайтов (начнется
103
                        автоматически)</a>
104
                </li>
105
                <li>
106
                    <a href="update-prefixes-db.php">Обновить локальную базу префиксов хешей вредоносных сайтов (начнется
107
                        автоматически)</a>
108
                </li>
109
            </ul>
110
        </div>
111
    <?php
112
    } catch (SafeBrowsingException $e) {
113
        echo "Safe Browsing Exception:<br/>";
114
        echo nl2br($e->getMessage());
115
    } catch (YandexException $e) {
116
        echo "Yandex Library Exception:<br/>";
117
        echo nl2br($e->getMessage());
118
    } catch (\Exception $e) {
119
        echo get_class($e) . "<br/>";
120
        echo nl2br($e->getMessage());
121
    }
122
    ?>
123
</div>
124
125
</body>
126
</html>
127