<?php include("config.php");
require_once(ROOT."/app_classes/module_campaigns.inc");
require_once(ROOT."/app_classes/module_tags.inc"); 
require_once(ROOT."/app_classes/module_sizes.inc");

if($_POST[login])
{
	$email = mysql_real_escape_string($_POST[email]);
	$password = mysql_real_escape_string($_POST[password]);
	$check = $logged->check_login($email,$password);
	if($check == 0)
	{
		echo language("ERROR_login_email_password");
		exit();
	}
	if($check == 2)
	{
		echo language("ERROR_login_user_blocked");
		exit();
	}
	$user = new class_users($check);
	if($user->get_verified() == 0 && $config->get_value('email_confirm'))
	{
		unset($_SESSION['email']);
		unset($_SESSION['unique']);
		echo language("ERROR_login_unverified");
		exit();
	}
	if($check)
	{
		echo "<script>window.location.href = '".APP_PATH."';</script>";
	}
	
}

if($_POST[logout])
{
	if($logged->logout())
	{	
		echo 1;
	}
}

if($_POST[user_settings])
{
	$user = new class_users($_POST[unique]);
	$new_password = mysql_real_escape_string($_POST[new_password]);
	$repeat_password = mysql_real_escape_string($_POST[repeat_password]);
	
		if($new_password && $repeat_password)
		{
			if(strlen($new_password) < 6 || strlen($repeat_password) <6 )
			{
			echo language('ERROR_password_characters');
			exit();
			}
			
			if($new_password == $repeat_password)
			{
				echo language('SUCCESS_password_changed');
				$user->set_password($new_password);
				exit();
			}
			else
			{
				echo language('ERROR_repeat_password');
				exit();
			}
		}
		$user->set_email($_POST[user_email]);
		echo "Email Changed";
	

}

if($_POST[delete_size]) 
{
	$size = new class_sizes($_POST[unique]);
	$size->del();
	echo language('SUCCESS_size_deleted');
}
if($_POST[create_size])
{
	if($_POST[width] && $_POST[height])
	{
	class_sizes::add($_POST[width],$_POST[height]);
	echo language('SUCCESS_size').$_POST[width]."x".$_POST[height];
	}
	else
	echo "All Fields Required";
}

if($_POST[create_tag] || $_POST[edit_tag])
{
	$name = mysql_real_escape_string($_POST[tag_name]);
	$url = mysql_real_escape_string($_POST[tag_url]);
	$size = mysql_real_escape_string($_POST[size]);
	$sizes = new class_sizes($size);
	if($name && $url && $size)
	{
		
		if(filter_var($url, FILTER_VALIDATE_URL) === FALSE)
		{
			echo language('ERROR_url');
			exit();
		}
		
		$parse = parse_url($url);
		$url = $parse['host'];
		if($_POST[create_tag])
		{
			class_tags::add($logged->get_unique(), $name, $url, 'all', $sizes->get_size(),$tag_cost);
			echo language('SUCCESS_tag_created');
		}
		if($_POST[edit_tag])
		{
			$tag = new class_tags($_POST[unique]);
			$tag->set_name($name);
			$tag->set_website($url);
			$tag->set_ad_type($sizes->get_size());
			echo language('SUCCESS_tag_modified');

		
		}
	}
	else
	echo language('COMMON_ERROR_fields');;

}

