1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RattfieldNz\SafeUrls; |
4
|
|
|
|
5
|
|
|
use RattfieldNz\SafeUrls\Libraries\Curl\Curl; |
6
|
|
|
use RattfieldNz\SafeUrls\Libraries\Data\Data; |
7
|
|
|
use RattfieldNz\SafeUrls\Libraries\Traits\StaticCalling; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class SafeUrlsFacade. |
11
|
|
|
* |
12
|
|
|
* The main class used for the SafeUrlsFacade package. |
13
|
|
|
* |
14
|
|
|
* @category PHP |
15
|
|
|
* |
16
|
|
|
* @author Rob Attfield <[email protected]> |
17
|
|
|
* @license https://github.com/rattfieldnz/safe-urls/blob/master/license.md MIT |
18
|
|
|
* |
19
|
|
|
* @link https://github.com/rattfieldnz/safe-urls/ |
20
|
|
|
*/ |
21
|
|
|
class SafeUrls |
22
|
|
|
{ |
23
|
|
|
use StaticCalling; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array Variable to hold list of urls to check. |
27
|
|
|
*/ |
28
|
|
|
private $urls; |
29
|
|
|
|
30
|
|
|
private $results; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* SafeUrlsFacade constructor. |
34
|
|
|
*/ |
35
|
17 |
|
public function __construct() |
36
|
|
|
{ |
37
|
|
|
// Initialise the list of urls as an empty array. |
38
|
17 |
|
$this->urls = []; |
39
|
|
|
|
40
|
|
|
// Initialise the list of urls as an empty JSON string. |
41
|
17 |
|
$this->results = ''; |
42
|
17 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Checks a given set of URLs with Google's Safe Browsing API. |
46
|
|
|
* |
47
|
|
|
* @param array $urls An array of URLs to check. |
48
|
|
|
* @param bool $resultsAsArray Determines whether results will be returned as array or JSON. |
49
|
|
|
* |
50
|
|
|
* @throws \ErrorException |
51
|
|
|
* |
52
|
|
|
* @return string|array The set of results, in JSON. |
53
|
|
|
*/ |
54
|
7 |
|
public static function check(array $urls, bool $resultsAsArray = false) |
55
|
|
|
{ |
56
|
7 |
|
$payload = Data::payload($urls); |
57
|
|
|
|
58
|
7 |
|
$data = (new Curl($payload))->getData(); |
59
|
|
|
|
60
|
7 |
|
return $resultsAsArray === false ? $data : json_decode($data, true); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Function used to test 'check' static method with mocks in PHPUnit. |
65
|
|
|
* |
66
|
|
|
* @param array $urls An array of URLs to check. |
67
|
|
|
* |
68
|
|
|
* @return array|string The set of results, in JSON. |
69
|
|
|
*/ |
70
|
1 |
|
public function checkCallStatic(array $urls) |
71
|
|
|
{ |
72
|
1 |
|
return $this->callStatic(self::class, 'check', $urls); |
73
|
|
|
} |
74
|
|
|
|
75
|
5 |
|
public function execute() |
76
|
|
|
{ |
77
|
5 |
|
$this->results = self::check($this->urls); |
78
|
|
|
|
79
|
5 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Add URLs to existing list for checking. |
84
|
|
|
* |
85
|
|
|
* @param array $urls An array of URLs to add. |
86
|
|
|
* |
87
|
|
|
* @return SafeUrls |
88
|
|
|
*/ |
89
|
12 |
|
public function add(array $urls): self |
90
|
|
|
{ |
91
|
12 |
|
$this->urls = array_merge($this->urls, $urls); |
92
|
|
|
|
93
|
12 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Remove URLs from existing list. |
98
|
|
|
* |
99
|
|
|
* @param array $urls An array of URLs to remove. |
100
|
|
|
* |
101
|
|
|
* @return SafeUrls |
102
|
|
|
*/ |
103
|
3 |
|
public function remove(array $urls): self |
104
|
|
|
{ |
105
|
3 |
|
$this->urls = array_values(array_diff($this->urls, $urls)); |
106
|
|
|
|
107
|
3 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the URLs currently saved. |
112
|
|
|
* |
113
|
|
|
* @param bool $asArray Determines whether results to be returned as array or JSON. |
114
|
|
|
* |
115
|
|
|
* @return array|string |
116
|
|
|
*/ |
117
|
6 |
|
public function getCurrentUrls(bool $asArray = false) |
118
|
|
|
{ |
119
|
6 |
|
return $asArray === false ? json_encode($this->urls) : $this->urls; |
120
|
|
|
} |
121
|
|
|
|
122
|
3 |
|
public function getResults() |
123
|
|
|
{ |
124
|
3 |
|
return $this->results; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Check to see if the URL has been marked as unsafe. |
129
|
|
|
* |
130
|
|
|
* @param string $url The URL to check. |
131
|
|
|
* @param string $resultSet An existing set of JSON results |
132
|
|
|
* to pass in as a parameter. |
133
|
|
|
* |
134
|
|
|
* @return bool True if the URL is unsafe, and false if |
135
|
|
|
* it is safe, |
136
|
|
|
*/ |
137
|
4 |
|
public function isDangerous(string $url, string $resultSet = ''): bool |
138
|
|
|
{ |
139
|
|
|
// Since Google API seems to not be working (even showing unsafe sites as 'safe'), |
140
|
|
|
// Below is a set of results retrieved via Postman. |
141
|
|
|
// See tests\Libraries\Curl\CurlTest testMalwareSocialEngineeringAnyPlatformUrl(). |
142
|
|
|
//$this->results = '{ "status": 200, "response": { "matches": [ { "threatType": "SOCIAL_ENGINEERING", "platformType": "ANY_PLATFORM", "threat": { "url": "https://testsafebrowsing.appspot.com/s/phishing.html" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "MALWARE", "platformType": "ANY_PLATFORM", "threat": { "url": "https://testsafebrowsing.appspot.com/s/malware.html" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "MALWARE", "platformType": "ANY_PLATFORM", "threat": { "url": "http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/MALWARE/URL/" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "SOCIAL_ENGINEERING", "platformType": "ANY_PLATFORM", "threat": { "url": "http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/SOCIAL_ENGINEERING/URL/" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "MALWARE", "platformType": "ANY_PLATFORM", "threat": { "url": "http://malware.testing.google.test/testing/malware/" }, "cacheDuration": "300s", "threatEntryType": "URL" }, { "threatType": "SOCIAL_ENGINEERING", "platformType": "ANY_PLATFORM", "threat": { "url": "http://malware.testing.google.test/testing/malware/" }, "cacheDuration": "300s", "threatEntryType": "URL" } ] } }'; |
143
|
|
|
//$data = json_decode($this->results); |
144
|
|
|
|
145
|
4 |
|
$data = !empty($resultSet) ? $resultSet : $this->results; |
146
|
|
|
|
147
|
4 |
|
$data = json_decode($data); |
148
|
4 |
|
$response = empty($data->response) ? null : $data->response; |
149
|
|
|
|
150
|
4 |
|
if (empty($response)) { |
151
|
2 |
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
3 |
|
if (isset($response->matches)) { |
155
|
3 |
|
foreach ($response->matches as $result) { |
156
|
3 |
|
if ($result->threat->url == $url) { |
157
|
2 |
|
return true; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
2 |
|
return false; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|