1616define ("ANET_LOG_INFO " ,2 );
1717define ("ANET_LOG_WARN " ,3 );
1818define ("ANET_LOG_ERROR " ,4 );
19-
2019//set level
2120define ("ANET_LOG_LEVEL " ,ANET_LOG_DEBUG );
2221
23- /**
24- * A class to implement logging.
25- *
26- * @package AuthorizeNet
27- * @subpackage net\authorize\util
28- */
29-
3022class Log
3123{
3224 private $ sensitiveXmlTags = NULL ;
33-
34- /**
35- * Takes an xml as string and masks the sensitive fields.
36- *
37- * @param string $rawString The xml as a string.
38- *
39- * @return string The xml as a string after masking sensitive fields
40- */
25+
4126 private function maskSensitiveXmlString ($ rawString ){
27+ //Tag name is compulsory, can leave patterns and repalcements blank
28+ // $tags= array("cardCode","cardNumber","expirationDate");
29+ // $patterns=array("","([^0-9]*)(\d+)(\d{4})(.*)","");
4230 $ patterns =array ();
31+ // $replacements=array("","$1xxxx-$3$4","");
4332 $ replacements =array ();
44-
4533 foreach ($ this ->sensitiveXmlTags as $ i => $ sensitiveTag ){
4634 $ tag = $ sensitiveTag ->tagName ;
47- $ inputPattern = " (.+) " ; //no need to mask null data
48- $ inputReplacement = "xxxx " ;
35+ $ inputPattern = $ sensitiveTag -> pattern ;
36+ $ inputReplacement = "XXXX " ;
4937
50- if (trim ($ sensitiveTag -> pattern )) {
51- $ inputPattern = $ sensitiveTag -> pattern ;
38+ if (! trim ($ inputPattern )) {
39+ $ inputPattern = " (.+) " ; //no need to mask null data
5240 }
5341 $ pattern = "/< " . $ tag . "> " . $ inputPattern ."<\/ " . $ tag . ">/ " ;
5442
@@ -64,138 +52,32 @@ private function maskSensitiveXmlString($rawString){
6452 return $ maskedString ;
6553 }
6654
67- /**
68- * Object data masking related functions START
69- */
70-
71- /**
72- * private function getPropertiesInclBase($reflClass).
73- *
74- * Receives a ReflectionObject, ...
75- * iteratively fetches the properties of the object (including from the base classes up the hierarchy), ...
76- * collects them in an array of ReflectionProperty and returns the array.
77- *
78- * @param ReflectionObject $reflClass
79- *
80- * @return \ReflectionProperty[]
81- */
82- private function getPropertiesInclBase ($ reflClass )
83- {
84- $ properties = array ();
85- try {
86- do {
87- $ curClassPropList = $ reflClass ->getProperties ();
88- foreach ($ curClassPropList as $ p ) {
89- $ p ->setAccessible (true );
90- }
91- $ properties = array_merge ($ curClassPropList , $ properties );
92- } while ($ reflClass = $ reflClass ->getParentClass ());
93- } catch (\ReflectionException $ e ) { }
94- return $ properties ;
95- }
96-
97- /**
98- * private function checkAndMask($prop, $obj).
99- *
100- * Receives a ReflectionProperty and an object, and returns a masked object if the ReflectionProperty corresponds to a sensitive field, else returns false.
101- *
102- * @param ReflectionProperty $prop
103- * @param object $obj
104- *
105- * @return string|bool
106- */
107- private function checkAndMask ($ prop , $ obj ){
108- foreach ($ this ->sensitiveXmlTags as $ i => $ sensitiveTag )
109- {
110- $ inputPattern = "(.+) " ;
111- $ inputReplacement = "xxxx " ;
112-
113- if (trim ($ sensitiveTag ->pattern )) {
114- $ inputPattern = $ sensitiveTag ->pattern ;
115- }
116- $ inputPattern ='/ ' .$ inputPattern .'/ ' ;
117-
118- if (trim ($ sensitiveTag ->replacement )) {
119- $ inputReplacement = $ sensitiveTag ->replacement ;
120- }
121-
122- if (strcmp ($ prop ->getName (),$ sensitiveTag ->tagName )==0 )
123- {
124- $ prop ->setValue ($ obj ,preg_replace ($ inputPattern ,$ inputReplacement ,$ prop ->getValue ($ obj )));
125- return $ prop ->getValue ($ obj );
126- }
127- }
128- return false ;
129- }
130-
131- /**
132- * called by getMasked() to mask sensitive fields of an object.
133- *
134- * @param object $obj
135- *
136- * @return object
137- */
138- private function maskSensitiveProperties ($ obj )
139- {
140- // first retrieve all properties of the passed object
141- $ reflectObj = new \ReflectionObject ($ obj );
142- $ props = $ this ->getPropertiesInclBase ($ reflectObj );
143-
144- // for composite property recursively execute; for scalars, do a check and mask
145- foreach ($ props as $ i => $ prop ){
146- $ propValue =$ prop ->getValue ($ obj );
147-
148- // for object and arrays, recursively call for inner elements
149- if (is_object ($ propValue )){
150- $ prop ->setValue ($ obj , $ this ->maskSensitiveProperties ($ propValue ));
151- }
152- else if (is_array ($ propValue )){
153- $ newVals =array ();
154- foreach ($ propValue as $ i =>$ arrEle )
155- {
156- $ newVals []=$ this ->maskSensitiveProperties ($ arrEle );
157- }
158- $ prop ->setValue ($ obj , $ newVals );
159- }
160- // else check if the property represents a sensitive field. If so, mask.
161- else {
162- $ res =$ this ->checkAndMask ($ prop , $ obj );
163- if ($ res )
164- $ prop ->setValue ($ obj , $ res );
165- }
166- }
167-
168- return $ obj ;
169- }
170-
171- /**
172- * Object data masking related functions END
173- */
174-
175- /**
176- * private function getMasked($raw).
177- *
178- * called by log()
179- *
180- * @param mixed $raw
181- *
182- * @return string
183- */
18455 private function getMasked ($ raw )
185- { //always returns string
56+ {
18657 $ messageType = gettype ($ raw );
18758 $ message ="" ;
18859 if ($ messageType == "string " ) {
18960 $ message = $ this ->maskSensitiveXmlString ($ raw );
19061 }
191- else if ($ messageType == "object " ){
192- $ obj = unserialize (serialize ($ raw )); // deep copying the object
193- $ message = print_r ($ this ->maskSensitiveProperties ($ obj ), true ); //object to string
194- }
19562 return $ message ;
19663 }
197-
198- private function log ($ logLevelPrefix , $ logMessage , $ flags ){
64+ public function debug ($ logMessage , $ flags =FILE_APPEND )
65+ {
66+ if (ANET_LOG_DEBUG >= ANET_LOG_LEVEL ){
67+ $ this ->log (ANET_LOG_DEBUG_PREFIX , $ logMessage ,$ flags );
68+ }
69+ }
70+ public function info ($ logMessage , $ flags =FILE_APPEND ){
71+ if (ANET_LOG_INFO >= ANET_LOG_LEVEL ) {
72+ $ this ->log (ANET_LOG_INFO_PREFIX , $ logMessage ,$ flags );
73+ }
74+ }
75+ public function error ($ logMessage , $ flags =FILE_APPEND ){
76+ if (ANET_LOG_ERROR >= ANET_LOG_LEVEL ) {
77+ $ this ->log (ANET_LOG_ERROR_PREFIX , $ logMessage ,$ flags );
78+ }
79+ }
80+ private function log ($ logLevelPrefix , $ logMessage , $ flags ){
19981 //masking
20082 $ logMessage = $ this ->getMasked ($ logMessage );
20183
@@ -215,26 +97,6 @@ private function log($logLevelPrefix, $logMessage, $flags){
21597 $ methodName , $ fileName , $ lineNumber , $ logMessage );
21698 file_put_contents (ANET_LOG_FILE , $ logString , $ flags );
21799 }
218-
219- public function debug ($ logMessage , $ flags =FILE_APPEND )
220- {
221- if (ANET_LOG_DEBUG >= ANET_LOG_LEVEL ){
222- $ this ->log (ANET_LOG_DEBUG_PREFIX , $ logMessage ,$ flags );
223- }
224- }
225-
226- public function info ($ logMessage , $ flags =FILE_APPEND ){
227- if (ANET_LOG_INFO >= ANET_LOG_LEVEL ) {
228- $ this ->log (ANET_LOG_INFO_PREFIX , $ logMessage ,$ flags );
229- }
230- }
231-
232- public function error ($ logMessage , $ flags =FILE_APPEND ){
233- if (ANET_LOG_ERROR >= ANET_LOG_LEVEL ) {
234- $ this ->log (ANET_LOG_ERROR_PREFIX , $ logMessage ,$ flags );
235- }
236- }
237-
238100 public function __construct (){
239101 $ this ->sensitiveXmlTags = ANetSensitiveFields::getSensitiveXmlTags ();
240102 }
0 commit comments