-
Notifications
You must be signed in to change notification settings - Fork 98
/
RemoteGet.php
729 lines (544 loc) · 18.5 KB
/
RemoteGet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
<?php
namespace gp\tool{
defined('is_running') or die('Not an entry point...');
class RemoteGet{
public static $redirected;
public static $maxlength = -1; // The maximum bytes to read. eg: stream_get_contents($handle, $maxlength)
public static $debug;
public static $methods = array('stream','curl','fopen','fsockopen');
protected $url_array = array();
protected $body = '';
protected $headers = '';
protected $bytes_written_total = 0;
/* determine if the functions exist for fetching remote files,
* test is done in order of preference
*
* notes from wordpress http.php
* The order for the GET/HEAD requests are Streams, HTTP Extension, Fopen,
* and finally Fsockopen. fsockopen() is used last, because it has the most
* overhead in its implementation. There isn't any real way around it, since
* redirects have to be supported, much the same way the other transports
* also handle redirects.
*
* @return mixed string indicates the method, False indicates it's not compatible
*/
public static function Test(){
foreach(self::$methods as $method){
if( self::Supported($method) ){
return $method;
}
}
return false;
}
/**
* Determine if a specific method is supported
*
*/
public static function Supported($method){
if( \gp\tool::IniGet('safe_mode') ){
return false;
}
return false;
switch($method){
case 'fsockopen':
return function_exists('fsockopen');
case 'curl';
return function_exists('curl_init') && function_exists('curl_exec');
}
//stream and fopen
return \gp\tool::IniGet('allow_url_fopen');
}
/**
* Return response only if successful, otherwise return false
*
*/
public static function Get_Successful($url,$args=array()){
$getter = new \gp\tool\RemoteGet();
$result = $getter->Get($url,$args);
if( (int)$result['response']['code'] >= 200 && (int)$result['response']['code'] < 300 ){
return $result['body'];
}
return false;
}
/**
* Attempt to get the resource at $url
* Loop through all potential methods until successful
*/
public function Get($url,$args=array()){
self::$debug = array();
self::$debug['Redir'] = 0;
self::$debug['FailedMethods'] = '';
self::$debug['NotSupported'] = '';
self::$redirected = null;
return $this->_get($url,$args);
}
protected function _get($url, $args = array()){
$url = rawurldecode($url);
$url = str_replace(' ','%20',$url); //spaces in the url can make the request fail
$url = self::FixScheme($url);
$this->url_array = self::ParseUrl($url);
if( $this->url_array === false ){
return false;
}
//arguments
$defaults = array(
'method' => 'GET',
'timeout' => 5,
'redirection' => 5,
'httpversion' => '1.0',
'user-agent' => 'Mozilla/5.0 (Typesetter RemoteGet) ',
//'user-agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0',
//could be added
//'blocking' => true,
//'headers' => array(),
//'body' => null,
//'cookies' => array(),
);
$args += $defaults;
foreach(self::$methods as $method){
if( !self::Supported($method) ){
self::$debug['NotSupported'] .= $method.',';
continue;
}
$result = self::GetMethod($method,$url,$args);
if( $result === false ){
self::$debug['FailedMethods'] .= $method.',';
return false;
}
self::$debug['Method'] = $method;
self::$debug['Len'] = strlen($result['body']);
return $result;
}
return false;
}
public function GetMethod($method,$url,$args=array()){
$func = $method.'_request';
if( method_exists($this,$func) ){
return $this->$func( $url, $args );
}
return false;
}
/**
* Fetch a url using php's stream_get_contents() function
*
*/
public function stream_request($url,$r){
//create context
$arrContext = array();
$arrContext['http'] = array(
'method' => 'GET',
'user_agent' => $r['user-agent'],
'max_redirects' => $r['redirection'],
'protocol_version' => (float) $r['httpversion'],
'timeout' => $r['timeout'],
);
if( isset($r['http']) ){
$arrContext['http'] = $r['http'] + $arrContext['http'];
}
if( isset($r['headers']) ){
$arrContext['http']['header'] = '';
foreach($r['headers'] as $hk => $hv){
$arrContext['http']['header'] .= $hk.': '.$hv."\r\n";
}
$arrContext['http']['header'] = trim($arrContext['http']['header']);
}
$context = stream_context_create($arrContext);
$handle = fopen($url, 'r', false, $context);
if( !$handle ){
self::$debug['stream'] = 'no handle';
return false;
}
self::stream_timeout($handle,$r['timeout']);
$strResponse = stream_get_contents($handle, self::$maxlength);
$theHeaders = self::StreamHeaders($handle);
fclose($handle);
$processedHeaders = self::processHeaders($theHeaders);
// If location is found, then assume redirect and redirect to location.
if( isset($processedHeaders['headers']['location']) ){
return $this->Redirect($processedHeaders,$r);
}
$strResponse = self::chunkTransferDecode($strResponse,$processedHeaders);
return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies']);
}
/**
* Fetch a url using php's fopen() function
*
*/
public function fopen_request($url,$r){
$handle = fopen($url, 'r');
if( !$handle ){
self::$debug['fopen'] = 'no handle';
return false;
}
self::stream_timeout($handle,$r['timeout']);
$strResponse = $this->ReadHandle($handle);
$theHeaders = self::StreamHeaders($handle);
fclose($handle);
$processedHeaders = self::processHeaders($theHeaders);
// If location is found, then assume redirect and redirect to location.
if( isset($processedHeaders['headers']['location']) ){
return $this->Redirect($processedHeaders,$r);
}
$strResponse = self::chunkTransferDecode($strResponse,$processedHeaders);
return array('headers' => $processedHeaders['headers'], 'body' => $strResponse, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies']);
}
/**
* Parse a URL and return its components
*
*/
public static function ParseUrl($url){
$arrURL = parse_url($url);
if( !isset($arrURL['port']) ){
$arrURL['port'] = 80;
}
$arrURL += array('path'=>'');
return $arrURL;
}
/**
* Make sure the url has an http or https scheme
*
*/
public static function FixScheme($url){
preg_match('#^[a-z]+:#',$url,$match);
if( empty($match) ){
return 'http://'.$url;
}
$match[0] = strtolower($match[0]);
if( $match[0] !== 'http:' && $match[0] !== 'https:' ){
$url = substr($url,strlen($match[0]));
$url = 'http://'.ltrim($url,'/');
}
return $url;
}
/**
* Fetch a url using php's fsockopen() function
*
*/
public function fsockopen_request($url,$r){
$fsockopen_host = $this->url_array['host'];
//fsockopen has issues with 'localhost' with IPv6 with certain versions of PHP, It attempts to connect to ::1,
// which fails when the server is not setup for it. For compatibility, always connect to the IPv4 address.
if ( 'localhost' == strtolower($fsockopen_host) )
$fsockopen_host = '127.0.0.1';
$iError = null; // Store error number
$strError = null; // Store error string
$handle = fsockopen( $fsockopen_host, $this->url_array['port'], $iError, $strError, $r['timeout'] );
if( $handle === false ){
self::$debug['fsock'] = 'no handle';
return false;
}
self::stream_timeout($handle,$r['timeout']);
$strHeaders = $this->ReqHeader($r);
fwrite($handle, $strHeaders);
$strResponse = $this->ReadHandle($handle);
fclose($handle);
$process = self::processResponse($strResponse);
$processedHeaders = self::processHeaders($process['headers']);
// If location is found, then assume redirect and redirect to location.
if( isset($processedHeaders['headers']['location']) ){
return $this->Redirect($processedHeaders,$r);
}
$strResponse = self::chunkTransferDecode($strResponse,$processedHeaders);
return array('headers' => $processedHeaders['headers'], 'body' => $process['body'], 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies']);
}
/**
* Return request header string
*
*/
protected function ReqHeader($r){
$requestPath = $this->url_array['path'] . ( isset($this->url_array['query']) ? '?' . $this->url_array['query'] : '' );
if ( empty($requestPath) )
$requestPath .= '/';
$strHeaders = strtoupper($r['method']) . ' ' . $requestPath . ' HTTP/' . $r['httpversion'] . "\r\n";
$strHeaders .= 'Host: ' . $this->url_array['host'] . "\r\n";
if ( isset($r['user-agent']) )
$strHeaders .= 'User-agent: ' . $r['user-agent'] . "\r\n";
$strHeaders .= "\r\n";
return $strHeaders;
}
/**
* Read all content from the handle
*
*/
protected function ReadHandle($handle){
$response = '';
while( !feof($handle) ){
$response .= fread($handle, 4096);
}
return $response;
}
/**
* Fetch a url using php's curl library
*
*/
protected function curl_request($url, $r){
$handle = curl_init();
/*
* CURLOPT_TIMEOUT and CURLOPT_CONNECTTIMEOUT expect integers. Have to use ceil since.
* a value of 0 will allow an unlimited timeout.
*/
$timeout = (int) ceil( $r['timeout'] );
curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $handle, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $handle, CURLOPT_URL, $url);
curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
//curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, ( $ssl_verify === true ) ? 2 : false );
//curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, $ssl_verify );
//if ( $ssl_verify ) {
// curl_setopt( $handle, CURLOPT_CAINFO, $r['sslcertificates'] );
//}
curl_setopt( $handle, CURLOPT_USERAGENT, $r['user-agent'] );
/*
* The option doesn't work with safe mode or when open_basedir is set, and there's
* a bug #17490 with redirected POST requests, so handle redirections outside Curl.
*/
curl_setopt( $handle, CURLOPT_FOLLOWLOCATION, false );
if ( defined( 'CURLOPT_PROTOCOLS' ) ) // PHP 5.2.10 / cURL 7.19.4
curl_setopt( $handle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS );
curl_setopt( $handle, CURLOPT_CUSTOMREQUEST, $r['method'] );
curl_setopt( $handle, CURLOPT_HEADERFUNCTION, array( $this, 'curl_headers' ) );
curl_setopt( $handle, CURLOPT_WRITEFUNCTION, array( $this, 'curl_body' ) );
curl_setopt( $handle, CURLOPT_HEADER, 0 );
if( $r['httpversion'] == '1.0' ){
curl_setopt( $handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
}else{
curl_setopt( $handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
}
curl_exec( $handle );
if( curl_errno( $handle ) ){
self::$debug['curl_error'] = curl_error( $handle );
curl_close( $handle );
return false;
}
curl_close( $handle );
$processedHeaders = self::processHeaders($this->headers);
// If location is found, then assume redirect and redirect to location.
if( isset($processedHeaders['headers']['location']) ){
return $this->Redirect($processedHeaders,$r);
}
return array('headers' => $processedHeaders['headers'], 'body' => $this->body, 'response' => $processedHeaders['response'], 'cookies' => $processedHeaders['cookies']);
}
/**
* Grab the headers of the cURL request
*
* Each header is sent individually to this callback, so we append to the $header property for temporary storage
*
* @since 3.2.0
* @access private
* @return int
*/
private function curl_headers( $handle, $headers ) {
$this->headers .= $headers;
return strlen( $headers );
}
/**
* Grab the body of the cURL request
*
* The contents of the document are passed in chunks, so we append to the $body property for temporary storage.
* Returning a length shorter than the length of $data passed in will cause cURL to abort the request with CURLE_WRITE_ERROR
*
* @since 3.6.0
* @access private
* @return int
*/
private function curl_body( $handle, $data ) {
$data_length = strlen( $data );
$this->body .= $data;
$this->bytes_written_total += $data_length;
// Upon event of this function returning less than strlen( $data ) curl will error with CURLE_WRITE_ERROR.
return $data_length;
}
/**
* Set the stream timeout
*
*/
public static function stream_timeout($handle,$time){
if( !function_exists('stream_set_timeout') ){
return;
}
$timeout = (int) floor( $time );
$utimeout = $timeout == $time ? 0 : 1000000 * $time % 1000000;
stream_set_timeout( $handle, $timeout, $utimeout );
}
/**
* Handle a redirect response
*
*/
public function Redirect($headers,$r){
if( $r['redirection']-- < 0 ){
trigger_error('Too many redirects');
return false;
}
//check location for releative value
$location = $headers['headers']['location'];
if( is_array($location) ){
$location = array_pop($location);
}
if( $location{0} == '/' ){
$location = $this->url_array['scheme'].'://'.$this->url_array['host'].$location;
}
self::$redirected = $location;
self::$debug['Redir'] = 1;
return $this->_get($location, $r);
}
/**
* Decodes chunk transfer-encoding, based off the HTTP 1.1 specification.
*
* Based off the HTTP http_encoding_dechunk function. Does not support UTF-8. Does not support
* returning footer headers. Shouldn't be too difficult to support it though.
*
* @todo Add support for footer chunked headers.
* @access public
* @since 1.7
* @static
*
* @param string $body Body content
* @return string Chunked decoded body on success or raw body on failure.
*/
public static function chunkTransferDecode($body,$headers){
if( !self::IsChunked($body,$headers) ){
return $body;
}
$parsed_body = '';
// We'll be altering $body, so need a backup in case of error.
$body_original = $body;
while ( true ) {
$has_chunk = (bool) preg_match( '/^([0-9a-f]+)[^\r\n]*\r\n/i', $body, $match );
if ( ! $has_chunk || empty( $match[1] ) )
return $body_original;
$length = hexdec( $match[1] );
$chunk_length = strlen( $match[0] );
// Parse out the chunk of data.
$parsed_body .= substr( $body, $chunk_length, $length );
// Remove the chunk from the raw data.
$body = substr( $body, $length + $chunk_length );
// End of the document.
if ( '0' === trim( $body ) )
return $parsed_body;
}
}
/**
* Return true if the response body is chunked
*
*/
public static function IsChunked($body, $headers){
$body = trim($body);
if( empty($body) ){
return false;
}
if( !isset( $headers['headers']['transfer-encoding'] ) || 'chunked' != $headers['headers']['transfer-encoding'] ){
return false;
}
// The body is not chunked encoded or is malformed.
if( ! preg_match( '/^([0-9a-f]+)[^\r\n]*\r\n/i',$body) ){
return false;
}
return true;
}
/**
* Gets stream headers, return false otherwise
*
* @access public
* @static
* @since 1.7
*
* @param resource $handle stream handle
* @return array|false Array with unprocessed string headers.
*/
public static function StreamHeaders($handle){
$meta = stream_get_meta_data($handle);
if( !isset($meta['wrapper_data']) ){
return $http_response_header; //$http_response_header is a PHP reserved variable which is set in the current-scope when using the HTTP Wrapper
}
$theHeaders = $meta['wrapper_data'];
if( isset($meta['wrapper_data']['headers']) ){
$theHeaders = $meta['wrapper_data']['headers'];
}
return $theHeaders;
}
/**
* Parses the responses and splits the parts into headers and body.
*
* @access public
* @static
* @since 1.7
*
* @param string $strResponse The full response string
* @return array Array with 'headers' and 'body' keys.
*/
public static function processResponse($strResponse) {
list($theHeaders, $theBody) = explode("\r\n\r\n", $strResponse, 2);
return array('headers' => $theHeaders, 'body' => $theBody);
}
/**
* Transform header string into an array.
*
* If an array is given then it is assumed to be raw header data with numeric keys with the
* headers as the values. No headers must be passed that were already processed.
*
* @access public
* @static
* @since 1.7
*
* @param string|array $headers
* @return array Processed string headers. If duplicate headers are encountered,
* Then a numbered array is returned as the value of that header-key.
*/
public static function processHeaders($headers) {
$headers = self::HeadersArray($headers);
$response = array('code' => 0, 'message' => '');
$cookies = array();
$newheaders = array();
foreach( $headers as $tempheader ){
if( false === strpos($tempheader, ':') ){
$stack = explode(' ', $tempheader, 3);
$stack[] = '';
list( , $response['code'], $response['message']) = $stack;
continue;
}
list($key, $value) = explode(':', $tempheader, 2);
$key = strtolower( $key );
$value = trim( $value );
if( isset( $newheaders[$key] ) ){
if( !is_array( $newheaders[$key] ) ){
$newheaders[$key] = array( $newheaders[$key] );
}
$newheaders[$key][] = $value;
}else{
$newheaders[$key] = $value;
}
}
self::$debug['Headers'] = count($newheaders);
return array('response' => $response, 'headers' => $newheaders, 'cookies' => $cookies);
}
/**
* Split header header string into array if needed
*
*/
protected static function HeadersArray($headers){
// split headers, one per array element
if ( is_string($headers) ) {
// tolerate line terminator: CRLF = LF (RFC 2616 19.3)
$headers = str_replace("\r\n", "\n", $headers);
// unfold folded header fields. LWS = [CRLF] 1*( SP | HT ) <US-ASCII SP, space (32)>, <US-ASCII HT, horizontal-tab (9)> (RFC 2616 2.2)
$headers = preg_replace('/\n[ \t]/', ' ', $headers);
// create the headers array
$headers = explode("\n", $headers);
}
$headers = (array)$headers;
$headers = array_filter($headers);
return $headers;
}
/**
* Output debug info about the most recent request
*
*/
public static function Debug($lang_key, $debug = array()){
$debug = array_merge(self::$debug,$debug);
return \gp\tool::Debug($lang_key, $debug);
}
}
}
namespace {
class gpRemoteGet extends \gp\tool\RemoteGet{}
}