Skip to content

Commit

Permalink
Merge pull request #8387 from Sesquipedalian/3.0/sid
Browse files Browse the repository at this point in the history
Does a better job dealing with deprecation of SID constant
  • Loading branch information
Sesquipedalian authored Dec 31, 2024
2 parents 5a0150e + d279eed commit a76ca3e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
17 changes: 10 additions & 7 deletions Sources/QueryString.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,17 @@ public static function isFilteredRequest(array $value_list, string $var): bool
*/
public static function ob_sessrewrite(string $buffer): string
{
// If Config::$scripturl is set to nothing, or the session ID is not defined (SSI?) just quit.
if (Config::$scripturl == '' || session_id() === false) {
// PHP 8.4 deprecated SID. A better long-term solution is needed, but this works for now.
$sid = defined('SID') ? @constant('SID') : null;

// If Config::$scripturl is set to nothing, or the SID is not defined (SSI?) just quit.
if (Config::$scripturl == '' || !isset($sid)) {
return $buffer;
}

// Do nothing if the session is cookied, or they are a crawler - guests are caught by redirectexit().
if (empty($_COOKIE) && session_id() != '' && !BrowserDetector::isBrowser('possibly_robot')) {
$buffer = preg_replace('/(?<!<link rel="canonical" href=)"' . preg_quote(Config::$scripturl, '/') . '(?!\?' . preg_quote(session_id(), '/') . ')\??/', '"' . Config::$scripturl . '?' . session_id() . '&amp;', $buffer);
if (empty($_COOKIE) && $sid != '' && !BrowserDetector::isBrowser('possibly_robot')) {
$buffer = preg_replace('/(?<!<link rel="canonical" href=)"' . preg_quote(Config::$scripturl, '/') . '(?!\?' . preg_quote($sid, '/') . ')\??/', '"' . Config::$scripturl . '?' . $sid . '&amp;', $buffer);
}
// Debugging templates, are we?
elseif (isset($_GET['debug'])) {
Expand All @@ -441,11 +444,11 @@ public static function ob_sessrewrite(string $buffer): string
)
) {
// Let's do something special for session ids!
if (session_id() != '') {
if ($sid != '') {
$buffer = preg_replace_callback(
'~"' . preg_quote(Config::$scripturl, '~') . '\?(?:' . session_id() . '(?:;|&|&amp;))((?:board|topic)=[^#"]+?)(#[^"]*?)?"~',
'~"' . preg_quote(Config::$scripturl, '~') . '\?(?:' . $sid . '(?:;|&|&amp;))((?:board|topic)=[^#"]+?)(#[^"]*?)?"~',
function ($m) {
return '"' . Config::$scripturl . '/' . strtr("{$m[1]}", '&;=', '//,') . '.html?' . session_id() . ($m[2] ?? '') . '"';
return '"' . Config::$scripturl . '/' . strtr("{$m[1]}", '&;=', '//,') . '.html?' . $sid . ($m[2] ?? '') . '"';
},
$buffer,
);
Expand Down
13 changes: 8 additions & 5 deletions Sources/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2236,9 +2236,12 @@ public static function redirectexit(string $setLocation = '', bool $refresh = fa
$setLocation = Config::$scripturl . ($setLocation != '' ? '?' . $setLocation : '');
}

// PHP 8.4 deprecated SID. A better long-term solution is needed, but this works for now.
$sid = defined('SID') ? @constant('SID') : null;

// Put the session ID in.
if (session_id() != '') {
$setLocation = preg_replace('/^' . preg_quote(Config::$scripturl, '/') . '(?!\?' . preg_quote(session_id(), '/') . ')\??/', Config::$scripturl . '?' . session_id() . ';', $setLocation);
if (isset($sid) && $sid != '') {
$setLocation = preg_replace('/^' . preg_quote(Config::$scripturl, '/') . '(?!\?' . preg_quote($sid, '/') . ')\??/', Config::$scripturl . '?' . $sid . ';', $setLocation);
}
// Keep that debug in their for template debugging!
elseif (isset($_GET['debug'])) {
Expand All @@ -2256,11 +2259,11 @@ public static function redirectexit(string $setLocation = '', bool $refresh = fa
Sapi::isSoftware([Sapi::SERVER_APACHE, Sapi::SERVER_LIGHTTPD, Sapi::SERVER_LITESPEED])
)
) {
if (session_id() != '') {
if (isset($sid) && $sid != '') {
$setLocation = preg_replace_callback(
'~^' . preg_quote(Config::$scripturl, '~') . '\?(?:' . session_id() . '(?:;|&|&amp;))((?:board|topic)=[^#]+?)(#[^"]*?)?$~',
'~^' . preg_quote(Config::$scripturl, '~') . '\?(?:' . $sid . '(?:;|&|&amp;))((?:board|topic)=[^#]+?)(#[^"]*?)?$~',
function ($m) {
return Config::$scripturl . '/' . strtr("{$m[1]}", '&;=', '//,') . '.html?' . session_id() . (isset($m[2]) ? "{$m[2]}" : '');
return Config::$scripturl . '/' . strtr("{$m[1]}", '&;=', '//,') . '.html?' . $sid . (isset($m[2]) ? "{$m[2]}" : '');
},
$setLocation,
);
Expand Down

0 comments on commit a76ca3e

Please sign in to comment.