Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
BaseController.php
1
<?php
2
3
namespace
Bitrix\Im\V2\Controller
;
4
5
use
Bitrix\Im\Dialog
;
6
use
Bitrix\Im\V2\Chat
;
7
use
Bitrix\Im\V2\Controller\Filter\ActionUuidHandler
;
8
use
Bitrix\Im\V2\Controller\Filter\AuthorizationPrefilter
;
9
use
Bitrix\Im\V2\Controller\Filter\CheckChatAccess
;
10
use
Bitrix\Im\V2\Controller\Filter\SameChatMessageFilter
;
11
use
Bitrix\Im\V2\Controller\Filter\StartIdFilter
;
12
use
Bitrix\Im\V2\Message
;
13
use
Bitrix\Im\V2\Message\MessageError
;
14
use
Bitrix\Im\V2\Rest\RestAdapter
;
15
use
Bitrix\Im\V2\Rest\RestConvertible
;
16
use
Bitrix\Main\Application
;
17
use
Bitrix\Main\Engine\AutoWire\ExactParameter
;
18
use
Bitrix\Main\Engine\Controller
;
19
use
Bitrix\Main\Engine\Response\Converter
;
20
use
Bitrix\Main\Text\Encoding
;
21
use
Bitrix\Main\Type\ParameterDictionary
;
22
23
abstract
class
BaseController
extends
Controller
24
{
25
protected
const
MAX_LIMIT
= 200;
26
protected
const
DEFAULT_LIMIT
= 50;
27
28
public
function
getAutoWiredParameters
()
29
{
30
return
[
31
new
ExactParameter
(
32
Chat::class
,
33
'chat'
,
34
function
($className,
string
$dialogId) {
35
$chatId =
Dialog::getChatId
($dialogId);
36
37
return
Chat::getInstance
((
int
)$chatId);
38
}
39
),
40
new
ExactParameter
(
41
Chat::class
,
42
'chat'
,
43
function
($className,
int
$chatId) {
44
return
Chat::getInstance
($chatId);
45
}
46
),
47
new
ExactParameter
(
48
Message::class
,
49
'message'
,
50
function
($className,
int
$messageId) {
51
return
$this->
getMessageById
($messageId);
52
}
53
),
54
];
55
}
56
57
protected
function
getDefaultPreFilters
()
58
{
59
return
array_merge(
60
[
61
new
AuthorizationPrefilter
(),
62
],
63
parent::getDefaultPreFilters(),
64
[
65
new
SameChatMessageFilter
(),
66
new
StartIdFilter
(),
67
new
CheckChatAccess
(),
68
new
ActionUuidHandler
(),
69
]
70
);
71
}
72
73
protected
function
getLimit
(
int
$limit): int
74
{
75
return
$limit > 0 && $limit <= static::MAX_LIMIT ? $limit : static::DEFAULT_LIMIT;
76
}
77
78
protected
function
toRestFormat
(
RestConvertible
...$entities): array
79
{
80
return
(
new
RestAdapter
(...$entities))->toRestFormat();
81
}
82
83
public
static
function
recursiveWhiteList
($fields, $whiteList,
bool
$sanitizeOnly =
false
)
84
{
85
$data = [];
86
$converter =
new
Converter
(Converter::TO_SNAKE | Converter::TO_UPPER);
87
foreach
($fields as $field => $value)
88
{
89
if
(is_array($value))
90
{
91
$data[$converter->process($field)] =
self::recursiveWhiteList
($value, $whiteList[$field],
true
);
92
}
93
elseif ((is_array($whiteList) && in_array($field, $whiteList)) || $sanitizeOnly)
94
{
95
$data[$converter->process($field)] = $value;
96
}
97
}
98
99
return
$data;
100
}
101
102
//todo: think about recursion in method.
103
protected
function
checkWhiteList
(array $fields, array $whiteList): array
104
{
105
$filteredFields = [];
106
107
foreach
($whiteList as $allowedField)
108
{
109
if
(isset($fields[$allowedField]))
110
{
111
$filteredFields[$allowedField] = $fields[$allowedField];
112
}
113
}
114
115
return
$filteredFields;
116
}
117
118
protected
function
getMessageById
(
int
$id): ?
Message
119
{
120
$message = new \Bitrix\Im\V2\Message($id);
121
122
if
($message->getMessageId() ===
null
)
123
{
124
$this->
addError
(
new
MessageError
(MessageError::MESSAGE_NOT_FOUND));
125
126
return
null
;
127
}
128
129
return
$message;
130
}
131
132
protected
function
convertCharToBool
(
string
$char,
bool
$default =
false
): bool
133
{
134
if
($char ===
'Y'
)
135
{
136
return
true
;
137
}
138
if
($char ===
'N'
)
139
{
140
return
false
;
141
}
142
143
return
$default;
144
}
145
146
protected
function
getRawValue
(
string
$key)
147
{
148
return
$this->prepareRawValue($this->request->getPostList(), $key)
149
?? $this->prepareRawValue($this->request->getQueryList(), $key)
150
?? null
151
;
152
}
153
154
private
function
prepareRawValue(
ParameterDictionary
$list,
string
$key)
155
{
156
$rawData = $list->
toArrayRaw
();
157
if
(isset($rawData[$key]))
158
{
159
if
(
Application::getInstance
()->isUtfMode())
160
{
161
return
$rawData[$key];
162
}
163
164
return
Encoding::convertEncoding($rawData[$key],
'UTF-8'
, SITE_CHARSET);
165
}
166
167
$data = $list->
toArray
();
168
169
return
$data[$key] ??
null
;
170
}
171
}
Bitrix\Im\Dialog
Definition
dialog.php:9
Bitrix\Im\Dialog\getChatId
static getChatId($dialogId, $userId=null)
Definition
dialog.php:91
Bitrix\Im\V2\Controller\BaseController
Definition
BaseController.php:24
Bitrix\Im\V2\Controller\BaseController\MAX_LIMIT
const MAX_LIMIT
Definition
BaseController.php:25
Bitrix\Im\V2\Controller\BaseController\getDefaultPreFilters
getDefaultPreFilters()
Definition
BaseController.php:57
Bitrix\Im\V2\Controller\BaseController\getAutoWiredParameters
getAutoWiredParameters()
Definition
BaseController.php:28
Bitrix\Im\V2\Controller\BaseController\toRestFormat
toRestFormat(RestConvertible ... $entities)
Definition
BaseController.php:78
Bitrix\Im\V2\Controller\BaseController\convertCharToBool
convertCharToBool(string $char, bool $default=false)
Definition
BaseController.php:132
Bitrix\Im\V2\Controller\BaseController\getMessageById
getMessageById(int $id)
Definition
BaseController.php:118
Bitrix\Im\V2\Controller\BaseController\getRawValue
getRawValue(string $key)
Definition
BaseController.php:146
Bitrix\Im\V2\Controller\BaseController\getLimit
getLimit(int $limit)
Definition
BaseController.php:73
Bitrix\Im\V2\Controller\BaseController\recursiveWhiteList
static recursiveWhiteList($fields, $whiteList, bool $sanitizeOnly=false)
Definition
BaseController.php:83
Bitrix\Im\V2\Controller\BaseController\checkWhiteList
checkWhiteList(array $fields, array $whiteList)
Definition
BaseController.php:103
Bitrix\Im\V2\Controller\BaseController\DEFAULT_LIMIT
const DEFAULT_LIMIT
Definition
BaseController.php:26
Bitrix\Im\V2\Controller\Filter\ActionUuidHandler
Definition
ActionUuidHandler.php:10
Bitrix\Im\V2\Controller\Filter\AuthorizationPrefilter
Definition
AuthorizationPrefilter.php:9
Bitrix\Im\V2\Controller\Filter\CheckChatAccess
Definition
CheckChatAccess.php:13
Bitrix\Im\V2\Controller\Filter\SameChatMessageFilter
Definition
SameChatMessageFilter.php:12
Bitrix\Im\V2\Controller\Filter\StartIdFilter
Definition
StartIdFilter.php:12
Bitrix\Im\V2\Message\MessageError
Definition
MessageError.php:9
Bitrix\Im\V2\Message
Definition
Message.php:44
Bitrix\Im\V2\Rest\RestAdapter
Definition
RestAdapter.php:6
Bitrix\Main\Application
Definition
application.php:28
Bitrix\Main\Application\getInstance
static getInstance()
Definition
application.php:95
Bitrix\Main\DB\Connection\$host
$host
Definition
connection.php:29
Bitrix\Main\Engine\AutoWire\ExactParameter
Definition
exactparameter.php:9
Bitrix\Main\Engine\Controller
Definition
controller.php:32
Bitrix\Main\Engine\Controller\addError
addError(Error $error)
Definition
controller.php:1051
Bitrix\Main\Engine\Response\Converter
Definition
converter.php:8
Bitrix\Main\Text\Encoding
Definition
encoding.php:8
Bitrix\Main\Type\Dictionary\toArray
toArray()
Definition
dictionary.php:197
Bitrix\Main\Type\ParameterDictionary
Definition
parameterdictionary.php:8
Bitrix\Main\Type\ParameterDictionary\toArrayRaw
toArrayRaw()
Definition
parameterdictionary.php:49
Bitrix\Im\V2\Rest\RestConvertible
Definition
RestConvertible.php:6
Bitrix\Im\V2\Chat
Definition
ChannelChat.php:3
Bitrix\Im\V2\Controller
Definition
BaseController.php:3
Bitrix\Im\V2\Message
Definition
AdditionalMessagePopupItem.php:3
Bitrix\Sender\Internals\QueryController
Definition
action.php:8
modules
im
lib
V2
Controller
BaseController.php
Создано системой
1.10.0