<?php

if ($_POST['submit'] != '' && $_POST['viptID'] == '0') {
	/* Update of Achrive DB */
	if ($_POST['wctoverlay'] != '1') { $_POST['wctoverlay'] = '0'; } if ($_POST['wctheaderline'] != '1') { $_POST['wctheaderline'] = '0'; } if ($_POST['wctsheme'] != '1') { $_POST['wctsheme'] = '0'; }
	$wpdb->get_row("UPDATE `".$wpdb->prefix."wct_list` SET `overlay`='".mres($_POST['wctoverlay'])."', `headerline`='".mres($_POST['wctheaderline'])."', `sheme`='".mres($_POST['wctsheme'])."' WHERE `id`='0' LIMIT 1;");
}
elseif ($_POST['submit'] != '' && $_POST['wctdelete'] != '1') {
	if ($_GET['wcttab2'] == 'sindex') {

		$tablet = $wpdb->get_row("SHOW CREATE TABLE `".$wpdb->prefix."wct".mres($_POST['viptID2'])."`;");
		$array=array(); foreach($tablet as $member=>$data) { $array[$member]=$data; }
		$felderm = explode("PRIMARY KEY",$array['Create Table']);
		$felder = array_slice(explode("`",$felderm[0]),5);
		$indexes = explode("`",$felderm[1]);

		/* check for all fields */
		for ($x=0;$felder[$x] !='';$x++) {
			$r['wcti_'.$felder[$x]] = '0';
			$j['wcti_'.$felder[$x]] = '0';
			$x++;
		}

		/* set status for all fields */
		foreach($_POST as $var => $wert) {
			if (strpos($var,"wcti_") !== false) {
				$r[$var] = $wert;
			}
		}

		/* find existing indexes */
		for ($x=3;$indexes[$x] !='';$x++) {
			$j['wcti_'.$indexes[$x]] = '1';
			$x++;
		}

		foreach ($r as $var => $wert) {
			if ($wert != $j[$var] AND $j[$var] != '1') {
				/* set new index */
				$addnew .= "ADD FULLTEXT INDEX `".str_replace("wcti_","",$var)."` (`".str_replace("wcti_","",$var)."`(32)),";
			}
			elseif ($wert != $j[$var] AND $j[$var] == '1') {
				/* delete index */
				$addnew .= "DROP INDEX `".str_replace("wcti_","",$var)."`,";
			}

		}
		if ($addnew != '') { $wpdb->get_row("ALTER TABLE `".$wpdb->prefix."wct".mres($_POST['viptID2'])."` ".mres(substr($addnew,0,strlen($addnew)-1)).";"); }
	}
	elseif ($_GET['page'] == 'wct_table_create') {
		/* Table not existing, creating */

		$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'] != '') {
		/* Alter a DB field */
		$wpdb->get_row("ALTER TABLE `".$wpdb->prefix."wct".mres($_POST['viptID2'])."` CHANGE COLUMN `".mres($_POST['wctfren'])."` `".mres($_POST['wctrfield'])."` ".mres($_POST['wcthidden'])." NULL DEFAULT NULL;");
	}
	elseif ($_POST['vipName'] != ''){
		/* Update of the table, create new fields  if needed and drop old ones */
		if ($_POST['wctoverlay'] != '1') { $_POST['wctoverlay'] = '0'; } if ($_POST['wctheaderline'] != '1') { $_POST['wctheaderline'] = '0'; } if ($_POST['wctsheme'] != '1') { $_POST['wctsheme'] = '0'; }
		$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';
}

?>