1 | <?php |
||
17 | class HttpGetRequest |
||
18 | { |
||
19 | public $origin; |
||
20 | public $scheme = 'http'; |
||
21 | public $host = 'example.com'; |
||
22 | public $port = 80; |
||
23 | public $path = '/'; |
||
24 | |||
25 | public $query = array(); |
||
26 | public $headers = array(); |
||
27 | |||
28 | public $curlOpts = array(); |
||
29 | |||
30 | public $username = null; |
||
31 | public $password = null; |
||
32 | |||
33 | public $maybePublic = false; |
||
34 | public $verbose = false; |
||
35 | |||
36 | /** @var CConfig */ |
||
37 | protected $config; |
||
38 | |||
39 | /** |
||
40 | * normalize url and authentication info |
||
41 | * @param string $origin domain text |
||
42 | * @param string $url |
||
43 | * @param IO\IOInterface $io |
||
44 | */ |
||
45 | 25 | public function __construct($origin, $url, IO\IOInterface $io) |
|
46 | { |
||
47 | 25 | $this->origin = $origin; |
|
48 | 25 | $this->importURL($url); |
|
49 | |||
50 | 25 | if ($this->username && $this->password) { |
|
51 | 3 | $io->setAuthentication($origin, $this->username, $this->password); |
|
52 | 25 | } elseif ($io->hasAuthentication($origin)) { |
|
53 | 1 | $auth = $io->getAuthentication($origin); |
|
54 | 1 | $this->username = $auth['username']; |
|
55 | 1 | $this->password = $auth['password']; |
|
56 | 1 | } |
|
57 | 25 | } |
|
58 | |||
59 | /** |
||
60 | * @param string $url |
||
61 | */ |
||
62 | 25 | public function importURL($url) |
|
82 | |||
83 | |||
84 | /** |
||
85 | * @param string $key |
||
86 | * @param string $default |
||
87 | */ |
||
88 | 25 | private static function setOr(array $struct, $key, $default = null) |
|
96 | |||
97 | /** |
||
98 | * process option for RemortFileSystem |
||
99 | * @return void |
||
100 | */ |
||
101 | 5 | public function processRFSOption(array $option) |
|
105 | |||
106 | 8 | public function getCurlOpts() |
|
107 | { |
||
108 | 8 | $curlOpts = $this->curlOpts + array( |
|
109 | 8 | CURLOPT_HTTPGET => true, |
|
110 | 8 | CURLOPT_FOLLOWLOCATION => true, |
|
111 | 8 | CURLOPT_MAXREDIRS => 20, |
|
112 | 8 | CURLOPT_ENCODING => 'gzip', |
|
113 | 8 | CURLOPT_HTTPHEADER => $this->headers, |
|
114 | 8 | CURLOPT_USERAGENT => $this->genUA(), |
|
115 | 8 | ); |
|
116 | |||
117 | 8 | $curlOpts[CURLOPT_VERBOSE] = (bool)$this->verbose; |
|
118 | |||
119 | 8 | if ($this->username && $this->password) { |
|
120 | 1 | $curlOpts[CURLOPT_USERPWD] = "$this->username:$this->password"; |
|
121 | 1 | } else { |
|
122 | 8 | unset($curlOpts[CURLOPT_USERPWD]); |
|
123 | } |
||
124 | |||
125 | 8 | $curlOpts[CURLOPT_URL] = $this->getUrl(); |
|
126 | |||
127 | 8 | return $curlOpts; |
|
128 | } |
||
129 | |||
130 | 11 | public function getURL() |
|
131 | { |
||
132 | 11 | if ($this->scheme) { |
|
133 | 11 | $url = "$this->scheme://"; |
|
134 | 11 | } else { |
|
135 | 1 | $url = ''; |
|
136 | } |
||
137 | 11 | $url .= $this->host; |
|
138 | |||
139 | 11 | if ($this->port) { |
|
140 | 8 | $url .= ":$this->port"; |
|
141 | 8 | } |
|
142 | |||
143 | 11 | $url .= $this->path; |
|
144 | |||
145 | 11 | if ($this->query) { |
|
|
|||
146 | 8 | $url .= '?' . http_build_query($this->query); |
|
147 | 8 | } |
|
148 | |||
149 | 11 | return $url; |
|
150 | } |
||
151 | |||
152 | 3 | public function setConfig(CConfig $config) |
|
156 | |||
157 | 3 | public function promptAuth(HttpGetResponse $res, IO\IOInterface $io) |
|
158 | { |
||
159 | 3 | $httpCode = $res->info['http_code']; |
|
189 | |||
190 | 8 | public static function genUA() |
|
206 | } |
||
207 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.