Skip to content

Commit

Permalink
fix: mentions incorrectly parsed if preceded by forward slash
Browse files Browse the repository at this point in the history
  • Loading branch information
samerton committed Dec 10, 2024
1 parent fbcb279 commit 5051d3e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
6 changes: 3 additions & 3 deletions core/classes/Misc/MentionsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MentionsParser
*/
public static function parse(int $author_id, string $value, string $link = null, array $alert_short = null, array $alert_full = null): string
{
if (preg_match_all('/@([A-Za-z0-9\-_!.]+)/', $value, $matches)) {
if (preg_match_all('/(?<!\/)@([A-Za-z0-9\-_!.]+)/', $value, $matches)) {
$matches = $matches[1];

foreach ($matches as $possible_username) {
Expand All @@ -33,10 +33,10 @@ public static function parse(int $author_id, string $value, string $link = null,
$user = new User($possible_username, 'nickname');

if ($user->exists()) {
$value = preg_replace('/' . preg_quote("@$possible_username", '/') . '/', '[user]' . $user->data()->id . '[/user]', $value);
$value = preg_replace('/(?<!\/)' . preg_quote("@$possible_username", '/') . '/', '[user]' . $user->data()->id . '[/user]', $value);

// Check if user is blocked by OP
if ($link && ($alert_full && $alert_short) && ($user->data()->id != $author_id) && !$user->isBlocked($user->data()->id, $author_id)) {
if ($value && $link && ($alert_full && $alert_short) && ($user->data()->id != $author_id) && !$user->isBlocked($user->data()->id, $author_id)) {
Alert::create($user->data()->id, 'tag', $alert_short, $alert_full, $link);
break;
}
Expand Down
3 changes: 3 additions & 0 deletions core/classes/Misc/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ public static function embedSafe(?string $content): string

$content = html_entity_decode($content);

// Also strip any mentions
$content = MentionsHook::stripPost(['content' => $content])['content'];

return self::truncate($content, 512, [
'html' => true,
]);
Expand Down
38 changes: 38 additions & 0 deletions modules/Core/hooks/MentionsHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,44 @@ static function (array $match) {
return $params;
}

/**
* Strips [user] tags and parses the ID to username
* e.g. [user]1[/user] would instead become (at)Username
*
* @param array $params
* @return array
*/
public static function stripPost(array $params = []): array {
if (parent::validateParams($params, ['content'])) {
$params['content'] = preg_replace_callback(
'/\[user\](.*?)\[\/user\]/ism',
static function (array $match) {
if (isset(MentionsHook::$_cache[$match[1]])) {
$userNickname = MentionsHook::$_cache[$match[1]][2];
} else {
$user = new User($match[1]);

if (!$user->exists()) {
return '@' . (new Language('core', LANGUAGE))->get('general', 'deleted_user');
}

$userId = $user->data()->id;
$userStyle = $user->getGroupStyle();
$userNickname = $user->data()->nickname;
$userProfileUrl = $user->getProfileURL();

MentionsHook::$_cache[$match[1]] = [$userId, $userStyle, $userNickname, $userProfileUrl];
}

return '@' . Output::getClean($userNickname);
},
$params['content']
);
}

return $params;
}

private static function validate(array $params): bool {
return parent::validateParams($params, ['content', 'user']);
}
Expand Down

0 comments on commit 5051d3e

Please sign in to comment.