//Create Campaign Banner
if($_POST[create_campaign_banner] || $_POST[edit_campaign_banner])
{
	
	$countries = 'all';
	if(!empty($_POST[countries]))
	$countries = implode(',',$_POST[countries]);

	if(!empty($_POST[websites]))
	$websites = implode(',',$_POST[websites]);
	
	$agents = "all";
	if(!empty($_POST[agents]))
	$agents = implode(',',$_POST[agents]);
	
	$agents = mysql_real_escape_string($agents);
	$websites = mysql_real_escape_string($websites);
	$countries = mysql_real_escape_string($countries);
	
	$ad_type = mysql_real_escape_string($_POST[ad_type]);
	$campaign_name = mysql_real_escape_string($_POST[campaign_name]);

	$url = mysql_real_escape_string($_POST[url]);
	$ad_content = preg_replace(array("/\r\n\r\n/", "/\n\n/","/\r\r/"), " ", htmlspecialchars(htmlentities($_POST[html]),ENT_QUOTES));
	if($ad_content)
	$html_content = 1;
	
	if( $websites && $campaign_name && $url && ($_FILES[myfile] || $_POST[edit_campaign_banner] || $html_content))
	{
		
		if(filter_var($url, FILTER_VALIDATE_URL) === FALSE)
		{
			echo language('ERROR_url');
			exit();
		}
		if($_FILES["myfile"]['tmp_name'])
		{   
   			if ($_FILES["myfile"]['size'] >= $config->get_value('upload_size'))
    		{
    			echo  language('ERROR_file_big').$config->get_value('upload_size')."KB !";
				exit();
    		}
			$extension = end(explode('.', $_FILES["myfile"]["name"]));
			$allowedExtensions = array("jpg","jpeg","gif","png");
			if(!in_array($extension, $allowedExtensions))
			{
				echo language('ERROR_image_extension');
				exit();
			}
		
		}
		
		if($_POST[create_campaign_banner])
		{
			
			if(!$_FILES["myfile"]['tmp_name'] && !$html_content)
			{
				echo language('ERROR_upload');
				exit();
			}
		if($_POST[html])
		class_campaigns::add($logged->get_unique(), $websites, $countries, $agents, $campaign_name, $campaign_cost, $budget,$bid, $ad_type, $url, $ad_content, $html_content,$limit,$capping);
		else
		class_campaigns::add($logged->get_unique(), $websites, $countries, $agents, $campaign_name, $campaign_cost, $budget,$bid, $ad_type, $url, $_FILES["myfile"], $html_content,$limit,$capping);
		echo language('SUCCESS_campaign_saved');
		exit();
		}
		if($_POST[edit_campaign_banner])
		{

			$campaign = new class_campaigns($_POST[unique]);
			$campaign->set_ad_destination($url);
			$campaign->set_name($campaign_name);
			$campaign->set_unique_agents($agents);
			$campaign->set_unique_countries($countries);
			$campaign->set_unique_websites($websites);
			if($html_content)
			{
				$campaign->set_html_content($html_content);
				$campaign->set_ad_content($ad_content);
				
			}
			if($_FILES["myfile"]['tmp_name'])
			{
				$campaign->set_html_content(0);
				$campaign->set_ad_content($_FILES["myfile"]);
				
			}
			echo language('SUCCESS_campaign_saved');
		}
		
	}
	else
	echo language('COMMON_ERROR_fields');;
}


if($_POST[change_hold])
{
	$type = substr($_POST[unique],0,1);
	if($type == "c")
	{
		$unique = substr($_POST[unique],1);
		$campaign = new class_campaigns($unique);
		$campaign->hold();
		echo language('SUCCESS_status_changed');
	}
	if($type == "t")
	{
		$unique = substr($_POST[unique],1);
		$tag = new class_tags($unique);
		$tag->hold();
		echo language('SUCCESS_status_changed');
	}
	
	if($type === "x")
	{
		
		$unique = substr($_POST[unique],1);
		$config->switcher($unique);
		echo language('SUCCESS_status_changed');

	}
}
if($_POST[delete_campaign])
{

	$campaign = new class_campaigns($_POST[unique]);
	$campaign->del();
	echo language('SUCCESS_campaign_deleted');
}
if($_POST[delete_tag])
{

	$tag = new class_tags($_POST[unique]);
	$tag->del();
	echo language('SUCCESS_tag_deleted');
}

if($_POST[general_settings])
{
	$platform_title = mysql_real_escape_string($_POST[platform_title]);
	$watermark = mysql_real_escape_string($_POST[watermark]);
	$watermark_title = mysql_real_escape_string($_POST[watermark_title]);
	$platform_logo = mysql_real_escape_string($_POST[platform_logo]);
	$platform_favicon = mysql_real_escape_string($_POST[platform_favicon]);
	$watermark_url = mysql_real_escape_string($_POST[watermark_url]);
	$config->set_value('platform_title',$platform_title);
	$config->set_value('watermark',$watermark);
	$config->set_value('watermark_url',$watermark_url);
	$config->set_value('watermark_title',$watermark_title);
	if($_FILES[upload_logo]['tmp_name'])
	{
			$target_dir = '../app_images/';
            $extension=pathinfo($_FILES[upload_logo]["name"],PATHINFO_EXTENSION);
            $target_dir .= 'logo.'.$extension;
            
                if (move_uploaded_file($_FILES[upload_logo]["tmp_name"], $target_dir))
				$config->set_value('platform_logo',APP_PATH.'/app_images/logo.'.$extension);
	}
	else
	$config->set_value('platform_logo',$platform_logo);
	if($_FILES[upload_favicon]['tmp_name'])
	{
			$target_dir = '../app_images/';
            $extension=pathinfo($_FILES[upload_favicon]["name"],PATHINFO_EXTENSION);
            $target_dir .= 'favicon.'.$extension;
            
                if (move_uploaded_file($_FILES[upload_favicon]["tmp_name"], $target_dir))
				$config->set_value('platform_favicon',APP_PATH.'/app_images/favicon.'.$extension);
	}
	else
	$config->set_value('platform_favicon',$platform_favicon);
	$config->set_value('language',$_POST[language]);
	echo language('SUCCESS_platform_details');

}
	mysql_close($conn);
?>