/
formatting.php
116 lines (87 loc) · 3.08 KB
/
formatting.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace dvzMentions\Formatting;
function getFormattedMessageFromPlaceholdersAndUsers(string $content, array $placeholders, array $users): string
{
if ($placeholders) {
foreach ($placeholders as $index => $fingerprint) {
$user = $users['byUserId'][ (int)$fingerprint['userId'] ] ?? $users['byUsername'][ mb_strtolower($fingerprint['username']) ] ?? null;
if ($user) {
$replacement = \dvzMentions\Formatting\getFormattedTag($user);
} else {
$replacement = $fingerprint['full'];
}
$content = str_replace('<DVZ_ME#' . $index . '>', $replacement, $content);
}
}
return $content;
}
function getMessageWithPlaceholders(string $message, array $matches, array &$placeholders = []): string
{
foreach ($matches as &$match) {
$fingerprint = [
'full' => $match['full'],
'username' => $match['username'],
'escapeCharacter' => $match['escapeCharacter'],
'userId' => $match['userId'],
];
$placeholderId = array_search($fingerprint, $placeholders);
if ($placeholderId === false) {
$placeholderId = count($placeholders);
$placeholders[] = $fingerprint;
}
$match['replacement'] = '<DVZ_ME#' . $placeholderId . '>';
}
$message = \dvzMentions\Formatting\replaceMatchesInMessage($message, $matches);
return $message;
}
function replaceMatchesInMessage(string $message, array $matches): string
{
$correction = 0;
foreach ($matches as $match) {
// offset, call character, correction
$start = $match['offset'] + $correction;
$length = strlen($match['full']);
$message = substr_replace($message, $match['replacement'], $start, $length);
$correction += strlen($match['replacement']) - $length;
}
return $message;
}
function getFormattedTag(array $user): string
{
global $mybb;
if (\dvzMentions\getSettingValue('keep_prefix')) {
$prefix = '@';
} else {
$prefix = null;
}
$usernameEscaped = \htmlspecialchars_uni($user['username']);
if (\dvzMentions\getSettingValue('apply_username_style') && isset($user['usergroup'], $user['displaygroup'])) {
$username = \format_name($usernameEscaped, $user['usergroup'], $user['displaygroup']);
} else {
$username = $usernameEscaped;
}
$attributes = [
'href="' . $mybb->settings['bburl'] . '/' . \get_profile_link($user['uid']) . '"',
'class="mycode_mention"',
];
if (\dvzMentions\getSettingValue('links_to_new_tabs')) {
$attributes[] = 'target="_blank"';
}
$attributesHtml = implode(' ', $attributes);
$html = $prefix . '<a ' . $attributesHtml . '>' . $username . '</a>';
return $html;
}
function getUserFieldList(): array
{
$fields = [
'uid',
'username',
];
if (\dvzMentions\getSettingValue('apply_username_style')) {
$fields = array_merge($fields, [
'usergroup',
'displaygroup',
]);
}
return $fields;
}