<?php
set_time_limit(0);
mysql_connect('localhost','root','');
mysql_select_db('etw_service');
mysql_query('SET NAMES UTF8');

 date_default_timezone_set("Asia/Shanghai");

function sql_select($sql){
       $result=mysql_query($sql);
       while($row=mysql_fetch_row($result)){
       $output[]=$row;
        }
	   mysql_free_result($result);
	   return $output;
 }
 
  function  get_client_ip()    //IP
{  
                    global  $_SERVER;  
     if  (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))    
       {  
                $realip  =  $_SERVER["HTTP_X_FORWARDED_FOR"];  
       }  
     elseif  (isset($_SERVER["HTTP_CLIENT_IP"]))    
      {  
                $realip  =  $_SERVER["HTTP_CLIENT_IP"];  
       }  
     else    
      {  
               $realip  =  $_SERVER["REMOTE_ADDR"];  
      }  
      return  $realip;              
} 

function allchatid($uid){
        $allid=sql_select("select yid from etw_onlines where usernameid=".$uid." group by yid");
		for($i=0;$i<count($allid);$i++){
		    $all[$i]=$allid[$i][0];
		}
		return $all;
}





  function ipcountry($ip){
     $ipd=explode('.',$ip);
     $k=3;
     $ips=0;
      for($i=0;$i<count($ipd);$i++){
       $temp=(int)$ipd[$i]*pow(256,$k);
	   $ips=$ips+$temp;   
       $k=$k-1;
      }
	 $sql="select * from ip where startip<=".$ips." and endip>=".$ips;
	 $result=mysql_query($sql);
	 $rs=mysql_fetch_array($result);
	 if($rs['country']==''){
	  return $ip;
	 }else{
	 return $rs['country'];
	 }
  }
 
 function remote_file_exists($url_file){
//检测输入
$url_file = trim($url_file);
if (empty($url_file)) { return false; }
$url_arr = parse_url($url_file);
if (!is_array($url_arr) || empty($url_arr)){ return false; }

//获取请求数据
$host = $url_arr['host'];
$path = $url_arr['path'] ."?". $url_arr['query'];
$port = isset($url_arr['port']) ? $url_arr['port'] : "80";

//连接服务器
$fp = fsockopen($host, $port, $err_no, $err_str, 30);
if (!$fp){ return false; }

//构造请求协议
$request_str = "GET ".$path." HTTP/1.1\r\n";
$request_str .= "Host: ".$host."\r\n";
$request_str .= "Connection: Close\r\n\r\n";

//发送请求
fwrite($fp, $request_str);
$first_header = fgets($fp, 1024);
fclose($fp);

//判断文件是否存在
if (trim($first_header) == ""){ return false; }
if (!preg_match("/200/", $first_header)){
return false;
}
return true;
}

 ?>