<?php

// Save Settings
$geo_capblack_legal = get_option('geo_capblack_legal');
 
// Change Settings
if ('changesettings' == $_POST['action'])
{
    update_option("geo_capblack_legal", $_POST['geo_capblack_legal']);
	update_option("geo_capblack_black", $_POST['geo_capblack_black']);
	update_option("geo_capblack_log_ips", $_POST['geo_capblack_log_ips']);
	update_option("geo_capblack_comments", $_POST['geo_capblack_comments']);
	update_option("geo_capblack_registration", $_POST['geo_capblack_registration']);
}

// Clear Log
if ('clear_log' == $_POST['action'])
{
	$sql_stats = 'TRUNCATE `' . $table_prefix . 'geo_capblack_log`';
		$wpdb->query(sprintf($sql_stats));
}

// Uninstall
function geo_capblack_uninstall() {
	global $wpdb, $table_prefix;

	delete_option('geo_capblack_legal');
	delete_option('geo_capblack_blocked_spam');
	delete_option('geo_capblack_not_whitelisted_comments');
	delete_option('geo_capblack_not_whitelisted_comments');
	delete_option('geo_capblack_whitelisted_comments');
	delete_option('geo_capblack_registered_comments');
	delete_option('geo_capblack_log_ips');
	
	$wpdb->query(sprintf('DROP TABLE `' . $table_prefix . 'geo_capblack_stats`'));
	$wpdb->query(sprintf('DROP TABLE `' . $table_prefix . 'geo_capblack_log`'));

	deactivate_plugin();
	update_option('recently_activated', array("geo-captcha-blacklist/geo-captcha-blacklist.php" => time()) + (array)get_option('recently_activated'));
	if(function_exists($wp_redirect)) {
		wp_redirect('index.php');
	} else {
		header("Location: index.php");
		exit;
	}
}

// Activate Uninstall
if ('full_uninstall_geo_capblack' == $_POST['action'] && 'y' == $_POST['uninstall_sure'])
{
	geo_capblack_uninstall();
}

// Deactivate Plugin
function deactivate_plugin() {
    $current = get_option('active_plugins');
    if(in_array("geo-captcha-blacklist/geo-captcha-blacklist.php", $current))
		array_splice($current, array_search("geo-captcha-blacklist/geo-captcha-blacklist.php", $current), 1);
    update_option('active_plugins', $current);
}

// Include pages
require_once(WP_PLUGIN_DIR . '/geo-captcha-geo-blacklist/geo-captcha-blacklist-page-settings.php');
require_once(WP_PLUGIN_DIR . '/geo-captcha-geo-blacklist/geo-captcha-blacklist-page-about.php');
require_once(WP_PLUGIN_DIR . '/geo-captcha-geo-blacklist/geo-captcha-blacklist-page-log.php');
require_once(WP_PLUGIN_DIR . '/geo-captcha-geo-blacklist/geo-captcha-blacklist-page-stats.php');

 
// Adds the option page for admin menu
function geo_capblack_add_menu() {
	global $submenu;

	add_option("geo_capblack_legal","DE\nAT\nCH");
	add_option("geo_capblack_black","");
	add_option("geo_capblack_blocked_spam","0");
	add_option("geo_capblack_not_whitelisted_comments","0");
	add_option("geo_capblack_whitelisted_comments","0");
	add_option("geo_capblack_registered_comments","0");
	add_option("geo_capblack_log_ips","1");
	add_option("geo_capblack_comments","1");
	add_option("geo_capblack_registration","1");
	
	add_option("geo_capblack_manuell_spam","0");
	add_option("geo_capblack_whitelisted_spam","0");

	add_menu_page(__('About', 'geo-captcha-blacklist'), 'Geo Captcha', 8, 'geo-captcha-blacklist.php', 'geo_capblack_about_page', WP_PLUGIN_URL . '/geo-captcha-geo-blacklist/images/geo-captcha-blacklist-icon16.png');
	add_submenu_page('geo-captcha-blacklist.php', __('Settings', 'geo-captcha-blacklist'), __('Settings', 'geo-captcha-blacklist'), 8, 'geo-captcha-blacklist-settings', 'geo_capblack_option_page');
	add_submenu_page('geo-captcha-blacklist.php', __('Log', 'geo-captcha-blacklist'), __('Log', 'geo-captcha-blacklist'), 8, 'geo-captcha-blacklist-logs', 'geo_capblack_log_page');
	add_submenu_page('geo-captcha-blacklist.php', __('Stats', 'geo-captcha-blacklist'), __('Stats', 'geo-captcha-blacklist'), 8, 'geo-captcha-blacklist-stats', 'geo_capblack_stats_page');
	
	$submenu['geo-captcha-blacklist.php'][500] = array( 'Support', 'read' , 'http://support.deformed-design.de' );
}

// Uninstall
if ( function_exists('register_uninstall_hook') )
    register_uninstall_hook(__FILE__, 'geo_capblack_uninstall');

// Register the WordPress-Hooks 
add_action('admin_menu', 'geo_capblack_add_menu');
?>