Php replaces sensitive string classes (with source code)
- /** String filter class
- * Date: 2013-01-09
- * Author: fdipzone
- * Ver: v1.0
- * Edit: bbs.it-home.org
- * Func:
- * Replace public replace with invalid characters
- * Public check whether illegal characters are contained
- * Private protect_white_list protection white list
- * Private resume_white_list restore the white list
- * Convert private getval White List key to value
- */
- Class StrFilter {// class start
-
- Private $ _ white_list = array ();
- Private $ _ black_list = array ();
- Private $ _ replacement = '*';
- Private $ _ LTAG = '[[##';
- Private $ _ RTAG = '##]]';
-
- /**
- * @ Param Array $ white_list
- * @ Param Array $ black_list
- * @ Param String $ replacement
- */
- Public function _ construct ($ white_list = array (), $ black_list = array (), $ replacement = '*'){
- $ This-> _ white_list = $ white_list;
- $ This-> _ black_list = $ black_list;
- $ This-> _ replacement = $ replacement;
- }
-
- /** Replace invalid characters
- * @ Param String $ content refers to the String to be replaced.
- * @ Return String the String after replacement
- */
- Public function replace ($ content ){
-
- If (! Isset ($ content) | $ content = ''){
- Return '';
- }
-
- // Protect white list
- $ Content = $ this-> protect_white_list ($ content );
-
- // Replace black list
- If ($ this-> _ black_list ){
- Foreach ($ this-> _ black_list as $ val ){
- $ Content = str_replace ($ val, $ this-> _ replacement, $ content );
- }
- }
-
- // Resume white list
- $ Content = $ this-> resume_white_list ($ content );
-
- Return $ content;
- }
-
- /** Check whether there are invalid tokens
- * @ Param String $ content String
- * @ Return boolean
- */
- Public function check ($ content ){
-
- If (! Isset ($ content) | $ content = ''){
- Return true;
- }
-
- // Protect white list
- $ Content = $ this-> protect_white_list ($ content );
-
- // Check
- If ($ this-> _ black_list ){
- Foreach ($ this-> _ black_list as $ val ){
- If (strstr ($ content, $ val )! = ''){
- Return false;
- }
- }
- }
- Return true;
- }
-
- /** Protect the white list
- * @ Param String $ content String
- * @ Return String
- */
- Private function protect_white_list ($ content ){
- If ($ this-> _ white_list ){
- Foreach ($ this-> _ white_list as $ key => $ val ){
- $ Content = str_replace ($ val, $ this-> _ LTAG. $ key. $ this-> _ RTAG, $ content );
- }
- }
- Return $ content;
- }
-
- /** Restore the white list
- * @ Param String $ content
- * @ Return String
- */
- Private function resume_white_list ($ content ){
- If ($ this-> _ white_list ){
- $ Content = preg_replace_callback ("/\[\[##(.*?) ##\] \]. *? /Si ", array ($ this, 'getval'), $ content );
- }
- Return $ content;
- }
- /** Restore the white list key to value
- * @ Param Array $ matches the key of white_list
- * @ Return String white_list val
- */
- Private function getval ($ matches ){
- Return isset ($ this-> _ white_list [$ matches [1])? $ This-> _ white_list [$ matches [1]: ''; // key-> val
- }
-
- } // Class end
-
- ?>
2. demo. php
- Header ("content-type: text/html; charset = utf8 ");
-
- Require ("StrFilter. class. php ");
-
- $ White = array ('Diaosi', 'caocao ');
- $ Black = array ('login', 'operation ');
-
- $ Content = "Fuck me, Cao, you are diaosi, I am jealous of you ";
-
- $ Obj = new StrFilter ($ white, $ black );
- Echo $ obj-> replace ($ content );
- ?>
Appendix: php replaces the class source code of sensitive strings. |