<?php

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

if ($_GET['wct_eid'] == '')
{
	$url = $this->generate_pagelink("/[&?]+wct_eid=[0-9]*/","");
	$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 `e_setup`,`t_setup`,`overlay`,`headerline`,`header`,`headersort`,`vortext`,`nachtext`,`o_setup`,`sheme`,`sort`,`sortB` FROM `".$wpdb->prefix."wct_list` WHERE `id`='".$id."' LIMIT 1;");
		if ($table->t_setup != '') {
			$out = "<style>".stripslashes($this->settings['css'])."</style>";
			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.') == 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;
						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-table\">";

			if ($table->headerline == '1') {
				$out .= "<tr class=\"wct-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 ($_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 (isset($_GET['wctdrop']) AND $_GET['wctdrop'] != 'NULL' AND $_GET['wctdrof'] != '') {
				if ($zusatz != '') { $zusatz .= " AND "; }
				else { $zusatz = "WHERE "; }
				$zusatz .= "`".mres($_GET['wctdrof'])."`='".mres($_GET['wctdrop'])."'";
			}
			if ($filter != '' AND strpos($filter,"=") !== FALSE) {
				if ($zusatz != '') { $zusatz .= " AND "; }
				else { $zusatz = "WHERE "; }
				$zusatz .= $filter;
			}


			/* How it should be sorted */
			if ($_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.",".$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)
				{
					// Remove breaklines between table
					$inhalt = preg_replace(array('/<\/td>[\r\n]*<td>/','/<\/td>[\r]*<td>/','/<\/td>[\n]*<td>/'), array('</td><td>','</td><td>','</td><td>'), apply_filters('wct_table',$table->t_setup));

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

					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 .= rbr(stripslashes(preg_replace("/\[(.*?)\]/e","\$row->$1",$inhalt)));
						}
						else {
							/* Language found */
							preg_match('/.*!--:'.qtrans_getLanguage().'-->(.*?)<!--:--.*/',$inhalt, $treffer);
							$out .= rbr(stripslashes(preg_replace("/\[(.*?)\]/e","\$row->$1",$treffer[1])));
						}
					}
					else {
						$out .= rbr(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().'-->(.*?)<!--:--/',rbr(apply_filters('wct_overlay',$table->o_setup)))) {
							$overlay .= "<div style=\"".$pos."\" id=\"wct_overlay".$row->id."\" class=\"wct-overlay\">".rbr(stripslashes(preg_replace("/\[(.*?)\]/e","\$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=\"".$pos."\" id=\"wct_overlay".$row->id."\" class=\"wct-overlay\">".rbr(stripslashes(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 = $this->generate_pagelink("/[&?]+wctstart=[0-9]*/","");

				$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<!-- 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);
		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>",rbr(apply_filters('wct_entry',$table->e_setup)));
				if (!preg_match('/!--:'.qtrans_getLanguage().'-->(.*?)<!--:--/',$inhalt)) {
					$out .= stripslashes(preg_replace("/\[(.*?)\]/e","\$qry->$1",$inhalt));
				}
				else {
					preg_match('/!--:'.qtrans_getLanguage().'-->(.*?)<!--:--/',$inhalt, $treffer);
					$out .= stripslashes(preg_replace("/\[(.*?)\]/e","\$qry->$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']);
	}
}

?>