22
33namespace wcf \system \steam ;
44
5+ use GuzzleHttp \Exception \BadResponseException ;
6+ use GuzzleHttp \Exception \GuzzleException ;
7+ use GuzzleHttp \Psr7 \Request ;
58use 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 ;
910use wcf \util \JSON ;
1011use wcf \util \StringUtil ;
1112
@@ -54,30 +55,38 @@ public static function execute(string $interface, string $method, int $version,
5455 $ apiURL = 'http://api.steampowered.com/ ' . $ interface . '/ ' . $ method . '/v ' . $ version . '/ ' ;
5556
5657 $ postParameters = [];
58+ $ headers = [];
5759 if ($ httpmethod == 'GET ' ) {
58- $ apiURL .= '? ' . http_build_query ($ parameters , null , '& ' );
60+ $ apiURL .= '? ' . http_build_query ($ parameters , '' , '& ' );
5961 } else {
6062 $ postParameters = $ parameters ;
63+ $ headers ['content-type ' ] = 'application/x-www-form-urlencoded ' ;
6164 }
6265
63- $ request = new HTTPRequest ( $ apiURL , [ ' method ' => $ httpmethod ], $ postParameters );
66+ $ request = new Request ( $ httpmethod , $ apiURL , $ headers , http_build_query ( $ postParameters, '' , ' & ' ) );
6467 try {
65- $ request -> execute ( );
66- $ reply = $ request -> getReply ();
68+ $ response = HttpFactory:: getDefaultClient ()-> send ( $ request );
69+ $ content = ( string ) $ response -> getBody ();
6770 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 ;
7174 }
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 ();
7680 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 );
8088 }
89+ throw new SteamException ('Wrong Steam API call or Steam API is not reachable. (Message: ' . $ e ->getMessage () . ') ' );
8190 }
8291 }
8392
@@ -100,7 +109,7 @@ public static function getOpenIDUrl(string $redirectUri, string $realm): string
100109 'openid.claimed_id ' => 'http://specs.openid.net/auth/2.0/identifier_select ' ,
101110 'openid.identity ' => 'http://specs.openid.net/auth/2.0/identifier_select '
102111 ];
103- return 'https://steamcommunity.com/openid/login? ' . http_build_query ($ data , null , '& ' );
112+ return 'https://steamcommunity.com/openid/login? ' . http_build_query ($ data , '' , '& ' );
104113 }
105114
106115 /**
@@ -119,11 +128,23 @@ public static function validateOpenID(): int
119128 }
120129 }
121130 $ params ['openid.mode ' ] = 'check_authentication ' ;
131+ $ parameters = \http_build_query ($ params , "" , '& ' );
122132
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+ }
127148
128149 if (strpos ($ content , 'is_valid:true ' ) === false ) {
129150 throw new SteamException ('Invalid authentication ' );
0 commit comments