<?php
header("content-type:text/html;charset=GB2312");
//ob_start();
//写文件
function file_write_contents($file,$text){
  @chmod($file,0777);
  if($fp=@fopen($file,'wb+')){
    @fwrite($fp,$text);
    fclose($fp);
  }
}
//得到城市
function getCITY($ipstr){
  $str='';
  if($ipstr!=''){
    $str=preg_replace('/^.*(自治区|省)/','',$ipstr);
    $str=preg_replace('/^.*(台湾|广东|江苏|浙江|山东|福建|辽宁|黑龙江|河北|湖北|湖南|安徽|吉林|海南|四川|陕西|山西|河南|新疆|江西|广西|内蒙古|云南|甘肃|宁夏|青海|贵州|西藏)/','',$str);

	$str=preg_replace('/(蓟|开|忠|黟|歙|芜湖|肖|宿|泗|和|泾|寿|滁|沙|宁|环|漳|岷|文|康|成|徽|礼|梅|横|容|藤|盘|赵|承德|唐|易|蠡|雄|滦|涉|魏|磁|丘|任|威|蔚|青|献|霸|景|密|嵩|杞|郏|叶|温|孟|淇|浚|滑|范|睢|宾|房|南|攸|炎陵|衡阳|邵阳|通化|丰|沛|南昌|上饶|义|本溪|辽阳|莒|陵|费|冠|惠民|曹|单|大同|盂|沁|应|祁|代|吉|蒲|古|隰|临|岚|兴|户|富|耀|陇|眉|凤|华|郫|安|荣|珙|高|宜宾|理|茂|泸|云|鄞)县/','$1xian',$str);
	$str=preg_replace('/(固|玉门|西峰|清|景德|鹰潭|北|丰|天)镇/','$1zhen',$str);
	$str=preg_replace('/(北道)区/','$1qu',$str);
	$str=preg_replace('/(西)盟/','$1meng',$str);
	
    $str=preg_replace('/(.+)市.*$/','$1',$str);
	$str=preg_replace('/(.+)县.*$/','$1',$str);
	$str=preg_replace('/(.+)镇.*$/','$1',$str);
	$str=preg_replace('/(.+)区.*$/','$1',$str);
	$str=preg_replace('/(.+)盟.*$/','$1',$str);
	
	$str=str_replace('xian','县',$str);
	$str=str_replace('zhen','镇',$str);
	$str=str_replace('qu','区',$str);
	$str=str_replace('meng','盟',$str);
  }
  $str=($str!='' && $str!='本机地址')?$str:'北京';
  //setcookie('weathercity',$str,time()+30*24*60*60,'/');
  return $str;
}

//得到天气
function getWEATHER($city){
  global $tmp;
  $weatherurl='http://php.weather.sina.com.cn/search.php?c=1&city='.$city.'&dpc=1';
  if($con=file_get_contents($weatherurl)){
    preg_match('/<\!--\s*右侧主内容\s*begin\s*-->(.*)<\!--\s*右侧主内容\s*end\s*-->/isU',$con,$matches);
    unset($con,$weatherurl);
    if($matches[1]){
      $matches[1]=preg_replace('/<ul\s+class="list_01">.*<\/script>/isU','</div></div>',$matches[1]);
	  $matches[1]=preg_replace('/<ul\s+class="m_right">.*<\/ul>/isU','',$matches[1]);
	  $today=$matches[1];
    }
    unset($matches);

  }
  if($today){
    $weather=$today;
    file_write_contents($tmp,$weather);
    return $weather;
  }else{
    //setcookie('weathercity','',0);
    return '';
  }
}

$city=$_COOKIE['weathercity'];
echo $_COOKIE['weathercity'];
//echo "3333";
//setcookie('weathercity',"aaha",time()+30*24*60*60);
//echo "bd".$_COOKIE["weathercity"]."<BR>";
if($_REQUEST['city'] && $_REQUEST['city']!='选择城市' && $_REQUEST['city']!='本机地址'){
  if($_REQUEST['city']!=$city){
    $city=$_REQUEST['city'];
    //setcookie('weathercity',$_REQUEST['city'],time()+30*24*60*60);
	setcookie('weathercity',$city,time()+30*24*60*60,"/");
	//echo "b".$_COOKIE["weathercity"]."<BR>";
    //echo '设置成功！下次访问将显示 '.$city.' 天气预报——';
  }else{
    //die('无效设置！原因：城市没有改变——');
  }
}

