|
1 | 1 | <?php |
| 2 | + |
2 | 3 | namespace wcf\system\steam; |
| 4 | + |
3 | 5 | use wcf\system\exception\SteamException; |
4 | 6 | use wcf\system\exception\SystemException; |
5 | 7 | use wcf\util\exception\HTTPException; |
|
11 | 13 | * Steam Web Api class |
12 | 14 | * api key can got from here: https://steamcommunity.com/dev/apikey |
13 | 15 | * method documentation is from original documentations |
14 | | - * |
| 16 | + * |
15 | 17 | * @see https://openid.net/specs/openid-authentication-2_0.html |
16 | 18 | * @see https://api.steampowered.com/ISteamWebAPIUtil/GetSupportedAPIList/v1/?key=<HA_STEAM_API_KEY> |
17 | | - * |
| 19 | + * |
18 | 20 | * @author Peter Lohse <hanashi@hanashi.eu> |
19 | 21 | * @copyright Hanashi Development |
20 | | - * @license Freie Lizenz (https://hanashi.dev/freie-lizenz/) |
21 | | - * @package WoltLabSuite\Core\System\Steam |
| 22 | + * @license Freie Lizenz (https://hanashi.dev/freie-lizenz/) |
| 23 | + * @package WoltLabSuite\Core\System\Steam |
22 | 24 | */ |
23 | | -class SteamAPI { |
24 | | - /** |
25 | | - * execute a Steam API call |
26 | | - * |
27 | | - * @var string $interface Steam API interface |
28 | | - * @var string $method API method that should be executed |
29 | | - * @var int $version version of API method |
30 | | - * @var array $data API method parameters |
31 | | - * @var string $httpmethod HTTP method e.g. GET or POST |
32 | | - * @var bool $useJSON use json e.g. for GetOwnedGames |
33 | | - * @return array/string returns an array or a string of Steam API answer |
34 | | - */ |
35 | | - public static function execute(string $interface, string $method, int $version, array $data = [], string $httpmethod = 'GET', bool $useJSON = false) { |
36 | | - if (!HA_STEAM_API_KEY) throw new SteamException('Steam API key not configured.'); |
| 25 | +class SteamAPI |
| 26 | +{ |
| 27 | + /** |
| 28 | + * execute a Steam API call |
| 29 | + * |
| 30 | + * @var string $interface Steam API interface |
| 31 | + * @var string $method API method that should be executed |
| 32 | + * @var int $version version of API method |
| 33 | + * @var array $data API method parameters |
| 34 | + * @var string $httpmethod HTTP method e.g. GET or POST |
| 35 | + * @var bool $useJSON use json e.g. for GetOwnedGames |
| 36 | + * @return array/string returns an array or a string of Steam API answer |
| 37 | + */ |
| 38 | + public static function execute(string $interface, string $method, int $version, array $data = [], string $httpmethod = 'GET', bool $useJSON = false) |
| 39 | + { |
| 40 | + if (!HA_STEAM_API_KEY) { |
| 41 | + throw new SteamException('Steam API key not configured.'); |
| 42 | + } |
37 | 43 |
|
38 | | - $parameters = [ |
39 | | - 'format' => 'json', |
40 | | - 'key' => HA_STEAM_API_KEY |
41 | | - ]; |
42 | | - if ($useJSON && count($data)) { |
43 | | - $parameters['input_json'] = JSON::encode($data); |
44 | | - } else { |
45 | | - $parameters = array_merge($parameters, $data); |
46 | | - } |
| 44 | + $parameters = [ |
| 45 | + 'format' => 'json', |
| 46 | + 'key' => HA_STEAM_API_KEY |
| 47 | + ]; |
| 48 | + if ($useJSON && count($data)) { |
| 49 | + $parameters['input_json'] = JSON::encode($data); |
| 50 | + } else { |
| 51 | + $parameters = array_merge($parameters, $data); |
| 52 | + } |
47 | 53 |
|
48 | | - $apiURL = 'http://api.steampowered.com/' . $interface . '/' . $method . '/v' . $version . '/'; |
| 54 | + $apiURL = 'http://api.steampowered.com/' . $interface . '/' . $method . '/v' . $version . '/'; |
49 | 55 |
|
50 | | - $postParameters = []; |
51 | | - if ($httpmethod == 'GET') { |
52 | | - $apiURL .= '?' . http_build_query($parameters, null, '&'); |
53 | | - } else { |
54 | | - $postParameters = $parameters; |
55 | | - } |
| 56 | + $postParameters = []; |
| 57 | + if ($httpmethod == 'GET') { |
| 58 | + $apiURL .= '?' . http_build_query($parameters, null, '&'); |
| 59 | + } else { |
| 60 | + $postParameters = $parameters; |
| 61 | + } |
56 | 62 |
|
57 | | - $request = new HTTPRequest($apiURL, ['method' => $httpmethod], $postParameters); |
58 | | - try { |
59 | | - $request->execute(); |
60 | | - $reply = $request->getReply(); |
61 | | - try { |
62 | | - return JSON::decode($reply['body'], true); |
63 | | - } catch (SystemException $e) { |
64 | | - return $reply['body']; |
65 | | - } |
66 | | - } catch (HTTPException $e) { |
67 | | - throw new SteamException('Wrong Steam API call or Steam API is not reachable. (Message: ' . $e->getMessage() . ')'); |
68 | | - } catch (SystemException $e) { |
69 | | - $reply = $request->getReply(); |
70 | | - try { |
71 | | - return JSON::decode($reply['body'], true); |
72 | | - } catch (SystemException $e) { |
73 | | - return $reply['body']; |
74 | | - } |
75 | | - } |
76 | | - } |
| 63 | + $request = new HTTPRequest($apiURL, ['method' => $httpmethod], $postParameters); |
| 64 | + try { |
| 65 | + $request->execute(); |
| 66 | + $reply = $request->getReply(); |
| 67 | + try { |
| 68 | + return JSON::decode($reply['body'], true); |
| 69 | + } catch (SystemException $e) { |
| 70 | + return $reply['body']; |
| 71 | + } |
| 72 | + } catch (HTTPException $e) { |
| 73 | + throw new SteamException('Wrong Steam API call or Steam API is not reachable. (Message: ' . $e->getMessage() . ')'); |
| 74 | + } catch (SystemException $e) { |
| 75 | + $reply = $request->getReply(); |
| 76 | + try { |
| 77 | + return JSON::decode($reply['body'], true); |
| 78 | + } catch (SystemException $e) { |
| 79 | + return $reply['body']; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
77 | 83 |
|
78 | | - /********************* OpenID *********************/ |
| 84 | + /********************* OpenID *********************/ |
79 | 85 |
|
80 | | - /** |
81 | | - * get OpenID login url |
82 | | - * |
83 | | - * @var string $redirectUri URL to which the OP SHOULD return the User-Agent with the response indicating the status of the request. |
84 | | - * @var string $realm URL pattern the OP SHOULD ask the end user to trust. |
85 | | - * @return string |
86 | | - */ |
87 | | - public static function getOpenIDUrl(string $redirectUri, string $realm) : string { |
88 | | - $data = [ |
89 | | - 'openid.ns' => 'http://specs.openid.net/auth/2.0', |
90 | | - 'openid.mode' => 'checkid_setup', |
91 | | - 'openid.return_to' => $redirectUri, |
92 | | - 'openid.realm' => $realm, |
93 | | - 'openid.claimed_id' => 'http://specs.openid.net/auth/2.0/identifier_select', |
94 | | - 'openid.identity' => 'http://specs.openid.net/auth/2.0/identifier_select' |
95 | | - ]; |
96 | | - return 'https://steamcommunity.com/openid/login?' . http_build_query($data, null, '&'); |
97 | | - } |
| 86 | + /** |
| 87 | + * get OpenID login url |
| 88 | + * |
| 89 | + * @var string $redirectUri URL to which the OP SHOULD return the User-Agent with the response indicating the status of the request. |
| 90 | + * @var string $realm URL pattern the OP SHOULD ask the end user to trust. |
| 91 | + * @return string |
| 92 | + */ |
| 93 | + public static function getOpenIDUrl(string $redirectUri, string $realm): string |
| 94 | + { |
| 95 | + $data = [ |
| 96 | + 'openid.ns' => 'http://specs.openid.net/auth/2.0', |
| 97 | + 'openid.mode' => 'checkid_setup', |
| 98 | + 'openid.return_to' => $redirectUri, |
| 99 | + 'openid.realm' => $realm, |
| 100 | + 'openid.claimed_id' => 'http://specs.openid.net/auth/2.0/identifier_select', |
| 101 | + 'openid.identity' => 'http://specs.openid.net/auth/2.0/identifier_select' |
| 102 | + ]; |
| 103 | + return 'https://steamcommunity.com/openid/login?' . http_build_query($data, null, '&'); |
| 104 | + } |
98 | 105 |
|
99 | | - /** |
100 | | - * validate OpenID data and returns SteamID |
101 | | - * |
102 | | - * @var array $data content of $_GET by redirect uri, where prefix is "openid." |
103 | | - * @return int 64 bit of SteamID |
104 | | - */ |
105 | | - public static function validateOpenID() : int { |
106 | | - $params = []; |
| 106 | + /** |
| 107 | + * validate OpenID data and returns SteamID |
| 108 | + * |
| 109 | + * @var array $data content of $_GET by redirect uri, where prefix is "openid." |
| 110 | + * @return int 64 bit of SteamID |
| 111 | + */ |
| 112 | + public static function validateOpenID(): int |
| 113 | + { |
| 114 | + $params = []; |
107 | 115 | foreach ($_GET as $key => $val) { |
108 | 116 | if (StringUtil::startsWith($key, 'openid')) { |
109 | 117 | $newKey = 'openid.' . substr($key, 7); |
110 | 118 | $params[$newKey] = $val; |
111 | 119 | } |
112 | 120 | } |
113 | | - $params['openid.mode'] = 'check_authentication'; |
| 121 | + $params['openid.mode'] = 'check_authentication'; |
114 | 122 |
|
115 | | - $request = new HTTPRequest('https://steamcommunity.com/openid/login', [], $params); |
116 | | - $request->execute(); |
117 | | - $reply = $request->getReply(); |
118 | | - $content = $reply['body']; |
| 123 | + $request = new HTTPRequest('https://steamcommunity.com/openid/login', [], $params); |
| 124 | + $request->execute(); |
| 125 | + $reply = $request->getReply(); |
| 126 | + $content = $reply['body']; |
119 | 127 |
|
120 | | - if (strpos($content, 'is_valid:true') === false) throw new SteamException('Invalid authentication'); |
| 128 | + if (strpos($content, 'is_valid:true') === false) { |
| 129 | + throw new SteamException('Invalid authentication'); |
| 130 | + } |
121 | 131 |
|
122 | | - if (!preg_match('/^https:\/\/steamcommunity.com\/openid\/id\/([0-9]+)$/', $params['openid.claimed_id'], $matches)) throw new SteamException('Invalid Steam ID'); |
123 | | - if (empty($matches[1] || !is_numeric($matches[1]))) throw new SteamException('Invalid Steam ID'); |
| 132 | + if (!preg_match('/^https:\/\/steamcommunity.com\/openid\/id\/([0-9]+)$/', $params['openid.claimed_id'], $matches)) { |
| 133 | + throw new SteamException('Invalid Steam ID'); |
| 134 | + } |
| 135 | + if (empty($matches[1] || !is_numeric($matches[1]))) { |
| 136 | + throw new SteamException('Invalid Steam ID'); |
| 137 | + } |
124 | 138 |
|
125 | | - return $matches[1]; |
126 | | - } |
| 139 | + return $matches[1]; |
| 140 | + } |
127 | 141 | } |
0 commit comments