1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WooCommerce Basic Authentication |
4
|
|
|
* |
5
|
|
|
* @category HttpClient |
6
|
|
|
* @package Automattic/WooCommerce |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Automattic\WooCommerce\HttpClient; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Basic Authentication class. |
13
|
|
|
* |
14
|
|
|
* @package Automattic/WooCommerce |
15
|
|
|
*/ |
16
|
|
|
class BasicAuth |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* cURL handle. |
20
|
|
|
* |
21
|
|
|
* @var resource |
22
|
|
|
*/ |
23
|
|
|
protected $ch; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Consumer key. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $consumerKey; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Consumer secret. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $consumerSecret; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Do query string auth. |
41
|
|
|
* |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $doQueryString; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Request parameters. |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $parameters; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Initialize Basic Authentication class. |
55
|
|
|
* |
56
|
|
|
* @param resource $ch cURL handle. |
57
|
|
|
* @param string $consumerKey Consumer key. |
58
|
|
|
* @param string $consumerSecret Consumer Secret. |
59
|
|
|
* @param bool $doQueryString Do or not query string auth. |
60
|
|
|
* @param array $parameters Request parameters. |
61
|
|
|
*/ |
62
|
|
|
public function __construct($ch, $consumerKey, $consumerSecret, $doQueryString, $parameters = []) |
63
|
|
|
{ |
64
|
|
|
$this->ch = $ch; |
65
|
|
|
$this->consumerKey = $consumerKey; |
66
|
|
|
$this->consumerSecret = $consumerSecret; |
67
|
|
|
$this->doQueryString = $doQueryString; |
68
|
|
|
$this->parameters = $parameters; |
69
|
|
|
|
70
|
|
|
$this->processAuth(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Process auth. |
75
|
|
|
*/ |
76
|
|
|
protected function processAuth() |
77
|
|
|
{ |
78
|
|
|
if ($this->doQueryString) { |
79
|
|
|
$this->parameters['consumer_key'] = $this->consumerKey; |
80
|
|
|
$this->parameters['consumer_secret'] = $this->consumerSecret; |
81
|
|
|
} else { |
82
|
|
|
\curl_setopt($this->ch, CURLOPT_USERPWD, $this->consumerKey . ':' . $this->consumerSecret); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get parameters. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function getParameters() |
92
|
|
|
{ |
93
|
|
|
return $this->parameters; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|