<html>
<head>
<title>天气预报模块</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="CSS/style.css" rel="stylesheet">
</head>
<body>
<?php
//载入公共文件
require_once './common.php';

//调用省份，城市和代码的数组表文件
require_once S_ROOT.'./citycode.inc.php';
$country = '';				//初始化变量
$weather = array();			//定义数组

//如果传入城市编码参数，则根据该参数获取指定城市信息
if (isset($_GET['citycode']) && $_GET['citycode']!='' && strlen($_GET['citycode'])<6) {
	$city = getcitybycode($_GET['citycode'],$p,$c,$n);
}elseif(isset($_COOKIE['citycode']) && $_COOKIE['citycode']!='') {
	$city = getcitybycode($_COOKIE['citycode'],$p,$c,$n);
}
unset($country, $p, $c, $n);				//销毁指定的变量


$htmlarr = get_cma_html($city['0']);
if(!is_array($htmlarr) || empty($htmlarr)){
	die('读取中央气象局天气信息失败！');
}

//将读取的数据存储到XML中
$data_dir = DATAPATH.$city[3].'/';
$live_file = $data_dir.'live_'.$city['0'].'.xml';			//将读取的天气信息存储到XML文件中
$cma_file = $data_dir.'cma_'.$city['0'].'.xml';				//将读取的天气指数存储到XML文件中


if (!$cfg['cache']){
    //无缓存，每次更新
    $live_weather = get_live_weather($htmlarr);
    $cma_weather = get_cma_weather($htmlarr);
} else {
	mk_dir($data_dir);	
	if (!@file_exists($live_file)|| date("Ymd")>date("Ymd",filemtime($live_file)) || ($_SGLOBAL['timestamp']-filemtime($live_file))>$cfg['lw_cachetime']) {
		@unlink($live_file);
		//获取实况天气信息，并保存
		$live_weather = get_live_weather($htmlarr);
		save_weather_xml($live_weather, $live_file, 0);
	} else {
		$live_weather = read_weather_xml($live_file, 0);
	}
	if (!@file_exists($cma_file) || date("Ymd")>date("Ymd",filemtime($cma_file)) || ($_SGLOBAL['timestamp']-filemtime($cma_file))>$cfg['w_cachetime']) {
		@unlink($cma_file);
		//获取未来三日天气信息和指数查询，并保存
		$cma_weather = get_cma_weather($htmlarr);
		save_weather_xml($cma_weather, $cma_file, 1);
	} else {
		$cma_weather = read_weather_xml($cma_file, 1);
	}
}
unset($htmlarr);		//销毁指定的变量

foreach ($cma_weather['cma']['gif'] as $k => $v) {			//遍历天气数组元素
	list($gif[$k][0],$gif[$k][1]) = explode(',', $v);
}
?>
<table width="1004" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="258" height="144" rowspan="3" background="images/index_01.gif">&nbsp;</td>
    <td width="746" height="50" valign="top" background="images/index_02.gif">&nbsp;</td>
  </tr>
  <tr>
    <td width="746" height="34" valign="top" background="images/index_03.gif">&nbsp;</td>
  </tr>
  <tr>
    <td width="746" height="60" valign="top" background="images/index_05.gif">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2" valign="top">
	<?php 
	define("_TPLPath_", S_ROOT.'./templates/');			//定义模板路径
	include template('weather.htm');					//调用模板文件
	?>
</td>
  </tr>
  <tr>
    <td height="48" colspan="2" align="center" valign="middle" bgcolor="#FFFFFF">吉林省明日科技有限公司版权所有<BR>
    Copyright &copy; 2008 www.mrbccd.com,   WeatherOnline Asia Ltd. All Rights Reserved.</td>
  </tr>
  <tr>
    <td height="31" colspan="2" background="images/index_19.gif">&nbsp;</td>
  </tr>
</table>
</body>
</html>