Passed
Push — devel-3.0 ( f3e30e...950ad4 )
by Rubén
03:36
created

RemoteSyslogHandler::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
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 Monolog\Handler\SyslogUdpHandler;
29
use Monolog\Logger;
30
use SP\Core\Events\Event;
31
use SP\Core\Events\EventReceiver;
32
use SP\Core\Language;
33
use SP\Providers\EventsTrait;
34
use SP\Providers\Provider;
35
use SplSubject;
36
37
/**
38
 * Class RemoteSyslogHandler
39
 *
40
 * @package SP\Providers\Log
41
 */
42
final class RemoteSyslogHandler 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
    const MESSAGE_FORMAT = '%s;%s';
71
72
    /**
73
     * @var string
74
     */
75
    private $events;
76
    /**
77
     * @var Logger
78
     */
79
    private $logger;
80
    /**
81
     * @var Language
82
     */
83
    private $language;
84
85
    /**
86
     * Devuelve los eventos que implementa el observador
87
     *
88
     * @return array
89
     */
90
    public function getEvents()
91
    {
92
        return self::EVENTS;
93
    }
94
95
    /**
96
     * Devuelve los eventos que implementa el observador en formato cadena
97
     *
98
     * @return string
99
     */
100
    public function getEventsString()
101
    {
102
        return $this->events;
103
    }
104
105
    /**
106
     * Receive update from subject
107
     *
108
     * @link  http://php.net/manual/en/splobserver.update.php
109
     *
110
     * @param SplSubject $subject <p>
111
     *                            The <b>SplSubject</b> notifying the observer of an update.
112
     *                            </p>
113
     *
114
     * @return void
115
     * @since 5.1.0
116
     */
117
    public function update(SplSubject $subject)
118
    {
119
        $this->updateEvent('update', new Event($subject));
120
    }
121
122
    /**
123
     * Evento de actualización
124
     *
125
     * @param string $eventType Nombre del evento
126
     * @param Event  $event     Objeto del evento
127
     */
128
    public function updateEvent($eventType, Event $event)
129
    {
130
        $this->language->setAppLocales();
131
132
        if (($e = $event->getSource()) instanceof \Exception) {
133
            /** @var \Exception $e */
134
            $this->logger->error(sprintf(self::MESSAGE_FORMAT, $eventType, __($e->getMessage())));
135
        } elseif (($eventMessage = $event->getEventMessage()) !== null) {
136
            $this->logger->debug(sprintf(self::MESSAGE_FORMAT, $eventType, $eventMessage->composeText(';')));
137
        }
138
139
        $this->language->unsetAppLocales();
140
    }
141
142
    /**
143
     * @param Container $dic
144
     *
145
     * @throws \DI\DependencyException
146
     * @throws \DI\NotFoundException
147
     */
148
    protected function initialize(Container $dic)
149
    {
150
        $this->language = $dic->get(Language::class);
151
152
        $configData = $this->config->getConfigData();
153
154
        $this->logger = $dic->get(Logger::class)
155
            ->pushHandler(
156
                new SyslogUdpHandler(
157
                    $configData->getSyslogServer(),
158
                    $configData->getSyslogPort(),
159
                    LOG_USER,
160
                    Logger::DEBUG,
161
                    true,
162
                    'syspass'
163
                )
164
            );
165
166
        $configEvents = $configData->getLogEvents();
167
168
        if (count($configEvents) === 0) {
169
            $this->events = $this->parseEventsToRegex(self::EVENTS);
170
        } else {
171
            $this->events = $this->parseEventsToRegex($configEvents);
172
        }
173
    }
174
}