Skip to content

Commit

Permalink
Sends Subs.php into the long night
Browse files Browse the repository at this point in the history
Like Load.php, Subs.php has been removed rather than turned into a backward compatibility stub file because every existing mod would assume that it had already been included and wouldn't try to include it themselves. The class_exists() calls in Subs-Compat.php are all we need to do now to maintain backward compatibility for those mods.

Signed-off-by: Jon Stovell <[email protected]>
  • Loading branch information
Sesquipedalian committed Nov 4, 2023
1 parent 97b06d4 commit 3cbc0f8
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 144 deletions.
133 changes: 91 additions & 42 deletions Sources/Subs-Compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,78 @@
if (!defined('SMF'))
die('No direct access...');

/*********************************************
* SMF\Config::$backward_compatibility support
*********************************************/

if (!empty(SMF\Config::$backward_compatibility))
{
/*
* In SMF 2.x, there was a file named Subs.php that was always loaded early in
* the startup process and that contained many utility functions. Everything
* else assumed that those functions were available. Subs.php went the way of
* the dodo in SMF 3.0 after all its functions were migrated elsewhere, but
* mods that rely on backward compatibilty support will still expect all those
* functions to be available. So if backward compatibilty support is enabled,
* we need to load a bunch of classes in order to make them available.
*/
class_exists('SMF\\Attachment');
class_exists('SMF\\BBCodeParser');
class_exists('SMF\\Logging');
class_exists('SMF\\PageIndex');
class_exists('SMF\\Theme');
class_exists('SMF\\Time');
class_exists('SMF\\TimeZone');
class_exists('SMF\\Topic');
class_exists('SMF\\Url');
class_exists('SMF\\User');
class_exists('SMF\\Graphics\\Image');
class_exists('SMF\\WebFetch\\WebFetchApi');
}

/***************************
* PHP version compatibility
***************************/

/*
* Prevent fatal errors under PHP 8 when a disabled internal function is called.
*
* Before PHP 8, calling a disabled internal function merely generated a
* warning that could be easily suppressed by the @ operator. But as of PHP 8
* a disabled internal function is treated like it is undefined, which means
* a fatal error will be thrown and execution will halt. SMF expects the old
* behaviour, so these no-op polyfills make sure that is what happens.
*/
if (version_compare(PHP_VERSION, '8.0.0', '>='))
{
// This is wrapped in a closure to keep the global namespace clean.
call_user_func(function()
{
/*
* This array contains function names that meet the following conditions:
*
* 1. SMF assumes they are defined, even if disabled. Note that prior to
* PHP 8, this was always true for internal functions.
*
* 2. Some hosts are known to disable them.
*
* 3. SMF can get by without them (as opposed to missing functions that
* really SHOULD cause execution to halt).
*/
$optional_funcs = array(
'set_time_limit',
);

foreach ($optional_funcs as $func)
{
if (!function_exists($func))
{
eval('function ' . $func . '() { trigger_error("' . $func . '() has been disabled", E_USER_WARNING); }');
}
}
});
}

