Skip to content

Commit 4a4a3c4

Browse files
committed
Removed testing messages, added documentation to Log.php.
1 parent c516f49 commit 4a4a3c4

1 file changed

Lines changed: 100 additions & 49 deletions

File tree

lib/net/authorize/util/Log.php

Lines changed: 100 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,39 @@
1616
define("ANET_LOG_INFO",2);
1717
define("ANET_LOG_WARN",3);
1818
define("ANET_LOG_ERROR",4);
19+
1920
//set level
2021
define("ANET_LOG_LEVEL",ANET_LOG_DEBUG);
2122

23+
/**
24+
* A class to implement logging.
25+
*
26+
* @package AuthorizeNet
27+
* @subpackage net\authorize\util
28+
*/
29+
2230
class Log
2331
{
2432
private $sensitiveXmlTags = NULL;
25-
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+
*/
2641
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})(.*)","");
3042
$patterns=array();
31-
// $replacements=array("","$1xxxx-$3$4","");
3243
$replacements=array();
44+
3345
foreach ($this->sensitiveXmlTags as $i => $sensitiveTag){
3446
$tag = $sensitiveTag->tagName;
35-
$inputPattern = $sensitiveTag->pattern;
47+
$inputPattern = "(.+)"; //no need to mask null data
3648
$inputReplacement = "xxxx";
3749

38-
if(!trim($inputPattern)) {
39-
$inputPattern = "(.+)"; //no need to mask null data
50+
if(trim($sensitiveTag->pattern)) {
51+
$inputPattern = $sensitiveTag->pattern;
4052
}
4153
$pattern = "/<" . $tag . ">". $inputPattern ."<\/" . $tag . ">/";
4254

@@ -51,7 +63,22 @@ private function maskSensitiveXmlString($rawString){
5163
$maskedString = preg_replace($patterns, $replacements, $rawString);
5264
return $maskedString;
5365
}
66+
67+
/**
68+
* Object data masking related functions START
69+
*/
5470

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+
*/
5582
private function getPropertiesInclBase($reflClass)
5683
{
5784
$properties = array();
@@ -67,11 +94,19 @@ private function getPropertiesInclBase($reflClass)
6794
return $properties;
6895
}
6996

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){
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){
72108
foreach($this->sensitiveXmlTags as $i => $sensitiveTag)
73109
{
74-
echo "strcmp ". $prop->getName().'---'.$sensitiveTag->tagName . PHP_EOL;
75110
$inputPattern = "(.+)";
76111
$inputReplacement = "xxxx";
77112

@@ -86,54 +121,66 @@ private function checkAndMask($prop,$obj){
86121

87122
if(strcmp($prop->getName(),$sensitiveTag->tagName)==0)
88123
{
89-
echo '|||'.preg_replace($inputPattern,$inputReplacement,$prop->getValue($obj));
90124
$prop->setValue($obj,preg_replace($inputPattern,$inputReplacement,$prop->getValue($obj)));
91125
return $prop->getValue($obj);
92126
}
93127
}
94128
return false;
95-
// ($prop->getValue($obj))
96129
}
97130

131+
/**
132+
* called by getMasked() to mask sensitive fields of an object.
133+
*
134+
* @param object $obj
135+
*
136+
* @return object
137+
*/
98138
private function maskSensitiveProperties ($obj)
99139
{
100-
// retrieve all properties of the passed object
101-
echo "here:";
140+
// first retrieve all properties of the passed object
102141
$reflectObj = new \ReflectionObject($obj);
103142
$props = $this->getPropertiesInclBase($reflectObj);
104143

144+
// for composite property recursively execute; for scalars, do a check and mask
105145
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)));
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));
111151
}
112-
else if(is_array($prop->getValue($obj))){
113-
echo "Is array".$prop->getName()."\n";
114-
//$prop->setValue($obj, null);
152+
else if(is_array($propValue)){
115153
$newVals=array();
116-
foreach($prop->getValue($obj) as $i=>$arrEle)
154+
foreach($propValue as $i=>$arrEle)
117155
{
118156
$newVals[]=$this->maskSensitiveProperties($arrEle);
119157
}
120158
$prop->setValue($obj, $newVals);
121159
}
160+
// else check if the property represents a sensitive field. If so, mask.
122161
else{
123-
echo "leaf value: " ; $prop->getValue($obj);
124-
$res=$this->checkAndMask($prop,$obj);
162+
$res=$this->checkAndMask($prop, $obj);
125163
if($res)
126164
$prop->setValue($obj, $res);
127165
}
128166
}
129-
130-
echo "************************";
131-
print_r($obj);
132-
print_r('size...'.count($props));
133167

134168
return $obj;
135169
}
136170

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+
*/
137184
private function getMasked($raw)
138185
{ //always returns string
139186
$messageType = gettype($raw);
@@ -142,29 +189,13 @@ private function getMasked($raw)
142189
$message = $this->maskSensitiveXmlString($raw);
143190
}
144191
else if($messageType == "object"){
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
192+
$obj = unserialize(serialize($raw)); // deep copying the object
147193
$message = print_r($this->maskSensitiveProperties($obj), true); //object to string
148194
}
149195
return $message;
150196
}
151-
public function debug($logMessage, $flags=FILE_APPEND)
152-
{
153-
if(ANET_LOG_DEBUG >= ANET_LOG_LEVEL){
154-
$this->log(ANET_LOG_DEBUG_PREFIX, $logMessage,$flags);
155-
}
156-
}
157-
public function info($logMessage, $flags=FILE_APPEND){
158-
if(ANET_LOG_INFO >= ANET_LOG_LEVEL) {
159-
$this->log(ANET_LOG_INFO_PREFIX, $logMessage,$flags);
160-
}
161-
}
162-
public function error($logMessage, $flags=FILE_APPEND){
163-
if(ANET_LOG_ERROR >= ANET_LOG_LEVEL) {
164-
$this->log(ANET_LOG_ERROR_PREFIX, $logMessage,$flags);
165-
}
166-
}
167-
private function log($logLevelPrefix, $logMessage, $flags){
197+
198+
private function log($logLevelPrefix, $logMessage, $flags){
168199
//masking
169200
$logMessage = $this->getMasked($logMessage);
170201

@@ -184,6 +215,26 @@ private function log($logLevelPrefix, $logMessage, $flags){
184215
$methodName, $fileName, $lineNumber, $logMessage);
185216
file_put_contents(ANET_LOG_FILE, $logString, $flags);
186217
}
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+
187238
public function __construct(){
188239
$this->sensitiveXmlTags = ANetSensitiveFields::getSensitiveXmlTags();
189240
}

0 commit comments

Comments
 (0)