Skip to content
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
5 changes: 3 additions & 2 deletions Sources/Actions/Admin/ACP.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use SMF\Menu;
use SMF\Parser;
use SMF\Routable;
use SMF\Sapi;
use SMF\SecurityToken;
use SMF\Theme;
use SMF\Url;
Expand Down Expand Up @@ -780,7 +781,7 @@ public function execute(): void

// Now - finally - call the right place!
if (isset($menu->include_data['file'])) {
require_once Config::canonicalPath(Config::$sourcedir . '/' . $menu->include_data['file']);
require_once Sapi::canonicalPath(Config::$sourcedir . '/' . $menu->include_data['file']);
}

// Get the right callable.
Expand Down Expand Up @@ -1888,7 +1889,7 @@ protected function init()
]);

if (file_exists($include)) {
require_once Config::canonicalPath($include);
require_once Sapi::canonicalPath($include);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Actions/Admin/Find.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public function internal(): void
IntegrationHook::call('integrate_admin_search', [&$this->language_files, &$this->include_files, &$this->settings_search]);

foreach ($this->include_files as $file) {
require_once Config::canonicalPath(Config::$sourcedir . '/' . $file . '.php');
require_once Sapi::canonicalPath(Config::$sourcedir . '/' . $file . '.php');
}

/* This is the huge array that defines everything... it's a huge array of items formatted as follows:
Expand Down
3 changes: 2 additions & 1 deletion Sources/Actions/Admin/Languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use SMF\Menu;
use SMF\PackageManager\PackageUtils;
use SMF\PackageManager\XmlArray;
use SMF\Sapi;
use SMF\SecurityToken;
use SMF\Theme;
use SMF\User;
Expand Down Expand Up @@ -905,7 +906,7 @@ function ($val1, $val2) {
// Quickly load General language entries.
$old_txt = Lang::$txt;

require Config::canonicalPath($general_filename);
require Sapi::canonicalPath($general_filename);

Utils::$context['lang_file_not_writable_message'] = is_writable($general_filename) ? '' : Lang::getTxt('lang_file_not_writable', ['file' => $general_filename], file: 'ManageSettings');

Expand Down
3 changes: 2 additions & 1 deletion Sources/Actions/Admin/Logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use SMF\IntegrationHook;
use SMF\Lang;
use SMF\Menu;
use SMF\Sapi;
use SMF\Theme;
use SMF\User;
use SMF\Utils;
Expand Down Expand Up @@ -156,7 +157,7 @@ public function execute(): void
];

if (!empty(self::$subactions[$this->subaction][0])) {
require_once Config::canonicalPath(Config::$sourcedir . '/' . self::$subactions[$this->subaction][0]);
require_once Sapi::canonicalPath(Config::$sourcedir . '/' . self::$subactions[$this->subaction][0]);
}

$call = \is_string(self::$subactions[$this->subaction][1]) && method_exists($this, self::$subactions[$this->subaction][1]) ? [$this, self::$subactions[$this->subaction][1]] : Utils::getCallable(self::$subactions[$this->subaction][1]);
Expand Down
2 changes: 1 addition & 1 deletion Sources/Actions/Admin/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ public function export(): void
ACP::saveDBSettings($config_vars);

// Create the new directory, but revert to the previous one if anything goes wrong.
require_once Config::canonicalPath(Config::$sourcedir . '/Actions/Profile/Export.php');
require_once Sapi::canonicalPath(Config::$sourcedir . '/Actions/Profile/Export.php');
create_export_dir($prev_export_dir);

// Ensure we don't lose track of any existing export files.
Expand Down
3 changes: 2 additions & 1 deletion Sources/Actions/Admin/Subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use SMF\Lang;
use SMF\Menu;
use SMF\Parser;
use SMF\Sapi;
use SMF\SecurityToken;
use SMF\TaskRunner;
use SMF\Theme;
Expand Down Expand Up @@ -2099,7 +2100,7 @@ public static function loadPaymentGateways(): array
fclose($fp);

if (str_contains($header, '// SMF Payment Gateway: ' . strtolower($matches[1]))) {
require_once Config::canonicalPath(Config::$sourcedir . '/' . $file);
require_once Sapi::canonicalPath(Config::$sourcedir . '/' . $file);

$gateways[] = [
'filename' => $file,
Expand Down
3 changes: 2 additions & 1 deletion Sources/Actions/Moderation/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use SMF\Lang;
use SMF\Menu;
use SMF\Routable;
use SMF\Sapi;
use SMF\Theme;
use SMF\User;
use SMF\Utils;
Expand Down Expand Up @@ -240,7 +241,7 @@ public function execute(): void
$this->createMenu();

if (isset(Menu::$loaded['moderate']->include_data['file'])) {
require_once Config::canonicalPath(Config::$sourcedir . '/' . Menu::$loaded['moderate']->include_data['file']);
require_once Sapi::canonicalPath(Config::$sourcedir . '/' . Menu::$loaded['moderate']->include_data['file']);
}

$call = \is_string(Menu::$loaded['moderate']->include_data['function']) && method_exists($this, Menu::$loaded['moderate']->include_data['function']) ? [$this, Menu::$loaded['moderate']->include_data['function']] : Utils::getCallable(Menu::$loaded['moderate']->include_data['function']);
Expand Down
2 changes: 1 addition & 1 deletion Sources/Actions/Profile/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ public function execute(): void

// File to include?
if (!empty($menu->include_data['file'])) {
require_once Config::canonicalPath(Config::$sourcedir . '/' . $menu->include_data['file']);
require_once Sapi::canonicalPath(Config::$sourcedir . '/' . $menu->include_data['file']);
}

// Build the link tree.
Expand Down
3 changes: 2 additions & 1 deletion Sources/Actions/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use SMF\Parser;
use SMF\Profile;
use SMF\Routable;
use SMF\Sapi;
use SMF\SecurityToken;
use SMF\Theme;
use SMF\User;
Expand Down Expand Up @@ -281,7 +282,7 @@ public function show(): void

// Or any standard ones?
if (!empty(Config::$modSettings['registration_fields'])) {
require_once Config::canonicalPath(Config::$sourcedir . '/Profile-Modify.php');
require_once Sapi::canonicalPath(Config::$sourcedir . '/Profile-Modify.php');

// Setup some important context.
Theme::loadTemplate('Profile');
Expand Down
3 changes: 2 additions & 1 deletion Sources/Actions/VerificationCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use SMF\Cache\CacheApi;
use SMF\Config;
use SMF\Routable;
use SMF\Sapi;
use SMF\Theme;
use SMF\User;
use SMF\Utils;
Expand Down Expand Up @@ -683,7 +684,7 @@ protected function showLetterImage(string $letter): bool
// Include it!
header('content-type: image/png');

include Config::canonicalPath(Theme::$current->settings['default_theme_dir'] . '/fonts/' . $random_font . '/' . strtoupper($letter) . '.png');
include Sapi::canonicalPath(Theme::$current->settings['default_theme_dir'] . '/fonts/' . $random_font . '/' . strtoupper($letter) . '.png');

// Nothing more to come.
die();
Expand Down
3 changes: 2 additions & 1 deletion Sources/Actions/XmlHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use SMF\Parser;
use SMF\Profile;
use SMF\Routable;
use SMF\Sapi;
use SMF\Theme;
use SMF\User;
use SMF\Utils;
Expand Down Expand Up @@ -230,7 +231,7 @@ public function newsletterpreview(): void
*/
public function sig_preview(): void
{
require_once Config::canonicalPath(Config::$sourcedir . '/Profile-Modify.php');
require_once Sapi::canonicalPath(Config::$sourcedir . '/Profile-Modify.php');

$user = isset($_POST['user']) ? (int) $_POST['user'] : 0;
$is_owner = $user == User::$me->id;
Expand Down
3 changes: 2 additions & 1 deletion Sources/Cache/CacheApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use SMF\Config;
use SMF\Debug\DebugUtils;
use SMF\IntegrationHook;
use SMF\Sapi;
use SMF\Utils;

abstract class CacheApi
Expand Down Expand Up @@ -514,7 +515,7 @@ final public static function quickGet(string $key, string $file, string|array $f
)
) {
if (!empty($file) && is_file(Config::$sourcedir . '/' . $file)) {
require_once Config::canonicalPath(Config::$sourcedir . '/' . $file);
require_once Sapi::canonicalPath(Config::$sourcedir . '/' . $file);
}

$cache_block = \call_user_func_array($function, $params);
Expand Down
125 changes: 1 addition & 124 deletions Sources/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ public static function reloadModSettings(): void
$include = strtr(trim($include), ['$boarddir' => self::$boarddir, '$sourcedir' => self::$sourcedir]);

if (file_exists($include)) {
require_once self::canonicalPath($include);
require_once Sapi::canonicalPath($include);
}
}
}
Expand Down Expand Up @@ -2851,129 +2851,6 @@ public static function checkCron(): void
}
}

