<?php

if ($_GET['dellic'] == '1') {
	$this->settings['form_serial'] = '';
	$this->settings['form_serialvu'] = '0';
	update_option('wuk_custom_tables', $this->settings);
}
elseif ($_POST['submit'] != '' AND $_GET['validate'] != '1') {
	$this->settings['role_ct'] = $_POST['wctrole_ct'];
	$this->settings['role_css'] = $_POST['wctrole_css'];
	$this->settings['role_archive'] = $_POST['wctrole_archive'];

	if ($_POST['wct_unfilteredhtml'] == '1') { $this->settings['wct_unfilteredhtml'] = '1'; }
	else { $this->settings['wct_unfilteredhtml'] = '0'; }

	if ($_POST['wct_immaster'] != 'yes') { $_POST['wct_immaster'] = 'no'; }
	$this->settings['wct_immaster'] = $_POST['wct_immaster'];

	$qry = $wpdb->get_results("SELECT `id`,`name` FROM `".$wpdb->prefix."wct_list`;");
	foreach ($qry as $row) {
		if ($_POST['wctreset'] != '1') {
			$this->settings['crole'.$row->id.'_s'] = $_POST['wctrole_crole'.$row->id.'_s'];
			$this->settings['crole'.$row->id.'_c'] = $_POST['wctrole_crole'.$row->id.'_c'];
			$this->settings['crole'.$row->id.'_i'] = $_POST['wctrole_crole'.$row->id.'_i'];
		}
		else {
			$this->settings['crole'.$row->id.'_s'] = $_POST['wctrole_crolem_s'];
			$this->settings['crole'.$row->id.'_c'] = $_POST['wctrole_crolem_c'];
			$this->settings['crole'.$row->id.'_i'] = $_POST['wctrole_crolem_i'];
		}
	}

	$this->settings['wct_cachetime'] = $_POST['wctrole_cachetime'];

	update_option('wuk_custom_tables', $this->settings);

	echo __('Changes successfully saved', 'wct');
}

echo "<style type=\"text/css\">
fieldset
{
	margin-top: 1em;
	margin-right: 0pt;
	margin-bottom: 1em;
	margin-left: 0pt;
	background-color: #fdfdfd;
	background-image: none;
	background-repeat: repeat;
	background-attachment: scroll;
	background-position: 0% 0%;
	padding-top: 0pt;
	padding-right: 1em;
	padding-bottom: 1em;
	padding-left: 1em;
	border-top-width: 1px;
	border-right-width-value: 1px;
	border-right-width-ltr-source: physical;
	border-right-width-rtl-source: physical;
	border-bottom-width: 1px;
	border-left-width-value: 1px;
	border-left-width-ltr-source: physical;
	border-left-width-rtl-source: physical;
	border-top-style: solid;
	border-right-style-value: solid;
	border-right-style-ltr-source: physical;
	border-right-style-rtl-source: physical;
	border-bottom-style: solid;
	border-left-style-value: solid;
	border-left-style-ltr-source: physical;
	border-left-style-rtl-source: physical;
	border-top-color: #bbbbbb;
	border-right-color-value: #bbbbbb;
	border-right-color-ltr-source: physical;
	border-right-color-rtl-source: physical;
	border-bottom-color: #bbbbbb;
	border-left-color-value: #bbbbbb;
	border-left-color-ltr-source: physical;
	border-left-color-rtl-source: physical;
}
</style>
<form action=\"admin.php?page=".$_GET['page']."\" method=\"post\" onSubmit=\"return validate(this)\" name=\"wctform\">".__('Setup who can access which pages of the plugin.<br/>You can only break down on groups, not on users!','wct')."<fieldset><legend><strong>" . __('Rights', 'wct') . "</strong></legend><h3>". __('Global rights', 'wct')."</h3><table>";
$this->tablerights(__('Create Table', 'wct') ,'ct',$this->settings['role_ct']);
$this->tablerights(__('Edit CSS', 'wct') ,'css',$this->settings['role_css']);
$this->tablerights(__('Archive Setup', 'wct') ,'archive',$this->settings['role_archive']);

