GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch Editing-Fixing (c4d168)
by Igor
03:30
created

Curl_HTTP_Client::store_cookies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Route4Me;
3
4
class CurlHttpClient
5
{
6
    var $ch ;
7
    var $debug = false;
8
    var $error_msg;
9
10
    function CurlHttpClient($debug = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
11
    {
12
        $this->debug = $debug;
13
        $this->init();
14
    }
15
    
16
    function init()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
17
    {
18
        // initialize curl handle
19
        $this->ch = curl_init();
20
        //set various options
21
        //set error in case http return code bigger than 300
22
        curl_setopt($this->ch, CURLOPT_FAILONERROR, true);
23
        // use gzip if possible
24
        curl_setopt($this->ch,CURLOPT_ENCODING , 'gzip, deflate');
25
        // do not veryfy ssl
26
        // this is important for windows
27
        // as well for being able to access pages with non valid cert
28
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
29
    }
30
    
31
    /**
32
     * Set username/pass for basic http auth
33
     * @param string user
34
     * @param string pass
35
     * @access public
36
     */
37
    function setCredentials($username,$password)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
38
    {
39
        curl_setopt($this->ch, CURLOPT_USERPWD, "$username:$password");
40
    }
41
    
42
    /**
43
     * Set referrer
44
     * @param string referrer url 
45
     * @access public
46
     */
47
    function setReferrer($referrer_url)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
    {
49
        curl_setopt($this->ch, CURLOPT_REFERER, $referrer_url);
50
    }
51
    
52
    /**
53
     * Set client's userAgent
54
     * @param string user agent
55
     * @access public
56
     */
57
    function setUserAgent($userAgent)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
58
    {
59
        curl_setopt($this->ch, CURLOPT_USERAGENT, $userAgent);
60
    }
61
    
62
    /**
63
     * Set to receive output headers in all output functions
64
     * @param boolean true to include all response headers with output, false otherwise
65
     * @access public
66
     */
67
    function includeResponseHeaders($value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
68
    {
69
        curl_setopt($this->ch, CURLOPT_HEADER, $value);
70
    }
71
    
72
    /**
73
     * Set proxy to use for each curl request
74
     * @param string proxy
75
     * @access public
76
     */
77
    function setProxy($proxy)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
78
    {
79
        curl_setopt($this->ch, CURLOPT_PROXY, $proxy);
80
    }
81
    
82
    /**
83
     * Send post data to target URL     
84
     * return data returned from url or false if error occured
85
     * @param string url
86
     * @param mixed post data (assoc array ie. $foo['post_var_name'] = $value or as string like var=val1&var2=val2)
87
     * @param string ip address to bind (default null)
88
     * @param int timeout in sec for complete curl operation (default 10)
89
     * @return string data
90
     * @access public
91
     */
92 View Code Duplication
    function sendPostData($url, $postdata, $ip=null, $timeout=10)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        //set various curl options first
95
        // set url to post to
96
        curl_setopt($this->ch, CURLOPT_URL,$url);
97
        // return into a variable rather than displaying it
98
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER,true);
99
        
100
        //bind to specific ip address if it is sent trough arguments
101
        if($ip) {
102
            if($this->debug)
103
            {
104
                echo "Binding to ip $ip\n";
105
            }
106
            curl_setopt($this->ch,CURLOPT_INTERFACE,$ip);
107
        }
108
        
109
        //set curl function timeout to $timeout
110
        curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout);
111
        
112
        //set method to post
113
        curl_setopt($this->ch, CURLOPT_POST, true);
114
        // set post string
115
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postdata);
116
        
117
        //and finally send curl request
118
        $result = curl_exec_redir($this->ch);
119
        if(curl_errno($this->ch)) {
120
            if($this->debug) {
121
                echo "Error Occured in Curl\n";
122
                echo "Error number: " .curl_errno($this->ch) ."\n";
123
                echo "Error message: " .curl_error($this->ch)."\n";
124
            }
125
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Route4Me\CurlHttpClient::sendPostData of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
126
        } else {
127
            return $result;
128
        }
129
    }
130
    
131
    /**
132
     * fetch data from target URL     
133
     * return data returned from url or false if error occured
134
     * @param string url     
135
     * @param string ip address to bind (default null)
136
     * @param int timeout in sec for complete curl operation (default 5)
137
     * @return string data
138
     * @access public
139
     */
140 View Code Duplication
    function fetchUrl($url, $ip=null, $timeout=5)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        // set url to post to
143
        curl_setopt($this->ch, CURLOPT_URL,$url);
144
        
145
        //set method to get
146
        curl_setopt($this->ch, CURLOPT_HTTPGET,true);
147
        // return into a variable rather than displaying it
148
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER,true);
149
        
150
        //bind to specific ip address if it is sent trough arguments
