31
31
*/
32
32
namespace GameNet \Jabber ;
33
33
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 ;
40
35
41
36
/**
42
37
* Class RpcClient
@@ -62,6 +57,8 @@ class RpcClient
62
57
const VCARD_DESCRIPTION = 'DESC ' ;
63
58
const VCARD_AVATAR_URL = 'EXTRA PHOTOURL ' ;
64
59
60
+ const RESPONSE_MAX_LENGTH = 10000000 ;
61
+
65
62
/**
66
63
* @var string
67
64
*/
@@ -86,6 +83,10 @@ class RpcClient
86
83
* @var string
87
84
*/
88
85
protected $ password ;
86
+ /**
87
+ * @var string
88
+ */
89
+ protected $ userAgent ;
89
90
90
91
public function __construct (array $ options )
91
92
{
@@ -103,6 +104,7 @@ public function __construct(array $options)
103
104
$ this ->password = isset ($ options ['password ' ]) ? $ options ['password ' ] : '' ;
104
105
$ this ->debug = isset ($ options ['debug ' ]) ? (bool )$ options ['debug ' ] : false ;
105
106
$ this ->timeout = isset ($ options ['timeout ' ]) ? (int )$ options ['timeout ' ] : 5 ;
107
+ $ this ->userAgent = isset ($ options ['userAgent ' ]) ? $ options ['userAgent ' ] : 'GameNet ' ;
106
108
107
109
if ($ this ->username && !$ this ->password ) {
108
110
throw new \InvalidArgumentException ("Password cannot be empty if username was defined " );
@@ -114,6 +116,7 @@ public function __construct(array $options)
114
116
115
117
/**
116
118
* @param int $timeout
119
+ * @return $this
117
120
*/
118
121
public function setTimeout ($ timeout )
119
122
{
@@ -122,6 +125,8 @@ public function setTimeout($timeout)
122
125
}
123
126
124
127
$ this ->timeout = $ timeout ;
128
+
129
+ return $ this ;
125
130
}
126
131
127
132
/**
@@ -132,32 +137,56 @@ public function getTimeout()
132
137
return $ this ->timeout ;
133
138
}
134
139
135
- protected function sendRequest ($ command , array $ params )
140
+ /**
141
+ * @param string $userAgent
142
+ * @return $this
143
+ */
144
+ public function setUserAgent ($ userAgent )
136
145
{
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
+ }
144
150
151
+ protected function sendRequest ($ command , array $ params )
152
+ {
145
153
if ($ this ->username && $ this ->password ) {
146
154
$ params = [
147
155
['user ' => $ this ->username , 'server ' => $ this ->server , 'password ' => $ this ->password ], $ params
148
156
];
149
157
}
150
158
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: " );
155
184
}
156
185
157
186
if ($ this ->debug ) {
158
- var_dump ($ command , $ client -> getPrependParams () , $ client -> getAppendParams (), $ result );
187
+ var_dump ($ command , $ params , $ response );
159
188
}
160
189
161
- return $ result ;
190
+ return $ xml ;
162
191
}
163
192
}
0 commit comments