Browse Source

Replaced Smileys /e preg_replace with a callback.

Signed-off-by: Jeremy D <[email protected]>
Jeremy D 11 years ago
parent
commit
4d2e31cd8f
1 changed files with 56 additions and 3 deletions
  1. 56 3
      Sources/Subs.php

+ 56 - 3
Sources/Subs.php

@@ -2393,7 +2393,9 @@ function parse_bbc($message, $smileys = true, $cache_id = '', $parse_tags = arra
 function parsesmileys(&$message)
 {
 	global $modSettings, $txt, $user_info, $context, $smcFunc;
-	static $smileyPregSearch = null, $smileyPregReplacements = array();
+//	static $smileyPregSearch = null, $smileyPregReplacements = array();
+	static $smileyPregSearch = null;
+	global $smileyPregReplacements;
 
 	// No smiley set at all?!
 	if ($user_info['smiley_set'] == 'none' || trim($message) == '')
@@ -2460,11 +2462,62 @@ function parsesmileys(&$message)
 			}
 		}
 
-		$smileyPregSearch = '~(?<=[>:\?\.\s' . $non_breaking_space . '[\]()*\\\;]|^)(' . implode('|', $searchParts) . ')(?=[^[:alpha:]0-9]|$)~e' . ($context['utf8'] ? 'u' : '');
+//		$smileyPregSearch = '~(?<=[>:\?\.\s' . $non_breaking_space . '[\]()*\\\;]|^)(' . implode('|', $searchParts) . ')(?=[^[:alpha:]0-9]|$)~e' . ($context['utf8'] ? 'u' : '');
+		$smileyPregSearch = '~(?<=[>:\?\.\s' . $non_breaking_space . '[\]()*\\\;]|^)(' . implode('|', $searchParts) . ')(?=[^[:alpha:]0-9]|$)~' . ($context['utf8'] ? 'u' : '');
 	}
 
 	// Replace away!
-	$message = preg_replace($smileyPregSearch, '$smileyPregReplacements[\'$1\']', $message);
+	/*
+	* TODO: When SMF supports only PHP 5.3+, we can change this to "uses" keyword and simpifly this.
+	*/
+	$callback = smielyPregReplaceCurry('smielyPregReplaceCallback', 2);
+	$message = preg_replace_callback($smileyPregSearch, $callback($smileyPregReplacements), $message);
+}
+
+/**
+ * Smiely Replacment Curry.
+ *
+ * This allows use to do delayed argument binding and bring in
+ * the replacement variables for smiley replacments.
+ *
+ * Original code from: http://php.net/manual/en/function.preg-replace-callback.php#88013
+ * This is needed until SMF only supports PHP 5.3+ and we change to "use"
+ *
+ * @param string $func
+ * @param string $arity
+ * @return function a lambda function bound to $func.
+ */
+function smielyPregReplaceCurry($func, $arity)
+{
+	return create_function('', "
+		\$args = func_get_args();
+		if(count(\$args) >= $arity)
+			return call_user_func_array('$func', \$args);
+		\$args = var_export(\$args, 1);
+		return create_function('','
+			\$a = func_get_args();
+			\$z = ' . \$args . ';
+			\$a = array_merge(\$z,\$a);
+			return call_user_func_array(\'$func\', \$a);
+		');
+	");
+}
+
+/**
+ * Smiely Replacment Callback.
+ *
+ * Our callback that does the actual smiley replacments.
+ *
+ * Original code from: http://php.net/manual/en/function.preg-replace-callback.php#88013
+ * This is needed until SMF only supports PHP 5.3+ and we change to "use"
+ *
+ * @param string $replacements
+ * @param string $matches
+ * @return string the replaced results.
+ */
+function smielyPregReplaceCallback($replacements, $matches)
+{
+    return $replacements[$matches[1]];
 }
 
 /**