if (!function_exists('smf_crc32'))
{
/**
Expand All @@ -48,6 +120,10 @@ function smf_crc32($number)
}
}

/*****************
* Polyfills, etc.
*****************/

if (!function_exists('mb_ord'))
{
/**
Expand Down Expand Up @@ -298,11 +374,13 @@ function mb_ord_chr_encoding($encoding = null)
return false;
}

/**
* IDNA_* constants used as flags for the idn_to_* functions.
*/
foreach (
array(
// This is wrapped in a closure to keep the global namespace clean.
call_user_func(function()
{
/**
* IDNA_* constants used as flags for the idn_to_* functions.
*/
$idna_constants = array(
'IDNA_DEFAULT' => 0,
'IDNA_ALLOW_UNASSIGNED' => 1,
'IDNA_USE_STD3_RULES' => 2,
Expand All @@ -312,13 +390,14 @@ function mb_ord_chr_encoding($encoding = null)
'IDNA_NONTRANSITIONAL_TO_UNICODE' => 32,
'INTL_IDNA_VARIANT_2003' => 0,
'INTL_IDNA_VARIANT_UTS46' => 1,
)
as $name => $value
)
{
if (!defined($name))
define($name, $value);
};
);

foreach ($idna_constants as $name => $value)
{
if (!defined($name))
define($name, $value);
}
});

if (!function_exists('idn_to_ascii'))
{
Expand Down Expand Up @@ -415,34 +494,4 @@ function hash_equals($known_string, $user_string)
}
}

/**
* Prevent fatal errors under PHP 8 when a disabled internal function is called.
*
* Before PHP 8, calling a disabled internal function merely generated a
* warning that could be easily suppressed by the @ operator. But as of PHP 8
* a disabled internal function is treated like it is undefined, which means
* a fatal error will be thrown and execution will halt. SMF expects the old
* behaviour, so these no-op polyfills make sure that is what happens.
*/
if (version_compare(PHP_VERSION, '8.0.0', '>='))
{
/*
* This array contains function names that meet the following conditions:
*
* 1. SMF assumes they are defined, even if disabled. Note that prior to
* PHP 8, this was always true for internal functions.
*
* 2. Some hosts are known to disable them.
*
* 3. SMF can get by without them (as opposed to missing functions that
* really SHOULD cause execution to halt).
*/
foreach (array('set_time_limit') as $func)
{
if (!function_exists($func))
eval('function ' . $func . '() { trigger_error("' . $func . '() has been disabled for security reasons", E_USER_WARNING); }');
}
unset($func);
}

?>
88 changes: 0 additions & 88 deletions Sources/Subs.php

This file was deleted.

1 change: 0 additions & 1 deletion Sources/tasks/CreatePost_Notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ class CreatePost_Notify extends BackgroundTask
*/
public function execute()
{
require_once(Config::$sourcedir . '/Subs.php');
Theme::loadEssential();

$msgOptions = &$this->_details['msgOptions'];
Expand Down
1 change: 0 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@
*/

require_once(SMF\Config::$sourcedir . '/Autoloader.php');
require_once(SMF\Config::$sourcedir . '/Subs.php');

// Ensure we don't trip over disabled internal functions
require_once(SMF\Config::$sourcedir . '/Subs-Compat.php');
Expand Down
8 changes: 2 additions & 6 deletions other/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -1504,8 +1504,6 @@ function AdminAccount()

$settingsDefs = Config::getSettingsDefs();

require_once(Config::$sourcedir . '/Subs.php');

// Reload $modSettings.
Config::reloadModSettings();

Expand Down Expand Up @@ -1560,9 +1558,9 @@ function AdminAccount()
$incontext['error'] = Lang::$txt['error_user_settings_no_password'];
return false;
}
if (!file_exists(Config::$sourcedir . '/Subs.php'))
if (!file_exists(Config::$sourcedir . '/Utils.php'))
{
$incontext['error'] = sprintf(Lang::$txt['error_sourcefile_missing'], 'Subs.php');
$incontext['error'] = sprintf(Lang::$txt['error_sourcefile_missing'], 'Utils.php');
return false;
}

Expand Down Expand Up @@ -1708,8 +1706,6 @@ function DeleteInstall()

chdir(Config::$boarddir);

require_once(Config::$sourcedir . '/Subs.php');

// Reload $modSettings.
Config::reloadModSettings();

Expand Down
1 change: 0 additions & 1 deletion other/update_unicode_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@

// 4. Get some more stuff we need.
require_once($sourcedir . '/Autoloader.php');
require_once($sourcedir . '/Subs.php');
SMF\Config::$boarddir = $boarddir;
SMF\Config::$sourcedir = $sourcedir;

Expand Down
5 changes: 0 additions & 5 deletions other/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,6 @@
Config::reloadModSettings();
}

// Include our helper functions.
require_once(Config::$sourcedir . '/Subs.php');

// Don't do security check if on Yabbse
if (!isset(Config::$modSettings['smfVersion']))
$disable_security = true;
Expand Down Expand Up @@ -715,7 +712,6 @@ function loadEssentialData()
@ini_set('session.save_handler', 'files');
@session_start();

require_once(Config::$sourcedir . '/Subs.php');
require_once(Config::$sourcedir . '/Subs-Compat.php');

@set_time_limit(600);
Expand Down Expand Up @@ -1858,7 +1854,6 @@ function DeleteUpgrade()
// Wipe this out...
$upcontext['user'] = array();

require_once(Config::$sourcedir . '/Subs.php');
Config::updateSettingsFile($changes);

// Clean any old cache files away.
Expand Down

0 comments on commit 3cbc0f8

Please sign in to comment.