diff --git a/e107_core/bbcodes/bb_code.php b/e107_core/bbcodes/bb_code.php index 124932f04..284026358 100644 --- a/e107_core/bbcodes/bb_code.php +++ b/e107_core/bbcodes/bb_code.php @@ -43,11 +43,7 @@ class bb_code extends e_bb_base if($pref['smiley_activate']) { - if (!is_object($tp->e_emote)) - { - $tp->e_emote = new e_emoteFilter; - } - $code_text = $tp->e_emote->filterEmotesRev($code_text); + $code_text = e107::getEmote()->filterEmotesRev($code_text); } $search = array(E_NL,'\','$', '<'); diff --git a/e107_core/shortcodes/batch/bbcode_shortcodes.php b/e107_core/shortcodes/batch/bbcode_shortcodes.php index 8bbb3b406..90777f98f 100644 --- a/e107_core/shortcodes/batch/bbcode_shortcodes.php +++ b/e107_core/shortcodes/batch/bbcode_shortcodes.php @@ -388,7 +388,7 @@ class bbcode_shortcodes extends e_shortcode function renderEmotes() { - $emotes = e107::getParser()->getEmotes(); + $emotes = e107::getEmote()->getList(); $pref = e107::getPref(); $text = ""; diff --git a/e107_handlers/core_functions.php b/e107_handlers/core_functions.php index d86dc3944..319a7ee02 100644 --- a/e107_handlers/core_functions.php +++ b/e107_handlers/core_functions.php @@ -402,6 +402,8 @@ if (!function_exists('asortbyindex')) asort ($sort_values); reset ($sort_values); + $sorted_arr = array(); + foreach($sort_values as $arr_key =>$arr_val) { $sorted_arr[] = $array[$arr_key]; @@ -418,21 +420,17 @@ if (!function_exists('r_emote')) */ function r_emote() { - global $sysprefs, $pref; - $tp = e107::getParser(); - - if (!is_object($tp->e_emote)) - { - // require_once(e_HANDLER.'emote_filter.php'); - $tp->e_emote = new e_emoteFilter; - } - + + $pack = e107::getPref('emotepack'); + + $list = e107::getEmote()->getList(); + $str = ''; - foreach($tp->e_emote->emotes as $key => $value) // filename => text code + foreach($list as $key => $value) // filename => text code { $key = str_replace("!", ".", $key); // Usually '.' was replaced by '!' when saving $key = preg_replace("#_(\w{3})$#", ".\\1", $key); // '_' followed by exactly 3 chars is file extension - $key = e_IMAGE_ABS."emotes/" . $pref['emotepack'] . "/" .$key; // Add in the file path + $key = e_IMAGE_ABS."emotes/" . $pack . "/" .$key; // Add in the file path $value2 = substr($value, 0, strpos($value, " ")); $value = ($value2 ? $value2 : $value); @@ -509,6 +507,7 @@ if (!function_exists('multiarray_sort')) $key = is_numeric($arr_key) ? "" : $arr_key; // retain assoc-array keys. $sorted_arr[$key] = $array[$arr_key]; }*/ + $sorted_arr = array(); foreach($sort_values as $arr_key=>$arr_val) { diff --git a/e107_handlers/e107_class.php b/e107_handlers/e107_class.php index 20dd1a6c3..6ab636fbd 100644 --- a/e107_handlers/e107_class.php +++ b/e107_handlers/e107_class.php @@ -192,6 +192,7 @@ class e107 'e_bbcode' => '{e_HANDLER}bbcode_handler.php', 'e_bb_base' => '{e_HANDLER}bbcode_handler.php', 'e_customfields' => '{e_HANDLER}e_customfields_class.php', + 'e_emote' => '{e_HANDLER}e_emote_class.php', 'e_file' => '{e_HANDLER}file_class.php', 'e_file_inspector_json_phar' => '{e_HANDLER}e_file_inspector_json_phar.php', 'e_form' => '{e_HANDLER}form_handler.php', @@ -209,6 +210,7 @@ class e107 'e_parser' => '{e_HANDLER}e_parse_class.php', 'e_parse_shortcode' => '{e_HANDLER}shortcode_handler.php', 'e_plugin' => '{e_HANDLER}plugin_class.php', + 'e_profanity' => '{e_HANDLER}e_profanity_class.php', 'e_ranks' => '{e_HANDLER}e_ranks_class.php', 'e_shortcode' => '{e_HANDLER}shortcode_handler.php', 'e_system_user' => '{e_HANDLER}user_model.php', @@ -3994,6 +3996,26 @@ class e107 return self::getSingleton('error_page'); } + /** + * Retrieve e_emote filter . + * + * @return e_emote + */ + public static function getEmote() + { + return self::getSingleton('e_emote'); + } + + /** + * Retrieve Profanity filter . + * + * @return e_profanity + */ + public static function getProfanity() + { + return self::getSingleton('e_profanity'); + } + /** * Parses an array into a valid, rawurlencoded query string. This differs from http_build_query() as we need to diff --git a/e107_handlers/e_emote_class.php b/e107_handlers/e_emote_class.php new file mode 100644 index 000000000..21e70216f --- /dev/null +++ b/e107_handlers/e_emote_class.php @@ -0,0 +1,133 @@ +clearPrefCache('emote'); + e107::getConfig('core')->set('emotepack', 'default')->save(false, true, false); + } + + $this->emotes = e107::getConfig('emote')->getPref(); + + if(empty($this->emotes)) + { + return; + } + + $base = defined('e_HTTP_STATIC') && is_string(e_HTTP_STATIC) ? e_HTTP_STATIC : SITEURLBASE; + + foreach($this->emotes as $key => $value) + { + + $value = trim($value); + + if($value) + { // Only 'activate' emote if there's a substitution string set + + + $key = preg_replace("#!(\w{3,}?)$#si", ".\\1", $key); + // Next two probably to sort out legacy issues - may not be required any more + // $key = preg_replace("#_(\w{3})$#", ".\\1", $key); + + $key = str_replace('!', '_', $key); + + $filename = e_IMAGE . 'emotes/' . $pref['emotepack'] . '/' . $key; + + + $fileloc = $base . e_IMAGE_ABS . 'emotes/' . $pref['emotepack'] . '/' . $key; + + $alt = str_replace(array('.png', '.gif', '.jpg'), '', $key); + + if(file_exists($filename)) + { + $tmp = explode(' ', $value); + foreach($tmp as $code) + { + $img = "\""'; + + $this->search[] = "\n" . $code; + $this->replace[] = "\n" . $img; + + $this->search[] = ' ' . $code; + $this->replace[] = ' ' . $img; + + $this->search[] = '>' . $code; // Fix for emote within html. + $this->replace[] = '>' . $img; + + $this->singleSearch[] = $code; + $this->singleReplace[] = $img; + + } + } + } + else + { + unset($this->emotes[$key]); + } + + + } + + // print_a($this->regSearch); + // print_a($this->regReplace); + + } + + /** + * Return a list of the available emoticons. + * @return array + */ + public function getList() + { + + return $this->emotes; + } + + /** + * @param $text + * @return string + */ + public function filterEmotes($text) + { + + if(empty($text)) + { + return ''; + } + + if(!empty($this->singleSearch) && (strlen($text) < 12) && in_array($text, $this->singleSearch)) // just one emoticon with no space, line-break or html tags around it. + { + return str_replace($this->singleSearch, $this->singleReplace, $text); + } + + return str_replace($this->search, $this->replace, $text); + + } + + + /** + * @param $text + * @return string|string[] + */ + public function filterEmotesRev($text) + { + + return str_replace($this->replace, $this->search, $text); + } + +} \ No newline at end of file diff --git a/e107_handlers/e_parse_class.php b/e107_handlers/e_parse_class.php index 3c061016f..df946c1ea 100644 --- a/e107_handlers/e_parse_class.php +++ b/e107_handlers/e_parse_class.php @@ -1592,12 +1592,7 @@ class e_parse extends e_parser // Convert emoticons to graphical icons, if enabled if ($opts['emotes']) { - if (!is_object($this->e_emote)) - { - // require_once(e_HANDLER.'emote_filter.php'); - $this->e_emote = new e_emoteFilter; - } - $sub_blk = $this->e_emote->filterEmotes($sub_blk); + $sub_blk = e107::getEmote()->filterEmotes($sub_blk); } @@ -2207,7 +2202,7 @@ class e_parse extends e_parser * @param string $text * @return mixed|string */ - public function ampEncode($text='') + private function ampEncode($text='') { // Fix any left-over '&' //first revert any previously converted. @@ -3014,7 +3009,7 @@ class e_parse extends e_parser public function getEmotes() { - return $this->e_emote->emotes; + return e107::getEmote()->getList(); } @@ -3236,7 +3231,7 @@ class e_parse extends e_parser } - public function doReplace($matches) + private function doReplace($matches) { if(defined($matches[1]) && (deftrue('ADMIN') || strpos($matches[1], 'ADMIN') === FALSE)) { @@ -5343,253 +5338,3 @@ class e_parser } - -class e_emotefilter -{ - private $search = array(); - private $replace = array(); - public $emotes; - private $singleSearch = array(); - private $singleReplace = array(); - - public function __construct() - { - $pref = e107::getPref(); - - if(empty($pref['emotepack'])) - { - $pref['emotepack'] = 'default'; - e107::getConfig('emote')->clearPrefCache('emote'); - e107::getConfig('core')->set('emotepack','default')->save(false,true,false); - } - - $this->emotes = e107::getConfig('emote')->getPref(); - - if(empty($this->emotes)) - { - return; - } - - $base = defined('e_HTTP_STATIC') && is_string(e_HTTP_STATIC) ? e_HTTP_STATIC : SITEURLBASE; - - foreach($this->emotes as $key => $value) - { - - $value = trim($value); - - if ($value) - { // Only 'activate' emote if there's a substitution string set - - - $key = preg_replace("#!(\w{3,}?)$#si", ".\\1", $key); - // Next two probably to sort out legacy issues - may not be required any more - // $key = preg_replace("#_(\w{3})$#", ".\\1", $key); - - $key = str_replace('!', '_', $key); - - $filename = e_IMAGE. 'emotes/' . $pref['emotepack'] . '/' . $key; - - - - $fileloc = $base.e_IMAGE_ABS. 'emotes/' . $pref['emotepack'] . '/' . $key; - - $alt = str_replace(array('.png','.gif', '.jpg'),'', $key); - - if(file_exists($filename)) - { - $tmp = explode(' ', $value); - foreach($tmp as $code) - { - $img = "\"".$alt.'; - - $this->search[] = "\n".$code; - $this->replace[] = "\n".$img; - - $this->search[] = ' ' .$code; - $this->replace[] = ' ' .$img; - - $this->search[] = '>' .$code; // Fix for emote within html. - $this->replace[] = '>' .$img; - - $this->singleSearch[] = $code; - $this->singleReplace[] = $img; - - } - - - /* - if(strstr($value, " ")) - { - $tmp = explode(" ", $value); - foreach($tmp as $code) - { - $this->search[] = " ".$code; - $this->search[] = "\n".$code; - - $this->replace[] = " \"".$alt."\" "; - $this->replace[] = "\n \"".$alt."\" "; - } - unset($tmp); - } - else - { - if($value) - { - $this->search[] = " ".$value; - $this->search[] = "\n".$value; - - $this->replace[] = " \"".$alt."\" "; - $this->replace[] = "\n \"".$alt."\" "; - } - }*/ - } - } - else - { - unset($this->emotes[$key]); - } - - - } - - // print_a($this->regSearch); - // print_a($this->regReplace); - - } - - - public function filterEmotes($text) - { - - if(empty($text)) - { - return ''; - } - - if(!empty($this->singleSearch) && (strlen($text) < 12) && in_array($text, $this->singleSearch)) // just one emoticon with no space, line-break or html tags around it. - { - return str_replace($this->singleSearch,$this->singleReplace,$text); - } - - return str_replace($this->search, $this->replace, $text); - - } - - - public function filterEmotesRev($text) - { - return str_replace($this->replace, $this->search, $text); - } -} - - -class e_profanityFilter -{ - protected $profanityList; - private $pref; - - public function __construct() - { - - $this->pref = e107::getPref(); - - if(empty($this->pref['profanity_words'])) - { - return null; - } - - $words = explode(',', $this->pref['profanity_words']); - $word_array = array(); - foreach($words as $word) - { - $word = trim($word); - if($word != '') - { - $word_array[] = $word; - if (strpos($word, '$') !== FALSE) - { - $word_array[] = str_replace('$', '\$', $word); // Special case - '$' may be 'in clear' or as entity - } - } - } - if(count($word_array)) - { - $this->profanityList = str_replace('#','\#',implode("\b|\b", $word_array)); // We can get entities in the string - confuse the regex delimiters - } - unset($words); - return TRUE; - } - - public function filterProfanities($text) - { - - if (empty($this->profanityList)) - { - return $text; - } - - if(!empty($this->pref['profanity_replace'])) - { - return preg_replace("#\b".$this->profanityList."\b#is", $this->pref['profanity_replace'], $text); - } - - return preg_replace_callback("#\b".$this->profanityList."\b#is", array($this, 'replaceProfanities'), $text); - } - - public function replaceProfanities($matches) - { - /*! - @function replaceProfanities callback - @abstract replaces vowels in profanity words with stars - @param text string - text string to be filtered - @result filtered text - */ - - return preg_replace('#a|e|i|o|u#i', '*', $matches[0]); - } -} - - -/** - * Backwards Compatibility Class textparse - */ -class textparse { - - public function editparse($text, $mode = 'off') - { - trigger_error(''.__METHOD__.' is deprecated. Use e107::getParser()->toForm($text) instead. ', E_USER_DEPRECATED); - - return e107::getParser()->toForm($text); - } - - public function tpa($text, $mode = '', $referrer = '', $highlight_search = false, $poster_id = '') - { - trigger_error(''.__METHOD__.' is deprecated. Use e107::getParser()->toHTML($text) instead. ', E_USER_DEPRECATED); - - return e107::getParser()->toHTML($text, true, $mode, $poster_id); - } - - public function tpj($text) - { - trigger_error(''.__METHOD__.' is deprecated. ', E_USER_DEPRECATED); - - return $text; - } - - public function formtpa($text, $mode = '') - { - trigger_error(''.__METHOD__.' is deprecated. Use e107::getParser()->toDB($text) instead. ', E_USER_DEPRECATED); - - unset($mode); // keep PHPStorm happy - - return e107::getParser()->toDB($text); - } - - public function formtparev($text) - { - trigger_error(''.__METHOD__.' is deprecated. Use e107::getParser()->toForm($text) instead. ', E_USER_DEPRECATED); - - return e107::getParser()->toForm($text); - } - -} \ No newline at end of file diff --git a/e107_handlers/e_profanity_class.php b/e107_handlers/e_profanity_class.php new file mode 100644 index 000000000..62ab5ed78 --- /dev/null +++ b/e107_handlers/e_profanity_class.php @@ -0,0 +1,80 @@ +pref = e107::getPref(); + + if(empty($this->pref['profanity_words'])) + { + return null; + } + + $words = explode(',', $this->pref['profanity_words']); + $word_array = array(); + foreach($words as $word) + { + $word = trim($word); + if($word != '') + { + $word_array[] = $word; + if(strpos($word, '$') !== false) + { + $word_array[] = str_replace('$', '\$', $word); // Special case - '$' may be 'in clear' or as entity + } + } + } + if(count($word_array)) + { + $this->profanityList = str_replace('#', '\#', implode("\b|\b", $word_array)); // We can get entities in the string - confuse the regex delimiters + } + unset($words); + + return true; + } + + /** + * @param $text + * @return string|string[]|null + */ + public function filterProfanities($text) + { + + if(empty($this->profanityList)) + { + return $text; + } + + if(!empty($this->pref['profanity_replace'])) + { + return preg_replace("#\b" . $this->profanityList . "\b#is", $this->pref['profanity_replace'], $text); + } + + return preg_replace_callback("#\b" . $this->profanityList . "\b#is", array($this, 'replaceProfanities'), $text); + } + + /** + * + * @param $matches + * @return string|string[]|null + */ + public function replaceProfanities($matches) + { + + /*! + @function replaceProfanities callback + @abstract replaces vowels in profanity words with stars + @param text string - text string to be filtered + @result filtered text + */ + + return preg_replace('#a|e|i|o|u#i', '*', $matches[0]); + } +} \ No newline at end of file diff --git a/e107_tests/tests/unit/e_parseTest.php b/e107_tests/tests/unit/e_parseTest.php index 94704f1af..9574618cc 100644 --- a/e107_tests/tests/unit/e_parseTest.php +++ b/e107_tests/tests/unit/e_parseTest.php @@ -499,6 +499,12 @@ while($row = $sql->fetch()) $result = $this->tp->post_toForm($text); $this->assertSame($expected, $result); + $array = array($text); + $arrayExp = array($expected); + $result = $this->tp->post_toForm($array); + $this->assertSame($arrayExp, $result); + + } /* public function testHtml_truncate()