1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RattfieldNz\SafeUrls\Libraries\Data; |
4
|
|
|
|
5
|
|
|
use RattfieldNz\SafeUrls\Libraries\Config\Config; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Data. |
9
|
|
|
* |
10
|
|
|
* @category PHP |
11
|
|
|
* |
12
|
|
|
* @author Rob Attfield <[email protected]> |
13
|
|
|
* @license https://github.com/rattfieldnz/safe-urls/blob/master/license.md MIT |
14
|
|
|
* |
15
|
|
|
* @link https://github.com/rattfieldnz/safe-urls/ |
16
|
|
|
*/ |
17
|
|
|
class Data |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Generate payload for Google Safe Browsing API. |
21
|
|
|
* |
22
|
|
|
* @param array $urls List of URLS to add to payload for checking. |
23
|
|
|
* @param array $threatTypes List of Google Threat Types. |
24
|
|
|
* @param array $platformTypes List of Google Platform Types. |
25
|
|
|
* @param array $threatEntryTypes List of Google Threat Platform Types. |
26
|
|
|
* |
27
|
|
|
* @return array The generated payload. |
28
|
|
|
*/ |
29
|
11 |
|
public static function payload(array $urls, array $threatTypes = [], array $platformTypes = [], array $threatEntryTypes = []): array |
30
|
|
|
{ |
31
|
|
|
return [ |
32
|
|
|
'client' => [ |
33
|
11 |
|
'clientId' => Config::clientId(), |
34
|
11 |
|
'clientVersion' => Config::clientVersion(), |
35
|
|
|
], |
36
|
|
|
'threatInfo' => [ |
37
|
11 |
|
'threatTypes' => !empty($threatTypes) ? $threatTypes : Config::threatTypes(), |
38
|
11 |
|
'platformTypes' => !empty($platformTypes) ? $platformTypes : Config::platformTypes(), |
39
|
11 |
|
'threatEntryTypes' => !empty($threatEntryTypes) ? $threatEntryTypes : Config::threatEntryTypes(), |
40
|
11 |
|
'threatEntries' => self::formatUrls($urls), |
41
|
|
|
], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Format URLS suitable for use with API. |
47
|
|
|
* |
48
|
|
|
* @param array $urls List of URLS to format. |
49
|
|
|
* |
50
|
|
|
* @return array The formatted URLs. |
51
|
|
|
*/ |
52
|
14 |
|
public static function formatUrls(array $urls): array |
53
|
|
|
{ |
54
|
14 |
|
$formattedUrls = []; |
55
|
14 |
|
foreach ($urls as $url) { |
56
|
12 |
|
$formattedUrls[] = ['url' => $url]; |
57
|
|
|
} |
58
|
|
|
|
59
|
14 |
|
return $formattedUrls; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get Google Safebrowing API URL with key. |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
8 |
|
public static function googleApiUrl() |
68
|
|
|
{ |
69
|
|
|
return 'https://safebrowsing.googleapis.com/v4/threatMatches:find?key='. |
70
|
8 |
|
Config::googleApiKey(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|