[contents] Use file_get_contents when in CLI mode & no certs (#962)

file_get_contents can natively use system root certificates, so use file_get_contents when in CLI mode with no root certificates for cURL.
This commit is contained in:
triatic 2018-12-26 19:04:55 +00:00 committed by LogMANOriginal
parent bb2329fa3a
commit dc83962483

View file

@ -52,6 +52,21 @@ function getContents($url, $header = array(), $opts = array()){
$params = [$url]; $params = [$url];
$cache->setParameters($params); $cache->setParameters($params);
// Use file_get_contents if in CLI mode with no root certificates defined
if(php_sapi_name() === 'cli' && ini_get('curl.cainfo') === '') {
$data = file_get_contents($url);
if($data === false) {
$errorCode = 500;
} else {
$errorCode = 200;
}
$curlError = '';
$curlErrno = '';
$headerSize = 0;
$finalHeader = array();
} else {
$ch = curl_init($url); $ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
@ -98,6 +113,8 @@ function getContents($url, $header = array(), $opts = array()){
curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$data = curl_exec($ch); $data = curl_exec($ch);
$errorCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch); $curlError = curl_error($ch);
$curlErrno = curl_errno($ch); $curlErrno = curl_errno($ch);
$curlInfo = curl_getinfo($ch); $curlInfo = curl_getinfo($ch);
@ -108,7 +125,6 @@ function getContents($url, $header = array(), $opts = array()){
Debug::log('Cant\'t download ' . $url . ' cUrl error: ' . $curlError . ' (' . $curlErrno . ')'); Debug::log('Cant\'t download ' . $url . ' cUrl error: ' . $curlError . ' (' . $curlErrno . ')');
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$errorCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header = substr($data, 0, $headerSize); $header = substr($data, 0, $headerSize);
Debug::log('Response header: ' . $header); Debug::log('Response header: ' . $header);
@ -117,6 +133,7 @@ function getContents($url, $header = array(), $opts = array()){
$finalHeader = end($headers); $finalHeader = end($headers);
curl_close($ch); curl_close($ch);
}
switch($errorCode) { switch($errorCode) {
case 200: // Contents received case 200: // Contents received