1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
event.php
См. документацию.
1<?php
2
4
6{
7 private static $instance = null;
8
9 private $isDBEngineActive = false;
11 private $fileLogger;
13 private $sysLogger;
14 private $syslogPriority = LOG_WARNING;
15
17 private $messageFormatter = null;
18
19 private static $syslogFacilities = array(
20 LOG_SYSLOG => "LOG_SYSLOG",
21 LOG_AUTH => "LOG_AUTH",
22 LOG_AUTHPRIV => "LOG_AUTHPRIV",
23 LOG_DAEMON => "LOG_DAEMON",
24 LOG_USER => "LOG_USER"
25 );
26
27 private static $syslogPriorities = array(
28 LOG_EMERG => "LOG_EMERG",
29 LOG_ALERT => "LOG_ALERT",
30 LOG_CRIT => "LOG_CRIT",
31 LOG_ERR => "LOG_ERR",
32 LOG_WARNING => "LOG_WARNING",
33 LOG_NOTICE => "LOG_NOTICE",
34 LOG_INFO => "LOG_INFO",
35 LOG_DEBUG => "LOG_DEBUG"
36 );
37
41 public static function getInstance()
42 {
43 if (is_null(self::$instance))
44 {
45 self::$instance = new static();
46 }
47 return self::$instance;
48 }
49
57 public function doLog($severity, $auditType, $itemName, $itemDescription)
58 {
59 $result = false;
60
61 if ($this->isDBEngineActive)
62 {
63 $result = CEventLog::log($severity, $auditType, "security", $itemName, $itemDescription);
64 }
65
66 if ($this->sysLogger || $this->fileLogger)
67 {
68 $message = $this->messageFormatter->format($auditType, $itemName, $itemDescription);
69
70 if ($this->sysLogger)
71 {
72 $level = Diag\SysLogger::priorityToLevel($this->syslogPriority);
73 $this->sysLogger->log($level, $message);
74 }
75
76 if ($this->fileLogger)
77 {
78 $message = static::sanitizeMessage($message);
79 $message .= "\n";
80
81 $this->fileLogger->warning($message);
82 }
83 $result = true;
84 }
85 return $result;
86 }
87
91 public static function getSyslogPriorities()
92 {
93 return static::$syslogPriorities;
94 }
95
99 public static function getSyslogFacilities()
100 {
101 if (static::isRunOnWin())
102 return array(LOG_USER => "LOG_USER");
103 else
104 return static::$syslogFacilities;
105 }
106
112 public function getEventsCount($timestamp = '')
113 {
114 if (!$this->isDBEngineActive)
115 return 0;
116
121 global $DB, $CACHE_MANAGER;
122 $ttl = 3600;
123 $cacheId = 'sec_events_count';
124 $cacheDir = '/security/events';
125
126 if ($CACHE_MANAGER->read($ttl, $cacheId, $cacheDir))
127 {
128 $result = $CACHE_MANAGER->get($cacheId);
129 }
130 else
131 {
132 if ($timestamp == '')
133 {
134 $days = COption::getOptionInt("main", "event_log_cleanup_days", 7);
135 if ($days > 7)
136 $days = 7;
137 $timestamp = convertTimeStamp(time()-$days*24*3600+CTimeZone::getOffset());
138 }
139
140 $arAudits = array(
141 "SECURITY_FILTER_SQL",
142 "SECURITY_FILTER_XSS",
143 "SECURITY_FILTER_XSS2",
144 "SECURITY_FILTER_PHP"
145 );
146
147 $strAuditsSql = implode("', '",$arAudits);
148
149 $strSql = "
150 SELECT COUNT(ID) AS COUNT
151 FROM
152 b_event_log
153 WHERE
154 AUDIT_TYPE_ID in ('".$strAuditsSql."')
155 AND
156 (MODULE_ID = 'security' and MODULE_ID is not null)
157 AND
158 TIMESTAMP_X >= ".$DB->charToDateFunction($DB->forSQL($timestamp))."
159 ";
160
161 $res = $DB->query($strSql);
162
163 if ($arRes = $res->fetch())
164 $result = $arRes["COUNT"];
165 else
166 $result = 0;
167
168 $CACHE_MANAGER->set($cacheId, $result);
169 }
170
171 return $result;
172 }
173
174 public function getMessageFormatter()
175 {
176 return $this->messageFormatter;
177 }
178
179 private function __construct()
180 {
181 if (COption::getOptionString("security", "security_event_db_active") === "Y")
182 $this->initializeDBEngine();
183
184 if (COption::getOptionString("security", "security_event_syslog_active") == "Y")
185 $this->initializeSyslogEngine();
186
187 if (COption::getOptionString("security", "security_event_file_active") == "Y")
188 $this->initializeFileEngine();
189
190 $this->messageFormatter = new CSecurityEventMessageFormatter(
191 COption::getOptionString("security", "security_event_format"),
192 COption::getOptionString("security", "security_event_userinfo_format")
193 );
194 }
195
196 private function initializeFileEngine()
197 {
198 $filePath = COption::getOptionString("security", "security_event_file_path");
199 if ($filePath && checkDirPath($filePath))
200 {
201 $this->fileLogger = new Diag\FileLogger($filePath, 0);
202 }
203 }
204
205 private function initializeDBEngine()
206 {
207 $this->isDBEngineActive = true;
208 }
209
210 private function initializeSyslogEngine()
211 {
212 if (self::isRunOnWin())
213 {
214 $facility = LOG_USER;
215 }
216 else
217 {
218 $facility = (int) COption::getOptionString("security", "security_event_syslog_facility");
219 }
220
221 $this->syslogPriority = COption::getOptionString("security", "security_event_syslog_priority");
222
223 $this->sysLogger = new Diag\SysLogger('Bitrix WAF', LOG_ODELAY, $facility);
224 }
225
229 private static function isRunOnWin()
230 {
231 return (strtoupper(substr(PHP_OS, 0, 3)) === "WIN");
232 }
233
238 private static function sanitizeMessage($message)
239 {
240 return str_replace(array("\r", "\n"), array("\\r", "\\n"), $message);
241 }
242}
static priorityToLevel(int $priority)
Определения syslogger.php:48
Определения event.php:6
static getSyslogFacilities()
Определения event.php:99
getEventsCount($timestamp='')
Определения event.php:112
doLog($severity, $auditType, $itemName, $itemDescription)
Определения event.php:57
getMessageFormatter()
Определения event.php:174
static getInstance()
Определения event.php:41
static getSyslogPriorities()
Определения event.php:91
global $CACHE_MANAGER
Определения clear_component_cache.php:7
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
global $DB
Определения cron_frame.php:29
Определения cachetracker.php:2
$message
Определения payment.php:8
$arRes
Определения options.php:104