<?php 
/*************************************************************
* Do not modify unless you know what you're doing, SERIOUSLY!
*************************************************************/
error_reporting(E_ERROR);

include_once (TEMPLATEPATH . '/library/functions/custom_post_type_defined.php');

load_theme_textdomain('default');
/*load_textdomain( 'default', TEMPLATEPATH.'/en_US.mo' );*/

// This theme uses post thumbnails
add_theme_support( 'post-thumbnails' );
// Add default posts and comments RSS feed links to head
add_theme_support( 'automatic-feed-links' );
	
global $blog_id;
if(get_option('upload_path') && !strstr(get_option('upload_path'),'wp-content/uploads'))
{
	$upload_folder_path = "wp-content/blogs.dir/$blog_id/files/";
}else
{
	$upload_folder_path = "wp-content/uploads/";
}
global $blog_id;
if($blog_id){ $thumb_url = "&amp;bid=$blog_id";}

if ( function_exists( 'add_theme_support' ) ){
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'nav-menus' );
	add_theme_support( 'automatic-feed-links' );
	}

### Rating Logs Table Name
global $wpdb;
$term_icon_column=$wpdb->get_var("SHOW COLUMNS FROM $wpdb->terms where field='term_icon'");
if(!$term_icon_column)
{
	$wpdb->query("ALTER TABLE $wpdb->terms ADD `term_icon` TEXT NULL DEFAULT NULL");
}
$rating_table_name = $wpdb->prefix.'ratings';

require(TEMPLATEPATH . "/product_menu.php");

include_once (TEMPLATEPATH . '/library/map/map_functions.php');
include_once (TEMPLATEPATH . '/library/rating/post_rating.php');
if(get_option('ptthemes_captcha_dislay')=='No'){}else
{
include_once (TEMPLATEPATH . '/library/captcha/captcha_function.php');
}
include_once (TEMPLATEPATH . '/language.php');

// Theme variables
include_once (TEMPLATEPATH . '/library/functions/theme_variables.php');

//** ADMINISTRATION FILES **//

    // Theme admin functions
    include_once ($functions_path . 'admin_functions.php');

    // Theme admin options
    include_once ($functions_path . 'admin_options.php');

    // Theme admin Settings
    include_once ($functions_path . 'admin_settings.php');

   
//** FRONT-END FILES **//

    // Widgets
    include_once ($functions_path . 'widgets_functions.php');
	include_once ($functions_path . 'common_widgets.php');

    // Custom
    include_once ($functions_path . 'custom_functions.php');


    // Comments
    include_once ($functions_path . 'comments_functions.php');
	
	include_once ($functions_path . 'yoast-canonical.php');

	include_once ($functions_path . 'yoast-breadcrumbs.php');
	
	include_once ($functions_path . 'most-popular.php');
	include_once ($functions_path . 'image_resizer.php');

	include_once (TEMPLATEPATH . '/admin/main.php');  // framework main file included

include_once ($functions_path . 'custom_post_type.php'); //custom post type
include_once ($functions_path . 'listing_filters.php'); //listing filter

