|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* MikoPBX - free phone system for small business |
|
4
|
|
|
* Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
17
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
declare(strict_types=1); |
|
21
|
|
|
|
|
22
|
|
|
namespace MikoPBX\Common\Providers; |
|
23
|
|
|
|
|
24
|
|
|
use MikoPBX\Core\System\Configs\SentryConf; |
|
25
|
|
|
use MikoPBX\Core\Workers\Libs\WorkerPrepareAdvices\CheckConnection; |
|
26
|
|
|
use MikoPBX\Core\Workers\WorkerMarketplaceChecker; |
|
27
|
|
|
use Sentry\ClientBuilder; |
|
28
|
|
|
use Sentry\SentrySdk; |
|
29
|
|
|
use Sentry\State\HubInterface; |
|
30
|
|
|
use Sentry\State\Scope; |
|
31
|
|
|
use Phalcon\Di\DiInterface; |
|
32
|
|
|
use Phalcon\Di\ServiceProviderInterface; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Registers the Sentry error handler service. |
|
36
|
|
|
* |
|
37
|
|
|
* @package MikoPBX\Common\Providers |
|
38
|
|
|
*/ |
|
39
|
|
|
class SentryErrorHandlerProvider implements ServiceProviderInterface |
|
40
|
|
|
{ |
|
41
|
|
|
public const SERVICE_NAME = 'sentryErrorHandler'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Registers sentry error handler service provider |
|
45
|
|
|
* |
|
46
|
|
|
* @param DiInterface $di The DI container. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function register(DiInterface $di): void |
|
49
|
|
|
{ |
|
50
|
|
|
$di->set( |
|
51
|
|
|
self::SERVICE_NAME, |
|
52
|
|
|
function () use ($di): ?HubInterface { |
|
53
|
|
|
|
|
54
|
|
|
// No internet no sentry |
|
55
|
|
|
if (!file_exists(CheckConnection::INTERNET_FLAG_FILE)){ |
|
56
|
|
|
return null; |
|
57
|
|
|
} |
|
58
|
|
|
// Check if config file exists, return null if not and disable sentry logger |
|
59
|
|
|
if (!file_exists(SentryConf::CONF_FILE)) { |
|
60
|
|
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
$options = json_decode(file_get_contents(SentryConf::CONF_FILE) ?? '', true); |
|
63
|
|
|
if (empty($options)) { |
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$moduleTag = 'unknown'; |
|
68
|
|
|
if ($di->has(RegistryProvider::SERVICE_NAME)) { |
|
69
|
|
|
$moduleTag = $di->getShared(RegistryProvider::SERVICE_NAME)->libraryName ?? 'unknown'; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// Create and bind the Sentry client |
|
73
|
|
|
$client = ClientBuilder::create($options)->getClient(); |
|
74
|
|
|
SentrySdk::init()->bindClient($client); |
|
75
|
|
|
|
|
76
|
|
|
// Configure Sentry scope with user info, license info, and library tag |
|
77
|
|
|
$licenseInfo = self::prepareLicenseInfo($di); |
|
78
|
|
|
|
|
79
|
|
|
SentrySdk::getCurrentHub()->configureScope( |
|
80
|
|
|
function (Scope $scope) use ($options, $licenseInfo, $moduleTag): void { |
|
|
|
|
|
|
81
|
|
|
if (!empty($licenseInfo->email)) { |
|
82
|
|
|
$scope->setUser(['id' => $licenseInfo->email]); |
|
83
|
|
|
} |
|
84
|
|
|
if (!empty($licenseInfo->key)) { |
|
85
|
|
|
$scope->setExtra('key', $licenseInfo->key); |
|
86
|
|
|
} |
|
87
|
|
|
if (!empty($licenseInfo->company)) { |
|
88
|
|
|
$scope->setExtra('company', $licenseInfo->company); |
|
89
|
|
|
} |
|
90
|
|
|
if (isset($moduleTag)) { |
|
91
|
|
|
$scope->setTag('library', $moduleTag); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
return SentrySdk::getCurrentHub(); |
|
97
|
|
|
} |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Prepares and returns the license info object. |
|
103
|
|
|
* |
|
104
|
|
|
* @return \stdClass The prepared license info object. |
|
105
|
|
|
*/ |
|
106
|
|
|
private static function prepareLicenseInfo(DiInterface $di): \stdClass |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
$licenseInfo = new \stdClass(); |
|
109
|
|
|
$licenseInfo->key = ''; |
|
110
|
|
|
$licenseInfo->email = ''; |
|
111
|
|
|
$licenseInfo->companyname = ''; |
|
112
|
|
|
|
|
113
|
|
|
if (!file_exists(WorkerMarketplaceChecker::LIC_FILE_PATH)){ |
|
114
|
|
|
return $licenseInfo; |
|
115
|
|
|
} |
|
116
|
|
|
// Retrieve the last get license request from the cache |
|
117
|
|
|
$xmlFromFile = simplexml_load_file(WorkerMarketplaceChecker::LIC_FILE_PATH); |
|
118
|
|
|
if ($xmlFromFile) { |
|
|
|
|
|
|
119
|
|
|
foreach ($xmlFromFile->attributes() as $attribute => $value) { |
|
120
|
|
|
if (!empty($licenseInfo->{'@attributes'}->$attribute)) { |
|
121
|
|
|
$licenseInfo->$attribute = $licenseInfo->{'@attributes'}->$attribute; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
return $licenseInfo; |
|
126
|
|
|
} |
|
127
|
|
|
} |
This check looks for imports that have been defined, but are not used in the scope.