Browse Source

Merge pull request #654 from LogMANOriginal/cURL

Use cURL instead of file_get_contents
LogMANOriginal 6 years ago
parent
commit
0f93370e92

+ 3 - 5
bridges/Arte7Bridge.php

@@ -64,13 +64,11 @@ class Arte7Bridge extends BridgeAbstract {
 			. $lang
 			. ($category != null ? '&category.code=' . $category : '');
 
-		$context = array(
-			'http' => array(
-				'header' => 'Authorization: Bearer '. self::API_TOKEN
-			)
+		$header = array(
+			'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);
 
 		foreach($input_json['videos'] as $element) {

+ 11 - 25
bridges/FacebookBridge.php

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

+ 0 - 2
bridges/KernelBugTrackerBridge.php

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

+ 2 - 12
bridges/VkBridge.php

@@ -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');
 
-		$opts = array(
-			'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);
+		$header = array('Accept-language: en', 'Cookie: remixlang=3');
 
-		return getContents($this->getURI(), false, $context);
+		return getContents($this->getURI(), $header);
 	}
 
 

+ 2 - 4
bridges/YoutubeBridge.php

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

+ 3 - 0
index.php

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

+ 24 - 58
lib/contents.php

@@ -1,77 +1,45 @@
 <?php
-function getContents($url,
-$use_include_path = false,
-$context = null,
-$offset = 0,
-$maxlen = null){
-	$contextOptions = array(
-		'http' => array(
-			'user_agent' => ini_get('user_agent'),
-			'accept_encoding' => 'gzip'
-		)
-	);
+function getContents($url, $header = array(), $opts = array()){
+	$ch = curl_init($url);
+	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
 
-	if(defined('PROXY_URL') && !defined('NOPROXY')) {
-		$contextOptions['http']['proxy'] = PROXY_URL;
-		$contextOptions['http']['request_fulluri'] = true;
+	if(is_array($header) && count($header) !== 0)
+		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);
 
-		if(is_null($context)) {
-			$context = stream_context_create($contextOptions);
-		} else {
-			$prevContext = $context;
-			if(!stream_context_set_option($context, $contextOptions)) {
-				$context = $prevContext;
-			}
+	if(is_array($opts)) {
+		foreach($opts as $key => $value) {
+			curl_setopt($ch, $key, $value);
 		}
 	}
 
-	if(is_null($maxlen)) {
-		$content = file_get_contents($url, $use_include_path, $context, $offset);
-	} else {
-		$content = file_get_contents($url, $use_include_path, $context, $offset, $maxlen);
+	if(defined('PROXY_URL') && !defined('NOPROXY')) {
+		curl_setopt($ch, CURLOPT_PROXY, PROXY_URL);
 	}
 
+	$content = curl_exec($ch);
+	curl_close($ch);
+
 	if($content === false)
 		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;
 }
 
 function getSimpleHTMLDOM($url,
-$use_include_path = false,
-$context = null,
-$offset = 0,
-$maxLen = null,
+$header = array(),
+$opts = array(),
 $lowercase = true,
 $forceTagsClosed = true,
 $target_charset = DEFAULT_TARGET_CHARSET,
 $stripRN = true,
 $defaultBRText = DEFAULT_BR_TEXT,
 $defaultSpanText = DEFAULT_SPAN_TEXT){
-	$content = getContents($url, $use_include_path, $context, $offset, $maxLen);
+	$content = getContents($url, $header, $opts);
 	return str_get_html($content,
 	$lowercase,
 	$forceTagsClosed,
@@ -89,10 +57,8 @@ $defaultSpanText = DEFAULT_SPAN_TEXT){
  */
 function getSimpleHTMLDOMCached($url,
 $duration = 86400,
-$use_include_path = false,
-$context = null,
-$offset = 0,
-$maxLen = null,
+$header = array(),
+$opts = array(),
 $lowercase = true,
 $forceTagsClosed = true,
 $target_charset = DEFAULT_TARGET_CHARSET,
@@ -116,7 +82,7 @@ $defaultSpanText = DEFAULT_SPAN_TEXT){
 	&& (!defined('DEBUG') || DEBUG !== true)) { // Contents within duration
 		$content = $cache->loadData();
 	} else { // Content not within duration
-		$content = getContents($url, $use_include_path, $context, $offset, $maxLen);
+		$content = getContents($url, $header, $opts);
 		if($content !== false) {
 			$cache->saveData($content);
 		}