From: Roland Häder <roland@mxchange.org>
Date: Thu, 26 Nov 2009 00:32:00 +0000 (+0000)
Subject: Broken mask fixed (%%) and request dispatcher sendRawRequest() rewritten to timeout... 
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=6a545ced209c14b75d2dc04f43ee010e9ae9b47c;p=mailer.git

Broken mask fixed (%%) and request dispatcher sendRawRequest() rewritten to timeout after 15 seconds of no reply in non-blocking mode
---

diff --git a/inc/filter-functions.php b/inc/filter-functions.php
index e96ab005b5..b797f90f15 100644
--- a/inc/filter-functions.php
+++ b/inc/filter-functions.php
@@ -247,7 +247,7 @@ function unregisterFilter ($filterName, $filterFunction, $force = false, $dry_ru
 // "Runs" the given filters, data is optional and can be any type of data
 function runFilterChain ($filterName, $data = null) {
 	// Is that filter chain there?
-	if (!isset($GLOBALS['cache_array']['filter']['chains'][$filterName])) {
+	if ((!isset($GLOBALS['cache_array']['filter']['chains'][$filterName])) && (isDebugModeEnabled())) {
 		// We should find all these non-existing filter chains
 		//* Only for tracking: */ if ($filterName != 'sql_admin_extra_data') {
 		//* Only for tracking: */ debug_report_bug('Filter chain <strong>' . $filterName . '</strong> not found!');
diff --git a/inc/functions.php b/inc/functions.php
index 267aa18fe3..aa691935e1 100644
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -1750,6 +1750,9 @@ function sendGetRequest ($script, $data = array()) {
 	// Add script data
 	$script .= $body;
 
+	// Remove trailed & to make it more conform
+	if (substr($script, -1, 1) == '&') $script = substr($script, 0, -1);
+
 	// Generate GET request header
 	$request  = 'GET /' . trim($script) . ' HTTP/1.1' . getConfig('HTTP_EOL');
 	$request .= 'Host: ' . $host . getConfig('HTTP_EOL');
@@ -1826,17 +1829,22 @@ function sendRawRequest ($host, $request) {
 	//* DEBUG: */ die("SCRIPT=" . $script.'<br />');
 	if ($useProxy === true) {
 		// Connect to host through proxy connection
-		$fp = @fsockopen(compileRawCode(getConfig('proxy_host')), bigintval(getConfig('proxy_port')), $errno, $errdesc, 30);
+		$fp = fsockopen(compileRawCode(getConfig('proxy_host')), bigintval(getConfig('proxy_port')), $errno, $errdesc, 30);
 	} else {
 		// Connect to host directly
-		$fp = @fsockopen($host, 80, $errno, $errdesc, 30);
+		$fp = fsockopen($host, 80, $errno, $errdesc, 30);
 	}
 
 	// Is there a link?
 	if (!is_resource($fp)) {
 		// Failed!
+		logDebugMessage(__FUNCTION__, __LINE__, $errdesc . ' (' . $errno . ')');
 		return $response;
-	} // END - if
+	} elseif ((!stream_set_blocking($fp, 0)) || (!stream_set_timeout($fp, 1))) {
+		// Cannot set non-blocking mode or timeout
+		logDebugMessage(__FUNCTION__, __LINE__, socket_strerror(socket_last_error()));
+		return $response;
+	}
 
 	// Do we use proxy?
 	if ($useProxy === true) {
@@ -1856,7 +1864,7 @@ function sendRawRequest ($host, $request) {
 		//* DEBUG: */ print('<strong>proxyTunnel=</strong><pre>' . $proxyTunnel.'</pre>');
 
 		// Write request
-		fputs($fp, $proxyTunnel);
+		fwrite($fp, $proxyTunnel);
 
 		// Got response?
 		if (feof($fp)) {
@@ -1874,11 +1882,39 @@ function sendRawRequest ($host, $request) {
 	} // END - if
 
 	// Write request
-	fputs($fp, $request);
+	fwrite($fp, $request);
+
+	// Start counting
+	$start = microtime(true);
 
 	// Read response
 	while (!feof($fp)) {
-		$response[] = trim(fgets($fp, 1024));
+		// Get info from stream
+		$info = stream_get_meta_data($fp);
+
+		// Is it timed out? 15 seconds is a really patient...
+		if (($info['timed_out'] == true) || (microtime(true) - $start) > 15) {
+			// Timeout
+			logDebugMessage(__FUNCTION__, __LINE__, 'Timed out to get data from host ' . $host);
+
+			// Abort here
+			break;
+		} // END - if
+
+		// Get line from stream
+		$line = fgets($fp, 128);
+
+		// Ignore empty lines because of non-blocking mode
+		if (empty($line)) {
+			// uslepp a little to avoid 100% CPU load
+			usleep(10);
+
+			// Skip this
+			continue;
+		} // END - if
+
+		// Add it to response
+		$response[] = trim($line);
 	} // END - while
 
 	// Close socket
@@ -1902,8 +1938,11 @@ function sendRawRequest ($host, $request) {
 
 	//* DEBUG: */ print('<strong>Response:</strong><pre>'.print_r($response, true).'</pre>');
 
-	// Proxy agent found?
-	if ((substr(strtolower($response[0]), 0, 11) == 'proxy-agent') && ($useProxy === true)) {
+	// Proxy agent found or something went wrong?
+	if (!isset($response[0])) {
+		// No response, maybe timeout
+		$response = array('', '', '');
+	} elseif ((substr(strtolower($response[0]), 0, 11) == 'proxy-agent') && ($useProxy === true)) {
 		// Proxy header detected, so remove two lines
 		array_shift($response);
 		array_shift($response);
@@ -2836,7 +2875,7 @@ function changeDataInFile ($FQFN, $comment, $prefix, $suffix, $DATA, $seek=0) {
 					} // END - if
 
 					// Write to temp file
-					fputs($fp_tmp, $line);
+					fwrite($fp_tmp, $line);
 				} // END - while
 
 				// Close temp file
diff --git a/inc/language/de.php b/inc/language/de.php
index 201ef9e171..3bf2f04963 100644
--- a/inc/language/de.php
+++ b/inc/language/de.php
@@ -1046,7 +1046,7 @@ addMessages(array(
 	'EXTENSION_PROBLEM_EXT_INACTIVE' => "Kann nicht fortfahren! Fehler: Erweiterung <span class=\"data\">%s</span> deaktiviert.",
 	'ADMIN_EXTENSION_PROBLEM_EXT_INACTIVE' => "Die Erweiterung <span class=\"data\">%s</span> ist derzeit deaktiviert. <a href=\"{%url=modules.php?module=admin&amp;what=extensions%}\">Hier</a> k&ouml;nnen Sie diese aktivieren.",
 	'EXTENSION_PROBLEM_EXT_NOT_INSTALLED' => "Kann nicht fortfahren! Fehler: Erweiterung <span class=\"data\">%s</span> nicht installiert.",
-	'ADMIN_EXTENSION_PROBLEM_EXT_NOT_INSTALLED' => "Die Erweiterung <span class=\"data\">%s</span> ist derzeit nicht installiert. <a href=\"{%url=modules.php?module=admin&amp;what=extensions%}\">Hier</a> k&ouml;nnen Sie diese aktivieren.",
+	'ADMIN_EXTENSION_PROBLEM_EXT_NOT_INSTALLED' => "Die Erweiterung <span class=\"data\">%s</span> ist derzeit nicht installiert. <a href=\"{%%url=modules.php?module=admin&amp;what=extensions%%}\">Hier</a> k&ouml;nnen Sie diese aktivieren.",
 	'EXTENSION_PROBLEM_NOT_INSTALLED' => "Erweiterung <span class=\"data\">%s</span> nicht installiert.",
 	'EXTENSION_PROBLEM_UNSET_EXT' => "Skriptfehler: Parameter <strong>ext</strong> nicht gesetzt.",
 	'EXTENSION_WARNING_EXT_INACTIVE' => "Die Erweiterung <span class=\"data\">%s</span> ist nicht aktiv. Dies kann zu St&ouml;rungen f&uuml;hren.<br />\nBitte aktivieren Sie diese unter <em>Verschiedenes-&gt;Erweiterungen</em>.",
diff --git a/inc/libs/yoomedia_functions.php b/inc/libs/yoomedia_functions.php
index 75a0362ebc..a0e9c5a70f 100644
--- a/inc/libs/yoomedia_functions.php
+++ b/inc/libs/yoomedia_functions.php
@@ -39,6 +39,57 @@
 // Some security stuff...
 if (!defined('__SECURITY')) {
 	die();
+} // END - if
+
+// Queries the given Yoo!Media API 2.0 script
+function YOOMEDIA_QUERY_API ($script, $countQuery = true) {
+	// Init response array
+	$response = array();
+
+	// Enougth queries left?
+	if ((getConfig('yoomedia_requests_remain') > 0) || ($countQuery === false)) {
+		// Prepare the low-level request
+		$requestString = sprintf("http://www.yoomedia.de/interface_2.0/%s?id=%s&sid=%s&pw=%s&reload=%s&ma=%s&uebrig=%s&verguetung=%s&erotik=%s",
+			$script,
+			getConfig('yoomedia_id'),
+			getConfig('yoomedia_sid'),
+			getConfig('yoomedia_passwd'),
+			getConfig('yoomedia_tm_max_reload'),
+			getConfig('yoomedia_tm_min_wait'),
+			getConfig('yoomedia_tm_clicks_remain'),
+			getConfig('yoomedia_tm_min_pay'),
+			getConfig('yoomedia_erotic_allowed')
+		);
+
+		// Run the query
+		$response = sendGetRequest($requestString);
+
+		// Convert from ISO to UTF-8 only if count is > 3 because <= 3 means timeout
+		if (count($response) > 3) {
+			// Convert all lines to UTF-8
+			foreach ($response as $k => $v) {
+				// Convert the line
+				$response[$k] = iconv('windows-1252', 'UTF-8//TRANSLIT', $v);
+				/*
+				// iconv()-less ISO-8859-1 -> UTF-8
+				$response[$k] = preg_replace(
+					"/([\x80-\xFF])/e",
+					"chr(0xC0|ord('\\1')>>6).chr(0x80|ord('\\1')&0x3F)",
+					$v
+				);
+				*/
+			} // END - foreach
+		} // END - if
+
+		// Shall we count the query as used?
+		if ($countQuery === true) {
+			// Then update the config!
+			updateConfiguration('yoomedia_requests_remain', 1, '-');
+		} // END - if
+	} // END - if
+
+	// Return the data
+	return $response;
 }
 
 // Test if the extension settings did work
@@ -91,53 +142,6 @@ function YOOMEDIA_TEST_CONFIG ($data) {
 	return ($errorCode == '0');
 }
 
-// Queries the given Yoo!Media API 2.0 script
-function YOOMEDIA_QUERY_API ($script, $countQuery = true) {
-	// Init response array
-	$response = array();
-
-	// Enougth queries left?
-	if ((getConfig('yoomedia_requests_remain') > 0) || ($countQuery === false)) {
-		// Prepare the low-level request
-		$requestString = sprintf("http://www.yoomedia.de/interface_2.0/%s?id=%s&sid=%s&pw=%s&reload=%s&ma=%s&uebrig=%s&verguetung=%s&erotik=%s",
-			$script,
-			getConfig('yoomedia_id'),
-			getConfig('yoomedia_sid'),
-			getConfig('yoomedia_passwd'),
-			getConfig('yoomedia_tm_max_reload'),
-			getConfig('yoomedia_tm_min_wait'),
-			getConfig('yoomedia_tm_clicks_remain'),
-			getConfig('yoomedia_tm_min_pay'),
-			getConfig('yoomedia_erotic_allowed')
-		);
-
-		// Run the query
-		$response = sendGetRequest($requestString);
-
-		// Convert from ISO to UTF-8
-		foreach ($response as $k => $v) {
-			$response[$k] = iconv('windows-1252', 'UTF-8//TRANSLIT', $v);
-			/*
-			// iconv()-less ISO-8859-1 -> UTF-8
-			$response[$k] = preg_replace(
-				"/([\x80-\xFF])/e",
-				"chr(0xC0|ord('\\1')>>6).chr(0x80|ord('\\1')&0x3F)",
-				$v
-			);
-			*/
-		} // END - if
-
-		// Shall we count the query as used?
-		if ($countQuery === true) {
-			// Then update the config!
-			updateConfiguration('yoomedia_requests_remain', 1, '-');
-		} // END - if
-	} // END - if
-
-	// Return the data
-	return $response;
-}
-
 // "Getter" for a parsed result for all text mails. This means an array without
 // the header lines will be returned
 function YOOMEDIA_GET_PARSED_RESULT_TEXTMAILS () {