//theme.php
function autoinstall_admin_header()
{
	global $wpdb;
	if(strstr($_SERVER['REQUEST_URI'],'themes.php') && $_REQUEST['template']=='' && $_GET['page']=='') 
	{
		
		if($_REQUEST['dummy']=='del')
		{
			delete_dummy_data();	
			$dummy_deleted = '<p><b>All Dummy data has been removed from your database successfully!</b></p>';
		}
		if($_REQUEST['dummy_insert'])
		{
			include_once (TEMPLATEPATH . '/auto_install.php');
		}
		if($_REQUEST['activated']=='true')
		{
			$theme_actived_success = '<p class="message">Theme activated successfully.</p>';	
		}
		$post_counts = $wpdb->get_var("select count(post_id) from $wpdb->postmeta where (meta_key='pt_dummy_content' || meta_key='tl_dummy_content') and meta_value=1");
		if($post_counts>0)
		{
			$dummy_data_msg = '<p> <b>Sample data has been populated on your site. Wish to delete sample data?</b><br />If you have problem with image, please copy images/dummy folder to wp-content/uploads <br />  <a class="button_delete" href="'.get_option('siteurl').'/wp-admin/themes.php?dummy=del">Yes Delete Please!</a><p>';
		}else
		{
			$dummy_data_msg = '<p> <b>Would you like to auto install this theme and populate sample data on your site?</b> <br />  <a class="button_insert" href="'.get_option('siteurl').'/wp-admin/themes.php?dummy_insert=1">Yes, insert sample data please</a></p>';
		}


		define('THEME_ACTIVE_MESSAGE','
	<style>
	.highlight { width:60% !important; background:#FFFFE0 !important; overflow:hidden; display:table; border:2px solid #558e23 !important; padding:15px 20px 0px 20px !important; -moz-border-radius:11px  !important;  -webkit-border-radius:11px  !important; } 
	.highlight p { color:#444 !important; font:15px Arial, Helvetica, sans-serif !important; text-align:center;  } 
	.highlight p.message { font-size:13px !important; }
	.highlight p a { color:#ff7e00; text-decoration:none !important; } 
	.highlight p a:hover { color:#000; }
	.highlight p a.button_insert 
		{ display:block; width:230px; margin:10px auto 0 auto;  background:#5aa145; padding:10px 15px; color:#fff; border:1px solid #4c9a35; -moz-border-radius:5px;  -webkit-border-radius:5px;  } 
	.highlight p a:hover.button_insert { background:#347c1e; color:#fff; border:1px solid #4c9a35;   } 
	.highlight p a.button_delete 
		{ display:block; width:140px; margin:10px auto 0 auto; background:#dd4401; padding:10px 15px; color:#fff; border:1px solid #9e3000; -moz-border-radius:5px;  -webkit-border-radius:5px;  } 
	.highlight p a:hover.button_delete { background:#c43e03; color:#fff; border:1px solid #9e3000;   } 
	#message0 { display:none !important;  }
	</style>
	
	<div class="updated highlight fade"> '.$theme_actived_success.$dummy_deleted.$dummy_data_msg.'</div>');
		echo THEME_ACTIVE_MESSAGE;
		
	}
}
	
add_action("admin_head", "autoinstall_admin_header"); // please comment this line if you wish to DEACTIVE SAMPLE DATA INSERT.

function delete_dummy_data()
{
	global $wpdb;
	delete_option('sidebars_widgets'); //delete widgets
	$productArray = array();
	$pids_sql = "select p.ID from $wpdb->posts p join $wpdb->postmeta pm on pm.post_id=p.ID where (meta_key='pt_dummy_content' || meta_key='tl_dummy_content') and meta_value=1";
	$pids_info = $wpdb->get_results($pids_sql);
	foreach($pids_info as $pids_info_obj)
	{
		wp_delete_post($pids_info_obj->ID);
	}
}

/////////////////PLACE PRICE SETTINGS CODING START/////////////////
global $wpdb,$table_prefix;
$price_db_table_name = $table_prefix . "price";
if($wpdb->get_var("SHOW TABLES LIKE \"$price_db_table_name\"") != $price_db_table_name)
{
	$price_table = 'CREATE TABLE IF NOT EXISTS `'.$price_db_table_name.'` (
	  `pid` int(11) NOT NULL AUTO_INCREMENT,
	  `title` varchar(255) NOT NULL,
	  `amount` float(12,2) NOT NULL,
	  `cat` varchar(255) NOT NULL,
	  `status` tinyint(2) NOT NULL DEFAULT \'1\',
	  `days` int(10) NOT NULL,
	  PRIMARY KEY (`pid`)
	)';
	$wpdb->query($price_table);
	
	$price_insert = '
	INSERT INTO `'.$price_db_table_name.'` (`pid`, `title`, `amount`, `status`, `days`) VALUES
	(1, "Free", 0.00, 1, 30),
	(2, "Featured", 10.00, 1, 90)';
	$wpdb->query($price_insert);
}
/////////////////PLACE PRICE SETTINGS CODING END/////////////////
/////////////////PLACE EXPIRY SETTINGS CODING START/////////////////
global $table_prefix, $wpdb;
$table_name = $table_prefix . "place_expire_session";
$current_date = date('Y-m-d');
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name)
{
   global $table_prefix, $wpdb,$table_name;
   $sql = 'DROP TABLE `' . $table_name . '`';  // drop the existing table
   mysql_query($sql);

	$sql = 'CREATE TABLE `'.$table_name.'` (
			`session_id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
			`execute_date` DATE NOT NULL ,
			`is_run` TINYINT( 4 ) NOT NULL DEFAULT "0"
			) ENGINE = MYISAM ;';
   mysql_query($sql);
}
$today_executed = $wpdb->get_var("select session_id from $table_name where execute_date=\"$current_date\"");
if($today_executed && $today_executed>0)
{
}else
{
	if(get_option('ptthemes_listing_expiry_disable'))
	{
		
	}else
	{
		if(get_option('ptthemes_listing_preexpiry_notice_disable'))
		{
			$number_of_grace_days = get_option('ptthemes_listing_preexpiry_notice_days');
			if($number_of_grace_days=='')
			{
				$number_of_grace_days=1;	
			}
			$postid_str = $wpdb->get_results("select p.ID,p.post_author,p.post_date, p.post_title from $wpdb->posts p where p.post_type='post' and p.post_status='publish' and datediff(\"$current_date\",date_format(p.post_date,'%Y-%m-%d')) > (select meta_value from $wpdb->postmeta pm where post_id=p.ID  and meta_key='alive_days')-$number_of_grace_days");
			foreach($postid_str as $postid_str_obj)
			{
				$ID = $postid_str_obj->ID;
				$post_author = $postid_str_obj->post_author;
				$post_date = date('dS m,Y',strtotime($postid_str_obj->post_date));
				$post_title = $postid_str_obj->post_title;
				$userinfo = $wpdb->get_results("select user_email,display_name,user_login from $wpdb->users where ID=\"$ID\"");
				$user_email = $userinfo[0]->user_email;
				$display_name = $userinfo[0]->display_name;
				$user_login = $userinfo[0]->user_login;
				
				$fromEmail = get_site_emailId();
				$fromEmailName = get_site_emailName();
				$alivedays = get_post_meta($ID,'alive_days',true);
				$productlink = get_permalink($ID);
				$loginurl = get_option('siteurl').'/?ptype=login';
				$siteurl = get_option('siteurl');
				$client_message = "<p>Dear $display_name,<p><p>Your listing -<a href=\"$productlink\"><b>$post_title</b></a> posted on  <u>$post_date</u> for $alivedays days.</p>
				<p>It's going to expiry after $number_of_grace_days day(s). If the listing expire, it will no longer appear on the site.</p>
				<p> If you want to renew, Please login to your member area of our site and renew it as soon as it expire. You may like to login the site from <a href=\"$loginurl\">$loginurl</a>.</p>
				<p>Your login ID is <b>$user_login</b> and Email ID is <b>$user_email</b>.</p>
				<p>Thank you,<br /><a href=\"$siteurl\">$fromEmailName</a>.</p>";
				
				$subject = 'Place listing expiration Notification';
				$headers  = 'MIME-Version: 1.0' . "\r\n";
				$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
				$headers .= 'To: '.$display_name.' <'.$user_email.'>' . "\r\n";
				$headers .= 'From: '.$fromEmailName.' <'.$fromEmail.'>' . "\r\n";
				@mail($user_email,$subject,$client_message,$headers);///To client email
			}
		}
		$postid_str = $wpdb->get_var("select group_concat(p.ID) from $wpdb->posts p where p.post_type='post' and p.post_status='publish' and datediff(\"$current_date\",date_format(p.post_date,'%Y-%m-%d')) > (select meta_value from $wpdb->postmeta pm where post_id=p.ID  and meta_key='alive_days')");
	
		if($postid_str)
		{
			$listing_ex_status = get_option('ptthemes_listing_ex_status');
			if($listing_ex_status=='')
			{
				$listing_ex_status = 'draft';	
			}
			$wpdb->query("update $wpdb->posts set post_status=\"$listing_ex_status\" where ID in ($postid_str)");
		}
		$wpdb->query("insert into $table_name (execute_date,is_run) values (\"$current_date\",'1')");
	}
}

/////////////////PLACE EXPIRY SETTINGS CODING END/////////////////

?>