-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathClient.php
More file actions
2148 lines (2001 loc) · 89.4 KB
/
Client.php
File metadata and controls
2148 lines (2001 loc) · 89.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace PhpXmlRpc;
use PhpXmlRpc\Exception\ValueErrorException;
use PhpXmlRpc\Helper\XMLParser;
use PhpXmlRpc\Traits\CharsetEncoderAware;
use PhpXmlRpc\Traits\DeprecationLogger;
/**
* Used to represent a client of an XML-RPC server.
*
* @property int $errno deprecated - public access left in purely for BC.
* @property string $errstr deprecated - public access left in purely for BC.
* @property string $method deprecated - public access left in purely for BC. Access via getUrl()/__construct()
* @property string $server deprecated - public access left in purely for BC. Access via getUrl()/__construct()
* @property int $port deprecated - public access left in purely for BC. Access via getUrl()/__construct()
* @property string $path deprecated - public access left in purely for BC. Access via getUrl()/__construct()
*/
class Client
{
use DeprecationLogger;
//use CharsetEncoderAware;
const USE_CURL_NEVER = 0;
const USE_CURL_ALWAYS = 1;
const USE_CURL_AUTO = 2;
const OPT_ACCEPTED_CHARSET_ENCODINGS = 'accepted_charset_encodings';
const OPT_ACCEPTED_COMPRESSION = 'accepted_compression';
const OPT_AUTH_TYPE = 'authtype';
const OPT_CA_CERT = 'cacert';
const OPT_CA_CERT_DIR = 'cacertdir';
const OPT_CERT = 'cert';
const OPT_CERT_PASS = 'certpass';
const OPT_COOKIES = 'cookies';
const OPT_DEBUG = 'debug';
const OPT_EXTRA_CURL_OPTS = 'extracurlopts';
const OPT_EXTRA_SOCKET_OPTS = 'extrasockopts';
const OPT_KEEPALIVE = 'keepalive';
const OPT_KEY = 'key';
const OPT_KEY_PASS = 'keypass';
const OPT_NO_MULTICALL = 'no_multicall';
const OPT_PASSWORD = 'password';
const OPT_PROXY = 'proxy';
const OPT_PROXY_AUTH_TYPE = 'proxy_authtype';
const OPT_PROXY_PASS = 'proxy_pass';
const OPT_PROXY_PORT = 'proxyport';
const OPT_PROXY_USER = 'proxy_user';
const OPT_REQUEST_CHARSET_ENCODING = 'request_charset_encoding';
const OPT_REQUEST_COMPRESSION = 'request_compression';
const OPT_RETURN_TYPE = 'return_type';
const OPT_SSL_VERSION = 'sslversion';
const OPT_TIMEOUT = 'timeout';
const OPT_USERNAME = 'username';
const OPT_USER_AGENT = 'user_agent';
const OPT_USE_CURL = 'use_curl';
const OPT_VERIFY_HOST = 'verifyhost';
const OPT_VERIFY_PEER = 'verifypeer';
const OPT_EXTRA_HEADERS = 'extra_headers';
/** @var string */
protected static $requestClass = '\\PhpXmlRpc\\Request';
/** @var string */
protected static $responseClass = '\\PhpXmlRpc\\Response';
/**
* @var int
* @deprecated will be removed in the future
*/
protected $errno;
/**
* @var string
* @deprecated will be removed in the future
*/
protected $errstr;
/// @todo: do all the ones below need to be public?
/**
* @var string
*/
protected $method = 'http';
/**
* @var string
*/
protected $server;
/**
* @var int
*/
protected $port = 0;
/**
* @var string
*/
protected $path;
/**
* @var int
*/
protected $debug = 0;
/**
* @var string
*/
protected $username = '';
/**
* @var string
*/
protected $password = '';
/**
* @var int
*/
protected $authtype = 1;
/**
* @var string
*/
protected $cert = '';
/**
* @var string
*/
protected $certpass = '';
/**
* @var string
*/
protected $cacert = '';
/**
* @var string
*/
protected $cacertdir = '';
/**
* @var string
*/
protected $key = '';
/**
* @var string
*/
protected $keypass = '';
/**
* @var bool
*/
protected $verifypeer = true;
/**
* @var int
*/
protected $verifyhost = 2;
/**
* @var int Corresponds to CURL_SSLVERSION_DEFAULT. Other CURL_SSLVERSION_ values are supported when in curl mode,
* and in socket mode different values from 0 to 7, matching the corresponding curl value. Old php versions
* do not support all values, php 5.4 and 5.5 do not support any in fact.
* NB: please do not use any version lower than TLS 1.3 (value: 7) as they are considered insecure.
*/
protected $sslversion = 0;
/**
* @var string
*/
protected $proxy = '';
/**
* @var int
*/
protected $proxyport = 0;
/**
* @var string
*/
protected $proxy_user = '';
/**
* @var string
*/
protected $proxy_pass = '';
/**
* @var int
*/
protected $proxy_authtype = 1;
/**
* @var array
*/
protected $cookies = array();
/**
* @var array
*/
protected $extrasockopts = array();
/**
* @var array
*/
protected $extracurlopts = array();
/**
* @var int
*/
protected $timeout = 0;
/**
* @var int
*/
protected $use_curl = self::USE_CURL_AUTO;
/**
* @var bool
*
* This determines whether the multicall() method will try to take advantage of the system.multicall xml-rpc method
* to dispatch to the server an array of requests in a single http roundtrip or simply execute many consecutive http
* calls. Defaults to FALSE, but it will be enabled automatically on the first failure of execution of
* system.multicall.
*/
protected $no_multicall = false;
/**
* @var array
*
* List of http compression methods accepted by the client for responses.
* NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib.
*
* NNB: you can set it to any non-empty array for HTTP11+ and HTTPS, since in those cases it will be up to CURL to
* decide the compression methods it supports. You might check for the presence of 'zlib' in the output of
* curl_version() to determine whether compression is supported or not
*/
protected $accepted_compression = array();
/**
* @var string|null
*
* Name of compression scheme to be used for sending requests.
* Either null, 'gzip' or 'deflate'.
*/
protected $request_compression = '';
/**
* @var bool
*
* Whether to use persistent connections for http 1.1 and https. Value set at constructor time.
*/
protected $keepalive = false;
/**
* @var string[]
*
* Charset encodings that can be decoded without problems by the client. Value set at constructor time
*/
protected $accepted_charset_encodings = array();
/**
* @var string
*
* The charset encoding that will be used for serializing request sent by the client.
* It defaults to NULL, which means using US-ASCII and encoding all characters outside the ASCII printable range
* using their xml character entity representation (this has the benefit that line end characters will not be mangled
* in the transfer, a CR-LF will be preserved as well as a singe LF).
* Valid values are 'US-ASCII', 'UTF-8' and 'ISO-8859-1'.
* For the fastest mode of operation, set your both your app internal encoding and this to UTF-8.
*/
protected $request_charset_encoding = '';
/**
* @var string
*
* Decides the content of Response objects returned by calls to send() and multicall().
* Valid values are 'xmlrpcvals', 'phpvals' or 'xml'.
*
* Determines whether the value returned inside a Response object as results of calls to the send() and multicall()
* methods will be a Value object, a plain php value or a raw xml string.
* Allowed values are 'xmlrpcvals' (the default), 'phpvals' and 'xml'.
* To allow the user to differentiate between a correct and a faulty response, fault responses will be returned as
* Response objects in any case.
* Note that the 'phpvals' setting will yield faster execution times, but some of the information from the original
* response will be lost. It will be e.g. impossible to tell whether a particular php string value was sent by the
* server as an xml-rpc string or base64 value.
*/
protected $return_type = XMLParser::RETURN_XMLRPCVALS;
/**
* @var string
*
* Sent to servers in http headers. Value set at constructor time.
*/
protected $user_agent;
/**
* Additional headers to be included in the requests.
*
* @var string[]
*/
protected $extra_headers = array();
/**
* CURL handle: used for keep-alive
* @internal
*/
public $xmlrpc_curl_handle = null;
/**
* @var array
*/
protected static $options = array(
self::OPT_ACCEPTED_CHARSET_ENCODINGS,
self::OPT_ACCEPTED_COMPRESSION,
self::OPT_AUTH_TYPE,
self::OPT_CA_CERT,
self::OPT_CA_CERT_DIR,
self::OPT_CERT,
self::OPT_CERT_PASS,
self::OPT_COOKIES,
self::OPT_DEBUG,
self::OPT_EXTRA_CURL_OPTS,
self::OPT_EXTRA_SOCKET_OPTS,
self::OPT_KEEPALIVE,
self::OPT_KEY,
self::OPT_KEY_PASS,
self::OPT_NO_MULTICALL,
self::OPT_PASSWORD,
self::OPT_PROXY,
self::OPT_PROXY_AUTH_TYPE,
self::OPT_PROXY_PASS,
self::OPT_PROXY_USER,
self::OPT_PROXY_PORT,
self::OPT_REQUEST_CHARSET_ENCODING,
self::OPT_REQUEST_COMPRESSION,
self::OPT_RETURN_TYPE,
self::OPT_SSL_VERSION,
self::OPT_TIMEOUT,
self::OPT_USE_CURL,
self::OPT_USER_AGENT,
self::OPT_USERNAME,
self::OPT_VERIFY_HOST,
self::OPT_VERIFY_PEER,
self::OPT_EXTRA_HEADERS,
);
/**
* @param string $path either the PATH part of the xml-rpc server URL, or complete server URL (in which case you
* should use an empty string for all other parameters)
* e.g. /xmlrpc/server.php
* e.g. http://phpxmlrpc.sourceforge.net/server.php
* e.g. https://james:bond@secret.service.com:444/xmlrpcserver?agent=007
* e.g. h2://fast-and-secure-services.org/endpoint
* @param string $server the server name / ip address
* @param integer $port the port the server is listening on, when omitted defaults to 80 or 443 depending on
* protocol used
* @param string $method the http protocol variant: defaults to 'http'; 'https', 'http11', 'h2' and 'h2c' can
* be used if CURL is installed. The value set here can be overridden in any call to $this->send().
* Use 'h2' to make the lib attempt to use http/2 over a secure connection, and 'h2c'
* for http/2 without tls. Note that 'h2c' will not use the h2c 'upgrade' method, and be
* thus incompatible with any server/proxy not supporting http/2. This is because POST
* request are not compatible with h2c upgrade.
*/
public function __construct($path, $server = '', $port = '', $method = '')
{
// allow user to specify all params in $path
if ($server == '' && $port == '' && $method == '') {
$parts = parse_url($path);
$server = $parts['host'];
$path = isset($parts['path']) ? $parts['path'] : '';
if (isset($parts['query'])) {
$path .= '?' . $parts['query'];
}
if (isset($parts['fragment'])) {
$path .= '#' . $parts['fragment'];
}
if (isset($parts['port'])) {
$port = $parts['port'];
}
if (isset($parts['scheme'])) {
$method = $parts['scheme'];
}
if (isset($parts['user'])) {
$this->username = $parts['user'];
}
if (isset($parts['pass'])) {
$this->password = $parts['pass'];
}
}
if ($path == '' || $path[0] != '/') {
$this->path = '/' . $path;
} else {
$this->path = $path;
}
$this->server = $server;
if ($port != '') {
$this->port = $port;
}
if ($method != '') {
$this->method = $method;
}
// if ZLIB is enabled, let the client by default accept compressed responses
if (function_exists('gzinflate') || (
function_exists('curl_version') && (($info = curl_version()) &&
((is_string($info) && strpos($info, 'zlib') !== null) || isset($info['libz_version'])))
)
) {
$this->accepted_compression = array('gzip', 'deflate');
}
// keepalives: enabled by default
$this->keepalive = true;
// by default the xml parser can support these 3 charset encodings
$this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII');
// NB: this is disabled to avoid making all the requests sent huge... mbstring supports more than 80 charsets!
//$ch = $this->getCharsetEncoder();
//$this->accepted_charset_encodings = $ch->knownCharsets();
// initialize user_agent string
$this->user_agent = PhpXmlRpc::$xmlrpcName . ' ' . PhpXmlRpc::$xmlrpcVersion;
}
/**
* @param string $name see all the OPT_ constants
* @param mixed $value
* @return $this
* @throws ValueErrorException on unsupported option
*/
public function setOption($name, $value)
{
if (in_array($name, static::$options)) {
$this->$name = $value;
return $this;
}
throw new ValueErrorException("Unsupported option '$name'");
}
/**
* @param string $name see all the OPT_ constants
* @return mixed
* @throws ValueErrorException on unsupported option
*/
public function getOption($name)
{
if (in_array($name, static::$options)) {
return $this->$name;
}
throw new ValueErrorException("Unsupported option '$name'");
}
/**
* Returns the complete list of Client options, with their value.
* @return array
*/
public function getOptions()
{
$values = array();
foreach (static::$options as $opt) {
$values[$opt] = $this->getOption($opt);
}
return $values;
}
/**
* @param array $options key: any valid option (see all the OPT_ constants)
* @return $this
* @throws ValueErrorException on unsupported option
*/
public function setOptions($options)
{
foreach ($options as $name => $value) {
$this->setOption($name, $value);
}
return $this;
}
/**
* Enable/disable the echoing to screen of the xml-rpc responses received. The default is not to output anything.
*
* The debugging information at level 1 includes the raw data returned from the XML-RPC server it was querying
* (including bot HTTP headers and the full XML payload), and the PHP value the client attempts to create to
* represent the value returned by the server.
* At level 2, the complete payload of the xml-rpc request is also printed, before being sent to the server.
* At level -1, the Response objects returned by send() calls will not carry information about the http response's
* cookies, headers and body, which might save some memory
*
* This option can be very useful when debugging servers as it allows you to see exactly what the client sends and
* the server returns. Never leave it enabled for production!
*
* @param integer $level values -1, 0, 1 and 2 are supported
* @return $this
*/
public function setDebug($level)
{
$this->debug = $level;
return $this;
}
/**
* Sets the username and password for authorizing the client to the server.
*
* With the default (HTTP) transport, this information is used for HTTP Basic authorization.
* Note that username and password can also be set using the class constructor.
* With HTTP 1.1 and HTTPS transport, NTLM and Digest authentication protocols are also supported. To enable them use
* the constants CURLAUTH_DIGEST and CURLAUTH_NTLM as values for the auth type parameter.
*
* @param string $user username
* @param string $password password
* @param integer $authType auth type. See curl_setopt man page for supported auth types. Defaults to CURLAUTH_BASIC
* (basic auth). Note that auth types NTLM and Digest will only work if the Curl php
* extension is enabled.
* @return $this
*/
public function setCredentials($user, $password, $authType = 1)
{
$this->username = $user;
$this->password = $password;
$this->authtype = $authType;
return $this;
}
/**
* Set the optional certificate and passphrase used in SSL-enabled communication with a remote server.
*
* Note: to retrieve information about the client certificate on the server side, you will need to look into the
* environment variables which are set up by the webserver. Different webservers will typically set up different
* variables.
*
* @param string $cert the name of a file containing a PEM formatted certificate
* @param string $certPass the password required to use it
* @return $this
*/
public function setCertificate($cert, $certPass = '')
{
$this->cert = $cert;
$this->certpass = $certPass;
return $this;
}
/**
* Add a CA certificate to verify server with in SSL-enabled communication when SetSSLVerifypeer has been set to TRUE.
*
* See the php manual page about CURLOPT_CAINFO for more details.
*
* @param string $caCert certificate file name (or dir holding certificates)
* @param bool $isDir set to true to indicate cacert is a dir. defaults to false
* @return $this
*/
public function setCaCertificate($caCert, $isDir = false)
{
if ($isDir) {
$this->cacertdir = $caCert;
} else {
$this->cacert = $caCert;
}
return $this;
}
/**
* Set attributes for SSL communication: private SSL key.
*
* NB: does not work in older php/curl installs.
* Thanks to Daniel Convissor.
*
* @param string $key The name of a file containing a private SSL key
* @param string $keyPass The secret password needed to use the private SSL key
* @return $this
*/
public function setKey($key, $keyPass)
{
$this->key = $key;
$this->keypass = $keyPass;
return $this;
}
/**
* Set attributes for SSL communication: verify the remote host's SSL certificate, and cause the connection to fail
* if the cert verification fails.
*
* By default, verification is enabled.
* To specify custom SSL certificates to validate the server with, use the setCaCertificate method.
*
* @param bool $i enable/disable verification of peer certificate
* @return $this
* @deprecated use setOption
*/
public function setSSLVerifyPeer($i)
{
$this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
$this->verifypeer = $i;
return $this;
}
/**
* Set attributes for SSL communication: verify the remote host's SSL certificate's common name (CN).
*
* Note that support for value 1 has been removed in cURL 7.28.1
*
* @param int $i Set to 1 to only the existence of a CN, not that it matches
* @return $this
* @deprecated use setOption
*/
public function setSSLVerifyHost($i)
{
$this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
$this->verifyhost = $i;
return $this;
}
/**
* Set attributes for SSL communication: SSL version to use. Best left at 0 (default value): let PHP decide.
*
* @param int $i use CURL_SSLVERSION_ constants. When in socket mode, use the same values: 2 (SSLv2) to 7 (TLSv1.3),
* 0 for auto (note that old php versions do not support all TLS versions).
* Note that, in curl mode, the actual ssl version in use might be higher than requested.
* NB: please do not use any version lower than TLS 1.3 as they are considered insecure.
* @return $this
* @deprecated use setOption
*/
public function setSSLVersion($i)
{
$this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
$this->sslversion = $i;
return $this;
}
/**
* Set proxy info.
*
* NB: CURL versions before 7.11.10 cannot use a proxy to communicate with https servers.
*
* @param string $proxyHost
* @param string $proxyPort Defaults to 8080 for HTTP and 443 for HTTPS
* @param string $proxyUsername Leave blank if proxy has public access
* @param string $proxyPassword Leave blank if proxy has public access
* @param int $proxyAuthType defaults to CURLAUTH_BASIC (Basic authentication protocol); set to constant CURLAUTH_NTLM
* to use NTLM auth with proxy (has effect only when the client uses the HTTP 1.1 protocol)
* @return $this
*/
public function setProxy($proxyHost, $proxyPort, $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1)
{
$this->proxy = $proxyHost;
$this->proxyport = $proxyPort;
$this->proxy_user = $proxyUsername;
$this->proxy_pass = $proxyPassword;
$this->proxy_authtype = $proxyAuthType;
return $this;
}
/**
* Enables/disables reception of compressed xml-rpc responses.
*
* This requires the "zlib" extension to be enabled in your php install. If it is, by default xmlrpc_client
* instances will enable reception of compressed content.
* Note that enabling reception of compressed responses merely adds some standard http headers to xml-rpc requests.
* It is up to the xml-rpc server to return compressed responses when receiving such requests.
*
* @param string $compMethod either 'gzip', 'deflate', 'any' or ''
* @return $this
*/
public function setAcceptedCompression($compMethod)
{
if ($compMethod == 'any') {
$this->accepted_compression = array('gzip', 'deflate');
} elseif ($compMethod == false) {
$this->accepted_compression = array();
} else {
$this->accepted_compression = array($compMethod);
}
return $this;
}
/**
* Enables/disables http compression of xml-rpc request.
*
* This requires the "zlib" extension to be enabled in your php install.
* Take care when sending compressed requests: servers might not support them (and automatic fallback to
* uncompressed requests is not yet implemented).
*
* @param string $compMethod either 'gzip', 'deflate' or ''
* @return $this
* @deprecated use setOption
*/
public function setRequestCompression($compMethod)
{
$this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
$this->request_compression = $compMethod;
return $this;
}
/**
* Adds a cookie to list of cookies that will be sent to server with every further request (useful e.g. for keeping
* session info outside the xml-rpc payload).
*
* NB: by default all cookies set via this method are sent to the server, regardless of path/domain/port. Taking
* advantage of those values is left to the single developer.
*
* @param string $name nb: will not be escaped in the request's http headers. Take care not to use CTL chars or
* separators!
* @param string $value
* @param string $path
* @param string $domain
* @param int $port do not use! Cookies are not separated by port
* @return $this
*
* @todo check correctness of urlencoding cookie value (copied from php way of doing it, but php is generally sending
* response not requests. We do the opposite...)
* @todo strip invalid chars from cookie name? As per RFC 6265, we should follow RFC 2616, Section 2.2
* @todo drop/rename $port parameter. Cookies are not isolated by port!
* @todo feature-creep allow storing 'expires', 'secure', 'httponly' and 'samesite' cookie attributes (we could do
* as php, and allow $path to be an array of attributes...)
*/
public function setCookie($name, $value = '', $path = '', $domain = '', $port = null)
{
$this->cookies[$name]['value'] = rawurlencode($value);
if ($path || $domain || $port) {
$this->cookies[$name]['path'] = $path;
$this->cookies[$name]['domain'] = $domain;
$this->cookies[$name]['port'] = $port;
}
return $this;
}
/**
* Directly set cURL options, for extra flexibility (when in cURL mode).
*
* It allows e.g. to bind client to a specific IP interface / address.
*
* @param array $options
* @return $this
* @deprecated use setOption
*/
public function setCurlOptions($options)
{
$this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
$this->extracurlopts = $options;
return $this;
}
/**
* @param int $useCurlMode self::USE_CURL_ALWAYS, self::USE_CURL_AUTO or self::USE_CURL_NEVER
* In 'auto' mode, curl is picked up based on features used, such as fe. NTLM auth, or https
* @return $this
* @deprecated use setOption
*/
public function setUseCurl($useCurlMode)
{
$this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
$this->use_curl = $useCurlMode;
return $this;
}
/**
* Set user-agent string that will be used by this client instance in http headers sent to the server.
*
* The default user agent string includes the name of this library and the version number.
*
* @param string $agentString
* @return $this
* @deprecated use setOption
*/
public function setUserAgent($agentString)
{
$this->logDeprecation('Method ' . __METHOD__ . ' is deprecated');
$this->user_agent = $agentString;
return $this;
}
/**
* @param null|int $component allowed values: PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_PATH
* @return string|int Notes: the path component will include query string and fragment; NULL is a valid value for port
* (in which case the default port for http/https will be used); the url scheme component will
* reflect the `$method` used in the constructor, so it might not be http or https
* @throws ValueErrorException on unsupported component
*/
public function getUrl($component = null)
{
if (is_int($component) || ($component !== null && ctype_digit($component))) {
switch ($component) {
case PHP_URL_SCHEME:
return $this->method;
case PHP_URL_HOST:
return $this->server;
case PHP_URL_PORT:
return $this->port;
case PHP_URL_PATH:
return $this->path;
case '':
default:
throw new ValueErrorException("Unsupported component '$component'");
}
}
$url = $this->method . '://' . $this->server;
if ($this->port == 0 || ($this->port == 80 && in_array($this->method, array('http', 'http10', 'http11', 'http11_only', 'h2c'))) ||
($this->port == 443 && in_array($this->method, array('https', 'h2')))) {
return $url . $this->path;
} else {
return $url . ':' . $this->port . $this->path;
}
}
/**
* Send an xml-rpc request to the server.
*
* @param Request|Request[]|string $req The Request object, or an array of requests for using multicall, or the
* complete xml representation of a request.
* When sending an array of Request objects, the client will try to make use of
* a single 'system.multicall' xml-rpc method call to forward to the server all
* the requests in a single HTTP round trip, unless $this->no_multicall has
* been previously set to TRUE (see the multicall method below), in which case
* many consecutive xml-rpc requests will be sent. The method will return an
* array of Response objects in both cases.
* The third variant allows to build by hand (or any other means) a complete
* xml-rpc request message, and send it to the server. $req should be a string
* containing the complete xml representation of the request. It is e.g. useful
* when, for maximal speed of execution, the request is serialized into a
* string using the native php xml-rpc functions (see http://www.php.net/xmlrpc)
* @param integer $timeout deprecated. Connection timeout, in seconds, If unspecified, the timeout set with setOption
* will be used. If that is 0, a platform specific timeout will apply.
* This timeout value is passed to fsockopen(). It is also used for detecting server
* timeouts during communication (i.e. if the server does not send anything to the client
* for $timeout seconds, the connection will be closed). When in CURL mode, this is the
* CURL timeout.
* NB: in both CURL and Socket modes, some conditions might lead to the client not
* respecting the given timeout. Eg. if the network is not connected
* @param string $method deprecated. Use the same value in the constructor instead.
* Valid values are 'http', 'http11', 'https', 'h2' and 'h2c'. If left empty,
* the http protocol chosen during creation of the object will be used.
* Use 'h2' to make the lib attempt to use http/2 over a secure connection, and 'h2c'
* for http/2 without tls. Note that 'h2c' will not use the h2c 'upgrade' method, and be
* thus incompatible with any server/proxy not supporting http/2. This is because POST
* request are not compatible with h2c upgrade.
* You can also use 'http11_only' to force usage of curl with http 1.1 (no http2)
* @return Response|Response[] Note that the client will always return a Response object, even if the call fails
*
* @todo allow throwing exceptions instead of returning responses in case of failed calls and/or Fault responses
* @todo refactor: we now support many options besides connection timeout and http version to use. Why only privilege those?
*/
public function send($req, $timeout = 0, $method = '')
{
if ($method !== '' || $timeout !== 0) {
$this->logDeprecation("Using non-default values for arguments 'method' and 'timeout' when calling method " . __METHOD__ . ' is deprecated');
}
// if user does not specify http protocol, use native method of this client
// (i.e. method set during call to constructor)
if ($method == '') {
$method = $this->method;
}
if ($timeout == 0) {
$timeout = $this->timeout;
}
if (is_array($req)) {
// $req is an array of Requests
/// @todo switch to the new syntax for multicall
return $this->multicall($req, $timeout, $method);
} elseif (is_string($req)) {
$n = new static::$requestClass('');
/// @todo we should somehow allow the caller to declare a custom contenttype too, esp. for the charset declaration
$n->setPayload($req);
$req = $n;
}
// where req is a Request
$req->setDebug($this->debug);
/// @todo move to its own function, to make it easier to change in subclasses
/// @todo we could be smarter about this:
/// - not force usage of curl if it is not present
/// - not force usage of curl for https (minor BC)
/// - use the presence of curl_extra_opts or socket_extra_opts as a hint
$useCurl = ($this->use_curl == self::USE_CURL_ALWAYS) || ($this->use_curl == self::USE_CURL_AUTO && (
in_array($method, array('https', 'http11', 'http11_only', 'h2c', 'h2')) ||
($this->username != '' && $this->authtype != 1) ||
($this->proxy != '' && $this->proxy_user != '' && $this->proxy_authtype != 1)
// uncomment the following if not forcing curl always for 'https'
//|| ($this->sslversion == 7 && PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION == '7.3')
//|| ($this->sslversion != 0 && PHP_MAJOR_VERSION < 6)
));
// BC - we go through sendPayloadCURL/sendPayloadSocket in case some subclass reimplemented those
if ($useCurl) {
$r = $this->sendPayloadCURL(
$req,
$this->server,
$this->port,
$timeout,
$this->username,
$this->password,
$this->authtype,
$this->cert,
$this->certpass,
$this->cacert,
$this->cacertdir,
$this->proxy,
$this->proxyport,
$this->proxy_user,
$this->proxy_pass,
$this->proxy_authtype,
// BC - http11 was used to force enabling curl
$method == 'http11' ? 'http' : ($method == 'http11_only' ? 'http11' : $method),
$this->keepalive,
$this->key,
$this->keypass,
$this->sslversion
);
} else {
$r = $this->sendPayloadSocket(
$req,
$this->server,
$this->port,
$timeout,
$this->username,
$this->password,
$this->authtype,
$this->cert,
$this->certpass,
$this->cacert,
$this->cacertdir,
$this->proxy,
$this->proxyport,
$this->proxy_user,
$this->proxy_pass,
$this->proxy_authtype,
$method == 'http11_only' ? 'http11' : $method,
$this->key,
$this->keypass,
$this->sslversion
);
}
return $r;
}
/**
* @param Request $req
* @param string $method
* @param string $server
* @param int $port
* @param string $path
* @param array $opts
* @return Response
*/
protected function sendViaSocket($req, $method, $server, $port, $path, $opts)
{
/// @todo log a warning if passed an unsupported method
// Only create the payload if it was not created previously
/// @todo what if the request's payload was created with a different encoding?
/// Also, if we do not call serialize(), the request will not set its content-type to have the charset declared
$payload = $req->getPayload();
if (empty($payload)) {
$payload = $req->serialize($opts['request_charset_encoding']);
}
// Deflate request body and set appropriate request headers
$encodingHdr = '';
if ($opts['request_compression'] == 'gzip' || $opts['request_compression'] == 'deflate') {
if ($opts['request_compression'] == 'gzip' && function_exists('gzencode')) {
$a = @gzencode($payload);
if ($a) {
$payload = $a;
$encodingHdr = "Content-Encoding: gzip\r\n";
} else {
$this->getLogger()->warning('XML-RPC: ' . __METHOD__ . ': gzencode failure in compressing request');
}
} else if (function_exists('gzcompress')) {
$a = @gzcompress($payload);
if ($a) {
$payload = $a;
$encodingHdr = "Content-Encoding: deflate\r\n";
} else {
$this->getLogger()->warning('XML-RPC: ' . __METHOD__ . ': gzcompress failure in compressing request');
}
} else {
$this->getLogger()->warning('XML-RPC: ' . __METHOD__ . ': desired request compression method is unsupported by this PHP install');
}
} else {
if ($opts['request_compression'] != '') {
$this->getLogger()->warning('XML-RPC: ' . __METHOD__ . ': desired request compression method is unsupported');
}
}
// thanks to Grant Rauscher
$credentials = '';
if ($opts['username'] != '') {
if ($opts['authtype'] != 1) {
$this->getLogger()->error('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth is supported with HTTP 1.0');
return new static::$responseClass(0, PhpXmlRpc::$xmlrpcerr['unsupported_option'],
PhpXmlRpc::$xmlrpcerr['unsupported_option'] . ': only Basic auth is supported with HTTP 1.0');
}
$credentials = 'Authorization: Basic ' . base64_encode($opts['username'] . ':' . $opts['password']) . "\r\n";
}
$acceptedEncoding = '';
if (is_array($opts['accepted_compression']) && count($opts['accepted_compression'])) {
$acceptedEncoding = 'Accept-Encoding: ' . implode(', ', $opts['accepted_compression']) . "\r\n";
}
if ($port == 0) {
$port = ($method === 'https') ? 443 : 80;
}
$proxyCredentials = '';
if ($opts['proxy']) {
if ($opts['proxyport'] == 0) {
$opts['proxyport'] = 8080;
}
$connectServer = $opts['proxy'];
$connectPort = $opts['proxyport'];
$transport = 'tcp';
$protocol = $method;
if ($method === 'http10' || $method === 'http11') {
$protocol = 'http';