Skip to content

Commit

Permalink
[contents] Use file_get_contents when in CLI mode & no certs (#962)
Browse files Browse the repository at this point in the history
file_get_contents can natively use system root certificates, so use file_get_contents when in CLI mode with no root certificates for cURL.
  • Loading branch information
triatic authored and logmanoriginal committed Dec 26, 2018
1 parent bb2329f commit dc83962
Showing 1 changed file with 60 additions and 43 deletions.
103 changes: 60 additions & 43 deletions lib/contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,71 +52,88 @@ function getContents($url, $header = array(), $opts = array()){
$params = [$url];
$cache->setParameters($params);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// 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;
}

if(is_array($header) && count($header) !== 0) {
$curlError = '';
$curlErrno = '';
$headerSize = 0;
$finalHeader = array();
} else {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Debug::log('Setting headers: ' . json_encode($header));
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
if(is_array($header) && count($header) !== 0) {

}
Debug::log('Setting headers: ' . json_encode($header));
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

}

curl_setopt($ch, CURLOPT_USERAGENT, ini_get('user_agent'));
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);

curl_setopt($ch, CURLOPT_USERAGENT, ini_get('user_agent'));
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
if(is_array($opts) && count($opts) !== 0) {

if(is_array($opts) && count($opts) !== 0) {
Debug::log('Setting options: ' . json_encode($opts));

Debug::log('Setting options: ' . json_encode($opts));
foreach($opts as $key => $value) {
curl_setopt($ch, $key, $value);
}

foreach($opts as $key => $value) {
curl_setopt($ch, $key, $value);
}

}
if(defined('PROXY_URL') && !defined('NOPROXY')) {

if(defined('PROXY_URL') && !defined('NOPROXY')) {
Debug::log('Setting proxy url: ' . PROXY_URL);
curl_setopt($ch, CURLOPT_PROXY, PROXY_URL);

Debug::log('Setting proxy url: ' . PROXY_URL);
curl_setopt($ch, CURLOPT_PROXY, PROXY_URL);
}

}
// We always want the response header as part of the data!
curl_setopt($ch, CURLOPT_HEADER, true);

// We always want the response header as part of the data!
curl_setopt($ch, CURLOPT_HEADER, true);
// Build "If-Modified-Since" header
if(!Debug::isEnabled() && $time = $cache->getTime()) { // Skip if cache file doesn't exist
Debug::log('Adding If-Modified-Since');
curl_setopt($ch, CURLOPT_TIMEVALUE, $time);
curl_setopt($ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
}

// Build "If-Modified-Since" header
if(!Debug::isEnabled() && $time = $cache->getTime()) { // Skip if cache file doesn't exist
Debug::log('Adding If-Modified-Since');
curl_setopt($ch, CURLOPT_TIMEVALUE, $time);
curl_setopt($ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
}
// Enables logging for the outgoing header
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

// Enables logging for the outgoing header
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$data = curl_exec($ch);
$errorCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

$data = curl_exec($ch);
$curlError = curl_error($ch);
$curlErrno = curl_errno($ch);
$curlInfo = curl_getinfo($ch);
$curlError = curl_error($ch);
$curlErrno = curl_errno($ch);
$curlInfo = curl_getinfo($ch);

Debug::log('Outgoing header: ' . json_encode($curlInfo));
Debug::log('Outgoing header: ' . json_encode($curlInfo));

if($data === false)
Debug::log('Cant\'t download ' . $url . ' cUrl error: ' . $curlError . ' (' . $curlErrno . ')');
if($data === false)
Debug::log('Cant\'t download ' . $url . ' cUrl error: ' . $curlError . ' (' . $curlErrno . ')');

$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$errorCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header = substr($data, 0, $headerSize);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($data, 0, $headerSize);

Debug::log('Response header: ' . $header);
Debug::log('Response header: ' . $header);

$headers = parseResponseHeader($header);
$finalHeader = end($headers);
$headers = parseResponseHeader($header);
$finalHeader = end($headers);

curl_close($ch);
curl_close($ch);
}

switch($errorCode) {
case 200: // Contents received
Expand Down

0 comments on commit dc83962

Please sign in to comment.