| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package Logtivity |
| 5 |
* @contact logtivity.io, hello@logtivity.io |
| 6 |
* @copyright 2024-2025 Logtivity. All rights reserved |
| 7 |
* @license https://www.gnu.org/licenses/gpl.html GNU/GPL |
| 8 |
* |
| 9 |
* This file is part of Logtivity. |
| 10 |
* |
| 11 |
* Logtivity is free software: you can redistribute it and/or modify |
| 12 |
* it under the terms of the GNU General Public License as published by |
| 13 |
* the Free Software Foundation, either version 2 of the License, or |
| 14 |
* (at your option) any later version. |
| 15 |
* |
| 16 |
* Logtivity is distributed in the hope that it will be useful, |
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
* GNU General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU General Public License |
| 22 |
* along with Logtivity. If not, see <https://www.gnu.org/licenses/>. |
| 23 |
*/ |
| 24 |
|
| 25 |
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace |
| 26 |
// phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps |
| 27 |
|
| 28 |
class Logtivity_Error_Logger extends Logtivity_Api |
| 29 |
{ |
| 30 |
use Logtivity_User_Logger_Trait; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var bool |
| 34 |
*/ |
| 35 |
protected bool $active = true; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var array |
| 39 |
*/ |
| 40 |
protected array $error = []; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var string[] |
| 44 |
*/ |
| 45 |
protected static array $recordedErrors = []; |
| 46 |
|
| 47 |
/** |
| 48 |
* @param array $error |
| 49 |
*/ |
| 50 |
public function __construct(array $error) |
| 51 |
{ |
| 52 |
$this->error = $error; |
| 53 |
|
| 54 |
$this->setUser(); |
| 55 |
|
| 56 |
parent::__construct(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* @return $this |
| 61 |
*/ |
| 62 |
public function stop(): self |
| 63 |
{ |
| 64 |
$this->active = false; |
| 65 |
|
| 66 |
return $this; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @return ?array |
| 71 |
*/ |
| 72 |
public function send(): void |
| 73 |
{ |
| 74 |
$message = $this->error['message'] ?? ''; |
| 75 |
if (in_array($message, self::$recordedErrors) === false) { |
| 76 |
self::$recordedErrors[] = $this->error['message']; |
| 77 |
|
| 78 |
do_action('wp_logtivity_error_logger_instance', $this); |
| 79 |
|
| 80 |
if ($this->active) { |
| 81 |
$this->makeRequest('/errors/store', $this->getData()); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
public function getData(): array |
| 90 |
{ |
| 91 |
$error = explode('Stack trace:', $this->error['message']); |
| 92 |
|
| 93 |
return [ |
| 94 |
'type' => $this->getErrorLevel($this->error['type']) ?? null, |
| 95 |
'message' => $error[0], |
| 96 |
'stack_trace' => $this->generateStackTrace( |
| 97 |
[ |
| 98 |
'file' => $this->error['file'] ?? null, |
| 99 |
'line' => $this->error['line'] ?? null, |
| 100 |
], |
| 101 |
$error[1] ?? null |
| 102 |
), |
| 103 |
'file' => $this->error['file'] ?? null, |
| 104 |
'line' => $this->error['line'] ?? null, |
| 105 |
'user_id' => $this->getUserID(), |
| 106 |
'username' => $this->maybeGetUsersUsername(), |
| 107 |
'ip_address' => $this->maybeGetUsersIp(), |
| 108 |
'user_authenticated' => $this->user->isLoggedIn(), |
| 109 |
'url' => $this->getCurrentUrl(), |
| 110 |
'method' => $this->getRequestMethod(), |
| 111 |
'php_version' => phpversion(), |
| 112 |
'level' => $this->error['level'] ?? null, |
| 113 |
]; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* @param array $line |
| 118 |
* @param ?string $stackTrace |
| 119 |
* |
| 120 |
* @return array |
| 121 |
*/ |
| 122 |
private function generateStackTrace(array $line, ?string $stackTrace): array |
| 123 |
{ |
| 124 |
$stackTraceObject = new Logtivity_Stack_Trace(); |
| 125 |
|
| 126 |
if (isset($this->error['stack_trace'])) { |
| 127 |
return $stackTraceObject->createFromArray($this->error['stack_trace']); |
| 128 |
} |
| 129 |
|
| 130 |
return array_merge( |
| 131 |
[$stackTraceObject->createFileObject($line['file'], $line['line'])], |
| 132 |
$stackTraceObject->createFromString($stackTrace) |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @return string |
| 138 |
*/ |
| 139 |
private function getRequestMethod(): string |
| 140 |
{ |
| 141 |
return sanitize_text_field($_SERVER['REQUEST_METHOD'] ?? ''); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* @return ?string |
| 146 |
*/ |
| 147 |
private function getCurrentUrl(): ?string |
| 148 |
{ |
| 149 |
$host = sanitize_text_field($_SERVER['HTTP_HOST'] ?? null); |
| 150 |
if ($host) { |
| 151 |
$ssl = sanitize_text_field($_SERVER['HTTPS'] ?? 'off') != 'off' |
| 152 |
|| sanitize_text_field($_SERVER['SERVER_PORT'] ?? '80') == 443; |
| 153 |
|
| 154 |
$protocol = $ssl ? 'https://' : 'http://'; |
| 155 |
|
| 156 |
$path = sanitize_text_field($_SERVER['REQUEST_URI'] ?? ''); |
| 157 |
|
| 158 |
$url = sanitize_url($protocol . $host . $path); |
| 159 |
|
| 160 |
if ($url != $protocol) { |
| 161 |
return $url; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return null; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @param $level |
| 170 |
* |
| 171 |
* @return string |
| 172 |
*/ |
| 173 |
private function getErrorLevel($level): string |
| 174 |
{ |
| 175 |
$errorLevels = logtivity_get_error_levels(); |
| 176 |
|
| 177 |
return $errorLevels[$level] ?? $level; |
| 178 |
} |
| 179 |
} |
| 180 |
|