<?php
/*
   Plugin Name: Custom Tables
   Plugin URI: http://blog.murawski.ch/2011/08/custom-tables-wordpress-plugin/
   Description: Create Tables and show on a page/article. Usable for all kind of diffrent databases.
   Version: 2.9.0
   Author: Web Updates KMU
   Author URI: http://wuk.ch/

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

   This program is distributed under the GNU General Public License, Version 2,
   June 1991. Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin
   St, Fifth Floor, Boston, MA 02110, USA

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

define('WCT_VERSION', '2.9.0');  
define('WCT_DBVERSION', '2011122901');    

if (!function_exists('mres')) { function mres($t) { $t = mysql_real_escape_string($t); return $t; } }
if (!function_exists('rbr')) { function rbr($t,$replace = "<br/>",$s='',$e='') { $t = html_entity_decode(str_replace(array($s."\r\n".$e,$s."\n".$e,$s."\r".$e),array($s.$replace.$e,$s.$replace.$e,$s.$replace.$e),$t)); return $t; } }

if (!defined('DIRECTORY_SEPARATOR')) {
	if (strpos(php_uname('s'), 'Win') !== false ) { define('DIRECTORY_SEPARATOR', '\\'); }
	else { define('DIRECTORY_SEPARATOR', '/'); }
}

if (!class_exists('wuk_custom_tables')) {
	class wuk_custom_tables {
		var $settings;
		var $wctpath;
		var $lastfilter;
		var $saltdone;

		function wuk_custom_tables() {
			global $wpdb;
			$this->getOptions();
			$wctpath = explode("custom-tables",plugin_dir_path( __FILE__ ));
			$this->wctpath = $wctpath[0]."custom-tables".DIRECTORY_SEPARATOR;
			
			/* Language File will be loaded */
			if (function_exists('load_plugin_textdomain')) load_plugin_textdomain('wct', PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)).'/languages', dirname(plugin_basename( __FILE__ )).'/languages');

			add_shortcode('wctable', array(&$this,'show_table'));

			add_shortcode('wctdate', array(&$this,'show_date'));
			add_shortcode('wctarchive', array(&$this,'show_archive'));
			add_shortcode('wctselect', array(&$this,'show_select'));

			add_shortcode('wctsearch', array(&$this,'show_search'));
			add_shortcode('wcteditpage', array(&$this,'show_editpage'));
			add_shortcode('wctclear', array(&$this,'show_clear'));
			add_shortcode('if', array(&$this,'smarttag_if'));
			add_shortcode('wctoverlay', array(&$this,'smarttag_overlay'));
			add_shortcode('wctform', array(&$this,'smarttag_form'));
			add_shortcode('wctphp', array(&$this,'smarttag_php'));
			if(is_admin()) {
				add_action('admin_menu', array(&$this, 'add_menupages'));
				if ($this->settings['dbversion'] == "2011072801") { $this->dbupdate('2011081501',1); }
				if ($this->settings['dbversion'] == "2011081501") { $this->dbupdate('2011090101',1); }
				if ($this->settings['dbversion'] == "2011090101") { $this->dbupdate('2011101801',1); }
				if ($this->settings['dbversion'] == "2011101801") { $this->dbupdate('2011103101',1); }
				if ($this->settings['dbversion'] == "2011103101") { $this->dbupdate('2011111101',1); }
				if ($this->settings['dbversion'] == "2011111101") { $this->dbupdate('2011113001',1); }
				if ($this->settings['dbversion'] == "2011113001") { $this->dbupdate('2011121101',1); }
				if ($this->settings['dbversion'] == "2011121101") { $this->dbupdate('2011122901',1); }

				add_action('in_plugin_update_message-custom-tables/custom-tables.php', array(&$this, 'plugin_updates'));

				$crons = $wpdb->get_row("SELECT count(id) as `menge` FROM `".$wpdb->prefix."wct_cron` WHERE `active`='1'");
				if ($crons->menge >= '1' AND !wp_next_scheduled('wct_task_hook')) {
					wp_schedule_event( time(), 'hourly', 'wct_task_hook');
				}
				elseif ($crons->menge < '1' AND wp_next_scheduled('wct_task_hook')) {
					wp_clear_scheduled_hook('wct_task_hook');
				}
			}
			if ($_GET['wcttab'] == "setup" OR $_GET['wcttab'] == "eviw") {
				add_filter( 'user_can_richedit', array(&$this, 'disable_wysiwyg') );
				add_filter( 'wp_default_editor', create_function('', 'return "html";') );
			}
			elseif ($_GET['wcttab'] == "content") {
				if ($this->settings['wct_unfilteredhtml'] == "1") {
					add_filter('tiny_mce_before_init', array( &$this, 'tinymce_filter_remove'), 999);
					add_filter('htmledit_pre', array( &$this, 'tinymce_htmledit'), 999);
					add_action('admin_print_footer_scripts', array( &$this, 'tinymce_replace'), 60);
				}
				// Tiny MCE Editor
				add_filter('wp_default_editor', create_function('', 'return "tinymce";') );
				add_filter('mce_buttons', create_function('', 'return array("pasteword","|","bold","italic","|","bullist","numlist","|","link","unlink","|","spellchecker");'),9999 );
				add_filter('mce_buttons_2', create_function('', 'return array();'),9999 );
				
				// Picture Upload
				add_action('admin_print_scripts', array( &$this, 'upload_scripts'));
				add_action('admin_print_styles', array( &$this, 'upload_styles'));
			}

			add_filter('wct_table', array( &$this, 'hook_table'));
			add_filter('wct_entry', array( &$this, 'hook_entry'));
			add_filter('wct_overlay', array( &$this, 'hook_overlay'));
			add_action('wct_task_hook', array( &$this, 'task_function'));

			//Fix for Front-end Editor
			if (strpos($_GET['page'],"wct_table") !== false ) { remove_action( '_admin_menu', array( 'scbAdminPage', '_pages_init')); }

		}

		function task_function() {
			global $wpdb;
			$qry = $wpdb->get_results("SELECT `id`,`command`,`schedule` FROM `".$wpdb->prefix."wct_cron` WHERE `nextrun`<='".time()."' AND `active`='1'");
			if (count($qry) >= '1') {
				foreach ($qry as $row) {
					$cronjobs = str_replace(array("\r","\n"),"",$this->sqldatefilter(stripslashes($row->command)));
					$wpdb->show_errors();
					ob_start();
					if (strpos($cronjobs,";") !== false) {
						$einzeln = explode(";",$cronjobs);
						for($x=0;$einzeln[$x]!='';$x++) {
							$wpdb->get_row($einzeln[$x].";");
						}
					}
					else {
						$wpdb->get_row(stripslashes($cronjobs.";"));
					}
					$fehler = ob_get_clean();
					$wpdb->hide_errors();

					$status = '1';
					if ($row->schedule == 'd') { $nexttime = time() + 86320; }
					elseif ($row->schedule == 't') { $nexttime = time() + 43140; }
					elseif ($row->schedule == 'h') { $nexttime = time() + 3540; }
					else { $status = '0'; }

					if ($fehler != '') {
						$status = '0';
						wp_mail(get_settings('admin_email'), __('WCT Cronjob deactivated','wct'), __('Cronjob was deactivated because of an error.','wct')."\r\n\r\nCommand: ".stripslashes($cronjobs)."\r\n\r\nErrors: ".$fehler);
					}

					$wpdb->get_row("UPDATE `".$wpdb->prefix."wct_cron` SET `nextrun`='".$nexttime."', `error`='".mres($fehler)."', `active`='".$status."' WHERE `id`='".mres($row->id)."' LIMIT 1");
				}
			}
		}

		function upload_scripts() {
			wp_enqueue_script('media-upload');
			wp_enqueue_script('thickbox');
			wp_enqueue_script('jquery');
		}

		function filter_tables($content) {
			$content = preg_replace(
				array(	'/>[\r\n]*<td/','/>[\r]*<td/','/>[\n]*<td/',
					'/>[\r\n]*<tr/','/>[\r]*<tr/','/>[\n]*<tr/',
					'/>[\r\n]*<\/table>/','/>[\r]*<\/table>/','/>[\n]*<\/table>/'
				),
				array(	'><td','><td','><td',
					'><tr','><tr','><tr',
					'></table>','></table>','></table>'
				),
				$content);
			return $content;
		}

		function upload_styles() {
			wp_enqueue_style('thickbox');
		}

		function tinymce_filter_remove($init) {
			$init['apply_source_formatting'] = 'true';
			return $init;
		}

		function tinymce_htmledit($init) {
			$init = str_replace( array('&amp;', '&lt;', '&gt;'), array('&', '<', '>'), $init );
			$init = wpautop($init);
			$init = htmlspecialchars($init, ENT_NOQUOTES);
			return $init;
		}

		function tinymce_replace() {
			global $merged_filters;
			echo "<script type=\"text/javascript\">
				//<![CDATA[
				jQuery('body').bind('afterPreWpautop', function(e, o){
					o.data = o.unfiltered
					.replace(/caption\\]\\[caption/g, 'caption] [caption')
					.replace(/<object[\s\S]+?<\\/object>/g, function(a) {
						return a.replace(/[\\r\\n]+/g, ' ');
					});

				}).bind('afterWpautop', function(e, o){
					o.data = o.unfiltered;
				});
				//]]>
			</script>";
		}

		function picture_upload($feld) {
			echo "<script>
				jQuery(document).ready(function() {";
				foreach ($feld as $var) {
					echo "jQuery('#upload_image_button".$var."').click(function() {
						formfield = jQuery('#upload_image".$var."').attr('id');
						tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
						return false;
					});";
				}
				echo "window.send_to_editor = function(html) {
						imgurl = jQuery('img',html).attr('src');
						jQuery('#' + formfield).val(imgurl);
						tb_remove();
					}
				});
			</script>";
		}

		function dbupdate($date,$write = '0') {
			global $wpdb;
			if (file_exists($this->wctpath."updates/".$date.".php")) {
				include($this->wctpath."updates/".$date.".php");
				if ($write == '1') {
					$this->settings['dbversion'] = $date;
					update_option('wuk_custom_tables', $this->settings);
				}
			}
			else {
				printf( __("Needed Custom Tables DB Update '%s' DB Update not found!",'wct'), $date);
			}

		}

		function show_table($atts) {
			global $wpdb;
			/* Table auf der Seite anzeigen per Shortcode einfügen */
			extract(shortcode_atts(array('id' => '1', 'limit' => '50', 'filter' => '', 'nofilter' => '', 'design'=>'', 'pages' => '12', 'css'=> ''), $atts));

			if ($filter != '' AND strpos($filter,"=") !== FALSE) {
				$this->lastfilter = " AND ".$this->sqldatefilter($filter);
			}
			else { $this->lastfilter = ''; }

			include($this->wctpath."pages/show_table.php");
			return $out;
		}

		function show_date($atts) {
			global $wpdb;		
			include($this->wctpath."pages/show_date.php");
			return $out;
		}

		function smarttag_if($atts, $content = '') {
			global $wpdb;
			extract(shortcode_atts(array('field' => '', 'check' => '==', 'var' => '', 'else'=> ''), $atts));
			if ($field == '' AND $atts[0] != '') {
				// Bugfix for People with HTML Code within the field
				$field = trim($atts[0]." ".$atts[1]." ".$atts[2]." ".$atts[3]." ".$atts[4]." ".$atts[5]." ".$atts[6]." ".$atts[7]);
			}
			include($this->wctpath."pages/tag_if.php");
			return do_shortcode($out);
		}

		function smarttag_php($atts, $content = '') {
			global $wpdb;
			if ($content != '') {
				define('WCT_PHP_ON', true);
				include($this->wctpath."pages/tag_php.php");
			}
			return $out;
		}

		function wct_sort_object($a, $b) {
			global $k, $l, $j;
			if ($a->$j == $b->$j ){ return 0; } 
			return ($a->$j < $b->$j) ? $k : $l;
		} 

		function smarttag_form($atts) {
			global $wpdb;
			extract(shortcode_atts(array('id' => '', 'limit' => '50'), $atts));

			if ($this->prem_chk() !== true) { $out = __('Premium functionality has not been activated in the plugin. Please check for a valid licence!', 'wct'); }
			elseif ($id != '') { include($this->wctpath."pages/show_form.php"); }
			return $out;
		}

		function smarttag_overlay($atts, $content = '') {
			extract(shortcode_atts(array('id' => '', 'color' => '1'), $atts));
			if ($id != '') {
				$out = "<div onmousemove=\"box_show('wct_overlay".$id."','wct_omaster".$id."','wct-td-hover')\" onmouseout=\"box_hide('wct_overlay".$id."','wct_omaster".$id."','wct-td".$color."')\">".$content."</div>";
			}
			return $out;
		}

		function show_clear($atts) {
			$url = $this->generate_pagelink("/[&?]+wct(.*)=.*&/","&");
			$out = "<input type=\"submit\" class=\"wct-button\" onclick=\"top.location.href='".$url."'; return false;\" value=\"".__('Clear','wct')."\">";
			return $out;
		}

		function filtermd($filter) {
			if ($filter != '') { $filtermd = eregi_replace("[^0-9a-zA-Z]","",md5($filter)); }
			else { $filtermd = ''; }
			return $filtermd;
		}

		function sqldatefilter($filter) {
			$filter = html_entity_decode(str_replace(array("&lt;","&gt;","time()-1d","time()+1d","time()"),array("<",">",(time()-86400),(time()+86400),time()),$filter));
			return $filter;
		}
		function show_select($atts) {
			global $wpdb;
			extract(shortcode_atts(array('id' => '1', 'field' => '', 'limit' => '20', 'maintext' => __('All entries','wct'), 'filter'=>'', 'sort'=>'anz DESC', salt=>''), $atts));
			if ($filter == '') { $filter = $this->lastfilter; }
			else { $filter = " AND ".$this->sqldatefilter($filter); }
			include($this->wctpath."pages/show_select.php");
			if (!is_array($this->saltdone)) { $this->saltdone = array(); }
			if ($out != '' && !in_array($salt, $this->saltdone)) {
				include(plugin_dir_path( __FILE__ )."pages/show_select_js.php");
			}
			return $out;
		}

		function wct_quicktag_button() {
			global $wpdb;
			$tableid = str_replace("wct_table_","",$_GET['page']);
			include($this->wctpath."pages/editor_js.php");
		}

		function show_archive($atts) {
			global $wpdb;	
			include($this->wctpath."pages/show_archive.php");
			return $out;
		}

		function show_search($atts) {
			global $wpdb;	
			extract(shortcode_atts(array('felder' => '*', 'text' => 'sss'), $atts));		
			include($this->wctpath."pages/show_search.php");
			return $out;
		}

		function show_editpage($atts) {
			global $wpdb;	
			extract(shortcode_atts(array('table' => '*', 'filter' => '', 'html' => '0'), $atts));

			$url = $this->generate_pagelink(array("/[&?]+wct_eid=[0-9]*/","/[&?]+tableselector=[0-9]*/"),"");
			echo "<form action=\"".$url."\" method=\"GET\"><select name=\"tableselector\">";
			$qry = $wpdb->get_results("SELECT `id`,`name` FROM `".$wpdb->prefix."wct_list` WHERE `id`!='0';");
			if (count($qry) >= '1') {
				foreach ($qry as $row) {
					echo "<option value=\"".$row->id."\">".$row->name."</option>";
				}
			}	
			echo "</select><input type=\"submit\" value=\"". __ ('Change Table', 'wct') ."\"></form><br/>";

			if ($table != '*' OR ($table == '*' AND $_GET['tableselector'] != '')) {
				if ($table == '*') { $tableid = (int)mres($_GET['tableselector']); }
				else { $tableid = $table; }

				$wctmp = $url."tableselector=";

				if ($_POST['submit'] != '') {
					include($this->wctpath."pages/save_tables.php");
				}
				include($this->wctpath."pages/editcontent.php");
			}
		}

		function tab_link($wcttab,$name,$shorttag,$wcttab2 = '',$text = '') {
			global $_GET;
			if ($wcttab2 != '') { $a = "&wcttab2=".$wcttab2; }
			$out = "<th onclick=\"document.location='admin.php?page=".$_GET['page']."&wcttab=".$shorttag.$a."'\" style=\"cursor:pointer;min-width:148px;width:148px;font-family: 'Lucida Grande',Verdana,Arial,'Bitstream Vera Sans',sans-serif;color: ";
			if ($wcttab == $shorttag && $wcttab2 == '') { $out .= "black"; }
			elseif ($wcttab2 == $_GET['wcttab2'] AND $_GET['wcttab2'] != '') { $out .= "black"; } 
			else { $out .= "#21759b"; }

			$out .= ";\">".__($name, 'wct').$text."</th>";
			return $out;
		}

		function taggen() {
			global $wpdb;
			include($this->wctpath."pages/wuk.php");
			include($this->wctpath."pages/tag_gen.php");
		}

		function wctmenu() {
			include($this->wctpath."pages/wuk.php");
			echo "<div><h2>".__('Custom Tables', 'wct')."</h2>".
			     __('On the Navigation panel at the left, you can create new Tables or edit the existing ones','wct').":<br/>".
			     "<img src=\"".plugins_url('custom-tables/img/tutorial/createeditdb.jpg')."\" border=\"0\" alt=\"Tutorial Picture\"><br/><br/>".
			     __('If you have questions or problems, please do not hesitate to contact us','wct').": <a href=\"http://wuk.ch/?piwik_campaign=plugin&piwik_kwd=custom-tables\">web updates kmu</a></div>";
		}

		function explodei($ASeperator, $ASource) {
			$ASeperator = strtolower($ASeperator);
			$x_insensitive = strtolower($ASource);
			$x_data = explode($ASeperator, $x_insensitive);
			$x_position = 0;
			$x_sep_len  = strlen($ASeperator);
  
			foreach ($x_data as $myElement) {
				$x_element_length = strlen($myElement);
				$retval[] = substr($ASource, $x_position, $x_element_length);
				$x_position += $x_element_length + $x_sep_len;
			}
			return $retval;
		}

		function create_table() {
			global $wpdb, $current_user;

			if ($this->info != '') {
				echo $this->info;
			}

			$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'; }
			$wcttab = $_GET['wcttab'];
			
			include($this->wctpath."pages/wuk.php");
			if ($_POST['submit'] != '') {
				include($this->wctpath."pages/save_tables.php");
			}

			if ($_GET['page'] != 'wct_table_create') {
				$tableid = str_replace("wct_table_","",$_GET['page']);
				echo "<div style=\"padding-top:15px;\"><table vspace=\"0\" hspace=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"background-image:url(" . plugins_url('custom-tables/img/tab.png') . ");height:30px;\" ><tr>";
				if ($tableid != '0') {
					if ($arr_user[$this->settings['crole'.$tableid.'_s']] <= $user->user_level) { echo $this->tab_link($wcttab,'Table Setup',''); }
					if ($arr_user[$this->settings['crole'.$tableid.'_c']] <= $user->user_level) { echo $this->tab_link($wcttab,'Edit Content','content'); }
					if ($arr_user[$this->settings['crole'.$tableid.'_i']] <= $user->user_level) { echo $this->tab_link($wcttab,'Import / Export','importexport'); }
					if ($arr_user[$this->settings['crole'.$tableid.'_s']] <= $user->user_level) { echo $this->tab_link($wcttab,'View Setup','setup'); }
				}
				else {
					if ($arr_user[$this->settings['role_archive']] <= $user->user_level) {
						echo $this->tab_link($wcttab,'Table Setup','show');
						echo $this->tab_link($wcttab,'View Setup','setup');
					}
				}

				echo "</tr></table></div>";
			}

			$wctmp = "admin.php?page=wct_table_";
			switch ($wcttab) {

				default:
					if ($tableid == '0') {
						if ($arr_user[$this->settings['role_archive']] <= $user->user_level) { include($this->wctpath."pages/setup_archive.php"); }
						else { include($this->wctpath."pages/405.php"); }
					}
					else {
						if ($arr_user[$this->settings['crole'.$tableid.'_s']] <= $user->user_level) { include($this->wctpath."pages/setup_table.php"); }
						elseif ($arr_user[$this->settings['crole'.$tableid.'_c']] <= $user->user_level) { include($this->wctpath."pages/editcontent.php"); }
						elseif ($arr_user[$this->settings['crole'.$tableid.'_i']] <= $user->user_level) { include($this->wctpath."pages/importexport.php"); }
						else { include($this->wctpath."pages/405.php"); }
					}
				break;	

				case 'content':
					include($this->wctpath."pages/editor_fixes.php");
					if ($arr_user[$this->settings['crole'.$tableid.'_c']] <= $user->user_level) { include($this->wctpath."pages/editcontent.php"); }
					else { include($this->wctpath."pages/405.php"); }
				break;

				case 'sdo':
					if ($tableid == '0' AND $arr_user[$this->settings['role_archive']] <= $user->user_level) { include($this->wctpath."pages/manage_designoutout.php"); }
					elseif ($tableid != '0' AND $arr_user[$this->settings['crole'.$tableid.'_s']] <= $user->user_level) { include($this->wctpath."pages/manage_designoutout.php"); }
					else { include($this->wctpath."pages/405.php"); }
				break;

				case 'importexport':
					if ($arr_user[$this->settings['crole'.$tableid.'_i']] <= $user->user_level) { include($this->wctpath."pages/importexport.php"); }
					else { include($this->wctpath."pages/405.php"); }
				break;

				case 'setup':
					include($this->wctpath."pages/editor_fixes.php");
					if ($tableid == '0' AND $arr_user[$this->settings['role_archive']] <= $user->user_level) { include($this->wctpath."pages/setup_view.php"); }
					elseif ($tableid != '0' AND $arr_user[$this->settings['crole'.$tableid.'_s']] <= $user->user_level) { include($this->wctpath."pages/setup_view.php"); }
					else { include($this->wctpath."pages/405.php"); }
				break;

				case 'show':
					include($this->wctpath."pages/setup_archive.php");
				break;
			}

		}

		function getOptions() {
			global $wpdb;
			$this->settings = get_option('wuk_custom_tables');
			if (!is_array($this->settings)) { $this->settings = array(); }

			if (!array_key_exists('wct_cachetime',$this->settings)) {
				$this->settings['wct_cachetime'] = '21600';
				update_option('wuk_custom_tables', $this->settings);
			}
			if (!array_key_exists('role_ct',$this->settings)) {
				$this->settings['role_ct'] = 'administrator';
				update_option('wuk_custom_tables', $this->settings);
			}
			if (!array_key_exists('role_css',$this->settings)) {
				$this->settings['role_css'] = 'administrator';
				update_option('wuk_custom_tables', $this->settings);
			}
			if (!array_key_exists('role_archive',$this->settings)) {
				$this->settings['role_archive'] = 'administrator';
				update_option('wuk_custom_tables', $this->settings);
			}
			if (!array_key_exists('wct_immaster',$this->settings)) {
				$this->settings['wct_immaster'] = 'no';
				update_option('wuk_custom_tables', $this->settings);
			}
			if (!array_key_exists('css',$this->settings)) {
				$this->settings['css'] = ".wct-overlay {\n\tpadding: 5px;\n\tbackground-color: white;\n\tborder: 1px black solid;\n\ttop: 1px;\n\tleft: 1px;\n\tvisibility: hidden;\n\toverflow: hidden;\n\tdisplay:none;\n\tmax-width: 300px;\n}\n.wct-td1 {\n\tbackground-color: #808080;\n}\n.wct-td2 {\n\tbackground-color: transparent;\n}\n.wct-td-hover {\n\tbackground-color: #DBD8FE;\n}\n.wct-sortdown {\n\tmargin: 0px 1px 0px 0px !important;\n}\n.wct-sortup {\n\tmargin: 0px 0px 0px 1px !important;\n}\n.wct-table {\n\twidth: 100% !important;\n}\n.wct-search {\n\twidth: 300px !important;\n}\n.wct-errorfield {\n\tborder-right: 0px !important;\n\tborder-bottom: 0px !important;\n\tborder-left: 0px !important;\n}\n.wctarchiveheader {\n\tfont-weight: bold !important;\n}\n.wctarchiveul {\n}\n.wctarchiveli {\n\tline-height: 1.1em !important;\n}\n.wctarchivea {\n\ttext-decoration: none !important;\n}";
				update_option('wuk_custom_tables', $this->settings);
			}
			if (!array_key_exists('form_serial',$this->settings)) {
				$this->settings['form_serial'] = '';
				update_option('wuk_custom_tables', $this->settings);
			}
			if (!array_key_exists('form_serialvu',$this->settings) OR $this->settings['form_serialvu'] == '') {
				$this->settings['form_serialvu'] = '0';
				update_option('wuk_custom_tables', $this->settings);
			}
			if (!array_key_exists('dbversion',$this->settings)) {
				// db mutation machen wenn eine db existiert!
				$table = $wpdb->get_row("SHOW CREATE TABLE `".$wpdb->prefix."wct_list`;");
				if (count($table) >= '1') {
					$array=array(); foreach($table as $member=>$data) { $array[$member]=$data; }
					if (strpos("`sort`",$array['Create Table']) == false AND $array['Create Table'] != '') {
						$this->info = __('Database Update done','wct');
						$wpdb->get_row("ALTER TABLE `".$wpdb->prefix."wct_list` ADD COLUMN `sort` varchar(32) NULL DEFAULT 'id', ADD COLUMN `sortB` enum('ASC','DESC') NOT NULL DEFAULT 'ASC';");
						$this->settings['dbversion'] = "2011072801";
						update_option('wuk_custom_tables', $this->settings);
					}
				}


				else {
					$this->settings['dbversion'] = WCT_DBVERSION;
					update_option('wuk_custom_tables', $this->settings);
				}
			}
		}

		function activate() {
			global $wpdb;
			$qry = $wpdb->get_results("SHOW CREATE TABLE `".$wpdb->prefix."wct_list`;");
			if (count($qry) < '1') {
				$wctpath = explode("custom-tables",plugin_dir_path( __FILE__ ));
				$wctpath = $wctpath[0]."custom-tables".DIRECTORY_SEPARATOR;
	    			include($wctpath."pages/activate.php");
			}
		}

		function uninstall() {
			global $wpdb;

			if ( wp_next_scheduled('wct_task_hook') ) {
				wp_clear_scheduled_hook('wct_task_hook');
			}

			delete_option('wuk_custom_tables');
			$qry = $wpdb->get_results("SELECT `id` FROM `".$wpdb->prefix."wct_list` WHERE `id`!='0';");
			if (count($qry) >= '1') {
				foreach ($qry as $row) {
					$wpdb->get_row("DROP TABLE `".$wpdb->prefix."wct".$row->id."`;");
				}
			}
			$wpdb->get_row("DROP TABLE `".$wpdb->prefix."wct_list`;");
			$wpdb->get_row("DROP TABLE `".$wpdb->prefix."wct_form`;");
		}

		function add_menupages() {
			global $wpdb;

			add_menu_page('Custom Tables','Custom Tables', 'level_0', __FILE__, array(&$this, 'wctmenu'));
			add_submenu_page(__FILE__, __('Create Table', 'wct'), __('Create Table', 'wct'), $this->settings['role_ct'], 'wct_table_create', array(&$this, 'create_table'));
			$qry = $wpdb->get_results("SELECT `id`,`name` FROM `".$wpdb->prefix."wct_list` WHERE `id`!='0' ORDER BY `id` ASC;");

			$arr_user = array('subscriber'=>0, 'contributor'=>1, 'author'=>2, 'editor'=>5, 'administrator'=>8);
			foreach ($qry as $row) {
				if (!array_key_exists('crole'.$row->id.'_s',$this->settings)) {
					$this->settings['crole'.$row->id.'_s'] = 'administrator';
					update_option('wuk_custom_tables', $this->settings);
				}
				if (!array_key_exists('crole'.$row->id.'_c',$this->settings)) {
					$mincap = $this->settings['crole'.$row->id.'_c'] = 'contributor';
					update_option('wuk_custom_tables', $this->settings);
					$mincap = $arr_user[$this->settings['crole'.$row->id.'_c']];
				}
				else {
					$mincap = $arr_user[$this->settings['crole'.$row->id.'_c']];
				}
				if (!array_key_exists('crole'.$row->id.'_i',$this->settings)) {
					$this->settings['crole'.$row->id.'_i'] = 'editor';
					update_option('wuk_custom_tables', $this->settings);
				}
	
				if ($mincap > $arr_user[$this->settings['crole'.$row->id.'_i']]) { $mincap = $arr_user[$this->settings['crole'.$row->id.'_i']]; };
				if ($mincap > $arr_user[$this->settings['crole'.$row->id.'_s']]) { $mincap = $arr_user[$this->settings['crole'.$row->id.'_s']]; };
				if ($_POST['vipID'] == '' OR $_POST['vipID'] != $row->id) { add_submenu_page(__FILE__, __('Table', 'wct')." ".$row->id , "&raquo; ".$row->name , "level_".$mincap, 'wct_table_'.$row->id , array(&$this, 'create_table')); }
			}
			add_submenu_page(__FILE__, __('Archive','wct'),"&raquo; ".__('Article Archive','wct'), $this->settings['role_archive'], 'wct_table_0', array(&$this, 'create_table'));
			
			if(is_admin()) {
				add_submenu_page(__FILE__, __('Create Form','wct'),__('Create Form','wct'), 'level_8', 'wct_editforms', array(&$this, 'CreateForm'));
				if ($this->settings['form_serial'] != '') {
					$qry = $wpdb->get_results("SELECT `id`,`name` FROM `".$wpdb->prefix."wct_form` ORDER BY `id` ASC;");

					foreach ($qry as $row) {
						add_submenu_page(__FILE__, __('Form', 'wct')." ".$row->id , "&raquo; ".$row->name." Form" , "level_8", 'wct_form_'.$row->id , array(&$this, 'CreateForm'));
					}
				}
			}
			add_submenu_page(__FILE__, __('Edit CSS', 'wct'), __('Edit CSS', 'wct'), $this->settings['role_css'], 'wct_css', array(&$this, 'edit_css'));
			if(is_admin()) {
				if ($this->settings['wct_unfilteredsql'] == '1') { add_submenu_page(__FILE__, __('Cronjob', 'wct'),__('Cronjob', 'wct'), 'level_8', 'wct_cronjob', array(&$this, 'Cronjob')); }

				add_submenu_page(__FILE__, __('Settings'),__('Settings'), 'level_8', 'wct_settings', array(&$this, 'Settings'));
				add_submenu_page(__FILE__, __('Backup/Restore', 'wct'),__('Backup/Restore', 'wct'), 'level_8', 'wct_backuprestore', array(&$this, 'backuprestore'));

				add_submenu_page(__FILE__, __('Support', 'wct'),__('Support', 'wct'), 'level_1', 'wct_support', array(&$this, 'Support'));
				add_submenu_page(__FILE__, 'Changelog','Changelog', 'level_8', 'wct_changelog', array(&$this, 'ChangeLog'));
				add_submenu_page(__FILE__, __('Smarttag Generator', 'wct'),__('Smarttag Generator', 'wct'), 'level_2', 'wct_taggen', array(&$this, 'taggen'));
			}
		}

		function Support() {
			include($this->wctpath."pages/wuk.php");
	    		include($this->wctpath."pages/support.php");
		}

		function Cronjob() {
			global $wpdb;
			include($this->wctpath."pages/wuk.php");
			if ($this->prem_chk() !== true) { $out = __('Premium functionality has not been activated in the plugin. Please check for a valid licence!', 'wct'); }
			else { include($this->wctpath."pages/cronjob.php"); }
		}

		function Settings() {
			global $wpdb;
			include($this->wctpath."pages/wuk.php");
	    		include($this->wctpath."pages/setup_settings.php");
		}

		function backuprestore() {
			global $wpdb;
			include($this->wctpath."pages/wuk.php");
	    		include($this->wctpath."pages/backuprestore.php");
		}

		function CreateForm() {
			global $wpdb, $current_user;

			if ($this->info != '') {
				echo $this->info;
			}

			$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'; }

			$wcttab = $_GET['wcttab'];
			
			include($this->wctpath."pages/wuk.php");
			echo "<br/><font size=\"+1\" color=\"#0000A0\">PREMIUM FEATURE</font><br/>";

			if ($this->settings['form_serial'] != '' AND $this->settings['form_serialvu'] >= time())  {
				include($this->wctpath."pages/setup_form.php");
				if ($this->settings['form_serialvu'] != '' AND $_GET['page'] == 'wct_editforms') { printf( "<br/><br/>".__('Premium features licensed. Valid until %s.','wct'), date("Y-m-d",$this->settings['form_serialvu'])); }
			}
			else {
				echo "<br/>".__('You can create Forms that Visitors or People can edit the content of your table on your own site, without having an account on your WordPress installation.', 'wct')."<br/>";
				printf( __('The Standardform can be added with %s to the page and includes all rights (read, write, delete, create) to edit all Tables.', 'wct'), '<input type="text" size="30" value=\'[wcteditpage table="*" filter="" html="0"]\' readonly>');
				echo "<br/>".__('You can use the filter as SQL Filter that not all Entries are shown. The html Tag can be used to enable (1) the HTML Editor or disable it (0).','wct')."<br/><br/>".__('To create individual forms with individual rights, you need to have a valid serial number for the premium feature.', 'wct');
				if ($this->settings['form_serial'] != '') {
					echo "<br/><br/>".__('The license has expired, please renew it','wct')." <a href=\"admin.php?page=wct_settings#premium\">". __('now','wct')."</a>.";
				}
				else {
					echo "<br/><br/>".__('No valid premium feature license found, please enter serial ','wct')." <a href=\"admin.php?page=wct_settings#premium\">". __('now','wct')."</a>.";
				}
			}
		}

		function ChangeLog() {
			include($this->wctpath."pages/wuk.php");
			include($this->wctpath."pages/view_changelog.php");
		}

		function edit_css() {
			include($this->wctpath."pages/wuk.php");
			include($this->wctpath."pages/setup_css.php");
		}

		function wct_sort($feld,$table) {
			$url = $this->generate_pagelink(array("/[&?]+wctinvs=1*/","/[&?]+wctsort=.*&/"),array("","&"));

			if ($feld == $_REQUEST['wctsort']) {
				if ($table->sheme == '1') {
					if ($_REQUEST['wctinvs'] == '1') { $zusatz1 = "_white";  } else { $zusatz2 = "_white"; }
				}
				else {
					if ($_REQUEST['wctinvs'] == '1') { $zusatz1 = "_black";  } else { $zusatz2 = "_black"; }
				}
			}
			$out .= "<a href=\"".$url."wctsort=".$feld."\"><img class=\"wct-sortdown\" src=\"".plugins_url('custom-tables/img/sort_down'.$zusatz2.'.gif')."\" alt=\"sort down\" border=\"0\" /></a>";
			$out .= "<a href=\"".$url."wctsort=".$feld."&wctinvs=1\"><img class=\"wct-sortup\" src=\"".plugins_url('custom-tables/img/sort_up'.$zusatz1.'.gif')."\" alt=\"sort up\" border=\"0\" /></a>";

			return $out;
		}

		function tablerights($label,$role,$recht = 'administrator') {
			global $profileuser;
			$user_roles = $profileuser->roles;
			$user_role = @array_shift($user_roles);
			echo "<tr><td><label for=\"role\">". $label ."</label></td><td><select name=\"wctrole_".$role."\" id=\"wctrole".$role."\">";
			wp_dropdown_roles($recht);
			echo "</select></td></tr>";
			return true;
		}

		function option($label,$role,$recht = '', $desc = '') {
			echo "<tr><td><label for=\"role\">". $label ."</label></td><td><input type=\"text\" name=\"wctrole_".$role."\" id=\"wctrole".$role."\" value=\"".$recht."\"/></td><td>&nbsp;<i>".$desc."</i></td></tr>";
			return true;
		}

		function disable_wysiwyg($wert) {
    			if($_GET['wcttab'] == 'setup') { return false; }
    			return $wert;
		}

		function generate_pagelink($arrayA,$arrayB) {
			include($this->wctpath."pages/generate_url.php");
			return $url;
		}

		function http_request($method, $url, $data = '', $auth = '', $check_status = true) {
			$status = 0;
			$method = strtoupper($method);
			if (function_exists('curl_init')) {
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, $url);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($ch, CURLOPT_USERAGENT, 'SMuBot');
				@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
				curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
				curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
				curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
				curl_setopt($ch, CURLOPT_TIMEOUT, 180);

				switch ($method) {
					case 'POST':
						curl_setopt($ch, CURLOPT_POST, true);
						curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
						break;

					case 'PURGE':
						curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PURGE');
						break;
				}
				if ($auth) { curl_setopt($ch, CURLOPT_USERPWD, $auth); }
				$contents = curl_exec($ch);
				$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
				curl_close($ch);
			}
			else {
				$parse_url = @parse_url($url);
				if ($parse_url && isset($parse_url['host'])) {
					$host = $parse_url['host'];
					$port = (isset($parse_url['port']) ? (int) $parse_url['port'] : 80);
					$path = (!empty($parse_url['path']) ? $parse_url['path'] : '/');
					$query = (isset($parse_url['query']) ? $parse_url['query'] : '');
					$request_uri = $path . ($query != '' ? '?' . $query : '');
					$request_headers_array = array(
						sprintf('%s %s HTTP/1.1', $method, $request_uri),
						sprintf('Host: %s', $host),
						sprintf('User-Agent: %s', 'SMuBot'),
						"Content-type: application/x-www-form-urlencoded"
					);
					if (!empty($data)) { $request_headers_array[] = sprintf('Content-Length: %d', strlen($data)); }
					if (!empty($auth)) { $request_headers_array[] = sprintf('Authorization: Basic %s', base64_encode($auth)); }
					$request_headers_array[] = "Connection: close";
					$request_headers = implode("\r\n", $request_headers_array);
					$request = $request_headers . "\r\n\r\n" . $data . "\r\n\r\n";
					$errno = null;
					$errstr = null;
					$fp = @fsockopen($host, 80, $errno, $errstr, 30);
					if (!$fp) { return false; }
					$response = '';
					@fputs($fp, $request);
					while (!@feof($fp)) { $response .= @fgets($fp, 4096); }
					@fclose($fp);
					list($response_headers, $contents) = explode("\r\n\r\n", $response, 2);
					$matches = null;
					if (preg_match('~^HTTP/1.[01] (\d+)~', $response_headers, $matches)) { $status = (int) $matches[1]; }
				}
			}

			if (!$check_status || $status == 200) { return $contents; }
			return false;
		}

		function plugin_updates() {
			$data = $this->http_request('GET','http://plugins.trac.wordpress.org/browser/custom-tables/trunk/readme.txt?format=txt');

			if ($data) {
				$matches = null;
				$regexp = '~==\s*Changelog\s*==\s*=\s*[0-9.]+\s*=(.*)(=\s*' . preg_quote(WCT_VERSION) . '\s*=|$)~Uis';

				if (preg_match($regexp, $data, $matches)) {
					$changelog = (array) preg_split('~[\r\n]+~', trim($matches[1]));

					echo '<div style="color: #f00;">'.__('Please find below the new Features / Bugfixes', 'wct').':</div><div style="font-weight: normal;">';
					$ul = false;

					foreach ($changelog as $index => $line) {
						if (preg_match('~^\s*\*\s*~', $line)) {
							if (!$ul) {
								echo '<ul style="list-style: disc; margin-left: 20px;">';
								$ul = true;
							}
							$line = preg_replace(array('~^\s*\*\s*~',"/\[(.*?)\]/"), array('',"<i>[$1]</i>"), htmlspecialchars($line));
							echo '<li style="width: 50%; margin: 0; float: left; ' . ($index % 2 == 0 ? 'clear: left;' : '') . '">' . $line . '</li>';
						} else {
							if ($ul) {
								echo '</ul><div style="clear: left;"></div>';
								$ul = false;
							}
							echo '<p style="margin: 5px 0;">' . htmlspecialchars($line) . '</p>';
						}
					}

					if ($ul) {
						echo '</ul><div style="clear: left;"></div>';
					}

					echo '</div>';
				}
			}
		}

		function ShowTinyMCE() {
			wp_enqueue_script( 'common' );
			wp_enqueue_script( 'jquery-color' );
			wp_print_scripts('editor');
			if (function_exists('add_thickbox')) add_thickbox();
			wp_print_scripts('media-upload');
			if (function_exists('wp_tiny_mce')) wp_tiny_mce();
			wp_admin_css();
			wp_enqueue_script('utils');
			do_action("admin_print_styles-post-php");
			do_action('admin_print_styles');
		}

		/* Hooks for additional Extensions */
		function hook_table($content) { return $content; }
		function hook_entry($content) { return $content; }
		function hook_overlay($content) { return $content; }
		function prem_chk($serial = '', $do = '0') {
			if ($serial == '') { $serial = $this->settings['form_serial']; }
			if ($do == '1') { return $serial; }
			else {
				$parts = explode('-',$serial);
				if(sizeof($parts) != 4) return false;
 				list($part1, $part2, $part3, $part4) = $parts;
				if (($part2 % 4) == substr($part3,2,1) AND ($part1 % 9) == substr($part2,0,1) AND ($part1 % 5) == substr($part2,3,1) AND $part4 == (($part3 + 4) % 8).(($part1 + $part3 * 39) % 7 ).(($part2 + 12 + $part3 / (2/7)) % 3 ).(($part2 * 27 + 1) % 3 )) { return true; }
				else { return false; }
			}
		}

		function iplugin($name,$value,$check,$sett,$desc = '',$settings = 'options-general.php') {
			$wuk_plugin_check = substr(plugin_dir_path( __FILE__ ),0,stripos( plugin_dir_path( __FILE__ ) , PLUGINDIR ) ).PLUGINDIR.DIRECTORY_SEPARATOR;

			echo "<tr><th scope=\"row\" valign=\"top\"><b>".$name."</b></th><td>&nbsp;</td><td>";
			if (file_exists($wuk_plugin_check.$check)) {
				echo __('Plugin is already installed','wct');
				if ($settings != '') {
					echo " (<a href=\"".$settings."?page=".$sett."\">Einstellungen</a>)";
				}
			}
			else {
				printf(__('Plugin %s not installed.', 'wct'), $name);
				if (strpos($value,"http") !== false) {
					echo " <a target=\"_blank\" href=\"".$value."\">".__('Install now', 'wct')."</a>";
				}
				elseif ($value != '') {
					echo " <a target=\"_blank\" href=\"plugin-install.php?tab=plugin-information&plugin=".$value."\">".__('Install now', 'wct')."</a>";
				}
			}
			echo ".";
			if ($desc != '') {
				echo "<br/><small>".$desc."</small>";
			}				
			echo "</td></tr>";
		}
	}

	add_action('init', 'wuk_custom_tables1');
	function wuk_custom_tables1() {
		global $wuk_custom_tables;
		$wuk_custom_tables = new wuk_custom_tables();
	}
}

include_once("tinymce-class.php");

if (function_exists('register_activation_hook')) { register_activation_hook(__FILE__, array('wuk_custom_tables', 'activate')); }
if (function_exists('register_uninstall_hook')) { register_uninstall_hook(__FILE__, array('wuk_custom_tables', 'uninstall')); }
if (!function_exists('wct_session_start')) { function wct_session_start() { session_start(); } }
if (!session_id()) { add_action('init', 'wct_session_start'); }


?>