/**
* Normalizes directory separators and resolves '.' and '..' in a file path.
*
* The $path does not need to point to an existing file.
*
* If $path does point to an existing file, or if an ancestor directory of
* $path exists, then \realpath() will be used to resolve that part of the
* path, unless the $real parameter is set to false.
*
* @param string $path The file path.
* @param string|bool $base_dir Base directory for relative paths.
* - If a string, relative paths are prepended with the string and a
* directory separator. Note that directory separators in this string
* will be normalized just like in $path.
* - If true, relative paths are prepended with the current working
* directory and a directory separator.
* - If false, relative paths are processed as given.
* Default: false.
* @param bool $real Whether to get the real path for existing files. This
* can be set to false if the caller wants to canonicalize a hypothetical
* path without any possibility of the real file structure interfering
* with the result.
* Default: true.
* @return string The canonical file path.
*/
public static function canonicalPath(string $path, string|bool $base_dir = false, bool $real = true): string
{
// If $path points to a real file, this is all we need to do.
if (!empty($real) && ($realpath = @realpath($path)) !== false) {
return $realpath;
}

$base_dir = \is_string($base_dir) ? rtrim(str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $base_dir), DIRECTORY_SEPARATOR) : (!empty($base_dir) ? getcwd() : false);

$path = trim(str_replace(['\\', '/'], DIRECTORY_SEPARATOR, (string) $path));

