Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
log.php
1<?php
2namespace Bitrix\Rest;
3
5
37class LogTable extends Main\Entity\DataManager
38{
44 public static function getTableName()
45 {
46 return 'b_rest_log';
47 }
48
54 public static function getMap()
55 {
56 return array(
57 'ID' => array(
58 'data_type' => 'integer',
59 'primary' => true,
60 'autocomplete' => true,
61 ),
62 'TIMESTAMP_X' => array(
63 'data_type' => 'datetime',
64 ),
65 'CLIENT_ID' => array(
66 'data_type' => 'string',
67 ),
68 'PASSWORD_ID' => array(
69 'data_type' => 'integer',
70 ),
71 'SCOPE' => array(
72 'data_type' => 'string',
73 ),
74 'METHOD' => array(
75 'data_type' => 'string',
76 ),
77 'REQUEST_METHOD' => array(
78 'data_type' => 'string',
79 ),
80 'REQUEST_URI' => array(
81 'data_type' => 'string',
82 ),
83 'REQUEST_AUTH' => array(
84 'data_type' => 'text',
85 'serialized' => true,
86 ),
87 'REQUEST_DATA' => array(
88 'data_type' => 'text',
89 'serialized' => true,
90 ),
91 'RESPONSE_STATUS' => array(
92 'data_type' => 'string',
93 ),
94 'RESPONSE_DATA' => array(
95 'data_type' => 'text',
96 'serialized' => true,
97 ),
98 );
99 }
100
112 public static function log(\CRestServer $server, $data)
113 {
114 if(static::checkEntry($server))
115 {
116 static::addEntry($server, $data);
117 }
118 }
119
127 public static function checkEntry(\CRestServer $server)
128 {
129 global $USER;
130
131 $logEndTime = intval(\Bitrix\Main\Config\Option::get('rest', 'log_end_time', 0));
132 if ($logEndTime < time())
133 {
134 return false;
135 }
136
137 $logOptions = @unserialize(
138 \Bitrix\Main\Config\Option::get('rest', 'log_filters', ''),
139 [
140 'allowed_classes' => false
141 ]
142 );
143 if (!is_array($logOptions))
144 {
145 $logOptions = array();
146 }
147
148 if(
149 isset($logOptions['client_id']) && $server->getClientId() !== $logOptions['client_id']
150 || isset($logOptions['password_id']) && $server->getPasswordId() !== $logOptions['password_id']
151 || isset($logOptions['scope']) && $server->getScope() !== $logOptions['scope']
152 || isset($logOptions['method']) && $server->getMethod() !== $logOptions['method']
153 || isset($logOptions['user_id']) && $USER->getId() !== $logOptions['user_id']
154 )
155 {
156 return false;
157 }
158
159 return true;
160 }
161
170 public static function addEntry(\CRestServer $server, $data)
171 {
172 $request = Main\Context::getCurrent()->getRequest();
173 static::filterResponseData($data);
174
175 static::add(array(
176 'CLIENT_ID' => $server->getClientId(),
177 'PASSWORD_ID' => $server->getPasswordId(),
178 'SCOPE' => $server->getScope(),
179 'METHOD' => $server->getMethod(),
180 'REQUEST_METHOD' => $request->getRequestMethod(),
181 'REQUEST_URI' => $request->getRequestUri(),
182 'REQUEST_AUTH' => $server->getAuth(),
183 'REQUEST_DATA' => $server->getQuery(),
184 'RESPONSE_STATUS' => \CHTTP::getLastStatus(),
185 'RESPONSE_DATA' => $data,
186 ));
187 }
188
189 public static function filterResponseData(&$data)
190 {
191 //filter non-searizable objects
192 if (is_object($data) && !method_exists($data, '__serialize'))
193 {
194 $data = '';
195 }
196 else if (is_array($data))
197 {
198 foreach ($data as &$oneData)
199 {
200 static::filterResponseData($oneData);
201 }
202 }
203 }
204
205 public static function getCountAll()
206 {
207 $entity = static::getEntity();
208 $sqlTableName = static::getTableName();
209
210 $sql = "SELECT count(1) CNT FROM {$sqlTableName}";
211 $query = $entity->getConnection()->query($sql);
212 return $query->fetch()["CNT"];
213 }
214
215 public static function clearAll()
216 {
217 $entity = static::getEntity();
218 $sqlTableName = static::getTableName();
219
220 $sql = "TRUNCATE TABLE {$sqlTableName}";
221 $entity->getConnection()->queryExecute($sql);
222 }
223
224 public static function cleanUpAgent()
225 {
226 $entity = static::getEntity();
227 $sqlTableName = static::getTableName();
228 $connection = $entity->getConnection();
229
230 $lastIdQuery = $connection->query("
231 SELECT max(ID) MID
232 from {$sqlTableName}
233 ");
234 $lastId = $lastIdQuery->fetch();
235 if ($lastId && $lastId['MID'])
236 {
237 $date = new Main\Type\DateTime();
238 $date->add("-7D");
239
240 $lastTimeQuery = $connection->query("
241 SELECT TIMESTAMP_X
242 from {$sqlTableName}
243 WHERE ID = $lastId[MID]
244 ");
245 $lastTime = $lastTimeQuery->fetch();
246 if ($lastTime && $lastTime['TIMESTAMP_X'] < $date)
247 {
248 static::clearAll();
249 }
250 }
251
252 return "\\Bitrix\\Rest\\LogTable::cleanUpAgent();";
253 }
254}
static getMap()
Definition log.php:54
static addEntry(\CRestServer $server, $data)
Definition log.php:170
static getCountAll()
Definition log.php:205
static filterResponseData(&$data)
Definition log.php:189
static clearAll()
Definition log.php:215
static cleanUpAgent()
Definition log.php:224
static checkEntry(\CRestServer $server)
Definition log.php:127
static log(\CRestServer $server, $data)
Definition log.php:112
static getTableName()
Definition log.php:44