1 <?php
2
3 namespace PayPal\Core;
4
5 use PayPal\Exception\PayPalConfigurationException;
6
7 8 9 10 11 12
13 class PayPalHttpConfig
14 {
15 16 17 18 19 20
21 public static $defaultCurlOptions = array(
22 CURLOPT_SSLVERSION => 6,
23 CURLOPT_CONNECTTIMEOUT => 10,
24 CURLOPT_RETURNTRANSFER => true,
25 CURLOPT_TIMEOUT => 60,
26 CURLOPT_USERAGENT => 'PayPal-PHP-SDK',
27 CURLOPT_HTTPHEADER => array(),
28 CURLOPT_SSL_VERIFYHOST => 2,
29 CURLOPT_SSL_VERIFYPEER => 1,
30 CURLOPT_SSL_CIPHER_LIST => 'TLSv1:TLSv1.2'
31
32
33 );
34
35 const HEADER_SEPARATOR = ';';
36 const HTTP_GET = 'GET';
37 const HTTP_POST = 'POST';
38
39 private $headers = array();
40
41 private $curlOptions;
42
43 private $url;
44
45 private $method;
46
47 48 49
50 private $retryCount = 0;
51
52 53 54 55 56 57 58
59 public function __construct($url = null, $method = self::HTTP_POST, $configs = array())
60 {
61 $this->url = $url;
62 $this->method = $method;
63 $this->curlOptions = $this->getHttpConstantsFromConfigs($configs, 'http.') + self::$defaultCurlOptions;
64
65 $curl = curl_version();
66 $sslVersion = isset($curl['ssl_version']) ? $curl['ssl_version'] : '';
67 if($sslVersion && substr_compare($sslVersion, "NSS/", 0, strlen("NSS/")) === 0) {
68
69 $this->removeCurlOption(CURLOPT_SSL_CIPHER_LIST);
70 }
71 }
72
73 74 75 76 77
78 public function getUrl()
79 {
80 return $this->url;
81 }
82
83 84 85 86 87
88 public function getMethod()
89 {
90 return $this->method;
91 }
92
93 94 95 96 97
98 public function getHeaders()
99 {
100 return $this->headers;
101 }
102
103 104 105 106 107 108
109 public function getHeader($name)
110 {
111 if (array_key_exists($name, $this->headers)) {
112 return $this->headers[$name];
113 }
114 return null;
115 }
116
117 118 119 120 121
122 public function setUrl($url)
123 {
124 $this->url = $url;
125 }
126
127 128 129 130 131
132 public function setHeaders(array $headers = array())
133 {
134 $this->headers = $headers;
135 }
136
137 138 139 140 141 142 143
144 public function addHeader($name, $value, $overWrite = true)
145 {
146 if (!array_key_exists($name, $this->headers) || $overWrite) {
147 $this->headers[$name] = $value;
148 } else {
149 $this->headers[$name] = $this->headers[$name] . self::HEADER_SEPARATOR . $value;
150 }
151 }
152
153 154 155 156 157
158 public function removeHeader($name)
159 {
160 unset($this->headers[$name]);
161 }
162
163 164 165 166 167
168 public function getCurlOptions()
169 {
170 return $this->curlOptions;
171 }
172
173 174 175 176 177 178
179 public function addCurlOption($name, $value)
180 {
181 $this->curlOptions[$name] = $value;
182 }
183
184 185 186 187 188
189 public function removeCurlOption($name)
190 {
191 unset($this->curlOptions[$name]);
192 }
193
194 195 196 197 198
199 public function setCurlOptions($options)
200 {
201 $this->curlOptions = $options;
202 }
203
204 205 206 207 208 209
210 public function setSSLCert($certPath, $passPhrase = null)
211 {
212 $this->curlOptions[CURLOPT_SSLCERT] = realpath($certPath);
213 if (isset($passPhrase) && trim($passPhrase) != "") {
214 $this->curlOptions[CURLOPT_SSLCERTPASSWD] = $passPhrase;
215 }
216 }
217
218 219 220 221 222
223 public function setHttpTimeout($timeout)
224 {
225 $this->curlOptions[CURLOPT_CONNECTTIMEOUT] = $timeout;
226 }
227
228 229 230 231 232 233
234 public function setHttpProxy($proxy)
235 {
236 $urlParts = parse_url($proxy);
237 if ($urlParts == false || !array_key_exists("host", $urlParts)) {
238 throw new PayPalConfigurationException("Invalid proxy configuration " . $proxy);
239 }
240 $this->curlOptions[CURLOPT_PROXY] = $urlParts["host"];
241 if (isset($urlParts["port"])) {
242 $this->curlOptions[CURLOPT_PROXY] .= ":" . $urlParts["port"];
243 }
244 if (isset($urlParts["user"])) {
245 $this->curlOptions[CURLOPT_PROXYUSERPWD] = $urlParts["user"] . ":" . $urlParts["pass"];
246 }
247 }
248
249 250 251 252 253
254 public function setHttpRetryCount($retryCount)
255 {
256 $this->retryCount = $retryCount;
257 }
258
259 260 261 262 263
264 public function getHttpRetryCount()
265 {
266 return $this->retryCount;
267 }
268
269 270 271 272 273
274 public function setUserAgent($userAgentString)
275 {
276 $this->curlOptions[CURLOPT_USERAGENT] = $userAgentString;
277 }
278
279 280 281 282 283 284 285
286 public function getHttpConstantsFromConfigs($configs = array(), $prefix)
287 {
288 $arr = array();
289 if ($prefix != null && is_array($configs)) {
290 foreach ($configs as $k => $v) {
291
292 if (substr($k, 0, strlen($prefix)) === $prefix) {
293 $newKey = ltrim($k, $prefix);
294 if (defined($newKey)) {
295 $arr[constant($newKey)] = $v;
296 }
297 }
298 }
299 }
300 return $arr;
301 }
302 }
303