echo "</table><h3>". __('Table rights', 'wct')."</h3><table><tr><td valign=\"top\"><table>";
$qry = $wpdb->get_results("SELECT `id`,`name` FROM `".$wpdb->prefix."wct_list`;");
foreach ($qry as $row) {
	if (!array_key_exists('crole'.$row->id.'_s',$this->settings)) {
		$this->settings['crole'.$row->id.'_s'] = 'administrator';
		update_option('crole'.$row->id.'_s', $this->settings);
	}
	if (!array_key_exists('crole'.$row->id.'_c',$this->settings)) {
		$this->settings['crole'.$row->id.'_c'] = 'contributor';
		update_option('crole'.$row->id.'_c', $this->settings);
	}
	if (!array_key_exists('crole'.$row->id.'_i',$this->settings)) {
		$this->settings['crole'.$row->id.'_i'] = 'editor';
		update_option('crole'.$row->id.'_i', $this->settings);
	}
	$this->tablerights("&raquo; ".$row->name." ".__('Setup Option', 'wct'),'crole'.$row->id.'_s',$this->settings['crole'.$row->id.'_s']);
	$this->tablerights("&raquo; ".$row->name." ".__('Import/Export', 'wct'),'crole'.$row->id.'_i',$this->settings['crole'.$row->id.'_i']);
	$this->tablerights("&raquo; ".$row->name." ".__('Content', 'wct'),'crole'.$row->id.'_c',$this->settings['crole'.$row->id.'_c']);
	echo "<tr height=\"10\"><td colspan=\"2\"></td></tr>";
}
echo "</table></td><td style=\"background-image:url(" . plugins_url('custom-tables/img/table.png') . ");\" valign=\"bottom\"><img src=\"".plugins_url('custom-tables/img/tableend.png')."\" border=\"0\" alt=\"\"/></td><td valign=\"top\">
<input type=\"checkbox\" name=\"wctreset\" value=\"1\"> ".__('Reset all tables to following Standard').":<table>";

$this->tablerights( __('Setup Option', 'wct'),'crolem_s','administrator');
$this->tablerights( __('Import/Export', 'wct'),'crolem_i','editor');
$this->tablerights( __('Content', 'wct'),'crolem_c','contributor');

echo "</table></td></tr></table><input type=\"submit\" name=\"submit\" value=\"". __('Save all Changes', 'wct') ."\"></fieldset>
<fieldset><legend><strong>" . __('Cache', 'wct') . "</strong></legend><table>";
$this->option(__('Cache expire', 'wct'),'cachetime',$this->settings['wct_cachetime'],__('How long is the Cache valid in secounds', 'wct'));
echo "</table>". __('<b>Attention:</b> Cache functionality will only work, if the Caching functionality in WordPress is enabled and a cache plugin is installed.','wct')."<br/><input type=\"submit\" name=\"submit\" value=\"". __('Save all Changes', 'wct') ."\"></fieldset>

<fieldset><legend><strong>" . __('Advanced Features', 'wct') . "</strong></legend>".
__('Table Indexes', 'wct')." <input type=\"checkbox\" name=\"wct_immaster\" value=\"yes\"";
if ($this->settings['wct_immaster'] == 'yes') { echo " checked"; }
echo "> ".__('If you want to enable Table Indexes to define which fields can be searched faster.', 'wct')."<br/>".
__('<b>Attention:</b> This feature should only be used by Database Engineers which knowing what they are doing. In fact, correct indexes can speedup the database by user searches, but on wrong indexes destroy database performance at all.','wct')."<br/><br/>".

__('Disable HTML Filter', 'wct')." <input type=\"checkbox\" name=\"wct_unfilteredhtml\" value=\"1\"";
if ($this->settings['wct_unfilteredhtml'] == '1') { echo " checked"; }
echo "> ".__('WordPress filters the HTML befor it saves. This filter removes &lt;p&gt; and &lt;br&gt; Tags. If you don\' like it, disable it here.', 'wct')."<br/>".
"<br/><input type=\"submit\" name=\"submit\" value=\"". __('Save all Changes', 'wct') ."\"></fieldset></form>";

