<?php
define('SITE_PATH',  dirname(__FILE__).DIRECTORY_SEPARATOR);
include(SITE_PATH . 'config.php');
$imgBytes       = "47494638396101000100800000FFFFFF00000021F90401000000002C00000000010001000002024401003B";
$queue_arr      = array();              // 队列存储数组
$url            = getget("url");        // 当前url
$permanent_id   = getget("perm_id");    // 先从QueryString获取permanent_id 如果不存在从Request.Cookies中获取
if (empty($permanent_id)) {
    $permanent_id = getcookie("__permanent_id");
}
$udid = getget("udid");
$_dangdang_com  = getcookie('dangdang_com'); // dangdang_com cookie中的值
parse_str($_dangdang_com, $dangdang_com);
$dangdang_com_customerid =  isset($dangdang_com['customerid']) ? $dangdang_com['customerid'] : '';
$refer_url = getget("refer_url");
$to_url = getget("to_url");
$platform = getget("platform");  //读取js生成的平台参数
$test = getget("test");

// 由于kafka没有字段概念，所以以下数组顺序不许更改
$queue_arr['machine']           = @$_SERVER['SERVER_ADDR'];                                             // 服务器IP
$queue_arr['ctr_id']            = getget("o_id");                                                       // 控件有ID或NAME ID优先
$queue_arr['region_ids']        = getget("region_ids");                                                 // 控件的父级关系
$queue_arr['url']               = !empty($url) ? custom_url(wash_url($url)) :'';                        // 当前url
$queue_arr['refer_url']         = !empty($refer_url) ? custom_url(wash_url($refer_url)) :'';            // refer url
$queue_arr['to_url']            = !empty($to_url) ? custom_url(wash_url($to_url)) :'';                  // 点击时候的url
$queue_arr['title']             = getget("title","");                                                   // 页面标题
$queue_arr['type']              = getget("type", "0");                                                  // 0,1,2,5
$queue_arr['http_user_agent']   = @$_SERVER['HTTP_USER_AGENT'];                                         // 浏览器类型
$queue_arr['time']              = date('Y-m-d H:i:s');                                                  // 当前时间
$queue_arr['visit_id']          = getcookie('__visit_id', '');                                          // 访次标识，一个__visit_id计一个访次
$queue_arr['is_first_visit']    = getget("is_first_visit");                                             // 是否为当前访次下的一个操作记录
$queue_arr['misc']              = getget("res");                                                        // 屏幕分辨率
$queue_arr['platform']          = $platform ? $platform : 'pc';                                         // 平台
$queue_arr['ref']               = getget("out_refer","");                                               // 流量来源（不存入数据库）
$domains                        = get_domain(custom_url($url),'www');                                               // 二三级域
$queue_arr['domain']            = $domains['2nd'];                                                      // 二级域
$queue_arr['permanent_id']      = empty($permanent_id) ? "0" : $permanent_id;                           // permanent_id
$queue_arr['udid']              = $udid;                                                                 // 获取app内传入的udid
$queue_arr['email']             = isset($dangdang_com['email']) ? base64_decode($dangdang_com['email']) : getcookie('email'); // 获取email
$queue_arr['cust_id']           = decode_cust_id($dangdang_com_customerid, $queue_arr['email']);        // 解析cust_id
$queue_arr['ip']                = getget('ip') ? getget('ip') : getip();                                // 获取IP
$queue_arr['trace_id']          = getget('trace_id');                                                   // 页面加载时间
$queue_arr['special']           = getget('special', '');                                                // 页面为ddclick收集特别部署的代码
$queue_arr['trace_id_new']      = genarate_unique_id();                                                 // 为实时推荐特别计算的数据（不存入数据库）
$queue_arr['click_info']        = convertParam(getget('cif', ''));                                      // 点击信息
$queue_arr['click_info']        = urlencode($queue_arr['click_info']);                                  // redis不能接受json再json_encode的数据，所以先编码
$queue_arr['reservation1']      = convertParam(getget('rsv1', ''));                                     // 预留字段1,点击控件的父级的dd_name属性，规则和region_ids相同
$queue_arr['reservation2']      = convertParam(getget('rsv2', ''));                                     // 预留字段2,点击控件的dd_name属性
$queue_arr['reservation3']      = convertParam(getget('rsv3', ''));                                     // 预留字段3,点击控件的父级的dd_area属性
$queue_arr['reservation4']      = '';                                                                   // 预留字段4, 新版m域域名
$queue_arr['reservation5']      = getcookie('__ddc_charset', '');                                       // 预留字段5  新版放置编码集合
$queue_arr['reservation6']      = getget('rsv6', '');                                                   // 预留字段6
$queue_arr['out_refer']         = convertCookie(getcookie('__out_refer'));                                // 站外来源Cookie
$queue_arr['ddc_1d']            = convertCookie(getcookie('__ddc_1d'));                                 // Last touch广告当天cookie
$queue_arr['ddc_24h']           = convertCookie(getcookie('__ddc_24h'));                                // Last touch广告24小时cookie
$queue_arr['ddc_15d']           = convertCookie(getcookie('__ddc_15d'));                                // First touch广告十五天cookie
$queue_arr['ddc_15d_f']         = convertCookie(getcookie('__ddc_15d_f'));                              // Last touch广告十五天cookie


