1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Utils;
use Composer\CaBundle\CaBundle;
use function base64_encode;
use function curl_exec;
use function curl_getinfo;
use function curl_init;
use function curl_setopt;
use function file_get_contents;
use function function_exists;
use function getenv;
use function ini_get;
use function intval;
use function is_array;
use function is_dir;
use function parse_url;
use function preg_match;
use function stream_context_create;
use function strlen;
use const CURL_IPRESOLVE_V4;
use const CURLINFO_HTTP_CODE;
use const CURLOPT_CAINFO;
use const CURLOPT_CAPATH;
use const CURLOPT_CONNECTTIMEOUT;
use const CURLOPT_CUSTOMREQUEST;
use const CURLOPT_FOLLOWLOCATION;
use const CURLOPT_HTTPHEADER;
use const CURLOPT_IPRESOLVE;
use const CURLOPT_POSTFIELDS;
use const CURLOPT_PROXY;
use const CURLOPT_PROXYUSERPWD;
use const CURLOPT_RETURNTRANSFER;
use const CURLOPT_SSL_VERIFYHOST;
use const CURLOPT_SSL_VERIFYPEER;
use const CURLOPT_TIMEOUT;
use const CURLOPT_USERAGENT;
use const PHP_SAPI;
/**
* Handles HTTP requests
*/
class HttpRequest
{
/** @var string */
private $proxyUrl;
/** @var string */
private $proxyUser;
/** @var string */
private $proxyPass;
public function __construct()
{
global $cfg;
$this->proxyUrl = $cfg['ProxyUrl'];
$this->proxyUser = $cfg['ProxyUser'];
$this->proxyPass = $cfg['ProxyPass'];
}
public static function setProxySettingsFromEnv(): void
{
global $cfg;
$httpProxy = getenv('http_proxy');
$urlInfo = parse_url((string) $httpProxy);
if (PHP_SAPI !== 'cli' || ! is_array($urlInfo)) {
return;
}
$cfg['ProxyUrl'] = ($urlInfo['host'] ?? '')
. (isset($urlInfo['port']) ? ':' . $urlInfo['port'] : '');
$cfg['ProxyUser'] = $urlInfo['user'] ?? '';
$cfg['ProxyPass'] = $urlInfo['pass'] ?? '';
}
/**
* Returns information with regards to handling the http request
*
* @param array $context Data about the context for which
* to http request is sent
*
* @return array of updated context information
*/
private function handleContext(array $context)
{
if (strlen($this->proxyUrl) > 0) {
$context['http'] = [
'proxy' => $this->proxyUrl,
'request_fulluri' => true,
];
if (strlen($this->proxyUser) > 0) {
$auth = base64_encode($this->proxyUser . ':' . $this->proxyPass);
$context['http']['header'] = 'Proxy-Authorization: Basic '
. $auth . "\r\n";
}
}
return $context;
}
/**
* Creates HTTP request using curl
*
* @param mixed $response HTTP response
* @param int $httpStatus HTTP response status code
* @param bool $returnOnlyStatus If set to true, the method would only return response status
*
* @return string|bool|null
*/
private function response(
$response,
$httpStatus,
$returnOnlyStatus
) {
if ($httpStatus == 404) {
return false;
}
if ($httpStatus != 200) {
return null;
}
if ($returnOnlyStatus) {
return true;
}
return $response;
}
/**
* Creates HTTP request using curl
*
* @param string $url Url to send the request
* @param string $method HTTP request method (GET, POST, PUT, DELETE, etc)
* @param bool $returnOnlyStatus If set to true, the method would only return response status
* @param mixed $content Content to be sent with HTTP request
* @param string $header Header to be set for the HTTP request
*
* @return string|bool|null
*/
private function curl(
$url,
$method,
$returnOnlyStatus = false,
$content = null,
$header = ''
) {
$curlHandle = curl_init($url);
if ($curlHandle === false) {
return null;
}
$curlStatus = 1;
if (strlen($this->proxyUrl) > 0) {
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_PROXY, $this->proxyUrl);
if (strlen($this->proxyUser) > 0) {
$curlStatus &= (int) curl_setopt(
$curlHandle,
CURLOPT_PROXYUSERPWD,
$this->proxyUser . ':' . $this->proxyPass
);
}
}
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_USERAGENT, 'phpMyAdmin');
if ($method !== 'GET') {
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, $method);
}
if ($header) {
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_HTTPHEADER, [$header]);
}
if ($method === 'POST') {
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $content);
}
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, 2);
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, true);
$caPathOrFile = CaBundle::getSystemCaRootBundlePath();
if (is_dir($caPathOrFile)) {
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_CAPATH, $caPathOrFile);
} else {
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_CAINFO, $caPathOrFile);
}
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, 0);
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10);
$curlStatus &= (int) curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, 10);
if (! $curlStatus) {
return null;
}
$response = @curl_exec($curlHandle);
if ($response === false) {
return null;
}
$httpStatus = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
return $this->response($response, $httpStatus, $returnOnlyStatus);
}
/**
* Creates HTTP request using file_get_contents
*
* @param string $url Url to send the request
* @param string $method HTTP request method (GET, POST, PUT, DELETE, etc)
* @param bool $returnOnlyStatus If set to true, the method would only return response status
* @param mixed $content Content to be sent with HTTP request
* @param string $header Header to be set for the HTTP request
*
* @return string|bool|null
*/
private function fopen(
$url,
$method,
$returnOnlyStatus = false,
$content = null,
$header = ''
) {
$context = [
'http' => [
'method' => $method,
'request_fulluri' => true,
'timeout' => 10,
'user_agent' => 'phpMyAdmin',
'header' => 'Accept: */*',
],
'ssl' => [
'verify_peer' => true,
'verify_peer_name' => true,
],
];
if ($header) {
$context['http']['header'] .= "\n" . $header;
}
if ($method === 'POST') {
$context['http']['content'] = $content;
}
$caPathOrFile = CaBundle::getSystemCaRootBundlePath();
if (is_dir($caPathOrFile)) {
$context['ssl']['capath'] = $caPathOrFile;
} else {
$context['ssl']['cafile'] = $caPathOrFile;
}
$context = $this->handleContext($context);
$response = @file_get_contents(
$url,
false,
stream_context_create($context)
);
if (! isset($http_response_header)) {
return null;
}
preg_match('#HTTP/[0-9\.]+\s+([0-9]+)#', $http_response_header[0], $out);
$httpStatus = intval($out[1]);
return $this->response($response, $httpStatus, $returnOnlyStatus);
}
/**
* Creates HTTP request
*
* @param string $url Url to send the request
* @param string $method HTTP request method (GET, POST, PUT, DELETE, etc)
* @param bool $returnOnlyStatus If set to true, the method would only return response status
* @param mixed $content Content to be sent with HTTP request
* @param string $header Header to be set for the HTTP request
*
* @return string|bool|null
*/
public function create(
$url,
$method,
$returnOnlyStatus = false,
$content = null,
$header = ''
) {
if (function_exists('curl_init')) {
return $this->curl($url, $method, $returnOnlyStatus, $content, $header);
}
if (ini_get('allow_url_fopen')) {
return $this->fopen($url, $method, $returnOnlyStatus, $content, $header);
}
return null;
}
}
|