Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.0] Clamps timestamps to accepted range in SMF\Time methods and improves handling of the [time] BBCode #8385

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions Sources/Msg.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,23 @@ function ($m) {

// Let's look at the time tags...
$message = preg_replace_callback(
'~\[time(?:=(absolute))*\](.+?)\[/time\]~i',
function ($m) {
return '[time]' . (is_numeric("{$m[2]}") || @strtotime("{$m[2]}") == 0 ? "{$m[2]}" : strtotime("{$m[2]}") - ("{$m[1]}" == 'absolute' ? 0 : ((Config::$modSettings['time_offset'] + User::$me->time_offset) * 3600))) . '[/time]';
'~\[time(?:=([^\\]]*))?\](.+?)\[/time\]~i',
function ($matches) {
return preg_replace(
[
'~^<time[^>]*\bdatetime="([^"]+)"[^>]*>(.*)</time>$~',
'~^<span[^>]*>.*</span>$~',
],
[
// If it parsed successfully, insert the resolved datetime value.
// This ensures that "[time]today[/time]" ends up resolving to
// the date the post was written, not the date it is being read.
'[time=$1]$2[/time]',
// If it didn't parse successfully, remove the BBC entirely.
$matches[2],
],
Parser::transform($matches[0], Parser::INPUT_BBC),
);
},
$message,
);
Expand Down Expand Up @@ -943,10 +957,14 @@ function ($matches) {
}

// Attempt to un-parse the time to something less awful.
// This form will never be created by Msg::preparsecode() in SMF 3.0+
// but it might be present in old data.
$message = preg_replace_callback(
'~\[time\](\d{0,10})\[/time\]~i',
function ($matches) {
return '[time]' . Time::create('@' . $matches[1])->setTimezone(new \DateTimeZone(User::getTimezone()))->format(Time::getDateFormat()) . '[/time]';
$time = Time::create('@' . $matches[1]);

return '[time=' . $time->format('Y-m-d\TH:i:sP') . ']' . $time->format(null, false) . '[/time]';
},
$message,
);
Expand Down
Loading
Loading