X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=inc%2Ffunctions.php;h=69477a7dc847bc199728c6894e5948d69559cee1;hb=3ce031a4ae807dd16a6e08e3e592fe7128de724a;hp=278c570375f636a7c09022d03a7fcadb2c1d0c32;hpb=4e5020660b07f30b7bf3ccc0a2ca664a19a21c0d;p=mailer.git diff --git a/inc/functions.php b/inc/functions.php index 278c570375..69477a7dc8 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -134,7 +134,7 @@ function sendHttpHeaders () { $now = gmdate('D, d M Y H:i:s') . ' GMT'; // Send HTTP header - sendHeader('HTTP/1.1 200'); + sendHeader('HTTP/1.1 200 OK'); // General headers for no caching sendHeader('Expires: ' . $now); // RFC2616 - Section 14.21 @@ -496,7 +496,7 @@ function loadEmailTemplate ($template, $content = array(), $userid = '0') { $GLOBALS['tpl_content'] = readFromFile($FQFN); // Run code - $GLOBALS['tpl_content'] = "\$newContent = decodeEntities(\"".compileRawCode(escapeQuotes($GLOBALS['tpl_content']))."\");"; + $GLOBALS['tpl_content'] = '$newContent = decodeEntities("' . compileRawCode(escapeQuotes($GLOBALS['tpl_content'])) . '");'; eval($GLOBALS['tpl_content']); } elseif (!empty($template)) { // Template file not found! @@ -536,7 +536,7 @@ function sendEmail ($toEmail, $subject, $message, $isHtml = 'N', $mailHeader = ' //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "TO={$toEmail},SUBJECT={$subject}
"); // Compile subject line (for POINTS constant etc.) - eval("\$subject = decodeEntities(\"".compileRawCode(escapeQuotes($subject))."\");"); + eval('$subject = decodeEntities("' . compileRawCode(escapeQuotes($subject)) . '");'); // Set from header if ((!isInStringIgnoreCase('@', $toEmail)) && ($toEmail > 0)) { @@ -582,10 +582,10 @@ function sendEmail ($toEmail, $subject, $message, $isHtml = 'N', $mailHeader = ' } // Compile "TO" - eval("\$toEmail = \"".compileRawCode(escapeQuotes($toEmail))."\";"); + eval('$toEmail = "' . compileRawCode(escapeQuotes($toEmail)) . '";'); // Compile "MSG" - eval("\$message = \"".str_replace('$', '$', compileRawCode(escapeQuotes($message)))."\";"); + eval('$message = "' . str_replace('$', '$', compileRawCode(escapeQuotes($message))) . '";'); // Fix HTML parameter (default is no!) if (empty($isHtml)) $isHtml = 'N'; @@ -599,13 +599,13 @@ Message : ' . htmlentities(utf8_decode($message)) . ' '); } elseif (($isHtml == 'Y') && (isExtensionActive('html_mail'))) { // Send mail as HTML away - sendHtmlEmail($toEmail, $subject, $message, $mailHeader); + return sendHtmlEmail($toEmail, $subject, $message, $mailHeader); } elseif (!empty($toEmail)) { // Send Mail away - sendRawEmail($toEmail, $subject, $message, $mailHeader); + return sendRawEmail($toEmail, $subject, $message, $mailHeader); } elseif ($isHtml != 'Y') { // Problem found! - sendRawEmail(getConfig('WEBMASTER'), '[PROBLEM:]' . $subject, $message, $mailHeader); + return sendRawEmail(getConfig('WEBMASTER'), '[PROBLEM:]' . $subject, $message, $mailHeader); } } @@ -634,7 +634,7 @@ function sendRawEmail ($toEmail, $subject, $message, $from) { $mail = new PHPMailer(); // Set charset to UTF-8 - $mail->CharSet('UTF-8'); + $mail->CharSet = 'UTF-8'; // Path for PHPMailer $mail->PluginDir = sprintf("%sinc/phpmailer/", getConfig('PATH')); @@ -665,9 +665,21 @@ function sendRawEmail ($toEmail, $subject, $message, $from) { $mail->AddCustomHeader('Errors-To:' . getConfig('WEBMASTER')); $mail->AddCustomHeader('X-Loop:' . getConfig('WEBMASTER')); $mail->Send(); + + // Has an error occured? + if (!empty($mail->ErrorInfo)) { + // Log message + logDebugMessage(__FUNCTION__, __LINE__, 'Error while sending mail: ' . $mail->ErrorInfo); + + // Raise an error + return false; + } else { + // All fine! + return true; + } } else { // Use legacy mail() command - mail($toEmail, $subject, decodeEntities($message), $from); + return mail($toEmail, $subject, decodeEntities($message), $from); } } @@ -1837,14 +1849,26 @@ function sendRawRequest ($host, $request) { $useProxy = true; } // END - if + // Load include + loadIncludeOnce('inc/classes/resolver.class.php'); + + // Get resolver instance + $resolver = new HostnameResolver(); + // Open connection //* DEBUG: */ die("SCRIPT=" . $script.'
'); if ($useProxy === true) { + // Resolve hostname into IP address + $ip = $resolver->resolveHostname(compileRawCode(getConfig('proxy_host'))); + // Connect to host through proxy connection - $fp = fsockopen(compileRawCode(getConfig('proxy_host')), bigintval(getConfig('proxy_port')), $errno, $errdesc, 30); + $fp = fsockopen($ip, bigintval(getConfig('proxy_port')), $errno, $errdesc, 30); } else { + // Resolve hostname into IP address + $ip = $resolver->resolveHostname($host); + // Connect to host directly - $fp = fsockopen($host, 80, $errno, $errdesc, 30); + $fp = fsockopen($ip, 80, $errno, $errdesc, 30); } // Is there a link? @@ -1860,35 +1884,13 @@ function sendRawRequest ($host, $request) { // Do we use proxy? if ($useProxy === true) { - // Generate CONNECT request header - $proxyTunnel = 'CONNECT ' . $host . ':80 HTTP/1.1' . getConfig('HTTP_EOL'); - $proxyTunnel .= 'Host: ' . $host . getConfig('HTTP_EOL'); - - // Use login data to proxy? (username at least!) - if (getConfig('proxy_username') != '') { - // Add it as well - $encodedAuth = base64_encode(compileRawCode(getConfig('proxy_username')) . ':' . compileRawCode(getConfig('proxy_password'))); - $proxyTunnel .= 'Proxy-Authorization: Basic ' . $encodedAuth . getConfig('HTTP_EOL'); - } // END - if + // Setup proxy tunnel + $response = setupProxyTunnel($host, $fp); - // Add last new-line - $proxyTunnel .= getConfig('HTTP_EOL'); - //* DEBUG: */ print('proxyTunnel=
' . $proxyTunnel.'
'); - - // Write request - fwrite($fp, $proxyTunnel); - - // Got response? - if (feof($fp)) { - // No response received - return $response; - } // END - if - - // Read the first line - $resp = trim(fgets($fp, 10240)); - $respArray = explode(' ', $resp); - if ((strtolower($respArray[0]) !== 'http/1.0') || ($respArray[1] != '200')) { + // If the response is invalid, abort + if ((count($response) == 3) && (empty($response[0])) && (empty($response[1])) && (empty($response[2]))) { // Invalid response! + logDebugMessage(__FUNCTION__, __LINE__, 'Proxy tunnel not working?'); return $response; } // END - if } // END - if @@ -1935,7 +1937,7 @@ function sendRawRequest ($host, $request) { // Time request if debug-mode is enabled if (isDebugModeEnabled()) { // Add debug message... - logDebugMessage(__FUNCTION__, __LINE__, 'Request took ' . (microtime(true) - $start) . ' seconds.'); + logDebugMessage(__FUNCTION__, __LINE__, 'Request took ' . (microtime(true) - $start) . ' seconds and returned ' . count($response) . ' line(s).'); } // END - if // Skip first empty lines @@ -1961,6 +1963,7 @@ function sendRawRequest ($host, $request) { if (!isset($response[0])) { // No response, maybe timeout $response = array('', '', ''); + logDebugMessage(__FUNCTION__, __LINE__, 'Invalid empty response array, maybe timed out?'); } elseif ((substr(strtolower($response[0]), 0, 11) == 'proxy-agent') && ($useProxy === true)) { // Proxy header detected, so remove two lines array_shift($response); @@ -1970,6 +1973,7 @@ function sendRawRequest ($host, $request) { // Was the request successfull? if ((!isInStringIgnoreCase('200 OK', $response[0])) || (empty($response[0]))) { // Not found / access forbidden + logDebugMessage(__FUNCTION__, __LINE__, 'Unexpected status code ' . $response[0] . ' detected. "200 OK" was expected.'); $response = array('', '', ''); } // END - if @@ -1977,6 +1981,47 @@ function sendRawRequest ($host, $request) { return $response; } +// Sets up a proxy tunnel for given hostname and through resource +function setupProxyTunnel ($host, $resource) { + // Initialize array + $response = array('', '', ''); + + // Generate CONNECT request header + $proxyTunnel = 'CONNECT ' . $host . ':80 HTTP/1.1' . getConfig('HTTP_EOL'); + $proxyTunnel .= 'Host: ' . $host . getConfig('HTTP_EOL'); + + // Use login data to proxy? (username at least!) + if (getConfig('proxy_username') != '') { + // Add it as well + $encodedAuth = base64_encode(compileRawCode(getConfig('proxy_username')) . ':' . compileRawCode(getConfig('proxy_password'))); + $proxyTunnel .= 'Proxy-Authorization: Basic ' . $encodedAuth . getConfig('HTTP_EOL'); + } // END - if + + // Add last new-line + $proxyTunnel .= getConfig('HTTP_EOL'); + //* DEBUG: */ print('proxyTunnel=
' . $proxyTunnel.'
'); + + // Write request + fwrite($fp, $proxyTunnel); + + // Got response? + if (feof($fp)) { + // No response received + return $response; + } // END - if + + // Read the first line + $resp = trim(fgets($fp, 10240)); + $respArray = explode(' ', $resp); + if ((strtolower($respArray[0]) !== 'http/1.0') || ($respArray[1] != '200')) { + // Invalid response! + return $response; + } // END - if + + // All fine! + return $respArray; +} + // Taken from www.php.net isInStringIgnoreCase() user comments function isEmailValid ($email) { // Check first part of email address @@ -2676,11 +2721,12 @@ function isUrlValidSimple ($url) { $pattern['d1g12'] = $http . $domain1 . $getstring1; $pattern['d2g12'] = $http . $domain2 . $getstring1; $pattern['ipg12'] = $http . $ip . $getstring1; + // Test all patterns $reg = false; foreach ($pattern as $key => $pat) { // Debug regex? - if (isDebugRegExpressionEnabled()) { + if (isDebugRegularExpressionEnabled()) { // @TODO Are these convertions still required? $pat = str_replace('.', "\.", $pat); $pat = str_replace('@', "\@", $pat); @@ -2688,7 +2734,7 @@ function isUrlValidSimple ($url) { } // END - if // Check if expression matches - $reg = ($reg || preg_match(('^' . $pat.'^'), $url)); + $reg = ($reg || preg_match(('^' . $pat . '^'), $url)); // Does it match? if ($reg === true) break; @@ -2895,14 +2941,14 @@ function handleLoginFailures ($accessLevel) { $OUT = ''; // Is the session data set? - if ((isSessionVariableSet('mxchange_' . $accessLevel . '_failures')) && (isSessionVariableSet('mxchange_' . $accessLevel . '_last_failure'))) { + if ((isSessionVariableSet('mailer_' . $accessLevel . '_failures')) && (isSessionVariableSet('mailer_' . $accessLevel . '_last_failure'))) { // Ignore zero values - if (getSession('mxchange_' . $accessLevel . '_failures') > 0) { + if (getSession('mailer_' . $accessLevel . '_failures') > 0) { // Non-guest has login failures found, get both data and prepare it for template //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "accessLevel={$accessLevel}
"); $content = array( - 'login_failures' => getSession('mxchange_' . $accessLevel . '_failures'), - 'last_failure' => generateDateTime(getSession('mxchange_' . $accessLevel . '_last_failure'), 2) + 'login_failures' => getSession('mailer_' . $accessLevel . '_failures'), + 'last_failure' => generateDateTime(getSession('mailer_' . $accessLevel . '_last_failure'), 2) ); // Load template @@ -2910,8 +2956,8 @@ function handleLoginFailures ($accessLevel) { } // END - if // Reset session data - setSession('mxchange_' . $accessLevel . '_failures', ''); - setSession('mxchange_' . $accessLevel . '_last_failure', ''); + setSession('mailer_' . $accessLevel . '_failures', ''); + setSession('mailer_' . $accessLevel . '_last_failure', ''); } // END - if // Return rendered content @@ -3886,6 +3932,21 @@ function searchDirsRecursive ($dir, &$last_changed, $lookFor = 'Date') { } // END - foreach } +// "Fixes" null or empty string to count of dashes +function fixNullEmptyToDashes ($str, $num) { + // Use str as default + $return = $str; + + // Is it empty? + if ((is_null($str)) || (trim($str) == '')) { + // Set it + $return = str_repeat('-', $num); + } // END - if + + // Return final string + return $return; +} + ////////////////////////////////////////////////// // AUTOMATICALLY RE-GENERATED MISSING FUNCTIONS // //////////////////////////////////////////////////