Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
988270a
[3.0] Move to dedicated Hooks table
jdarwood007 Jul 25, 2025
cfb5a02
Was for another PR that got stuck in my files
jdarwood007 Jul 26, 2025
8b637a1
Some hooks didn't use the integrate_ prefix, add a workaround for the…
jdarwood007 Jul 30, 2025
eac51a6
include hooks would dump the file name into the function name. Move …
jdarwood007 Jul 30, 2025
97837e1
Reduces type juggling on class handling
jdarwood007 Jul 30, 2025
638ae63
Cleanup at the end will take care of this.
jdarwood007 Aug 8, 2025
0940a7e
Merge branch 'release-3.0' into 3.0/HooksTable
jdarwood007 Aug 24, 2025
85e25f0
Merge branch 'release-3.0' into 3.0/HooksTable
jdarwood007 Aug 26, 2025
4f59b49
Remove a call in the upgrader
jdarwood007 Aug 26, 2025
6ada2ea
Merge branch 'release-3.0' into 3.0/HooksTable
jdarwood007 Sep 20, 2025
5c507e0
Ensure we use the canonicalPath
jdarwood007 Sep 20, 2025
733a692
Merge branch 'release-3.0' into 3.0/HooksTable
jdarwood007 Oct 26, 2025
cf0e9a1
Typo
jdarwood007 Oct 26, 2025
dde597b
PHP CS
jdarwood007 Oct 26, 2025
b7bd6d2
Update Sources/PackageManager/PackageManager.php
jdarwood007 Oct 26, 2025
9ab5a9b
Merge branch 'release-3.0' into 3.0/HooksTable
jdarwood007 Oct 26, 2025
d236202
Merge branch 'release-3.0' into 3.0/HooksTable
jdarwood007 Nov 14, 2026
daea48c
package_id fixes
jdarwood007 Nov 14, 2026
5d20c9f
Merge branch 'release-3.0' into 3.0/HooksTable
jdarwood007 Nov 14, 2026
2ea6bbd
We can't handle errors if we don't have a database yet
jdarwood007 Nov 14, 2026
506019b
Forgot to call a file load
jdarwood007 Nov 14, 2026
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
4 changes: 2 additions & 2 deletions Languages/en_US/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -757,8 +757,8 @@
$txt['hooks_field_hook_name'] = 'Hook Name';
$txt['hooks_field_function_name'] = 'Function Name';
$txt['hooks_field_function_method'] = 'Function is a method and its class is instantiated';
$txt['hooks_field_function'] = 'Function: {real_function}';
$txt['hooks_field_included_file'] = 'Included file: {included_file}';
$txt['hooks_field_function'] = 'Function: {function}';
$txt['hooks_field_included_file'] = 'Included file: {file}';
$txt['hooks_field_file_name'] = 'File Name';
$txt['hooks_field_hook_exists'] = 'Status';
$txt['hooks_active'] = 'Exists';
Expand Down
197 changes: 115 additions & 82 deletions Sources/Actions/Admin/Maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -1816,23 +1816,16 @@ public function hooks(): void
{
$filter_url = '';
$current_filter = '';
$hooks = $this->getIntegrationHooks();
$defined_hooks = array_keys(array_filter(IntegrationHook::get(), fn($val) => \count($val) > 0));

$hooks_filters = [];

if (isset($_GET['filter'], $hooks[$_GET['filter']])) {
if (isset($_GET['filter'], $defined_hooks[$_GET['filter']])) {
$filter_url = ';filter=' . $_GET['filter'];
$current_filter = $_GET['filter'];
}
$filtered_hooks = array_filter(
$hooks,
function ($hook) use ($current_filter) {
return $current_filter == '' || $current_filter == $hook;
},
ARRAY_FILTER_USE_KEY,
);
ksort($hooks);

foreach ($hooks as $hook => $functions) {
foreach ($defined_hooks as $hook) {
$hooks_filters[] = '<option' . ($current_filter == $hook ? ' selected ' : '') . ' value="' . $hook . '">' . $hook . '</option>';
}

Expand Down Expand Up @@ -1863,21 +1856,16 @@ function ($hook) use ($current_filter) {
'base_href' => Config::$scripturl . '?action=admin;area=maintain;sa=hooks' . $filter_url . ';' . Utils::$context['session_var'] . '=' . Utils::$context['session_id'],
'default_sort_col' => 'hook_name',
'get_items' => [
'function' => __CLASS__ . '::getIntegrationHooksData',
'function' => __CLASS__ . '::list_getHooks',
'params' => [
$filtered_hooks,
strtr(Config::$boarddir, '\\', '/'),
strtr(Config::$sourcedir, '\\', '/'),
$current_filter,
],
],
'get_count' => [
'value' => array_reduce(
$filtered_hooks,
function ($accumulator, $functions) {
return $accumulator + \count($functions);
},
0,
),
'function' => __CLASS__ . '::list_getNumHooks',
'params' => [
$current_filter,
],
],
'no_items_label' => Lang::getTxt('hooks_no_hooks', file: 'Admin'),
'columns' => [
Expand All @@ -1893,39 +1881,39 @@ function ($accumulator, $functions) {
'reverse' => 'hook_name DESC',
],
],
'function_name' => [
'func' => [
'header' => [
'value' => Lang::getTxt('hooks_field_function_name', file: 'Admin'),
],
'data' => [
'function' => function ($data) {
// Show a nice icon to indicate this is an instance.
$instance = (!empty($data['instance']) ? '<span class="main_icons news" title="' . Lang::getTxt('hooks_field_function_method', file: 'Admin') . '"></span> ' : '');
$instance = ($data['is_object'] ? '<span class="main_icons news" title="' . Lang::getTxt('hooks_field_function_method', file: 'Admin') . '"></span> ' : '');

if (!empty($data['included_file']) && !empty($data['real_function'])) {
if (!empty($data['file']) && !empty($data['function'])) {
return $instance . Lang::getTxt('hooks_field_function', $data, file: 'Admin') . '<br>' . Lang::getTxt('hooks_field_included_file', $data, file: 'Admin');
}

return $instance . $data['real_function'];
return $instance . $data['function'];
},
'class' => 'word_break',
],
'sort' => [
'default' => 'function_name',
'reverse' => 'function_name DESC',
'default' => 'func',
'reverse' => 'func DESC',
],
],
'file_name' => [
'file' => [
'header' => [
'value' => Lang::getTxt('hooks_field_file_name', file: 'Admin'),
],
'data' => [
'db' => 'file_name',
'db' => 'file',
'class' => 'word_break',
],
'sort' => [
'default' => 'file_name',
'reverse' => 'file_name DESC',
'default' => 'file',
'reverse' => 'file DESC',
],
],
'status' => [
Expand All @@ -1936,15 +1924,15 @@ function ($accumulator, $functions) {
'data' => [
'function' => function ($data) use ($filter_url) {
// Cannot update temp hooks in any way, really. Just show the appropriate icon.
if ($data['status'] == 'temp') {
return '<span class="main_icons ' . ($data['hook_exists'] ? 'posts' : 'error') . '" title="' . $data['img_text'] . '"></span>';
if ($data['is_temp']) {
return '<span class="main_icons ' . ($data['exists'] ? 'posts' : 'error') . '" title="' . $data['img_text'] . '"></span>';
}

$change_status = ['before' => '', 'after' => ''];

// Can only enable/disable if it exists...
if ($data['hook_exists']) {
$change_status['before'] = '<a href="' . Config::$scripturl . '?action=admin;area=maintain;sa=hooks;do=' . ($data['enabled'] ? 'disable' : 'enable') . ';hook=' . $data['hook_name'] . ';function=' . urlencode($data['function_name']) . $filter_url . ';' . Utils::$context['admin-hook_token_var'] . '=' . Utils::$context['admin-hook_token'] . ';' . Utils::$context['session_var'] . '=' . Utils::$context['session_id'] . '" data-confirm="' . Lang::getTxt('quickmod_confirm', file: 'General') . '" class="you_sure">';
if ($data['exists']) {
$change_status['before'] = '<a href="' . Config::$scripturl . '?action=admin;area=maintain;sa=hooks;do=' . ($data['is_enabled'] ? 'disable' : 'enable') . ';id=' . $data['id_hook'] . $filter_url . ';' . Utils::$context['admin-hook_token_var'] . '=' . Utils::$context['admin-hook_token'] . ';' . Utils::$context['session_var'] . '=' . Utils::$context['session_id'] . '" data-confirm="' . Lang::getTxt('quickmod_confirm', file: 'General') . '" class="you_sure">';
$change_status['after'] = '</a>';
}

Expand All @@ -1953,8 +1941,8 @@ function ($accumulator, $functions) {
'class' => 'centertext',
],
'sort' => [
'default' => 'status',
'reverse' => 'status DESC',
'default' => 'is_enabled',
'reverse' => 'is_enabled DESC',
],
],
],
Expand Down Expand Up @@ -1991,7 +1979,7 @@ function ($accumulator, $functions) {
'data' => [
'function' => function ($data) use ($filter_url) {
// Note: Cannot remove temp hooks via the UI...
if (!$data['hook_exists'] && $data['status'] != 'temp') {
if (!$data['exists'] && $data['status'] != 'temp') {
return '
<a href="' . Config::$scripturl . '?action=admin;area=maintain;sa=hooks;do=remove;hook=' . $data['hook_name'] . ';function=' . urlencode($data['function_name']) . $filter_url . ';' . Utils::$context['admin-hook_token_var'] . '=' . Utils::$context['admin-hook_token'] . ';' . Utils::$context['session_var'] . '=' . Utils::$context['session_id'] . '" data-confirm="' . Lang::getTxt('quickmod_confirm', file: 'General') . '" class="you_sure">
<span class="main_icons delete" title="' . Lang::getTxt('hooks_button_remove', file: 'Admin') . '"></span>
Expand Down Expand Up @@ -2024,64 +2012,109 @@ function ($accumulator, $functions) {
* @param int $start The item to start with (for pagination purposes)
* @param int $per_page How many items to display on each page
* @param string $sort A string indicating how to sort things
* @param object|array $filtered_hooks
* @param string $filter hook name to filter by.
* @param string $normalized_boarddir
* @param string $normalized_sourcedir
* @return array An array of information about the integration hooks
*/
public static function getIntegrationHooksData($start, $per_page, $sort, $filtered_hooks, $normalized_boarddir, $normalized_sourcedir): array
public static function list_getHooks(int $start, int $per_page, string $sort, string $filter = ''): array
{
$function_list = $sort_array = $temp_data = [];
$files = self::getFileRecursive($normalized_sourcedir);

foreach ($files as $currentFile => $fileInfo) {
$function_list += self::getDefinedFunctionsInFile($currentFile);
}

$sort_types = [
'hook_name' => ['hook_name', SORT_ASC],
'hook_name DESC' => ['hook_name', SORT_DESC],
'function_name' => ['function_name', SORT_ASC],
'function_name DESC' => ['function_name', SORT_DESC],
'file_name' => ['file_name', SORT_ASC],
'file_name DESC' => ['file_name', SORT_DESC],
'status' => ['status', SORT_ASC],
'status DESC' => ['status', SORT_DESC],
];
$ret = [];

$request = Db::$db->query(
'SELECT id_hook, is_enabled, hook_name, func, file, class, is_object, package_id
FROM {db_prefix}hooks' . (!empty($filter) ? '
WHERE hook_name = {string:filter}' : '') . '
ORDER BY {raw:sort}
LIMIT {int:start}, {int:per_page}',
[
'filter' => $filter,
'sort' => $sort,
'start' => $start,
'per_page' => $per_page,
],
);

foreach ($filtered_hooks as $hook => $functions) {
foreach ($functions as $rawFunc) {
$hookParsedData = self::parseIntegrationHook($hook, $rawFunc);
foreach (Db::$db->fetch_all($request) as $row) {
$hooks[$row['hook_name']] ??= IntegrationHook::get($row['hook_name']);
$hook = array_find($hooks[$row['hook_name']], fn($val) => $val['id_hook'] == $row['id_hook']);

$ret[(int) $row['id_hook']] = [
'id_hook' => (int) $row['id_hook'],
'is_enabled' => $row['is_enabled'] === '1' ? true : false,
'hook_name' => trim($row['hook_name'] ?? ''),
'function' => trim($row['func'] ?? ''),
'file' => trim($row['file'] ?? ''),
'class' => trim($row['class'] ?? ''),
'is_object' => $row['is_object'] == '1' ? true : false,
'is_temp' => $hook !== null && $hook['is_temp'],
'package_id' => $row['package_id'] ?? null,
'exists' => $hook !== null,
'status' => $hook !== null ? ($hook['is_enabled'] ? 'allow' : 'moderate') : 'deny',
'img_text' => Lang::getTxt('hooks_' . ($hook !== null ? ($row['is_enabled'] === '1' ? 'active' : 'disabled') : 'missing'), file: 'Admin'),
];
}
Db::$db->free_result($request);

// Handle hooks pointing outside the sources directory.
$absPath_clean = rtrim($hookParsedData['absPath'], '!');
// load up any temp hooks.
foreach (IntegrationHook::get() as $name => $hooks) {
foreach ($hooks as $id => $row) {
if (empty($row['is_temp'])) {
continue;
}

if ($absPath_clean != '' && !isset($files[$absPath_clean]) && file_exists($absPath_clean)) {
$function_list += self::getDefinedFunctionsInFile($absPath_clean);
if (!empty($filter) && $row['hook_name'] !== $filter) {
continue;
}

$hook_exists = isset($function_list[$hookParsedData['call']]) || (str_ends_with($hook, '_include') && isset($files[$absPath_clean]));
$hook_temp = !empty(Utils::$context['integration_hooks_temporary'][$hook][$hookParsedData['rawData']]);
$temp = [
'hook_name' => $hook,
'function_name' => $hookParsedData['rawData'],
'real_function' => $hookParsedData['call'],
'included_file' => $hookParsedData['hookFile'],
'file_name' => strtr($hookParsedData['absPath'] ?: ($function_list[$hookParsedData['call']] ?? ''), [$normalized_boarddir => '.']),
'instance' => $hookParsedData['object'],
'hook_exists' => $hook_exists,
'status' => ($hook_temp ? 'temp' : ($hook_exists ? ($hookParsedData['enabled'] ? 'allow' : 'moderate') : 'deny')),
'img_text' => Lang::getTxt('hooks_' . ($hook_exists ? ($hook_temp ? 'temp' : ($hookParsedData['enabled'] ? 'active' : 'disabled')) : 'missing'), file: 'Admin'),
'enabled' => $hookParsedData['enabled'],
$ret[(int) $row['id_hook']] = [
'id_hook' => (int) $row['id_hook'],
'is_enabled' => $row['is_enabled'] === '1' ? true : false,
'hook_name' => trim($row['hook_name'] ?? ''),
'function' => trim($row['function'] ?? ''),
'file' => trim($row['file'] ?? ''),
'class' => trim($row['class'] ?? ''),
'is_object' => $row['is_object'] == '1' ? true : false,
'is_temp' => $row['is_temp'],
'package_id' => $row['package_id'] ?? null,
'exists' => $hook !== null,
'status' => 'temp',
'img_text' => Lang::getTxt('hooks_temp', file: 'Admin'),
];
$sort_array[] = $temp[$sort_types[$sort][0]];
$temp_data[] = $temp;

}
}

array_multisort($sort_array, $sort_types[$sort][1], $temp_data);
return $ret;
}

/**
* Return the number of hooks of the specified type recorded in the database.
* (the specified type being attachments or avatars).
*
* @param string $filter hook name to filter by.
* @return int The number of hooks
*/
public static function list_getNumHooks(string $filter = ''): int
{
$request = Db::$db->query(
'SELECT COUNT(*)
FROM {db_prefix}hooks' . (!empty($filter) ? '
WHERE hook_name = {string:filter}' : ''),
[
'filter' => $filter,
],
);

list($num_hooks) = Db::$db->fetch_row($request);
Db::$db->free_result($request);
$num_hooks = (int) $num_hooks;

if (!empty($filter)) {
return $num_hooks + \count(IntegrationHook::get($filter));
}

return \array_slice($temp_data, $start, $per_page, true);
return (int) $num_hooks + array_sum(array_map('count', IntegrationHook::get()));
}

/**
Expand Down
54 changes: 22 additions & 32 deletions Sources/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,6 @@ class Config
*/
public static string $scripturl;

/**
* @var \Composer\Autoload\ClassLoader
*
* Autoloader instance.
* This is used to support the integrate_autoload hook.
*/
public static \Composer\Autoload\ClassLoader $loader;

/****************************
* Internal static properties
****************************/
Expand Down Expand Up @@ -1151,18 +1143,8 @@ public static function reloadModSettings(): void
die('SMF file version (' . SMF_VERSION . ') does not match SMF database version (' . self::$modSettings['smfVersion'] . ').<br>Run the SMF upgrader to fix this.<br><a href="https://wiki.simplemachines.org/smf/Upgrading">More information</a>.');
}

// Any autoloader integrations to add?
if (isset(self::$modSettings['integrate_autoload'])) {
$class_map = [];

IntegrationHook::call('integrate_autoload', [&$class_map]);

foreach ($class_map as $prefix => $dirname) {
self::$loader->addPsr4($prefix, self::$sourcedir . '/' . $dirname);
}
}

// Ensure the cache_enable setting reflects reality.
// Load up any hooks.
IntegrationHook::load();
self::$modSettings['cache_enable'] = Cache\CacheApi::$enable;

// Used to force browsers to download fresh CSS and JavaScript when necessary
Expand Down Expand Up @@ -1242,22 +1224,24 @@ public static function reloadModSettings(): void
$integration_settings = Utils::jsonDecode(SMF_INTEGRATION_SETTINGS, true);

foreach ($integration_settings as $hook => $function) {
IntegrationHook::add($hook, $function, false);
if (\is_string($function)) {
IntegrationHook::add($hook, $function, false);
} else {
IntegrationHook::register(
name: $hook,
function: $function['function'],
file: $function['file'],
class: $function['class'],
is_object: !empty($function['is_object']),
is_enabled: true,
permanent: false,
);
}
}
}

// Any files to pre include?
if (!empty(self::$modSettings['integrate_pre_include'])) {
$pre_includes = explode(',', self::$modSettings['integrate_pre_include']);

foreach ($pre_includes as $include) {
$include = strtr(trim($include), ['$boarddir' => self::$boarddir, '$sourcedir' => self::$sourcedir]);

if (file_exists($include)) {
require_once Sapi::canonicalPath($include);
}
}
}
IntegrationHook::call('integrate_pre_include');

Utils::load();

Expand Down Expand Up @@ -1319,6 +1303,12 @@ public static function updateModSettings(array $change_array, bool $update = fal
// In some cases, this may be better and faster, but for large sets we don't want so many UPDATEs.
if ($update) {
foreach ($change_array as $variable => $value) {
// If this is a integration setting, we need to step in.
if (Config::$backward_compatibility && str_starts_with($variable, 'integrate_')) {
IntegrationHook::processUpdateModSettings($variable, $value);
continue;
}

Db\DatabaseApi::$db->query(
'UPDATE {db_prefix}settings
SET value = {' . (\is_bool($value) ? 'raw' : 'string') . ':value}
Expand Down
Loading
Loading