1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
UrlItem.php
См. документацию.
1
<?php
2
3
namespace
Bitrix\Im\V2\Entity\Url;
4
5
use Bitrix\Im\Common;
6
use Bitrix\Im\V2\Message;
7
use Bitrix\Im\V2\Rest\RestEntity;
8
use Bitrix\Main\UrlPreview\UrlMetadataTable;
9
use Bitrix\Main\UrlPreview\UrlPreview;
10
use Bitrix\Main\Web\Uri;
11
use CIMMessageParamAttach;
12
13
class
UrlItem
implements
RestEntity
14
{
15
protected
string
$url
=
''
;
16
protected
?
array
$metadata
= [];
17
protected
static
array
$staticMetadataCache
= [];
18
protected
?
RichData
$richData
;
19
protected
?\CIMMessageParamAttach
$urlAttach
=
null
;
20
21
public
static
function
getRestEntityName
(): string
22
{
23
return
'url'
;
24
}
25
26
public
function
__construct
(?
string
$url
=
null
,
bool
$withFetchMetadata =
true
)
27
{
28
if
(!empty(
$url
))
29
{
30
$this->
setUrl
($url);
31
if
($this->
getUrl
())
32
{
33
$metadata = static::$staticMetadataCache[$this->
getUrl
()] ??
null
;
34
if
($metadata ===
null
&& $withFetchMetadata)
35
{
36
try
37
{
38
$metadata
= UrlPreview::getMetadataByUrl($this->
getUrl
(),
true
,
false
);
39
}
40
catch
(\
Exception
$exception)
41
{
42
$metadata
=
false
;
43
}
44
static::$staticMetadataCache[$this->
getUrl
()] = $metadata;
45
}
46
47
if
(
$metadata
!==
false
&&
$metadata
!==
null
)
48
{
49
$this->
setMetadata
($metadata);
50
}
51
}
52
}
53
}
54
55
public
static
function
initByMetadata
(
array
$metadata
): self
56
{
57
return
(
new
static
())->setMetadata(
$metadata
)->setUrl(
$metadata
[
'URL'
]);
58
}
59
60
public
static
function
initByPreviewUrlId
(
int
$previewUrlId,
bool
$withHtml =
true
): ?self
61
{
62
if
($withHtml)
63
{
64
$metadata
= UrlPreview::getMetadataAndHtmlByIds([$previewUrlId]);
65
}
66
else
67
{
68
$metadata
= UrlPreview::getMetadataByIds([$previewUrlId]);
69
}
70
if
(
$metadata
===
false
|| !isset(
$metadata
[$previewUrlId]))
71
{
72
return
null
;
73
}
74
75
return
static::initByMetadata(
$metadata
[$previewUrlId]);
76
}
77
82
public
static
function
getUrlsFromText
(?
string
$text
):
array
83
{
84
if
(
$text
===
null
)
85
{
86
return
[];
87
}
88
89
$textParser = static::getTextParser();
90
$text
= $textParser->convertText(
$text
);
91
92
$text
= preg_replace(
'/-{54}.+?-{54}/su'
,
""
,
$text
);
93
$text
= preg_replace(
'/\[CODE](.*?)\[\/CODE]/siu'
,
""
,
$text
);
94
95
preg_replace_callback(
96
'/^(>>(.*)(\n)?)/miu'
,
97
static
fn() =>
" XXX"
,
98
$text
99
);
100
101
$result
= [];
102
preg_replace_callback(
103
'/\[url(=(?P<URL>[^\]]+))?](?P<TEXT>.*?)\[\/url]/iu'
,
104
static
function
(
array
$matches
) use (&
$result
) {
105
$link = !empty(
$matches
[
'URL'
])?
$matches
[
'URL'
]:
$matches
[
'TEXT'
];
106
if
(!empty($link))
107
{
108
$link = static::normalizeUrl($link);
109
if
(static::isUrlValid($link))
110
{
111
$result
[] = $link;
112
}
113
}
114
},
115
$text
116
);
117
118
return
$result
;
119
}
120
121
public
static
function
getFirstUrlFromText
(?
string
$text
): ?string
122
{
123
return
self::getUrlsFromText
(
$text
)[0] ??
null
;
124
}
125
126
public
static
function
getByMessage
(
Message
$message
): ?self
127
{
128
$firstUrl =
self::getFirstUrlFromText
(
$message
->getMessage());
129
130
if
($firstUrl ===
null
)
131
{
132
return
null
;
133
}
134
135
return
new
self
($firstUrl);
136
}
137
138
public
function
getId
(): ?int
139
{
140
if
(isset($this->richData))
141
{
142
return
$this->
getRichData
()->getId();
143
}
144
145
$metadata = $this->
getMetadata
();
146
147
return
$metadata[
'ID'
] ??
null
;
148
}
149
150
public
function
toRestFormat
(
array
$option
= []):
array
151
{
152
return
[
153
'source'
=> $this->
getUrl
(),
154
'richData'
=> $this->
getRichData
()->toRestFormat(),
155
];
156
}
157
158
protected
static
function
getTextParser
(): \
CTextParser
159
{
160
$textParser = new \CTextParser();
161
162
$textParser->anchorType =
'bbcode'
;
163
164
foreach
($textParser->allow as $tag => $value)
165
{
166
$textParser->allow[$tag] =
'N'
;
167
}
168
$textParser->allow[
'HTML'
] =
'Y'
;
169
$textParser->allow[
'ANCHOR'
] =
'Y'
;
170
$textParser->allow[
'TEXT_ANCHOR'
] =
'Y'
;
171
172
return
$textParser;
173
}
174
175
protected
static
function
normalizeUrl
(
string
$url
): string
176
{
177
$uri
=
new
Uri
(
$url
);
178
if
(
$uri
->getHost() ===
''
)
179
{
180
$uri
=
new
Uri
(
Common::getPublicDomain
().
$url
);
181
}
182
183
return
$uri
->getUri();
184
}
185
186
protected
static
function
isUrlValid
(
string
$url
): bool
187
{
188
return
!(
189
!($parsedUrl = \parse_url(
$url
))
190
|| empty($parsedUrl[
'host'
])
191
|| strpos($parsedUrl[
'host'
],
'.'
) ===
false
// domain without dots
192
|| preg_match(
"/[\s]+/"
, $parsedUrl[
'host'
])
// spaces in the host
193
|| (!empty($parsedUrl[
'port'
]) && !is_numeric($parsedUrl[
'port'
]))
// non digit port
194
);
195
}
196
197
//region Setters & getters
198
199
public
function
getUrl
(): string
200
{
201
return
$this->url
;
202
}
203
204
public
function
setUrl
(
string
$url
): self
205
{
206
$url
= static::normalizeUrl(
$url
);
207
if
(static::isUrlValid(
$url
))
208
{
209
$this->url =
$url
;
210
}
211
return
$this;
212
}
213
214
public
function
getMetadata
():
array
215
{
216
return
$this->metadata
;
217
}
218
219
public
function
setMetadata
(
array
$metadata
): self
220
{
221
$this->metadata =
$metadata
;
222
return
$this;
223
}
224
225
public
function
isStaticUrl
(): bool
226
{
227
$metadata
= $this->
getMetadata
();
228
229
return
!empty(
$metadata
) && (
$metadata
[
'TYPE'
] == UrlMetadataTable::TYPE_STATIC);
230
}
231
232
public
function
isDynamicUrl
(): bool
233
{
234
$metadata
= $this->
getMetadata
();
235
236
return
!empty(
$metadata
) && (
$metadata
[
'TYPE'
] == UrlMetadataTable::TYPE_DYNAMIC);
237
}
238
239
public
function
getRichData
():
RichData
240
{
241
if
(isset($this->richData))
242
{
243
return
$this->richData
;
244
}
245
246
$this->richData =
new
RichData
();
247
248
$metadata
= $this->
getMetadata
();
249
250
if
(empty(
$metadata
))
251
{
252
return
$this->richData
;
253
}
254
255
if
(
$metadata
[
'TYPE'
] === UrlMetadataTable::TYPE_STATIC)
256
{
257
$this->
setRichData
(
RichData::initByAttach
($this->
getUrlAttach
()));
258
}
259
elseif
($metadata[
'TYPE'
] === UrlMetadataTable::TYPE_DYNAMIC)
260
{
261
$richData
= UrlPreview::getImRich(
$metadata
[
'URL'
],
true
);
262
if
(
$richData
===
false
||
$richData
->getType() ===
null
)
263
{
264
$richData
= $this->richData->setType(
RichData::DYNAMIC_TYPE
);
265
}
266
$this->
setRichData
($richData);
267
}
268
269
return
$this->richData->setId(
$metadata
[
'ID'
]);
270
}
271
272
public
function
setRichData
(?
RichData
$richData
): self
273
{
274
$this->richData =
$richData
;
275
return
$this;
276
}
277
278
public
function
isRich
(): bool
279
{
280
return
!empty($this->metadata);
281
}
282
283
public
function
getUrlAttach
(): ?\CIMMessageParamAttach
284
{
285
if
($this->urlAttach ===
null
)
286
{
287
if
($this->
isRich
())
288
{
289
$this->urlAttach =
\CIMMessageLink::formatAttach
($this->
getMetadata
()) ?:
null
;
290
}
291
}
292
293
return
$this->urlAttach
;
294
}
295
296
public
function
setUrlAttach
(?CIMMessageParamAttach
$urlAttach
): self
297
{
298
$this->urlAttach =
$urlAttach
;
299
return
$this;
300
}
301
302
//endregion
303
}
Bitrix\Im\Common\getPublicDomain
static getPublicDomain()
Определения
common.php:8
Bitrix\Im\V2\Entity\Url\RichData
Определения
RichData.php:8
Bitrix\Im\V2\Entity\Url\RichData\initByAttach
static initByAttach(?\CIMMessageParamAttach $attach)
Определения
RichData.php:24
Bitrix\Im\V2\Entity\Url\RichData\DYNAMIC_TYPE
const DYNAMIC_TYPE
Определения
RichData.php:14
Bitrix\Im\V2\Entity\Url\UrlItem
Определения
UrlItem.php:14
Bitrix\Im\V2\Entity\Url\UrlItem\getMetadata
getMetadata()
Определения
UrlItem.php:214
Bitrix\Im\V2\Entity\Url\UrlItem\getRichData
getRichData()
Определения
UrlItem.php:239
Bitrix\Im\V2\Entity\Url\UrlItem\getRestEntityName
static getRestEntityName()
Определения
UrlItem.php:21
Bitrix\Im\V2\Entity\Url\UrlItem\setUrlAttach
setUrlAttach(?CIMMessageParamAttach $urlAttach)
Определения
UrlItem.php:296
Bitrix\Im\V2\Entity\Url\UrlItem\getId
getId()
Определения
UrlItem.php:138
Bitrix\Im\V2\Entity\Url\UrlItem\normalizeUrl
static normalizeUrl(string $url)
Определения
UrlItem.php:175
Bitrix\Im\V2\Entity\Url\UrlItem\toRestFormat
toRestFormat(array $option=[])
Определения
UrlItem.php:150
Bitrix\Im\V2\Entity\Url\UrlItem\initByPreviewUrlId
static initByPreviewUrlId(int $previewUrlId, bool $withHtml=true)
Определения
UrlItem.php:60
Bitrix\Im\V2\Entity\Url\UrlItem\setMetadata
setMetadata(array $metadata)
Определения
UrlItem.php:219
Bitrix\Im\V2\Entity\Url\UrlItem\$richData
RichData $richData
Определения
UrlItem.php:18
Bitrix\Im\V2\Entity\Url\UrlItem\__construct
__construct(?string $url=null, bool $withFetchMetadata=true)
Определения
UrlItem.php:26
Bitrix\Im\V2\Entity\Url\UrlItem\$metadata
array $metadata
Определения
UrlItem.php:16
Bitrix\Im\V2\Entity\Url\UrlItem\getTextParser
static getTextParser()
Определения
UrlItem.php:158
Bitrix\Im\V2\Entity\Url\UrlItem\initByMetadata
static initByMetadata(array $metadata)
Определения
UrlItem.php:55
Bitrix\Im\V2\Entity\Url\UrlItem\$staticMetadataCache
static array $staticMetadataCache
Определения
UrlItem.php:17
Bitrix\Im\V2\Entity\Url\UrlItem\isDynamicUrl
isDynamicUrl()
Определения
UrlItem.php:232
Bitrix\Im\V2\Entity\Url\UrlItem\setRichData
setRichData(?RichData $richData)
Определения
UrlItem.php:272
Bitrix\Im\V2\Entity\Url\UrlItem\getUrlAttach
getUrlAttach()
Определения
UrlItem.php:283
Bitrix\Im\V2\Entity\Url\UrlItem\getUrlsFromText
static getUrlsFromText(?string $text)
Определения
UrlItem.php:82
Bitrix\Im\V2\Entity\Url\UrlItem\isStaticUrl
isStaticUrl()
Определения
UrlItem.php:225
Bitrix\Im\V2\Entity\Url\UrlItem\$urlAttach
CIMMessageParamAttach $urlAttach
Определения
UrlItem.php:19
Bitrix\Im\V2\Entity\Url\UrlItem\$url
string $url
Определения
UrlItem.php:15
Bitrix\Im\V2\Entity\Url\UrlItem\getUrl
getUrl()
Определения
UrlItem.php:199
Bitrix\Im\V2\Entity\Url\UrlItem\getFirstUrlFromText
static getFirstUrlFromText(?string $text)
Определения
UrlItem.php:121
Bitrix\Im\V2\Entity\Url\UrlItem\setUrl
setUrl(string $url)
Определения
UrlItem.php:204
Bitrix\Im\V2\Entity\Url\UrlItem\isUrlValid
static isUrlValid(string $url)
Определения
UrlItem.php:186
Bitrix\Im\V2\Entity\Url\UrlItem\isRich
isRich()
Определения
UrlItem.php:278
Bitrix\Im\V2\Entity\Url\UrlItem\getByMessage
static getByMessage(Message $message)
Определения
UrlItem.php:126
Bitrix\Main\Web\Uri
Определения
uri.php:17
CIMMessageLink\formatAttach
static formatAttach($linkParam)
Определения
im_message_param.php:1555
CTextParser
Определения
textparser.php:21
array
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения
file_new.php:804
$result
$result
Определения
get_property_values.php:14
Bitrix\Im\V2\Rest\RestEntity
Определения
RestEntity.php:8
$uri
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/urlrewrite.php")) $uri
Определения
urlrewrite.php:61
Bitrix\Im\Message
Определения
Uuid.php:3
Bitrix\Main\DI\Exception
Определения
circulardependencyexception.php:3
$message
$message
Определения
payment.php:8
elseif
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения
prolog_main_admin.php:393
$text
$text
Определения
template_pdf.php:79
$option
$option
Определения
options.php:1711
$matches
$matches
Определения
index.php:22
bitrix
modules
im
lib
V2
Entity
Url
UrlItem.php
Создано системой
1.14.0