//echo $_COOKIE['weathercity']."<BR>";


  if(!$city){
    require_once('getip.php');
    $myobj=new ipLocation();
    $ip=$myobj->getIP();
    $address=$myobj->getaddress($ip);
    $myobj=NULL;
    $city=getCITY($address["area1"]);
	echo "test";
  }
  $tmp=urlencode($city).'2.txt';
  $weather='';
  if(file_exists($tmp)){
    $tt1=date("Ymd",@filemtime($tmp));
    $tt2=date("Ymd");
    if(($wea=file_get_contents($tmp)) && ($tt1==$tt2)){
      $weather=$wea;
    }
  }
  if(!$weather){
    $weather=getWEATHER($city);
  }


//ob_end_flush();

function img_weather($arr){
	switch($arr){
	case 晴:
		return "images/0.png";	
	case 多云:
		return "images/1.png";	
	case 阴:
		return "images/2.png";		
	case 阵雨:
		return "images/3.png";
	case 雷阵雨:
		return "images/4.png";		
	case 小雨:
		return "images/7.png";	
	case 小到中雨:
		return "images/8.png";	
	case 中雨:
		return "images/8.png";	
	case 大雨:
		return "images/9.png";	
	case 小雪:
		return "images/13.png";	
	case 小到中雪:
		return "images/13.png";	
	case 雾:
		return "images/18.png";
	case 雨夹雪:
		return "images/6.png";
	case 中雪:
		return "images/15.png";
	case 大雪:
		return "images/16.png";	
	default:
		return "images/20.png";
}
}
//echo '当前默认城市：'.$city.'<br /><br /><div>'.preg_replace('/"/','\"',preg_replace('/[\n\r]+/','',$weather)).'';
//由于显示方式的原因，需要对$weather做进一步的处理
$myarr=explode("体感",$weather);
$myarr1=explode("未来4天",$myarr[1]);
preg_match_all("/\d+℃/",$myarr1[0],$today_wendu);
//echo $today_wendu[0][0].$today_wendu[0][1];//今天最高温度和最低温度
$myarr2=explode("<li>",$myarr1[0]);
$today_tianqi=split("</li>",$myarr2[1]);
$today_fengxiang=split("</li>",$myarr2[2]);
//echo $today_tianqi[0].$today_fengxiang[0];//今天天气和风向
echo img_weather($today_tianqi[0])."|txl|".$today_tianqi[0]."|txl|".$today_wendu[0][1]."～".$today_wendu[0][0]."|txl|".$today_fengxiang[0]."|txl|";

$myarr3=explode("明天",$weather,2);
$myarr4=explode("后天",$myarr3[1],2);
$myarr5=explode("<li>",$myarr4[0],2);
$myarr6=explode("</li>",$myarr5[1],2);
//echo "a".$myarr6[0]."a";//明天的天气
$myarr7=explode("<li>",$myarr5[1],3);
$myarr8=explode("</li>",$myarr7[1],2);
$myarr9=explode("</li>",$myarr7[2],2);
//echo "b".$myarr8[0]." ".$myarr9[0]."b";//明天的风向
preg_match_all("/\d+℃/",$myarr4[0],$tomorrow_wendu);
//echo "c".$tomorrow_wendu[0][0].$tomorrow_wendu[0][1]."c";//明天的温度
echo img_weather($myarr6[0])."|txl|".$myarr6[0]."|txl|".$tomorrow_wendu[0][1]."～".$tomorrow_wendu[0][0]."|txl|".$myarr8[0]." ".$myarr9[0]."|txl|";

$myarr3=explode("后天",$weather,2);
$myarr4=explode("mod_02",$myarr3[1],2);
$myarr5=explode("<li>",$myarr4[0],2);
$myarr6=explode("</li>",$myarr5[1],2);
//echo "d".$myarr6[0]."d";//明天的天气
$myarr7=explode("<li>",$myarr5[1],3);
$myarr8=explode("</li>",$myarr7[1],2);
$myarr9=explode("</li>",$myarr7[2],2);
//echo "e".$myarr8[0]." ".$myarr9[0]."e";//明天的风向
preg_match_all("/\d+℃/",$myarr4[0],$tomorrow_wendu);
//echo "f".$tomorrow_wendu[0][0].$tomorrow_wendu[0][1]."f";//明天的温度
echo img_weather($myarr6[0])."|txl|".$myarr6[0]."|txl|".$tomorrow_wendu[0][1]."～".$tomorrow_wendu[0][0]."|txl|".$myarr8[0]." ".$myarr9[0]."|txl|".$city;
//echo '当前默认城市：'.$city.'<br /><br /><div>'.$weather.'';

?>