Failed Conditions
Pull Request — master (#8)
by Mateusz
03:08
created

src/HttpClient.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the puli/installer package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Installer;
13
14
use RuntimeException;
15
16
/**
17
 * Downloads files.
18
 *
19
 * This file was adapted from the installer file bundled with Composer. For the
20
 * original file, authors and copyright information see
21
 *
22
 * https://github.com/composer/getcomposer.org/blob/master/web/installer
23
 *
24
 * @author Nils Adermann <[email protected]>
25
 * @author Jordi Boggiano <[email protected]>
26
 * @author Thomas Rudolph <[email protected]>
27
 * @author Bernhard Schussek <[email protected]>
28
 */
29
class HttpClient
30
{
31
    /**
32
     * @var array
33
     */
34
    private $headers = array(
35
        "Connection: close\r\n",
36
        "User-Agent: Puli Installer\r\n",
37
    );
38
39
    /**
40
     * @var bool
41
     */
42
    private $tls = false;
43
44
    /**
45
     * Creates the HTTP client.
46
     *
47
     * @param bool $disableTls Whether to disable TLS.
48
     */
49 5
    public function __construct($disableTls = false)
50
    {
51 5
        $this->tls = !$disableTls;
52
53 5
        if (extension_loaded('zlib')) {
54 5
            $this->headers[] = "Accept-Encoding: gzip\r\n";
55 5
        }
56 5
    }
57
58
    /**
59
     * Downloads a file.
60
     *
61
     * @param string $url The URL of the file to download.
62
     *
63
     * @return string The downloaded file body.
64
     */
65 5
    public function download($url)
66
    {
67 5
        if ($this->tls) {
68 5
            humbug_set_headers($this->headers);
69 5
            $result = humbug_get_contents($url);
70 5
        } else {
71
            $context = $this->getNonSslStreamContext($url);
72
            $result = file_get_contents($url, null, $context);
73
        }
74
75 5
        if ($result && extension_loaded('zlib')) {
76 5
            $decode = false;
77 5
            $responseHeaders = $this->tls ? humbug_get_headers() : $http_response_header;
0 ignored issues
show
The variable $http_response_header does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
78
79 5
            foreach ($responseHeaders as $header) {
80 5
                if (preg_match('{^content-encoding: *gzip *$}i', $header)) {
81
                    $decode = true;
82
                    continue;
83 5
                } elseif (preg_match('{^HTTP/}i', $header)) {
84 5
                    $decode = false;
85 5
                }
86 5
            }
87
88 5
            if ($decode) {
89
                $result = version_compare(PHP_VERSION, '5.4.0', '>=')
90
                    ? zlib_decode($result)
91
                    // work around issue with gzuncompress & co that do not work with all gzip checksums
92
                    : file_get_contents('compress.zlib://data:application/octet-stream;base64,'.base64_encode($result));
93
94
                if (!$result) {
95
                    throw new RuntimeException('Failed to decode zlib stream');
96
                }
97
            }
98 5
        }
99
100 5
        return $result;
101
    }
102
103
    private function getNonSslStreamContext($url)
104
    {
105
        $options = array();
106
107
        // Handle system proxy
108
        if (!empty($_SERVER['HTTP_PROXY']) || !empty($_SERVER['http_proxy'])) {
109
            // Some systems seem to rely on a lowercased version instead...
110
            $proxy = parse_url(!empty($_SERVER['http_proxy']) ? $_SERVER['http_proxy'] : $_SERVER['HTTP_PROXY']);
111
        }
112
113
        if (!empty($proxy)) {
114
            $proxyURL = isset($proxy['scheme']) ? $proxy['scheme'].'://' : '';
115
            $proxyURL .= isset($proxy['host']) ? $proxy['host'] : '';
116
117
            if (isset($proxy['port'])) {
118
                $proxyURL .= ':'.$proxy['port'];
119
            } elseif ('http://' === substr($proxyURL, 0, 7)) {
120
                $proxyURL .= ':80';
121
            } elseif ('https://' === substr($proxyURL, 0, 8)) {
122
                $proxyURL .= ':443';
123
            }
124
125
            // http(s):// is not supported in proxy
126
            $proxyURL = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $proxyURL);
127
128
            if (0 === strpos($proxyURL, 'ssl:') && !extension_loaded('openssl')) {
129
                throw new RuntimeException('You must enable the openssl extension to use a proxy over https');
130
            }
131
132
            $options['http'] = array(
133
                'proxy' => $proxyURL,
134
            );
135
136
            // enabled request_fulluri unless it is explicitly disabled
137
            switch (parse_url($url, PHP_URL_SCHEME)) {
138 View Code Duplication
                case 'http': // default request_fulluri to true
139
                    $reqFullUriEnv = getenv('HTTP_PROXY_REQUEST_FULLURI');
140
                    if ($reqFullUriEnv === false || $reqFullUriEnv === '' || (strtolower($reqFullUriEnv) !== 'false' && (bool) $reqFullUriEnv)) {
141
                        $options['http']['request_fulluri'] = true;
142
                    }
143
                    break;
144 View Code Duplication
                case 'https': // default request_fulluri to true
145
                    $reqFullUriEnv = getenv('HTTPS_PROXY_REQUEST_FULLURI');
146
                    if ($reqFullUriEnv === false || $reqFullUriEnv === '' || (strtolower($reqFullUriEnv) !== 'false' && (bool) $reqFullUriEnv)) {
147
                        $options['http']['request_fulluri'] = true;
148
                    }
149
                    break;
150
            }
151
152
            if (isset($proxy['user'])) {
153
                $auth = urldecode($proxy['user']);
154
                if (isset($proxy['pass'])) {
155
                    $auth .= ':'.urldecode($proxy['pass']);
156
                }
157
                $auth = base64_encode($auth);
158
159
                $options['http']['header'] = "Proxy-Authorization: Basic {$auth}\r\n";
160
            }
161
        }
162
163
        if (isset($options['http']['header'])) {
164
            $options['http']['header'] .= implode('', $this->headers);
165
        } else {
166
            $options['http']['header'] = implode('', $this->headers);
167
        }
168
169
        $options['http']['protocol_version'] = 1.1;
170
171
        return stream_context_create($options);
172
    }
173
}
174