<?php
/*
   Plugin Name: Custom Tables Search
   Plugin URI: http://blog.murawski.ch/2011/08/custom-tables-wordpress-plugin/
   Description: [PREMIUM FEATURE] Extend the normal WordPress Search with the search in the Custom Tables
   Version: 3.0.2
   Author: Web Updates KMU
   Author URI: http://wuk.ch/

   Copyright (c) 2011 web updates kmu <stefan@wuk.ch>
   All rights reserved.
*/

function wct_search_addon($args) {
	global $wpdb;
	// Do not find multiple times the same results
	foreach ($args as $var) { $donts .= "'".$var->ID."',"; }
	if ($donts != '') { $donts = "blog_posts.ID NOT IN (".substr($donts,0,strlen($donts)-1).") AND "; }

	// Search all posts and pages with wctable in it
	$qry = $wpdb->get_results("SELECT blog_posts.* FROM blog_posts WHERE ".$donts." blog_posts.post_content LIKE '%[wctable%' AND blog_posts.post_type IN ('post', 'page') AND blog_posts.post_status = 'publish' ORDER BY blog_posts.post_date DESC LIMIT 0, 10;");
	if (count($qry) >= '1') {
		foreach ($qry as $row) {
			preg_match_all("/\[wctable.?+id=[\"]*([0-9]+)[\"]*/",$row->post_content,$treffer);
			foreach ($treffer[1] as $wert) {
				$t = "table".$wert;
				if ($$t == '') {
					$tabl2 = $wpdb->get_row("SHOW CREATE TABLE `".$wpdb->prefix."wct".$wert."`;");
					$array=array(); foreach($tabl2 as $member=>$data) { $array[$member]=$data; }

					$settings = get_option('wuk_custom_tables');
					if ($settings['search_charset'] == "auto" OR $settings['search_charset'] == "") {
						preg_match("/CHARSET=(.*)/",$array['Create Table'],$treffer);

						if ($treffer[1] != '') { 
							$pos = strpos($treffer[1]," ");
							if ($pos === false) { $charset = $treffer[1]; }
							else { $charset = substr($treffer[1],0,$pos); }
						}
						else {
							$charset = "latin1";
						}
					}
					elseif ($settings['search_charset'] != '') {
						$charset = $settings['search_charset'];
					}

					$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) {
						 	$feld5[$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]);
						}
						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]);
						}
					}

					unset($searchfields,$searchfields2);
					$query = $_GET['s'];
					$query2 = $query = mb_convert_encoding($query ,mb_detect_encoding($query),$charset);

					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); }

					if (is_array($feld2)) {
						foreach ($feld2 as $werd) {
							if ($werd['i'] != 'id') { $searchfields .= "OR LOWER(`".$werd['i']."`) LIKE '%".mres(strtolower($query))."%' "; }
						}
					}
					if (is_array($feld3)) {
						foreach ($feld3 as $werd) {
							if ($werd['i'] != 'status') { $searchfields .= "OR LOWER(`".$werd['i']."`)='".mres(strtolower($query))."' "; }
						}
					}
					if (is_array($feld5)) {
						foreach ($feld5 as $werd) {
							if ($werd['i'] != 'status') { $searchfields .= "OR FIND_IN_SET('".mres(strtolower($query))."',LOWER(`".$werd['i']."`))>0 "; }
						}
					}
					if (isset($db_timestamp)) {
						if (is_array($feld4)) {
							foreach ($feld4 as $werd) {
								$searchfields .= "OR `".$werd['i']."`='".$db_timestamp."' ";
							}
						}
						$_GET['wctsz'] = $db_timestamp;

						$mast = $wpdb->get_row("SELECT `searchaddon` FROM `".$wpdb->prefix."wct_list` WHERE id='".$wert."' LIMIT 1;");
						if ($mast->searchaddon != '') {
							$searchfields .= "OR (".str_replace("SEARCH",$db_timestamp,$mast->searchaddon).") ";
						}
					}
					$searchfields = str_replace(array($wpdb->prefix."wct".$wert,",`id`"),array("",""),$searchfields);

					$abfrage = "SELECT `id` FROM `".$wpdb->prefix."wct".$wert."` WHERE (".substr($searchfields,3,strlen($searchfields)-3).") AND `status`='active' LIMIT 1;";
					$qry = $wpdb->get_results($abfrage);
					if (count($qry) == '1') {
						$$t = '1';
						$lastID = $row->ID;

						$_GET['wctsf'] = "*";
						$_GET['wcts'] = $_GET['s'];

						$n = count($args);
						$args[$n]->ID = $row->ID;
 						$args[$n]->post_author = $row->post_author;
 						$args[$n]->post_date = $row->post_date;
 						$args[$n]->post_date_gmt = $row->post_date_gmt;
 						$args[$n]->post_content = $row->post_content;
 						$args[$n]->post_title = $row->post_title;
 						$args[$n]->post_excerpt = $row->post_excerpt;
 						$args[$n]->post_status = $row->post_status;
 						$args[$n]->comment_status = $row->comment_status;
 						$args[$n]->ping_status = $row->ping_status;
 						$args[$n]->post_password = $row->post_password;
 						$args[$n]->post_name = $row->post_name;
 						$args[$n]->to_ping = $row->to_ping;
 						$args[$n]->pinged = $row->pinged;
 						$args[$n]->post_modified = $row->post_modified; 
 						$args[$n]->post_modified_gmt = $row->post_modified_gmt;
 						$args[$n]->post_content_filtered = $row->post_content_filtered;
 						$args[$n]->post_parent = $row->post_parent;
 						$args[$n]->guid = $row->guid;
 						$args[$n]->menu_order = $row->menu_order;
 						$args[$n]->post_type = $row->post_type;
 						$args[$n]->post_mime_type = $row->post_mime_type;
 						$args[$n]->comment_count = $row->comment_count;
					}
					else {
						$$t = '0';
					}
				}
				elseif ($$t == '1' AND $lastID != $row->ID) {
					$_GET['wctsf'] = "*";
					$_GET['wcts'] = $_GET['s'];

					$n = count($args);
					$args[$n]->ID = $row->ID;
 					$args[$n]->post_author = $row->post_author;
 					$args[$n]->post_date = $row->post_date;
 					$args[$n]->post_date_gmt = $row->post_date_gmt;
 					$args[$n]->post_content = $row->post_content;
 					$args[$n]->post_title = $row->post_title;
 					$args[$n]->post_excerpt = $row->post_excerpt;
 					$args[$n]->post_status = $row->post_status;
 					$args[$n]->comment_status = $row->comment_status;
 					$args[$n]->ping_status = $row->ping_status;
 					$args[$n]->post_password = $row->post_password;
 					$args[$n]->post_name = $row->post_name;
 					$args[$n]->to_ping = $row->to_ping;
 					$args[$n]->pinged = $row->pinged;
 					$args[$n]->post_modified = $row->post_modified; 
 					$args[$n]->post_modified_gmt = $row->post_modified_gmt;
 					$args[$n]->post_content_filtered = $row->post_content_filtered;
 					$args[$n]->post_parent = $row->post_parent;
 					$args[$n]->guid = $row->guid;
 					$args[$n]->menu_order = $row->menu_order;
 					$args[$n]->post_type = $row->post_type;
 					$args[$n]->post_mime_type = $row->post_mime_type;
 					$args[$n]->comment_count = $row->comment_count;
				}
			}
		}
	}
	return $args;
}

$current = get_settings('active_plugins');
$settings = get_option('wuk_custom_tables');
if (!in_array( 'custom-tables/custom-tables.php', $current) OR $settings['form_serial'] == '') {
	array_splice($current, array_search( 'custom-tables/custom-tables-search.php', $current), 1 );
	update_option('active_plugins', $current);
	header('Location: plugins.php?deactivate=true');
}

if ($_GET['s'] != '') {
	add_filter('posts_results','wct_search_addon');
	add_filter('the_excerpt', 'do_shortcode', 11);
	add_filter('the_content', 'do_shortcode', 12);
}

?>