| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * hirak/prestissimo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @author Hiraku NAKANO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license MIT https://github.com/hirak/prestissimo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace Hirak\Prestissimo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Composer\IO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Composer\Composer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Composer\Config as CConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Composer\Downloader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Composer\Util\NoProxyPattern; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Simple Container for http-get request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class HttpGetRequest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public $origin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public $scheme = 'http'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public $host = 'example.com'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public $port = 80; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public $path = '/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public $query = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public $headers = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public $curlOpts = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public $username = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public $password = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public $maybePublic = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public $verbose = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** @var CConfig */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     protected $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** @internal */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     const TOKEN_LABEL = 'access_token'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * normalize url and authentication info | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param string $origin domain text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param IO\IOInterface $io | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 21 |  |     public function __construct($origin, $url, IO\IOInterface $io) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 21 |  |         $this->origin = $origin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 21 |  |         $this->importURL($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 21 |  |         $this->setupProxy(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 21 |  |         if ($this->username && $this->password) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 3 |  |             $io->setAuthentication($origin, $this->username, $this->password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 21 |  |         } elseif ($io->hasAuthentication($origin)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |             $auth = $io->getAuthentication($origin); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |             $this->username = $auth['username']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |             $this->password = $auth['password']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 21 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 21 |  |     private function setupProxy() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         // no_proxy skip | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 21 |  |         if (isset($_SERVER['no_proxy'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $pattern = new NoProxyPattern($_SERVER['no_proxy']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             if ($pattern->test($this->getURL())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 unset($this->curlOpts[CURLOPT_PROXY]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 21 |  |         $httpProxy = self::issetOr($_SERVER, 'http_proxy', 'HTTP_PROXY'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 21 |  |         if ($httpProxy && $this->scheme === 'http') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |             $this->curlOpts[CURLOPT_PROXY] = $httpProxy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 20 |  |         $httpsProxy = self::issetOr($_SERVER, 'https_proxy', 'HTTPS_PROXY'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 20 |  |         if ($httpsProxy && $this->scheme === 'https') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |             $this->curlOpts[CURLOPT_PROXY] = $httpsProxy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 19 |  |         unset($this->curlOpts[CURLOPT_PROXY]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 19 |  |         unset($this->curlOpts[CURLOPT_PROXYUSERPWD]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 19 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 21 |  |     private static function issetOr(array $arr, $key1, $key2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 21 |  |         if (isset($arr[$key1])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 2 |  |             return $arr[$key1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 21 |  |         if (isset($arr[$key2])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 2 |  |             return $arr[$key2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 20 |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 21 |  |     public function importURL($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 21 |  |         $struct = parse_url($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         // @codeCoverageIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         if (!$struct) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             throw new \InvalidArgumentException("$url is not valid URL"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         // @codeCoverageIgnoreEnd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 21 |  |         $this->scheme = self::setOr($struct, 'scheme'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 21 |  |         $this->host = self::setOr($struct, 'host'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 21 |  |         $this->port = self::setOr($struct, 'port'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 21 |  |         $this->path = self::setOr($struct, 'path'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 21 |  |         $this->username = self::setOr($struct, 'user'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 21 |  |         $this->password = self::setOr($struct, 'pass'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 21 |  |         if (!empty($struct['query'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 3 |  |             parse_str($struct['query'], $this->query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 21 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @param array $struct | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param string $default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 21 |  |     private static function setOr(array $struct, $key, $default = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 21 |  |         if (!empty($struct[$key])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 21 |  |             return $struct[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 21 |  |         return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * process option for RemortFileSystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 2 |  |     public function processRFSOption(array $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 2 |  |         if (isset($options[static::TOKEN_LABEL])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 2 |  |             $this->query['access_token'] = $options[static::TOKEN_LABEL]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 4 |  |     public function getCurlOpts() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 4 |  |         $headers = $this->headers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 4 |  |         if ($this->username && $this->password) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 1 |  |             foreach ($headers as $i => $header) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |                 if (0 === strpos($header, 'Authorization:')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                     unset($headers[$i]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 1 |  |             $headers[] = 'Authorization: Basic ' . base64_encode("$this->username:$this->password"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 4 |  |         $curlOpts = $this->curlOpts + array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 4 |  |             CURLOPT_HTTPGET => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 4 |  |             CURLOPT_FOLLOWLOCATION => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 4 |  |             CURLOPT_MAXREDIRS => 20, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 4 |  |             CURLOPT_ENCODING => 'gzip', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 4 |  |             CURLOPT_HTTPHEADER => $headers, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 4 |  |             CURLOPT_USERAGENT => $this->genUA(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 4 |  |             CURLOPT_VERBOSE => (bool)$this->verbose, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 4 |  |             CURLOPT_URL => $this->getUrl(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 4 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 4 |  |         unset($curlOpts[CURLOPT_USERPWD]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 4 |  |         if ($ciphers = $this->nssCiphers()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             $curlOpts[CURLOPT_SSL_CIPHER_LIST] = $ciphers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 4 |  |         return $curlOpts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      * enable ECC cipher suites in cURL/NSS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 | 4 |  |     public function nssCiphers() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 4 |  |         static $cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 | 4 |  |         if (isset($cache)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 | 3 |  |             return $cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 2 |  |         $ver = curl_version(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 2 |  |         if (preg_match('/^NSS.*Basic ECC$/', $ver['ssl_version'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |             $ciphers = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |             foreach (new \SplFileObject(__DIR__ . '/../../res/nss_ciphers.txt') as $line) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |                 $line = trim($line); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |                 if ($line) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |                     $ciphers[] = $line; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |             return $cache = implode(',', $ciphers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 | 2 |  |         return $cache = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 | 11 |  |     public function getURL() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 | 11 |  |         $url = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 | 11 |  |         if ($this->scheme) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 | 11 |  |             $url .= "$this->scheme://"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 | 11 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 | 11 |  |         $url .= $this->host; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 | 11 |  |         if ($this->port) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 | 3 |  |             $url .= ":$this->port"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 | 11 |  |         $url .= $this->path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 | 11 |  |         if ($this->query) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 | 3 |  |             $url .= '?' . http_build_query($this->query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 | 11 |  |         return $url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 | 2 |  |     public function setConfig(CConfig $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 | 2 |  |         $this->config = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 | 3 |  |     public function promptAuth(HttpGetResponse $res, IO\IOInterface $io) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 | 3 |  |         $httpCode = $res->info['http_code']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |         // 404s are only handled for github | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 | 3 |  |         if (404 === $httpCode) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |         // fail if the console is not interactive | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 | 3 |  |         if (!$io->isInteractive() && ($httpCode === 401 || $httpCode === 403)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 | 1 |  |             $message = "The '{$this->getURL()}' URL required authentication.\nYou must be using the interactive console to authenticate"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 | 1 |  |             throw new Downloader\TransportException($message, $httpCode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |         // fail if we already have auth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 | 2 |  |         if ($io->hasAuthentication($this->origin)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 | 1 |  |             throw new Downloader\TransportException("Invalid credentials for '{$this->getURL()}', aborting.", $httpCode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 | 1 |  |         $io->overwrite("    Authentication required (<info>$this->host</info>):"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 | 1 |  |         $username = $io->ask('      Username: '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 | 1 |  |         $password = $io->askAndHideAnswer('      Password: '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 | 1 |  |         $io->setAuthentication($this->origin, $username, $password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 | 1 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |      * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |      * @param int $privateCode 404|403 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |      * @param Composer\Util\GitHub|Composer\Util\GitLab $util | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |      * @param HttpGetResponse $res | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |      * @param IO\IOInterface $io | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |      * @throws Composer\Downloader\TransportException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 271 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 272 | 2 |  |     public function promptAuthWithUtil($privateCode, $util, HttpGetResponse $res, IO\IOInterface $io) | 
            
                                                                        
                            
            
                                    
            
            
                | 273 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 274 | 2 |  |         $httpCode = $res->info['http_code']; | 
            
                                                                        
                            
            
                                    
            
            
                | 275 | 2 |  |         $message = "\nCould not fetch {$this->getURL()}, enter your $this->origin credentials "; | 
            
                                                                        
                            
            
                                    
            
            
                | 276 | 2 |  |         if ($privateCode === $httpCode) { | 
            
                                                                        
                            
            
                                    
            
            
                | 277 | 1 |  |             $message .= 'to access private repos'; | 
            
                                                                        
                            
            
                                    
            
            
                | 278 | 1 |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 279 | 1 |  |             $message .= 'to go over the API rate limit'; | 
            
                                                                        
                            
            
                                    
            
            
                | 280 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 281 | 2 |  |         if ($util->authorizeOAuth($this->origin)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 282 | 1 |  |             return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 283 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 284 | 2 |  |         if ($io->isInteractive() && | 
            
                                                                        
                            
            
                                    
            
            
                | 285 | 2 |  |             $util->authorizeOAuthInteractively($this->origin, $message)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 286 | 1 |  |             return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 287 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 288 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 289 | 1 |  |         throw new Downloader\TransportException("Could not authenticate against $this->origin", $httpCode); | 
            
                                                                        
                            
            
                                    
            
            
                | 290 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 | 4 |  |     public static function genUA() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 | 4 |  |         static $ua; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 | 4 |  |         if ($ua) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 | 3 |  |             return $ua; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 | 2 |  |         $phpVersion = defined('HHVM_VERSION') ? 'HHVM ' . HHVM_VERSION : 'PHP ' . PHP_VERSION; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 | 2 |  |         return $ua = sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 | 2 |  |             'Composer/%s (%s; %s; %s)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 | 2 |  |             str_replace('@package_version@', 'source', Composer::VERSION), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 | 2 |  |             php_uname('s'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 | 2 |  |             php_uname('r'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |             $phpVersion | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 | 2 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 311 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 312 |  |  |  | 
            
                        
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: