<?php
if(isset($_REQUEST['advert_logos']) && is_array($_REQUEST['advert_logos']) && count($_REQUEST['advert_logos'])) {
	$is_admin = intval($_REQUEST['is_admin']) == 1; 
	$advert_logos = $_REQUEST['advert_logos'];
	$domain_id = intval($_REQUEST['domain_id']);
	$path = $protocol . "www.cypruswebsites.com/";
	$q = "select * from domaincategories where catdom_id='$domain_id' and parent_id='0';";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) == 1) {
		$row = mysql_fetch_assoc($r);
		$domain_id = intval($row['catdom_id']);
		$domainname = str_replace('www.', '', $row['domainname']);
		$addonname = trim($row['addonname']);
		$main_server = intval($row['hosting_server']) == 0;
		
	}
	else {
		die();
		$domain_id = 0;
	}
	
	$host = $_SERVER['HTTP_HOST'];
	if(str_replace('www.', '', $host) !== $domainname) {
		$host = $host . "/$addonname";
		$click_count_path = $_SERVER['HTTP_HOST'] . "/ageneral/";
	}
	else {
		$click_count_path = $_SERVER['HTTP_HOST'] . "/";
		if(!$main_server) {
			$path = $protocol . $host . "/";
		}
	}
	
	$click_count_path = $protocol . $click_count_path;
	
	
	$host = $protocol . $host . "/";
	
	$adverts = array();
	
	
	//print_r($advert_logos);
	
	foreach ($advert_logos as $i=>$bc) {
		$tmp = explode("_", $bc);
		$advertisment_id = intval($tmp[0]);
		$click_from = trim($tmp[1]);
		$catdom_id = intval($tmp[2]);
		$logo_link = '';
		if($advertisment_id > 0 && $click_from != "" && $domain_id > 0) {
		
			ob_start();
			
			$q = "select * from advertisment where advertisment_id='$advertisment_id';";
			$r = @mysql_query($q);
			if($r && mysql_num_rows($r) > 0) {
				while ($row = mysql_fetch_assoc($r)) {
					$advertisment_id = intval($row['advertisment_id']);
					$shortNameALT = str_replace('"','', stripslashes($row["short_title_en"]));
					
					if (!$is_admin && $domain_id > 0){
						mysql_query("UPDATE advertisment SET views=views+1 WHERE advertisment_id='$advertisment_id'") or die (mysql_error());
						count_views('adverts_stats_views', 'advertisment_id', $advertisment_id, $domain_id, $catdom_id, 'top');
					}
					
					$logo_link = '<a ';
					if($row['open_window']=="yes" && $row['website']!=""){
						$logo_link .= ' href="'. $row['website'] .'" ';
					}
					elseif($row['open_window']=="relative" && $row['website']!=""){
						$logo_link .= ' href="'. $row['website'] .'" ';
					}
					else {
						$logo_link .= ' href="click_count.php?link=' .$shortNameURL. '&amp;type=www&amp;id=' . $row['advertisment_id'] . '&amp;domain='.$domainname.'&amp;domain_id='.$domain_id.'&amp;click_from='.$click_from.'&amp;catdom_id='.$catdom_id.'" target="_blank" class="click-count"';
					}
					
					$logo_link .= ' title="' . $shortNameALT . ': Homepage">';
					
					
				}
			}
			
			$logo_content = ob_get_contents();
			ob_end_clean();
			
			if($logo_link != "") {
				$adverts [] = array("advert_code" => $bc, "advert_link" => $logo_link);
			}
			
		}
		
	}
	
	echo array2json($adverts);
	
}
?>