|
2 | 2 |
|
3 | 3 | namespace wcf\system\steam; |
4 | 4 |
|
| 5 | +use GuzzleHttp\Exception\BadResponseException; |
| 6 | +use GuzzleHttp\Exception\GuzzleException; |
| 7 | +use GuzzleHttp\Psr7\Request; |
5 | 8 | use wcf\system\exception\SteamException; |
6 | | -use wcf\system\exception\SystemException; |
7 | | -use wcf\util\exception\HTTPException; |
8 | | -use wcf\util\HTTPRequest; |
| 9 | +use wcf\system\io\HttpFactory; |
9 | 10 | use wcf\util\JSON; |
10 | 11 | use wcf\util\StringUtil; |
11 | 12 |
|
@@ -54,30 +55,38 @@ public static function execute(string $interface, string $method, int $version, |
54 | 55 | $apiURL = 'http://api.steampowered.com/' . $interface . '/' . $method . '/v' . $version . '/'; |
55 | 56 |
|
56 | 57 | $postParameters = []; |
| 58 | + $headers = []; |
57 | 59 | if ($httpmethod == 'GET') { |
58 | 60 | $apiURL .= '?' . http_build_query($parameters, '', '&'); |
59 | 61 | } else { |
60 | 62 | $postParameters = $parameters; |
| 63 | + $headers['content-type'] = 'application/x-www-form-urlencoded'; |
61 | 64 | } |
62 | 65 |
|
63 | | - $request = new HTTPRequest($apiURL, ['method' => $httpmethod], $postParameters); |
| 66 | + $request = new Request($httpmethod, $apiURL, $headers, http_build_query($postParameters, '', '&')); |
64 | 67 | try { |
65 | | - $request->execute(); |
66 | | - $reply = $request->getReply(); |
| 68 | + $response = HttpFactory::getDefaultClient()->send($request); |
| 69 | + $content = (string)$response->getBody(); |
67 | 70 | try { |
68 | | - return JSON::decode($reply['body'], true); |
69 | | - } catch (SystemException $e) { |
70 | | - return $reply['body']; |
| 71 | + return JSON::decode($content, true); |
| 72 | + } catch (\Exception $e) { |
| 73 | + return $content; |
71 | 74 | } |
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(); |
| 75 | + } catch (BadResponseException $e) { |
| 76 | + if (\ENABLE_DEBUG_MODE) { |
| 77 | + \wcf\functions\exception\logThrowable($e); |
| 78 | + } |
| 79 | + $content = (string)$e->getResponse()->getBody(); |
76 | 80 | try { |
77 | | - return JSON::decode($reply['body'], true); |
78 | | - } catch (SystemException $e) { |
79 | | - return $reply['body']; |
| 81 | + return JSON::decode($content, true); |
| 82 | + } catch (\Exception $e) { |
| 83 | + return $content; |
| 84 | + } |
| 85 | + } catch (GuzzleException $e) { |
| 86 | + if (\ENABLE_DEBUG_MODE) { |
| 87 | + \wcf\functions\exception\logThrowable($e); |
80 | 88 | } |
| 89 | + throw new SteamException('Wrong Steam API call or Steam API is not reachable. (Message: ' . $e->getMessage() . ')'); |
81 | 90 | } |
82 | 91 | } |
83 | 92 |
|
@@ -119,11 +128,23 @@ public static function validateOpenID(): int |
119 | 128 | } |
120 | 129 | } |
121 | 130 | $params['openid.mode'] = 'check_authentication'; |
| 131 | + $parameters = \http_build_query($params, "", '&'); |
122 | 132 |
|
123 | | - $request = new HTTPRequest('https://steamcommunity.com/openid/login', [], $params); |
124 | | - $request->execute(); |
125 | | - $reply = $request->getReply(); |
126 | | - $content = $reply['body']; |
| 133 | + $headers = [ |
| 134 | + 'content-type' => 'application/x-www-form-urlencoded' |
| 135 | + ]; |
| 136 | + |
| 137 | + $request = new Request('POST', 'https://steamcommunity.com/openid/login', $headers, $parameters); |
| 138 | + $content = ''; |
| 139 | + try { |
| 140 | + $response = HttpFactory::getDefaultClient()->send($request); |
| 141 | + $content = (string)$response->getBody(); |
| 142 | + } catch (\Exception $e) { |
| 143 | + if (\ENABLE_DEBUG_MODE) { |
| 144 | + \wcf\functions\exception\logThrowable($e); |
| 145 | + } |
| 146 | + throw new SteamException('API error - ' . $e->getMessage()); |
| 147 | + } |
127 | 148 |
|
128 | 149 | if (strpos($content, 'is_valid:true') === false) { |
129 | 150 | throw new SteamException('Invalid authentication'); |
|
0 commit comments