X-Git-Url: https://git.mxchange.org/?p=mailer.git;a=blobdiff_plain;f=inc%2Ffunctions.php;h=66b0c63e190892a6e095c919f5eabb0edf6bed47;hp=278c570375f636a7c09022d03a7fcadb2c1d0c32;hb=8d70fd41c880ca61bafb47a69a6411c741c71dff;hpb=4e5020660b07f30b7bf3ccc0a2ca664a19a21c0d diff --git a/inc/functions.php b/inc/functions.php index 278c570375..66b0c63e19 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 @@ -149,19 +149,21 @@ function sendHttpHeaders () { // Compiles the final output function compileFinalOutput () { // Init counter - $cnt = '0'; + $cnt = 0; // Add page header and footer addPageHeaderFooter(); // Compile all out - while (((strpos($GLOBALS['output'], '{--') > 0) || (strpos($GLOBALS['output'], '{!') > 0) || (strpos($GLOBALS['output'], '{?') > 0)) && ($cnt < 3)) { + while (((strpos($GLOBALS['output'], '{--') !== false) || (strpos($GLOBALS['output'], '{!') !== false) || (strpos($GLOBALS['output'], '{?') !== false) || (strpos($GLOBALS['output'], '{%') !== false)) && ($cnt < 3)) { // Init common variables $content = array(); $newContent = ''; // Compile it - $eval = "\$newContent = \"".compileCode(escapeQuotes($GLOBALS['output']))."\";"; + //* DEBUG: */ print '
'.htmlentities($GLOBALS['output']).'
'; + $eval = '$newContent = "' . compileCode(escapeQuotes($GLOBALS['output'])) . '";'; + //* DEBUG: */ die('
'.htmlentities($eval).'
'); eval($eval); // Was that eval okay? @@ -169,6 +171,8 @@ function compileFinalOutput () { // Something went wrong! debug_report_bug('Evaluation error:
' . linenumberCode($eval) . '
', false); } // END - if + + // Use it again $GLOBALS['output'] = $newContent; // Count round @@ -496,7 +500,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 +540,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 +586,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 +603,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 +638,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 +669,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); } } @@ -773,6 +789,9 @@ function translatePoolType ($type) { // Translates the american decimal dot into a german comma function translateComma ($dotted, $cut = true, $max = '0') { + // First, cast all to double, due to PHP changes + $dotted = (double) $dotted; + // Default is 3 you can change this in admin area "Misc -> Misc Options" if (!isConfigEntrySet('max_comma')) setConfigEntry('max_comma', 3); @@ -1182,7 +1201,7 @@ function addSelectionBox ($type, $default, $prefix = '', $id = '0', $class = 're $year = date('Y', time()); // Use configured min age or fixed? - if ((isExtensionActive('other')) && (getExtensionVersion('other') >= '0.2.1')) { + if (isExtensionInstalledAndNewer('order', '0.2.1')) { // Configured $startYear = $year - getConfig('min_age'); } else { @@ -1210,7 +1229,7 @@ function addSelectionBox ($type, $default, $prefix = '', $id = '0', $class = 're // Get current year and subtract the configured minimum age $OUT .= "\n"; // Calculate earliest year depending on extension version - if ((isExtensionActive('other')) && (getExtensionVersion('other') >= '0.2.1')) { + if (isExtensionInstalledAndNewer('order', '0.2.1')) { // Use configured minimum age $year = date('Y', time()) - getConfig('min_age'); } else { @@ -1767,11 +1786,9 @@ function sendGetRequest ($script, $data = array()) { } else { $request .= 'User-Agent: ' . getConfig('TITLE') . '/' . getConfig('VERSION') . getConfig('HTTP_EOL'); } - $request .= 'Accept: text/plain;q=0.8' . getConfig('HTTP_EOL'); + $request .= 'Accept: image/png,image/*;q=0.8,text/plain,text/html,*/*;q=0.5' . getConfig('HTTP_EOL'); $request .= 'Accept-Charset: UTF-8,*' . getConfig('HTTP_EOL'); - $request .= 'Cache-Control: no-cache' . getConfig('HTTP_EOL'); - $request .= 'Content-Type: text/plain' . getConfig('HTTP_EOL'); - $request .= 'Content-Length: 0' . getConfig('HTTP_EOL'); + $request .= 'Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0' . getConfig('HTTP_EOL'); $request .= 'Connection: close' . getConfig('HTTP_EOL'); $request .= getConfig('HTTP_EOL'); @@ -1798,7 +1815,7 @@ function sendPostRequest ($script, $postData) { $body = http_build_query($postData, '', '&'); // Generate POST request header - $request = 'POST /' . trim($script) . ' HTTP/1.1' . getConfig('HTTP_EOL'); + $request = 'POST /' . trim($script) . ' HTTP/1.0' . getConfig('HTTP_EOL'); $request .= 'Host: ' . $host . getConfig('HTTP_EOL'); $request .= 'Referer: ' . getConfig('URL') . '/admin.php' . getConfig('HTTP_EOL'); $request .= 'User-Agent: ' . getConfig('TITLE') . '/' . getConfig('FULL_VERSION') . getConfig('HTTP_EOL'); @@ -1837,14 +1854,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 +1889,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 - - // Add last new-line - $proxyTunnel .= getConfig('HTTP_EOL'); - //* DEBUG: */ print('proxyTunnel=
' . $proxyTunnel.'
'); + // Setup proxy tunnel + $response = setupProxyTunnel($host, $fp); - // 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 +1942,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 +1968,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 +1978,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 +1986,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.0' . 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 @@ -2061,7 +2111,7 @@ function generateEmailLink ($email, $table = 'admins') { if ((isExtensionActive('admins')) && ($table == 'admins')) { // Create email link for contacting admin in guest area $EMAIL = generateAdminEmailLink($email); - } elseif ((isExtensionActive('user')) && (getExtensionVersion('user') >= '0.3.3') && ($table == 'user_data')) { + } elseif ((isExtensionInstalledAndNewer('user', '0.3.3')) && ($table == 'user_data')) { // Create email link for contacting a member within admin area (or later in other areas, too?) $EMAIL = generateUserEmailLink($email, 'admin'); } elseif ((isExtensionActive('sponsor')) && ($table == 'sponsor_data')) { @@ -2676,11 +2726,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 +2739,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 +2946,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 +2961,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 @@ -3034,7 +3085,7 @@ function determineReferalId () { } elseif ((isExtensionInstalledAndNewer('user', '0.3.4')) && (getConfig('select_user_zero_refid') == 'Y')) { // Select a random user which has confirmed enougth mails $GLOBALS['refid'] = determineRandomReferalId(); - } elseif ((isExtensionInstalled('sql_patches')) && (getConfig('def_refid') > 0)) { + } elseif ((isExtensionInstalledAndNewer('sql_patches', '0.1.2')) && (getConfig('def_refid') > 0)) { // Set default refid as refid in URL $GLOBALS['refid'] = getConfig('def_refid'); } else { @@ -3886,6 +3937,43 @@ 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; +} + +// Handles the braces [] of a field (e.g. value of 'name' attribute) +function handleFieldWithBraces ($field) { + // Are there braces [] at the end? + if (substr($field, -2, 2) == '[]') { + // Try to find one and replace it. I do it this way to allow easy + // extending of this code. + foreach (array('admin_list_builder_id_value') as $key) { + // Is the cache entry set? + if (isset($GLOBALS[$key])) { + // Insert it + $field = str_replace('[]', '[' . $GLOBALS[$key] . ']', $field); + + // And abort + break; + } // END - if + } // END - foreach + } // END - if + + // Return it + return $field; +} + ////////////////////////////////////////////////// // AUTOMATICALLY RE-GENERATED MISSING FUNCTIONS // //////////////////////////////////////////////////