From 3493ac10b1748fea3b3f570da91bfc6f3c55f066 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 17 Sep 2023 01:44:15 +0000 Subject: [PATCH] impl. request stat collection --- Message/Http/HttpHeader.php | 35 +++++++++++++++ Message/Http/ImpressionStat.php | 80 +++++++++++++++++++++++++++++++++ Uri/HttpUri.php | 2 +- Uri/UriInterface.php | 2 + 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 Message/Http/ImpressionStat.php diff --git a/Message/Http/HttpHeader.php b/Message/Http/HttpHeader.php index b46345fc0..9d1b56131 100755 --- a/Message/Http/HttpHeader.php +++ b/Message/Http/HttpHeader.php @@ -145,6 +145,41 @@ final class HttpHeader extends HeaderAbstract return $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1'; } + public function getReferer() : string + { + return $_SERVER['HTTP_REFERER'] ?? ''; + } + + public function getRequestTime() : int + { + return (int) ($_SERVER['REQUEST_TIME'] ?? \time()); + } + + public function getRequestIp() : string + { + return $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? ''; + } + + public function getBrowserName() { + $userAgent = $_SERVER['HTTP_USER_AGENT']; + + if (\strpos($userAgent, 'Opera') !== false || \strpos($userAgent, 'OPR/') !== false) { + return 'Opera'; + } elseif (\strpos($userAgent, 'Edge') !== false) { + return 'Microsoft Edge'; + } elseif (\strpos($userAgent, 'Chrome') !== false) { + return 'Google Chrome'; + } elseif (\strpos($userAgent, 'Safari') !== false) { + return 'Safari'; + } elseif (\strpos($userAgent, 'Firefox') !== false) { + return 'Mozilla Firefox'; + } elseif (\strpos($userAgent, 'MSIE') !== false || \strpos($userAgent, 'Trident/7') !== false) { + return 'Internet Explorer'; + } + + return 'Unknown'; + } + /** * Get all headers for apache and nginx * diff --git a/Message/Http/ImpressionStat.php b/Message/Http/ImpressionStat.php new file mode 100644 index 000000000..30e02637a --- /dev/null +++ b/Message/Http/ImpressionStat.php @@ -0,0 +1,80 @@ +language = $request->header->l11n->language; + $this->country = $request->header->l11n->country; + $this->uri = \substr($request->uri->uri, 0, 255); + $this->host = $request->uri->host; + $this->path = \substr($request->uri->path, 0, 255); + $this->address = $request->header->getRequestIp(); + $this->datetime = $request->header->getRequestTime(); + $this->referer = \substr($request->header->getReferer(), 0, 255); + $this->userAgent = $request->header->getBrowserName(); + } + + public function toArray() : array + { + return [ + 'address' => $this->address, + 'date' => \date('d-m-y', $this->datetime), + 'hour' => \date('H', $this->datetime), + 'host' => $this->host, + 'path' => $this->path, + 'uri' => $this->uri, + 'agent' => $this->userAgent, + 'language' => $this->language, + 'country' => $this->country, + 'referer' => $this->referer, + 'datetime' => $this->datetime, + ]; + } +} diff --git a/Uri/HttpUri.php b/Uri/HttpUri.php index 0815e0c8b..52b737062 100755 --- a/Uri/HttpUri.php +++ b/Uri/HttpUri.php @@ -108,7 +108,7 @@ final class HttpUri implements UriInterface * @var string * @since 1.0.0 */ - private string $path; + public string $path; /** * Uri path with offset. diff --git a/Uri/UriInterface.php b/Uri/UriInterface.php index 66b1a9e67..89c3528c3 100755 --- a/Uri/UriInterface.php +++ b/Uri/UriInterface.php @@ -24,6 +24,8 @@ namespace phpOMS\Uri; * @property array $fragments Fragments * @property string $user User * @property string $pass Password + * @property string $uri Uri + * @property string $path Path * * @package phpOMS\Uri * @license OMS License 2.0