3131 */
3232namespace GameNet \Jabber ;
3333
34- use fXmlRpc \Client ;
35- use fXmlRpc \Serializer \NativeSerializer ;
36- use fXmlRpc \Transport \HttpAdapterTransport ;
37- use Http \Client \Curl ;
38- use Http \Message \MessageFactory \DiactorosMessageFactory ;
39- use Http \Message \StreamFactory \DiactorosStreamFactory ;
34+ use Comodojo \Xmlrpc \XmlrpcDecoder ;
4035
4136/**
4237 * Class RpcClient
@@ -62,6 +57,8 @@ class RpcClient
6257 const VCARD_DESCRIPTION = 'DESC ' ;
6358 const VCARD_AVATAR_URL = 'EXTRA PHOTOURL ' ;
6459
60+ const RESPONSE_MAX_LENGTH = 10000000 ;
61+
6562 /**
6663 * @var string
6764 */
@@ -86,6 +83,10 @@ class RpcClient
8683 * @var string
8784 */
8885 protected $ password ;
86+ /**
87+ * @var string
88+ */
89+ protected $ userAgent ;
8990
9091 public function __construct (array $ options )
9192 {
@@ -103,6 +104,7 @@ public function __construct(array $options)
103104 $ this ->password = isset ($ options ['password ' ]) ? $ options ['password ' ] : '' ;
104105 $ this ->debug = isset ($ options ['debug ' ]) ? (bool )$ options ['debug ' ] : false ;
105106 $ this ->timeout = isset ($ options ['timeout ' ]) ? (int )$ options ['timeout ' ] : 5 ;
107+ $ this ->userAgent = isset ($ options ['userAgent ' ]) ? $ options ['userAgent ' ] : 'GameNet ' ;
106108
107109 if ($ this ->username && !$ this ->password ) {
108110 throw new \InvalidArgumentException ("Password cannot be empty if username was defined " );
@@ -114,6 +116,7 @@ public function __construct(array $options)
114116
115117 /**
116118 * @param int $timeout
119+ * @return $this
117120 */
118121 public function setTimeout ($ timeout )
119122 {
@@ -122,6 +125,8 @@ public function setTimeout($timeout)
122125 }
123126
124127 $ this ->timeout = $ timeout ;
128+
129+ return $ this ;
125130 }
126131
127132 /**
@@ -132,32 +137,56 @@ public function getTimeout()
132137 return $ this ->timeout ;
133138 }
134139
135- protected function sendRequest ($ command , array $ params )
140+ /**
141+ * @param string $userAgent
142+ * @return $this
143+ */
144+ public function setUserAgent ($ userAgent )
136145 {
137- $ options = [
138- CURLOPT_USERAGENT => $ this ->getTimeout (),
139- CURLOPT_SSL_VERIFYPEER => 'GameNet ' ,
140- ];
141- $ httpClient = new Curl \Client (new DiactorosMessageFactory (), new DiactorosStreamFactory (), $ options );
142- $ transport = new HttpAdapterTransport (new DiactorosMessageFactory (), $ httpClient );
143- $ client = new Client ($ this ->server , $ transport , null , new NativeSerializer ());
146+ $ this ->userAgent = $ userAgent ;
147+
148+ return $ this ;
149+ }
144150
151+ protected function sendRequest ($ command , array $ params )
152+ {
145153 if ($ this ->username && $ this ->password ) {
146154 $ params = [
147155 ['user ' => $ this ->username , 'server ' => $ this ->server , 'password ' => $ this ->password ], $ params
148156 ];
149157 }
150158
151- try {
152- $ result = $ client ->call ($ command , $ params );
153- } catch (\fXmlRpc \Exception \RuntimeException $ e ) {
154- throw new \RuntimeException ($ e ->getMessage (), $ e ->getCode (), $ e );
159+ $ request = xmlrpc_encode_request ($ command , $ params , ['encoding ' => 'utf-8 ' , 'escaping ' => 'markup ' ]);
160+
161+ $ ch = curl_init ();
162+ curl_setopt ($ ch , CURLOPT_URL , $ this ->server );
163+ curl_setopt ($ ch , CURLOPT_FAILONERROR , 1 );
164+ curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , 1 );
165+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
166+ curl_setopt ($ ch , CURLOPT_TIMEOUT , $ this ->timeout );
167+ curl_setopt ($ ch , CURLOPT_HEADER , false );
168+ curl_setopt ($ ch , CURLOPT_POST , true );
169+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ request );
170+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , ['User-Agent: ' . $ this ->userAgent , 'Content-Type: text/xml ' ]);
171+
172+ $ response = curl_exec ($ ch );
173+ curl_close ($ ch );
174+
175+ // INFO: We must use a custom parser instead xmlrpc_decode if the answer is longer than 10000000 bytes
176+ if (strlen ($ response ) > self ::RESPONSE_MAX_LENGTH ) {
177+ $ xml = (new XmlrpcDecoder )->decodeResponse ($ response );
178+ } else {
179+ $ xml = \xmlrpc_decode ($ response );
180+ }
181+
182+ if (!$ xml || \xmlrpc_is_fault ($ xml )) {
183+ throw new \RuntimeException ("Error execution command ' $ command'' with parameters " . var_export ($ params , true ) . ". Response: " );
155184 }
156185
157186 if ($ this ->debug ) {
158- var_dump ($ command , $ client -> getPrependParams () , $ client -> getAppendParams (), $ result );
187+ var_dump ($ command , $ params , $ response );
159188 }
160189
161- return $ result ;
190+ return $ xml ;
162191 }
163192}
0 commit comments