Exemple #1
0
function handle_heartbeat_cmd($PARAMS)
{
    /******************************************
    	  例外处理,一次心跳完成账户绑定,关键字搜集等
    	******************************************/
    //支持微信等不支持cookies的客户端,一次请求完成心跳和账户绑定
    if (isset($PARAMS['plat'])) {
        handle_bind_account($PARAMS);
    }
    if (count($cmd_que = msg_queue())) {
        foreach ($cmd_que as $cmd_item) {
            if (empty($cmd_item)) {
                continue;
            }
            switch ($cmd_item['cmd']) {
                case 'bind':
                    handle_bind_account($cmd_item);
                    break;
                case 'kword':
                    handle_bind_keyword($cmd_item);
                    break;
                default:
                    omp_trace('error cookie cmd: ' . json_encode($cmd_item));
            }
        }
    }
    /******************************************
    	  组织一个更新的心跳包
    	******************************************/
    $url_obj = parse_url(@$_SERVER['HTTP_REFERER']);
    omp_trace('heartbeat: ' . @$url_obj['host'] . @$url_obj['path']);
    //omp_trace('ua: '.@$_SERVER['HTTP_USER_AGENT']);
    list($is_new, $device, $device_saved) = get_cookie_saved();
    $device_param = @$PARAMS['device'];
    if ($device_param) {
        if ($device_param !== $device) {
            $device = $device_param;
        }
    }
    $result = array('device' => $device);
    omp_trace('device: ' . $device);
    $browser_save = array();
    $browser_save['device'] = $device;
    $browser_save['ip_addr'] = $_SERVER['REMOTE_ADDR'];
    $browser_save['new_user'] = $is_new;
    $browser_save['ismobiledevice'] = $device_saved[0];
    $browser_save['browser'] = $device_saved[1];
    $browser_save['platform'] = $device_saved[2];
    $browser_save['device_name'] = $device_saved[3];
    $browser_save['region'] = get_region_city();
    $browser_save['language'] = get_accept_language();
    $browser_save['Visiting'] = @$_SERVER['HTTP_REFERER'];
    $browser_save['UserAgent'] = @$_SERVER['HTTP_USER_AGENT'];
    $browser_save['XRequestWith'] = @$_SERVER['HTTP_X_REQUESTED_WITH'];
    /******************************************
    	  检查看看该账户有没有绑定信息
    	  客户端凭此判断是否提交bind账户操作
    	******************************************/
    $mem = api_open_mmc();
    $browser_save['binded'] = false;
    if ($binded_list = $mem->ns_get(NS_BINDED_LIST, $device)) {
        $browser_save['binded'] = true;
        //获取保存的账户信息
        if ($bind_account = $mem->ns_get(NS_BINDED_CAPTION, $device)) {
            $browser_save['bind_account'] = implode(';', $bind_account);
            $result['binded'] = $binded_list;
        }
    }
    omp_trace('get account: ' . @$browser_save['bind_account']);
    /******************************************
    	  更新心跳,维护在线列表
    	******************************************/
    $list_stat = mmc_array_set(NS_DEVICE_LIST, $device, $browser_save, CACHE_EXPIRE_SECONDS);
    if ($list_stat > 0) {
        if ($list_stat === 1) {
            require_once 'sched_list.php';
            update_sched_tasks();
        }
        $browser_save['new_visitor'] = true;
    } else {
        $browser_save['new_visitor'] = false;
    }
    omp_trace('update heartbeat');
    /******************************************
    	  如果不需要返回消息,就此打住
    	******************************************/
    $bypass_msg = isset($PARAMS['nomsg']) ? $PARAMS['nomsg'] === 'true' : false;
    if ($bypass_msg) {
        if ($is_debug = is_debug_client()) {
            $result['trace'] = omp_trace(null);
        }
        $events = array();
        $events['debug'] = $is_debug;
        $events['first_session'] = $browser_save['new_user'];
        $events['first_visit'] = $browser_save['new_visitor'];
        $result['events'] = $events;
        return jsonp($result);
    }
    /******************************************
    	  获取计划任务消息 
    	******************************************/
    if ($items_result = get_popup_messages($browser_save, $session_pv, $global_pv)) {
        $result['sched_msg'] = $items_result;
        $browser_save['sched_msg'] = base64_encode(json_encode($items_result));
    }
    omp_trace('sched messages');
    /******************************************
    	  获取替换任务消息 
    	******************************************/
    if ($replace_items = get_replace_messages($browser_save, $PARAMS)) {
        $result['replace_msg'] = $replace_items;
        $browser_save['replace_msg'] = base64_encode(json_encode($replace_items));
    }
    omp_trace('replace messages');
    /*************************************
    	  获取异步消息
    	*************************************/
    if ($cmdbox_list = $mem->ns_get(NS_HEARTBEAT_MESSAGE, $device)) {
        $cmdbox = array_shift($cmdbox_list);
        $result['async_msg'] = $cmdbox;
        $browser_save['async_msg'] = base64_encode(json_encode($cmdbox));
        if (count($cmdbox_list) == 0) {
            $mem->ns_delete(NS_HEARTBEAT_MESSAGE, $device);
        } else {
            $mem->ns_set(NS_HEARTBEAT_MESSAGE, $device, $cmdbox_list, CACHE_EXPIRE_SECONDS);
        }
    }
    omp_trace('async messages');
    /*************************************
    		输出结果
    	*************************************/
    call_notifier($browser_save);
    omp_trace('call notifier');
    if ($is_debug = is_debug_client()) {
        $result['trace'] = omp_trace(null);
    }
    $events = array();
    $events['debug'] = $is_debug;
    $events['first_session'] = $browser_save['new_user'];
    $events['first_visit'] = $browser_save['new_visitor'];
    $events['gw_bind_time'] = time();
    $events['gw_kword_time'] = time();
    $events['session_pv'] = $session_pv;
    $events['global_pv'] = $global_pv;
    $result['events'] = $events;
    return jsonp($result);
}
Exemple #2
0
function update_device_saved($device_saved)
{
    //更新统计信息
    if (time() - $device_saved['last_pageview'] > SESSION_INTERVAL_TIME) {
        $device_saved['start_session'] = time();
        $device_saved['session_count'] += 1;
        $device_saved['session_pv'] = 0;
    }
    $device_saved['pageview_count'] += 1;
    $device_saved['session_pv'] += 1;
    $device_saved['is_first_pageview'] = $device_saved['pageview_count'] < 2;
    $device_saved['is_first_session'] = $device_saved['session_count'] < 2;
    $device_saved['is_first_day'] = time() - $device_saved['first_pageview'] < COOKIE_TIMEOUT_NEW;
    $device_saved['is_first_session_pv'] = $device_saved['session_pv'] < 2;
    $device_saved['staytime'] = time() - $device_saved['start_session'];
    $device_saved['is_registed'] = !empty($device_saved['kword_account_md5']);
    //获取需要附加的信息
    //这些都是会动态改变的字段
    $ip_addr = @$_SERVER['REMOTE_ADDR'];
    $region = get_locale_mem($ip_addr);
    $lang = get_accept_language();
    $ip_changed = isset($device_saved['ip_addr']) ? $device_saved['ip_addr'] != $ip_addr : false;
    $region_changed = isset($device_saved['region']) ? $device_saved['region'] != $region : false;
    $lang_changed = isset($device_saved['language']) ? $device_saved['language'] != $lang : false;
    $device_saved['ip_addr'] = $ip_addr;
    $device_saved['region'] = $region;
    $device_saved['language'] = $lang;
    $device_saved['Visiting'] = @$_SERVER['HTTP_REFERER'];
    $device_saved['XRequestWith'] = @$_SERVER['HTTP_X_REQUESTED_WITH'];
    $device_saved['ip_changed'] = $ip_changed;
    $device_saved['region_changed'] = $region_changed;
    $device_saved['lang_changed'] = $lang_changed;
    return $device_saved;
}