151
        if($ip) {
152
            if($this->debug) {
153
                echo "Binding to ip $ip\n";
154
            }
155
            
156
            curl_setopt($this->ch,CURLOPT_INTERFACE,$ip);
157
        }
158
        
159
        //set curl function timeout to $timeout
160
        curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout);
161
        
162
        //and finally send curl request
163
        $result = curl_exec_redir($this->ch);
164
        if(curl_errno($this->ch)) {
165
            if($this->debug) {
166
                echo "Error Occured in Curl\n";
167
                echo "Error number: " .curl_errno($this->ch) ."\n";
168
                echo "Error message: " .curl_error($this->ch)."\n";
169
            }
170
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Route4Me\CurlHttpClient::fetchUrl of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
171
        } else {
172
            return $result;
173
        }
174
    }
175
    
176
    /**
177
     * Fetch data from target URL
178
     * and store it directly to file          
179
     * @param string url     
180
     * @param resource value stream resource(ie. fopen)
181
     * @param string ip address to bind (default null)
182
     * @param int timeout in sec for complete curl operation (default 5)
183
     * @return boolean true on success false othervise
184
     * @access public
185
     */
186 View Code Duplication
    function fetchIntoFile($url, $fp, $ip=null, $timeout=5)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
    {
188
        // set url to post to
189
        curl_setopt($this->ch, CURLOPT_URL,$url);
190
        //set method to get
191
        curl_setopt($this->ch, CURLOPT_HTTPGET, true);
192
        // store data into file rather than displaying it
193
        curl_setopt($this->ch, CURLOPT_FILE, $fp);
194
        
195
        //bind to specific ip address if it is sent trough arguments
196
        if($ip) {
197
            if($this->debug) {
198
                echo "Binding to ip $ip\n";
199
            }
200
            curl_setopt($this->ch, CURLOPT_INTERFACE, $ip);
201
        }
202
        
203
        //set curl function timeout to $timeout
204
        curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout);
205
        
206
        //and finally send curl request
207
        $result = curl_exec_redir($this->ch);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
208
        if(curl_errno($this->ch)) {
209
            if($this->debug) {
210
                echo "Error Occured in Curl\n";
211
                echo "Error number: " .curl_errno($this->ch) ."\n";
212
                echo "Error message: " .curl_error($this->ch)."\n";
213
            }
214
            
215
            return false;
216
        } else  {
217
            return true;
218
        }
219
    }
220
    
221
    /**
222
     * Send multipart post data to the target URL     
223
     * return data returned from url or false if error occured
224
     * (contribution by vule nikolic, [email protected])
225
     * @param string url
226
     * @param array assoc post data array ie. $foo['post_var_name'] = $value
227
     * @param array assoc $file_field_array, contains file_field name = value - path pairs
228
     * @param string ip address to bind (default null)
229
     * @param int timeout in sec for complete curl operation (default 30 sec)
230
     * @return string data
231
     * @access public
232
     */
233
    function sendMultipartPostData($url, $postdata, $file_field_array=array(), $ip=null, $timeout=30)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
234
    {
235
        //set various curl options first
236
        // set url to post to
237
        curl_setopt($this->ch, CURLOPT_URL, $url);
238
        // return into a variable rather than displaying it
239
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
240
        
241
        //bind to specific ip address if it is sent trough arguments
242
        if($ip) {
243
            if($this->debug) {
244
                echo "Binding to ip $ip\n";
245
            }
246
            
247
            curl_setopt($this->ch,CURLOPT_INTERFACE,$ip);
248
        }
249
        
250
        //set curl function timeout to $timeout
251
        curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout);
252
        //set method to post
253
        curl_setopt($this->ch, CURLOPT_POST, true);
254
        // disable Expect header
255
        // hack to make it working
256
        $headers = array("Expect: ");
257
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
258
        // initialize result post array
259
        $result_post = array();
0 ignored issues
show
Unused Code introduced by
$result_post is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
260
        //generate post string
261
        $post_array = array();
262
        $post_string_array = array();
263
        
264
        if(!is_array($postdata)) {
265
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Route4Me\CurlHttpClient::sendMultipartPostData of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
266
        }
267
        
268
        foreach($postdata as $key=>$value) {
269
            $post_array[$key] = $value;
270
            $post_string_array[] = urlencode($key)."=".urlencode($value);
271
        }
272
        
273
        $post_string = implode("&",$post_string_array);
274
        if($this->debug) {
275
            echo "Post String: $post_string\n";
276
        }
277
        
278
        // set post string
279
        //curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post_string);
280
        // set multipart form data - file array field-value pairs
281
        if(!empty($file_field_array)) {
282
            foreach($file_field_array as $var_name => $var_value) {
283
                if(strpos(PHP_OS, "WIN") !== false) $var_value = str_replace("/", "\\", $var_value); // win hack
284
                $file_field_array[$var_name] = "@".$var_value;
285
            }
286
        }
