Skip to content
Open
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
34 changes: 1 addition & 33 deletions Sources/IntegrationHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,39 +116,7 @@ public function execute(array $parameters = []): array

// Loop through each callable.
foreach ($this->callables as $func_string => $callable) {
// Is it valid?
if (\is_callable($callable)) {
$this->results[$func_string] = \call_user_func_array($callable, $parameters);
}
// This failed, but we want to do so silently.
elseif ($this->ignore_errors) {
// return $this->results;
continue;
}
// Whatever it was supposed to call, it failed :(
else {
// Get a full path to show on error.
if (str_contains($func_string, '|')) {
list($file, $func) = explode('|', $func_string);

$path = strtr($file, [
'$boarddir' => Config::$boarddir,
'$sourcedir' => Config::$sourcedir,
]);

if (str_contains($path, '$themedir') && class_exists('SMF\\Theme', false) && !empty(Theme::$current->settings['theme_dir'])) {
$path = strtr($path, [
'$themedir' => Theme::$current->settings['theme_dir'],
]);
}

ErrorHandler::log(Lang::getTxt('hook_fail_call_to', [$func, $path], file: 'Errors'), 'general');
}
// Assume the file resides on Config::$boarddir somewhere...
else {
ErrorHandler::log(Lang::getTxt('hook_fail_call_to', [$func_string, Config::$boarddir], file: 'Errors'), 'general');
}
}
$this->results[$func_string] = $callable(...$parameters);
}

return $this->results;
Expand Down
21 changes: 11 additions & 10 deletions Sources/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2419,9 +2419,6 @@ public static function getCallable(string|callable $input, ?bool $ignore_errors
return \is_callable($input) ? $input : false;
}

// Sanitize and trim the input.
$input = Utils::htmlspecialchars(Utils::htmlTrim($input));

// Attempt to load a file, if applicable.
$input = self::loadFile($input);

Expand All @@ -2431,20 +2428,24 @@ public static function getCallable(string|callable $input, ?bool $ignore_errors
}

// Process static or instance method callables.
if (str_contains($input, '::')) {
list($class, $method) = explode('::', $input);
$pos = strpos($input, '::');

if ($pos !== false) {
$class = substr($input, 0, $pos);
$method = substr($input, $pos + 2);

// Handle instance creation for methods with "#".
if (str_contains($method, '#')) {
if (!isset(Utils::$context['instances'])) {
Utils::$context['instances'] = [];
}

$instances = &Utils::$context['instances'];
// Remove the "#" and ensure an instance exists.
$method = str_replace('#', '', $method);

if (empty(Utils::$context['instances'][$class]) || !(Utils::$context['instances'][$class] instanceof $class)) {
Utils::$context['instances'][$class] = new $class();
if (empty($instances[$class]) || !($instances[$class] instanceof $class)) {
$instances[$class] = new $class();

// Optionally track instance creation for debugging.
if (DebugUtils::isDebugEnabled()) {
Expand All @@ -2456,14 +2457,14 @@ public static function getCallable(string|callable $input, ?bool $ignore_errors
}
}

$callable = [Utils::$context['instances'][$class], $method];
$callable = $instances[$class]->$method(...);
} else {
// Static method reference.
$callable = [$class, $method];
$callable = $class::$method(...);
}
} else {
// Treat as a plain function.
$callable = $input;
$callable = $input(...);
}

// Validate the callable.
Expand Down
Loading