diff --git a/src/Stackify/Log/Entities/Api/WebRequestDetail.php b/src/Stackify/Log/Entities/Api/WebRequestDetail.php index d47ae93..227438e 100644 --- a/src/Stackify/Log/Entities/Api/WebRequestDetail.php +++ b/src/Stackify/Log/Entities/Api/WebRequestDetail.php @@ -121,6 +121,8 @@ class WebRequestDetail /** * Constructor + * + * @return void */ private function __construct() { @@ -143,6 +145,7 @@ private function __construct() $agentConfig->getCaptureErrorHeadersWhitelist() ) : null; + $this->Cookies = isset($_COOKIE) && $agentConfig->getCaptureErrorCookies() ? self::getRequestMap( $_COOKIE, @@ -158,6 +161,7 @@ private function __construct() $agentConfig->getCaptureGetVariablesWhitelist() ) : null; + $this->PostData = isset($_POST) && $agentConfig->getCapturePostVariables() ? self::getRequestMap( $_POST, @@ -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(); } /** @@ -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; + } } \ No newline at end of file diff --git a/src/Stackify/Log/Transport/Config/AbstractConfig.php b/src/Stackify/Log/Transport/Config/AbstractConfig.php index 3b98ba7..c9b48a6 100644 --- a/src/Stackify/Log/Transport/Config/AbstractConfig.php +++ b/src/Stackify/Log/Transport/Config/AbstractConfig.php @@ -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; } /** @@ -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 @@ -607,7 +630,7 @@ public function getDebug() * * @return void */ - protected function logError($message) + public function logError($message) { $this->log($message, func_get_args(), false); } @@ -619,7 +642,7 @@ protected function logError($message) * * @return void */ - protected function logDebug($message) + public function logDebug($message) { if (!$this->getDebug()) { return;