|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* sysPass |
|
4
|
|
|
* |
|
5
|
|
|
* @author nuxsmin |
|
6
|
|
|
* @link https://syspass.org |
|
7
|
|
|
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org |
|
8
|
|
|
* |
|
9
|
|
|
* This file is part of sysPass. |
|
10
|
|
|
* |
|
11
|
|
|
* sysPass 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 3 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* sysPass 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 sysPass. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace SP\Providers\Log; |
|
26
|
|
|
|
|
27
|
|
|
use DI\Container; |
|
28
|
|
|
use SP\Core\Events\Event; |
|
29
|
|
|
use SP\Core\Events\EventReceiver; |
|
30
|
|
|
use SP\Core\Language; |
|
31
|
|
|
use SP\DataModel\EventlogData; |
|
32
|
|
|
use SP\Providers\EventsTrait; |
|
33
|
|
|
use SP\Providers\Provider; |
|
34
|
|
|
use SP\Services\EventLog\EventlogService; |
|
35
|
|
|
use SplSubject; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Class LogHandler |
|
39
|
|
|
* |
|
40
|
|
|
* @package SP\Providers\Log |
|
41
|
|
|
*/ |
|
42
|
|
|
final class DatabaseLogHandler extends Provider implements EventReceiver |
|
43
|
|
|
{ |
|
44
|
|
|
use EventsTrait; |
|
45
|
|
|
|
|
46
|
|
|
const EVENTS = [ |
|
47
|
|
|
'create.', |
|
48
|
|
|
'delete.', |
|
49
|
|
|
'edit.', |
|
50
|
|
|
'exception', |
|
51
|
|
|
'save.', |
|
52
|
|
|
'show.account.pass', |
|
53
|
|
|
'show.account.link', |
|
54
|
|
|
'copy.account.pass', |
|
55
|
|
|
'clear.eventlog', |
|
56
|
|
|
'login.', |
|
57
|
|
|
'logout', |
|
58
|
|
|
'track.', |
|
59
|
|
|
'acl.deny', |
|
60
|
|
|
'check.tempMasterPassword', |
|
61
|
|
|
'expire.tempMasterPassword', |
|
62
|
|
|
'refresh.masterPassword', |
|
63
|
|
|
'update.', |
|
64
|
|
|
'import.ldap.', |
|
65
|
|
|
'run.', |
|
66
|
|
|
'send.mail', |
|
67
|
|
|
'show.authToken' |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var EventlogService |
|
72
|
|
|
*/ |
|
73
|
|
|
private $eventlogService; |
|
74
|
|
|
/** |
|
75
|
|
|
* @var string |
|
76
|
|
|
*/ |
|
77
|
|
|
private $events; |
|
78
|
|
|
/** |
|
79
|
|
|
* @var Language |
|
80
|
|
|
*/ |
|
81
|
|
|
private $language; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Receive update from subject |
|
85
|
|
|
* |
|
86
|
|
|
* @link http://php.net/manual/en/splobserver.update.php |
|
87
|
|
|
* |
|
88
|
|
|
* @param SplSubject $subject <p> |
|
89
|
|
|
* The <b>SplSubject</b> notifying the observer of an update. |
|
90
|
|
|
* </p> |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
* @since 5.1.0 |
|
94
|
|
|
*/ |
|
95
|
|
|
public function update(SplSubject $subject) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->updateEvent('update', new Event($subject)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Evento de actualización |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $eventType Nombre del evento |
|
104
|
|
|
* @param Event $event Objeto del evento |
|
105
|
|
|
*/ |
|
106
|
|
|
public function updateEvent($eventType, Event $event) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->language->setAppLocales(); |
|
109
|
|
|
|
|
110
|
|
|
$eventlogData = new EventlogData(); |
|
111
|
|
|
$eventlogData->setAction($eventType); |
|
112
|
|
|
$eventlogData->setLevel('INFO'); |
|
113
|
|
|
|
|
114
|
|
|
if (($e = $event->getSource()) instanceof \Exception) { |
|
115
|
|
|
/** @var \Exception $e */ |
|
116
|
|
|
$eventlogData->setLevel('ERROR'); |
|
117
|
|
|
$eventlogData->setDescription(__($e->getMessage())); |
|
118
|
|
|
} elseif (($eventMessage = $event->getEventMessage()) !== null) { |
|
119
|
|
|
$eventlogData->setDescription($eventMessage->composeText()); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
try { |
|
123
|
|
|
$this->eventlogService->create($eventlogData); |
|
124
|
|
|
} catch (\Exception $e) { |
|
125
|
|
|
processException($e); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$this->language->unsetAppLocales(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Devuelve los eventos que implementa el observador en formato cadena |
|
133
|
|
|
* |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getEventsString() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->events; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Devuelve los eventos que implementa el observador |
|
143
|
|
|
* |
|
144
|
|
|
* @return array |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getEvents() |
|
147
|
|
|
{ |
|
148
|
|
|
return self::EVENTS; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param Container $dic |
|
153
|
|
|
* |
|
154
|
|
|
* @throws \DI\DependencyException |
|
155
|
|
|
* @throws \DI\NotFoundException |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function initialize(Container $dic) |
|
158
|
|
|
{ |
|
159
|
|
|
$this->language = $dic->get(Language::class); |
|
160
|
|
|
$this->eventlogService = $dic->get(EventlogService::class); |
|
161
|
|
|
|
|
162
|
|
|
$configEvents = $this->config->getConfigData()->getLogEvents(); |
|
163
|
|
|
|
|
164
|
|
|
if (count($configEvents) === 0) { |
|
165
|
|
|
$this->events = $this->parseEventsToRegex(self::EVENTS); |
|
166
|
|
|
} else { |
|
167
|
|
|
$this->events = $this->parseEventsToRegex($configEvents); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
} |