a8523bcf40194ad5dfeed8628656e77f6ac9c3bb
[WP-Plugins/glm-member-db.git] /
1 <?php
2 namespace PayPal\Core;
3 use PayPal\Exception\PPConfigurationException;
4 class PPHttpConfig {
5
6         /**
7          * Some default options for curl
8          * These are typically overridden by PPConnectionManager
9          */
10         public static $DEFAULT_CURL_OPTS = array(
11                 CURLOPT_SSLVERSION => 3,
12                 CURLOPT_CONNECTTIMEOUT => 10,
13                 CURLOPT_RETURNTRANSFER => TRUE,
14                 CURLOPT_TIMEOUT        => 60,   // maximum number of seconds to allow cURL functions to execute
15                 CURLOPT_USERAGENT      => 'PayPal-PHP-SDK',
16                 CURLOPT_HTTPHEADER     => array(),
17                 CURLOPT_SSL_VERIFYHOST => 2,
18                 CURLOPT_SSL_VERIFYPEER => 1
19         );      
20         
21         const HEADER_SEPARATOR = ';';   
22         const HTTP_GET = 'GET';
23         const HTTP_POST = 'POST';
24
25         private $headers = array();
26
27         private $curlOptions;
28
29         private $url;
30
31         private $method;
32         /***
33          * Number of times to retry a failed HTTP call
34          */
35         private $retryCount;
36
37         /**
38          * 
39          * @param string $url
40          * @param string $method  HTTP method (GET, POST etc) defaults to POST
41          */
42         public function __construct($url=null, $method=self::HTTP_POST) {
43                 $this->url = $url;
44                 $this->method = $method;
45                 $this->curlOptions = self::$DEFAULT_CURL_OPTS;
46         }
47         
48         public function getUrl() {
49                 return $this->url;
50         }
51         
52         public function getMethod() {
53                 return $this->method;
54         }
55         
56         public function getHeaders() {
57                 return $this->headers;
58         }
59         
60         public function getHeader($name) {
61                 if(array_key_exists($name, $this->headers)) {
62                         return $this->headers[$name];
63                 }
64                 return NULL;
65         }
66
67         public function setUrl($url) {
68                 $this->url = $url;
69         }
70         
71         public function setHeaders(array $headers) {
72                 $this->headers = $headers;
73         }
74
75         public function addHeader($name, $value, $overWrite=true) {
76                 if(!array_key_exists($name, $this->headers) || $overWrite) {
77                         $this->headers[$name] = $value;
78                 } else {
79                         $this->headers[$name] = $this->headers[$name] . HEADER_SEPARATOR . $value;                      
80                 }
81         }
82         
83         public function removeHeader($name) {
84                 unset($this->headers[$name]);
85         }
86
87         
88         
89         public function getCurlOptions() {
90                 return $this->curlOptions;
91         }
92
93         public function addCurlOption($name, $value) {
94                 $this->curlOptions[$name] = $value;
95         }
96
97         public function setCurlOptions($options) {
98                 $this->curlOptions = $options;
99         }
100
101         
102
103         /**
104          * Set ssl parameters for certificate based client authentication
105          *
106          * @param string $certPath - path to client certificate file (PEM formatted file)
107          */
108         public function setSSLCert($certPath, $passPhrase=NULL) {               
109                 $this->curlOptions[CURLOPT_SSLCERT] = realpath($certPath);
110                 if(isset($passPhrase) && trim($passPhrase) != "") {
111                         $this->curlOptions[CURLOPT_SSLCERTPASSWD] = $passPhrase;
112                 }
113         }
114
115         /**
116          * Set connection timeout in seconds
117          * @param integer $timeout
118          */
119         public function setHttpTimeout($timeout) {
120                 $this->curlOptions[CURLOPT_CONNECTTIMEOUT] = $timeout;
121         }
122
123         /**
124          * Set HTTP proxy information
125          * @param string $proxy
126          * @throws PPConfigurationException
127          */
128         public function setHttpProxy($proxy) {
129                 $urlParts = parse_url($proxy);
130                 if($urlParts == false || !array_key_exists("host", $urlParts))
131                         throw new PPConfigurationException("Invalid proxy configuration ".$proxy);
132
133                 $this->curlOptions[CURLOPT_PROXY] = $urlParts["host"];
134                 if(isset($urlParts["port"]))
135                         $this->curlOptions[CURLOPT_PROXY] .=  ":" . $urlParts["port"];
136                 if(isset($urlParts["user"]))
137                         $this->curlOptions[URLOPT_PROXYUSERPWD] = $urlParts["user"] . ":" . $urlParts["pass"];
138         }       
139         
140         /**
141          * @param integer $retry
142          */
143         public function setHttpRetryCount($retryCount) {
144                 $this->retryCount = $retryCount;
145         }       
146
147         public function getHttpRetryCount() {
148                 return $this->retryCount;
149         }
150         
151         /**
152          * Sets the User-Agent string on the HTTP request
153          * @param string $userAgentString
154          */
155         public function setUserAgent($userAgentString) {
156                 $this->curlOptions[CURLOPT_USERAGENT] = $userAgentString;
157         }
158 }