Skip to content

Commit c516f49

Browse files
committed
Added logging for objects with masking, deep copying
TO DO: Remove debug statements, Organize code
1 parent d1ffca1 commit c516f49

1 file changed

Lines changed: 73 additions & 27 deletions

File tree

lib/net/authorize/util/Log.php

Lines changed: 73 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private function maskSensitiveXmlString($rawString){
3333
foreach ($this->sensitiveXmlTags as $i => $sensitiveTag){
3434
$tag = $sensitiveTag->tagName;
3535
$inputPattern = $sensitiveTag->pattern;
36-
$inputReplacement = "XXXX";
36+
$inputReplacement = "xxxx";
3737

3838
if(!trim($inputPattern)) {
3939
$inputPattern = "(.+)"; //no need to mask null data
@@ -51,45 +51,89 @@ private function maskSensitiveXmlString($rawString){
5151
$maskedString = preg_replace($patterns, $replacements, $rawString);
5252
return $maskedString;
5353
}
54+
55+
private function getPropertiesInclBase($reflClass)
56+
{
57+
$properties = array();
58+
try {
59+
do {
60+
$curClassPropList = $reflClass->getProperties();
61+
foreach ($curClassPropList as $p) {
62+
$p->setAccessible(true);
63+
}
64+
$properties = array_merge($curClassPropList, $properties);
65+
} while ($reflClass = $reflClass->getParentClass());
66+
} catch (\ReflectionException $e) { }
67+
return $properties;
68+
}
69+
70+
// recieves a ReflectionProperty and an object, and returns a masked object if its a sensitive field, else false
71+
private function checkAndMask($prop,$obj){
72+
foreach($this->sensitiveXmlTags as $i => $sensitiveTag)
73+
{
74+
echo "strcmp ". $prop->getName().'---'.$sensitiveTag->tagName . PHP_EOL;
75+
$inputPattern = "(.+)";
76+
$inputReplacement = "xxxx";
77+
78+
if(trim($sensitiveTag->pattern)) {
79+
$inputPattern = $sensitiveTag->pattern;
80+
}
81+
$inputPattern='/'.$inputPattern.'/';
82+
83+
if(trim($sensitiveTag->replacement)) {
84+
$inputReplacement = $sensitiveTag->replacement;
85+
}
86+
87+
if(strcmp($prop->getName(),$sensitiveTag->tagName)==0)
88+
{
89+
echo '|||'.preg_replace($inputPattern,$inputReplacement,$prop->getValue($obj));
90+
$prop->setValue($obj,preg_replace($inputPattern,$inputReplacement,$prop->getValue($obj)));
91+
return $prop->getValue($obj);
92+
}
93+
}
94+
return false;
95+
// ($prop->getValue($obj))
96+
}
97+
5498
private function maskSensitiveProperties ($obj)
5599
{
100+
// retrieve all properties of the passed object
101+
echo "here:";
56102
$reflectObj = new \ReflectionObject($obj);
57-
// //echo "reflection object name: " . $reflectObj->getName();
58-
// if($reflectObj->getName()=="name"){
59-
// return;
60-
// }
61-
$props = $reflectObj->getParentClass()->getProperties();
62-
print_r($props);
63-
foreach($props as $i => $prop){
64-
$prop->setAccessible(true);
65-
//
66-
// echo "propValue: " ;
67-
print_r($prop->getValue($obj));
68-
// print_r($propValue);
103+
$props = $this->getPropertiesInclBase($reflectObj);
69104

70-
if(is_object($prop->getValue($obj))){
71-
echo "Is object\n";
72-
$prop->setValue($obj, null);
73-
// $prop->setValue($obj, $this->maskSensitiveProperties($prop->getValue($obj)));
105+
foreach($props as $i => $prop){
106+
print_r($prop->getValue($obj));
107+
if(is_object($prop->getValue($obj))){
108+
echo "Is object".$prop->getName()."\n";
109+
//$prop->setValue($obj, null);
110+
$prop->setValue($obj, $this->maskSensitiveProperties($prop->getValue($obj)));
111+
}
112+
else if(is_array($prop->getValue($obj))){
113+
echo "Is array".$prop->getName()."\n";
114+
//$prop->setValue($obj, null);
115+
$newVals=array();
116+
foreach($prop->getValue($obj) as $i=>$arrEle)
117+
{
118+
$newVals[]=$this->maskSensitiveProperties($arrEle);
119+
}
120+
$prop->setValue($obj, $newVals);
74121
}
75122
else{
76123
echo "leaf value: " ; $prop->getValue($obj);
124+
$res=$this->checkAndMask($prop,$obj);
125+
if($res)
126+
$prop->setValue($obj, $res);
77127
}
78-
79-
// var_dump($obj);
80128
}
81-
//NULL even obj disp>layed print_r(get_object_vars($obj));
82-
// print_r($obj);
83-
//NULL even obj displayed print_r(get_class_vars(get_class($obj)));
129+
84130
echo "************************";
85131
print_r($obj);
86132
print_r('size...'.count($props));
133+
87134
return $obj;
88-
// echo"var_dump-------";
89-
// var_dump($reflectObj);
90-
// echo"var_dump-------";
91-
// return $reflectObj;
92135
}
136+
93137
private function getMasked($raw)
94138
{ //always returns string
95139
$messageType = gettype($raw);
@@ -98,7 +142,9 @@ private function getMasked($raw)
98142
$message = $this->maskSensitiveXmlString($raw);
99143
}
100144
else if($messageType == "object"){
101-
$message = print_r($this->maskSensitiveProperties($raw), true); //object to string
145+
$obj = unserialize(serialize($raw));
146+
// deep copying objects http://stackoverflow.com/questions/185934/how-do-i-create-a-copy-of-an-object-in-php
147+
$message = print_r($this->maskSensitiveProperties($obj), true); //object to string
102148
}
103149
return $message;
104150
}

0 commit comments

Comments
 (0)