]> git.mxchange.org Git - friendica.git/blob - src/Network/Curl.php
7cab25ee877333c2caa8f72b6c29ef6cb67e24de
[friendica.git] / src / Network / Curl.php
1 <?php
2
3 namespace Friendica\Network;
4
5
6 use Friendica\Network\HTTPException\InternalServerErrorException;
7
8 /**
9  * A content class for Curl call results
10  */
11 class Curl
12 {
13         /**
14          * @var int HTTP return code or 0 if timeout or failure
15          */
16         private $returnCode;
17
18         /**
19          * @var string the content type of the Curl call
20          */
21         private $contentType;
22
23         /**
24          * @var string the HTTP headers of the Curl call
25          */
26         private $header;
27
28         /**
29          * @var boolean true (if HTTP 2xx result) or false
30          */
31         private $isSuccess;
32
33         /**
34          * @var string the URL which was called
35          */
36         private $url;
37
38         /**
39          * @var string in case of redirect, content was finally retrieved from this URL
40          */
41         private $redirectUrl;
42
43         /**
44          * @var string fetched content
45          */
46         private $body;
47
48         /**
49          * @var array some informations about the fetched data
50          */
51         private $info;
52
53         /**
54          * @var boolean true if the URL has a redirect
55          */
56         private $isRedirectUrl;
57
58         /**
59          * @var boolean true if the curl request timed out
60          */
61         private $isTimeout;
62
63         /**
64          * @var int the error number or 0 (zero) if no error
65          */
66         private $errorNumber;
67
68         /**
69          * @var string the error message or '' (the empty string) if no
70          */
71         private $error;
72
73         /**
74          * Creates an errored CURL response
75          *
76          * @param string $url optional URL
77          *
78          * @return Curl a CURL with error response
79          */
80         public static function createErrorCurl($url = '')
81         {
82                 return new Curl(
83                         $url,
84                         '',
85                         [ 'http_code' => 0 ]
86                         );
87         }
88
89         /**
90          * Curl constructor.
91          * @param string $url the URL which was called
92          * @param string $result the result of the curl execution
93          * @param array $info an additional info array
94          * @param int $errorNumber the error number or 0 (zero) if no error
95          * @param string $error the error message or '' (the empty string) if no
96          *
97          * @throws InternalServerErrorException when HTTP code of the CURL response is missing
98          */
99         public function __construct($url, $result, $info, $errorNumber = 0, $error = '')
100         {
101                 if (!array_key_exists('http_code', $info)) {
102                         throw new InternalServerErrorException('CURL response doesn\'t contains a response HTTP code');
103                 }
104
105                 $this->returnCode = $info['http_code'];
106                 $this->url = $url;
107                 $this->info = $info;
108                 $this->errorNumber = $errorNumber;
109                 $this->error = $error;
110
111                 logger($url . ': ' . $this->returnCode . " " . $result, LOGGER_DATA);
112
113                 $this->parseBodyHeader($result);
114                 $this->checkSuccess();
115                 $this->checkRedirect();
116                 $this->checkInfo();
117         }
118
119         private function parseBodyHeader($result)
120         {
121                 // Pull out multiple headers, e.g. proxy and continuation headers
122                 // allow for HTTP/2.x without fixing code
123
124                 $header = '';
125                 $base = $result;
126                 while (preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/', $base)) {
127                         $chunk = substr($base, 0, strpos($base, "\r\n\r\n") + 4);
128                         $header .= $chunk;
129                         $base = substr($base, strlen($chunk));
130                 }
131
132                 $this->body = substr($result, strlen($header));
133                 $this->header = $header;
134         }
135
136         private function checkSuccess()
137         {
138                 $this->isSuccess = ($this->returnCode >= 200 && $this->returnCode <= 299) || $this->errorNumber == 0;
139
140                 if (!$this->isSuccess) {
141                         logger('error: ' . $this->url . ': ' . $this->returnCode . ' - ' . $this->error, LOGGER_INFO);
142                         logger('debug: ' . print_r($this->info, true), LOGGER_DATA);
143                 }
144
145                 if (!$this->isSuccess && $this->errorNumber == CURLE_OPERATION_TIMEDOUT) {
146                         $this->isTimeout = true;
147                 } else {
148                         $this->isTimeout = false;
149                 }
150         }
151
152         private function checkRedirect()
153         {
154                 if (!array_key_exists('url', $this->info)) {
155                         $this->redirectUrl = '';
156                 } else {
157                         $this->redirectUrl = $this->info['url'];
158                 }
159
160                 if ($this->returnCode == 301 || $this->returnCode == 302 || $this->returnCode == 303 || $this->returnCode== 307) {
161                         $new_location_info = (!array_key_exists('redirect_url', $this->info) ? '' : @parse_url($this->info['redirect_url']));
162                         $old_location_info = (!array_key_exists('url', $this->info) ? '' : @parse_url($this->info['url']));
163
164                         $this->redirectUrl = $new_location_info;
165
166                         if (empty($new_location_info['path']) && !empty($new_location_info['host'])) {
167                                 $this->redirectUrl = $new_location_info['scheme'] . '://' . $new_location_info['host'] . $old_location_info['path'];
168                         }
169
170                         $matches = [];
171
172                         if (preg_match('/(Location:|URI:)(.*?)\n/i', $this->header, $matches)) {
173                                 $this->redirectUrl = trim(array_pop($matches));
174                         }
175                         if (strpos($this->redirectUrl, '/') === 0) {
176                                 $this->redirectUrl = $old_location_info["scheme"] . "://" . $old_location_info["host"] . $this->redirectUrl;
177                         }
178                         $old_location_query = @parse_url($this->url, PHP_URL_QUERY);
179
180                         if ($old_location_query != '') {
181                                 $this->redirectUrl .= '?' . $old_location_query;
182                         }
183
184                         $this->isRedirectUrl = filter_var($this->redirectUrl, FILTER_VALIDATE_URL);
185                 } else {
186                         $this->isRedirectUrl = false;
187                 }
188         }
189
190         private function checkInfo()
191         {
192                 if (isset($this->info['content_type'])) {
193                         $this->contentType = $this->info['content_type'];
194                 } else {
195                         $this->contentType = '';
196                 }
197         }
198
199         /**
200          * Gets the Curl Code
201          *
202          * @return string The Curl Code
203          */
204         public function getReturnCode()
205         {
206                 return $this->returnCode;
207         }
208
209         /**
210          * Returns the Curl Content Type
211          *
212          * @return string the Curl Content Type
213          */
214         public function getContentType()
215         {
216                 return $this->contentType;
217         }
218
219         /**
220          * Returns the Curl headers
221          *
222          * @return string the Curl headers
223          */
224         public function getHeader()
225         {
226                 return $this->header;
227         }
228
229         /**
230          * @return bool
231          */
232         public function isSuccess()
233         {
234                 return $this->isSuccess;
235         }
236
237         /**
238          * @return string
239          */
240         public function getUrl()
241         {
242                 return $this->url;
243         }
244
245         /**
246          * @return string
247          */
248         public function getRedirectUrl()
249         {
250                 return $this->redirectUrl;
251         }
252
253         /**
254          * @return string
255          */
256         public function getBody()
257         {
258                 return $this->body;
259         }
260
261         /**
262          * @return array
263          */
264         public function getInfo()
265         {
266                 return $this->info;
267         }
268
269         /**
270          * @return bool
271          */
272         public function isRedirectUrl()
273         {
274                 return $this->isRedirectUrl;
275         }
276
277         /**
278          * @return int
279          */
280         public function getErrorNumber()
281         {
282                 return $this->errorNumber;
283         }
284
285         /**
286          * @return string
287          */
288         public function getError()
289         {
290                 return $this->error;
291         }
292
293         /**
294          * @return bool
295          */
296         public function isTimeout()
297         {
298                 return $this->isTimeout;
299         }
300 }