Blame Identity/Webenv/phpBB/3.0.4/includes/acp/acp_modules.php

ef5584
ef5584
/**
ef5584
*
ef5584
* @package acp
ef5584
* @version $Id: acp_modules.php 9179 2008-12-06 18:23:42Z toonarmy $
ef5584
* @copyright (c) 2005 phpBB Group
ef5584
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
ef5584
*
ef5584
*/
ef5584
ef5584
/**
ef5584
* @ignore
ef5584
*/
ef5584
if (!defined('IN_PHPBB'))
ef5584
{
ef5584
	exit;
ef5584
}
ef5584
ef5584
/**
ef5584
* - Able to check for new module versions (modes changed/adjusted/added/removed)
ef5584
* Icons for:
ef5584
* - module enabled and displayed (common)
ef5584
* - module enabled and not displayed
ef5584
* - module deactivated
ef5584
* - category (enabled)
ef5584
* - category disabled
ef5584
*/
ef5584
ef5584
/**
ef5584
* @package acp
ef5584
*/
ef5584
class acp_modules
ef5584
{
ef5584
	var $module_class = '';
ef5584
	var $parent_id;
ef5584
	var $u_action;
ef5584
ef5584
	function main($id, $mode)
ef5584
	{
ef5584
		global $db, $user, $auth, $template, $module;
ef5584
		global $config, $phpbb_admin_path, $phpbb_root_path, $phpEx;
ef5584
ef5584
		// Set a global define for modules we might include (the author is able to prevent execution of code by checking this constant)
ef5584
		define('MODULE_INCLUDE', true);
ef5584
ef5584
		$user->add_lang('acp/modules');
ef5584
		$this->tpl_name = 'acp_modules';
ef5584
ef5584
		// module class
ef5584
		$this->module_class = $mode;
ef5584
ef5584
		if ($this->module_class == 'ucp')
ef5584
		{
ef5584
			$user->add_lang('ucp');
ef5584
		}
ef5584
		else if ($this->module_class == 'mcp')
ef5584
		{
ef5584
			$user->add_lang('mcp');
ef5584
		}
ef5584
ef5584
		if ($module->p_class != $this->module_class)
ef5584
		{
ef5584
			$module->add_mod_info($this->module_class);
ef5584
		}
ef5584
ef5584
		$this->page_title = strtoupper($this->module_class);
ef5584
ef5584
		$this->parent_id = request_var('parent_id', 0);
ef5584
		$module_id = request_var('m', 0);
ef5584
		$action = request_var('action', '');
ef5584
		$errors = array();
ef5584
ef5584
		switch ($action)
ef5584
		{
ef5584
			case 'delete':
ef5584
				if (!$module_id)
ef5584
				{
ef5584
					trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
ef5584
				}
ef5584
ef5584
				if (confirm_box(true))
ef5584
				{
ef5584
					// Make sure we are not directly within a module
ef5584
					if ($module_id == $this->parent_id)
ef5584
					{
ef5584
						$sql = 'SELECT parent_id
ef5584
							FROM ' . MODULES_TABLE . '
ef5584
							WHERE module_id = ' . $module_id;
ef5584
						$result = $db->sql_query($sql);
ef5584
						$this->parent_id = (int) $db->sql_fetchfield('parent_id');
ef5584
						$db->sql_freeresult($result);
ef5584
					}
ef5584
ef5584
					$errors = $this->delete_module($module_id);
ef5584
ef5584
					if (!sizeof($errors))
ef5584
					{
ef5584
						$this->remove_cache_file();
ef5584
						trigger_error($user->lang['MODULE_DELETED'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
ef5584
					}
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					confirm_box(false, 'DELETE_MODULE', build_hidden_fields(array(
ef5584
						'i'			=> $id,
ef5584
						'mode'		=> $mode,
ef5584
						'parent_id'	=> $this->parent_id,
ef5584
						'module_id'	=> $module_id,
ef5584
						'action'	=> $action,
ef5584
					)));
ef5584
				}
ef5584
ef5584
			break;
ef5584
			
ef5584
			case 'enable':
ef5584
			case 'disable':
ef5584
				if (!$module_id)
ef5584
				{
ef5584
					trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
ef5584
				}
ef5584
ef5584
				$sql = 'SELECT *
ef5584
					FROM ' . MODULES_TABLE . "
ef5584
					WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
						AND module_id = $module_id";
ef5584
				$result = $db->sql_query($sql);
ef5584
				$row = $db->sql_fetchrow($result);
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
				if (!$row)
ef5584
				{
ef5584
					trigger_error($user->lang['NO_MODULE'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
ef5584
				}
ef5584
ef5584
				$sql = 'UPDATE ' . MODULES_TABLE . '
ef5584
					SET module_enabled = ' . (($action == 'enable') ? 1 : 0) . "
ef5584
					WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
						AND module_id = $module_id";
ef5584
				$db->sql_query($sql);
ef5584
ef5584
				add_log('admin', 'LOG_MODULE_' . strtoupper($action), $this->lang_name($row['module_langname']));
ef5584
				$this->remove_cache_file();
ef5584
ef5584
			break;
ef5584
ef5584
			case 'move_up':
ef5584
			case 'move_down':
ef5584
				if (!$module_id)
ef5584
				{
ef5584
					trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
ef5584
				}
ef5584
ef5584
				$sql = 'SELECT *
ef5584
					FROM ' . MODULES_TABLE . "
ef5584
					WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
						AND module_id = $module_id";
ef5584
				$result = $db->sql_query($sql);
ef5584
				$row = $db->sql_fetchrow($result);
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
				if (!$row)
ef5584
				{
ef5584
					trigger_error($user->lang['NO_MODULE'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
ef5584
				}
ef5584
ef5584
				$move_module_name = $this->move_module_by($row, $action, 1);
ef5584
ef5584
				if ($move_module_name !== false)
ef5584
				{
ef5584
					add_log('admin', 'LOG_MODULE_' . strtoupper($action), $this->lang_name($row['module_langname']), $move_module_name);
ef5584
					$this->remove_cache_file();
ef5584
				}
ef5584
		
ef5584
			break;
ef5584
ef5584
			case 'quickadd':
ef5584
				$quick_install = request_var('quick_install', '');
ef5584
ef5584
				if (confirm_box(true))
ef5584
				{
ef5584
					if (!$quick_install || strpos($quick_install, '::') === false)
ef5584
					{
ef5584
						break;
ef5584
					}
ef5584
ef5584
					list($module_basename, $module_mode) = explode('::', $quick_install);
ef5584
ef5584
					// Check if module name and mode exist...
ef5584
					$fileinfo = $this->get_module_infos($module_basename);
ef5584
					$fileinfo = $fileinfo[$module_basename];
ef5584
ef5584
					if (isset($fileinfo['modes'][$module_mode]))
ef5584
					{
ef5584
						$module_data = array(
ef5584
							'module_basename'	=> $module_basename,
ef5584
							'module_enabled'	=> 0,
ef5584
							'module_display'	=> (isset($fileinfo['modes'][$module_mode]['display'])) ? $fileinfo['modes'][$module_mode]['display'] : 1,
ef5584
							'parent_id'			=> $this->parent_id,
ef5584
							'module_class'		=> $this->module_class,
ef5584
							'module_langname'	=> $fileinfo['modes'][$module_mode]['title'],
ef5584
							'module_mode'		=> $module_mode,
ef5584
							'module_auth'		=> $fileinfo['modes'][$module_mode]['auth'],
ef5584
						);
ef5584
ef5584
						$errors = $this->update_module_data($module_data);
ef5584
ef5584
						if (!sizeof($errors))
ef5584
						{
ef5584
							$this->remove_cache_file();
ef5584
	
ef5584
							trigger_error($user->lang['MODULE_ADDED'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
ef5584
						}
ef5584
					}
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					confirm_box(false, 'ADD_MODULE', build_hidden_fields(array(
ef5584
						'i'			=> $id,
ef5584
						'mode'		=> $mode,
ef5584
						'parent_id'	=> $this->parent_id,
ef5584
						'action'	=> 'quickadd',
ef5584
						'quick_install'	=> $quick_install,
ef5584
					)));
ef5584
				}
ef5584
ef5584
			break;
ef5584
ef5584
			case 'edit':
ef5584
ef5584
				if (!$module_id)
ef5584
				{
ef5584
					trigger_error($user->lang['NO_MODULE_ID'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
ef5584
				}
ef5584
				
ef5584
				$module_row = $this->get_module_row($module_id);
ef5584
ef5584
			// no break
ef5584
ef5584
			case 'add':
ef5584
ef5584
				if ($action == 'add')
ef5584
				{
ef5584
					$module_row = array(
ef5584
						'module_basename'	=> '',
ef5584
						'module_enabled'	=> 0,
ef5584
						'module_display'	=> 1,
ef5584
						'parent_id'			=> 0,
ef5584
						'module_langname'	=> utf8_normalize_nfc(request_var('module_langname', '', true)),
ef5584
						'module_mode'		=> '',
ef5584
						'module_auth'		=> '',
ef5584
					);
ef5584
				}
ef5584
				
ef5584
				$module_data = array();
ef5584
ef5584
				$module_data['module_basename'] = request_var('module_basename', (string) $module_row['module_basename']);
ef5584
				$module_data['module_enabled'] = request_var('module_enabled', (int) $module_row['module_enabled']);
ef5584
				$module_data['module_display'] = request_var('module_display', (int) $module_row['module_display']);
ef5584
				$module_data['parent_id'] = request_var('module_parent_id', (int) $module_row['parent_id']);
ef5584
				$module_data['module_class'] = $this->module_class;
ef5584
				$module_data['module_langname'] = utf8_normalize_nfc(request_var('module_langname', (string) $module_row['module_langname'], true));
ef5584
				$module_data['module_mode'] = request_var('module_mode', (string) $module_row['module_mode']);
ef5584
ef5584
				$submit = (isset($_POST['submit'])) ? true : false;
ef5584
ef5584
				if ($submit)
ef5584
				{
ef5584
					if (!$module_data['module_langname'])
ef5584
					{
ef5584
						trigger_error($user->lang['NO_MODULE_LANGNAME'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
ef5584
					}
ef5584
ef5584
					$module_type = request_var('module_type', 'category');
ef5584
ef5584
					if ($module_type == 'category')
ef5584
					{
ef5584
						$module_data['module_basename'] = $module_data['module_mode'] = $module_data['module_auth'] = '';
ef5584
						$module_data['module_display'] = 1;
ef5584
					}
ef5584
ef5584
					if ($action == 'edit')
ef5584
					{
ef5584
						$module_data['module_id'] = $module_id;
ef5584
					}
ef5584
ef5584
					// Adjust auth row
ef5584
					if ($module_data['module_basename'] && $module_data['module_mode'])
ef5584
					{
ef5584
						$fileinfo = $this->get_module_infos($module_data['module_basename']);
ef5584
						$module_data['module_auth'] = $fileinfo[$module_data['module_basename']]['modes'][$module_data['module_mode']]['auth'];
ef5584
					}
ef5584
ef5584
					$errors = $this->update_module_data($module_data);
ef5584
ef5584
					if (!sizeof($errors))
ef5584
					{
ef5584
						$this->remove_cache_file();
ef5584
	
ef5584
						trigger_error((($action == 'add') ? $user->lang['MODULE_ADDED'] : $user->lang['MODULE_EDITED']) . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id));
ef5584
					}
ef5584
				}
ef5584
ef5584
				// Category/not category?
ef5584
				$is_cat = (!$module_data['module_basename']) ? true : false;
ef5584
ef5584
				// Get module information
ef5584
				$module_infos = $this->get_module_infos();
ef5584
ef5584
				// Build name options
ef5584
				$s_name_options = $s_mode_options = '';
ef5584
				foreach ($module_infos as $option => $values)
ef5584
				{
ef5584
					if (!$module_data['module_basename'])
ef5584
					{
ef5584
						$module_data['module_basename'] = $option;
ef5584
					}
ef5584
ef5584
					// Name options
ef5584
					$s_name_options .= '<option value="' . $option . '"' . (($option == $module_data['module_basename']) ? ' selected="selected"' : '') . '>' . $this->lang_name($values['title']) . ' [' . $this->module_class . '_' . $option . ']</option>';
ef5584
ef5584
					$template->assign_block_vars('m_names', array('NAME' => $option, 'A_NAME' => addslashes($option)));
ef5584
ef5584
					// Build module modes
ef5584
					foreach ($values['modes'] as $m_mode => $m_values)
ef5584
					{
ef5584
						if ($option == $module_data['module_basename'])
ef5584
						{
ef5584
							$s_mode_options .= '<option value="' . $m_mode . '"' . (($m_mode == $module_data['module_mode']) ? ' selected="selected"' : '') . '>' . $this->lang_name($m_values['title']) . '</option>';
ef5584
						}
ef5584
						
ef5584
						$template->assign_block_vars('m_names.modes', array(
ef5584
							'OPTION'		=> $m_mode,
ef5584
							'VALUE'			=> $this->lang_name($m_values['title']),
ef5584
							'A_OPTION'		=> addslashes($m_mode),
ef5584
							'A_VALUE'		=> addslashes($this->lang_name($m_values['title'])))
ef5584
						);
ef5584
					}
ef5584
				}
ef5584
				
ef5584
				$s_cat_option = '<option value="0"' . (($module_data['parent_id'] == 0) ? ' selected="selected"' : '') . '>' . $user->lang['NO_PARENT'] . '</option>';
ef5584
ef5584
				$template->assign_vars(array_merge(array(
ef5584
					'S_EDIT_MODULE'		=> true,
ef5584
					'S_IS_CAT'			=> $is_cat,
ef5584
					'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),
ef5584
					'S_MODULE_NAMES'	=> $s_name_options,
ef5584
					'S_MODULE_MODES'	=> $s_mode_options,
ef5584
					'U_BACK'			=> $this->u_action . '&parent_id=' . $this->parent_id,
ef5584
					'U_EDIT_ACTION'		=> $this->u_action . '&parent_id=' . $this->parent_id,
ef5584
ef5584
					'L_TITLE'			=> $user->lang[strtoupper($action) . '_MODULE'],
ef5584
					
ef5584
					'MODULENAME'		=> $this->lang_name($module_data['module_langname']),
ef5584
					'ACTION'			=> $action,
ef5584
					'MODULE_ID'			=> $module_id,
ef5584
ef5584
				),
ef5584
					array_change_key_case($module_data, CASE_UPPER))
ef5584
				);
ef5584
ef5584
				if (sizeof($errors))
ef5584
				{
ef5584
					$template->assign_vars(array(
ef5584
						'S_ERROR'	=> true,
ef5584
						'ERROR_MSG'	=> implode('
', $errors))
ef5584
					);
ef5584
				}
ef5584
ef5584
				return;
ef5584
ef5584
			break;
ef5584
		}
ef5584
ef5584
		// Default management page
ef5584
		if (sizeof($errors))
ef5584
		{
ef5584
			$template->assign_vars(array(
ef5584
				'S_ERROR'	=> true,
ef5584
				'ERROR_MSG'	=> implode('
', $errors))
ef5584
			);
ef5584
		}
ef5584
ef5584
		if (!$this->parent_id)
ef5584
		{
ef5584
			$navigation = strtoupper($this->module_class);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$navigation = '' . strtoupper($this->module_class) . '';
ef5584
ef5584
			$modules_nav = $this->get_module_branch($this->parent_id, 'parents', 'descending');
ef5584
ef5584
			foreach ($modules_nav as $row)
ef5584
			{
ef5584
				$langname = $this->lang_name($row['module_langname']);
ef5584
ef5584
				if ($row['module_id'] == $this->parent_id)
ef5584
				{
ef5584
					$navigation .= ' -> ' . $langname;
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					$navigation .= ' -> ' . $langname . '';
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
ef5584
		// Jumpbox
ef5584
		$module_box = $this->make_module_select($this->parent_id, false, false, false, false);
ef5584
ef5584
		$sql = 'SELECT *
ef5584
			FROM ' . MODULES_TABLE . "
ef5584
			WHERE parent_id = {$this->parent_id}
ef5584
				AND module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
			ORDER BY left_id";
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		if ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			do
ef5584
			{
ef5584
				$langname = $this->lang_name($row['module_langname']);
ef5584
ef5584
				if (!$row['module_enabled'])
ef5584
				{
ef5584
					$module_image = '' . $user->lang['DEACTIVATED_MODULE'] .'';
ef5584
				}
ef5584
				else
ef5584
				{
ef5584
					$module_image = (!$row['module_basename'] || $row['left_id'] + 1 != $row['right_id']) ? '' . $user->lang['CATEGORY'] . '' : '' . $user->lang['MODULE'] . '';
ef5584
				}
ef5584
ef5584
				$url = $this->u_action . '&parent_id=' . $this->parent_id . '&m=' . $row['module_id'];
ef5584
ef5584
				$template->assign_block_vars('modules', array(
ef5584
					'MODULE_IMAGE'		=> $module_image,
ef5584
					'MODULE_TITLE'		=> $langname,
ef5584
					'MODULE_ENABLED'	=> ($row['module_enabled']) ? true : false,
ef5584
					'MODULE_DISPLAYED'	=> ($row['module_display']) ? true : false,
ef5584
ef5584
					'S_ACP_CAT_SYSTEM'			=> ($this->module_class == 'acp' && $row['module_langname'] == 'ACP_CAT_SYSTEM') ? true : false,
ef5584
					'S_ACP_MODULE_MANAGEMENT'	=> ($this->module_class == 'acp' && ($row['module_basename'] == 'modules' || $row['module_langname'] == 'ACP_MODULE_MANAGEMENT')) ? true : false,
ef5584
ef5584
					'U_MODULE'			=> $this->u_action . '&parent_id=' . $row['module_id'],
ef5584
					'U_MOVE_UP'			=> $url . '&action=move_up',
ef5584
					'U_MOVE_DOWN'		=> $url . '&action=move_down',
ef5584
					'U_EDIT'			=> $url . '&action=edit',
ef5584
					'U_DELETE'			=> $url . '&action=delete',
ef5584
					'U_ENABLE'			=> $url . '&action=enable',
ef5584
					'U_DISABLE'			=> $url . '&action=disable')
ef5584
				);
ef5584
			}
ef5584
			while ($row = $db->sql_fetchrow($result));
ef5584
		}
ef5584
		else if ($this->parent_id)
ef5584
		{
ef5584
			$row = $this->get_module_row($this->parent_id);
ef5584
ef5584
			$url = $this->u_action . '&parent_id=' . $this->parent_id . '&m=' . $row['module_id'];
ef5584
ef5584
			$template->assign_vars(array(
ef5584
				'S_NO_MODULES'		=> true,
ef5584
				'MODULE_TITLE'		=> $langname,
ef5584
				'MODULE_ENABLED'	=> ($row['module_enabled']) ? true : false,
ef5584
				'MODULE_DISPLAYED'	=> ($row['module_display']) ? true : false,
ef5584
ef5584
				'U_EDIT'			=> $url . '&action=edit',
ef5584
				'U_DELETE'			=> $url . '&action=delete',
ef5584
				'U_ENABLE'			=> $url . '&action=enable',
ef5584
				'U_DISABLE'			=> $url . '&action=disable')
ef5584
			);
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		// Quick adding module
ef5584
		$module_infos = $this->get_module_infos();
ef5584
ef5584
		// Build quick options
ef5584
		$s_install_options = '';
ef5584
		foreach ($module_infos as $option => $values)
ef5584
		{
ef5584
			// Name options
ef5584
			$s_install_options .= '<optgroup label="' . $this->lang_name($values['title']) . ' [' . $this->module_class . '_' . $option . ']">';
ef5584
ef5584
			// Build module modes
ef5584
			foreach ($values['modes'] as $m_mode => $m_values)
ef5584
			{
ef5584
				$s_install_options .= '<option value="' . $option . '::' . $m_mode . '">   ' . $this->lang_name($m_values['title']) . '</option>';
ef5584
			}
ef5584
ef5584
			$s_install_options .= '</optgroup>';
ef5584
		}
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'U_SEL_ACTION'		=> $this->u_action,
ef5584
			'U_ACTION'			=> $this->u_action . '&parent_id=' . $this->parent_id,
ef5584
			'NAVIGATION'		=> $navigation,
ef5584
			'MODULE_BOX'		=> $module_box,
ef5584
			'PARENT_ID'			=> $this->parent_id,
ef5584
			'S_INSTALL_OPTIONS'	=> $s_install_options,
ef5584
			)
ef5584
		);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Get row for specified module
ef5584
	*/
ef5584
	function get_module_row($module_id)
ef5584
	{
ef5584
		global $db, $user;
ef5584
ef5584
		$sql = 'SELECT *
ef5584
			FROM ' . MODULES_TABLE . "
ef5584
			WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
				AND module_id = $module_id";
ef5584
		$result = $db->sql_query($sql);
ef5584
		$row = $db->sql_fetchrow($result);
ef5584
		$db->sql_freeresult($result);
ef5584
		
ef5584
		if (!$row)
ef5584
		{
ef5584
			trigger_error($user->lang['NO_MODULE'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
ef5584
		}
ef5584
ef5584
		return $row;
ef5584
	}
ef5584
	
ef5584
	/**
ef5584
	* Get available module information from module files
ef5584
	*/
ef5584
	function get_module_infos($module = '', $module_class = false)
ef5584
	{
ef5584
		global $phpbb_root_path, $phpEx;
ef5584
		
ef5584
		$module_class = ($module_class === false) ? $this->module_class : $module_class;
ef5584
ef5584
		$directory = $phpbb_root_path . 'includes/' . $module_class . '/info/';
ef5584
		$fileinfo = array();
ef5584
ef5584
		if (!$module)
ef5584
		{
ef5584
			$dh = @opendir($directory);
ef5584
ef5584
			if (!$dh)
ef5584
			{
ef5584
				return $fileinfo;
ef5584
			}
ef5584
ef5584
			while (($file = readdir($dh)) !== false)
ef5584
			{
ef5584
				// Is module?
ef5584
				if (preg_match('/^' . $module_class . '_.+\.' . $phpEx . '$/', $file))
ef5584
				{
ef5584
					$class = str_replace(".$phpEx", '', $file) . '_info';
ef5584
ef5584
					if (!class_exists($class))
ef5584
					{
ef5584
						include($directory . $file);
ef5584
					}
ef5584
ef5584
					// Get module title tag
ef5584
					if (class_exists($class))
ef5584
					{
ef5584
						$c_class = new $class();
ef5584
						$module_info = $c_class->module();
ef5584
						$fileinfo[str_replace($module_class . '_', '', $module_info['filename'])] = $module_info;
ef5584
					}
ef5584
				}
ef5584
			}
ef5584
			closedir($dh);
ef5584
ef5584
			ksort($fileinfo);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$filename = $module_class . '_' . basename($module);
ef5584
			$class = $module_class . '_' . basename($module) . '_info';
ef5584
ef5584
			if (!class_exists($class))
ef5584
			{
ef5584
				include($directory . $filename . '.' . $phpEx);
ef5584
			}
ef5584
ef5584
			// Get module title tag
ef5584
			if (class_exists($class))
ef5584
			{
ef5584
				$c_class = new $class();
ef5584
				$module_info = $c_class->module();
ef5584
				$fileinfo[str_replace($module_class . '_', '', $module_info['filename'])] = $module_info;
ef5584
			}
ef5584
		}
ef5584
		
ef5584
		return $fileinfo;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Simple version of jumpbox, just lists modules
ef5584
	*/
ef5584
	function make_module_select($select_id = false, $ignore_id = false, $ignore_acl = false, $ignore_nonpost = false, $ignore_emptycat = true, $ignore_noncat = false)
ef5584
	{
ef5584
		global $db, $user, $auth, $config;
ef5584
ef5584
		$sql = 'SELECT module_id, module_enabled, module_basename, parent_id, module_langname, left_id, right_id, module_auth
ef5584
			FROM ' . MODULES_TABLE . "
ef5584
			WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
			ORDER BY left_id ASC";
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$right = $iteration = 0;
ef5584
		$padding_store = array('0' => '');
ef5584
		$module_list = $padding = '';
ef5584
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			if ($row['left_id'] < $right)
ef5584
			{
ef5584
				$padding .= '   ';
ef5584
				$padding_store[$row['parent_id']] = $padding;
ef5584
			}
ef5584
			else if ($row['left_id'] > $right + 1)
ef5584
			{
ef5584
				$padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
ef5584
			}
ef5584
ef5584
			$right = $row['right_id'];
ef5584
ef5584
			if (!$ignore_acl && $row['module_auth'])
ef5584
			{
ef5584
				// We use zero as the forum id to check - global setting.
ef5584
				if (!p_master::module_auth($row['module_auth'], 0))
ef5584
				{
ef5584
					continue;
ef5584
				}
ef5584
			}
ef5584
ef5584
			// ignore this module?
ef5584
			if ((is_array($ignore_id) && in_array($row['module_id'], $ignore_id)) || $row['module_id'] == $ignore_id)
ef5584
			{
ef5584
				continue;
ef5584
			}
ef5584
ef5584
			// empty category
ef5584
			if (!$row['module_basename'] && ($row['left_id'] + 1 == $row['right_id']) && $ignore_emptycat)
ef5584
			{
ef5584
				continue;
ef5584
			}
ef5584
ef5584
			// ignore non-category?
ef5584
			if ($row['module_basename'] && $ignore_noncat)
ef5584
			{
ef5584
				continue;
ef5584
			}
ef5584
ef5584
			$selected = (is_array($select_id)) ? ((in_array($row['module_id'], $select_id)) ? ' selected="selected"' : '') : (($row['module_id'] == $select_id) ? ' selected="selected"' : '');
ef5584
ef5584
			$langname = $this->lang_name($row['module_langname']);
ef5584
			$module_list .= '<option value="' . $row['module_id'] . '"' . $selected . ((!$row['module_enabled']) ? ' class="disabled"' : '') . '>' . $padding . $langname . '</option>';
ef5584
ef5584
			$iteration++;
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		unset($padding_store);
ef5584
ef5584
		return $module_list;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Get module branch
ef5584
	*/
ef5584
	function get_module_branch($module_id, $type = 'all', $order = 'descending', $include_module = true)
ef5584
	{
ef5584
		global $db;
ef5584
ef5584
		switch ($type)
ef5584
		{
ef5584
			case 'parents':
ef5584
				$condition = 'm1.left_id BETWEEN m2.left_id AND m2.right_id';
ef5584
			break;
ef5584
ef5584
			case 'children':
ef5584
				$condition = 'm2.left_id BETWEEN m1.left_id AND m1.right_id';
ef5584
			break;
ef5584
ef5584
			default:
ef5584
				$condition = 'm2.left_id BETWEEN m1.left_id AND m1.right_id OR m1.left_id BETWEEN m2.left_id AND m2.right_id';
ef5584
			break;
ef5584
		}
ef5584
ef5584
		$rows = array();
ef5584
ef5584
		$sql = 'SELECT m2.*
ef5584
			FROM ' . MODULES_TABLE . ' m1
ef5584
			LEFT JOIN ' . MODULES_TABLE . " m2 ON ($condition)
ef5584
			WHERE m1.module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
				AND m2.module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
				AND m1.module_id = $module_id
ef5584
			ORDER BY m2.left_id " . (($order == 'descending') ? 'ASC' : 'DESC');
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			if (!$include_module && $row['module_id'] == $module_id)
ef5584
			{
ef5584
				continue;
ef5584
			}
ef5584
ef5584
			$rows[] = $row;
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		return $rows;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Remove modules cache file
ef5584
	*/
ef5584
	function remove_cache_file()
ef5584
	{
ef5584
		global $cache;
ef5584
ef5584
		// Sanitise for future path use, it's escaped as appropriate for queries
ef5584
		$p_class = str_replace(array('.', '/', '\\'), '', basename($this->module_class));
ef5584
		
ef5584
		$cache->destroy('_modules_' . $p_class);
ef5584
ef5584
		// Additionally remove sql cache
ef5584
		$cache->destroy('sql', MODULES_TABLE);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Return correct language name
ef5584
	*/
ef5584
	function lang_name($module_langname)
ef5584
	{
ef5584
		global $user;
ef5584
ef5584
		return (!empty($user->lang[$module_langname])) ? $user->lang[$module_langname] : $module_langname;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Update/Add module
ef5584
	*
ef5584
	* @param bool $run_inline if set to true errors will be returned and no logs being written
ef5584
	*/
ef5584
	function update_module_data(&$module_data, $run_inline = false)
ef5584
	{
ef5584
		global $db, $user;
ef5584
ef5584
		if (!isset($module_data['module_id']))
ef5584
		{
ef5584
			// no module_id means we're creating a new category/module
ef5584
			if ($module_data['parent_id'])
ef5584
			{
ef5584
				$sql = 'SELECT left_id, right_id
ef5584
					FROM ' . MODULES_TABLE . "
ef5584
					WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
ef5584
						AND module_id = " . (int) $module_data['parent_id'];
ef5584
				$result = $db->sql_query($sql);
ef5584
				$row = $db->sql_fetchrow($result);
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
				if (!$row)
ef5584
				{
ef5584
					if ($run_inline)
ef5584
					{
ef5584
						return 'PARENT_NO_EXIST';
ef5584
					}
ef5584
ef5584
					trigger_error($user->lang['PARENT_NO_EXIST'] . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING);
ef5584
				}
ef5584
ef5584
				// Workaround
ef5584
				$row['left_id'] = (int) $row['left_id'];
ef5584
				$row['right_id'] = (int) $row['right_id'];
ef5584
ef5584
				$sql = 'UPDATE ' . MODULES_TABLE . "
ef5584
					SET left_id = left_id + 2, right_id = right_id + 2
ef5584
					WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
ef5584
						AND left_id > {$row['right_id']}";
ef5584
				$db->sql_query($sql);
ef5584
ef5584
				$sql = 'UPDATE ' . MODULES_TABLE . "
ef5584
					SET right_id = right_id + 2
ef5584
					WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
ef5584
						AND {$row['left_id']} BETWEEN left_id AND right_id";
ef5584
				$db->sql_query($sql);
ef5584
ef5584
				$module_data['left_id'] = (int) $row['right_id'];
ef5584
				$module_data['right_id'] = (int) $row['right_id'] + 1;
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				$sql = 'SELECT MAX(right_id) AS right_id
ef5584
					FROM ' . MODULES_TABLE . "
ef5584
					WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'";
ef5584
				$result = $db->sql_query($sql);
ef5584
				$row = $db->sql_fetchrow($result);
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
				$module_data['left_id'] = (int) $row['right_id'] + 1;
ef5584
				$module_data['right_id'] = (int) $row['right_id'] + 2;
ef5584
			}
ef5584
ef5584
			$sql = 'INSERT INTO ' . MODULES_TABLE . ' ' . $db->sql_build_array('INSERT', $module_data);
ef5584
			$db->sql_query($sql);
ef5584
ef5584
			$module_data['module_id'] = $db->sql_nextid();
ef5584
ef5584
			if (!$run_inline)
ef5584
			{
ef5584
				add_log('admin', 'LOG_MODULE_ADD', $this->lang_name($module_data['module_langname']));
ef5584
			}
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$row = $this->get_module_row($module_data['module_id']);
ef5584
ef5584
			if ($module_data['module_basename'] && !$row['module_basename'])
ef5584
			{
ef5584
				// we're turning a category into a module
ef5584
				$branch = $this->get_module_branch($module_data['module_id'], 'children', 'descending', false);
ef5584
ef5584
				if (sizeof($branch))
ef5584
				{
ef5584
					return array($user->lang['NO_CATEGORY_TO_MODULE']);
ef5584
				}
ef5584
			}
ef5584
ef5584
			if ($row['parent_id'] != $module_data['parent_id'])
ef5584
			{
ef5584
				$this->move_module($module_data['module_id'], $module_data['parent_id']);
ef5584
			}
ef5584
ef5584
			$update_ary = $module_data;
ef5584
			unset($update_ary['module_id']);
ef5584
ef5584
			$sql = 'UPDATE ' . MODULES_TABLE . '
ef5584
				SET ' . $db->sql_build_array('UPDATE', $update_ary) . "
ef5584
				WHERE module_class = '" . $db->sql_escape($module_data['module_class']) . "'
ef5584
					AND module_id = " . (int) $module_data['module_id'];
ef5584
			$db->sql_query($sql);
ef5584
ef5584
			if (!$run_inline)
ef5584
			{
ef5584
				add_log('admin', 'LOG_MODULE_EDIT', $this->lang_name($module_data['module_langname']));
ef5584
			}
ef5584
		}
ef5584
ef5584
		return array();
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Move module around the tree
ef5584
	*/
ef5584
	function move_module($from_module_id, $to_parent_id)
ef5584
	{
ef5584
		global $db;
ef5584
ef5584
		$moved_modules = $this->get_module_branch($from_module_id, 'children', 'descending');
ef5584
		$from_data = $moved_modules[0];
ef5584
		$diff = sizeof($moved_modules) * 2;
ef5584
ef5584
		$moved_ids = array();
ef5584
		for ($i = 0; $i < sizeof($moved_modules); ++$i)
ef5584
		{
ef5584
			$moved_ids[] = $moved_modules[$i]['module_id'];
ef5584
		}
ef5584
ef5584
		// Resync parents
ef5584
		$sql = 'UPDATE ' . MODULES_TABLE . "
ef5584
			SET right_id = right_id - $diff
ef5584
			WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
				AND left_id < " . (int) $from_data['right_id'] . '
ef5584
				AND right_id > ' . (int) $from_data['right_id'];
ef5584
		$db->sql_query($sql);
ef5584
ef5584
		// Resync righthand side of tree
ef5584
		$sql = 'UPDATE ' . MODULES_TABLE . "
ef5584
			SET left_id = left_id - $diff, right_id = right_id - $diff
ef5584
			WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
				AND left_id > " . (int) $from_data['right_id'];
ef5584
		$db->sql_query($sql);
ef5584
ef5584
		if ($to_parent_id > 0)
ef5584
		{
ef5584
			$to_data = $this->get_module_row($to_parent_id);
ef5584
ef5584
			// Resync new parents
ef5584
			$sql = 'UPDATE ' . MODULES_TABLE . "
ef5584
				SET right_id = right_id + $diff
ef5584
				WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
					AND " . (int) $to_data['right_id'] . ' BETWEEN left_id AND right_id
ef5584
					AND ' . $db->sql_in_set('module_id', $moved_ids, true);
ef5584
			$db->sql_query($sql);
ef5584
ef5584
			// Resync the righthand side of the tree
ef5584
			$sql = 'UPDATE ' . MODULES_TABLE . "
ef5584
				SET left_id = left_id + $diff, right_id = right_id + $diff
ef5584
				WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
					AND left_id > " . (int) $to_data['right_id'] . '
ef5584
					AND ' . $db->sql_in_set('module_id', $moved_ids, true);
ef5584
			$db->sql_query($sql);
ef5584
ef5584
			// Resync moved branch
ef5584
			$to_data['right_id'] += $diff;
ef5584
			if ($to_data['right_id'] > $from_data['right_id'])
ef5584
			{
ef5584
				$diff = '+ ' . ($to_data['right_id'] - $from_data['right_id'] - 1);
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				$diff = '- ' . abs($to_data['right_id'] - $from_data['right_id'] - 1);
ef5584
			}
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$sql = 'SELECT MAX(right_id) AS right_id
ef5584
				FROM ' . MODULES_TABLE . "
ef5584
				WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
					AND " . $db->sql_in_set('module_id', $moved_ids, true);
ef5584
			$result = $db->sql_query($sql);
ef5584
			$row = $db->sql_fetchrow($result);
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			$diff = '+ ' . (int) ($row['right_id'] - $from_data['left_id'] + 1);
ef5584
		}
ef5584
ef5584
		$sql = 'UPDATE ' . MODULES_TABLE . "
ef5584
			SET left_id = left_id $diff, right_id = right_id $diff
ef5584
			WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
				AND " . $db->sql_in_set('module_id', $moved_ids);
ef5584
		$db->sql_query($sql);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Remove module from tree
ef5584
	*/
ef5584
	function delete_module($module_id)
ef5584
	{
ef5584
		global $db, $user;
ef5584
ef5584
		$row = $this->get_module_row($module_id);
ef5584
ef5584
		$branch = $this->get_module_branch($module_id, 'children', 'descending', false);
ef5584
ef5584
		if (sizeof($branch))
ef5584
		{
ef5584
			return array($user->lang['CANNOT_REMOVE_MODULE']);
ef5584
		}
ef5584
ef5584
		// If not move
ef5584
		$diff = 2;
ef5584
		$sql = 'DELETE FROM ' . MODULES_TABLE . "
ef5584
			WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
				AND module_id = $module_id";
ef5584
		$db->sql_query($sql);
ef5584
ef5584
		$row['right_id'] = (int) $row['right_id'];
ef5584
		$row['left_id'] = (int) $row['left_id'];
ef5584
ef5584
		// Resync tree
ef5584
		$sql = 'UPDATE ' . MODULES_TABLE . "
ef5584
			SET right_id = right_id - $diff
ef5584
			WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
				AND left_id < {$row['right_id']} AND right_id > {$row['right_id']}";
ef5584
		$db->sql_query($sql);
ef5584
ef5584
		$sql = 'UPDATE ' . MODULES_TABLE . "
ef5584
			SET left_id = left_id - $diff, right_id = right_id - $diff
ef5584
			WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
				AND left_id > {$row['right_id']}";
ef5584
		$db->sql_query($sql);
ef5584
ef5584
		add_log('admin', 'LOG_MODULE_REMOVED', $this->lang_name($row['module_langname']));
ef5584
ef5584
		return array();
ef5584
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Move module position by $steps up/down
ef5584
	*/
ef5584
	function move_module_by($module_row, $action = 'move_up', $steps = 1)
ef5584
	{
ef5584
		global $db;
ef5584
ef5584
		/**
ef5584
		* Fetch all the siblings between the module's current spot
ef5584
		* and where we want to move it to. If there are less than $steps
ef5584
		* siblings between the current spot and the target then the
ef5584
		* module will move as far as possible
ef5584
		*/
ef5584
		$sql = 'SELECT module_id, left_id, right_id, module_langname
ef5584
			FROM ' . MODULES_TABLE . "
ef5584
			WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
				AND parent_id = " . (int) $module_row['parent_id'] . '
ef5584
				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');
ef5584
		$result = $db->sql_query_limit($sql, $steps);
ef5584
ef5584
		$target = array();
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$target = $row;
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		if (!sizeof($target))
ef5584
		{
ef5584
			// The module is already on top or bottom
ef5584
			return false;
ef5584
		}
ef5584
ef5584
		/**
ef5584
		* $left_id and $right_id define the scope of the nodes that are affected by the move.
ef5584
		* $diff_up and $diff_down are the values to substract or add to each node's left_id
ef5584
		* and right_id in order to move them up or down.
ef5584
		* $move_up_left and $move_up_right define the scope of the nodes that are moving
ef5584
		* up. Other nodes in the scope of ($left_id, $right_id) are considered to move down.
ef5584
		*/
ef5584
		if ($action == 'move_up')
ef5584
		{
ef5584
			$left_id = (int) $target['left_id'];
ef5584
			$right_id = (int) $module_row['right_id'];
ef5584
ef5584
			$diff_up = (int) ($module_row['left_id'] - $target['left_id']);
ef5584
			$diff_down = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);
ef5584
ef5584
			$move_up_left = (int) $module_row['left_id'];
ef5584
			$move_up_right = (int) $module_row['right_id'];
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$left_id = (int) $module_row['left_id'];
ef5584
			$right_id = (int) $target['right_id'];
ef5584
ef5584
			$diff_up = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);
ef5584
			$diff_down = (int) ($target['right_id'] - $module_row['right_id']);
ef5584
ef5584
			$move_up_left = (int) ($module_row['right_id'] + 1);
ef5584
			$move_up_right = (int) $target['right_id'];
ef5584
		}
ef5584
ef5584
		// Now do the dirty job
ef5584
		$sql = 'UPDATE ' . MODULES_TABLE . "
ef5584
			SET left_id = left_id + CASE
ef5584
				WHEN left_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
ef5584
				ELSE {$diff_down}
ef5584
			END,
ef5584
			right_id = right_id + CASE
ef5584
				WHEN right_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
ef5584
				ELSE {$diff_down}
ef5584
			END
ef5584
			WHERE module_class = '" . $db->sql_escape($this->module_class) . "'
ef5584
				AND left_id BETWEEN {$left_id} AND {$right_id}
ef5584
				AND right_id BETWEEN {$left_id} AND {$right_id}";
ef5584
		$db->sql_query($sql);
ef5584
ef5584
		$this->remove_cache_file();
ef5584
ef5584
		return $this->lang_name($target['module_langname']);
ef5584
	}
ef5584
}
ef5584
ef5584
?>