Skip to content

Commit 9406ac9

Browse files
Improves handling of the [time] BBCode
Signed-off-by: Jon Stovell <[email protected]>
1 parent eacae8a commit 9406ac9

File tree

2 files changed

+369
-8
lines changed

2 files changed

+369
-8
lines changed

Sources/Msg.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -758,9 +758,23 @@ function ($m) {
758758

759759
// Let's look at the time tags...
760760
$message = preg_replace_callback(
761-
'~\[time(?:=(absolute))*\](.+?)\[/time\]~i',
762-
function ($m) {
763-
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]';
761+
'~\[time(?:=([^\\]]*))?\](.+?)\[/time\]~i',
762+
function ($matches) {
763+
return preg_replace(
764+
[
765+
'~^<time[^>]*\bdatetime="([^"]+)"[^>]*>(.*)</time>$~',
766+
'~^<span[^>]*>.*</span>$~',
767+
],
768+
[
769+
// If it parsed successfully, insert the resolved datetime value.
770+
// This ensures that "[time]today[/time]" ends up resolving to
771+
// the date the post was written, not the date it is being read.
772+
'[time=$1]$2[/time]',
773+
// If it didn't parse successfully, remove the BBC entirely.
774+
$matches[2],
775+
],
776+
Parser::transform($matches[0], Parser::INPUT_BBC),
777+
);
764778
},
765779
$message,
766780
);
@@ -943,10 +957,14 @@ function ($matches) {
943957
}
944958

945959
// Attempt to un-parse the time to something less awful.
960+
// This form will never be created by Msg::preparsecode() in SMF 3.0+
961+
// but it might be present in old data.
946962
$message = preg_replace_callback(
947963
'~\[time\](\d{0,10})\[/time\]~i',
948964
function ($matches) {
949-
return '[time]' . Time::create('@' . $matches[1])->setTimezone(new \DateTimeZone(User::getTimezone()))->format(Time::getDateFormat()) . '[/time]';
965+
$time = Time::create('@' . $matches[1]);
966+
967+
return '[time=' . $time->format('Y-m-d\TH:i:sP') . ']' . $time->format(null, false) . '[/time]';
950968
},
951969
$message,
952970
);

0 commit comments

Comments
 (0)