|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the keinos/mastodon-streaming-api-config package. |
|
5
|
|
|
* |
|
6
|
|
|
* - Authors, copyright, license, usage and etc.: |
|
7
|
|
|
* - See: https://github.com/KEINOS/Mastodon_StreamingAPI_Config/ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace KEINOS\MSTDN_TOOLS\Config; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\HttpClient\HttpClient; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
trait StaticMethodsTrait |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param string $access_token |
|
20
|
|
|
* @return bool |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function isCompliantAccessToken(string $access_token): bool |
|
23
|
|
|
{ |
|
24
|
|
|
if (empty(trim($access_token))) { |
|
25
|
|
|
return false; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
if (! self::isStringAlphaNumeric($access_token)) { |
|
29
|
|
|
return false; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
// Mastodon's token is 64 byte in length |
|
33
|
|
|
if (64 !== strlen($access_token)) { |
|
34
|
|
|
return false; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return true; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public static function isStringAlphaNumeric(string $string): bool |
|
41
|
|
|
{ |
|
42
|
|
|
// Some server doesn't enable ctype_alnum by default |
|
43
|
|
|
//return ctype_alnum($string); |
|
44
|
|
|
|
|
45
|
|
|
return (preg_match('/^[a-zA-Z0-9]+$/', $string) > 0); |
|
46
|
|
|
} |
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $url The target URL of the server. |
|
49
|
|
|
* @return bool Returns true if the URL is 200 status. |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function isUrlAlive(string $url): bool |
|
52
|
|
|
{ |
|
53
|
|
|
$client_http = HttpClient::create(); |
|
54
|
|
|
|
|
55
|
|
|
$response = $client_http->request('HEAD', $url); |
|
56
|
|
|
|
|
57
|
|
|
return (200 === $response->getStatusCode()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Checks if the URL scheme/protocol is "http:". Note that it's not "https:" to check. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $url |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function isUrlProtocolHttp(string $url): bool |
|
67
|
|
|
{ |
|
68
|
|
|
$target = 'http:'; |
|
69
|
|
|
|
|
70
|
|
|
return ($target === substr(trim($url), 0, strlen($target))); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public static function isUrlValidFormat(string $url): bool |
|
74
|
|
|
{ |
|
75
|
|
|
return (false !== filter_var($url, \FILTER_VALIDATE_URL)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Request to the Mastodon API endpoint. The endpoint must return in JSON. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $url_api URL to the API's endpoint. |
|
82
|
|
|
* @param string $access_token (Optional) |
|
83
|
|
|
* @return array<mixed,mixed> |
|
84
|
|
|
* @throws \Exception |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function requestApi(string $url_api, string $access_token = ''): array |
|
87
|
|
|
{ |
|
88
|
|
|
try { |
|
89
|
|
|
if (self::isUrlProtocolHttp($url_api)) { |
|
90
|
|
|
$msg = 'Not in "https:" protocol. We do not allow "http:" protocol.'; |
|
91
|
|
|
throw new \Exception($msg); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$header = []; |
|
95
|
|
|
if (! empty(trim($access_token))) { |
|
96
|
|
|
if (! self::isCompliantAccessToken($access_token)) { |
|
97
|
|
|
$msg = 'Invalid access token format.'; |
|
98
|
|
|
throw new \Exception($msg); |
|
99
|
|
|
} |
|
100
|
|
|
$header = [ |
|
101
|
|
|
'headers' => [ |
|
102
|
|
|
'Authorization' => "Bearer ${access_token}", |
|
103
|
|
|
] |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$client_http = HttpClient::create(); |
|
108
|
|
|
$response = $client_http->request('GET', $url_api, $header); |
|
109
|
|
|
|
|
110
|
|
|
// Get contents as array |
|
111
|
|
|
return $response->toArray(); |
|
112
|
|
|
} catch (\Exception $e) { |
|
113
|
|
|
$msg = 'Error while requesting to the server.' . PHP_EOL |
|
114
|
|
|
. '- Error details: ' . $e->getMessage() . PHP_EOL; |
|
115
|
|
|
throw new \Exception($msg); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths