Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/Stackify/Log/Entities/Api/WebRequestDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class WebRequestDetail

/**
* Constructor
*
* @return void
*/
private function __construct()
{
Expand All @@ -143,6 +145,7 @@ private function __construct()
$agentConfig->getCaptureErrorHeadersWhitelist()
)
: null;

$this->Cookies = isset($_COOKIE) && $agentConfig->getCaptureErrorCookies()
? self::getRequestMap(
$_COOKIE,
Expand All @@ -158,6 +161,7 @@ private function __construct()
$agentConfig->getCaptureGetVariablesWhitelist()
)
: null;

$this->PostData = isset($_POST) && $agentConfig->getCapturePostVariables()
? self::getRequestMap(
$_POST,
Expand All @@ -181,6 +185,8 @@ private function __construct()
$this->SessionData = isset($_SESSION) ? self::getRequestMap($_SESSION, array('*'), array('*')) : null;
$this->PostDataRaw = file_get_contents('php://input');
}

$this->checkParseRawPostDataToJson();
}

/**
Expand Down Expand Up @@ -359,4 +365,58 @@ protected function getHeaders($blacklist = null, $whitelist = null)
return self::getRequestMap($headers, $blacklist, $whitelist);
}

/**
* Check ParseRawPostDataToJson setting
*
* @return void
*/
protected function checkParseRawPostDataToJson()
{
$agentConfig = Agent::getInstance();

if (!$agentConfig->getParseRawPostDataToJson()) {
return;
}

// Ignore Setting if Post Data is not empty, already parsed the form
if (!empty($this->PostData)) {
return;
}

// FIXME: Check Request Content-Type?
$jsonDecodedPostData = json_decode($this->PostDataRaw, true);
if (empty($jsonDecodedPostData)) {
$message = json_last_error_msg();
// Avoid spamming error log in case of malformed/invalid raw post data
$agentConfig->logDebug("[". __CLASS__ ."][". __FUNCTION__ ."] Failed to parse JSON. Url: $this->RequestUrl - Method: $this->HttpMethod - Message: $message");
return;
}

$this->PostData = $jsonDecodedPostData && $agentConfig->getCapturePostVariables()
? self::getRequestMap(
$jsonDecodedPostData,
$agentConfig->getCapturePostVariablesBlacklist(),
$agentConfig->getCapturePostVariablesWhitelist()
)
: null;

if (empty($this->PostData)) {
return;
}

if (!$agentConfig->getCaptureRawPostData()) {
return;
}

// TODO: Re-encode the data?
// It will change the masked/blacklisted values to masked
$reencodedJsonRawPostData = json_encode($this->PostData);
if (empty($reencodedJsonRawPostData)) {
// Avoid spamming error log in case of malformed/invalid post data array, depth or flag setting
$agentConfig->logError("[". __CLASS__ ."][". __FUNCTION__ ."] Failed to encode Post Data. Url: $this->RequestUrl - Method: $this->HttpMethod - Message: $message");
return;
}

$this->PostDataRaw = $reencodedJsonRawPostData;
}
}
27 changes: 25 additions & 2 deletions src/Stackify/Log/Transport/Config/AbstractConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ public function __construct()
$ds = DIRECTORY_SEPARATOR;
$this->DebugLogPath = realpath(dirname(__FILE__) . "$ds..$ds..") . $ds . 'debug/log.log';
$this->Debug = false;

$this->ParseRawPostDataToJson = false;
}

/**
Expand Down Expand Up @@ -409,6 +411,27 @@ public function setDebug($enable = null)
$this->Debug = $this->getBoolean($enable, 'Debug');
}

/**
* Set capture $_POST variable option
*
* @param boolean $enable Enable
*
* @return void
*/
public function setParseRawPostDataToJson($enable = null)
{
$this->ParseRawPostDataToJson = $this->getBoolean($enable, 'ParseRawPostDataToJson');
}

/**
* Get Parse Raw POST data to JSON option
*
* @return boolean
*/
public function getParseRawPostDataToJson()
{
return $this->ParseRawPostDataToJson;
}

/**
* Get capture raw POST data option
Expand Down Expand Up @@ -607,7 +630,7 @@ public function getDebug()
*
* @return void
*/
protected function logError($message)
public function logError($message)
{
$this->log($message, func_get_args(), false);
}
Expand All @@ -619,7 +642,7 @@ protected function logError($message)
*
* @return void
*/
protected function logDebug($message)
public function logDebug($message)
{
if (!$this->getDebug()) {
return;
Expand Down