/** * Prepare log object * @param integer $flags * @param object $settings * @param object $params * @param string $HTTPMethod * @param array $headers * @param object|null $additionalParameters * @return object */ private static function prepareObject($flags, $settings, $params, $HTTPMethod, $headers, $additionalParameters) { list($URI) = \Phramework\URIStrategy\URITemplate::URI(); $object = (object) ['request_id' => Phramework::getRequestUUID(), 'URI' => $URI, 'method' => $HTTPMethod, 'user_id' => null, 'ip_address' => \Phramework\Models\Util::getIPAddress(), 'request_headers' => null, 'request_params' => null, 'request_body_raw' => null, 'request_timestamp' => $_SERVER['REQUEST_TIME'], 'response_timestamp' => time(), 'response_headers' => null, 'response_body' => null, 'response_status_code' => http_response_code(), 'exception' => null, 'exception_class' => null, 'errors' => null, 'call_trace' => null, 'flags' => $flags, 'additional_parameters' => $additionalParameters]; if (($flags & self::LOG_USER_ID) !== 0) { $user = Phramework::getUser(); $object->user_id = $user ? $user->id : false; } /* Request flags */ if (($flags & self::LOG_REQUEST_HEADERS) !== 0) { //Asterisk authorization header value except schema if (isset($headers['Authorization'])) { list($authorizationSchema) = sscanf($headers['Authorization'], '%s %s'); $headers['Authorization'] = $authorizationSchema . ' ***'; } $object->request_headers = $headers; } else { $request_headers = []; if (($flags & self::LOG_REQUEST_HEADER_CONTENT_TYPE) !== 0) { //Write content type $request_headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE] = isset($headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE]) ? $headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE] : null; } if (($flags & self::LOG_REQUEST_HEADER_AGENT) !== 0) { $request_headers['User-Agent'] = isset($headers['User-Agent']) ? $headers['User-Agent'] : null; } if (($flags & self::LOG_REQUEST_HEADER_REFERER) !== 0) { $request_headers['Referer'] = isset($headers['Referer']) ? $headers['Referer'] : null; } if (($flags & self::LOG_REQUEST_HEADER_ACCEPT) !== 0) { $request_headers['Accept'] = isset($headers['Accept']) ? $headers['Accept'] : null; } if (!empty($request_headers)) { $object->request_headers = $request_headers; } } if (($flags & self::LOG_REQUEST_PARAMS) !== 0) { $object->request_params = $params; } if (($flags & self::LOG_REQUEST_BODY_RAW) !== 0) { $bodyRaw = file_get_contents('php://input'); if (strlen($bodyRaw) > $settings->body_raw_limit) { $bodyRaw = 'TRIMMED' . PHP_EOL . substr($bodyRaw, 0, $settings->body_raw_limit); } //Apply FILTER_SANITIZE_STRING $object->request_body_raw = \Phramework\Models\Filter::string($bodyRaw); //include content type headers if disabled if (!empty($bodyRaw) && ($flags & self::LOG_REQUEST_HEADERS) === 0 && ($flags & self::LOG_REQUEST_HEADER_CONTENT_TYPE) === 0) { $contentType = isset($headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE]) ? $headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE] : null; if (empty($object->request_headers)) { //make sure it's array $object->request_headers = []; } $object->request_headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE] = $contentType; } } $responseHeaders = new \stdClass(); foreach (headers_list() as $header) { list($key, $value) = explode(': ', $header); $responseHeaders->{$key} = $value; } /* Response flags */ if (($flags & self::LOG_RESPONSE_HEADER) !== 0) { $object->response_headers = $responseHeaders; } if (($flags & self::LOG_RESPONSE_BODY) !== 0) { $object->response_body = ob_get_contents(); if (($flags & self::LOG_RESPONSE_HEADER) === 0) { //show content type if headers are disabled $object->response_headers = (object) [\Phramework\Models\Request::HEADER_CONTENT_TYPE => isset($responseHeaders->{\Phramework\Models\Request::HEADER_CONTENT_TYPE}) ? $responseHeaders->{\Phramework\Models\Request::HEADER_CONTENT_TYPE} : null]; } } return $object; }
/** * Log query to database * @param string $query * @param array $parameters * Query parameters * @param integer $startTimestamp * Timestamp before query was executed * @param null|Exception $exception * *[Optional]* Exception object if any */ protected function log($query, $parameters, $startTimestamp, $exception = null) { $endTimestamp = time(); $duration = $endTimestamp - $startTimestamp; $user = \Phramework\Phramework::getUser(); $user_id = $user ? $user->id : null; //Get request URI list($URI) = \Phramework\URIStrategy\URITemplate::URI(); //Get request method $method = \Phramework\Phramework::getMethod(); $debugBacktrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); //Function used by database adapter $adapterFunction = $debugBacktrace[1]['function']; //Remove current log function call //Remove QueryLogAdapter execute* function call array_splice($debugBacktrace, 0, 2); foreach ($debugBacktrace as $k => &$v) { if (isset($v['class'])) { $class = $v['class']; $function = $v['function']; //Check if matrix has an entry for this class if (property_exists($this->matrix, $class)) { $matrixEntry = $this->matrix->{$class}; if (is_object($matrixEntry) || is_array($matrixEntry)) { //If vector, then is vector contains values for multiple methods of this class //Work with objects if (is_array($matrixEntry)) { $matrixEntry = (object) $matrixEntry; } if (property_exists($matrixEntry, $function)) { //If non positive value, dont log current query if (!$matrixEntry->{$function}) { return self::LOG_INGORED; } } } else { //scalar, this entry has a single value for all methods of this class //If non positive value, dont log current query if (!$matrixEntry) { return self::LOG_INGORED; } } } $v = $v['class'] . '::' . $v['function']; } else { $v = $v['function']; } } $schemaTable = $this->schema ? '"' . $this->schema . '"."' . $this->table . '"' : '"' . $this->table . '"'; //Insert query log record into table return $this->logAdapter->execute('INSERT INTO ' . $schemaTable . '( "request_id", "query", "parameters", "start_timestamp", "duration", "function", "URI", "method", "additional_parameters", "call_trace", "user_id", "exception" ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [\Phramework\Phramework::getRequestUUID(), $query, $parameters ? json_encode($parameters) : null, $startTimestamp, $duration, $adapterFunction, $URI, $method, $this->additionalParameters ? json_encode($this->additionalParameters) : null, json_encode($debugBacktrace), $user_id, $exception ? serialize(QueryLog::flattenExceptionBacktrace($exception)) : null]); }