Verzeichnisstruktur phpBB-3.0.0
- Veröffentlicht
- 12.12.2007
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
acp_modules.php
0001 <?php
0002 /**
0003 *
0004 * @package acp
0005 * @version $Id$
0006 * @copyright (c) 2005 phpBB Group
0007 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
0008 *
0009 */
0010
0011 /**
0012 * @ignore
0013 */
0014 if (!defined('IN_PHPBB'))
0015 {
0016 exit;
0017 }
0018
0019 /**
0020 * - Able to check for new module versions (modes changed/adjusted/added/removed)
0021 * Icons for:
0022 * - module enabled and displayed (common)
0023 * - module enabled and not displayed
0024 * - module deactivated
0025 * - category (enabled)
0026 * - category disabled
0027 */
0028
0029 /**
0030 * @package acp
0031 */
0032 class acp_modules
0033 {
0034 var $module_class = '';
0035 var $parent_id;
0036 var $u_action;
0037
0038 function main($id, $mode)
0039 {
0040 global $db, $user, $auth, $template, $module;
0041 global $config, $phpbb_admin_path, $phpbb_root_path, $phpEx;
0042
0043 // Set a global define for modules we might include (the author is able to prevent execution of code by checking this constant)
0044 define('MODULE_INCLUDE', true);
0045
0046 $user->add_lang('acp/modules');
0047 $this->tpl_name = 'acp_modules';
0048
0049 // module class
0050 $this->module_class = $mode;
0051
0052 if ($this->module_class == 'ucp')
0053 {
0054 $user->add_lang('ucp');
0055 }
0056 else if ($this->module_class == 'mcp')
0057 {
0058 $user->add_lang('mcp');
0059 }
0060
0061 if ($module->p_class != $this->module_class)
0062 {
0063 $module->add_mod_info($this->module_class);
0064 }
0065
0066 $this->page_title = strtoupper($this->module_class);
0067
0068 $this->parent_id = request_var('parent_id', 0);
0069 $module_id = request_var('m', 0);
0070 $action = request_var('action', '');
0071 $errors = array();
0072
0073 switch ($action)
0074 {
0075 case 'delete':
0076 if (!$module_id)
0077 {
0078 trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
0079 }
0080
0081 if (confirm_box(true))
0082 {
0083 // Make sure we are not directly within a module
0084 if ($module_id == $this->parent_id)
0085 {
0086 $sql = 'SELECT parent_id
0087 FROM ' . MODULES_TABLE . '
0088 WHERE module_id = ' . $module_id;
0089 $result = $db->sql_query($sql);
0090 $this->parent_id = (int) $db->sql_fetchfield('parent_id');
0091 $db->sql_freeresult($result);
0092 }
0093
0094 $errors = $this->delete_module($module_id);
0095
0096 if (!sizeof($errors))
0097 {
0098 $this->remove_cache_file();
0099 trigger_error($user->lang['MODULE_DELETED'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
0100 }
0101 }
0102 else
0103 {
0104 confirm_box(false, 'DELETE_MODULE', build_hidden_fields(array(
0105 'i' => $id,
0106 'mode' => $mode,
0107 'parent_id' => $this->parent_id,
0108 'module_id' => $module_id,
0109 'action' => $action,
0110 )));
0111 }
0112
0113 break;
0114
0115 case 'enable':
0116 case 'disable':
0117 if (!$module_id)
0118 {
0119 trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
0120 }
0121
0122 $sql = 'SELECT *
0123 FROM ' . MODULES_TABLE . "
0124 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
0125 AND module_id = $module_id";
0126 $result = $db->sql_query($sql);
0127 $row = $db->sql_fetchrow($result);
0128 $db->sql_freeresult($result);
0129
0130 if (!$row)
0131 {
0132 trigger_error($user->lang['NO_MODULE'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
0133 }
0134
0135 $sql = 'UPDATE ' . MODULES_TABLE . '
0136 SET module_enabled = ' . (($action == 'enable') ? 1 : 0) . "
0137 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
0138 AND module_id = $module_id";
0139 $db->sql_query($sql);
0140
0141 add_log('admin', 'LOG_MODULE_' . strtoupper($action), $this->lang_name($row['module_langname']));
0142 $this->remove_cache_file();
0143
0144 break;
0145
0146 case 'move_up':
0147 case 'move_down':
0148 if (!$module_id)
0149 {
0150 trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
0151 }
0152
0153 $sql = 'SELECT *
0154 FROM ' . MODULES_TABLE . "
0155 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
0156 AND module_id = $module_id";
0157 $result = $db->sql_query($sql);
0158 $row = $db->sql_fetchrow($result);
0159 $db->sql_freeresult($result);
0160
0161 if (!$row)
0162 {
0163 trigger_error($user->lang['NO_MODULE'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
0164 }
0165
0166 $move_module_name = $this->move_module_by($row, $action, 1);
0167
0168 if ($move_module_name !== false)
0169 {
0170 add_log('admin', 'LOG_MODULE_' . strtoupper($action), $this->lang_name($row['module_langname']), $move_module_name);
0171 $this->remove_cache_file();
0172 }
0173
0174 break;
0175
0176 case 'quickadd':
0177 $quick_install = request_var('quick_install', '');
0178
0179 if (confirm_box(true))
0180 {
0181 if (!$quick_install || strpos($quick_install, '::') === false)
0182 {
0183 break;
0184 }
0185
0186 list($module_basename, $module_mode) = explode('::', $quick_install);
0187
0188 // Check if module name and mode exist...
0189 $fileinfo = $this->get_module_infos($module_basename);
0190 $fileinfo = $fileinfo[$module_basename];
0191
0192 if (isset($fileinfo['modes'][$module_mode]))
0193 {
0194 $module_data = array(
0195 'module_basename' => $module_basename,
0196 'module_enabled' => 0,
0197 'module_display' => (isset($fileinfo['modes'][$module_mode]['display'])) ? $fileinfo['modes'][$module_mode]['display'] : 1,
0198 'parent_id' => $this->parent_id,
0199 'module_class' => $this->module_class,
0200 'module_langname' => $fileinfo['modes'][$module_mode]['title'],
0201 'module_mode' => $module_mode,
0202 'module_auth' => $fileinfo['modes'][$module_mode]['auth'],
0203 );
0204
0205 $errors = $this->update_module_data($module_data);
0206
0207 if (!sizeof($errors))
0208 {
0209 $this->remove_cache_file();
0210
0211 trigger_error($user->lang['MODULE_ADDED'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
0212 }
0213 }
0214 }
0215 else
0216 {
0217 confirm_box(false, 'ADD_MODULE', build_hidden_fields(array(
0218 'i' => $id,
0219 'mode' => $mode,
0220 'parent_id' => $this->parent_id,
0221 'action' => 'quickadd',
0222 'quick_install' => $quick_install,
0223 )));
0224 }
0225
0226 break;
0227
0228 case 'edit':
0229
0230 if (!$module_id)
0231 {
0232 trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
0233 }
0234
0235 $module_row = $this->get_module_row($module_id);
0236
0237 // no break
0238
0239 case 'add':
0240
0241 if ($action == 'add')
0242 {
0243 $module_row = array(
0244 'module_basename' => '',
0245 'module_enabled' => 0,
0246 'module_display' => 1,
0247 'parent_id' => 0,
0248 'module_langname' => utf8_normalize_nfc(request_var('module_langname', '', true)),
0249 'module_mode' => '',
0250 'module_auth' => '',
0251 );
0252 }
0253
0254 $module_data = array();
0255
0256 $module_data['module_basename'] = request_var('module_basename', (string) $module_row['module_basename']);
0257 $module_data['module_enabled'] = request_var('module_enabled', (int) $module_row['module_enabled']);
0258 $module_data['module_display'] = request_var('module_display', (int) $module_row['module_display']);
0259 $module_data['parent_id'] = request_var('module_parent_id', (int) $module_row['parent_id']);
0260 $module_data['module_class'] = $this->module_class;
0261 $module_data['module_langname'] = utf8_normalize_nfc(request_var('module_langname', (string) $module_row['module_langname'], true));
0262 $module_data['module_mode'] = request_var('module_mode', (string) $module_row['module_mode']);
0263
0264 $submit = (isset($_POST['submit'])) ? true : false;
0265
0266 if ($submit)
0267 {
0268 if (!$module_data['module_langname'])
0269 {
0270 trigger_error($user->lang['NO_MODULE_LANGNAME'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
0271 }
0272
0273 $module_type = request_var('module_type', 'category');
0274
0275 if ($module_type == 'category')
0276 {
0277 $module_data['module_basename'] = $module_data['module_mode'] = $module_data['module_auth'] = '';
0278 $module_data['module_display'] = 1;
0279 }
0280
0281 if ($action == 'edit')
0282 {
0283 $module_data['module_id'] = $module_id;
0284 }
0285
0286 // Adjust auth row
0287 if ($module_data['module_basename'] && $module_data['module_mode'])
0288 {
0289 $fileinfo = $this->get_module_infos($module_data['module_basename']);
0290 $module_data['module_auth'] = $fileinfo[$module_data['module_basename']]['modes'][$module_data['module_mode']]['auth'];
0291 }
0292
0293 $errors = $this->update_module_data($module_data);
0294
0295 if (!sizeof($errors))
0296 {
0297 $this->remove_cache_file();
0298
0299 trigger_error((($action == 'add') ? $user->lang['MODULE_ADDED'] : $user->lang['MODULE_EDITED']) . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
0300 }
0301 }
0302
0303 // Category/not category?
0304 $is_cat = (!$module_data['module_basename']) ? true : false;
0305
0306 // Get module information
0307 $module_infos = $this->get_module_infos();
0308
0309 // Build name options
0310 $s_name_options = $s_mode_options = '';
0311 foreach ($module_infos as $option => $values)
0312 {
0313 if (!$module_data['module_basename'])
0314 {
0315 $module_data['module_basename'] = $option;
0316 }
0317
0318 // Name options
0319 $s_name_options .= '<option value="' . $option . '"' . (($option == $module_data['module_basename']) ? ' selected="selected"' : '') . '>' . $this->lang_name($values['title']) . ' [' . $this->module_class . '_' . $option . ']</option>';
0320
0321 $template->assign_block_vars('m_names', array('NAME' => $option, 'A_NAME' => addslashes($option)));
0322
0323 // Build module modes
0324 foreach ($values['modes'] as $m_mode => $m_values)
0325 {
0326 if ($option == $module_data['module_basename'])
0327 {
0328 $s_mode_options .= '<option value="' . $m_mode . '"' . (($m_mode == $module_data['module_mode']) ? ' selected="selected"' : '') . '>' . $this->lang_name($m_values['title']) . '</option>';
0329 }
0330
0331 $template->assign_block_vars('m_names.modes', array(
0332 'OPTION' => $m_mode,
0333 'VALUE' => $this->lang_name($m_values['title']),
0334 'A_OPTION' => addslashes($m_mode),
0335 'A_VALUE' => addslashes($this->lang_name($m_values['title'])))
0336 );
0337 }
0338 }
0339
0340 $s_cat_option = '<option value="0"' . (($module_data['parent_id'] == 0) ? ' selected="selected"' : '') . '>' . $user->lang['NO_PARENT'] . '</option>';
0341
0342 $template->assign_vars(array_merge(array(
0343 'S_EDIT_MODULE' => true,
0344 'S_IS_CAT' => $is_cat,
0345 '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),
0346 'S_MODULE_NAMES' => $s_name_options,
0347 'S_MODULE_MODES' => $s_mode_options,
0348 'U_BACK' => $this->u_action . '&parent_id=' . $this->parent_id,
0349 'U_EDIT_ACTION' => $this->u_action . '&parent_id=' . $this->parent_id,
0350
0351 'L_TITLE' => $user->lang[strtoupper($action) . '_MODULE'],
0352
0353 'MODULENAME' => $this->lang_name($module_data['module_langname']),
0354 'ACTION' => $action,
0355 'MODULE_ID' => $module_id,
0356
0357 ),
0358 array_change_key_case($module_data, CASE_UPPER))
0359 );
0360
0361 if (sizeof($errors))
0362 {
0363 $template->assign_vars(array(
0364 'S_ERROR' => true,
0365 'ERROR_MSG' => implode('<br />', $errors))
0366 );
0367 }
0368
0369 return;
0370
0371 break;
0372 }
0373
0374 // Default management page
0375 if (sizeof($errors))
0376 {
0377 $template->assign_vars(array(
0378 'S_ERROR' => true,
0379 'ERROR_MSG' => implode('<br />', $errors))
0380 );
0381 }
0382
0383 if (!$this->parent_id)
0384 {
0385 $navigation = strtoupper($this->module_class);
0386 }
0387 else
0388 {
0389 $navigation = '<a href="' . $this->u_action . '">' . strtoupper($this->module_class) . '</a>';
0390
0391 $modules_nav = $this->get_module_branch($this->parent_id, 'parents', 'descending');
0392
0393 foreach ($modules_nav as $row)
0394 {
0395 $langname = $this->lang_name($row['module_langname']);
0396
0397 if ($row['module_id'] == $this->parent_id)
0398 {
0399 $navigation .= ' -> ' . $langname;
0400 }
0401 else
0402 {
0403 $navigation .= ' -> <a href="' . $this->u_action . '&parent_id=' . $row['module_id'] . '">' . $langname . '</a>';
0404 }
0405 }
0406 }
0407
0408 // Jumpbox
0409 $module_box = $this->make_module_select($this->parent_id, false, false, false, false);
0410
0411 $sql = 'SELECT *
0412 FROM ' . MODULES_TABLE . "
0413 WHERE parent_id = {$this->parent_id}
0414 AND module_class = '" . $db->sql_escape($this->module_class) . "'
0415 ORDER BY left_id";
0416 $result = $db->sql_query($sql);
0417
0418 if ($row = $db->sql_fetchrow($result))
0419 {
0420 do
0421 {
0422 $langname = $this->lang_name($row['module_langname']);
0423
0424 if (!$row['module_enabled'])
0425 {
0426 $module_image = '<img src="images/icon_folder_lock.gif" alt="' . $user->lang['DEACTIVATED_MODULE'] .'" />';
0427 }
0428 else
0429 {
0430 $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'] . '" />';
0431 }
0432
0433 $url = $this->u_action . '&parent_id=' . $this->parent_id . '&m=' . $row['module_id'];
0434
0435 $template->assign_block_vars('modules', array(
0436 'MODULE_IMAGE' => $module_image,
0437 'MODULE_TITLE' => $langname,
0438 'MODULE_ENABLED' => ($row['module_enabled']) ? true : false,
0439 'MODULE_DISPLAYED' => ($row['module_display']) ? true : false,
0440
0441 'S_ACP_CAT_SYSTEM' => ($this->module_class == 'acp' && $row['module_langname'] == 'ACP_CAT_SYSTEM') ? true : false,
0442 'S_ACP_MODULE_MANAGEMENT' => ($this->module_class == 'acp' && ($row['module_basename'] == 'modules' || $row['module_langname'] == 'ACP_MODULE_MANAGEMENT')) ? true : false,
0443
0444 'U_MODULE' => $this->u_action . '&parent_id=' . $row['module_id'],
0445 'U_MOVE_UP' => $url . '&action=move_up',
0446 'U_MOVE_DOWN' => $url . '&action=move_down',
0447 'U_EDIT' => $url . '&action=edit',
0448 'U_DELETE' => $url . '&action=delete',
0449 'U_ENABLE' => $url . '&action=enable',
0450 'U_DISABLE' => $url . '&action=disable')
0451 );
0452 }
0453 while ($row = $db->sql_fetchrow($result));
0454 }
0455 else if ($this->parent_id)
0456 {
0457 $row = $this->get_module_row($this->parent_id);
0458
0459 $url = $this->u_action . '&parent_id=' . $this->parent_id . '&m=' . $row['module_id'];
0460
0461 $template->assign_vars(array(
0462 'S_NO_MODULES' => true,
0463 'MODULE_TITLE' => $langname,
0464 'MODULE_ENABLED' => ($row['module_enabled']) ? true : false,
0465 'MODULE_DISPLAYED' => ($row['module_display']) ? true : false,
0466
0467 'U_EDIT' => $url . '&action=edit',
0468 'U_DELETE' => $url . '&action=delete',
0469 'U_ENABLE' => $url . '&action=enable',
0470 'U_DISABLE' => $url . '&action=disable')
0471 );
0472 }
0473 $db->sql_freeresult($result);
0474
0475 // Quick adding module
0476 $module_infos = $this->get_module_infos();
0477
0478 // Build quick options
0479 $s_install_options = '';
0480 foreach ($module_infos as $option => $values)
0481 {
0482 // Name options
0483 $s_install_options .= '<optgroup label="' . $this->lang_name($values['title']) . ' [' . $this->module_class . '_' . $option . ']">';
0484
0485 // Build module modes
0486 foreach ($values['modes'] as $m_mode => $m_values)
0487 {
0488 $s_install_options .= '<option value="' . $option . '::' . $m_mode . '"> ' . $this->lang_name($m_values['title']) . '</option>';
0489 }
0490
0491 $s_install_options .= '</optgroup>';
0492 }
0493
0494 $template->assign_vars(array(
0495 'U_SEL_ACTION' => $this->u_action,
0496 'U_ACTION' => $this->u_action . '&parent_id=' . $this->parent_id,
0497 'NAVIGATION' => $navigation,
0498 'MODULE_BOX' => $module_box,
0499 'PARENT_ID' => $this->parent_id,
0500 'S_INSTALL_OPTIONS' => $s_install_options,
0501 )
0502 );
0503 }
0504
0505 /**
0506 * Get row for specified module
0507 */
0508 function get_module_row($module_id)
0509 {
0510 global $db, $user;
0511
0512 $sql = 'SELECT *
0513 FROM ' . MODULES_TABLE . "
0514 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
0515 AND module_id = $module_id";
0516 $result = $db->sql_query($sql);
0517 $row = $db->sql_fetchrow($result);
0518 $db->sql_freeresult($result);
0519
0520 if (!$row)
0521 {
0522 trigger_error($user->lang['NO_MODULE'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
0523 }
0524
0525 return $row;
0526 }
0527
0528 /**
0529 * Get available module information from module files
0530 */
0531 function get_module_infos($module = '', $module_class = false)
0532 {
0533 global $phpbb_root_path, $phpEx;
0534
0535 $module_class = ($module_class === false) ? $this->module_class : $module_class;
0536
0537 $directory = $phpbb_root_path . 'includes/' . $module_class . '/info/';
0538 $fileinfo = array();
0539
0540 if (!$module)
0541 {
0542 $dh = @opendir($directory);
0543
0544 if (!$dh)
0545 {
0546 return $fileinfo;
0547 }
0548
0549 while (($file = readdir($dh)) !== false)
0550 {
0551 // Is module?
0552 if (preg_match('/^' . $module_class . '_.+\.' . $phpEx . '$/', $file))
0553 {
0554 $class = str_replace(".$phpEx", '', $file) . '_info';
0555
0556 if (!class_exists($class))
0557 {
0558 include($directory . $file);
0559 }
0560
0561 // Get module title tag
0562 if (class_exists($class))
0563 {
0564 $c_class = new $class();
0565 $module_info = $c_class->module();
0566 $fileinfo[str_replace($module_class . '_', '', $module_info['filename'])] = $module_info;
0567 }
0568 }
0569 }
0570 closedir($dh);
0571
0572 ksort($fileinfo);
0573 }
0574 else
0575 {
0576 $filename = $module_class . '_' . basename($module);
0577 $class = $module_class . '_' . basename($module) . '_info';
0578
0579 if (!class_exists($class))
0580 {
0581 include($directory . $filename . '.' . $phpEx);
0582 }
0583
0584 // Get module title tag
0585 if (class_exists($class))
0586 {
0587 $c_class = new $class();
0588 $module_info = $c_class->module();
0589 $fileinfo[str_replace($module_class . '_', '', $module_info['filename'])] = $module_info;
0590 }
0591 }
0592
0593 return $fileinfo;
0594 }
0595
0596 /**
0597 * Simple version of jumpbox, just lists modules
0598 */
0599 function make_module_select($select_id = false, $ignore_id = false, $ignore_acl = false, $ignore_nonpost = false, $ignore_emptycat = true, $ignore_noncat = false)
0600 {
0601 global $db, $user, $auth, $config;
0602
0603 $sql = 'SELECT module_id, module_enabled, module_basename, parent_id, module_langname, left_id, right_id, module_auth
0604 FROM ' . MODULES_TABLE . "
0605 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
0606 ORDER BY left_id ASC";
0607 $result = $db->sql_query($sql);
0608
0609 $right = $iteration = 0;
0610 $padding_store = array('0' => '');
0611 $module_list = $padding = '';
0612
0613 while ($row = $db->sql_fetchrow($result))
0614 {
0615 if ($row['left_id'] < $right)
0616 {
0617 $padding .= ' ';
0618 $padding_store[$row['parent_id']] = $padding;
0619 }
0620 else if ($row['left_id'] > $right + 1)
0621 {
0622 $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
0623 }
0624
0625 $right = $row['right_id'];
0626
0627 if (!$ignore_acl && $row['module_auth'])
0628 {
0629 // We use zero as the forum id to check - global setting.
0630 if (!p_master::module_auth($row['module_auth'], 0))
0631 {
0632 continue;
0633 }
0634 }
0635
0636 // ignore this module?
0637 if ((is_array($ignore_id) && in_array($row['module_id'], $ignore_id)) || $row['module_id'] == $ignore_id)
0638 {
0639 continue;
0640 }
0641
0642 // empty category
0643 if (!$row['module_basename'] && ($row['left_id'] + 1 == $row['right_id']) && $ignore_emptycat)
0644 {
0645 continue;
0646 }
0647
0648 // ignore non-category?
0649 if ($row['module_basename'] && $ignore_noncat)
0650 {
0651 continue;
0652 }
0653
0654 $selected = (is_array($select_id)) ? ((in_array($row['module_id'], $select_id)) ? ' selected="selected"' : '') : (($row['module_id'] == $select_id) ? ' selected="selected"' : '');
0655
0656 $langname = $this->lang_name($row['module_langname']);
0657 $module_list .= '<option value="' . $row['module_id'] . '"' . $selected . ((!$row['module_enabled']) ? ' class="disabled"' : '') . '>' . $padding . $langname . '</option>';
0658
0659 $iteration++;
0660 }
0661 unset($padding_store);
0662
0663 return $module_list;
0664 }
0665
0666 /**
0667 * Get module branch
0668 */
0669 function get_module_branch($module_id, $type = 'all', $order = 'descending', $include_module = true)
0670 {
0671 global $db;
0672
0673 switch ($type)
0674 {
0675 case 'parents':
0676 $condition = 'm1.left_id BETWEEN m2.left_id AND m2.right_id';
0677 break;
0678
0679 case 'children':
0680 $condition = 'm2.left_id BETWEEN m1.left_id AND m1.right_id';
0681 break;
0682
0683 default:
0684 $condition = 'm2.left_id BETWEEN m1.left_id AND m1.right_id OR m1.left_id BETWEEN m2.left_id AND m2.right_id';
0685 break;
0686 }
0687
0688 $rows = array();
0689
0690 $sql = 'SELECT m2.*
0691 FROM ' . MODULES_TABLE . ' m1
0692 LEFT JOIN ' . MODULES_TABLE . " m2 ON ($condition)
0693 WHERE m1.module_class = '" . $db->sql_escape($this->module_class) . "'
0694 AND m2.module_class = '" . $db->sql_escape($this->module_class) . "'
0695 AND m1.module_id = $module_id
0696 ORDER BY m2.left_id " . (($order == 'descending') ? 'ASC' : 'DESC');
0697 $result = $db->sql_query($sql);
0698
0699 while ($row = $db->sql_fetchrow($result))
0700 {
0701 if (!$include_module && $row['module_id'] == $module_id)
0702 {
0703 continue;
0704 }
0705
0706 $rows[] = $row;
0707 }
0708 $db->sql_freeresult($result);
0709
0710 return $rows;
0711 }
0712
0713 /**
0714 * Remove modules cache file
0715 */
0716 function remove_cache_file()
0717 {
0718 global $cache;
0719
0720 // Sanitise for future path use, it's escaped as appropriate for queries
0721 $p_class = str_replace(array('.', '/', '\\'), '', basename($this->module_class));
0722
0723 $cache->destroy('_modules_' . $p_class);
0724
0725 // Additionally remove sql cache
0726 $cache->destroy('sql', MODULES_TABLE);
0727 }
0728
0729 /**
0730 * Return correct language name
0731 */
0732 function lang_name($module_langname)
0733 {
0734 global $user;
0735
0736 return (!empty($user->lang[$module_langname])) ? $user->lang[$module_langname] : $module_langname;
0737 }
0738
0739 /**
0740 * Update/Add module
0741 *
0742 * @param bool $run_inline if set to true errors will be returned and no logs being written
0743 */
0744 function update_module_data(&$module_data, $run_inline = false)
0745 {
0746 global $db, $user;
0747
0748 if (!isset($module_data['module_id']))
0749 {
0750 // no module_id means we're creating a new category/module
0751 if ($module_data['parent_id'])
0752 {
0753 $sql = 'SELECT left_id, right_id
0754 FROM ' . MODULES_TABLE . "
0755 WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
0756 AND module_id = " . (int) $module_data['parent_id'];
0757 $result = $db->sql_query($sql);
0758 $row = $db->sql_fetchrow($result);
0759 $db->sql_freeresult($result);
0760
0761 if (!$row)
0762 {
0763 if ($run_inline)
0764 {
0765 return 'PARENT_NO_EXIST';
0766 }
0767
0768 trigger_error($user->lang['PARENT_NO_EXIST'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
0769 }
0770
0771 // Workaround
0772 $row['left_id'] = (int) $row['left_id'];
0773 $row['right_id'] = (int) $row['right_id'];
0774
0775 $sql = 'UPDATE ' . MODULES_TABLE . "
0776 SET left_id = left_id + 2, right_id = right_id + 2
0777 WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
0778 AND left_id > {$row['right_id']}";
0779 $db->sql_query($sql);
0780
0781 $sql = 'UPDATE ' . MODULES_TABLE . "
0782 SET right_id = right_id + 2
0783 WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
0784 AND {$row['left_id']} BETWEEN left_id AND right_id";
0785 $db->sql_query($sql);
0786
0787 $module_data['left_id'] = (int) $row['right_id'];
0788 $module_data['right_id'] = (int) $row['right_id'] + 1;
0789 }
0790 else
0791 {
0792 $sql = 'SELECT MAX(right_id) AS right_id
0793 FROM ' . MODULES_TABLE . "
0794 WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'";
0795 $result = $db->sql_query($sql);
0796 $row = $db->sql_fetchrow($result);
0797 $db->sql_freeresult($result);
0798
0799 $module_data['left_id'] = (int) $row['right_id'] + 1;
0800 $module_data['right_id'] = (int) $row['right_id'] + 2;
0801 }
0802
0803 $sql = 'INSERT INTO ' . MODULES_TABLE . ' ' . $db->sql_build_array('INSERT', $module_data);
0804 $db->sql_query($sql);
0805
0806 $module_data['module_id'] = $db->sql_nextid();
0807
0808 if (!$run_inline)
0809 {
0810 add_log('admin', 'LOG_MODULE_ADD', $this->lang_name($module_data['module_langname']));
0811 }
0812 }
0813 else
0814 {
0815 $row = $this->get_module_row($module_data['module_id']);
0816
0817 if ($module_data['module_basename'] && !$row['module_basename'])
0818 {
0819 // we're turning a category into a module
0820 $branch = $this->get_module_branch($module_data['module_id'], 'children', 'descending', false);
0821
0822 if (sizeof($branch))
0823 {
0824 return array($user->lang['NO_CATEGORY_TO_MODULE']);
0825 }
0826 }
0827
0828 if ($row['parent_id'] != $module_data['parent_id'])
0829 {
0830 $this->move_module($module_data['module_id'], $module_data['parent_id']);
0831 }
0832
0833 $update_ary = $module_data;
0834 unset($update_ary['module_id']);
0835
0836 $sql = 'UPDATE ' . MODULES_TABLE . '
0837 SET ' . $db->sql_build_array('UPDATE', $update_ary) . "
0838 WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
0839 AND module_id = " . (int) $module_data['module_id'];
0840 $db->sql_query($sql);
0841
0842 if (!$run_inline)
0843 {
0844 add_log('admin', 'LOG_MODULE_EDIT', $this->lang_name($module_data['module_langname']));
0845 }
0846 }
0847
0848 return array();
0849 }
0850
0851 /**
0852 * Move module around the tree
0853 */
0854 function move_module($from_module_id, $to_parent_id)
0855 {
0856 global $db;
0857
0858 $moved_modules = $this->get_module_branch($from_module_id, 'children', 'descending');
0859 $from_data = $moved_modules[0];
0860 $diff = sizeof($moved_modules) * 2;
0861
0862 $moved_ids = array();
0863 for ($i = 0; $i < sizeof($moved_modules); ++$i)
0864 {
0865 $moved_ids[] = $moved_modules[$i]['module_id'];
0866 }
0867
0868 // Resync parents
0869 $sql = 'UPDATE ' . MODULES_TABLE . "
0870 SET right_id = right_id - $diff
0871 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
0872 AND left_id < " . (int) $from_data['right_id'] . '
0873 AND right_id > ' . (int) $from_data['right_id'];
0874 $db->sql_query($sql);
0875
0876 // Resync righthand side of tree
0877 $sql = 'UPDATE ' . MODULES_TABLE . "
0878 SET left_id = left_id - $diff, right_id = right_id - $diff
0879 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
0880 AND left_id > " . (int) $from_data['right_id'];
0881 $db->sql_query($sql);
0882
0883 if ($to_parent_id > 0)
0884 {
0885 $to_data = $this->get_module_row($to_parent_id);
0886
0887 // Resync new parents
0888 $sql = 'UPDATE ' . MODULES_TABLE . "
0889 SET right_id = right_id + $diff
0890 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
0891 AND " . (int) $to_data['right_id'] . ' BETWEEN left_id AND right_id
0892 AND ' . $db->sql_in_set('module_id', $moved_ids, true);
0893 $db->sql_query($sql);
0894
0895 // Resync the righthand side of the tree
0896 $sql = 'UPDATE ' . MODULES_TABLE . "
0897 SET left_id = left_id + $diff, right_id = right_id + $diff
0898 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
0899 AND left_id > " . (int) $to_data['right_id'] . '
0900 AND ' . $db->sql_in_set('module_id', $moved_ids, true);
0901 $db->sql_query($sql);
0902
0903 // Resync moved branch
0904 $to_data['right_id'] += $diff;
0905 if ($to_data['right_id'] > $from_data['right_id'])
0906 {
0907 $diff = '+ ' . ($to_data['right_id'] - $from_data['right_id'] - 1);
0908 }
0909 else
0910 {
0911 $diff = '- ' . abs($to_data['right_id'] - $from_data['right_id'] - 1);
0912 }
0913 }
0914 else
0915 {
0916 $sql = 'SELECT MAX(right_id) AS right_id
0917 FROM ' . MODULES_TABLE . "
0918 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
0919 AND " . $db->sql_in_set('module_id', $moved_ids, true);
0920 $result = $db->sql_query($sql);
0921 $row = $db->sql_fetchrow($result);
0922 $db->sql_freeresult($result);
0923
0924 $diff = '+ ' . (int) ($row['right_id'] - $from_data['left_id'] + 1);
0925 }
0926
0927 $sql = 'UPDATE ' . MODULES_TABLE . "
0928 SET left_id = left_id $diff, right_id = right_id $diff
0929 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
0930 AND " . $db->sql_in_set('module_id', $moved_ids);
0931 $db->sql_query($sql);
0932 }
0933
0934 /**
0935 * Remove module from tree
0936 */
0937 function delete_module($module_id)
0938 {
0939 global $db, $user;
0940
0941 $row = $this->get_module_row($module_id);
0942
0943 $branch = $this->get_module_branch($module_id, 'children', 'descending', false);
0944
0945 if (sizeof($branch))
0946 {
0947 return array($user->lang['CANNOT_REMOVE_MODULE']);
0948 }
0949
0950 // If not move
0951 $diff = 2;
0952 $sql = 'DELETE FROM ' . MODULES_TABLE . "
0953 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
0954 AND module_id = $module_id";
0955 $db->sql_query($sql);
0956
0957 $row['right_id'] = (int) $row['right_id'];
0958 $row['left_id'] = (int) $row['left_id'];
0959
0960 // Resync tree
0961 $sql = 'UPDATE ' . MODULES_TABLE . "
0962 SET right_id = right_id - $diff
0963 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
0964 AND left_id < {$row['right_id']} AND right_id > {$row['right_id']}";
0965 $db->sql_query($sql);
0966
0967 $sql = 'UPDATE ' . MODULES_TABLE . "
0968 SET left_id = left_id - $diff, right_id = right_id - $diff
0969 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
0970 AND left_id > {$row['right_id']}";
0971 $db->sql_query($sql);
0972
0973 add_log('admin', 'LOG_MODULE_REMOVED', $this->lang_name($row['module_langname']));
0974
0975 return array();
0976
0977 }
0978
0979 /**
0980 * Move module position by $steps up/down
0981 */
0982 function move_module_by($module_row, $action = 'move_up', $steps = 1)
0983 {
0984 global $db;
0985
0986 /**
0987 * Fetch all the siblings between the module's current spot
0988 * and where we want to move it to. If there are less than $steps
0989 * siblings between the current spot and the target then the
0990 * module will move as far as possible
0991 */
0992 $sql = 'SELECT module_id, left_id, right_id, module_langname
0993 FROM ' . MODULES_TABLE . "
0994 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
0995 AND parent_id = " . (int) $module_row['parent_id'] . '
0996 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');
0997 $result = $db->sql_query_limit($sql, $steps);
0998
0999 $target = array();
1000 while ($row = $db->sql_fetchrow($result))
1001 {
1002 $target = $row;
1003 }
1004 $db->sql_freeresult($result);
1005
1006 if (!sizeof($target))
1007 {
1008 // The module is already on top or bottom
1009 return false;
1010 }
1011
1012 /**
1013 * $left_id and $right_id define the scope of the nodes that are affected by the move.
1014 * $diff_up and $diff_down are the values to substract or add to each node's left_id
1015 * and right_id in order to move them up or down.
1016 * $move_up_left and $move_up_right define the scope of the nodes that are moving
1017 * up. Other nodes in the scope of ($left_id, $right_id) are considered to move down.
1018 */
1019 if ($action == 'move_up')
1020 {
1021 $left_id = (int) $target['left_id'];
1022 $right_id = (int) $module_row['right_id'];
1023
1024 $diff_up = (int) ($module_row['left_id'] - $target['left_id']);
1025 $diff_down = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);
1026
1027 $move_up_left = (int) $module_row['left_id'];
1028 $move_up_right = (int) $module_row['right_id'];
1029 }
1030 else
1031 {
1032 $left_id = (int) $module_row['left_id'];
1033 $right_id = (int) $target['right_id'];
1034
1035 $diff_up = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);
1036 $diff_down = (int) ($target['right_id'] - $module_row['right_id']);
1037
1038 $move_up_left = (int) ($module_row['right_id'] + 1);
1039 $move_up_right = (int) $target['right_id'];
1040 }
1041
1042 // Now do the dirty job
1043 $sql = 'UPDATE ' . MODULES_TABLE . "
1044 SET left_id = left_id + CASE
1045 WHEN left_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
1046 ELSE {$diff_down}
1047 END,
1048 right_id = right_id + CASE
1049 WHEN right_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
1050 ELSE {$diff_down}
1051 END
1052 WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
1053 AND left_id BETWEEN {$left_id} AND {$right_id}
1054 AND right_id BETWEEN {$left_id} AND {$right_id}";
1055 $db->sql_query($sql);
1056
1057 $this->remove_cache_file();
1058
1059 return $this->lang_name($target['module_langname']);
1060 }
1061 }
1062
1063 ?>