//基调流量过滤
if (stripos($queue_arr['http_user_agent'], 'networkbench') !== false) {
    header("content-type:image/gif");
    echo hextobin($imgBytes);
    exit;
}

$kafka_root = PC_KAFKA_ROOT;
$topic = PC_KAFKA_TOPIC;
$port = PC_KAFKA_PORT;
$separator = PC_KAFKA_SEPARATOR;

//按平台来区分推送的topic
switch ($platform){
	case 'touch';
		$kafka_root = MOBILE_KAFKA_ROOT;
		$topic = MOBILE_KAFKA_TOPIC;
		$port = MOBILE_KAFKA_PORT;
		$queue_arr['platform'] = 'touch';
		
		//三级域名的URL转化
		if ($domains['3rd'] !== '') {
			$queue_arr['reservation4'] = $queue_arr['url'];
			$queue_arr['url'] = change_m_url($queue_arr['url'], $domains['3rd']);
		}
		break;
	case 'android';
		$kafka_root = MOBILE_KAFKA_ROOT;
		$topic = MOBILE_KAFKA_TOPIC_APPH5;
		$port = MOBILE_KAFKA_PORT;
		$queue_arr['platform'] = 'android';
		break;
	case 'iphone';
		$kafka_root = MOBILE_KAFKA_ROOT;
		$topic = MOBILE_KAFKA_TOPIC_APPH5;
		$port = MOBILE_KAFKA_PORT;
		$queue_arr['platform'] = 'iphone';
		break;
	case 'ipad';
		$kafka_root = MOBILE_KAFKA_ROOT;
		$topic = MOBILE_KAFKA_TOPIC_APPH5;
		$port = MOBILE_KAFKA_PORT;
		$queue_arr['platform'] = 'ipad';
		break;
	case 'miniprogram';
		$kafka_root = MOBILE_KAFKA_ROOT;
		$topic = MOBILE_KAFKA_TOPIC_APPH5;
		$port = MOBILE_KAFKA_PORT;
		$queue_arr['platform'] = 'miniprogram';
		break;
	case 'wechat';
		$kafka_root = MOBILE_KAFKA_ROOT;
		$topic = MOBILE_KAFKA_TOPIC;
		$port = MOBILE_KAFKA_PORT;
		$queue_arr['platform'] = 'wechat';
		break;
	//ebook|android、ebook|iphone
}

//微信平台的单独再次验证
if (strpos($queue_arr['http_user_agent'], 'MicroMessenger') !== false){
	$kafka_root = MOBILE_KAFKA_ROOT;
	$topic = MOBILE_KAFKA_TOPIC;
	$port = MOBILE_KAFKA_PORT;
	$queue_arr['platform'] = 'wechat';
}

//电子书平台特殊处理 
if($domains['2nd'] == 'e'){
	$kafka_root = PC_KAFKA_ROOT;
	$topic = PC_KAFKA_TOPIC;
	$port = PC_KAFKA_PORT;
}


// 判断是否是爬虫（spider、crawler、bot、seek）
if (stripos($queue_arr['http_user_agent'], 'spider') !== false || stripos($queue_arr['http_user_agent'], 'crawler') !== false || stripos($queue_arr['http_user_agent'], 'bot') !== false || stripos($queue_arr['http_user_agent'], 'seek') !== false ){
    $topic = SPIDER_KAFKA_TOPIC;
}


//过滤压力测试数据
if($test == 1){
    $topic = TEST_KAFKA_TOPIC;
}

//进kafka之前先恢复编码
$queue_arr['click_info'] = urldecode($queue_arr['click_info']);

//生产消息
$producer = new Kafka_Producer($kafka_root, $port, Kafka_Encoder::COMPRESSION_NONE);
$messages = implode($separator,$queue_arr);
$re_send  = 0;
$bytes	  = 0;
while(!$bytes){
	$bytes = $producer->send(array($messages), $topic);
	$re_send++;
	if($re_send > 3){
		break;
	}
}
$producer->close();

//写入本地文本数据
//Ddclick_Log::write_datalog($messages, false);

//回传img
header("content-type:image/gif");
echo hextobin($imgBytes);
?>
