1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/acp/acp_modules.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,1065 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package acp
1.8 +* @version $Id$
1.9 +* @copyright (c) 2005 phpBB Group
1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.11 +*
1.12 +*/
1.13 +
1.14 +/**
1.15 +* @ignore
1.16 +*/
1.17 +if (!defined('IN_PHPBB'))
1.18 +{
1.19 + exit;
1.20 +}
1.21 +
1.22 +/**
1.23 +* - Able to check for new module versions (modes changed/adjusted/added/removed)
1.24 +* Icons for:
1.25 +* - module enabled and displayed (common)
1.26 +* - module enabled and not displayed
1.27 +* - module deactivated
1.28 +* - category (enabled)
1.29 +* - category disabled
1.30 +*/
1.31 +
1.32 +/**
1.33 +* @package acp
1.34 +*/
1.35 +class acp_modules
1.36 +{
1.37 + var $module_class = '';
1.38 + var $parent_id;
1.39 + var $u_action;
1.40 +
1.41 + function main($id, $mode)
1.42 + {
1.43 + global $db, $user, $auth, $template, $module;
1.44 + global $config, $phpbb_admin_path, $phpbb_root_path, $phpEx;
1.45 +
1.46 + // Set a global define for modules we might include (the author is able to prevent execution of code by checking this constant)
1.47 + define('MODULE_INCLUDE', true);
1.48 +
1.49 + $user->add_lang('acp/modules');
1.50 + $this->tpl_name = 'acp_modules';
1.51 +
1.52 + // module class
1.53 + $this->module_class = $mode;
1.54 +
1.55 + if ($this->module_class == 'ucp')
1.56 + {
1.57 + $user->add_lang('ucp');
1.58 + }
1.59 + else if ($this->module_class == 'mcp')
1.60 + {
1.61 + $user->add_lang('mcp');
1.62 + }
1.63 +
1.64 + if ($module->p_class != $this->module_class)
1.65 + {
1.66 + $module->add_mod_info($this->module_class);
1.67 + }
1.68 +
1.69 + $this->page_title = strtoupper($this->module_class);
1.70 +
1.71 + $this->parent_id = request_var('parent_id', 0);
1.72 + $module_id = request_var('m', 0);
1.73 + $action = request_var('action', '');
1.74 + $errors = array();
1.75 +
1.76 + switch ($action)
1.77 + {
1.78 + case 'delete':
1.79 + if (!$module_id)
1.80 + {
1.81 + trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
1.82 + }
1.83 +
1.84 + if (confirm_box(true))
1.85 + {
1.86 + // Make sure we are not directly within a module
1.87 + if ($module_id == $this->parent_id)
1.88 + {
1.89 + $sql = 'SELECT parent_id
1.90 + FROM ' . MODULES_TABLE . '
1.91 + WHERE module_id = ' . $module_id;
1.92 + $result = $db->sql_query($sql);
1.93 + $this->parent_id = (int) $db->sql_fetchfield('parent_id');
1.94 + $db->sql_freeresult($result);
1.95 + }
1.96 +
1.97 + $errors = $this->delete_module($module_id);
1.98 +
1.99 + if (!sizeof($errors))
1.100 + {
1.101 + $this->remove_cache_file();
1.102 + trigger_error($user->lang['MODULE_DELETED'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
1.103 + }
1.104 + }
1.105 + else
1.106 + {
1.107 + confirm_box(false, 'DELETE_MODULE', build_hidden_fields(array(
1.108 + 'i' => $id,
1.109 + 'mode' => $mode,
1.110 + 'parent_id' => $this->parent_id,
1.111 + 'module_id' => $module_id,
1.112 + 'action' => $action,
1.113 + )));
1.114 + }
1.115 +
1.116 + break;
1.117 +
1.118 + case 'enable':
1.119 + case 'disable':
1.120 + if (!$module_id)
1.121 + {
1.122 + trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
1.123 + }
1.124 +
1.125 + $sql = 'SELECT *
1.126 + FROM ' . MODULES_TABLE . "
1.127 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.128 + AND module_id = $module_id";
1.129 + $result = $db->sql_query($sql);
1.130 + $row = $db->sql_fetchrow($result);
1.131 + $db->sql_freeresult($result);
1.132 +
1.133 + if (!$row)
1.134 + {
1.135 + trigger_error($user->lang['NO_MODULE'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
1.136 + }
1.137 +
1.138 + $sql = 'UPDATE ' . MODULES_TABLE . '
1.139 + SET module_enabled = ' . (($action == 'enable') ? 1 : 0) . "
1.140 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.141 + AND module_id = $module_id";
1.142 + $db->sql_query($sql);
1.143 +
1.144 + add_log('admin', 'LOG_MODULE_' . strtoupper($action), $this->lang_name($row['module_langname']));
1.145 + $this->remove_cache_file();
1.146 +
1.147 + break;
1.148 +
1.149 + case 'move_up':
1.150 + case 'move_down':
1.151 + if (!$module_id)
1.152 + {
1.153 + trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
1.154 + }
1.155 +
1.156 + $sql = 'SELECT *
1.157 + FROM ' . MODULES_TABLE . "
1.158 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.159 + AND module_id = $module_id";
1.160 + $result = $db->sql_query($sql);
1.161 + $row = $db->sql_fetchrow($result);
1.162 + $db->sql_freeresult($result);
1.163 +
1.164 + if (!$row)
1.165 + {
1.166 + trigger_error($user->lang['NO_MODULE'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
1.167 + }
1.168 +
1.169 + $move_module_name = $this->move_module_by($row, $action, 1);
1.170 +
1.171 + if ($move_module_name !== false)
1.172 + {
1.173 + add_log('admin', 'LOG_MODULE_' . strtoupper($action), $this->lang_name($row['module_langname']), $move_module_name);
1.174 + $this->remove_cache_file();
1.175 + }
1.176 +
1.177 + break;
1.178 +
1.179 + case 'quickadd':
1.180 + $quick_install = request_var('quick_install', '');
1.181 +
1.182 + if (confirm_box(true))
1.183 + {
1.184 + if (!$quick_install || strpos($quick_install, '::') === false)
1.185 + {
1.186 + break;
1.187 + }
1.188 +
1.189 + list($module_basename, $module_mode) = explode('::', $quick_install);
1.190 +
1.191 + // Check if module name and mode exist...
1.192 + $fileinfo = $this->get_module_infos($module_basename);
1.193 + $fileinfo = $fileinfo[$module_basename];
1.194 +
1.195 + if (isset($fileinfo['modes'][$module_mode]))
1.196 + {
1.197 + $module_data = array(
1.198 + 'module_basename' => $module_basename,
1.199 + 'module_enabled' => 0,
1.200 + 'module_display' => (isset($fileinfo['modes'][$module_mode]['display'])) ? $fileinfo['modes'][$module_mode]['display'] : 1,
1.201 + 'parent_id' => $this->parent_id,
1.202 + 'module_class' => $this->module_class,
1.203 + 'module_langname' => $fileinfo['modes'][$module_mode]['title'],
1.204 + 'module_mode' => $module_mode,
1.205 + 'module_auth' => $fileinfo['modes'][$module_mode]['auth'],
1.206 + );
1.207 +
1.208 + $errors = $this->update_module_data($module_data);
1.209 +
1.210 + if (!sizeof($errors))
1.211 + {
1.212 + $this->remove_cache_file();
1.213 +
1.214 + trigger_error($user->lang['MODULE_ADDED'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
1.215 + }
1.216 + }
1.217 + }
1.218 + else
1.219 + {
1.220 + confirm_box(false, 'ADD_MODULE', build_hidden_fields(array(
1.221 + 'i' => $id,
1.222 + 'mode' => $mode,
1.223 + 'parent_id' => $this->parent_id,
1.224 + 'action' => 'quickadd',
1.225 + 'quick_install' => $quick_install,
1.226 + )));
1.227 + }
1.228 +
1.229 + break;
1.230 +
1.231 + case 'edit':
1.232 +
1.233 + if (!$module_id)
1.234 + {
1.235 + trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
1.236 + }
1.237 +
1.238 + $module_row = $this->get_module_row($module_id);
1.239 +
1.240 + // no break
1.241 +
1.242 + case 'add':
1.243 +
1.244 + if ($action == 'add')
1.245 + {
1.246 + $module_row = array(
1.247 + 'module_basename' => '',
1.248 + 'module_enabled' => 0,
1.249 + 'module_display' => 1,
1.250 + 'parent_id' => 0,
1.251 + 'module_langname' => utf8_normalize_nfc(request_var('module_langname', '', true)),
1.252 + 'module_mode' => '',
1.253 + 'module_auth' => '',
1.254 + );
1.255 + }
1.256 +
1.257 + $module_data = array();
1.258 +
1.259 + $module_data['module_basename'] = request_var('module_basename', (string) $module_row['module_basename']);
1.260 + $module_data['module_enabled'] = request_var('module_enabled', (int) $module_row['module_enabled']);
1.261 + $module_data['module_display'] = request_var('module_display', (int) $module_row['module_display']);
1.262 + $module_data['parent_id'] = request_var('module_parent_id', (int) $module_row['parent_id']);
1.263 + $module_data['module_class'] = $this->module_class;
1.264 + $module_data['module_langname'] = utf8_normalize_nfc(request_var('module_langname', (string) $module_row['module_langname'], true));
1.265 + $module_data['module_mode'] = request_var('module_mode', (string) $module_row['module_mode']);
1.266 +
1.267 + $submit = (isset($_POST['submit'])) ? true : false;
1.268 +
1.269 + if ($submit)
1.270 + {
1.271 + if (!$module_data['module_langname'])
1.272 + {
1.273 + trigger_error($user->lang['NO_MODULE_LANGNAME'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
1.274 + }
1.275 +
1.276 + $module_type = request_var('module_type', 'category');
1.277 +
1.278 + if ($module_type == 'category')
1.279 + {
1.280 + $module_data['module_basename'] = $module_data['module_mode'] = $module_data['module_auth'] = '';
1.281 + $module_data['module_display'] = 1;
1.282 + }
1.283 +
1.284 + if ($action == 'edit')
1.285 + {
1.286 + $module_data['module_id'] = $module_id;
1.287 + }
1.288 +
1.289 + // Adjust auth row
1.290 + if ($module_data['module_basename'] && $module_data['module_mode'])
1.291 + {
1.292 + $fileinfo = $this->get_module_infos($module_data['module_basename']);
1.293 + $module_data['module_auth'] = $fileinfo[$module_data['module_basename']]['modes'][$module_data['module_mode']]['auth'];
1.294 + }
1.295 +
1.296 + $errors = $this->update_module_data($module_data);
1.297 +
1.298 + if (!sizeof($errors))
1.299 + {
1.300 + $this->remove_cache_file();
1.301 +
1.302 + trigger_error((($action == 'add') ? $user->lang['MODULE_ADDED'] : $user->lang['MODULE_EDITED']) . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
1.303 + }
1.304 + }
1.305 +
1.306 + // Category/not category?
1.307 + $is_cat = (!$module_data['module_basename']) ? true : false;
1.308 +
1.309 + // Get module information
1.310 + $module_infos = $this->get_module_infos();
1.311 +
1.312 + // Build name options
1.313 + $s_name_options = $s_mode_options = '';
1.314 + foreach ($module_infos as $option => $values)
1.315 + {
1.316 + if (!$module_data['module_basename'])
1.317 + {
1.318 + $module_data['module_basename'] = $option;
1.319 + }
1.320 +
1.321 + // Name options
1.322 + $s_name_options .= '<option value="' . $option . '"' . (($option == $module_data['module_basename']) ? ' selected="selected"' : '') . '>' . $this->lang_name($values['title']) . ' [' . $this->module_class . '_' . $option . ']</option>';
1.323 +
1.324 + $template->assign_block_vars('m_names', array('NAME' => $option, 'A_NAME' => addslashes($option)));
1.325 +
1.326 + // Build module modes
1.327 + foreach ($values['modes'] as $m_mode => $m_values)
1.328 + {
1.329 + if ($option == $module_data['module_basename'])
1.330 + {
1.331 + $s_mode_options .= '<option value="' . $m_mode . '"' . (($m_mode == $module_data['module_mode']) ? ' selected="selected"' : '') . '>' . $this->lang_name($m_values['title']) . '</option>';
1.332 + }
1.333 +
1.334 + $template->assign_block_vars('m_names.modes', array(
1.335 + 'OPTION' => $m_mode,
1.336 + 'VALUE' => $this->lang_name($m_values['title']),
1.337 + 'A_OPTION' => addslashes($m_mode),
1.338 + 'A_VALUE' => addslashes($this->lang_name($m_values['title'])))
1.339 + );
1.340 + }
1.341 + }
1.342 +
1.343 + $s_cat_option = '<option value="0"' . (($module_data['parent_id'] == 0) ? ' selected="selected"' : '') . '>' . $user->lang['NO_PARENT'] . '</option>';
1.344 +
1.345 + $template->assign_vars(array_merge(array(
1.346 + 'S_EDIT_MODULE' => true,
1.347 + 'S_IS_CAT' => $is_cat,
1.348 + 'S_CAT_OPTIONS' => $s_cat_option . $this->make_module_select($module_data['parent_id'], ($action == 'edit') ? $module_row['module_id'] : false, false, false, false, true),
1.349 + 'S_MODULE_NAMES' => $s_name_options,
1.350 + 'S_MODULE_MODES' => $s_mode_options,
1.351 + 'U_BACK' => $this->u_action . '&parent_id=' . $this->parent_id,
1.352 + 'U_EDIT_ACTION' => $this->u_action . '&parent_id=' . $this->parent_id,
1.353 +
1.354 + 'L_TITLE' => $user->lang[strtoupper($action) . '_MODULE'],
1.355 +
1.356 + 'MODULENAME' => $this->lang_name($module_data['module_langname']),
1.357 + 'ACTION' => $action,
1.358 + 'MODULE_ID' => $module_id,
1.359 +
1.360 + ),
1.361 + array_change_key_case($module_data, CASE_UPPER))
1.362 + );
1.363 +
1.364 + if (sizeof($errors))
1.365 + {
1.366 + $template->assign_vars(array(
1.367 + 'S_ERROR' => true,
1.368 + 'ERROR_MSG' => implode('<br />', $errors))
1.369 + );
1.370 + }
1.371 +
1.372 + return;
1.373 +
1.374 + break;
1.375 + }
1.376 +
1.377 + // Default management page
1.378 + if (sizeof($errors))
1.379 + {
1.380 + $template->assign_vars(array(
1.381 + 'S_ERROR' => true,
1.382 + 'ERROR_MSG' => implode('<br />', $errors))
1.383 + );
1.384 + }
1.385 +
1.386 + if (!$this->parent_id)
1.387 + {
1.388 + $navigation = strtoupper($this->module_class);
1.389 + }
1.390 + else
1.391 + {
1.392 + $navigation = '<a href="' . $this->u_action . '">' . strtoupper($this->module_class) . '</a>';
1.393 +
1.394 + $modules_nav = $this->get_module_branch($this->parent_id, 'parents', 'descending');
1.395 +
1.396 + foreach ($modules_nav as $row)
1.397 + {
1.398 + $langname = $this->lang_name($row['module_langname']);
1.399 +
1.400 + if ($row['module_id'] == $this->parent_id)
1.401 + {
1.402 + $navigation .= ' -> ' . $langname;
1.403 + }
1.404 + else
1.405 + {
1.406 + $navigation .= ' -> <a href="' . $this->u_action . '&parent_id=' . $row['module_id'] . '">' . $langname . '</a>';
1.407 + }
1.408 + }
1.409 + }
1.410 +
1.411 + // Jumpbox
1.412 + $module_box = $this->make_module_select($this->parent_id, false, false, false, false);
1.413 +
1.414 + $sql = 'SELECT *
1.415 + FROM ' . MODULES_TABLE . "
1.416 + WHERE parent_id = {$this->parent_id}
1.417 + AND module_class = '" . $db->sql_escape($this->module_class) . "'
1.418 + ORDER BY left_id";
1.419 + $result = $db->sql_query($sql);
1.420 +
1.421 + if ($row = $db->sql_fetchrow($result))
1.422 + {
1.423 + do
1.424 + {
1.425 + $langname = $this->lang_name($row['module_langname']);
1.426 +
1.427 + if (!$row['module_enabled'])
1.428 + {
1.429 + $module_image = '<img src="images/icon_folder_lock.gif" alt="' . $user->lang['DEACTIVATED_MODULE'] .'" />';
1.430 + }
1.431 + else
1.432 + {
1.433 + $module_image = (!$row['module_basename'] || $row['left_id'] + 1 != $row['right_id']) ? '<img src="images/icon_subfolder.gif" alt="' . $user->lang['CATEGORY'] . '" />' : '<img src="images/icon_folder.gif" alt="' . $user->lang['MODULE'] . '" />';
1.434 + }
1.435 +
1.436 + $url = $this->u_action . '&parent_id=' . $this->parent_id . '&m=' . $row['module_id'];
1.437 +
1.438 + $template->assign_block_vars('modules', array(
1.439 + 'MODULE_IMAGE' => $module_image,
1.440 + 'MODULE_TITLE' => $langname,
1.441 + 'MODULE_ENABLED' => ($row['module_enabled']) ? true : false,
1.442 + 'MODULE_DISPLAYED' => ($row['module_display']) ? true : false,
1.443 +
1.444 + 'S_ACP_CAT_SYSTEM' => ($this->module_class == 'acp' && $row['module_langname'] == 'ACP_CAT_SYSTEM') ? true : false,
1.445 + 'S_ACP_MODULE_MANAGEMENT' => ($this->module_class == 'acp' && ($row['module_basename'] == 'modules' || $row['module_langname'] == 'ACP_MODULE_MANAGEMENT')) ? true : false,
1.446 +
1.447 + 'U_MODULE' => $this->u_action . '&parent_id=' . $row['module_id'],
1.448 + 'U_MOVE_UP' => $url . '&action=move_up',
1.449 + 'U_MOVE_DOWN' => $url . '&action=move_down',
1.450 + 'U_EDIT' => $url . '&action=edit',
1.451 + 'U_DELETE' => $url . '&action=delete',
1.452 + 'U_ENABLE' => $url . '&action=enable',
1.453 + 'U_DISABLE' => $url . '&action=disable')
1.454 + );
1.455 + }
1.456 + while ($row = $db->sql_fetchrow($result));
1.457 + }
1.458 + else if ($this->parent_id)
1.459 + {
1.460 + $row = $this->get_module_row($this->parent_id);
1.461 +
1.462 + $url = $this->u_action . '&parent_id=' . $this->parent_id . '&m=' . $row['module_id'];
1.463 +
1.464 + $template->assign_vars(array(
1.465 + 'S_NO_MODULES' => true,
1.466 + 'MODULE_TITLE' => $langname,
1.467 + 'MODULE_ENABLED' => ($row['module_enabled']) ? true : false,
1.468 + 'MODULE_DISPLAYED' => ($row['module_display']) ? true : false,
1.469 +
1.470 + 'U_EDIT' => $url . '&action=edit',
1.471 + 'U_DELETE' => $url . '&action=delete',
1.472 + 'U_ENABLE' => $url . '&action=enable',
1.473 + 'U_DISABLE' => $url . '&action=disable')
1.474 + );
1.475 + }
1.476 + $db->sql_freeresult($result);
1.477 +
1.478 + // Quick adding module
1.479 + $module_infos = $this->get_module_infos();
1.480 +
1.481 + // Build quick options
1.482 + $s_install_options = '';
1.483 + foreach ($module_infos as $option => $values)
1.484 + {
1.485 + // Name options
1.486 + $s_install_options .= '<optgroup label="' . $this->lang_name($values['title']) . ' [' . $this->module_class . '_' . $option . ']">';
1.487 +
1.488 + // Build module modes
1.489 + foreach ($values['modes'] as $m_mode => $m_values)
1.490 + {
1.491 + $s_install_options .= '<option value="' . $option . '::' . $m_mode . '"> ' . $this->lang_name($m_values['title']) . '</option>';
1.492 + }
1.493 +
1.494 + $s_install_options .= '</optgroup>';
1.495 + }
1.496 +
1.497 + $template->assign_vars(array(
1.498 + 'U_SEL_ACTION' => $this->u_action,
1.499 + 'U_ACTION' => $this->u_action . '&parent_id=' . $this->parent_id,
1.500 + 'NAVIGATION' => $navigation,
1.501 + 'MODULE_BOX' => $module_box,
1.502 + 'PARENT_ID' => $this->parent_id,
1.503 + 'S_INSTALL_OPTIONS' => $s_install_options,
1.504 + )
1.505 + );
1.506 + }
1.507 +
1.508 + /**
1.509 + * Get row for specified module
1.510 + */
1.511 + function get_module_row($module_id)
1.512 + {
1.513 + global $db, $user;
1.514 +
1.515 + $sql = 'SELECT *
1.516 + FROM ' . MODULES_TABLE . "
1.517 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.518 + AND module_id = $module_id";
1.519 + $result = $db->sql_query($sql);
1.520 + $row = $db->sql_fetchrow($result);
1.521 + $db->sql_freeresult($result);
1.522 +
1.523 + if (!$row)
1.524 + {
1.525 + trigger_error($user->lang['NO_MODULE'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
1.526 + }
1.527 +
1.528 + return $row;
1.529 + }
1.530 +
1.531 + /**
1.532 + * Get available module information from module files
1.533 + */
1.534 + function get_module_infos($module = '', $module_class = false)
1.535 + {
1.536 + global $phpbb_root_path, $phpEx;
1.537 +
1.538 + $module_class = ($module_class === false) ? $this->module_class : $module_class;
1.539 +
1.540 + $directory = $phpbb_root_path . 'includes/' . $module_class . '/info/';
1.541 + $fileinfo = array();
1.542 +
1.543 + if (!$module)
1.544 + {
1.545 + $dh = @opendir($directory);
1.546 +
1.547 + if (!$dh)
1.548 + {
1.549 + return $fileinfo;
1.550 + }
1.551 +
1.552 + while (($file = readdir($dh)) !== false)
1.553 + {
1.554 + // Is module?
1.555 + if (preg_match('/^' . $module_class . '_.+\.' . $phpEx . '$/', $file))
1.556 + {
1.557 + $class = str_replace(".$phpEx", '', $file) . '_info';
1.558 +
1.559 + if (!class_exists($class))
1.560 + {
1.561 + include($directory . $file);
1.562 + }
1.563 +
1.564 + // Get module title tag
1.565 + if (class_exists($class))
1.566 + {
1.567 + $c_class = new $class();
1.568 + $module_info = $c_class->module();
1.569 + $fileinfo[str_replace($module_class . '_', '', $module_info['filename'])] = $module_info;
1.570 + }
1.571 + }
1.572 + }
1.573 + closedir($dh);
1.574 +
1.575 + ksort($fileinfo);
1.576 + }
1.577 + else
1.578 + {
1.579 + $filename = $module_class . '_' . basename($module);
1.580 + $class = $module_class . '_' . basename($module) . '_info';
1.581 +
1.582 + if (!class_exists($class))
1.583 + {
1.584 + include($directory . $filename . '.' . $phpEx);
1.585 + }
1.586 +
1.587 + // Get module title tag
1.588 + if (class_exists($class))
1.589 + {
1.590 + $c_class = new $class();
1.591 + $module_info = $c_class->module();
1.592 + $fileinfo[str_replace($module_class . '_', '', $module_info['filename'])] = $module_info;
1.593 + }
1.594 + }
1.595 +
1.596 + return $fileinfo;
1.597 + }
1.598 +
1.599 + /**
1.600 + * Simple version of jumpbox, just lists modules
1.601 + */
1.602 + function make_module_select($select_id = false, $ignore_id = false, $ignore_acl = false, $ignore_nonpost = false, $ignore_emptycat = true, $ignore_noncat = false)
1.603 + {
1.604 + global $db, $user, $auth, $config;
1.605 +
1.606 + $sql = 'SELECT module_id, module_enabled, module_basename, parent_id, module_langname, left_id, right_id, module_auth
1.607 + FROM ' . MODULES_TABLE . "
1.608 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.609 + ORDER BY left_id ASC";
1.610 + $result = $db->sql_query($sql);
1.611 +
1.612 + $right = $iteration = 0;
1.613 + $padding_store = array('0' => '');
1.614 + $module_list = $padding = '';
1.615 +
1.616 + while ($row = $db->sql_fetchrow($result))
1.617 + {
1.618 + if ($row['left_id'] < $right)
1.619 + {
1.620 + $padding .= ' ';
1.621 + $padding_store[$row['parent_id']] = $padding;
1.622 + }
1.623 + else if ($row['left_id'] > $right + 1)
1.624 + {
1.625 + $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
1.626 + }
1.627 +
1.628 + $right = $row['right_id'];
1.629 +
1.630 + if (!$ignore_acl && $row['module_auth'])
1.631 + {
1.632 + // We use zero as the forum id to check - global setting.
1.633 + if (!p_master::module_auth($row['module_auth'], 0))
1.634 + {
1.635 + continue;
1.636 + }
1.637 + }
1.638 +
1.639 + // ignore this module?
1.640 + if ((is_array($ignore_id) && in_array($row['module_id'], $ignore_id)) || $row['module_id'] == $ignore_id)
1.641 + {
1.642 + continue;
1.643 + }
1.644 +
1.645 + // empty category
1.646 + if (!$row['module_basename'] && ($row['left_id'] + 1 == $row['right_id']) && $ignore_emptycat)
1.647 + {
1.648 + continue;
1.649 + }
1.650 +
1.651 + // ignore non-category?
1.652 + if ($row['module_basename'] && $ignore_noncat)
1.653 + {
1.654 + continue;
1.655 + }
1.656 +
1.657 + $selected = (is_array($select_id)) ? ((in_array($row['module_id'], $select_id)) ? ' selected="selected"' : '') : (($row['module_id'] == $select_id) ? ' selected="selected"' : '');
1.658 +
1.659 + $langname = $this->lang_name($row['module_langname']);
1.660 + $module_list .= '<option value="' . $row['module_id'] . '"' . $selected . ((!$row['module_enabled']) ? ' class="disabled"' : '') . '>' . $padding . $langname . '</option>';
1.661 +
1.662 + $iteration++;
1.663 + }
1.664 + $db->sql_freeresult($result);
1.665 +
1.666 + unset($padding_store);
1.667 +
1.668 + return $module_list;
1.669 + }
1.670 +
1.671 + /**
1.672 + * Get module branch
1.673 + */
1.674 + function get_module_branch($module_id, $type = 'all', $order = 'descending', $include_module = true)
1.675 + {
1.676 + global $db;
1.677 +
1.678 + switch ($type)
1.679 + {
1.680 + case 'parents':
1.681 + $condition = 'm1.left_id BETWEEN m2.left_id AND m2.right_id';
1.682 + break;
1.683 +
1.684 + case 'children':
1.685 + $condition = 'm2.left_id BETWEEN m1.left_id AND m1.right_id';
1.686 + break;
1.687 +
1.688 + default:
1.689 + $condition = 'm2.left_id BETWEEN m1.left_id AND m1.right_id OR m1.left_id BETWEEN m2.left_id AND m2.right_id';
1.690 + break;
1.691 + }
1.692 +
1.693 + $rows = array();
1.694 +
1.695 + $sql = 'SELECT m2.*
1.696 + FROM ' . MODULES_TABLE . ' m1
1.697 + LEFT JOIN ' . MODULES_TABLE . " m2 ON ($condition)
1.698 + WHERE m1.module_class = '" . $db->sql_escape($this->module_class) . "'
1.699 + AND m2.module_class = '" . $db->sql_escape($this->module_class) . "'
1.700 + AND m1.module_id = $module_id
1.701 + ORDER BY m2.left_id " . (($order == 'descending') ? 'ASC' : 'DESC');
1.702 + $result = $db->sql_query($sql);
1.703 +
1.704 + while ($row = $db->sql_fetchrow($result))
1.705 + {
1.706 + if (!$include_module && $row['module_id'] == $module_id)
1.707 + {
1.708 + continue;
1.709 + }
1.710 +
1.711 + $rows[] = $row;
1.712 + }
1.713 + $db->sql_freeresult($result);
1.714 +
1.715 + return $rows;
1.716 + }
1.717 +
1.718 + /**
1.719 + * Remove modules cache file
1.720 + */
1.721 + function remove_cache_file()
1.722 + {
1.723 + global $cache;
1.724 +
1.725 + // Sanitise for future path use, it's escaped as appropriate for queries
1.726 + $p_class = str_replace(array('.', '/', '\\'), '', basename($this->module_class));
1.727 +
1.728 + $cache->destroy('_modules_' . $p_class);
1.729 +
1.730 + // Additionally remove sql cache
1.731 + $cache->destroy('sql', MODULES_TABLE);
1.732 + }
1.733 +
1.734 + /**
1.735 + * Return correct language name
1.736 + */
1.737 + function lang_name($module_langname)
1.738 + {
1.739 + global $user;
1.740 +
1.741 + return (!empty($user->lang[$module_langname])) ? $user->lang[$module_langname] : $module_langname;
1.742 + }
1.743 +
1.744 + /**
1.745 + * Update/Add module
1.746 + *
1.747 + * @param bool $run_inline if set to true errors will be returned and no logs being written
1.748 + */
1.749 + function update_module_data(&$module_data, $run_inline = false)
1.750 + {
1.751 + global $db, $user;
1.752 +
1.753 + if (!isset($module_data['module_id']))
1.754 + {
1.755 + // no module_id means we're creating a new category/module
1.756 + if ($module_data['parent_id'])
1.757 + {
1.758 + $sql = 'SELECT left_id, right_id
1.759 + FROM ' . MODULES_TABLE . "
1.760 + WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
1.761 + AND module_id = " . (int) $module_data['parent_id'];
1.762 + $result = $db->sql_query($sql);
1.763 + $row = $db->sql_fetchrow($result);
1.764 + $db->sql_freeresult($result);
1.765 +
1.766 + if (!$row)
1.767 + {
1.768 + if ($run_inline)
1.769 + {
1.770 + return 'PARENT_NO_EXIST';
1.771 + }
1.772 +
1.773 + trigger_error($user->lang['PARENT_NO_EXIST'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
1.774 + }
1.775 +
1.776 + // Workaround
1.777 + $row['left_id'] = (int) $row['left_id'];
1.778 + $row['right_id'] = (int) $row['right_id'];
1.779 +
1.780 + $sql = 'UPDATE ' . MODULES_TABLE . "
1.781 + SET left_id = left_id + 2, right_id = right_id + 2
1.782 + WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
1.783 + AND left_id > {$row['right_id']}";
1.784 + $db->sql_query($sql);
1.785 +
1.786 + $sql = 'UPDATE ' . MODULES_TABLE . "
1.787 + SET right_id = right_id + 2
1.788 + WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
1.789 + AND {$row['left_id']} BETWEEN left_id AND right_id";
1.790 + $db->sql_query($sql);
1.791 +
1.792 + $module_data['left_id'] = (int) $row['right_id'];
1.793 + $module_data['right_id'] = (int) $row['right_id'] + 1;
1.794 + }
1.795 + else
1.796 + {
1.797 + $sql = 'SELECT MAX(right_id) AS right_id
1.798 + FROM ' . MODULES_TABLE . "
1.799 + WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'";
1.800 + $result = $db->sql_query($sql);
1.801 + $row = $db->sql_fetchrow($result);
1.802 + $db->sql_freeresult($result);
1.803 +
1.804 + $module_data['left_id'] = (int) $row['right_id'] + 1;
1.805 + $module_data['right_id'] = (int) $row['right_id'] + 2;
1.806 + }
1.807 +
1.808 + $sql = 'INSERT INTO ' . MODULES_TABLE . ' ' . $db->sql_build_array('INSERT', $module_data);
1.809 + $db->sql_query($sql);
1.810 +
1.811 + $module_data['module_id'] = $db->sql_nextid();
1.812 +
1.813 + if (!$run_inline)
1.814 + {
1.815 + add_log('admin', 'LOG_MODULE_ADD', $this->lang_name($module_data['module_langname']));
1.816 + }
1.817 + }
1.818 + else
1.819 + {
1.820 + $row = $this->get_module_row($module_data['module_id']);
1.821 +
1.822 + if ($module_data['module_basename'] && !$row['module_basename'])
1.823 + {
1.824 + // we're turning a category into a module
1.825 + $branch = $this->get_module_branch($module_data['module_id'], 'children', 'descending', false);
1.826 +
1.827 + if (sizeof($branch))
1.828 + {
1.829 + return array($user->lang['NO_CATEGORY_TO_MODULE']);
1.830 + }
1.831 + }
1.832 +
1.833 + if ($row['parent_id'] != $module_data['parent_id'])
1.834 + {
1.835 + $this->move_module($module_data['module_id'], $module_data['parent_id']);
1.836 + }
1.837 +
1.838 + $update_ary = $module_data;
1.839 + unset($update_ary['module_id']);
1.840 +
1.841 + $sql = 'UPDATE ' . MODULES_TABLE . '
1.842 + SET ' . $db->sql_build_array('UPDATE', $update_ary) . "
1.843 + WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
1.844 + AND module_id = " . (int) $module_data['module_id'];
1.845 + $db->sql_query($sql);
1.846 +
1.847 + if (!$run_inline)
1.848 + {
1.849 + add_log('admin', 'LOG_MODULE_EDIT', $this->lang_name($module_data['module_langname']));
1.850 + }
1.851 + }
1.852 +
1.853 + return array();
1.854 + }
1.855 +
1.856 + /**
1.857 + * Move module around the tree
1.858 + */
1.859 + function move_module($from_module_id, $to_parent_id)
1.860 + {
1.861 + global $db;
1.862 +
1.863 + $moved_modules = $this->get_module_branch($from_module_id, 'children', 'descending');
1.864 + $from_data = $moved_modules[0];
1.865 + $diff = sizeof($moved_modules) * 2;
1.866 +
1.867 + $moved_ids = array();
1.868 + for ($i = 0; $i < sizeof($moved_modules); ++$i)
1.869 + {
1.870 + $moved_ids[] = $moved_modules[$i]['module_id'];
1.871 + }
1.872 +
1.873 + // Resync parents
1.874 + $sql = 'UPDATE ' . MODULES_TABLE . "
1.875 + SET right_id = right_id - $diff
1.876 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.877 + AND left_id < " . (int) $from_data['right_id'] . '
1.878 + AND right_id > ' . (int) $from_data['right_id'];
1.879 + $db->sql_query($sql);
1.880 +
1.881 + // Resync righthand side of tree
1.882 + $sql = 'UPDATE ' . MODULES_TABLE . "
1.883 + SET left_id = left_id - $diff, right_id = right_id - $diff
1.884 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.885 + AND left_id > " . (int) $from_data['right_id'];
1.886 + $db->sql_query($sql);
1.887 +
1.888 + if ($to_parent_id > 0)
1.889 + {
1.890 + $to_data = $this->get_module_row($to_parent_id);
1.891 +
1.892 + // Resync new parents
1.893 + $sql = 'UPDATE ' . MODULES_TABLE . "
1.894 + SET right_id = right_id + $diff
1.895 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.896 + AND " . (int) $to_data['right_id'] . ' BETWEEN left_id AND right_id
1.897 + AND ' . $db->sql_in_set('module_id', $moved_ids, true);
1.898 + $db->sql_query($sql);
1.899 +
1.900 + // Resync the righthand side of the tree
1.901 + $sql = 'UPDATE ' . MODULES_TABLE . "
1.902 + SET left_id = left_id + $diff, right_id = right_id + $diff
1.903 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.904 + AND left_id > " . (int) $to_data['right_id'] . '
1.905 + AND ' . $db->sql_in_set('module_id', $moved_ids, true);
1.906 + $db->sql_query($sql);
1.907 +
1.908 + // Resync moved branch
1.909 + $to_data['right_id'] += $diff;
1.910 + if ($to_data['right_id'] > $from_data['right_id'])
1.911 + {
1.912 + $diff = '+ ' . ($to_data['right_id'] - $from_data['right_id'] - 1);
1.913 + }
1.914 + else
1.915 + {
1.916 + $diff = '- ' . abs($to_data['right_id'] - $from_data['right_id'] - 1);
1.917 + }
1.918 + }
1.919 + else
1.920 + {
1.921 + $sql = 'SELECT MAX(right_id) AS right_id
1.922 + FROM ' . MODULES_TABLE . "
1.923 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.924 + AND " . $db->sql_in_set('module_id', $moved_ids, true);
1.925 + $result = $db->sql_query($sql);
1.926 + $row = $db->sql_fetchrow($result);
1.927 + $db->sql_freeresult($result);
1.928 +
1.929 + $diff = '+ ' . (int) ($row['right_id'] - $from_data['left_id'] + 1);
1.930 + }
1.931 +
1.932 + $sql = 'UPDATE ' . MODULES_TABLE . "
1.933 + SET left_id = left_id $diff, right_id = right_id $diff
1.934 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.935 + AND " . $db->sql_in_set('module_id', $moved_ids);
1.936 + $db->sql_query($sql);
1.937 + }
1.938 +
1.939 + /**
1.940 + * Remove module from tree
1.941 + */
1.942 + function delete_module($module_id)
1.943 + {
1.944 + global $db, $user;
1.945 +
1.946 + $row = $this->get_module_row($module_id);
1.947 +
1.948 + $branch = $this->get_module_branch($module_id, 'children', 'descending', false);
1.949 +
1.950 + if (sizeof($branch))
1.951 + {
1.952 + return array($user->lang['CANNOT_REMOVE_MODULE']);
1.953 + }
1.954 +
1.955 + // If not move
1.956 + $diff = 2;
1.957 + $sql = 'DELETE FROM ' . MODULES_TABLE . "
1.958 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.959 + AND module_id = $module_id";
1.960 + $db->sql_query($sql);
1.961 +
1.962 + $row['right_id'] = (int) $row['right_id'];
1.963 + $row['left_id'] = (int) $row['left_id'];
1.964 +
1.965 + // Resync tree
1.966 + $sql = 'UPDATE ' . MODULES_TABLE . "
1.967 + SET right_id = right_id - $diff
1.968 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.969 + AND left_id < {$row['right_id']} AND right_id > {$row['right_id']}";
1.970 + $db->sql_query($sql);
1.971 +
1.972 + $sql = 'UPDATE ' . MODULES_TABLE . "
1.973 + SET left_id = left_id - $diff, right_id = right_id - $diff
1.974 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.975 + AND left_id > {$row['right_id']}";
1.976 + $db->sql_query($sql);
1.977 +
1.978 + add_log('admin', 'LOG_MODULE_REMOVED', $this->lang_name($row['module_langname']));
1.979 +
1.980 + return array();
1.981 +
1.982 + }
1.983 +
1.984 + /**
1.985 + * Move module position by $steps up/down
1.986 + */
1.987 + function move_module_by($module_row, $action = 'move_up', $steps = 1)
1.988 + {
1.989 + global $db;
1.990 +
1.991 + /**
1.992 + * Fetch all the siblings between the module's current spot
1.993 + * and where we want to move it to. If there are less than $steps
1.994 + * siblings between the current spot and the target then the
1.995 + * module will move as far as possible
1.996 + */
1.997 + $sql = 'SELECT module_id, left_id, right_id, module_langname
1.998 + FROM ' . MODULES_TABLE . "
1.999 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.1000 + AND parent_id = " . (int) $module_row['parent_id'] . '
1.1001 + 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');
1.1002 + $result = $db->sql_query_limit($sql, $steps);
1.1003 +
1.1004 + $target = array();
1.1005 + while ($row = $db->sql_fetchrow($result))
1.1006 + {
1.1007 + $target = $row;
1.1008 + }
1.1009 + $db->sql_freeresult($result);
1.1010 +
1.1011 + if (!sizeof($target))
1.1012 + {
1.1013 + // The module is already on top or bottom
1.1014 + return false;
1.1015 + }
1.1016 +
1.1017 + /**
1.1018 + * $left_id and $right_id define the scope of the nodes that are affected by the move.
1.1019 + * $diff_up and $diff_down are the values to substract or add to each node's left_id
1.1020 + * and right_id in order to move them up or down.
1.1021 + * $move_up_left and $move_up_right define the scope of the nodes that are moving
1.1022 + * up. Other nodes in the scope of ($left_id, $right_id) are considered to move down.
1.1023 + */
1.1024 + if ($action == 'move_up')
1.1025 + {
1.1026 + $left_id = (int) $target['left_id'];
1.1027 + $right_id = (int) $module_row['right_id'];
1.1028 +
1.1029 + $diff_up = (int) ($module_row['left_id'] - $target['left_id']);
1.1030 + $diff_down = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);
1.1031 +
1.1032 + $move_up_left = (int) $module_row['left_id'];
1.1033 + $move_up_right = (int) $module_row['right_id'];
1.1034 + }
1.1035 + else
1.1036 + {
1.1037 + $left_id = (int) $module_row['left_id'];
1.1038 + $right_id = (int) $target['right_id'];
1.1039 +
1.1040 + $diff_up = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);
1.1041 + $diff_down = (int) ($target['right_id'] - $module_row['right_id']);
1.1042 +
1.1043 + $move_up_left = (int) ($module_row['right_id'] + 1);
1.1044 + $move_up_right = (int) $target['right_id'];
1.1045 + }
1.1046 +
1.1047 + // Now do the dirty job
1.1048 + $sql = 'UPDATE ' . MODULES_TABLE . "
1.1049 + SET left_id = left_id + CASE
1.1050 + WHEN left_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
1.1051 + ELSE {$diff_down}
1.1052 + END,
1.1053 + right_id = right_id + CASE
1.1054 + WHEN right_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
1.1055 + ELSE {$diff_down}
1.1056 + END
1.1057 + WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1.1058 + AND left_id BETWEEN {$left_id} AND {$right_id}
1.1059 + AND right_id BETWEEN {$left_id} AND {$right_id}";
1.1060 + $db->sql_query($sql);
1.1061 +
1.1062 + $this->remove_cache_file();
1.1063 +
1.1064 + return $this->lang_name($target['module_langname']);
1.1065 + }
1.1066 +}
1.1067 +
1.1068 +?>
1.1069 \ No newline at end of file