Example #1
0
             $end_date = $row["end_date"];
             $days = ceil((strtotime($now) - strtotime($start_date)) / (60 * 60 * 24));
             $y = intval(substr($start_date, 0, 4));
             $m = intval(substr($start_date, 5, 2));
             $d = intval(substr($start_date, 8, 2));
             $start_date = date('Y-m-d', mktime(0, 0, 0, $m, $d + $days, $y));
             $y = intval(substr($end_date, 0, 4));
             $m = intval(substr($end_date, 5, 2));
             $d = intval(substr($end_date, 8, 2));
             $end_date = date('Y-m-d', mktime(0, 0, 0, $m, $d + $days, $y));
             $sql = 'update tbl_user_status' . ' set start_date=' . correctSQL(substr($start_date, 0, 10)) . '   , end_date=' . correctSQL(substr($end_date, 0, 10)) . ' where userid=' . $uid . ' and delete_flag=0';
             executeSQL($conn, $sql);
         }
     }
     // set session data
     saveLoginInfo($userid, $macid);
 }
 // get serviceid, expiretime
 $row = getUserServiceInfo($conn, $uid);
 if ($row == null) {
     invalidUser();
 }
 if ($row["expiretime"] == null || $row["serviceid"] == null) {
     expiredUser($userid);
 }
 $expiretime = $row["expiretime"];
 $remainday = intval($row["remainday"]);
 $serviceid = $row["serviceid"];
 if ($expiretime != '0000-00-00' && $remainday <= 0) {
     expiredUser($userid);
 }
    function update_peer_info($peer_info)
    {
        // save history
        if ($peer_info['peer_type'] == NORMAL_PEER) {
            try {
                $this->begin_transaction();
                $macid = $peer_info['macid'];
                if (isset($peer_info['userid']) && strlen($peer_info['userid']) > 0) {
                    saveLoginInfo($peer_info['userid'], $peer_info['macid']);
                    $sql = "\n\t\t\t\t\t\tselect \n\t\t\t\t\t\t\t'" . addslashes($peer_info['uid']) . "' as uid,\n\t\t\t\t\t\t\t'" . addslashes($peer_info['userid']) . "' as userid,\n\t\t\t\t\t\t\tb.channelid\n\t\t\t\t\t\tfrom \n\t\t\t\t\t\t(SELECT id as channelid from tbl_channel where channel_hashid='" . addslashes($peer_info['channel_id']) . "' and delete_flag=0) b\n\t\t\t\t\t";
                } else {
                    if ($peer_info['peer_subtype'] == PEER_SUB_TYPE_ANDROID || $peer_info['peer_subtype'] == PEER_SUB_TYPE_ANDROIDBOX) {
                        $wan_ip = $_SERVER['REMOTE_ADDR'];
                        $lan_ip = $peer_info["lan_ip"];
                        saveLoginInfo($macid, $wan_ip . $lan_ip);
                    }
                    $sql = "\n\t\t\t\t\t\tselect a.uid, b.channelid, a.userid\n\t\t\t\t\t\tfrom \n\t\t\t\t\t\t(\n\t\t\t\t\t\t\tselect id as uid, userid from tbl_user where delete_flag=0 and status=1 and macid='" . addslashes($macid) . "' limit 1\n\t\t\t\t\t\t) a,\n\t\t\t\t\t\t(SELECT id as channelid from tbl_channel where channel_hashid='" . addslashes($peer_info['channel_id']) . "' and delete_flag=0) b\n\t\t\t\t\t";
                }
                $result = mysql_query($sql, $this->dbc);
                if (mysql_num_rows($result) > 0) {
                    if ($row = mysql_fetch_assoc($result)) {
                        $peer_info["userid"] = $row["userid"];
                        $uid = $row["uid"];
                        $channelid = $row["channelid"];
                        $sql = "\n\t\t\t\t\t\t\tupdate tbl_user_history a,\n\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\tselect id, channelid from tbl_user_history\n\t\t\t\t\t\t\t\twhere delete_flag=0 and userid='" . $uid . "' and timestampdiff(second, update_date, now()) < " . USER_LOGIN_TIMEOUT . "\n\t\t\t\t\t\t\t\torder by update_date desc limit 1\n\t\t\t\t\t\t\t) b\n\t\t\t\t\t\t\tset \n\t\t\t\t\t\t\ta.end_time=now(), \n\t\t\t\t\t\t\ta.update_date=now() \n\t\t\t\t\t\t\twhere a.id=b.id and b.channelid='" . $channelid . "'";
                        mysql_query($sql, $this->dbc);
                        if (mysql_affected_rows($this->dbc) == 0) {
                            $sql = "INSERT INTO tbl_user_history\n\t\t\t\t\t\t\t\t\tSET \n\t\t\t\t\t\t\t\t\t\tuserid    = '" . addslashes($uid) . "',\n\t\t\t\t\t\t\t\t\t\tchannelid  = '" . addslashes($channelid) . "',\n\t\t\t\t\t\t\t\t\t\tstart_time = now(),\n\t\t\t\t\t\t\t\t\t\tend_time = null,\n\t\t\t\t\t\t\t\t\t\ttype         = 1,\n\t\t\t\t\t\t\t\t\t\tcreate_id = 'tracker',\n\t\t\t\t\t\t\t\t\t\tcreate_date = now(),\n\t\t\t\t\t\t\t\t\t\tupdate_id = 'tracker',\n\t\t\t\t\t\t\t\t\t\tupdate_date = now();\n\t\t\t\t\t\t\t\t\t";
                            mysql_query($sql, $this->dbc);
                        }
                    }
                }
                $this->commit_transaction();
            } catch (Exception $e) {
                $this->rollback_transaction();
            }
        }
        // ----------- memcached (Peer Info) -------------
        $peer_info_id = 'peer_info_' . $peer_info['channel_id'] . '_' . $peer_info['peer_id'];
        // get old data
        $abi_speed = 0;
        $peer_info_data = $this->memcached->get($peer_info_id);
        if ($peer_info_data != null) {
            $abi_speed = ($peer_info['abi'] - $peer_info_data['abi']) / max(1, time() - $peer_info_data['last_report']);
        } else {
            // insert new data
            $peer_info_data = array();
            $peer_info_data["regdate"] = time();
        }
        $peer_info_data["peer_version"] = $peer_info['peer_version'];
        $peer_info_data["wan_ip"] = $peer_info['wan_ip'];
        $peer_info_data["wan_port"] = $peer_info['wan_port'];
        $peer_info_data["lan_ip"] = $peer_info['lan_ip'];
        $peer_info_data["lan_port"] = $peer_info['lan_port'];
        $peer_info_data["macid"] = $peer_info['macid'];
        $peer_info_data["type"] = $peer_info['peer_type'];
        $peer_info_data["subtype"] = $peer_info['peer_subtype'];
        $peer_info_data["abi_speed"] = $abi_speed;
        $peer_info_data["abi"] = $peer_info['abi'];
        $peer_info_data["download_rate"] = $peer_info['download_rate'];
        $peer_info_data["upload_rate"] = $peer_info['upload_rate'];
        $peer_info_data["last_report"] = time();
        if (array_key_exists('debug', $peer_info)) {
            $peer_info_data["debug"] = $peer_info['debug'];
        }
        $peer_info_data["userid"] = $peer_info['userid'];
        $this->memcached->set($peer_info_id, $peer_info_data, MEMCACHE_COMPRESSED, TRACKER_TIMEOUT);
        // ----------- memcached (debug) -----------------------------
        if ($peer_info['peer_type'] == NORMAL_PEER && array_key_exists('debug', $peer_info)) {
            $debug_info_id = 'debug_info';
            $debug_info_data = $this->memcached->get($debug_info_id);
            // delete old data
            if ($debug_info_data != null && time() - $debug_info_data["expire_time"] > TRACKER_TIMEOUT * 5) {
                $debug_info_data = null;
            }
            if ($debug_info_data == null) {
                // insert new data
                $debug_info_data = array();
                $debug_info_data["download_total"] = 0;
                $debug_info_data["download_p2p"] = 0;
                $debug_info_data["upload_total"] = 0;
                $debug_info_data["expire_time"] = time();
            }
            $arr = split("_", $peer_info['debug']);
            $debug_info_data["download_total"] += $arr[3] + $arr[4];
            $debug_info_data["download_p2p"] += $arr[4];
            $debug_info_data["upload_total"] += $arr[5];
            $this->memcached->set($debug_info_id, $debug_info_data, MEMCACHE_COMPRESSED, TRACKER_TIMEOUT);
        }
        // ----------- memcached (Peer List in channel) -------------
        $peer_list_id = 'peer_list_' . $peer_info['channel_id'];
        $peer_num_1 = 0;
        $peer_num_2 = 0;
        $peer_num_3 = 0;
        $peer_num_4 = 0;
        // get old data
        $peer_list_data = $this->memcached->get($peer_list_id);
        if ($peer_list_data != null) {
            $new_list_data = array();
            foreach ($peer_list_data as $key => $value) {
                // delete old peer
                if (time() - $value["last_report"] > TRACKER_TIMEOUT) {
                    continue;
                }
                $new_list_data[$key] = $value;
                if ($value['type'] == BROADCASTER_PEER) {
                    $peer_num_1++;
                } else {
                    if ($value['type'] == SUPER_PEER) {
                        $peer_num_2++;
                    } else {
                        if ($value['type'] == NORMAL_PEER) {
                            $peer_num_3++;
                        } else {
                            if ($value['type'] == BROADCASTER_SUPER_PEER) {
                                $peer_num_4++;
                            }
                        }
                    }
                }
                // get seeder, leecher list
                if ($peer_info['peer_type'] == BROADCASTER_PEER || $peer_info['peer_type'] == BROADCASTER_SUPER_PEER) {
                    continue;
                }
                if ($key == $peer_info['peer_id']) {
                    // self
                    continue;
                }
                $is_seeder = false;
                $is_leecher = false;
                if ($peer_info['peer_type'] == SUPER_PEER) {
                    if ($value['type'] == BROADCASTER_PEER || $value['type'] == BROADCASTER_SUPER_PEER) {
                        $is_seeder = true;
                    } else {
                        if ($value['type'] == SUPER_PEER && SP_FROM_ONLY_BROADCAST == 0) {
                            $is_leecher = true;
                        }
                    }
                } else {
                    if ($peer_info['peer_type'] == NORMAL_PEER) {
                        //Check Play Type
                        $peer_play_type = 1;
                        // 0:live, 1:vod
                        if ($peer_info["abi"] == 0 || $this->max_seeder_abi - LIVE_MAX_DELAY_TIME > $peer_info["abi"]) {
                            $peer_play_type = 0;
                        } else {
                            $peer_play_type = 1;
                        }
                        if ($value['subtype'] == PEER_SUB_TYPE_NONE) {
                            $value['subtype'] = PEER_SUB_TYPE_BCSP_LIVE;
                        }
                        if ($value['type'] == BROADCASTER_SUPER_PEER && $value['subtype'] == PEER_SUB_TYPE_BCSP_LIVE && $peer_play_type == 0 && FROM_ONLY_SUPERPEER == 0) {
                            $is_seeder = true;
                        } else {
                            if ($value['type'] == BROADCASTER_SUPER_PEER && $value['subtype'] == PEER_SUB_TYPE_BCSP_VOD && $peer_play_type == 1 && FROM_ONLY_SUPERPEER == 0) {
                                $is_seeder = true;
                            } else {
                                if ($value['type'] == SUPER_PEER && FROM_ONLY_BROADCAST_SUPERPEER == 0) {
                                    $is_seeder = true;
                                } else {
                                    if ($value['type'] == NORMAL_PEER && $value['lan_port'] != 0 && FROM_NORMALPEER == 1) {
                                        $is_leecher = true;
                                    }
                                }
                            }
                        }
                    }
                }
                if ($is_seeder || $is_leecher) {
                    $data = array();
                    $data['lan_ip'] = $value['lan_ip'];
                    $data['lan_port'] = $value['lan_port'];
                    $data['wan_ip'] = $value['wan_ip'];
                    $data['wan_port'] = $value['wan_port'];
                    $data['type'] = $value['type'];
                    $data['peer_hash_id'] = $key;
                    if ($is_seeder) {
                        $this->seeder_list[] = $data;
                    } else {
                        $this->leecher_list[] = $data;
                    }
                }
            }
            $peer_list_data = $new_list_data;
        } else {
            $peer_list_data = array();
        }
        // insert new data
        $peer_info_data = array();
        $peer_info_data['lan_ip'] = $peer_info['lan_ip'];
        $peer_info_data['lan_port'] = $peer_info['lan_port'];
        $peer_info_data['wan_ip'] = $peer_info['wan_ip'];
        $peer_info_data['wan_port'] = $peer_info['wan_port'];
        $peer_info_data["type"] = $peer_info['peer_type'];
        $peer_info_data["subtype"] = $peer_info['peer_subtype'];
        $peer_info_data["last_report"] = time();
        $peer_list_data[$peer_info['peer_id']] = $peer_info_data;
        $this->memcached->set($peer_list_id, $peer_list_data, MEMCACHE_COMPRESSED, TRACKER_TIMEOUT);
        // ----------- memcached (Channel Info) -------------
        $channel_info_id = 'channel_info_' . $peer_info['channel_id'];
        // get old data
        $channel_info_data = $this->memcached->get($channel_info_id);
        if ($channel_info_data != null) {
            $max_abi_id = 'max_abi_' . $peer_info['peer_type'];
            if ($channel_info_data[$max_abi_id] < $peer_info['abi'] && $peer_info['abi'] < 9000000000) {
                $channel_info_data[$max_abi_id] = $peer_info['abi'];
                $channel_info_data['max_abi_time_' . $peer_info['peer_type']] = time();
            }
        } else {
            $channel_info_data = array();
            $channel_info_data["max_abi_1"] = 0;
            $channel_info_data["max_abi_time_1"] = time();
            $channel_info_data["max_abi_2"] = 0;
            $channel_info_data["max_abi_time_2"] = time();
            $channel_info_data["max_abi_3"] = 0;
            $channel_info_data["max_abi_time_3"] = time();
            $channel_info_data["max_abi_4"] = 0;
            $channel_info_data["max_abi_time_4"] = time();
            $channel_info_data['max_abi_' . $peer_info['peer_type']] = $peer_info['abi'];
            $channel_info_data['max_abi_time_' . $peer_info['peer_type']] = time();
        }
        if (!isset($channel_info_data["grouptype"])) {
            $sql = 'SELECT grouptype, piecelength
						FROM tbl_channel
						WHERE channel_hashid = "' . addslashes($peer_info['channel_id']) . '" AND	delete_flag=0
						LIMIT 1';
            $result = mysql_query($sql, $this->dbc);
            if (mysql_num_rows($result) > 0) {
                $row = mysql_fetch_assoc($result);
                $channel_info_data["grouptype"] = $row['grouptype'];
                $channel_info_data["piecelength"] = $row['piecelength'];
            }
        }
        $channel_info_data["peer_num_1"] = $peer_num_1;
        $channel_info_data["peer_num_2"] = $peer_num_2;
        $channel_info_data["peer_num_3"] = $peer_num_3;
        $channel_info_data["peer_num_4"] = $peer_num_4;
        $this->memcached->set($channel_info_id, $channel_info_data, MEMCACHE_COMPRESSED, TRACKER_TIMEOUT);
        $this->group_type = $channel_info_data["grouptype"];
        // ----------- memcached (Server Info) -------------
        if ($peer_info['peer_type'] == BROADCASTER_PEER || $peer_info['peer_type'] == BROADCASTER_SUPER_PEER || $peer_info['peer_type'] == SUPER_PEER) {
            $server_info_id = 'server_info_' . $peer_info['wan_ip'];
            // get old data
            $server_info = $this->memcached->get($server_info_id);
            if ($server_info == null) {
                $server_info = array();
                $server_info["channels"] = array();
            }
            if (!isset($server_info["bandwidth"])) {
                $sql = "SELECT bandwidth\n\t\t\t\t\t\t\tFROM tbl_server\n\t\t\t\t\t\t\tWHERE ip='" . addslashes($peer_info['ip']) . "' AND\tdelete_flag=0\n\t\t\t\t\t\t\tLIMIT 1";
                $result = mysql_query($sql, $this->dbc);
                if (mysql_num_rows($result) > 0) {
                    $row = mysql_fetch_assoc($result);
                    $server_info["bandwidth"] = $row['bandwidth'];
                }
            }
            $peerslist =& $server_info["channels"][$peer_info['channel_id']];
            if ($peerslist == null) {
                $server_info["channels"][$peer_info['channel_id']] = array();
                $peerslist =& $server_info["channels"][$peer_info['channel_id']];
            }
            $peerslist[$peer_info['peer_id']] = $peer_info['peer_id'];
            $this->memcached->set($server_info_id, $server_info, MEMCACHE_COMPRESSED, TRACKER_TIMEOUT);
        }
        // ----------- memcached (Channel List) -------------
        // get old data
        $channel_list = $this->memcached->get('channel_list');
        if ($channel_list == null) {
            $channel_list = array();
        }
        $channel_list[$peer_info['channel_id']] = 1;
        $this->memcached->set('channel_list', $channel_list, MEMCACHE_COMPRESSED, TRACKER_TIMEOUT);
        // max seeder abi
        if ($channel_info_data["grouptype"] == 2) {
            // FILE
            $this->max_seeder_abi = max($channel_info_data["max_abi_1"], $channel_info_data["max_abi_4"]);
        } else {
            $this->max_seeder_abi = max($channel_info_data["max_abi_1"] + floor((time() - $channel_info_data["max_abi_time_1"]) * 1000 / $channel_info_data["piecelength"]), $channel_info_data["max_abi_4"] + floor((time() - $channel_info_data["max_abi_time_4"]) * 1000 / $channel_info_data["piecelength"]));
        }
    }