287
        
288
        // set post data
289
        $result_post = array_merge($post_array, $file_field_array);
290
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $result_post);
291
        
292
        //and finally send curl request
293
        $result = curl_exec_redir($this->ch);
294
        if(curl_errno($this->ch)) {
295
            if($this->debug) {
296
                echo "Error Occured in Curl\n";
297
                echo "Error number: " .curl_errno($this->ch) ."\n";
298
                echo "Error message: " .curl_error($this->ch)."\n";
299
            }
300
            
301
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Route4Me\CurlHttpClient::sendMultipartPostData of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
302
        } else {
303
            return $result;
304
        }
305
    }
306
    
307
    /**
308
     * Set file location where cookie data will be stored and send on each new request
309
     * @param string absolute path to cookie file (must be in writable dir)
310
     * @access public
311
     */
312
    function storeCookies($cookie_file)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
313
    {
314
        // use cookies on each request (cookies stored in $cookie_file)
315
        curl_setopt ($this->ch, CURLOPT_COOKIEJAR, $cookie_file);
316
        curl_setopt ($this->ch, CURLOPT_COOKIEFILE, $cookie_file);
317
    }
318
    
319
    /**
320
     * Set custom cookie
321
     * @param string cookie
322
     * @access public
323
     */
324
    function setCookie($cookie)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
325
    {        
326
        curl_setopt ($this->ch, CURLOPT_COOKIE, $cookie);
327
    }
328
    
329
    /**
330
     * Get last URL info 
331
     * usefull when original url was redirected to other location    
332
     * @access public
333
     * @return string url
334
     */
335
    function getEffectiveUrl()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
336
    {
337
        return curl_getinfo($this->ch, CURLINFO_EFFECTIVE_URL);
338
    }
339
    
340
    /**
341
     * Get http response code     
342
     * @access public
343
     * @return int
344
     */
345
    function getHttpResponseCode()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
346
    {
347
        return curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
348
    }
349
    
350
    /**
351
     * Return last error message and error number
352
     * @return string error msg
353
     * @access public
354
     */
355
    function getErrorMsg()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
356
    {
357
        $err = "Error number: " .curl_errno($this->ch) ."\n";
358
        $err .="Error message: " .curl_error($this->ch)."\n";
359
        return $err;
360
    }
361
    
362
    /**
363
     * Close curl session and free resource
364
     * Usually no need to call this function directly
365
     * in case you do you have to call init() to recreate curl
366
     * @access public
367
     */
368
    function close()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
369
    {
370
        //close curl session and free up resources
371
        curl_close($this->ch);
372
    }
373
}
374
375
/**
376
 * This function allows curl to follow redirects without using CURLOPT_FOLLOWLOCATION
377
 * which is disabled when using open_basedir or safe_mode
378
 */
379
function curlExecRedir($ch)
380
{
381
    static $curl_loops = 0;
382
    static $curl_max_loops = 20;
383
    
384
    if ($curl_loops++ >= $curl_max_loops) {
385
        $curl_loops = 0;
386
        return FALSE;
387
    }
388
    
389
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
390
    curl_setopt($ch, CURLOPT_HEADER, true);
391
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
392
    curl_setopt($ch, CURLOPT_VERBOSE, false);
393
    $data = curl_exec($ch);
394
    $data = str_replace("\r", "\n", str_replace("\r\n", "\n", $data));
395
    list($header, $data) = explode("\n\n", $data, 2);
396
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
397
    
398
    //echo "*** Got HTTP code: $http_code ***\n";
399
    //echo "**  Got headers: \n$header\n\n";
400
    
401
    if ( $http_code == 301 || $http_code == 302 ) {
402
        // If we're redirected, we should revert to GET
403
        curl_setopt($ch, CURLOPT_HTTPGET,true);
404
        
405
        $matches = array();
406
        preg_match('/Location:\s*(.*?)(\n|$)/i', $header, $matches);
407
        $url = @parse_url(trim($matches[1]));
408
        
409
        if (!$url) {
410
            //couldn't process the url to redirect to
411
            $curl_loops = 0;
412
            return $data;
413
        }
414
        
415
        $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
416
        if (empty($url['scheme']))
417
            $url['scheme'] = $last_url['scheme'];
418
        
419
        if (empty($url['host']))
420
            $url['host'] = $last_url['host'];
421
        
422
        if (empty($url['path']))
423
            $url['path'] = $last_url['path'];
424
        
425
        $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . (!empty($url['query'])?'?'.$url['query']:'');
426
        //echo "Being redirected to $new_url\n";
427
        curl_setopt($ch, CURLOPT_URL, $new_url);
428
        
429
        return curl_exec_redir($ch);
430
    } else {
431
        $curl_loops=0;
432
        return $data;
433
    }
434
}
435
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
436