2c3952038e63245f2153bce1804efe459eb2402d
[WP-Plugins/glm-member-db-registrations.git] /
1 <?php
2 namespace PayPal\Core;
3 use PayPal\Core\PPLoggingManager;
4 use PayPal\Exception\PPConfigurationException;
5 use PayPal\Exception\PPConnectionException;
6
7 /**
8  * A wrapper class based on the curl extension.
9  * Requires the PHP curl module to be enabled.
10  * See for full requirements the PHP manual: http://php.net/curl
11  */
12
13
14 class PPHttpConnection
15 {
16
17         private $httpConfig;
18
19         /**
20          * HTTP status codes for which a retry must be attempted
21          */
22         private static $retryCodes = array('401', '403', '404', );
23
24         private $logger;
25
26         public function __construct($httpConfig, $config)
27         {
28                 if( !function_exists("curl_init") ) {
29                         throw new PPConfigurationException("Curl module is not available on this system");
30                 }
31                 $this->httpConfig = $httpConfig;
32                 $this->logger = new PPLoggingManager(__CLASS__, $config);
33         }       
34
35         private function getHttpHeaders() {
36                 
37                 $ret = array();
38                 foreach($this->httpConfig->getHeaders() as $k=>$v) {
39                         $ret[] = "$k: $v";
40                 }
41                 return $ret;
42         }
43         
44         /**
45          * Executes an HTTP request
46          *
47          * @param string $data query string OR POST content as a string
48          * @throws PPConnectionException
49          */
50         public function execute($data) {
51                 $this->logger->fine("Connecting to " . $this->httpConfig->getUrl());                    
52                 $this->logger->fine("Payload " . $data);
53
54                 $ch = curl_init($this->httpConfig->getUrl());
55                 curl_setopt_array($ch, $this->httpConfig->getCurlOptions());            
56                 curl_setopt($ch, CURLOPT_URL, $this->httpConfig->getUrl());             
57                 curl_setopt($ch, CURLOPT_HEADER, false);
58                 curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHttpHeaders());
59                 
60                 switch($this->httpConfig->getMethod()) {
61                         case 'POST':
62                                 curl_setopt($ch, CURLOPT_POST, true);
63                                 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
64                                 break;
65                 }
66                 if($this->httpConfig->getMethod() != NULL) {
67                         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->httpConfig->getMethod());
68                 }
69                 foreach($this->getHttpHeaders() as $header) {
70                         //TODO: Strip out credentials and other secure info when logging.
71                         $this->logger->info("Adding header $header");
72                 }
73                 $result = curl_exec($ch);
74                 if ((curl_errno($ch) == 60) && ($this->httpConfig->getMethod() != "DELETE")) {
75                         $this->logger->info("Invalid or no certificate authority found - Retrying using bundled CA certs file");
76                         curl_setopt($ch, CURLOPT_CAINFO,
77                         dirname(__FILE__) . '/cacert.pem');
78                         $result = curl_exec($ch);
79                 }
80                 $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
81                 $retries = 0;
82                 if(in_array($httpStatus, self::$retryCodes) && $this->httpConfig->getHttpRetryCount() != null) {
83                         $this->logger->info("Got $httpStatus response from server. Retrying");
84
85                         do      {
86                                 $result = curl_exec($ch);
87                                 $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
88                         } while (in_array($httpStatus, self::$retryCodes) && (++$retries < $this->httpConfig->getHttpRetryCount()) );
89                 }
90                 if ( curl_errno($ch) ) {
91                         $ex = new PPConnectionException($this->httpConfig->getUrl(), curl_error($ch), curl_errno($ch));
92                         curl_close($ch);
93                         throw $ex;
94                 }
95
96                 curl_close($ch);
97
98                 if(in_array($httpStatus, self::$retryCodes)) {
99                         $ex = new PPConnectionException($this->httpConfig->getUrl() , 
100                                         "Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}. Retried $retries times.");
101                         $ex->setData($result);
102                         throw $ex;
103                 } else if($httpStatus < 200 || $httpStatus >=300) {
104                         $ex = new PPConnectionException($this->httpConfig->getUrl() , 
105                                         "Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}.");
106                         $ex->setData($result);
107                         throw $ex;
108                 }
109                 return $result;
110         }
111
112 }