<?php
/***************************************************************************
 *                            admin_modcp.php
 *                            ---------------
 *   begin                : Sunday, Dec 26, 2004
 *   copyright            : (C) 2005 web-milk.co.uk
 *   email                : milkman@web-milk.co.uk
 *
 *   $Id: admin_modcp.php, v1.1.00.01 2005/08/15 17:01:20 kooky Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

define('IN_PHPBB', 1);

if( !empty($setmodules) )
{
	$file = basename(__FILE__);
	$module['Modcp']['Configuration'] = $file;
	return;
}

// Dummy function, always returns true;
function check_perms( $category = '', $module = '', $level = '' )
{
	return true;
}

//
// Let's set the root dir for phpBB
//
$phpbb_root_path = './../';
require($phpbb_root_path . 'extension.inc');
require('./pagestart.' . $phpEx);

// 
// Let's get the config data that we want
// 
$sql = "SELECT *
	FROM " . CONFIG_TABLE . "
	WHERE config_name LIKE 'modcp_module_%'";
if (!$result = $db->sql_query($sql))
{
	message_die(CRITICAL_ERROR, 'Could not query config information in admin_board', '', __LINE__, __FILE__, $sql);
}
else
{
	while ($row = $db->sql_fetchrow($result))
	{
		// The config_name is in the form modcp_module_<Cat-name>_<Mod-name>, let's split it up.
		list($category, $module_name) = split('_', substr($row['config_name'], strlen('modcp_module_')));

		$category = str_replace('-', '_', $category);
		$module_name = str_replace('-', '_', $module_name);

		$installed_module[$category][$module_name] = explode(',', $row['config_value']);
	}
}

//
// Let's do the update baby, yeah!
//
if (isset($HTTP_POST_VARS['submit']))
{
	$level_used = array(
		MAIN_MOD => 1,	// Forced on
		MOD      => 0,
		SUPPORT  => 0,
		VIP      => 0,
		BOT      => 0
	);
	foreach($HTTP_POST_VARS as $config_name => $config_value)
	{
		if ($config_name != 'submit')
		{
			// Split the config_name up and check if it is installed.
			list($category, $module_name) = split('_', substr($config_name, strlen('modcp_module_')));

			$category = str_replace('-', '_', $category);
			$module_name = str_replace('-', '_', $module_name);

			// Main mod has access to all when a module is enabled!
			$n_perms = count($config_value); if (in_array(0, $config_value)) $n_perms--;
			if ( $n_perms && !in_array(MAIN_MOD, $config_value) )
			{
				$config_value[] = MAIN_MOD;
			}

			// Make a note of the user levels that have permissions.
			foreach ($config_value as $l) if ($l != 0)
			{
				$level_used[$l] = 1;
			}

			$config_value = implode(',', $config_value);

			if (isset($installed_module[$category][$module_name]))
			{
				// Update
				$sql = "UPDATE " . CONFIG_TABLE . " SET
					config_value = '" . str_replace("\'", "''", $config_value) . "'
					WHERE config_name = '$config_name'";
				if ( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "Failed to update general configuration for $category/$module_name", "", __LINE__, __FILE__, $sql);
				}
			}
			else
			{
				// Insert
				$sql = "INSERT INTO " . CONFIG_TABLE . " (config_name, config_value)
					VALUES ('$config_name', '". str_replace("\'", "''", $config_value) . "')";
				if ( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "Failed to update general configuration for $category/$module_name", "", __LINE__, __FILE__, $sql);
				}
			}
		}
	}

	// Update the used flags
	$comma = '';
	foreach ($level_used as $l => $used) if ($used == 1)
	{
		$level_used_list .= "$comma$l";
		$comma = ',';
	}

	$sql = "UPDATE " . CONFIG_TABLE . " SET
		config_value = '" . str_replace("\'", "''", $level_used_list) . "'
		WHERE config_name = 'modcp_levels_used'";
	if ( !$db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, "Failed to update general configuration for $category/$module_name", "", __LINE__, __FILE__, $sql);
	}

	$message = $lang['Config_updated'] . '<br /><br />' . sprintf($lang['Click_return_config'], '<a href="' . append_sid('admin_modcp.' . $phpEx) . '">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="' . append_sid('index.' . $phpEx . '?pane=right') . '">', '</a>');
	message_die(GENERAL_MESSAGE, $message);
}
elseif (isset($HTTP_POST_VARS['mode']) || isset($HTTP_GET_VARS['mode']))
{
	if (isset($HTTP_POST_VARS['mode']))
	{
		$mode = $HTTP_POST_VARS['mode'];
	}
	else
	{
		$mode = urldecode($HTTP_GET_VARS['mode']);
	}
	if (isset($HTTP_POST_VARS['name']))
	{
		$name = $HTTP_POST_VARS['name'];
	}
	else
	{
		$name = urldecode($HTTP_GET_VARS['name']);
	}

	// Check that the name is a modcp_module
	list($category, $module_name) = split('_', substr($name, strlen('modcp_module_')));

	$category = str_replace('-', '_', $category);
	$module_name = str_replace('-', '_', $module_name);

	if (!isset($installed_module[$category][$module_name]))
	{
		message_die(GENERAL_ERROR, "The requested module ($category/$module_name) is not installed.");
	}

	switch($mode)
	{
		case 'disable':
			if (!empty($name))
			{
				// Update
				$sql = "UPDATE " . CONFIG_TABLE . " SET
					config_value = '0'
					WHERE config_name = '$name'";
				if ( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "Failed to update general configuration for $category/$module_name", "", __LINE__, __FILE__, $sql);
				}

				$message = $lang['Config_updated'] . '<br /><br />' . sprintf($lang['Click_return_config'], '<a href="' . append_sid('admin_modcp.' . $phpEx) . '">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="' . append_sid('index.' . $phpEx . '?pane=right') . '">', '</a>');
				message_die(GENERAL_MESSAGE, $message);
			}
			break;
		case 'remove':
			if (!empty($name))
			{
				// Delete
				$sql = "DELETE FROM " . CONFIG_TABLE . "
					WHERE config_name = '$name'";
				if ( !$db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "Failed to update general configuration for $category/$module_name", "", __LINE__, __FILE__, $sql);
				}

				$message = $lang['Config_updated'] . '<br /><br />' . sprintf($lang['Click_return_config'], '<a href="' . append_sid('admin_modcp.' . $phpEx) . '">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="' . append_sid('index.' . $phpEx . '?pane=right') . '">', '</a>');
				message_die(GENERAL_MESSAGE, $message);
			}
			break;
	}
}

// 
// Get a list of the modules present
// 
$dir = @opendir($phpbb_root_path .'modcp');

$module = array();
$modcp_module = array();
$setmodules = 1;
while( $file = @readdir($dir) )
{
	if ( preg_match("/^admin_.*?\." . $phpEx . "$/", $file) )
	{
		include($phpbb_root_path . 'modcp/' . $file);
	}
}

@closedir($dir);

unset($setmodules);
ksort($module);
ksort($modcp_module);

// 
// Compare the two lists to make a list of new, installable modules.
// 
if (!empty($modcp_module))
{
	foreach($modcp_module as $category => $module_list)
	{
		foreach($module_list as $mod => $v)
		{
			if (!isset($installed_module[$category][$mod]))
			{
				$new_module[$category][$mod] = array();
			}
		}
	}
	unset($module_list);
}

// 
// Let's do some outputting..
// 

// Global stuff...
$template->set_filenames(array(
	'body' => 'admin/modcp_config_body.tpl'
	)
);

$s_hidden_fields = '';

$template->assign_vars(array(
	'S_MODCP_ACTION' => append_sid('admin_modcp.' . $phpEx),
	'L_MODCP_TITLE' => $lang['Modcp_title'],
	'L_MODCP_EXPLAIN' => $lang['Modcp_explain'],
	'L_WHO_ACCESS' => $lang['Who_access'],

	'L_MODULE_PERMISSIONS' => $lang['Modcp_permissions'],

	'L_NEW_MODULES' => $lang['New_modules'],
	'L_NEW_MODULES_EXPLAIN' => $lang['New_modules_explain'],

	'L_MODULE_NO_INSTALL' => $lang['Module_no_install'],
	'L_MODULE_NO_INSTALL_EXPLAIN' => $lang['Module_no_install_explain'],

	'L_SUBMIT' => $lang['Submit'],
	'L_RESET' => $lang['Reset'],
	'L_DISABLE' => $lang['Disable'],
	'L_REMOVE' => $lang['Remove'],

	'S_HIDDEN_FIELDS' => $s_hidden_fields)
);

$checkbox_mask = '<input type="checkbox" id="%FIELD_NAME%[%N%]" name="%FIELD_NAME%[%N%]" value="%LEVEL%"%CHECKED% /> <label for="%FIELD_NAME%[%N%]">%LANG%</label>&nbsp;&nbsp;';

//  First the installed modules, if there are any..
if (!empty($installed_module))
{
	foreach($installed_module as $category => $module_list)
	{
		ksort($module_list);

		// Prepare the category name
		$cat_name = (isset($lang[$category])) ? $lang[$category] : ucwords(str_replace('_', ' ', $category));

		$template->assign_block_vars('category', array(
			'L_CAT_NAME' => $cat_name)
		);

		$i = 0; // Reset for each category
		foreach($module_list as $mod => $v)
		{
			// Prepare the module name
			$mod_name = (isset($lang[$mod])) ? $lang[$mod] : ucwords(str_replace('_', ' ', $mod));

			// Prepare the field name
			$field_name = 'modcp_module_' . str_replace('_', '-', $category) . '_' . str_replace('_', '-', $mod);

			$row_color = ( !($i % 2) ) ? $theme['td_color1'] : $theme['td_color2'];
			$row_class = ( !($i % 2) ) ? $theme['td_class1'] : $theme['td_class2'];

			// Does the module file still exist?
			if (isset($modcp_module[$category][$mod]))
			{
				$s_checkboxes = '<input type="hidden" name='. $field_name .'[0]" value="0" />';

				// Is user class present?  Test for MAIN_MOD
				if (defined('MAIN_MOD'))
				{
					$n = 1;

					// Main Moderators (needed for the User Class Mod only)
					$checked = (in_array(MAIN_MOD, $v)) ? ' checked="checked"' : '';
					$this_checkbox = $checkbox_mask;
					$this_checkbox = str_replace('%FIELD_NAME%', $field_name, $this_checkbox);
					$this_checkbox = str_replace('%N%', $n++, $this_checkbox);
					$this_checkbox = str_replace('%LEVEL%', MAIN_MOD, $this_checkbox);
					$this_checkbox = str_replace('%CHECKED%', $checked, $this_checkbox);
					$this_checkbox = str_replace('%LANG%', $lang['Main_Moderators'], $this_checkbox);
					$s_checkboxes .= $this_checkbox;

					// Moderators
					$checked = (in_array(MOD, $v)) ? ' checked="checked"' : '';
					$this_checkbox = $checkbox_mask;
					$this_checkbox = str_replace('%FIELD_NAME%', $field_name, $this_checkbox);
					$this_checkbox = str_replace('%N%', $n++, $this_checkbox);
					$this_checkbox = str_replace('%LEVEL%', MOD, $this_checkbox);
					$this_checkbox = str_replace('%CHECKED%', $checked, $this_checkbox);
					$this_checkbox = str_replace('%LANG%', $lang['Moderators'], $this_checkbox);
					$s_checkboxes .= $this_checkbox;

					// Support Team (needed for the User Class Mod only)
					$checked = (in_array(SUPPORT, $v)) ? ' checked="checked"' : '';
					$this_checkbox = $checkbox_mask;
					$this_checkbox = str_replace('%FIELD_NAME%', $field_name, $this_checkbox);
					$this_checkbox = str_replace('%N%', $n++, $this_checkbox);
					$this_checkbox = str_replace('%LEVEL%', SUPPORT, $this_checkbox);
					$this_checkbox = str_replace('%CHECKED%', $checked, $this_checkbox);
					$this_checkbox = str_replace('%LANG%', $lang['Support'], $this_checkbox);
					$s_checkboxes .= $this_checkbox;

					// V.I.P.s (needed for the User Class Mod only)
					$checked = (in_array(VIP, $v)) ? ' checked="checked"' : '';
					$this_checkbox = $checkbox_mask;
					$this_checkbox = str_replace('%FIELD_NAME%', $field_name, $this_checkbox);
					$this_checkbox = str_replace('%N%', $n++, $this_checkbox);
					$this_checkbox = str_replace('%LEVEL%', VIP, $this_checkbox);
					$this_checkbox = str_replace('%CHECKED%', $checked, $this_checkbox);
					$this_checkbox = str_replace('%LANG%', $lang['VIP'], $this_checkbox);
					$s_checkboxes .= $this_checkbox;

					// Bots (needed for the User Class Mod only)
					$checked = (in_array(BOT, $v)) ? ' checked="checked"' : '';
					$this_checkbox = $checkbox_mask;
					$this_checkbox = str_replace('%FIELD_NAME%', $field_name, $this_checkbox);
					$this_checkbox = str_replace('%N%', $n++, $this_checkbox);
					$this_checkbox = str_replace('%LEVEL%', BOT, $this_checkbox);
					$this_checkbox = str_replace('%CHECKED%', $checked, $this_checkbox);
					$this_checkbox = str_replace('%LANG%', $lang['Bots'], $this_checkbox);
					$s_checkboxes .= $this_checkbox;
				}
				else
				{
					// There's only one person who can use this, a moderator!
					$checked = (in_array(MOD, $v)) ? ' checked="checked"' : '';
					$this_checkbox = $checkbox_mask;
					$this_checkbox = str_replace('%FIELD_NAME%', $field_name, $this_checkbox);
					$this_checkbox = str_replace('%N%', $n++, $this_checkbox);
					$this_checkbox = str_replace('%LEVEL%', MOD, $this_checkbox);
					$this_checkbox = str_replace('%CHECKED%', $checked, $this_checkbox);
					$this_checkbox = str_replace('%LANG%', $lang['Moderators'], $this_checkbox);
					$s_checkboxes .= $this_checkbox;
				}

				$template->assign_block_vars('category.module', array(
					'ROW_CLASS' => $row_class,
					'ROW_COLOR' => '#' . $row_color,

					'L_MODULE_NAME' => $mod_name,

					'S_CHECKBOXES' => $s_checkboxes,

					'U_DISABLE' => append_sid("admin_modcp.$phpEx?mode=disable&amp;name=" . urlencode($field_name)),
					'U_REMOVE' => append_sid("admin_modcp.$phpEx?mode=remove&amp;name=" . urlencode($field_name))
					)
				);
			}
			else if (!isset($module[$category][$mod]))
			{
				$template->assign_block_vars('category.remove_module', array(
					'ROW_CLASS' => $row_class,
					'ROW_COLOR' => '#' . $row_color,

					'L_MODULE_NAME' => $mod_name,
					'L_MESSAGE' => $lang['Module_not_present'],
					'U_REMOVE' => append_sid("admin_modcp.$phpEx?mode=remove&amp;name=" . urlencode($field_name))
					)
				);
			}
			else
			{
				$template->assign_block_vars('category.remove_module', array(
					'ROW_CLASS' => $row_class,
					'ROW_COLOR' => '#' . $row_color,

					'L_MODULE_NAME' => $mod_name,
					'L_MESSAGE' => $lang['Module_not_configure'],
					'U_REMOVE' => append_sid("admin_modcp.$phpEx?mode=remove&amp;name=" . urlencode($field_name))
					)
				);
			}
			// Increase the module counter
			$i++;
		}
	}
}
else
{
	$template->assign_block_vars('nothing_installed', array(
		'L_MESSAGE' => $lang['New_modules_no_install'])
	);
}

// Now the modules that are not installed yet...
if (!empty($new_module))
{
	// A switch for new stuff.
	$template->assign_block_vars('new', array());

	foreach($new_module as $category => $module_list)
	{
		ksort($module_list);

		// Prepare the category name
		$cat_name = (isset($lang[$category])) ? $lang[$category] : ucwords(str_replace('_', ' ', $category));
	
		$template->assign_block_vars('new.category', array(
			'L_CAT_NAME' => $cat_name)
		);

		$i = 0;  // Reset for each category
		foreach($module_list as $mod => $v)
		{
			// Prepare the module name
			$mod_name = (isset($lang[$mod])) ? $lang[$mod] : ucwords(str_replace('_', ' ', $mod));

			// Prepare the field name
			$field_name = 'modcp_module_' . str_replace('_', '-', $category) . '_' . str_replace('_', '-', $mod);

			$s_checkboxes = '';
			// Is user class present?  Test for MAIN_MOD
			if (defined('MAIN_MOD'))
			{
				$n = 1;

				// Main Moderators (needed for the User Class Mod only)
				$this_checkbox = $checkbox_mask;
				$this_checkbox = str_replace('%FIELD_NAME%', $field_name, $this_checkbox);
				$this_checkbox = str_replace('%N%', $n++, $this_checkbox);
				$this_checkbox = str_replace('%LEVEL%', MAIN_MOD, $this_checkbox);
				$this_checkbox = str_replace('%CHECKED%', '', $this_checkbox);
				$this_checkbox = str_replace('%LANG%', $lang['Main_Moderators'], $this_checkbox);
				$s_checkboxes .= $this_checkbox;

				// Moderators
				$this_checkbox = $checkbox_mask;
				$this_checkbox = str_replace('%FIELD_NAME%', $field_name, $this_checkbox);
				$this_checkbox = str_replace('%N%', $n++, $this_checkbox);
				$this_checkbox = str_replace('%LEVEL%', MOD, $this_checkbox);
				$this_checkbox = str_replace('%CHECKED%', '', $this_checkbox);
				$this_checkbox = str_replace('%LANG%', $lang['Moderators'], $this_checkbox);
				$s_checkboxes .= $this_checkbox;

				// Support Team (needed for the User Class Mod only)
				$this_checkbox = $checkbox_mask;
				$this_checkbox = str_replace('%FIELD_NAME%', $field_name, $this_checkbox);
				$this_checkbox = str_replace('%N%', $n++, $this_checkbox);
				$this_checkbox = str_replace('%LEVEL%', SUPPORT, $this_checkbox);
				$this_checkbox = str_replace('%CHECKED%', '', $this_checkbox);
				$this_checkbox = str_replace('%LANG%', $lang['Support'], $this_checkbox);
				$s_checkboxes .= $this_checkbox;

				// V.I.P.s (needed for the User Class Mod only)
				$this_checkbox = $checkbox_mask;
				$this_checkbox = str_replace('%FIELD_NAME%', $field_name, $this_checkbox);
				$this_checkbox = str_replace('%N%', $n++, $this_checkbox);
				$this_checkbox = str_replace('%LEVEL%', VIP, $this_checkbox);
				$this_checkbox = str_replace('%CHECKED%', '', $this_checkbox);
				$this_checkbox = str_replace('%LANG%', $lang['VIP'], $this_checkbox);
				$s_checkboxes .= $this_checkbox;

				// Bots (needed for the User Class Mod only)
				$this_checkbox = $checkbox_mask;
				$this_checkbox = str_replace('%FIELD_NAME%', $field_name, $this_checkbox);
				$this_checkbox = str_replace('%N%', $n++, $this_checkbox);
				$this_checkbox = str_replace('%LEVEL%', BOT, $this_checkbox);
				$this_checkbox = str_replace('%CHECKED%', '', $this_checkbox);
				$this_checkbox = str_replace('%LANG%', $lang['Bots'], $this_checkbox);
				$s_checkboxes .= $this_checkbox;
			}
			else
			{
				// There's only one person who can use this, a moderator!
				$this_checkbox = $checkbox_mask;
				$this_checkbox = str_replace('%FIELD_NAME%', $field_name, $this_checkbox);
				$this_checkbox = str_replace('%N%', $n++, $this_checkbox);
				$this_checkbox = str_replace('%LEVEL%', MOD, $this_checkbox);
				$this_checkbox = str_replace('%CHECKED%', '', $this_checkbox);
				$this_checkbox = str_replace('%LANG%', $lang['Moderators'], $this_checkbox);
				$s_checkboxes .= $this_checkbox;
			}

			//
			// An extra checkbox saying "No Permissions"
			//
			$this_checkbox = $checkbox_mask;
			$this_checkbox = str_replace('%FIELD_NAME%', $field_name, $this_checkbox);
			$this_checkbox = str_replace('%N%', $n++, $this_checkbox);
			$this_checkbox = str_replace('%LEVEL%', 0, $this_checkbox);
			$this_checkbox = str_replace('%CHECKED%', '', $this_checkbox);
			$this_checkbox = str_replace('%LANG%', $lang['No_one'], $this_checkbox);
			$s_checkboxes .= $this_checkbox;

			$row_color = ( !($i % 2) ) ? $theme['td_color1'] : $theme['td_color2'];
			$row_class = ( !($i % 2) ) ? $theme['td_class1'] : $theme['td_class2'];

			$template->assign_block_vars('new.category.module', array(
				'ROW_CLASS' => $row_class,
				'ROW_COLOR' => '#' . $row_color,

				'L_MODULE_NAME' => $mod_name,

				'S_CHECKBOXES' => $s_checkboxes
				)
			);
			// Increase the module counter
			$i++;
		}
	}
}

// And lastly the files that cannot be installed...
if (!empty($module))
{
	// A switch for new stuff.
	$template->assign_block_vars('old', array());

	foreach($module as $category => $module_list)
	{
		ksort($module_list);

		// Prepare the category name
		$cat_name = (isset($lang[$category])) ? $lang[$category] : ucwords(str_replace('_', ' ', $category));

		$template->assign_block_vars('old.category', array(
			'L_CAT_NAME' => $cat_name
			)
		);

		$i = 0;  // Reset for each category
		foreach($module_list as $mod => $v)
		{
			// Prepare the module name
			if (isset($lang[$mod]))
			{
				$mod_name = $lang[$mod];
			}
			else
			{
				$mod_name = ucwords(str_replace('_', ' ', $mod));
			}

			if ( !($i % 2) )
			{
				$row_color = $theme['td_color1'];
			}
			else
			{
				$row_color = $theme['td_color2'];
			}
			if ( !($i % 2) )
			{
				$row_class = $theme['td_class1'];
			}
			else
			{
				$row_class = $theme['td_class2'];
			}

			$template->assign_block_vars('old.category.module', array(
				'ROW_CLASS' => $row_class,
				'ROW_COLOR' => '#' . $row_color,

				'L_MODULE_NAME' => $mod_name
				)
			);
			// Increase the module counter
			$i++;
		}
	}
}

$template->pparse('body');
include('./page_footer_admin.' . $phpEx);

?>