From 82ba828dcdb063ea3292fad79008469d6423d223 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Wed, 4 Nov 2009 07:32:13 +0000 Subject: [PATCH] Cache path changed and compileRawCode() introduced --- .gitattributes | 3 +- .gitignore | 2 +- inc/cache/{templates => _compiled}/.htaccess | 0 inc/cache/_compiled/templates/.htaccess | 1 + inc/config-functions.php | 2 +- inc/db/lib-mysql3.php | 2 +- inc/filters.php | 73 ++++++++++++-------- inc/functions.php | 54 +++++++++------ inc/libs/primera_functions.php | 4 +- inc/libs/wernis_functions.php | 4 +- modules.php | 2 +- 11 files changed, 88 insertions(+), 59 deletions(-) rename inc/cache/{templates => _compiled}/.htaccess (100%) create mode 100644 inc/cache/_compiled/templates/.htaccess diff --git a/.gitattributes b/.gitattributes index 8891988c5f..ca82c559f5 100644 --- a/.gitattributes +++ b/.gitattributes @@ -95,7 +95,8 @@ inc/autopurge/purge-mails.php -text inc/autopurge/purge-tsks.php -text inc/autopurge/purge-unconfirmed.php -text inc/cache/.htaccess -text -inc/cache/templates/.htaccess -text +inc/cache/_compiled/.htaccess -text +inc/cache/_compiled/templates/.htaccess -text inc/classes/.htaccess -text inc/classes/cachesystem.class.php -text inc/classes/rdf.class.php -text diff --git a/.gitignore b/.gitignore index 36fda3f619..2307ae3c66 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ inc/.secret/.* inc/.secret/dummy.php inc/cache/*.cache inc/cache/.revision +inc/cache/_compiled/templates/*.cache inc/cache/config-local.php inc/cache/debug.log inc/cache/sess_* -inc/cache/templates/*.cache diff --git a/inc/cache/templates/.htaccess b/inc/cache/_compiled/.htaccess similarity index 100% rename from inc/cache/templates/.htaccess rename to inc/cache/_compiled/.htaccess diff --git a/inc/cache/_compiled/templates/.htaccess b/inc/cache/_compiled/templates/.htaccess new file mode 100644 index 0000000000..14249c50bd --- /dev/null +++ b/inc/cache/_compiled/templates/.htaccess @@ -0,0 +1 @@ +Deny from all \ No newline at end of file diff --git a/inc/config-functions.php b/inc/config-functions.php index 3b9b1759ff..cda835e775 100644 --- a/inc/config-functions.php +++ b/inc/config-functions.php @@ -109,7 +109,7 @@ function mergeConfig ($newConfig) { // Increment or init with given value or 1 as default the given config entry function incrementConfigEntry ($configEntry, $value=1) { // Increment it if set or init it with 1 - if (getConfig($configEntry) > 0) { + if (isConfigEntrySet($configEntry)) { $GLOBALS['config'][$configEntry] += $value; } else { $GLOBALS['config'][$configEntry] = $value; diff --git a/inc/db/lib-mysql3.php b/inc/db/lib-mysql3.php index 625125b39d..b4e386ce8e 100644 --- a/inc/db/lib-mysql3.php +++ b/inc/db/lib-mysql3.php @@ -72,7 +72,7 @@ function SQL_QUERY ($sqlString, $F, $L) { $sqlString = str_replace("\t", ' ', str_replace("\n", ' ', str_replace("\r", ' ', $sqlString))); // Compile config out - $sqlString = FILTER_COMPILE_CONFIG($sqlString); + $sqlString = FILTER_COMPILE_CONFIG($sqlString, true); // Starting time $querytimeBefore = array_sum(explode(' ', microtime())); diff --git a/inc/filters.php b/inc/filters.php index fab11f202f..431b5ac367 100644 --- a/inc/filters.php +++ b/inc/filters.php @@ -398,40 +398,55 @@ function FILTER_DETERMINE_USERNAME () { } // Filter for compiling config entries -function FILTER_COMPILE_CONFIG ($code) { - // Compile {?some_var?} to getConfig('some_var') - preg_match_all('/\{\?(([a-zA-Z0-9-_]+)*)\?\}/', $code, $matches); +function FILTER_COMPILE_CONFIG ($code, $compiled = false) { + // Save the uncompiled code + $uncompiled = $code; + + // Do we have cache? + if (!isset($GLOBALS['compiled_config'][$code])) { + // Compile {?some_var?} to getConfig('some_var') + preg_match_all('/\{\?(([a-zA-Z0-9-_]+)*)\?\}/', $code, $matches); + + // Some entries found? + if ((count($matches) > 0) && (count($matches[0]) > 0)) { + // Replace all matches + foreach ($matches[0] as $key => $match) { + // Do we have cache? + if (!isset($GLOBALS['compile_config'][$matches[1][$key]])) { + // Is the config valid? + if (isConfigEntrySet($matches[1][$key])) { + // Set it for caching + $GLOBALS['compile_config'][$matches[1][$key]] = "\".getConfig('" . $matches[1][$key] . "').\""; + } elseif (isConfigEntrySet('default_' . strtoupper($matches[1][$key]))) { + // Use default value + $GLOBALS['compile_config'][$matches[1][$key]] = "\".getConfig('" . 'DEFAULT_' . strtoupper($matches[1][$key]) . "').\""; + } elseif (isMessageIdValid('DEFAULT_' . strtoupper($matches[1][$key]))) { + // No config, try the language system + $GLOBALS['compile_config'][$matches[1][$key]] = "\".getMessage('". 'DEFAULT_' . strtoupper($matches[1][$key]) . "').\""; + } else { + // Unhandled! + $GLOBALS['compile_config'][$matches[1][$key]] = '!' . $matches[1][$key] . '!'; + } + } // END - if - // Some entries found? - if ((count($matches) > 0) && (count($matches[0]) > 0)) { - // Replace all matches - foreach ($matches[0] as $key => $match) { - // Do we have cache? - if (!isset($GLOBALS['compile_config'][$matches[1][$key]])) { - // Is the config valid? - if (isConfigEntrySet($matches[1][$key])) { - // Set it for caching - $GLOBALS['compile_config'][$matches[1][$key]] = getConfig($matches[1][$key]); - } elseif (isConfigEntrySet('default_' . strtoupper($matches[1][$key]))) { - // Use default value - $GLOBALS['compile_config'][$matches[1][$key]] = getConfig('DEFAULT_' . strtoupper($matches[1][$key])); - } elseif (isMessageIdValid('DEFAULT_' . strtoupper($matches[1][$key]))) { - // No config, try the language system - $GLOBALS['compile_config'][$matches[1][$key]] = getMessage('DEFAULT_' . strtoupper($matches[1][$key])); - } else { - // Unhandled! - $GLOBALS['compile_config'][$matches[1][$key]] = '!' . $matches[1][$key] . '!'; - } - } // END - if + // Use this for replacing + $code = str_replace($match, $GLOBALS['compile_config'][$matches[1][$key]], $code); + //* DEBUG: */ if (($match == '{?URL?}') && (strlen($code) > 10000)) die('
'.htmlentities($code).'
'); + } // END - foreach + } // END - if - // Use this for replacing - $code = str_replace($match, $GLOBALS['compile_config'][$matches[1][$key]], $code); - //* DEBUG: */ if (($match == '{?URL?}') && (strlen($code) > 10000)) die('
'.htmlentities($code).'
'); - } // END - foreach + // Add it to cache + $GLOBALS['compiled_config'][$uncompiled] = $code; + } // END - if + + // Should we compile it? + if ($compiled === true) { + // Run the code + eval("\$GLOBALS['compiled_config'][\$uncompiled] = \"" . $GLOBALS['compiled_config'][$uncompiled] . "\";"); } // END - if // Return compiled code - return $code; + return $GLOBALS['compiled_config'][$uncompiled]; } // Filter for compiling extension data diff --git a/inc/functions.php b/inc/functions.php index c9d5300347..90e2bc5058 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -382,9 +382,6 @@ function loadTemplate ($template, $return=false, $content=array()) { function loadEmailTemplate ($template, $content = array(), $UID = 0) { global $DATA; - // Our configuration is kept non-global here - $_CONFIG = getConfigArray(); - // Make sure all template names are lowercase! $template = strtolower($template); @@ -526,7 +523,7 @@ function loadEmailTemplate ($template, $content = array(), $UID = 0) { unset($DATA); // Compile the code and eval it - $eval = '$newContent = "' . compileCode(smartAddSlashes($newContent)) . '";'; + $eval = '$newContent = "' . compileRawCode(smartAddSlashes($newContent)) . '";'; eval($eval); // Return content @@ -538,7 +535,7 @@ function sendEmail ($toEmail, $subject, $message, $isHtml = 'N', $mailHeader = ' //* DEBUG: */ outputHtml(__FUNCTION__."(".__LINE__."):TO={$toEmail},SUBJECT={$subject}
"); // Compile subject line (for POINTS constant etc.) - eval("\$subject = decodeEntities(\"".compileCode(smartAddSlashes($subject))."\");"); + eval("\$subject = decodeEntities(\"".compileRawCode(smartAddSlashes($subject))."\");"); // Set from header if ((!eregi('@', $toEmail)) && ($toEmail > 0)) { @@ -584,10 +581,10 @@ function sendEmail ($toEmail, $subject, $message, $isHtml = 'N', $mailHeader = ' } // Compile "TO" - eval("\$toEmail = \"".compileCode(smartAddSlashes($toEmail))."\";"); + eval("\$toEmail = \"".compileRawCode(smartAddSlashes($toEmail))."\";"); // Compile "MSG" - eval("\$message = \"".compileCode(smartAddSlashes($message))."\";"); + eval("\$message = \"".compileRawCode(smartAddSlashes($message))."\";"); // Fix HTML parameter (default is no!) if (empty($isHtml)) $isHtml = 'N'; @@ -921,7 +918,7 @@ function makeTime ($hours, $minutes, $seconds, $stamp) { // Redirects to an URL and if neccessarry extends it with own base URL function redirectToUrl ($URL) { // Compile out codes - eval('$URL = "' . compileCode($URL) . '";'); + eval('$URL = "' . compileRawCode($URL) . '";'); // Check if http(s):// is there if ((substr($URL, 0, 7) != 'http://') && (substr($URL, 0, 8) != 'https://')) { @@ -999,6 +996,27 @@ function compileCode ($code, $simple = false, $constants = true, $full = true) { // Start couting $startCompile = explode(' ', microtime()); + // Comile the code + $code = compileRawCode($code, $simple, $constants, $full); + + // Get timing + $compiled = explode(' ', microtime()); + + // Add timing + $code .= ''; + + // Return compiled code + return $code; +} + +// Compiles the code (use compileCode() only for HTML because of the comments) +function compileRawCode ($code, $simple = false, $constants = true, $full = true) { + // Is the code a string? + if (!is_string($code)) { + // Silently return it + return $code; + } // END - if + // Init replacement-array with full security characters $secChars = $GLOBALS['security_chars']; @@ -1074,13 +1092,7 @@ function compileCode ($code, $simple = false, $constants = true, $full = true) { } // END - foreach } // END - if - // Get timing - $compiled = explode(' ', microtime()); - - // Add timing - $code .= ''; - - // Return compiled code + // Return it return $code; } @@ -1806,7 +1818,7 @@ function sendRawRequest ($host, $request) { //* DEBUG: */ die("SCRIPT=" . $script.'
'); if ($useProxy === true) { // Connect to host through proxy connection - $fp = @fsockopen(compileCode(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); @@ -1827,7 +1839,7 @@ function sendRawRequest ($host, $request) { // Use login data to proxy? (username at least!) if (getConfig('proxy_username') != '') { // Add it as well - $encodedAuth = base64_encode(compileCode(getConfig('proxy_username')) . getConfig('ENCRYPT_SEPERATOR') . compileCode(getConfig('proxy_password'))); + $encodedAuth = base64_encode(compileRawCode(getConfig('proxy_username')) . getConfig('ENCRYPT_SEPERATOR') . compileRawCode(getConfig('proxy_password'))); $proxyTunnel .= "Proxy-Authorization: Basic " . $encodedAuth . getConfig('HTTP_EOL'); } // END - if @@ -2780,7 +2792,7 @@ function compileUriCode ($code, $simple = true) { // Function taken from user comments on www.php.net / function eregi() function isUrlValidSimple ($url) { // Prepare URL - $url = secureString(str_replace("\\", '', compileCode(urldecode($url)))); + $url = secureString(str_replace("\\", '', compileRawCode(urldecode($url)))); // Allows http and https $http = "(http|https)+(:\/\/)"; @@ -3647,7 +3659,7 @@ function isTemplateCached ($template) { // Do we have cached this result? if (!isset($GLOBALS['template_cache'][$template])) { // Generate FQFN - $FQFN = sprintf("%stemplates/%s.tpl.cache", getConfig('CACHE_PATH'), $template); + $FQFN = sprintf("%s_compiled/templates/%s.tpl.cache", getConfig('CACHE_PATH'), $template); // Is it there? $GLOBALS['template_cache'][$template] = isFileReadable($FQFN); @@ -3662,7 +3674,7 @@ function flushTemplateCache ($template, $eval) { // Is this cache flushed? if (!isTemplateCached($template)) { // Generate FQFN - $FQFN = sprintf("%stemplates/%s.tpl.cache", getConfig('CACHE_PATH'), $template); + $FQFN = sprintf("%s_compiled/templates/%s.tpl.cache", getConfig('CACHE_PATH'), $template); // Replace username with a call $eval = str_replace('$username', '".getUsername()."', $eval); @@ -3677,7 +3689,7 @@ function readTemplateCache ($template) { // Check it again if (isTemplateCached($template)) { // Generate FQFN - $FQFN = sprintf("%stemplates/%s.tpl.cache", getConfig('CACHE_PATH'), $template); + $FQFN = sprintf("%s_compiled/templates/%s.tpl.cache", getConfig('CACHE_PATH'), $template); // And read from it $GLOBALS['template_eval'][$template] = readFromFile($FQFN); diff --git a/inc/libs/primera_functions.php b/inc/libs/primera_functions.php index b8cce1b194..54c5929a05 100644 --- a/inc/libs/primera_functions.php +++ b/inc/libs/primera_functions.php @@ -257,7 +257,7 @@ function executePrimeraWithdraw ($primusNick, $userMd5, $amount) { $api = new PrimeraApi($primusNick, $userMd5); // Prepare purpose - eval("\$purpose = \"".compileCode(sprintf(getMessage('PRIMERA_API_PURPOSE_WITHDRAW'), getSession('sponsorid')))."\";"); + eval("\$purpose = \"".compileRawCode(sprintf(getMessage('PRIMERA_API_PURPOSE_WITHDRAW'), getSession('sponsorid')))."\";"); // Pay the Primera return $api->payPrimera($primusNick, $amount, $purpose); @@ -269,7 +269,7 @@ function executePrimeraPayout ($primusNick, $userMd5, $amount) { $api = new PrimeraApi(getConfig('primera_api_name'), getConfig('primera_api_md5')); // Prepare purpose - eval("\$purpose = \"".compileCode(sprintf(getMessage('PRIMERA_API_PURPOSE_PAYOUT'), getUserId()))."\";"); + eval("\$purpose = \"".compileRawCode(sprintf(getMessage('PRIMERA_API_PURPOSE_PAYOUT'), getUserId()))."\";"); // Pay the Primera return $api->payPrimera($primusNick, $amount, $purpose); diff --git a/inc/libs/wernis_functions.php b/inc/libs/wernis_functions.php index b1ea78fb9f..be5acd5de2 100644 --- a/inc/libs/wernis_functions.php +++ b/inc/libs/wernis_functions.php @@ -238,7 +238,7 @@ function WERNIS_EXECUTE_WITHDRAW ($wdsId, $userMd5, $amount) { $result = false; // Prepare the purpose - eval("\$purpose = \"".compileCode(sprintf(getMessage('WERNIS_API_PURPOSE_WITHDRAW'), getUserId()))."\";"); + eval("\$purpose = \"".compileRawCode(sprintf(getMessage('WERNIS_API_PURPOSE_WITHDRAW'), getUserId()))."\";"); // Prepare the request data $requestData = array( @@ -278,7 +278,7 @@ function WERNIS_EXECUTE_PAYOUT ($wdsId, $amount) { $result = false; // Prepare the purpose - eval("\$purpose = \"".compileCode(sprintf(getMessage('WERNIS_API_PURPOSE_PAYOUT'), getUserId()))."\";"); + eval("\$purpose = \"".compileRawCode(sprintf(getMessage('WERNIS_API_PURPOSE_PAYOUT'), getUserId()))."\";"); // Prepare the request data $requestData = array( diff --git a/modules.php b/modules.php index de90da0cd2..4034faef7d 100644 --- a/modules.php +++ b/modules.php @@ -37,7 +37,7 @@ ************************************************************************/ // XDEBUG call -/* DEBUG: */ xdebug_start_trace(); +//* DEBUG: */ xdebug_start_trace(); // Load security stuff here require('inc/libs/security_functions.php'); -- 2.30.2