<?php

/* hack for qtranslatesupport */
if(!function_exists('qtrans_getLanguage')){function qtrans_getLanguage(){}}

if ($_GET['wcteid'] == '') {
	$url = $this->generate_pagelink("/[&?]+wcteid=[0-9]*/","");
	$cache = eregi_replace("[^a-zA-Z]","",md5($filter));
	$out = wp_cache_get( 'wct_table'.$id.$cache.$_GET['wctdrop'].$_REQUEST['wctstart'].'s'.$_REQUEST['wctsort'].$_REQUEST['wctinvs'] , 'wct');
	if ($out == false OR is_user_logged_in()) {

		if ($design != '' AND $design != '') {
			$designnotfound = '0'; 
			$table = $wpdb->get_row("SELECT `e_setup`,`t_setup`,`overlay`,`headerline`,`header`,`headersort`,`vortext`,`nachtext`,`o_setup`,`sheme`,`sort`,`sortB`,`searchaddon` FROM `".$wpdb->prefix."wct_setup` WHERE `table_id`='".$id."' AND `id`='".mres($design)."' LIMIT 1;");
			if (count($table) != '1') { $designnotfound = '1';  }
		}
		if ($designnotfound == '1' OR !isset($designnotfound)) {
			$table = $wpdb->get_row("SELECT `e_setup`,`t_setup`,`overlay`,`headerline`,`header`,`headersort`,`vortext`,`nachtext`,`o_setup`,`sheme`,`sort`,`sortB`,`searchaddon` FROM `".$wpdb->prefix."wct_list` WHERE `id`='".$id."' LIMIT 1;");
		}

		if ($table->t_setup != '') {
			global $wctcssdone;
			if ($wctcssdone != '1') { $out = "<style>".stripslashes($this->settings['css'])."</style>"; $wctcssdone = '1'; }
			else { $out = ''; }

			if ($table->vortext != '') { $out .= rbr(stripslashes($table->vortext))."<br/>&nbsp;"; }
			if ($table->overlay == '1') {
			$out .= "<script>
				function box_show(name,namB,color) {
					document.getElementById(\"hH\").value = document.getElementById(name).offsetHeight;
					document.getElementById(namB).setAttribute(\"class\", color);
					document.getElementById(name).style.top = document.getElementById(\"My\").value+\"px\";
					document.getElementById(name).style.left = document.getElementById(\"Mx\").value+\"px\";
					document.getElementById(name).style.visibility = \"visible\";
					document.getElementById(name).style.display = 'block';
				}
				function box_hide(name,namB,color) {
					document.getElementById(namB).setAttribute(\"class\", color);
					document.getElementById(name).style.visibility = \"hidden\";
					document.getElementById(name).style.display = 'none';
				}
				var IE = false;
				if (navigator.appName == \"Microsoft Internet Explorer\"){IE = true}
				if (!IE){document.captureEvents(Event.MOUSEMOVE) }
				document.onmousemove = getMouseXY;
				function getMouseXY(m){
					if (IE) {var tmpX = event.clientX;var tmpY = event.clientY;}
					else if(m.clientY){var tmpX = m.clientX;var tmpY = m.clientY;}
				 	else {var tmpX = m.pageX;var tmpY = m.pageY;}

					if (IE) {
						if (document.documentElement.scrollTop) {var iL = document.documentElement.scrollLeft;var iV = document.documentElement.scrollTop;var hH = document.documentElement.offsetHeight;var wH = document.documentElement.offsetWidth;}
						else if (document.body.scrollTop) {var iL = document.body.scrollLeft;var iV = document.body.scrollTop; var hH = document.body.offsetHeight;var wH = document.body.offsetWidth;}
						else { iL = 0; iV = 0; }

						if ('IE6' == '";
						if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== false) { $pos = "IE6"; }
						$out .= $pos."') {
							var ie6pos = findPos('wct-table');
							iV = iV - ie6pos[1] +140;
							iL = iL - ie6pos[0] +12;
						}
					}
					else {
						iL = 0; iV = 0;
						var hH = window.innerHeight;var wH = window.innerWidth;
					}

				  	var breite = tmpX + iL + 10;
				  	var hohe = tmpY + iV;

					var divhohe = parseInt(document.getElementById(\"hH\").value);
					if (hH < (hohe + divhohe)) {
						hohe = hH - divhohe;
					}

					document.getElementById(\"My\").value = hohe;
					document.getElementById(\"Mx\").value = breite;
				}
				function findPos(obj) {
					obj = document.getElementById(obj);

					var posX = obj.offsetLeft;var posY = obj.offsetTop;

					while(obj.offsetParent) {
						if(obj==document.getElementsByTagName('body')[0]){break;}
						else {
							posX=posX+obj.offsetParent.offsetLeft;
							posY=posY+obj.offsetParent.offsetTop;
							obj=obj.offsetParent;
						}
					}
					var posArray=[posX,posY];
					return posArray;
				}
				</script>
				<input type=\"hidden\" id=\"Mx\"><input type=\"hidden\" id=\"My\"><input type=\"hidden\" id=\"hH\">";
			}
			$out .= "<table name=\"wct-table\" id=\"wct-table\" class=\"wct".$css."-table\">";

			if ($table->headerline == '1') {
				$out .= "<tr class=\"wct".$css."-headline\">";
				$y = '0';

				$sort = explode(",",$table->headersort);
				if ($table->header == '') {
					$inhalt = str_replace("[LINK]","",apply_filters('wct_table',$table->t_setup));
					preg_match_all("/\[(.*?)\]/",$inhalt,$felde);
					$felder = $felde[1];
					foreach ($felder as $feld) {
						$out .= "<td><b>".$feld."</b>";
						if ($sort[$y] != '') { $out .= " ".$this->wct_sort($sort[$y],$table); }
						$out .= "</td>";
						$y++;
					}
				}
				else {
					$inhalt = stripslashes($table->header);
					$felder = explode(",",$inhalt);
					foreach ($felder as $feld) {
						/*qtranslate support */
						if (preg_match('/.*!--:'.qtrans_getLanguage().'-->(.*?)<!--:--.*/',rbr($feld))) {
							/* Language found */
							preg_match('/.*!--:'.qtrans_getLanguage().'-->(.*?)<!--:--.*/',rbr($feld), $treffer);
							$feld = $treffer[1];
						}
						$out .= "<td><b>".rbr(do_shortcode($feld))."</b>";
						if ($sort[$y] != '') { $out .= " ".$this->wct_sort($sort[$y],$table); }
						$out .= "</td>";
						$y++;
					}
				}
				$out .= "</tr>";
			}

			/* Welche Felder sind erlaubt zum suchen */
			if ($nofilter == '' AND $_GET['wcts'] != '') {
				$wanted = ",".str_replace("%2C",",",$_GET['wctsf']).",";
				$table2 = $wpdb->get_row("SHOW CREATE TABLE `".$wpdb->prefix."wct".$id."`;");
				$array=array(); foreach($table2 as $member=>$data) { $array[$member]=$data; }
				$felder = explode(",",str_replace(array("8,2","','","\r","\n"),array("8.2","'.'","",""),$array['Create Table']));

				unset($feld2,$feld3,$feld4);
				$feld2[1]['i'] = 'id';
				$feld2[1]['d'] = 'filter';

				for ($i=2;$felder[$i] != '';$i++) {

					if (strpos ($felder[$i-1]," enum") !== false) {
						 $feld3[$i]['i'] = preg_replace("/.*`(.*)`\s.*/","$1",$felder[$i-1]);
					}
					elseif (strpos ($felder[$i-1]," set") !== false) {
						 $feld2[$i]['i'] = preg_replace("/.*`(.*)`\s.*/","$1",$felder[$i-1]);
					}
					elseif (strpos ($felder[$i-1]," text") !== false) {
						 $feld2[$i]['i'] = preg_replace("/.*`(.*)`\s.*/","$1",$felder[$i-1]);
						 $feld2[$i]['d'] = "pass";
					}
					elseif (strpos ($felder[$i-1]," int(10)") !== false) {
						 $feld4[$i]['i'] = preg_replace("/.*`(.*)`\s.*/","$1",$felder[$i-1]);
					}
					else {
						 $feld2[$i]['i'] = preg_replace("/.*`(.*)`\s(.*\))\s.*/","$1",$felder[$i-1]);
						 $feld2[$i]['d'] = "pass";
					}
				}
				unset($searchfields,$searchfields2);
				foreach ($feld2 as $var => $wert) {
					if ($_GET['wctsf'] != '*') {
						if (strpos($wanted,",".$wert['i'].",") !== FALSE) {
							if ($wert['d'] != 'filter') { $searchfields .= ",LOWER(`".$wert['i']."`)"; }
							else { $searchfields .= ",`".$wert['i']."`"; }
						}
					}
					else {
						if ($wert['d'] != 'filter') { $searchfields .= ",LOWER(`".$wert['i']."`)"; }
						else { $searchfields .= ",`".$wert['i']."`"; }
					}
				}
				foreach ($feld3 as $werd) {
					if ($werd['i'] != 'status') { $searchfields2 .= "OR `".$werd['i']."`='".mres($_GET['wcts'])."' "; }
				}

				if ($_GET['wctsz'] == '') {
					$query2 = $_GET['wcts'];
					if (preg_match("/^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{4})$/", $query2, $regs)) { $db_timestamp = mktime(1,1,1,$regs[2], $regs[1], $regs[3]); }
					elseif (preg_match("/^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{2})$/", $query2, $regs)) { $db_timestamp = mktime(1,1,1,$regs[2], $regs[1], '20'.$regs[3]); }
					elseif (preg_match("/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})$/", $query2, $regs)) { $db_timestamp = mktime(1,1,1,$regs[1], $regs[2], $regs[3]); }
					elseif (preg_match("/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2})$/", $query2, $regs)) { $db_timestamp = mktime(1,1,1,$regs[1], $regs[2], '20'.$regs[3]); }
					elseif (preg_match("/^([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})$/", $query2, $regs)) { $db_timestamp = mktime(1,1,1,$regs[1], $regs[2], $regs[3]); }
					elseif (preg_match("/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/", $query2, $regs)) { $db_timestamp = mktime(1,1,1,$regs[3], $regs[2], $regs[1]); }
					elseif (preg_match("/^([0-9]{1,2})\.([0-9]{1,2})\.$/", $query2, $regs)) { $db_timestamp = mktime(1,1,1,$regs[2], $regs[1], date("Y",time())); }
					else {	unset($db_timestamp); }	
				}
				else { $db_timestamp = (integer)$_GET['wctsz']; }

				if (isset($db_timestamp) AND $db_timestamp != '') {
					foreach ($feld4 as $werd) {
						$searchfields2 .= "OR `".$werd['i']."`='".$db_timestamp."' ";
					}
					if ($table->searchaddon != '') {
						$searchfields2 .= "OR (".str_replace("SEARCH",$db_timestamp,$table->searchaddon).") ";
					}
				}

				if ($_GET['wctsf'] == '*') { $searchfields = str_replace(array(",".$wpdb->prefix."wct".$id,",id"),array("",""),$searchfields); }
				if ($searchfields != '') {
					$searchfields = substr($searchfields,1,strlen($searchfields));
				}
			}

			/* Searchstring if needed */
			if ($nofilter == '' AND $_GET['wcts'] != '' AND $searchfields != '') {
				$sf = $searchfields;
				$zusatz = " AND ( CONCAT( ".$sf." ) LIKE '%".mres(strtolower ($_GET['wcts']))."%' ".$searchfields2.")";
			}

			/* Filter if needed */
			if ($nofilter == '' AND isset($_GET['wctdrop']) AND $_GET['wctdrop'] != 'NULL' AND $_GET['wctdrof'] != '') {
				$zusatz .= " AND `".mres($_GET['wctdrof'])."` LIKE '".mres(base64_decode($_GET['wctdrop']))."'";
			}
			if ($this->lastfilter != '') {
				$zusatz .= $this->lastfilter;
			}

			/* How it should be sorted */
			if ($nofilter == '' AND $_REQUEST['wctsort'] != '') {
				$zusatz .= " ORDER BY `".mres($_REQUEST['wctsort'])."` ";
				if ($_REQUEST['wctinvs'] == '1') { $zusatz .= "ASC"; } else { $zusatz .= "DESC"; }
			}
			else {
				$zusatz .= " ORDER BY `".$table->sort."` ".$table->sortB." "; 
			}

			/* Limit of Results, Next Pages */
			if ($_REQUEST['wctstart'] != '') {
				$start = (integer)$_REQUEST['wctstart'];
				$limit2 = ($start-1).",".$limit;
			} else { $limit2 = $limit; }

			$abfrage = "SELECT * FROM `".$wpdb->prefix."wct".$id."` WHERE `status`='active'".$zusatz." LIMIT ".$limit2.";";

			$qry = $wpdb->get_results($abfrage);
			if (count($qry) >= '1') {

				if ($this->settings['nice_setfields'] == '1') {
					$table2 = $wpdb->get_row("SHOW CREATE TABLE `".$wpdb->prefix."wct".$id."`;");
					$array=array(); foreach($table2 as $member=>$data) { $array[$member]=$data; }
					$felder = explode(",",str_replace(array("8,2","','","\r","\n"),array("8.2","'.'","",""),$array['Create Table']));
					for ($i=2;$felder[$i] != '';$i++) {
						if (strpos ($felder[$i-1]," set") !== false) {
							$nicesetfeld[] = preg_replace("/.*`(.*)`\s.*/","$1",$felder[$i-1]);
						}
					}
				}

				foreach ($qry as $row) {

					if ($this->settings['nice_setfields'] == '1') {
						foreach ($nicesetfeld as $wert) {
							$row->$wert = str_replace(",",", ",$row->$wert);
						}
					}

					// Remove breaklines between table
					$inhalt = $this->filter_tables(apply_filters('wct_table',$table->t_setup));

					// Replace link
					$inhalt = rbr(str_replace("[LINK]",$url."wcteid=".$row->id,stripslashes($inhalt)));

					if ($color == "1") { $color = "2"; } else { $color = "1"; }

					$out .= "<tr class=\"wct".$css."-td".$color."\" id=\"wct_omaster".$row->id."\" ";
					if ($table->overlay == '1' AND strpos($inhalt,"[wctoverlay]") === FALSE) { $out .= "onmousemove=\"box_show('wct_overlay".$row->id."','wct_omaster".$row->id."','wct".$css."-td-hover')\" onmouseout=\"box_hide('wct_overlay".$row->id."','wct_omaster".$row->id."','wct".$css."-td".$color."')\""; }
					else { $out .= "onmouseover=\"this.setAttribute('class', 'wct".$css."-td-hover')\" onmouseout=\"this.setAttribute('class', 'wct".$css."-td".$color."')\""; }

					if ($row->link != '') {
						$out .= " onclick=\"location.href='".$row->link."'\"";
					}
					elseif (strpos($table->t_setup,"[LINK]") == FALSE AND $table->e_setup != '') {
						$out .= " onclick=\"location.href='".$url."wcteid=".$row->id."'\"";
					}

					$out .= ">";
					if (has_filter('the_editor', 'qtrans_modifyRichEditor')) {

						/*qtranslate support*/
						if (!preg_match('/.*!--:'.qtrans_getLanguage().'-->(.*?)<!--:--.*/',$inhalt))
						{
							/* No multilanguage content found */
							$tmp = rbr(stripslashes(preg_replace("/\{(.*?)\}/e","stripslashes(\$row->$1)",$inhalt)));
						}
						else {
							/* Language found */
							preg_match('/.*!--:'.qtrans_getLanguage().'-->(.*?)<!--:--.*/',$inhalt, $treffer);
							$tmp = rbr(stripslashes(preg_replace("/\{(.*?)\}/e","stripslashes(\$row->$1)",$treffer[1])));
						}
					}
					else {
						$tmp = rbr(preg_replace("/\{(.*?)\}/e","stripslashes(\$row->$1)",$inhalt));
					}

					if ($table->overlay == '1' AND strpos($inhalt,"[wctoverlay]") !== FALSE) { $tmp = str_replace("[wctoverlay]","[wctoverlay id=\"".$row->id."\" color=\"".$color."\"]",$tmp); }
					elseif (strpos($inhalt,"[wctoverlay]") !== FALSE) { $tmp = str_replace(array("[wctoverlay]","[/wctoverlay]"),array("",""),$tmp); }

					$out .= $tmp;	
					if (is_user_logged_in()) {
						global $current_user,$arr_user;
						if (!isset($arr_user)) {
							$arr_user = array('subscriber'=>0, 'contributor'=>1, 'author'=>2, 'editor'=>5, 'administrator'=>8);
							get_currentuserinfo();
							$user = $current_user;
							if ($user->blog_capabilities['administrator'] == '1' AND $user->user_level == '0') { $user->user_level = '10'; }
						}
						if ($arr_user[$this->settings['crole'.$id.'_c']] <= $user->user_level) {
							$out .= "<td><a style=\"text-decoration:none;\" href=\"".admin_url()."admin.php?page=wct_table_".$id."&wcttab=content&action=edit&rid=".$row->id."\">[". __('Edit','wct')."]</a>";
						}
					}

					$out .= "</tr>";
					if ($table->overlay == '1') {

						if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== FALSE) { $pos = "position: absolute;"; }
						else { $pos = "position: fixed;"; }

						if (!preg_match('/!--:'.qtrans_getLanguage().'-->(.*?)<!--:--/',rbr($this->filter_tables(apply_filters('wct_overlay',$table->o_setup))))) {
							$overlay .= "<div style=\"z-index:9999;".$pos."\" id=\"wct_overlay".$row->id."\" class=\"wct".$css."-overlay\">".do_shortcode(rbr(rbr(stripslashes(preg_replace("/\{(.*?)\}/e","stripslashes(\$row->$1)",stripslashes(apply_filters('wct_overlay',$table->o_setup))))," ",">","<")))."</div>";
						}
						else {
							preg_match('/.*!--:'.qtrans_getLanguage().'-->(.*?)<!--:--.*/',rbr(apply_filters('wct_overlay',$table->o_setup)), $treffer);
							$overlay .= "<div style=\"z-index:9999;".$pos."\" id=\"wct_overlay".$row->id."\" class=\"wct".$css."-overlay\">".do_shortcode(rbr(rbr(stripslashes(preg_replace("/\{(.*?)\}/e","stripslashes(\$row->$1)",$treffer[1]))," ",">","<")))."</div>";
						}
					}
				}
			}
			else {
				$out .= "<tr><td colspan=\"500\">".__('No entries found', 'wct')."</td></tr>";
			}

			$qry = $wpdb->get_row("SELECT count(id) as `anz` FROM `".$wpdb->prefix."wct".$id."` WHERE `status`='active'".$zusatz);
			$menge = ceil($qry->anz / $limit);

			if ($menge > '1') {
				$url = $this->generate_pagelink("/[&?]+wctstart=[0-9]*/","");
				$out .= "<tr><td colspan=\"500\" class=\"wct".$css."-errorfield\"><center><b>".__('Page', 'wct').":</b>&nbsp;";
				for ($x=1;$x <= $menge;$x++) {
					$l = ($limit * ($x - 1)) + 1;
					if($_GET['wctstart'] == $l OR ($l == '1' AND $_GET['wctstart'] == '')) { $out .= $x."&nbsp;"; }
					else {	$out .= "<a href=\"".$url."wctstart=".$l."\">".$x."</a>&nbsp;"; }

					if ($menge > $pages) {
						if ($x == floor ($pages / 2)) {
							$x = $menge - floor($pages / 2);
							$out .= "...&nbsp;";
						}
					}

				}

				$out .= "</center></td></tr>";
			}

			$out .= "</table>\n\n<!-- Custom Tables Plugin 05a1a29bdcae7b12229e651a9fd48b11 -->\n\n";
			if ($table->overlay == '1') {
				echo $overlay;
			}

			if ($table->nachtext != '') { $out .= "<br/>".rbr(stripslashes($table->nachtext)); }
		}
		else {
			$out = "<p>".__('Table Setup', 'wct')." ".__('not configured', 'wct')."</p>";
		}
		$out = do_shortcode($out);
		if (!is_user_logged_in()) { wp_cache_set( 'wct_table'.$id.$cache.$_GET['wctdrop'].$_REQUEST['wctstart'].'s'.$_REQUEST['wctsort'].$_REQUEST['wctinvs'], $out, 'wct', $this->settings['wct_cachetime']); }
	}
}
else {
	$entry = (integer)$_GET['wcteid'];
	$out = wp_cache_get( 'wct_table'.$id.'e'.$entry , 'wct');
	if ($out == false) {
		$url = $this->generate_pagelink("/[&?]+wcteid=[0-9]*/","");

		if ($design != '' AND $design != '') {
			$designnotfound = '0'; 
			$table = $wpdb->get_row("SELECT `e_setup` FROM `".$wpdb->prefix."wct_setup` WHERE `table_id`='".$id."' AND `id`='".mres($design)."' LIMIT 1;");
			if (count($table) != '1') { $designnotfound = '1';  }
		}
		if ($designnotfound == '1' OR !isset($designnotfound)) {
			$table = $wpdb->get_row("SELECT `e_setup` FROM `".$wpdb->prefix."wct_list` WHERE `id`='".$id."' LIMIT 1;");
		}

		if ($table->e_setup != '') {
			$out = "<p>";
			$qry = $wpdb->get_row("SELECT * FROM `".$wpdb->prefix."wct".$id."` WHERE `id`='".mres($entry)."' AND `status`='active' LIMIT 1;");
			if ($qry->id != '') {
				// Remove breaklines between table
				$inhalt = rbr($this->filter_tables(apply_filters('wct_entry',$table->e_setup)));

				if (!preg_match('/!--:'.qtrans_getLanguage().'-->(.*?)<!--:--/',$inhalt)) {
					$inhalt = rbr(rbr(stripslashes(preg_replace("/\{(.*?)\}/e","\$qry->$1",$inhalt))," ",">","<"));
				}
				else {
					preg_match('/!--:'.qtrans_getLanguage().'-->(.*?)<!--:--/',$inhalt, $treffer);
					$inhalt = rbr(rbr(stripslashes(preg_replace("/\{(.*?)\}/e","\$qry->$1",$treffer[1]))," ",">","<"));
				}
				$out .= str_replace("[BACK]","<a href=\"".$url."\">".__('back', 'wct')."</a>",$inhalt);

				if (is_user_logged_in()) {
					global $current_user,$arr_user;
					if (!isset($arr_user)) {
						$arr_user = array('subscriber'=>0, 'contributor'=>1, 'author'=>2, 'editor'=>5, 'administrator'=>8);
						get_currentuserinfo();
						$user = $current_user;
						if ($user->blog_capabilities['administrator'] == '1' AND $user->user_level == '0') { $user->user_level = '10'; }
					}
					if ($arr_user[$this->settings['crole'.$id.'_c']] <= $user->user_level) {
						$out .= "<br/><br/><a style=\"text-decoration:none;\" href=\"".admin_url()."admin.php?page=wct_table_".$id."&wcttab=content&action=edit&rid=".$entry."\">". __('Edit','wct')."</a>";
					}
				}
			}
			else {
				$out .= "<p>".__('Entry not found', 'wct')."</p>";
			}
			$out .= "</p>";
		}
		else {
			$out = "<p>".__('Entry Setup', 'wct')." ".__('not configured', 'wct')."</p>";
		}
		$out = do_shortcode($out);
		wp_cache_set( 'wct_table'.$id.'e'.$entry, $out, 'wct', $this->settings['wct_cachetime']);
	}
}

if ($_GET['wct_chkliz'] == '1') {
	$t = "pre";$p = "ch";$t = $t."m_".$p."k";$t = $this->$t('',1);

	$data = $this->http_request('POST','http://api.wuk.ch/wct-premium.php',"serial=".trim($t[1]."-".$t[2]."-".$t[3])."&dom=".$_SERVER['SERVER_NAME']);
	
	if (strpos($data,"|") !== false) {
		echo "Illegal Key found: '".$t[1]."-".$t[2]."-".$t[3]."-".$t[4]."'";
		$this->settings['form_serial'] = '';
		$this->settings['form_serialvu'] = '0';
		update_option('wuk_custom_tables', $this->settings);
	}
	else {
		echo "Serial Valid, see SALT: '".md5("wct-".$t[2].$t[3])."'";
	}
}

?>