<?php

if ($_POST['submit'] != '' && $_POST['wctdelete'] != '1') {
	if ($_GET['page'] == 'wct_table_create') {

		$wpdb->get_row("INSERT INTO `".$wpdb->prefix."wct_list` SET `name`='".mres($_POST['vipName'])."', `secret`='".md5("wct_".time())."', `overlay`='".mres($_POST['wctoverlay'])."', `headerline`='".mres($_POST['wctheaderline'])."', `sheme`='".mres($_POST['wctsheme'])."';");
		$tableid = mysql_insert_id();
		$create = "CREATE TABLE `".$wpdb->prefix."wct".$tableid."` (\n`id` int(11) NOT NULL AUTO_INCREMENT,\n";

		foreach ($_POST['vipInfo'] as $var => $wert) {
			/*Filter for: Put only alphanumberic values as fieldnames into the database*/
			$wert = filter_var(strtolower(str_replace(" ","_",$wert)), FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z0-9]+$/")));
			if ($wert != '') {
				if ($_POST['vipInfoB'][$var] == 'varchar32') { $create .= "`".mres($wert)."` varchar(32) NULL DEFAULT NULL,\n"; }
				elseif ($_POST['vipInfoB'][$var] == 'varchar64') { $create .= "`".mres($wert)."` varchar(64) NULL DEFAULT NULL,\n"; }
				elseif ($_POST['vipInfoB'][$var] == 'int11') { $create .= "`".mres($wert)."` int(11) NULL DEFAULT NULL,\n"; }
				elseif ($_POST['vipInfoB'][$var] == 'smallint6') { $create .= "`".mres($wert)."` smallint(6) NULL DEFAULT NULL,\n"; }
				elseif ($_POST['vipInfoB'][$var] == 'float82') { $create .= "`".mres($wert)."` float(8,2) NULL DEFAULT NULL,\n"; }
				else { $create .= "`".mres($wert)."` text NULL DEFAULT NULL,\n"; }
			}
		}

		$create .=  "PRIMARY KEY (`id`)\n) DEFAULT CHARSET=utf8;";
		$wpdb->get_row($create);
		$_GET['page'] = 'wct_table_'.$tableid;
	}
	elseif($_POST['wctfren'] != '' AND $_POST['wctrfield'] != '') {
		$wpdb->get_row("ALTER TABLE `".$wpdb->prefix."wct".$tableid."` CHANGE COLUMN `".mres($_POST['wctfren'])."` `".mres($_POST['wctrfield'])."` ".mres($_POST['wcthidden'])." NULL DEFAULT NULL;");
	}
	elseif ($_POST['vipName'] != ''){
		/*Update der Table, gegebenenfalls neue Werte in die DB schreiben!*/
		$wpdb->get_row("UPDATE `".$wpdb->prefix."wct_list` SET `name`='".mres($_POST['vipName'])."', `overlay`='".mres($_POST['wctoverlay'])."', `headerline`='".mres($_POST['wctheaderline'])."', `sheme`='".mres($_POST['wctsheme'])."' WHERE `id`='".mres($_POST['viptID'])."' LIMIT 1;");

		$zeile = '0';
		$weg= array_diff($_POST['vipInfoC'],$_POST['vipInfo']);
		foreach ($weg as $var => $wert) {
			if ($zeile >= '1') { $out .= ",\n"; } $zeile++;
			$out .= "DROP COLUMN `".$wert."`";
		}

		$dazu = array_diff($_POST['vipInfo'],$_POST['vipInfoC']);
		foreach ($dazu as $var => $wert) {
			foreach ($_POST['vipInfo'] as $a => $b) {
				if ($b == $wert) {
					$gefunden = $_POST['vipInfoB'][$a];
				}
			}
			if ($zeile >= '1') { $out .= ",\n"; } $zeile++;

			if ($gefunden == 'varchar32') { $out .= "ADD COLUMN `".mres($wert)."` varchar(32) NULL DEFAULT NULL"; }
			elseif ($gefunden == 'varchar64') { $out .= "ADD COLUMN `".mres($wert)."` varchar(64) NULL DEFAULT NULL"; }
			elseif ($gefunden == 'int11') { $out .= "ADD COLUMN `".mres($wert)."` int(11) NULL DEFAULT NULL"; }
			elseif ($gefunden == 'smallint6') { $out .= "ADD COLUMN `".mres($wert)."` smallint(6) NULL DEFAULT NULL"; }
			elseif ($gefunden == 'float82') { $out .= "ADD COLUMN `".mres($wert)."` float(8,2) NULL DEFAULT NULL"; }
			else { $out .= "ADD COLUMN `".mres($wert)."` text NULL DEFAULT NULL"; }
		}
		$wpdb->get_row("ALTER TABLE `".$wpdb->prefix."wct".mres($_POST['viptID'])."` ".$out.";");
	} 
}
elseif ($_POST['wctdelete'] == '1') {
	/* Table will be deleted from index and itself with the data*/
	$wpdb->get_row("DELETE FROM `".$wpdb->prefix."wct_list` WHERE `id`='".mres($_POST['vipID'])."' LIMIT 1;");
	$wpdb->get_row("DROP TABLE `".$wpdb->prefix."wct".mres($_POST['vipID'])."`;");
	$_GET['page'] = 'wct_table_create';
}
?>