echo "<fieldset id=\"premium\" name=\"premium\"><legend><strong>PREMIUM FEATURE</strong></legend>";

if ($this->settings['form_serial'] == '') {
	if ($_POST['wct_serial_key'] != '') {
		if (preg_match('/^[0-9]{4}-[0-9]{4}-[0-9]{4}$/', trim($_POST['wct_serial_key']))) {
			$data = $this->http_request('POST','http://api.wuk.ch/wct-premium.php','serial='.trim($_POST['wct_serial_key'])."&dom=".$_SERVER['SERVER_NAME']);
			if (strpos($data,"|") !== false) {
				$tmp = explode("|",$data);
				$data = $tmp[0];
				$vu = $tmp[1];
			}
			if (preg_match('/^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}$/', $data)) {
				if ($this->prem_chk($data) == true) {
					$this->settings['form_serial'] = $data;
					if ($vu != '') { $this->settings['form_serialvu'] = $vu; }
					update_option('wuk_custom_tables', $this->settings);
					echo __('Thanks for supporting web updates kmu. Please reload page.', 'wct')."<br/><br/>";
				}
				else {
					printf("<br/><br/><font color=\"red\">".__('Serial not accepted: \'%s\'', 'wct')."</font>", $data);
				}
			}
			else {
				printf("<br/><br/><font color=\"red\">".__('Serial not accepted: \'%s\'', 'wct')."</font>", $data);
			}
		}
		else {
			echo "<form action=\"admin.php?page=wct_settings&validate=1\" method=\"POST\"><b>".__('Serial', 'wct').":</b> <input type=\"text\" size=\"15\" name=\"wct_serial_key\" /> <input type=\"submit\" value=\"".__('Validate','wct')." *\"></form>";
			echo "<i>* ". __('Validation need to have online connection to web updates kmu server!','wct')."</i>";
			echo "<br/><br/><font color=\"red\">".__('Please check the serial number, the submitted serial number has a wrong format', 'wct')."</font>";
		}
	}
	else {
		echo "<form action=\"admin.php?page=wct_settings&validate=1\" method=\"POST\"><b>".__('Serial', 'wct').":</b> <input type=\"text\" size=\"15\" name=\"wct_serial_key\" /> <input type=\"submit\" value=\"".__('Validate','wct')." *\"></form>";
		echo "<i>* ". __('Validation need to have online connection to web updates kmu server!','wct')."</i>";
		$aktuellekosten = '30.00';

		$paypal = array('_cart','Custom Table - Premium Feature License for '.$_SERVER['SERVER_NAME'],$aktuellekosten,'');
		echo "<table><tr><td valign=\"top\">";
		include($this->wctpath."pages/paypal.php");
		echo "</td><td valign=\"top\"><br/>";
		printf( __('Premium feature will remain for 25 years and all new WCT Premium feature will be automatically licenced with this serial!<br/>The licence costs are %s &euro; *','wct'), $aktuellekosten);
		echo "</td></tr></table>";
	}
}
elseif ($this->settings['form_serialvu'] >= time())  {
	if ($this->settings['form_serialvu'] != '') { printf( __('Premium features licensed. Valid until %s.','wct')."<br/><br/>", date("Y-m-d",$this->settings['form_serialvu'])); }
}
else {
	echo __('The license has expired, please delete and renew it','wct')." <a href=\"admin.php?page=wct_settings&dellic=1\">". __('now','wct')."</a>.";
}
echo "<b>". __('Features which are included','wct').":</b>
<LU>
<li>". __('Custom Forms','wct')."</li>
<li>". __('Category Widget (on enum fields)','wct')."</li>
<li>". __('Search & Replace in Table','wct')."</li>
</LU>";
if ($this->settings['form_serial'] == '') {
	echo "<br/>* ". __('Please note, the key price can increase in the future, the valid price is in the latest plugin version committed. Prices in older Pluginversions are not valid and don\'t garantee the same price.','wct')."<br/>* ". __('Premium Feature License is valid for 1 Domain only!','wct')."</fieldset>";
}
?>