<?php
require_once(dirname(__FILE__) . "/../../adbcon.php");
$category_uid = isset($_REQUEST['category_uid']) ? $_REQUEST['category_uid'] : '';
$catdom_id = isset($_REQUEST['domain_code']) ? intval($_REQUEST['domain_code']) : 0;

$status = 1;

if(strlen($category_uid) == 16 && $catdom_id > 0) {
	
	$q = "select * from domaincategories where catdom_id='$catdom_id';";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) == 1) {
		$row = mysql_fetch_assoc($r);
		$domain_id = $row['catdom_id'];
		$parent_id = $row['parent_id'];
		if($parent_id > 0) {
			$q = "select * from domaincategories where catdom_id='$parent_id';";
			$r = @mysql_query($q);
			if($r && mysql_num_rows($r) == 1) {
				$row = mysql_fetch_assoc($r);
				$domain_id = $row['catdom_id'];
			}
		}
		$domainname = str_replace("www.", "", $row['domainname']);
		$hosting_server = $row['hosting_server'];
	}
	
	
	$q = "select c.* from left_content c, module m where c.catdom_id='$catdom_id' and c.module_id=m.module_id and m.url='bl_middle' and c.`type`='module' and c.location='middle';";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) == 1) {
		$row = mysql_fetch_assoc($r);
		$catdom_module_id = $row['catdom_module_id'];
		$module_params = (trim($row['module_params']) != "") ?  unserialize(base64_decode($row['module_params'])) : array();
		
		if(isset($module_params['module_bl_categs']) && is_array($module_params['module_bl_categs'])) {
			$module_bl_categs = $module_params['module_bl_categs'];
			if(count($module_bl_categs) > 0) {
				$tmp = array();
				foreach ($module_bl_categs as $uid=>$title) {
					if($uid != $category_uid) {
						$tmp [$uid] = $title;
					}
				}
				$module_params['module_bl_categs'] = $tmp;
				
				$mp = base64_encode(serialize($module_params));
				$q = "update left_content set module_params='$mp' where catdom_module_id='$catdom_module_id';";
				$r = @mysql_query($q);
				if(!$r || mysql_affected_rows() == -1) {
					$status = 0;
				}
			}
		}
	}
	
	if($status == 1) {
		if(strpos($domainname, "/") === false) {
			if(str_replace("www.", "", $_SERVER['HTTP_HOST']) != $domainname && $hosting_server > 0) {
				$url = $protocol . "www.$domainname/amodules/bl_middle/remove_category.php?category_uid=$category_uid&domain_code=$catdom_id";
				$ch = curl_init($url);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				$ret = curl_exec($ch);
				curl_close($ch);
			}
		}
	}
	
	
}
echo $status;
?>