Common program search keyword transcoding

Published: 2018-01-12

Some search-type websites are mistakenly blocked due to malicious searches by users. Such websites themselves contain no illegal information; it is only because the search keywords are displayed on the web page (as shown below)

, you can refer to the following method to transcode the output keywords


image.png


DEDECMS program 

This article performs search transcoding for the Dedecms program. Other programs use a similar principle: transcode the keywords in search results before output


1: Check the website encoding. It will be displayed below when you access the backend, with two versions: GBK and UTF8


image.png


2: Copy the following code to the end of the include/extend.func.php file (Copy the corresponding code according to your website encoding. When editing web pages, it is recommended to use notepad and editplus software)

 UTF-8 encoding

function CharCodeAt($str)
{
	$strr="";
	for($i=0;$i<mb_strlen($str,'utf-8');$i++){
		$char = mb_substr($str,$i,1, 'UTF-8');
		if (mb_check_encoding($char, 'UTF-8'))
		{
			$ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
			$strr.="&#".hexdec(bin2hex($ret));
		}
		else
		{
                return null;
		}
	}
	return $strr;
}

GBK encoding

function CharCodeAt($str)
{
	$strr="";
	for($i=0;$i<mb_strlen($str,'gb2312');$i++){
		$char = mb_substr($str,$i,1, 'gb2312');
		if (mb_check_encoding($char, 'gb2312'))
		{
			$ret = mb_convert_encoding($char, 'UTF-32BE', 'gb2312');
			$strr.="&#".hexdec(bin2hex($ret));
		}
		else
		{
                return null;
		}
	}
	return $strr;
}


3: Modify the search template file templets/default/search.htm  [The path may vary for other templates]


Find: {dede:global name='keyword' function='RemoveXSS(@me)'/}

Replace with: {dede:global name='keyword' function='CharCodeAt(RemoveXSS(@me))'/}


4: View the source code of the search page to check whether it takes effect


image.png



Discuz program, example for Discuz!X3.4  GBK version


1. Check the web page encoding

        1. Right-click the web page and view the source code

        blob.png

         2. Check $_config['output']['charset'] in config/config.php

        blob.png


2. According to the encoding, copy the CharCodeAt function above, modify the source/function/function_search.php file, and add it to the end of the file, note: before ?>

        blob.png

3. Modify the source/module/search/search_forum.php program 

Add the following two lines of code at line 129 

$keyword = CharCodeAt($keyword);

$modkeyword = CharCodeAt($modkeyword);

blob.png


4. View the source code of the search page to check whether it takes effect

     blob.png