1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-06 07:35:29 +02:00

[ticket/13777] Move module management into service

PHPBB3-13777
This commit is contained in:
MateBartus 2015-05-31 13:19:50 +02:00
parent c0972f74ca
commit 71d03647f0
7 changed files with 727 additions and 530 deletions

View File

@ -11,6 +11,7 @@ imports:
- { resource: services_help.yml }
- { resource: services_language.yml }
- { resource: services_mimetype_guesser.yml }
- { resource: services_module.yml }
- { resource: services_notification.yml }
- { resource: services_password.yml }
- { resource: services_profilefield.yml }

View File

@ -0,0 +1,10 @@
services:
module.manager:
class: phpbb\module\module_manager
arguments:
- @cache.driver
- @dbal.conn
- @ext.manager
- %tables.modules%
- %core.root_path%
- %core.php_ext%

View File

@ -19,6 +19,8 @@ if (!defined('IN_PHPBB'))
exit;
}
use phpbb\module\exception\module_exception;
/**
* - Able to check for new module versions (modes changed/adjusted/added/removed)
* Icons for:
@ -37,8 +39,10 @@ class acp_modules
function main($id, $mode)
{
global $db, $user, $auth, $template, $module, $request, $phpbb_log;
global $config, $phpbb_admin_path, $phpbb_root_path, $phpEx;
global $db, $user, $template, $module, $request, $phpbb_log, $phpbb_container;
/** @var \phpbb\module\module_manager $module_manager */
$module_manager = $phpbb_container->get('module.manager');
// Set a global define for modules we might include (the author is able to prevent execution of code by checking this constant)
define('MODULE_INCLUDE', true);
@ -91,13 +95,20 @@ class acp_modules
$db->sql_freeresult($result);
}
$errors = $this->delete_module($module_id);
if (!sizeof($errors))
try
{
$this->remove_cache_file();
trigger_error($user->lang['MODULE_DELETED'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
$row = $module_manager->get_module_row($module_id, $this->module_class);
$module_manager->delete_module($module_id, $this->module_class);
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_MODULE_REMOVED', false, array($user->lang($row['module_langname'])));
}
catch (module_exception $e)
{
$msg = $user->lang($e->getMessage());
trigger_error($msg . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
}
$module_manager->remove_cache_file($this->module_class);
trigger_error($user->lang['MODULE_DELETED'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
}
else
{
@ -138,8 +149,8 @@ class acp_modules
AND module_id = $module_id";
$db->sql_query($sql);
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_MODULE_' . strtoupper($action), false, array($this->lang_name($row['module_langname'])));
$this->remove_cache_file();
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_MODULE_' . strtoupper($action), false, array($user->lang($row['module_langname'])));
$module_manager->remove_cache_file($this->module_class);
break;
@ -163,12 +174,16 @@ class acp_modules
trigger_error($user->lang['NO_MODULE'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
}
$move_module_name = $this->move_module_by($row, $action, 1);
if ($move_module_name !== false)
try
{
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_MODULE_' . strtoupper($action), false, array($this->lang_name($row['module_langname']), $move_module_name));
$this->remove_cache_file();
$move_module_name = $module_manager->move_module_by($row, $this->module_class, $action, 1);
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_MODULE_' . strtoupper($action), false, array($user->lang($row['module_langname']), $move_module_name));
$module_manager->remove_cache_file($this->module_class);
}
catch (module_exception $e)
{
// Do nothing
}
if ($request->is_ajax())
@ -194,7 +209,7 @@ class acp_modules
list($module_basename, $module_mode) = explode('::', $quick_install);
// Check if module name and mode exist...
$fileinfo = $this->get_module_infos($module_basename);
$fileinfo = $module_manager->get_module_infos($module_basename, $this->module_class);
$fileinfo = $fileinfo[$module_basename];
if (isset($fileinfo['modes'][$module_mode]))
@ -210,11 +225,20 @@ class acp_modules
'module_auth' => $fileinfo['modes'][$module_mode]['auth'],
);
$errors = $this->update_module_data($module_data);
try
{
$module_manager->update_module_data($module_data);
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_MODULE_ADD', false, array($user->lang($module_data['module_langname'])));
}
catch (\phpbb\module\exception\module_exception $e)
{
$msg = $user->lang($e->getMessage());
trigger_error($msg . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
}
if (!sizeof($errors))
{
$this->remove_cache_file();
$module_manager->remove_cache_file($this->module_class);
trigger_error($user->lang['MODULE_ADDED'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
}
@ -240,7 +264,15 @@ class acp_modules
trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
}
$module_row = $this->get_module_row($module_id);
try
{
$module_row = $module_manager->get_module_row($module_id, $this->module_class);
}
catch (\phpbb\module\exception\module_not_found_exception $e)
{
$msg = $user->lang($e->getMessage());
trigger_error($msg . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
}
// no break
@ -294,15 +326,29 @@ class acp_modules
// Adjust auth row
if ($module_data['module_basename'] && $module_data['module_mode'])
{
$fileinfo = $this->get_module_infos($module_data['module_basename']);
$fileinfo = $module_manager->get_module_infos($module_data['module_basename'], $this->module_class);
$module_data['module_auth'] = $fileinfo[$module_data['module_basename']]['modes'][$module_data['module_mode']]['auth'];
}
$errors = $this->update_module_data($module_data);
try
{
$module_manager->update_module_data($module_data);
$phpbb_log->add('admin',
$user->data['user_id'],
$user->ip,
($action === 'edit') ? 'LOG_MODULE_EDIT' : 'LOG_MODULE_ADD',
false,
array($user->lang($module_data['module_langname']))
); }
catch (\phpbb\module\exception\module_exception $e)
{
$msg = $user->lang($e->getMessage());
trigger_error($msg . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
}
if (!sizeof($errors))
{
$this->remove_cache_file();
$module_manager->remove_cache_file($this->module_class);
trigger_error((($action == 'add') ? $user->lang['MODULE_ADDED'] : $user->lang['MODULE_EDITED']) . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
}
@ -312,7 +358,7 @@ class acp_modules
$is_cat = (!$module_data['module_basename']) ? true : false;
// Get module information
$module_infos = $this->get_module_infos();
$module_infos = $module_manager->get_module_infos('', $this->module_class);
// Build name options
$s_name_options = $s_mode_options = '';
@ -324,7 +370,7 @@ class acp_modules
}
// Name options
$s_name_options .= '<option value="' . $option . '"' . (($option == $module_data['module_basename']) ? ' selected="selected"' : '') . '>' . $this->lang_name($values['title']) . ' [' . $option . ']</option>';
$s_name_options .= '<option value="' . $option . '"' . (($option == $module_data['module_basename']) ? ' selected="selected"' : '') . '>' . $user->lang($values['title']) . ' [' . $option . ']</option>';
$template->assign_block_vars('m_names', array('NAME' => $option, 'A_NAME' => addslashes($option)));
@ -333,14 +379,14 @@ class acp_modules
{
if ($option == $module_data['module_basename'])
{
$s_mode_options .= '<option value="' . $m_mode . '"' . (($m_mode == $module_data['module_mode']) ? ' selected="selected"' : '') . '>' . $this->lang_name($m_values['title']) . '</option>';
$s_mode_options .= '<option value="' . $m_mode . '"' . (($m_mode == $module_data['module_mode']) ? ' selected="selected"' : '') . '>' . $user->lang($m_values['title']) . '</option>';
}
$template->assign_block_vars('m_names.modes', array(
'OPTION' => $m_mode,
'VALUE' => $this->lang_name($m_values['title']),
'VALUE' => $user->lang($m_values['title']),
'A_OPTION' => addslashes($m_mode),
'A_VALUE' => addslashes($this->lang_name($m_values['title'])))
'A_VALUE' => addslashes($user->lang($m_values['title'])))
);
}
}
@ -358,7 +404,7 @@ class acp_modules
'L_TITLE' => $user->lang[strtoupper($action) . '_MODULE'],
'MODULENAME' => $this->lang_name($module_data['module_langname']),
'MODULENAME' => $user->lang($module_data['module_langname']),
'ACTION' => $action,
'MODULE_ID' => $module_id,
@ -406,11 +452,11 @@ class acp_modules
{
$navigation = '<a href="' . $this->u_action . '">' . strtoupper($this->module_class) . '</a>';
$modules_nav = $this->get_module_branch($this->parent_id, 'parents', 'descending');
$modules_nav = $module_manager->get_module_branch($this->parent_id, $this->module_class, 'parents');
foreach ($modules_nav as $row)
{
$langname = $this->lang_name($row['module_langname']);
$langname = $user->lang($row['module_langname']);
if ($row['module_id'] == $this->parent_id)
{
@ -437,7 +483,7 @@ class acp_modules
{
do
{
$langname = $this->lang_name($row['module_langname']);
$langname = $user->lang($row['module_langname']);
if (!$row['module_enabled'])
{
@ -472,7 +518,15 @@ class acp_modules
}
else if ($this->parent_id)
{
$row = $this->get_module_row($this->parent_id);
try
{
$row = $module_manager->get_module_row($this->parent_id, $this->module_class);
}
catch (\phpbb\module\exception\module_not_found_exception $e)
{
$msg = $user->lang($e->getMessage());
trigger_error($msg . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
}
$url = $this->u_action . '&amp;parent_id=' . $this->parent_id . '&amp;m=' . $row['module_id'];
@ -491,19 +545,19 @@ class acp_modules
$db->sql_freeresult($result);
// Quick adding module
$module_infos = $this->get_module_infos();
$module_infos = $module_manager->get_module_infos('', $this->module_class);
// Build quick options
$s_install_options = '';
foreach ($module_infos as $option => $values)
{
// Name options
$s_install_options .= '<optgroup label="' . $this->lang_name($values['title']) . ' [' . $option . ']">';
$s_install_options .= '<optgroup label="' . $user->lang($values['title']) . ' [' . $option . ']">';
// Build module modes
foreach ($values['modes'] as $m_mode => $m_values)
{
$s_install_options .= '<option value="' . $option . '::' . $m_mode . '">&nbsp; &nbsp;' . $this->lang_name($m_values['title']) . '</option>';
$s_install_options .= '<option value="' . $option . '::' . $m_mode . '">&nbsp; &nbsp;' . $user->lang($m_values['title']) . '</option>';
}
$s_install_options .= '</optgroup>';
@ -520,104 +574,6 @@ class acp_modules
);
}
/**
* Get row for specified module
*/
function get_module_row($module_id)
{
global $db, $user;
$sql = 'SELECT *
FROM ' . MODULES_TABLE . "
WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
AND module_id = $module_id";
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
if (!$row)
{
trigger_error($user->lang['NO_MODULE'] . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
}
return $row;
}
/**
* Get available module information from module files
*
* @param string $module
* @param bool|string $module_class
* @param bool $use_all_available Use all available instead of just all
* enabled extensions
* @return array
*/
function get_module_infos($module = '', $module_class = false, $use_all_available = false)
{
global $phpbb_extension_manager, $phpbb_root_path, $phpEx;
$module_class = ($module_class === false) ? $this->module_class : $module_class;
$directory = $phpbb_root_path . 'includes/' . $module_class . '/info/';
$fileinfo = array();
$finder = $phpbb_extension_manager->get_finder($use_all_available);
$modules = $finder
->extension_suffix('_module')
->extension_directory("/$module_class")
->core_path("includes/$module_class/info/")
->core_prefix($module_class . '_')
->get_classes(true);
foreach ($modules as $cur_module)
{
// Skip entries we do not need if we know the module we are
// looking for
if ($module && strpos(str_replace('\\', '_', $cur_module), $module) === false && $module !== $cur_module)
{
continue;
}
$info_class = preg_replace('/_module$/', '_info', $cur_module);
// If the class does not exist it might be following the old
// format. phpbb_acp_info_acp_foo needs to be turned into
// acp_foo_info and the respective file has to be included
// manually because it does not support auto loading
$old_info_class_file = str_replace("phpbb_{$module_class}_info_", '', $cur_module);
$old_info_class = $old_info_class_file . '_info';
if (class_exists($old_info_class))
{
$info_class = $old_info_class;
}
else if (!class_exists($info_class))
{
$info_class = $old_info_class;
// need to check class exists again because previous checks triggered autoloading
if (!class_exists($info_class) && file_exists($directory . $old_info_class_file . '.' . $phpEx))
{
include($directory . $old_info_class_file . '.' . $phpEx);
}
}
if (class_exists($info_class))
{
$info = new $info_class();
$module_info = $info->module();
$main_class = (isset($module_info['filename'])) ? $module_info['filename'] : $cur_module;
$fileinfo[$main_class] = $module_info;
}
}
ksort($fileinfo);
return $fileinfo;
}
/**
* Simple version of jumpbox, just lists modules
*/
@ -678,7 +634,7 @@ class acp_modules
$selected = (is_array($select_id)) ? ((in_array($row['module_id'], $select_id)) ? ' selected="selected"' : '') : (($row['module_id'] == $select_id) ? ' selected="selected"' : '');
$langname = $this->lang_name($row['module_langname']);
$langname = $user->lang($row['module_langname']);
$module_list .= '<option value="' . $row['module_id'] . '"' . $selected . ((!$row['module_enabled']) ? ' class="disabled"' : '') . '>' . $padding . $langname . '</option>';
$iteration++;
@ -689,401 +645,4 @@ class acp_modules
return $module_list;
}
/**
* Get module branch
*/
function get_module_branch($module_id, $type = 'all', $order = 'descending', $include_module = true)
{
global $db;
switch ($type)
{
case 'parents':
$condition = 'm1.left_id BETWEEN m2.left_id AND m2.right_id';
break;
case 'children':
$condition = 'm2.left_id BETWEEN m1.left_id AND m1.right_id';
break;
default:
$condition = 'm2.left_id BETWEEN m1.left_id AND m1.right_id OR m1.left_id BETWEEN m2.left_id AND m2.right_id';
break;
}
$rows = array();
$sql = 'SELECT m2.*
FROM ' . MODULES_TABLE . ' m1
LEFT JOIN ' . MODULES_TABLE . " m2 ON ($condition)
WHERE m1.module_class = '" . $db->sql_escape($this->module_class) . "'
AND m2.module_class = '" . $db->sql_escape($this->module_class) . "'
AND m1.module_id = $module_id
ORDER BY m2.left_id " . (($order == 'descending') ? 'ASC' : 'DESC');
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
if (!$include_module && $row['module_id'] == $module_id)
{
continue;
}
$rows[] = $row;
}
$db->sql_freeresult($result);
return $rows;
}
/**
* Remove modules cache file
*/
function remove_cache_file()
{
global $phpbb_container;
// Sanitise for future path use, it's escaped as appropriate for queries
$p_class = str_replace(array('.', '/', '\\'), '', basename($this->module_class));
$phpbb_container->get('cache.driver')->destroy('_modules_' . $p_class);
// Additionally remove sql cache
$phpbb_container->get('cache.driver')->destroy('sql', MODULES_TABLE);
}
/**
* Return correct language name
*/
function lang_name($module_langname)
{
global $user;
return (!empty($user->lang[$module_langname])) ? $user->lang[$module_langname] : $module_langname;
}
/**
* Update/Add module
*
* @param array &$module_data The module data
* @param bool $run_inline if set to true errors will be returned and no logs being written
*/
function update_module_data(&$module_data, $run_inline = false)
{
global $db, $user, $phpbb_log;
if (!isset($module_data['module_id']))
{
// no module_id means we're creating a new category/module
if ($module_data['parent_id'])
{
$sql = 'SELECT left_id, right_id
FROM ' . MODULES_TABLE . "
WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
AND module_id = " . (int) $module_data['parent_id'];
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
if (!$row)
{
if ($run_inline)
{
return 'PARENT_NO_EXIST';
}
trigger_error($user->lang['PARENT_NO_EXIST'] . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
}
// Workaround
$row['left_id'] = (int) $row['left_id'];
$row['right_id'] = (int) $row['right_id'];
$sql = 'UPDATE ' . MODULES_TABLE . "
SET left_id = left_id + 2, right_id = right_id + 2
WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
AND left_id > {$row['right_id']}";
$db->sql_query($sql);
$sql = 'UPDATE ' . MODULES_TABLE . "
SET right_id = right_id + 2
WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
AND {$row['left_id']} BETWEEN left_id AND right_id";
$db->sql_query($sql);
$module_data['left_id'] = (int) $row['right_id'];
$module_data['right_id'] = (int) $row['right_id'] + 1;
}
else
{
$sql = 'SELECT MAX(right_id) AS right_id
FROM ' . MODULES_TABLE . "
WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'";
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$module_data['left_id'] = (int) $row['right_id'] + 1;
$module_data['right_id'] = (int) $row['right_id'] + 2;
}
$sql = 'INSERT INTO ' . MODULES_TABLE . ' ' . $db->sql_build_array('INSERT', $module_data);
$db->sql_query($sql);
$module_data['module_id'] = $db->sql_nextid();
if (!$run_inline)
{
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_MODULE_ADD', false, array($this->lang_name($module_data['module_langname'])));
}
}
else
{
$row = $this->get_module_row($module_data['module_id']);
if ($module_data['module_basename'] && !$row['module_basename'])
{
// we're turning a category into a module
$branch = $this->get_module_branch($module_data['module_id'], 'children', 'descending', false);
if (sizeof($branch))
{
return array($user->lang['NO_CATEGORY_TO_MODULE']);
}
}
if ($row['parent_id'] != $module_data['parent_id'])
{
$this->move_module($module_data['module_id'], $module_data['parent_id']);
}
$update_ary = $module_data;
unset($update_ary['module_id']);
$sql = 'UPDATE ' . MODULES_TABLE . '
SET ' . $db->sql_build_array('UPDATE', $update_ary) . "
WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
AND module_id = " . (int) $module_data['module_id'];
$db->sql_query($sql);
if (!$run_inline)
{
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_MODULE_EDIT', false, array($this->lang_name($module_data['module_langname'])));
}
}
return array();
}
/**
* Move module around the tree
*/
function move_module($from_module_id, $to_parent_id)
{
global $db;
$moved_modules = $this->get_module_branch($from_module_id, 'children', 'descending');
$from_data = $moved_modules[0];
$diff = sizeof($moved_modules) * 2;
$moved_ids = array();
for ($i = 0; $i < sizeof($moved_modules); ++$i)
{
$moved_ids[] = $moved_modules[$i]['module_id'];
}
// Resync parents
$sql = 'UPDATE ' . MODULES_TABLE . "
SET right_id = right_id - $diff
WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
AND left_id < " . (int) $from_data['right_id'] . '
AND right_id > ' . (int) $from_data['right_id'];
$db->sql_query($sql);
// Resync righthand side of tree
$sql = 'UPDATE ' . MODULES_TABLE . "
SET left_id = left_id - $diff, right_id = right_id - $diff
WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
AND left_id > " . (int) $from_data['right_id'];
$db->sql_query($sql);
if ($to_parent_id > 0)
{
$to_data = $this->get_module_row($to_parent_id);
// Resync new parents
$sql = 'UPDATE ' . MODULES_TABLE . "
SET right_id = right_id + $diff
WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
AND " . (int) $to_data['right_id'] . ' BETWEEN left_id AND right_id
AND ' . $db->sql_in_set('module_id', $moved_ids, true);
$db->sql_query($sql);
// Resync the righthand side of the tree
$sql = 'UPDATE ' . MODULES_TABLE . "
SET left_id = left_id + $diff, right_id = right_id + $diff
WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
AND left_id > " . (int) $to_data['right_id'] . '
AND ' . $db->sql_in_set('module_id', $moved_ids, true);
$db->sql_query($sql);
// Resync moved branch
$to_data['right_id'] += $diff;
if ($to_data['right_id'] > $from_data['right_id'])
{
$diff = '+ ' . ($to_data['right_id'] - $from_data['right_id'] - 1);
}
else
{
$diff = '- ' . abs($to_data['right_id'] - $from_data['right_id'] - 1);
}
}
else
{
$sql = 'SELECT MAX(right_id) AS right_id
FROM ' . MODULES_TABLE . "
WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
AND " . $db->sql_in_set('module_id', $moved_ids, true);
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$diff = '+ ' . (int) ($row['right_id'] - $from_data['left_id'] + 1);
}
$sql = 'UPDATE ' . MODULES_TABLE . "
SET left_id = left_id $diff, right_id = right_id $diff
WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
AND " . $db->sql_in_set('module_id', $moved_ids);
$db->sql_query($sql);
}
/**
* Remove module from tree
*/
function delete_module($module_id)
{
global $db, $user, $phpbb_log;
$row = $this->get_module_row($module_id);
$branch = $this->get_module_branch($module_id, 'children', 'descending', false);
if (sizeof($branch))
{
return array($user->lang['CANNOT_REMOVE_MODULE']);
}
// If not move
$diff = 2;
$sql = 'DELETE FROM ' . MODULES_TABLE . "
WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
AND module_id = $module_id";
$db->sql_query($sql);
$row['right_id'] = (int) $row['right_id'];
$row['left_id'] = (int) $row['left_id'];
// Resync tree
$sql = 'UPDATE ' . MODULES_TABLE . "
SET right_id = right_id - $diff
WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
AND left_id < {$row['right_id']} AND right_id > {$row['right_id']}";
$db->sql_query($sql);
$sql = 'UPDATE ' . MODULES_TABLE . "
SET left_id = left_id - $diff, right_id = right_id - $diff
WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
AND left_id > {$row['right_id']}";
$db->sql_query($sql);
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_MODULE_REMOVED', false, array($this->lang_name($row['module_langname'])));
return array();
}
/**
* Move module position by $steps up/down
*/
function move_module_by($module_row, $action = 'move_up', $steps = 1)
{
global $db;
/**
* Fetch all the siblings between the module's current spot
* and where we want to move it to. If there are less than $steps
* siblings between the current spot and the target then the
* module will move as far as possible
*/
$sql = 'SELECT module_id, left_id, right_id, module_langname
FROM ' . MODULES_TABLE . "
WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
AND parent_id = " . (int) $module_row['parent_id'] . '
AND ' . (($action == 'move_up') ? 'right_id < ' . (int) $module_row['right_id'] . ' ORDER BY right_id DESC' : 'left_id > ' . (int) $module_row['left_id'] . ' ORDER BY left_id ASC');
$result = $db->sql_query_limit($sql, $steps);
$target = array();
while ($row = $db->sql_fetchrow($result))
{
$target = $row;
}
$db->sql_freeresult($result);
if (!sizeof($target))
{
// The module is already on top or bottom
return false;
}
/**
* $left_id and $right_id define the scope of the nodes that are affected by the move.
* $diff_up and $diff_down are the values to substract or add to each node's left_id
* and right_id in order to move them up or down.
* $move_up_left and $move_up_right define the scope of the nodes that are moving
* up. Other nodes in the scope of ($left_id, $right_id) are considered to move down.
*/
if ($action == 'move_up')
{
$left_id = (int) $target['left_id'];
$right_id = (int) $module_row['right_id'];
$diff_up = (int) ($module_row['left_id'] - $target['left_id']);
$diff_down = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);
$move_up_left = (int) $module_row['left_id'];
$move_up_right = (int) $module_row['right_id'];
}
else
{
$left_id = (int) $module_row['left_id'];
$right_id = (int) $target['right_id'];
$diff_up = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);
$diff_down = (int) ($target['right_id'] - $module_row['right_id']);
$move_up_left = (int) ($module_row['right_id'] + 1);
$move_up_right = (int) $target['right_id'];
}
// Now do the dirty job
$sql = 'UPDATE ' . MODULES_TABLE . "
SET left_id = left_id + CASE
WHEN left_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
ELSE {$diff_down}
END,
right_id = right_id + CASE
WHEN right_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
ELSE {$diff_down}
END
WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
AND left_id BETWEEN {$left_id} AND {$right_id}
AND right_id BETWEEN {$left_id} AND {$right_id}";
$db->sql_query($sql);
$this->remove_cache_file();
return $this->lang_name($target['module_langname']);
}
}

View File

@ -0,0 +1,19 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\module\exception;
class module_class_not_defined_exception extends module_exception
{
}

View File

@ -0,0 +1,19 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\module\exception;
class module_exception extends \phpbb\exception\runtime_exception
{
}

View File

@ -0,0 +1,19 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\module\exception;
class module_not_found_exception extends module_exception
{
}

View File

@ -0,0 +1,570 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\module;
use phpbb\module\exception\module_class_not_defined_exception;
use phpbb\module\exception\module_exception;
use phpbb\module\exception\module_not_found_exception;
class module_manager
{
/**
* @var \phpbb\cache\driver\driver_interface
*/
protected $cache;
/**
* @var \phpbb\db\driver\driver_interface
*/
protected $db;
/**
* @var \phpbb\extension\manager
*/
protected $extension_manager;
/**
* @var string
*/
protected $modules_table;
/**
* @var string
*/
protected $phpbb_root_path;
/**
* @var string
*/
protected $php_ext;
/**
* Constructor
*
* @param \phpbb\cache\driver\driver_interface $cache Cache driver
* @param \phpbb\db\driver\driver_interface $db Database driver
* @param \phpbb\extension\manager $ext_manager Extension manager
* @param string $modules_table Module database table's name
* @param string $phpbb_root_path Path to phpBB's root
* @param string $php_ext Extension of PHP files
*/
public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\extension\manager $ext_manager, $modules_table, $phpbb_root_path, $php_ext)
{
$this->cache = $cache;
$this->db = $db;
$this->extension_manager = $ext_manager;
$this->modules_table = $modules_table;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
}
/**
* Get row for specified module
*
* @param int $module_id ID of the module
* @param string $module_class Class of the module (acp, ucp, mcp etc...)
*
* @return array Array of data fetched from the database
*
* @throws \phpbb\module\exception\module_not_found_exception When there is no module with $module_id
*/
public function get_module_row($module_id, $module_class)
{
$module_id = (int) $module_id;
$sql = 'SELECT *
FROM ' . $this->modules_table . "
WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
AND module_id = $module_id";
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if (!$row)
{
throw new module_not_found_exception('NO_MODULE');
}
return $row;
}
/**
* Get available module information from module files
*
* @param string $module ID of module
* @param string $module_class Class of the module (acp, ucp, mcp etc...)
* @param bool $use_all_available Use all available instead of just all
* enabled extensions
*
* @return array Array with module information gathered from module info files.
*/
public function get_module_infos($module = '', $module_class = null, $use_all_available = false)
{
if ($module_class === null)
{
throw new module_class_not_defined_exception();
}
$directory = $this->phpbb_root_path . 'includes/' . $module_class . '/info/';
$fileinfo = array();
$finder = $this->extension_manager->get_finder($use_all_available);
$modules = $finder
->extension_suffix('_module')
->extension_directory("/$module_class")
->core_path("includes/$module_class/info/")
->core_prefix($module_class . '_')
->get_classes(true);
foreach ($modules as $cur_module)
{
// Skip entries we do not need if we know the module we are
// looking for
if ($module && strpos(str_replace('\\', '_', $cur_module), $module) === false && $module !== $cur_module)
{
continue;
}
$info_class = preg_replace('/_module$/', '_info', $cur_module);
// If the class does not exist it might be following the old
// format. phpbb_acp_info_acp_foo needs to be turned into
// acp_foo_info and the respective file has to be included
// manually because it does not support auto loading
$old_info_class_file = str_replace("phpbb_{$module_class}_info_", '', $cur_module);
$old_info_class = $old_info_class_file . '_info';
if (class_exists($old_info_class))
{
$info_class = $old_info_class;
}
else if (!class_exists($info_class))
{
$info_class = $old_info_class;
// need to check class exists again because previous checks triggered autoloading
if (!class_exists($info_class) && file_exists($directory . $old_info_class_file . '.' . $this->php_ext))
{
include($directory . $old_info_class_file . '.' . $this->php_ext);
}
}
if (class_exists($info_class))
{
$info = new $info_class();
$module_info = $info->module();
$main_class = (isset($module_info['filename'])) ? $module_info['filename'] : $cur_module;
$fileinfo[$main_class] = $module_info;
}
}
ksort($fileinfo);
return $fileinfo;
}
/**
* Get module branch
*
* @param int $module_id ID of the module
* @param string $module_class Class of the module (acp, ucp, mcp etc...)
* @param string $type Type of branch (Expected values: all, parents or children)
* @param bool $include_module Whether or not to include the specified module with $module_id
*
* @return array Returns an array containing the modules in the specified branch type.
*/
public function get_module_branch($module_id, $module_class, $type = 'all', $include_module = true)
{
$module_id = (int) $module_id;
switch ($type)
{
case 'parents':
$condition = 'm1.left_id BETWEEN m2.left_id AND m2.right_id';
break;
case 'children':
$condition = 'm2.left_id BETWEEN m1.left_id AND m1.right_id';
break;
default:
$condition = 'm2.left_id BETWEEN m1.left_id AND m1.right_id OR m1.left_id BETWEEN m2.left_id AND m2.right_id';
break;
}
$rows = array();
$sql = 'SELECT m2.*
FROM ' . $this->modules_table . ' m1
LEFT JOIN ' . $this->modules_table . " m2 ON ($condition)
WHERE m1.module_class = '" . $this->db->sql_escape($module_class) . "'
AND m2.module_class = '" . $this->db->sql_escape($module_class) . "'
AND m1.module_id = $module_id
ORDER BY m2.left_id DESC";
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
if (!$include_module && $row['module_id'] == $module_id)
{
continue;
}
$rows[] = $row;
}
$this->db->sql_freeresult($result);
return $rows;
}
/**
* Remove modules cache file
*
* @param string $module_class Class of the module (acp, ucp, mcp etc...)
*/
public function remove_cache_file($module_class)
{
// Sanitise for future path use, it's escaped as appropriate for queries
$cache_class = str_replace(array('.', '/', '\\'), '', basename($module_class));
$this->cache->destroy('_modules_' . $cache_class);
$this->cache->destroy('sql', $this->modules_table);
}
/**
* Update/Add module
*
* @param array &$module_data The module data
*
* @throws \phpbb\module\exception\module_not_found_exception When parent module or the category is not exist
*/
public function update_module_data(&$module_data)
{
if (!isset($module_data['module_id']))
{
// no module_id means we're creating a new category/module
if ($module_data['parent_id'])
{
$sql = 'SELECT left_id, right_id
FROM ' . $this->modules_table . "
WHERE module_class = '" . $this->db->sql_escape($module_data['module_class']) . "'
AND module_id = " . (int) $module_data['parent_id'];
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if (!$row)
{
throw new module_not_found_exception('PARENT_NOT_EXIST');
}
// Workaround
$row['left_id'] = (int) $row['left_id'];
$row['right_id'] = (int) $row['right_id'];
$sql = 'UPDATE ' . $this->modules_table . "
SET left_id = left_id + 2, right_id = right_id + 2
WHERE module_class = '" . $this->db->sql_escape($module_data['module_class']) . "'
AND left_id > {$row['right_id']}";
$this->db->sql_query($sql);
$sql = 'UPDATE ' . $this->modules_table . "
SET right_id = right_id + 2
WHERE module_class = '" . $this->db->sql_escape($module_data['module_class']) . "'
AND {$row['left_id']} BETWEEN left_id AND right_id";
$this->db->sql_query($sql);
$module_data['left_id'] = (int) $row['right_id'];
$module_data['right_id'] = (int) $row['right_id'] + 1;
}
else
{
$sql = 'SELECT MAX(right_id) AS right_id
FROM ' . $this->modules_table . "
WHERE module_class = '" . $this->db->sql_escape($module_data['module_class']) . "'";
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
$module_data['left_id'] = (int) $row['right_id'] + 1;
$module_data['right_id'] = (int) $row['right_id'] + 2;
}
$sql = 'INSERT INTO ' . $this->modules_table . ' ' . $this->db->sql_build_array('INSERT', $module_data);
$this->db->sql_query($sql);
$module_data['module_id'] = $this->db->sql_nextid();
}
else
{
$row = $this->get_module_row($module_data['module_id'], $module_data['module_class']);
if ($module_data['module_basename'] && !$row['module_basename'])
{
// we're turning a category into a module
$branch = $this->get_module_branch($module_data['module_id'], $module_data['module_class'], 'children', false);
if (sizeof($branch))
{
throw new module_not_found_exception('NO_CATEGORY_TO_MODULE');
}
}
if ($row['parent_id'] != $module_data['parent_id'])
{
$this->move_module($module_data['module_id'], $module_data['parent_id'], $module_data['module_class']);
}
$update_ary = $module_data;
unset($update_ary['module_id']);
$sql = 'UPDATE ' . $this->modules_table . '
SET ' . $this->db->sql_build_array('UPDATE', $update_ary) . "
WHERE module_class = '" . $this->db->sql_escape($module_data['module_class']) . "'
AND module_id = " . (int) $module_data['module_id'];
$this->db->sql_query($sql);
}
}
/**
* Move module around the tree
*
* @param int $from_module_id ID of the current parent module
* @param int $to_parent_id ID of the target parent module
* @param string $module_class Class of the module (acp, ucp, mcp etc...)
*
* @throws \phpbb\module\exception\module_not_found_exception If the module specified to move modules from does not
* have any children.
*/
public function move_module($from_module_id, $to_parent_id, $module_class)
{
$moved_modules = $this->get_module_branch($from_module_id, $module_class, 'children');
if (empty($moved_modules))
{
throw new module_not_found_exception();
}
$from_data = $moved_modules[0];
$diff = sizeof($moved_modules) * 2;
$moved_ids = array();
for ($i = 0; $i < sizeof($moved_modules); ++$i)
{
$moved_ids[] = $moved_modules[$i]['module_id'];
}
// Resync parents
$sql = 'UPDATE ' . $this->modules_table . "
SET right_id = right_id - $diff
WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
AND left_id < " . (int) $from_data['right_id'] . '
AND right_id > ' . (int) $from_data['right_id'];
$this->db->sql_query($sql);
// Resync righthand side of tree
$sql = 'UPDATE ' . $this->modules_table . "
SET left_id = left_id - $diff, right_id = right_id - $diff
WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
AND left_id > " . (int) $from_data['right_id'];
$this->db->sql_query($sql);
if ($to_parent_id > 0)
{
$to_data = $this->get_module_row($to_parent_id, $module_class);
// Resync new parents
$sql = 'UPDATE ' . $this->modules_table . "
SET right_id = right_id + $diff
WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
AND " . (int) $to_data['right_id'] . ' BETWEEN left_id AND right_id
AND ' . $this->db->sql_in_set('module_id', $moved_ids, true);
$this->db->sql_query($sql);
// Resync the righthand side of the tree
$sql = 'UPDATE ' . $this->modules_table . "
SET left_id = left_id + $diff, right_id = right_id + $diff
WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
AND left_id > " . (int) $to_data['right_id'] . '
AND ' . $this->db->sql_in_set('module_id', $moved_ids, true);
$this->db->sql_query($sql);
// Resync moved branch
$to_data['right_id'] += $diff;
if ($to_data['right_id'] > $from_data['right_id'])
{
$diff = '+ ' . ($to_data['right_id'] - $from_data['right_id'] - 1);
}
else
{
$diff = '- ' . abs($to_data['right_id'] - $from_data['right_id'] - 1);
}
}
else
{
$sql = 'SELECT MAX(right_id) AS right_id
FROM ' . $this->modules_table . "
WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
AND " . $this->db->sql_in_set('module_id', $moved_ids, true);
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
$diff = '+ ' . (int) ($row['right_id'] - $from_data['left_id'] + 1);
}
$sql = 'UPDATE ' . $this->modules_table . "
SET left_id = left_id $diff, right_id = right_id $diff
WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
AND " . $this->db->sql_in_set('module_id', $moved_ids);
$this->db->sql_query($sql);
}
/**
* Remove module from tree
*
* @param int $module_id ID of the module to delete
* @param string $module_class Class of the module (acp, ucp, mcp etc...)
*
* @throws \phpbb\module\exception\module_exception When the specified module cannot be removed
*/
public function delete_module($module_id, $module_class)
{
$module_id = (int) $module_id;
$row = $this->get_module_row($module_id, $module_class);
$branch = $this->get_module_branch($module_id, $module_class, 'children', false);
if (sizeof($branch))
{
throw new module_exception('CANNOT_REMOVE_MODULE');
}
// If not move
$diff = 2;
$sql = 'DELETE FROM ' . $this->modules_table . "
WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
AND module_id = $module_id";
$this->db->sql_query($sql);
$row['right_id'] = (int) $row['right_id'];
$row['left_id'] = (int) $row['left_id'];
// Resync tree
$sql = 'UPDATE ' . $this->modules_table . "
SET right_id = right_id - $diff
WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
AND left_id < {$row['right_id']} AND right_id > {$row['right_id']}";
$this->db->sql_query($sql);
$sql = 'UPDATE ' . $this->modules_table . "
SET left_id = left_id - $diff, right_id = right_id - $diff
WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
AND left_id > {$row['right_id']}";
$this->db->sql_query($sql);
}
/**
* Move module position by $steps up/down
*
* @param array $module_row Array of module data
* @param string $module_class Class of the module (acp, ucp, mcp etc...)
* @param string $action Direction of moving (valid values: move_up or move_down)
* @param int $steps Number of steps to move module
*
* @return string Returns the language name of the module
*
* @throws \phpbb\module\exception\module_not_found_exception When the specified module does not exists
*/
public function move_module_by($module_row, $module_class, $action = 'move_up', $steps = 1)
{
/**
* Fetch all the siblings between the module's current spot
* and where we want to move it to. If there are less than $steps
* siblings between the current spot and the target then the
* module will move as far as possible
*/
$sql = 'SELECT module_id, left_id, right_id, module_langname
FROM ' . $this->modules_table . "
WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
AND parent_id = " . (int) $module_row['parent_id'] . '
AND ' . (($action == 'move_up') ? 'right_id < ' . (int) $module_row['right_id'] . ' ORDER BY right_id DESC' : 'left_id > ' . (int) $module_row['left_id'] . ' ORDER BY left_id ASC');
$result = $this->db->sql_query_limit($sql, $steps);
$target = array();
while ($row = $this->db->sql_fetchrow($result))
{
$target = $row;
}
$this->db->sql_freeresult($result);
if (!sizeof($target))
{
// The module is already on top or bottom
throw new module_not_found_exception();
}
/**
* $left_id and $right_id define the scope of the nodes that are affected by the move.
* $diff_up and $diff_down are the values to substract or add to each node's left_id
* and right_id in order to move them up or down.
* $move_up_left and $move_up_right define the scope of the nodes that are moving
* up. Other nodes in the scope of ($left_id, $right_id) are considered to move down.
*/
if ($action == 'move_up')
{
$left_id = (int) $target['left_id'];
$right_id = (int) $module_row['right_id'];
$diff_up = (int) ($module_row['left_id'] - $target['left_id']);
$diff_down = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);
$move_up_left = (int) $module_row['left_id'];
$move_up_right = (int) $module_row['right_id'];
}
else
{
$left_id = (int) $module_row['left_id'];
$right_id = (int) $target['right_id'];
$diff_up = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);
$diff_down = (int) ($target['right_id'] - $module_row['right_id']);
$move_up_left = (int) ($module_row['right_id'] + 1);
$move_up_right = (int) $target['right_id'];
}
// Now do the dirty job
$sql = 'UPDATE ' . $this->modules_table . "
SET left_id = left_id + CASE
WHEN left_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
ELSE {$diff_down}
END,
right_id = right_id + CASE
WHEN right_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
ELSE {$diff_down}
END
WHERE module_class = '" . $this->db->sql_escape($module_class) . "'
AND left_id BETWEEN {$left_id} AND {$right_id}
AND right_id BETWEEN {$left_id} AND {$right_id}";
$this->db->sql_query($sql);
$this->remove_cache_file($module_class);
return $target['module_langname'];
}
}