<?php 

include("../../functions.php");
include("../../configuration.php");
include("../../dbhandle.php");
include("../../security.php");
include("class.country.php");

if (isset($_GET['command'])) {
$command = $_GET['command'];
$command = AntiXSS::setEncoding($command, "UTF-8"); 
$command = str_replace("<", "", "$command");
$command = str_replace(">", "", "$command");
$command = str_replace("'", "", "$command");
$command = str_replace('"', "", "$command");
$command = str_replace("(", "", "$command");
$command = str_replace(")", "", "$command");
$apikey = $_GET['api'];
$apikey = AntiXSS::setEncoding($apikey, "UTF-8"); 
$apikey = str_replace("<", "", "$apikey");
$apikey = str_replace(">", "", "$apikey");
$apikey = str_replace("'", "", "$apikey");
$apikey = str_replace('"', "", "$apikey");
$apikey = str_replace("(", "", "$apikey");
$apikey = str_replace(")", "", "$apikey");
$data1 = $_GET['data1'];
$data1 = AntiXSS::setEncoding($data1, "UTF-8"); 
$data1 = str_replace("<", "", "$data1");
$data1 = str_replace(">", "", "$data1");
$data1 = str_replace("'", "", "$data1");
$data1 = str_replace('"', "", "$data1");
$data1 = str_replace("(", "", "$data1");
$data1 = str_replace(")", "", "$data1");
if($data1 == 'null') {$data1='';}
$data2 = $_GET['data2'];
$data2 = AntiXSS::setEncoding($data2, "UTF-8"); 
$data2 = str_replace("<", "", "$data2");
$data2 = str_replace(">", "", "$data2");
$data2 = str_replace("'", "", "$data2");
$data2 = str_replace('"', "", "$data2");
$data2 = str_replace("(", "", "$data2");
$data2 = str_replace(")", "", "$data2");
if($data2 == 'null') {$data2='';}
$data3 = $_GET['data3'];
$data3 = AntiXSS::setEncoding($data3, "UTF-8"); 
$data3 = str_replace("<", "", "$data3");
$data3 = str_replace(">", "", "$data3");
$data3 = str_replace("'", "", "$data3");
$data3 = str_replace('"', "", "$data3");
$data3 = str_replace("(", "", "$data3");
$data3 = str_replace(")", "", "$data3");
if($data3 == 'null') {$data3='';}
$data4 = $_GET['data4'];
$data4 = AntiXSS::setEncoding($data4, "UTF-8"); 
$data4 = str_replace("<", "", "$data4");
$data4 = str_replace(">", "", "$data4");
$data4 = str_replace("'", "", "$data4");
$data4 = str_replace('"', "", "$data4");
$data4 = str_replace("(", "", "$data4");
$data4 = str_replace(")", "", "$data4");
if($data4 == 'null') {$data4='';}
$data5 = $_GET['data5'];
$data5 = AntiXSS::setEncoding($data5, "UTF-8"); 
$data5 = str_replace("<", "", "$data5");
$data5 = str_replace(">", "", "$data5");
$data5 = str_replace("'", "", "$data5");
$data5 = str_replace('"', "", "$data5");
$data5 = str_replace("(", "", "$data5");
$data5 = str_replace(")", "", "$data5");
if($data5 == 'null') {$data5='';}
$data6 = $_GET['data6'];
$data6 = AntiXSS::setEncoding($data6, "UTF-8"); 
$data6 = str_replace("<", "", "$data6");
$data6 = str_replace(">", "", "$data6");
$data6 = str_replace("'", "", "$data6");
$data6 = str_replace('"', "", "$data6");
$data6 = str_replace("(", "", "$data6");
$data6 = str_replace(")", "", "$data6");
if($data6 == 'null') {$data6='';}
$data7 = $_GET['data7'];
$data7 = AntiXSS::setEncoding($data7, "UTF-8"); 
$data7 = str_replace("<", "", "$data7");
$data7 = str_replace(">", "", "$data7");
$data7 = str_replace("'", "", "$data7");
$data7 = str_replace('"', "", "$data7");
$data7 = str_replace("(", "", "$data7");
$data7 = str_replace(")", "", "$data7");
if($data7 == 'null') {$data7='';}
// FIND USER WHERE API == ENTERED API
$pub1 = mysql_query("SELECT * FROM Accounts_Classic_Publishers WHERE api='$apikey'") or die('1');
$pub2 = mysql_fetch_array($pub1);
$adv1 = mysql_query("SELECT * FROM Accounts_Classic_Advertisers WHERE api='$apikey'") or die('1');
$adv2 = mysql_fetch_array($adv1);
$session_username = $pub2['username'];

//
//$insert = "INSERT INTO MyGuests (valuea, valueb) VALUES ('valuea', 'valueb')";
//$update = "UPDATE MyGuests SET valuea='valuea' WHERE id='2'";
//$delete = "DELETE FROM MyGuests WHERE id='3' AND api='apikey'";
//

if(mysql_num_rows($adv1) > 0){
if(mysql_num_rows($pub1) > 0){
if($command == 'createlink') {//COMPLETE
	//createlink
	//input:
	//       >>[data1] = url
	//       >>[data2] = password
	//       >>[data3] = adtype
	//       >>[data4] = title
	//output:
	//       >>status
	//       >>url
	//       >>code
  $charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  $len = 5;
  $numrows = 1;
  
  while ($numrows != 0){
  		for ($i = 0; $i <= $len; $i++) {
      	$rand = rand() % strlen($charset);
      	$tmp = substr($charset,$rand, 1);
      	$code .= $tmp;
  		}
  		$query = mysql_query("SELECT * FROM Links_All WHERE code='$code'") or die('1');
  		$numrows = mysql_num_rows($query);
  	}

	$url = mysql_real_escape_string($data1);
	$passwordnew = md5($data3);
  	mysql_query("INSERT INTO Links_All VALUES ('', '$data1', '$code','$session_username','$passwordnew','0','0.00','$data4','$data2')") or die('1');
  	$qstats = mysql_query("UPDATE Web_Stats SET sclinks7=sclinks7 + '1' WHERE id='1'") or die('1');
  	$query = mysql_query("SELECT * FROM Links_All WHERE code='$code'") or die('1');
  	$numrows = mysql_num_rows($query);
  	?>
	{"status":"1","url":"<?php echo"$mainurl$code"; ?>","code":"<?php echo"$code"; ?>"}
	<?php
}elseif($command == 'editlink') {//COMPLETE
	//editlink
	//input:
	//       >>[data1] = url
	//       >>[data2] = password
	//       >>[data3] = adtype
	//       >>[data4] = title
	//       >>[data5] = code
	//output:
	//       >>status
	// Update vales in DB
	$passwordnew = md5($data3);
	$sqledit = mysql_query("UPDATE Links_All SET advtype='$data3', url='$data1', title='$data4', password='$passwordnew' WHERE code='$data5' AND username='$session_username'");
	// Link Successfully edited
	?>
	{"status":"1"}
	<?php

}elseif($command == 'deletelink') {//COMPLETE
	//deletelink
	//input:
	//       >>[data1] = code
	//output:
	//       >>status
	// Update vales in DB
	$delete = mysql_query("DELETE FROM Links_All WHERE code='$data1' AND username='$session_username'");
	// Link Successfully edited
	?>
	{"status":"1"}
	<?php

}elseif($command == 'linkinfo') {//COMPLETE
	//linkinfo
	//input:
	//       >>[data1] = code
	//output:
	//       >>status
	//       >>code
	//       >>shorturl
	//       >>longurl
	//       >>adtype
	//       >>title
	//       >>earned
	$link1 = mysql_query("SELECT * FROM Links_All WHERE code='$data1' AND username='$session_username'") or die('1');
	$link2 = mysql_fetch_array($link1);
	?>
		{"status":"1","code":"<?php echo $link2['code'];?>","shorturl":"<?php echo $mainurl;?><?php echo $link2['code'];?>","longurl":"<?php echo $link2['url']?>","adtype":"<?php echo $link2['advtype']?>","title":"<?php echo $link2['title']?>","earned":"<?php echo $link2['mearned']?>"}
	<?php

}elseif($command == 'usersreflink') {//COMPLETE
	//usersreflink
	//input:
	//       >>
	//output:
	//       >>status
	//       >>reflink
	?>
		{"status":"1","reflink":"<?php echo $mainurl;?>signup.<?php echo $session_username; ?>"}
	<?php

}elseif($command == 'createcampaign') {
	//createcampaign
	//input:
	//       >>[data1] = url
	//       >>[data2] = mediaurl
	//       >>[data3] = adtype
	//       >>[data4] = title
	//       >>[data5] = countries
	//       >>[data6] = active
	//output:
	//       >>status
	//       >>id

}elseif($command == 'editcampaign') {//HALF-COMPLETE / URL,IMAGEURL CONTROL / COUNTRY CONTROL / ACTIVE ONLY 01
	//editcampaign
	//input:
	//       >>[data1] = id
	//       >>[data2] = url
	//       >>[data3] = mediaurl
	//       >>[data4] = adtype
	//       >>[data5] = title
	//       >>[data6] = countries
	//       >>[data7] = active
	//output:
	//       >>status
	if($data4 == 'interstitial') {
		$adtype="interstitial";
	}elseif($data4 == 'video') {
		$adtype="video";
	}elseif($data4 == 'banner') {
		$adtype="banner";
	}else {
		die('{"status":"0","error":"Bad format of value [data4] > Ad type"}');
	}
	$sqledit = mysql_query("UPDATE Ads_All SET title='$data5', adtype='$adtype', url='$data2', imageurl='$data3', countries='$data6', active='$data7' WHERE id='$data1' AND owner='$session_username'");
	// Link Successfully edited
	if (!$sqledit) {
		?>
    		{"status":"0","error":"Campaign is not in our database"}
    	<?php
	}else{
		?>
			{"status":"1"}
		<?php
	}

}elseif($command == 'deletecampaign') {//COMPLETE
	//deletecampaign
	//input:
	//       >>[data1] = id
	//output:
	//       >>status
	// Update vales in DB
	$delete = mysql_query("DELETE FROM Ads_All WHERE id='$data1' AND owner='$session_username'");
	// Link Successfully edited
	if (!$delete) {
		?>
    		{"status":"0","error":"Campaign is not in our database"}
    	<?php
	}else{
		?>
			{"status":"1"}
		<?php
	}

}elseif($command == 'campaigninfo') {//COMPLETE
	//campaigninfo
	//input:
	//       >>[data1] = id
	//output:
	//       >>status
	//       >>id
	//       >>url
	//       >>mediaurl
	//       >>adtype
	//       >>title
	//       >>countries
	//       >>active
	//       >>approved
	$campaign1 = mysql_query("SELECT * FROM Ads_All WHERE id='$data1' AND owner='$session_username'") or die('1');
	$campaign2 = mysql_fetch_array($campaign1);
	?>
		{"status":"1","owner":"<?php echo $campaign2['owner'];?>","adtype":"<?php echo $campaign2['adtype'];?>","url":"<?php echo $campaign2['url']?>","mediaurl":"<?php echo $campaign2['imageurl']?>","credits":"<?php echo $campaign2['credits']?>","countries":"<?php echo $campaign2['countries']?>","active":"<?php echo $campaign2['active']?>","approved":"<?php echo $campaign2['approved']?>"}
	<?php

}elseif($command == 'userslinks') {//COMPLETE
	//userslinks
	//input:
	//       >>
	//output:
		//       >>status
		//:while
			//       >>code
			//       >>shorturl
			//       >>url
			//       >>adtype
			//       >>title
			//       >>earned

			$query = " SELECT * FROM Links_All WHERE username='$session_username'";
			$result = mysql_query($query) or die('Error, query failed');

			// print the random numbers
			
			if(mysql_num_rows($result)<1){
   		?>
			{
   			"status":"0",
    			"error":"User do not have any links",
			}
   <?php
   }else{
	$numberoflink = 0;
	?>
{
    "status":"1",
    "links": {
		<?php
     	while($row = mysql_fetch_array($result))
			{
				$numberoflink = $numberoflink + 1;
				if (empty($row['title'])) {
					$row['title'] = "---";
				};
				$urloflink = "$mainurl{$row['code']}";
				if($numberoflink>1) {echo ",";}
				?>
				"<?php echo $numberoflink; ?>": [
            	{"code":"<?php echo $row['code'];?>"},
            	{"shorturl":"<?php echo $row['url'];?>"},
            	{"url":"<?php echo $urloflink;?>"},
            	{"adtype":"<?php echo $row['advtype'];?>"},
            	{"title":"<?php echo $row['title'];?>"},
            	{"earned":"<?php echo $row['mearned'];?>"}
        		]
        		<?php
			}
		?>
		}
}
		<?php

       }

}elseif($command == 'userscampaigns') {//COMPLETE
	//userscampaigns
	//input:
	//       >>
	//output:
		//       >>status
		//:while
			//       >>id
			//       >>url
			//       >>mediaurl
			//       >>adtype
			//       >>title
			//       >>countries
			//       >>active
			//       >>approved
			
	$query = " SELECT * FROM Ads_All WHERE owner='$session_username'";
	$result = mysql_query($query) or die('Error, query failed');

			// print the random numbers
			
			if(mysql_num_rows($result)<1){
   		?>
			{
   			"status":"0",
    			"error":"User do not have any campaigns",
			}
   <?php
   }else{
	$numberoflink = 0;
	?>
{
    "status":"1",
    "campaigns": {
		<?php
     	while($row = mysql_fetch_array($result))
			{
				$numberoflink = $numberoflink + 1;
				if (empty($row['title'])) {
					$row['title'] = "---";
				};
				$urloflink = "$mainurl{$row['code']}";
				if($numberoflink>1) {echo ",";}
				?>
				"<?php echo $numberoflink; ?>": [
            	{"id":"<?php echo $row['id'];?>"},
            	{"title":"<?php echo $row['title'];?>"},
            	{"owner":"<?php echo $row['owner'];?>"},
            	{"adtype":"<?php echo $row['adtype'];?>"},
            	{"url":"<?php echo $row['url'];?>"},
            	{"mediaurl":"<?php echo $row['imageurl'];?>"},
            	{"credits":"<?php echo $row['credits'];?>"},
            	{"countries":"<?php echo $row['countries'];?>"},
            	{"active":"<?php echo $row['active'];?>"},
            	{"approved":"<?php echo $row['approved'];?>"}
        		]
        		<?php
			}
		?>
		}
}
		<?php

       }
	
}elseif($command == 'userstrafficweek') {//COMPLETE
	//userstrafficweek
	//input:
	//       >>
	//output:
	//       >>status
	//       >>data7
	//       >>data6
	//       >>data5
	//       >>data4
	//       >>data3
	//       >>data2
	//       >>data1
	?>
	{
    "status":"1",
    "traffic": {
        "publisher": [
            {"data7":"<?php echo $pub2['date7'];?>"},
            {"date6":"<?php echo $pub2['date6'];?>"},
            {"date5":"<?php echo $pub2['date5'];?>"},
            {"date4":"<?php echo $pub2['date4'];?>"},
            {"date3":"<?php echo $pub2['date3'];?>"},
            {"date2":"<?php echo $pub2['date2'];?>"},
            {"date1":"<?php echo $pub2['date1'];?>"}
        ],
        "advertiser": [
            {"data7":"<?php echo $adv2['date7'];?>"},
            {"date6":"<?php echo $adv2['date6'];?>"},
            {"date5":"<?php echo $adv2['date5'];?>"},
            {"date4":"<?php echo $adv2['date4'];?>"},
            {"date3":"<?php echo $adv2['date3'];?>"},
            {"date2":"<?php echo $adv2['date2'];?>"},
            {"date1":"<?php echo $adv2['date1'];?>"}
        ]
    }
}
	<?php

}elseif($command == 'usersbalances') {//COMPLETE
	//usersbalances
	//input:
	//       >>
	//output:
	//       >>status
	//       >>publisher
	//       >>advertiser
	?>
		{"status":"1","publisher":"<?php echo $pub2['balance'];?>","advertiser":"<?php echo $adv2['balance'];?>"}
	<?php

}elseif($command == 'userreflist') {
	//userreflist
	//input:
	//       >>
	//output:
		//       >>status
		//:while
			//       >>#
			//       >>username
			//       >>adtype
			//       >>dateregistered
			//       >>earned
			//       >>commission

}elseif($command == 'usersrefearnings') {//COMPLETE
	//usersrefearnings
	//input:
	//       >>
	//output:
	//       >>status
	//       >>earnings
	?>
		{"status":"1","refearnings":"<?php echo $pub2['refearnings'];?>"}
	<?php

}elseif($command == 'userswidget') {//COMPLETE
	//userswidget
	//input:
	//       >>
	//output:
	//       >>status
	//       >>publisher
	?>
		{"status":"1","widgetlink":"<?php echo $mainurl;?>widget.<?php echo $session_username; ?>"}
	<?php
}else {
	?>
	{"status":"commandnotexist"}
	<?php
}
}else {
	?>
	{"status":"badapikey"}
	<?php
}
}else {
	?>
	{"status":"badapikey"}
	<?php
}
}else {
	?>
	{"status":"commandnotset"}
	<?php
}
?>
