55
66class ANetSensitiveFields
77{
8- private static function getDefaulSensitiveXmlTags (){
9- // return array( //format for each element: array(tag name, regex-pattern, regex-replacement)
10- // array("tagName" => "cardCode","pattern" => "","replacement" => "","disableMask"=>false),
11- // array("tagName" => "cardNumber","pattern" => "([^0-9]*)(\d+)(\d{4})(.*)","replacement" => "$1xxxx-$3$4","disableMask"=>false),
12- // array("tagName" => "expirationDate","pattern" => "","replacement" => "","disableMask"=>false)
13- // );
14- return array ( //format for each element: array(tag name, regex-pattern, regex-replacement)
15- new SensitiveTag ("cardCode " ),
16- new SensitiveTag ("cardNumber " ,"(\d+)(\d{4}) " ,"$1xxxx-$3$4 " ,false ),
17- new SensitiveTag ("expirationDate " )
18- );
19- }
20- public static function getSensitiveXmlTags (){
21- $ sensitiveTags = array ();
8+ private static $ sensitiveTags = NULL ;
9+ private static $ sensitiveStringRegexes = NULL ;
10+
11+ private static function fetchFromConfigFiles (){
2212 $ configFilePath = dirname (__FILE__ ) . "/ " . ANET_SENSITIVE_XMLTAGS_JSON_FILE ;
2313 $ userConfigFile = ANET_SENSITIVE_XMLTAGS_JSON_FILE ;
2414 $ presentUserConfigFile = file_exists ($ userConfigFile );
2515 if ($ presentUserConfigFile ) { //client config for tags
2616 //read list of tags(and associate regex-patterns and replacements) from .json file
2717 $ jsonFileObejct = json_decode (file_get_contents ($ userConfigFile ));
28- $ sensitiveTags = $ jsonFileObejct ["sensitiveTags " ];
18+ self ::$ sensitiveTags = $ jsonFileObejct ->sensitiveTags ;
19+ self ::$ sensitiveStringRegexes = $ jsonFileObejct ->sensitiveStringRegexes ;
2920 if (json_last_error () === JSON_ERROR_NONE ) {// JSON is valid
3021 }
3122 else {
@@ -38,22 +29,25 @@ public static function getSensitiveXmlTags(){
3829 exit ("ERROR: No config file: " . $ configFilePath );
3930 }
4031 $ jsonFileObejct = json_decode (file_get_contents ($ configFilePath ));
41- $ sensitiveTags = $ jsonFileObejct ->sensitiveTags ;
32+ self ::$ sensitiveTags = $ jsonFileObejct ->sensitiveTags ;
33+ self ::$ sensitiveStringRegexes = $ jsonFileObejct ->sensitiveStringRegexes ;
4234 if (json_last_error () === JSON_ERROR_NONE ) {
4335 }
4436 else {
4537 exit ("ERROR: Invalid json in: " . $ configFilePath . " json_last_error_msg : " . json_last_error_msg ());
4638 }
4739 }
48- //Check for disableMask flag in case of client json.
49- $ applySensitiveTags = array ();
50- foreach ($ sensitiveTags as $ sensitiveTag ){
51- if ($ sensitiveTag ->disableMask ){
52- //skip masking continue;
53- }
54- else {
55- array_push ($ applySensitiveTags ,$ sensitiveTag );
56- } }
57- return $ applySensitiveTags ;
40+ }
41+ public static function getSensitiveStringRegexes (){
42+ if (NULL == self ::$ sensitiveStringRegexes ) {
43+ self ::fetchFromConfigFiles ();
44+ }
45+ return self ::$ sensitiveStringRegexes ;
46+ }
47+ public static function getSensitiveXmlTags (){
48+ if (NULL == self ::$ sensitiveTags ) {
49+ self ::fetchFromConfigFiles ();
50+ }
51+ return self ::$ sensitiveTags ;
5852 }
5953}
0 commit comments