<?php

$url = str_replace("/&","/?",WP_SITEURL.preg_replace("/[&?]+wct_eid=[0-9]*/","",$_SERVER[REQUEST_URI]));
if (strpos($url,"?") !== FALSE) { $url .= "&"; } else { $url .= "?"; }

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

if ($_GET['wct_eid'] == '')
{

	$out = wp_cache_get( 'wct_table'.$id.$_GET['wctdrop'].$_REQUEST['wctstart'].'s'.$_REQUEST['wctsort'].$_REQUEST['wctinvs'] , 'wct');
	if ($out == false) {
		$table = $wpdb->get_row("SELECT `t_setup`,`overlay`,`headerline`,`header`,`headersort`,`vortext`,`nachtext`,`o_setup`,`sheme` FROM `".$wpdb->prefix."wct_list` WHERE `id`='".$id."' LIMIT 1;");
		if ($table->t_setup != '') {
			if ($table->vortext != '') { $out = stripslashes($table->vortext)."<br/>&nbsp;"; }
			if ($table->overlay == '1') {
			$out .= "<script>
				function box_show(name,namB,color)
				{
					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; }
						else if (document.body.scrollTop) {var iL = document.body.scrollLeft;var iV = document.body.scrollTop; }
						else { iL = 0; iV = 0; }

						if ('IE6' == '";
						if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') == true) { $pos = "IE6"; }
						$out .= $pos."') {
							var ie6pos = findPos('wct-table');
							iV = iV - ie6pos[1] +140;
							iL = iL - ie6pos[0] +12;
						}
					}
					else {
						iL = 0; iV = 0;
					}
				  	document.getElementById(\"Mx\").value = tmpX + iL + 10;
				  	document.getElementById(\"My\").value = tmpY + iV;
				}
				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\">";
			}
			$out .="<table name=\"wct-table\" id=\"wct-table\" class=\"wct-table\">";

			if ($table->headerline == '1') {
				$out .= "<tr class=\"wct-headline\">";
				$y = '0';
				$sort = explode(",",$table->headersort);
				if ($table->header == '') {
					$inhalt = str_replace("[LINK]","",$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);
					/*qtranslate support */
					if (preg_match('/.*!--:'.qtrans_getLanguage().'-->(.*?)<!--:--.*/',$inhalt)) {
						/* Language found */
						preg_match('/.*!--:'.qtrans_getLanguage().'-->(.*?)<!--:--.*/',$inhalt, $treffer);
						$inhalt = $treffer[1];
					}
					$felder = explode(",",$inhalt);
					foreach ($felder as $feld) {
						$out .= "<td><b>".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 ($_GET['wcts'] != '') {
				$wanted = ",".str_replace("%2C",",",$_GET['wctsf']).",";
				$table2 = $wpdb->get_row("SHOW CREATE TABLE `".$wpdb->prefix."wct".$id."`;");
				/*Hack to transform PHP Object to Array, because Objectname has a Space in the middle*/
				$array=array(); foreach($table2 as $member=>$data) { $array[$member]=$data; }
				$felder = explode(",",str_replace("8,2","8.2",$array['Create Table']));
				for ($i=1;$felder[$i] != '';$i++) {
					$feld2 .= preg_replace("/`(.*)`.*/",",$1",$felder[$i-1]);
				}
				$feld2 = str_replace(array("\r","\n"," "),array("","",""),$feld2);
				$feld2 = explode(",",substr($feld2,1,strlen($feld2)));
				for ($i=1;$feld2[$i] != '';$i++) {
					if ($_GET['wctsf'] != '*') {
						if (strpos($wanted,",".$feld2[$i].",") !== FALSE) {
							$searchfields .= ",".$feld2[$i];
						}
					}
					else {
						$searchfields .= ",".$feld2[$i];
					}
				}
				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 ($_GET['wcts'] != '' AND $searchfields != '') {
				$sf = "`".str_replace(",","`,`",$searchfields)."`";
				$zusatz = "WHERE CONCAT( ".$sf.") LIKE '%".mres($_GET['wcts'])."%'";
			}

			/* Filter if needed */
			if ($_GET['wctdrop'] != '' AND $_GET['wctdrof'] != '') {
				if ($zusatz != '') { $zusatz .= " AND "; }
				else { $zusatz = "WHERE "; }
				$zusatz .= "`".mres($_GET['wctdrof'])."`='".mres($_GET['wctdrop'])."'";
			}


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

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

			/* Main query of the selected database */
			$qry = $wpdb->get_results("SELECT * FROM `".$wpdb->prefix."wct".$id."`".$zusatz." LIMIT ".$limit2.";");
			if (count($qry) >= '1') {
				foreach ($qry as $row)
				{
					$inhalt = str_replace("[LINK]",$url."wct_eid=".$row->id,stripslashes($table->t_setup));

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

					$out .= "<tr class=\"wct-td".$color."\" id=\"wct_omaster".$row->id."\" ";
					if ($table->overlay == '1') { $out .= "onmousemove=\"box_show('wct_overlay".$row->id."','wct_omaster".$row->id."','wct-td-hover')\" onmouseout=\"box_hide('wct_overlay".$row->id."','wct_omaster".$row->id."','wct-td".$color."')\""; }
					else { $out .= "onmouseover=\"class='wct-td-hover'\" onmouseout=\"class='wct-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."wct_eid=".$row->id."'\"";
					}

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

						/*qtranslate support*/
						if (!preg_match('/.*!--:'.qtrans_getLanguage().'-->(.*?)<!--:--.*/',$inhalt))
						{
							/* No multilanguage content found */
							$out .= preg_replace("/\[(.*?)\]/e","\$row->$1",$inhalt);
						}
						else {
							/* Language found */
							preg_match('/.*!--:'.qtrans_getLanguage().'-->(.*?)<!--:--.*/',$inhalt, $treffer);
							$out .= preg_replace("/\[(.*?)\]/e","\$row->$1",$treffer[1]);
						}
					}
					else {
						$out .= preg_replace("/\[(.*?)\]/e","\$row->$1",$inhalt);
					}
					$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().'-->(.*?)<!--:--.*/',stripslashes($table->o_setup))) {
							/* No multilanguage content found*/
							$overlay .= "<div style=\"".$pos."\" id=\"wct_overlay".$row->id."\" class=\"wct-overlay\">".preg_replace("/\[(.*?)\]/e","\$row->$1",stripslashes($table->o_setup))."</div>";
						}
						else {
							/* Language found */
							preg_match('/.*!--:'.qtrans_getLanguage().'-->(.*?)<!--:--.*/',stripslashes($table->o_setup), $treffer);
							$overlay .= "<div style=\"".$pos."\" id=\"wct_overlay".$row->id."\" class=\"wct-overlay\">".preg_replace("/\[(.*?)\]/e","\$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."`".$zusatz);
			$menge = ceil($qry->anz / $limit);
			if ($menge > '1') {
				$url = str_replace("/&","/?",WP_SITEURL.preg_replace("/[&?]+wctstart=[0-9]*/","",$_SERVER[REQUEST_URI]));
				if (strpos($url,"?") !== FALSE) { $url .= "&"; } else { $url .= "?"; }
				$out .= "<tr><td colspan=\"500\" class=\"wct-errorfield\"><center><b>".__('Page', 'wct').":</b> ";
				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;"; }
				}
				$out .= "</center></td></tr>";
			}

			$out .= "</table>\n\n".base64_decode('PCEtLSBDdXN0b20gVGFibGVzIFBsdWdpbiBmcm9tIGh0dHA6Ly93dWsuY2gvIC0tPg==')."\n\n";
			if ($table->overlay == '1') {
				echo $overlay;
			}

			if ($table->nachtext != '') { $out .= "<br/>".stripslashes($table->nachtext); }
		}
		else {
			$out = "<p>".__('Table Setup', 'wct')." ".__('not configured', 'wct')."</p>";
		}
		$out = do_shortcode($out);
		wp_cache_set( 'wct_table'.$id.$_GET['wctdrop'].$_REQUEST['wctstart'].'s'.$_REQUEST['wctsort'].$_REQUEST['wctinvs'], $out, 'wct', $this->settings['wct_cachetime']);
	}
}
else {
	$entry = (integer)$_GET['wct_eid'];
	$out = wp_cache_get( 'wct_table'.$id.'e'.$entry , 'wct');
	if ($out == false) {
		$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)."' LIMIT 1;");
			if ($qry->id != '') {
				$inhalt = str_replace("[BACK]","<a href=\"".$url."\">".__('back', 'wct')."</a>",stripslashes($table->e_setup));
				if (!preg_match('/.*!--:'.qtrans_getLanguage().'-->(.*?)<!--:--.*/',$inhalt))
				{
					$out .= preg_replace("/\[(.*?)\]/e","\$qry->$1",$inhalt);
				}
				else {
					preg_match('/.*!--:'.qtrans_getLanguage().'-->(.*?)<!--:--.*/',$inhalt, $treffer);
					$out .= preg_replace("/\[(.*?)\]/e","\$row->$1",$treffer[1]);
				}
			}
			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']);
	}
}

?>