// We need to know the path of the root directory.
if (DIRECTORY_SEPARATOR === '/') {
$root = '';
$is_absolute = str_starts_with($path, DIRECTORY_SEPARATOR);
} else {
// Windows network shares and devices.
if (str_starts_with($path, DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR)) {
if (\in_array(substr($path, 2, 2), ['?' . DIRECTORY_SEPARATOR, '.' . DIRECTORY_SEPARATOR])) {
$root = substr($path, 0, strpos($path, DIRECTORY_SEPARATOR, 3));
} else {
$root = '';

for ($i = 0; $i < 3; $i++) {
$root = substr($path, 0, strpos($path, DIRECTORY_SEPARATOR, \strlen($root) + 1));
}
}
}
// Windows absolute DOS-style path.
elseif (strpos($path, ':') !== false && strpos($path, DIRECTORY_SEPARATOR) === strpos($path, ':') + 1) {
$root = substr($path, 0, strpos($path, DIRECTORY_SEPARATOR));
}
// Windows relative path.
else {
$root = substr(getcwd(), 0, strcspn(getcwd(), DIRECTORY_SEPARATOR));

// If relative to current drive's root, make it absolute.
if (strpos($path, DIRECTORY_SEPARATOR) === 0) {
$path = $root . $path;
}
}

$is_absolute = str_starts_with($path, $root . DIRECTORY_SEPARATOR);
}

// Build canonical path.
$canonical_path = '';

if ($is_absolute) {
$path = substr($path, \strlen($root . DIRECTORY_SEPARATOR));
$path_parts = [$root];
} elseif (\is_string($base_dir)) {
$path_parts = explode(DIRECTORY_SEPARATOR, $base_dir);
} else {
$path_parts = [];
}

foreach (explode(DIRECTORY_SEPARATOR, $path) as $key => $part) {
if (empty($part) || $part === '.') {
continue;
}

if ($part === '..') {
if ($is_absolute && $path_parts === [$root]) {
continue;
}

if (empty($path_parts) || $path_parts[0] === '..') {
$path_parts[] = $part;
} else {
array_pop($path_parts);
}
} else {
$path_parts[] = $part;
}

$canonical_path = implode(DIRECTORY_SEPARATOR, $path_parts);

if (empty($real) || \in_array($canonical_path, ['', '.', '..'])) {
continue;
}

// Check for intermediate symlinks.
$realpath = @realpath($canonical_path);

if ($realpath !== false && $realpath !== $canonical_path) {
$path_parts = explode(DIRECTORY_SEPARATOR, $realpath);
}
}

// Ambiguity is bad.
if ($canonical_path === '') {
$canonical_path = $is_absolute ? $root . DIRECTORY_SEPARATOR : '.';
}

return $canonical_path;
}

/*************************
* Internal static methods
*************************/
Expand Down
2 changes: 1 addition & 1 deletion Sources/Forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ protected static function findAction(?string $action): string|callable|false

// Otherwise, it was set - so let's go to that action.
if (!empty(self::$actions[$action][0])) {
require_once Config::canonicalPath(Config::$sourcedir . '/' . self::$actions[$action][0]);
require_once Sapi::canonicalPath(Config::$sourcedir . '/' . self::$actions[$action][0]);
}

// Do the right thing.
Expand Down
3 changes: 2 additions & 1 deletion Sources/Graphics/Gif/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace SMF\Graphics\Gif;

use SMF\Config;
use SMF\Sapi;

class File
{
Expand Down Expand Up @@ -184,5 +185,5 @@ public function get_png_data(string $background_color): string|bool

// 64-bit only functions?
if (!\function_exists('smf_crc32')) {
require_once Config::canonicalPath(Config::$sourcedir . '/Subs-Compat.php');
require_once Sapi::canonicalPath(Config::$sourcedir . '/Subs-Compat.php');
}
4 changes: 2 additions & 2 deletions Sources/ItemList.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ protected function setStartAndItemsPerPage(): void
$this->total_num_items = $this->options['get_count']['value'];
} else {
if (isset($this->options['get_count']['file'])) {
require_once Config::canonicalPath($this->options['get_count']['file']);
require_once Sapi::canonicalPath($this->options['get_count']['file']);
}

$call = Utils::getCallable($this->options['get_count']['function']);
Expand Down Expand Up @@ -483,7 +483,7 @@ protected function getItems(): void
} else {
// Get the file with the function for the item list.
if (isset($this->options['get_items']['file'])) {
require_once Config::canonicalPath($this->options['get_items']['file']);
require_once Sapi::canonicalPath($this->options['get_items']['file']);
}

$call = Utils::getCallable($this->options['get_items']['function']);
Expand Down
Loading