Merge pull request #654 from LogMANOriginal/cURL

Use cURL instead of file_get_contents
This commit is contained in:
LogMANOriginal 2018-04-06 20:49:58 +02:00 committed by GitHub
commit 0f93370e92
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 106 deletions

View file

@ -64,13 +64,11 @@ class Arte7Bridge extends BridgeAbstract {
. $lang . $lang
. ($category != null ? '&category.code=' . $category : ''); . ($category != null ? '&category.code=' . $category : '');
$context = array( $header = array(
'http' => array( 'Authorization: Bearer ' . self::API_TOKEN
'header' => 'Authorization: Bearer '. self::API_TOKEN
)
); );
$input = getContents($url, false, stream_context_create($context)) or die('Could not request ARTE.'); $input = getContents($url, $header) or die('Could not request ARTE.');
$input_json = json_decode($input, true); $input_json = json_decode($input, true);
foreach($input_json['videos'] as $element) { foreach($input_json['videos'] as $element) {

View file

@ -96,17 +96,15 @@ class FacebookBridge extends BridgeAbstract {
$captcha_action = $_SESSION['captcha_action']; $captcha_action = $_SESSION['captcha_action'];
$captcha_fields = $_SESSION['captcha_fields']; $captcha_fields = $_SESSION['captcha_fields'];
$captcha_fields['captcha_response'] = preg_replace("/[^a-zA-Z0-9]+/", "", $_POST['captcha_response']); $captcha_fields['captcha_response'] = preg_replace("/[^a-zA-Z0-9]+/", "", $_POST['captcha_response']);
$http_options = array(
'http' => array( $header = array("Content-type:
'method' => 'POST', application/x-www-form-urlencoded\r\nReferer: $captcha_action\r\nCookie: noscript=1\r\n");
'user_agent' => ini_get('user_agent'), $opts = array(
'header' => array("Content-type: CURLOPT_POST => 1,
application/x-www-form-urlencoded\r\nReferer: $captcha_action\r\nCookie: noscript=1\r\n"), CURLOPT_POSTFIELDS => http_build_query($captcha_fields)
'content' => http_build_query($captcha_fields)
),
); );
$context = stream_context_create($http_options);
$html = getContents($captcha_action, false, $context); $html = getContents($captcha_action, $header, $opts);
if($html === false) { if($html === false) {
returnServerError('Failed to submit captcha response back to Facebook'); returnServerError('Failed to submit captcha response back to Facebook');
@ -120,15 +118,7 @@ class FacebookBridge extends BridgeAbstract {
//Retrieve page contents //Retrieve page contents
if(is_null($html)) { if(is_null($html)) {
$http_options = array( $header = array('Accept-Language: ' . getEnv('HTTP_ACCEPT_LANGUAGE') . "\r\n");
'http' => array(
'method' => 'GET',
'user_agent' => ini_get('user_agent'),
'header' => 'Accept-Language: ' . getEnv('HTTP_ACCEPT_LANGUAGE') . "\r\n"
)
);
$context = stream_context_create($http_options);
// First character cannot be a forward slash // First character cannot be a forward slash
if(strpos($this->getInput('u'), "/") === 0) { if(strpos($this->getInput('u'), "/") === 0) {
@ -136,14 +126,10 @@ class FacebookBridge extends BridgeAbstract {
} }
if(!strpos($this->getInput('u'), "/")) { if(!strpos($this->getInput('u'), "/")) {
$html = getSimpleHTMLDOM(self::URI . urlencode($this->getInput('u')) . '?_fb_noscript=1', $html = getSimpleHTMLDOM(self::URI . urlencode($this->getInput('u')) . '?_fb_noscript=1', $header)
false,
$context)
or returnServerError('No results for this query.'); or returnServerError('No results for this query.');
} else { } else {
$html = getSimpleHTMLDOM(self::URI . 'pages/' . $this->getInput('u') . '?_fb_noscript=1', $html = getSimpleHTMLDOM(self::URI . 'pages/' . $this->getInput('u') . '?_fb_noscript=1', $header)
false,
$context)
or returnServerError('No results for this query.'); or returnServerError('No results for this query.');
} }
} }

View file

@ -45,9 +45,7 @@ class KernelBugTrackerBridge extends BridgeAbstract {
// We use the print preview page for simplicity // We use the print preview page for simplicity
$html = getSimpleHTMLDOMCached($this->getURI() . '&format=multiple', $html = getSimpleHTMLDOMCached($this->getURI() . '&format=multiple',
86400, 86400,
false,
null, null,
0,
null, null,
true, true,
true, true,

View file

@ -109,19 +109,9 @@ class VkBridge extends BridgeAbstract
{ {
ini_set('user-agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0'); ini_set('user-agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0');
$opts = array( $header = array('Accept-language: en', 'Cookie: remixlang=3');
'http' => array(
'method' => "GET",
'user_agent' => ini_get('user_agent'),
'accept_encoding' => 'gzip',
'header' => "Accept-language: en\r\n
Cookie: remixlang=3\r\n"
)
);
$context = stream_context_create($opts); return getContents($this->getURI(), $header);
return getContents($this->getURI(), false, $context);
} }

View file

@ -140,10 +140,8 @@ class YoutubeBridge extends BridgeAbstract {
private function ytGetSimpleHTMLDOM($url){ private function ytGetSimpleHTMLDOM($url){
return getSimpleHTMLDOM($url, return getSimpleHTMLDOM($url,
$use_include_path = false, $header = array(),
$context = null, $opts = array(),
$offset = 0,
$maxLen = null,
$lowercase = true, $lowercase = true,
$forceTagsClosed = true, $forceTagsClosed = true,
$target_charset = DEFAULT_TARGET_CHARSET, $target_charset = DEFAULT_TARGET_CHARSET,

View file

@ -80,6 +80,9 @@ if(!extension_loaded('mbstring'))
if(!extension_loaded('simplexml')) if(!extension_loaded('simplexml'))
die('"simplexml" extension not loaded. Please check "php.ini"'); die('"simplexml" extension not loaded. Please check "php.ini"');
if(!extension_loaded('curl'))
die('"curl" extension not loaded. Please check "php.ini"');
// configuration checks // configuration checks
if(ini_get('allow_url_fopen') !== "1") if(ini_get('allow_url_fopen') !== "1")
die('"allow_url_fopen" is not set to "1". Please check "php.ini'); die('"allow_url_fopen" is not set to "1". Please check "php.ini');

View file

@ -1,77 +1,45 @@
<?php <?php
function getContents($url, function getContents($url, $header = array(), $opts = array()){
$use_include_path = false, $ch = curl_init($url);
$context = null, curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$offset = 0, curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$maxlen = null){
$contextOptions = array(
'http' => array(
'user_agent' => ini_get('user_agent'),
'accept_encoding' => 'gzip'
)
);
if(defined('PROXY_URL') && !defined('NOPROXY')) { if(is_array($header) && count($header) !== 0)
$contextOptions['http']['proxy'] = PROXY_URL; curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$contextOptions['http']['request_fulluri'] = true;
if(is_null($context)) { curl_setopt($ch, CURLOPT_USERAGENT, ini_get('user_agent'));
$context = stream_context_create($contextOptions); curl_setopt($ch, CURLOPT_ENCODING, '');
} else { curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
$prevContext = $context;
if(!stream_context_set_option($context, $contextOptions)) { if(is_array($opts)) {
$context = $prevContext; foreach($opts as $key => $value) {
} curl_setopt($ch, $key, $value);
} }
} }
if(is_null($maxlen)) { if(defined('PROXY_URL') && !defined('NOPROXY')) {
$content = file_get_contents($url, $use_include_path, $context, $offset); curl_setopt($ch, CURLOPT_PROXY, PROXY_URL);
} else {
$content = file_get_contents($url, $use_include_path, $context, $offset, $maxlen);
} }
$content = curl_exec($ch);
curl_close($ch);
if($content === false) if($content === false)
debugMessage('Cant\'t download ' . $url); debugMessage('Cant\'t download ' . $url);
// handle compressed data
foreach($http_response_header as $header) {
if(stristr($header, 'content-encoding')) {
switch(true) {
case stristr($header, 'gzip'):
$content = gzinflate(substr($content, 10, -8));
break;
case stristr($header, 'compress'):
//TODO
case stristr($header, 'deflate'):
//TODO
case stristr($header, 'brotli'):
//TODO
returnServerError($header . '=> Not implemented yet');
break;
case stristr($header, 'identity'):
break;
default:
returnServerError($header . '=> Unknown compression');
}
}
}
return $content; return $content;
} }
function getSimpleHTMLDOM($url, function getSimpleHTMLDOM($url,
$use_include_path = false, $header = array(),
$context = null, $opts = array(),
$offset = 0,
$maxLen = null,
$lowercase = true, $lowercase = true,
$forceTagsClosed = true, $forceTagsClosed = true,
$target_charset = DEFAULT_TARGET_CHARSET, $target_charset = DEFAULT_TARGET_CHARSET,
$stripRN = true, $stripRN = true,
$defaultBRText = DEFAULT_BR_TEXT, $defaultBRText = DEFAULT_BR_TEXT,
$defaultSpanText = DEFAULT_SPAN_TEXT){ $defaultSpanText = DEFAULT_SPAN_TEXT){
$content = getContents($url, $use_include_path, $context, $offset, $maxLen); $content = getContents($url, $header, $opts);
return str_get_html($content, return str_get_html($content,
$lowercase, $lowercase,
$forceTagsClosed, $forceTagsClosed,
@ -89,10 +57,8 @@ $defaultSpanText = DEFAULT_SPAN_TEXT){
*/ */
function getSimpleHTMLDOMCached($url, function getSimpleHTMLDOMCached($url,
$duration = 86400, $duration = 86400,
$use_include_path = false, $header = array(),
$context = null, $opts = array(),
$offset = 0,
$maxLen = null,
$lowercase = true, $lowercase = true,
$forceTagsClosed = true, $forceTagsClosed = true,
$target_charset = DEFAULT_TARGET_CHARSET, $target_charset = DEFAULT_TARGET_CHARSET,
@ -116,7 +82,7 @@ $defaultSpanText = DEFAULT_SPAN_TEXT){
&& (!defined('DEBUG') || DEBUG !== true)) { // Contents within duration && (!defined('DEBUG') || DEBUG !== true)) { // Contents within duration
$content = $cache->loadData(); $content = $cache->loadData();
} else { // Content not within duration } else { // Content not within duration
$content = getContents($url, $use_include_path, $context, $offset, $maxLen); $content = getContents($url, $header, $opts);
if($content !== false) { if($content !== false) {
$cache->saveData($content); $cache->saveData($content);
} }