<?php
@ ob_start();
@ require 'writable/set/set.php';
@ include 'writable/set/set_getweather_step.php';

header('Cache-Control:no-cache,must-revalidate');  
header('Pragma:no-cache');

$system_weather_from = $web['weather_from'];
$web['weather_from'] = (!empty($_COOKIE['weatherfrom']) && is_dir('readonly/weather/'.$_COOKIE['weatherfrom'])) ? $_COOKIE['weatherfrom'] : $web['weather_from'];
if (!function_exists('get_en_url')) {
  function get_en_url($d) {
    return urlencode(base64_encode($d));
  }
}





if (!empty($_GET['err'])) {
  header("content-type: text/html; charset=gb2312");
  echo '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>解决天气预报错误</title>
<style>
body { text-align:center; }
form { margin:0; }
#err { background-color:#FFFFFF; width:600px; margin:auto; text-align:left; }
</style>
<script>
function setCookie(name,value,time){
  var timePos = 8;
  var cookieEnabled=(typeof navigator.cookieEnabled!="undefined" && navigator.cookieEnabled)?true:false;
  if(cookieEnabled == false){
    alert("您的浏览器未开通cookie，程序无法正常使用！");
    return false;
  }
  if(!time||isNaN(time))time=365; //此 cookie 将被保存 365 天
  var cookieStr=encodeURIComponent(value);
  var expireStr=" expires="+(new Date(new Date().getTime()+(time*24*60*60+timePos*3600)*1000).toGMTString())+";";
  document.cookie=name+"="+cookieStr+";"+expireStr+" path=/;";
}
</script>
</head>
<body>

<p>天气预报出错啦！</p>
<iframe id="reloadcity" src="p.php?xml_id=weather&xml_file='.get_en_url('readonly/weather/getweather.php').'&char=utf-8" style="display:none;"></iframe>
<ul id="err">
  <li>去源天气站看一下是不是出问题了哟：<a href="'.$_GET['err'].'" target="_blank" style="color:blue;">去检测</a></li>
  <li>';
?>
<form id="wform" action="weather.php" method="post" target="_self">
<input id="weather_fr" name="weather_fr" type="hidden">

<div style="position:relative;">
            <div style="cursor:pointer"> [ <a href="javascript:void(0)" id="optionName" onclick="document.getElementById('optionMenu').style.display=''; return false;">切换天气频道</a><span class="mainmore" onmouseover="document.getElementById('optionMenu').style.display='';">&raquo;</span> ] （应该可以解决）</div>
            <ul id="optionMenu" onmouseover="this.style.display='';" onmouseout="this.style.display='none';" style="position:absolute; top:20px; left:0; z-index:99; padding:5px 10px; background-color:#FFFFFF; border:1px #D8D8D8 solid; display:none;">
<?php
if ($w_f = @glob('readonly/weather/*', GLOB_ONLYDIR)) {
  foreach($w_f as $w_type) {
    $w_type = basename($w_type);
    $w_set = $system_weather_from==$w_type ? '（系统默认）':'';
    if ($web['weather_from'] == $w_type) {
      echo '
<li><span>当前天气源：'.$w_type.''.$w_set.'</span></li>';
    } else {
      echo '
<li><a href="javascript:void(0)" onclick="document.getElementById(\'weather_fr\').value=\''.$w_type.'\'; document.getElementById(\'wform\').submit(); return false;">切换到：'.$w_type.''.$w_set.'</a></li>';
    }
    unset($w_type, $wfr_title);
  }
}
unset($w_f, $w_type);
?>
            </ul>
          </div>
</form>


<?php
  echo '</li>
  <li><a href="javascript:void(0)" onclick="setCookie(\'weathercity\',\'\',-366);setCookie(\'weathercity2\',\'\',-366);setCookie(\'weatherfrom\',\'\',-366);document.getElementById(\'reloadcity\').contentWindow.location.reload(true);alert(\'清除成功。请重启浏览器并刷新首页观察\');return false;" style="color:blue;">清除Cookie</a></li>
</ul>
</body>
</html>
';
  die;
}

$NOWCITY = $_COOKIE['weathercity'];

if (!empty($_POST['weather_fr']) && is_dir('readonly/weather/'.$_POST['weather_fr'])) {
  $_COOKIE['weatherfrom'] = '';
  @ setcookie('weatherfrom', '', -(time() + 365 * 24 * 60 * 60), '/'); //+8*3600
  $_COOKIE['weathercity2'] = '';
  @ setcookie('weathercity2', '', -(time() + 365 * 24 * 60 * 60), '/'); //+8*3600
  if ($_POST['weather_fr'] != $web['weather_from']) {
    $_COOKIE['weatherfrom'] = $_POST['weather_fr'];
    @ setcookie('weatherfrom', $_COOKIE['weatherfrom'], time() + 365 * 24 * 60 * 60, '/'); //+8*3600
    if (isset($_COOKIE['weathercity']) && !empty($_COOKIE['weathercity'])) {
      preg_match('/header\(.+charset\=([\w\-]+)/i', @file_get_contents('readonly/weather/'.$_COOKIE['weatherfrom'].'/weather.php'), $m);
      $cha_to = !empty($m[1]) ? $m[1] : '';
      unset($m);
      if (function_exists('mb_detect_encoding')) {
        $cha = @mb_detect_encoding($_COOKIE['weathercity'], array('UTF-8','ASCII','EUC-CN','CP936','BIG-5','GB2312','GBK'));
      } else {
        preg_match('/header\(.+charset\=([\w\-]+)/i', @file_get_contents('readonly/weather/'.$web['weather_from'].'/weather.php'), $m);
        $cha = !empty($m[1]) ? $m[1] : '';
        unset($m);
      }
      if ($cha != $cha_to) {
        if (function_exists('iconv')) {
          $_COOKIE['weathercity'] = @iconv($cha, $cha_to, $_COOKIE['weathercity']);
          if (preg_match('/\''.preg_quote($_COOKIE['weathercity'], '/').'\|/', @file_get_contents('readonly/weather/'.$_COOKIE['weatherfrom'].'/getweather_seek.php'))) {
            @ setcookie('weathercity', $_COOKIE['weathercity'], time() + 365 * 24 * 60 * 60, '/'); //+8*3600
          } else {
            $_COOKIE['weathercity'] = '';
            @ setcookie('weathercity', '', -(time() + 365 * 24 * 60 * 60), '/'); //+8*3600
          }
        } else {
          $_COOKIE['weathercity'] = '';
          @ setcookie('weathercity', '', -(time() + 365 * 24 * 60 * 60), '/'); //+8*3600
        }
      }
    }
    $web['weather_from'] = $_POST['weather_fr'];
    $NOWCITY_ = 1;
  }
  //header('Location: weather.php');
  //die;
}





if (empty($_GET['area'])) {
  if (!$title_city = $_GET['city']) {
    $title_city = $_COOKIE['weathercity'];
  } else {
    $title_city = $_GET['city'];
  }

} else {
  if ($_GET['area'] == 'china') {
    if (empty($_GET['province'])) {
      $title_city = iconv("gbk", "utf-8", '国内');
    } else {
      $title_city = $_GET['province'];
    }
  } elseif ($_GET['area'] == 'international') {
    if ($_GET['country']) {
      $title_city = $_GET['country'];
    } elseif ($_GET['continent'])  {
      $title_city = $_GET['continent'];
    } else {
      $title_city = iconv("gbk", "utf-8", '国际');
    }
  }
}

@ include 'readonly/weather/'.$web['weather_from'].'/weather.php';

echo '
<script type="text/javascript" language="javaScript">
  function relaodparentH() {
    if (par != null) {
      '.$rel1.'
      if (typeof bodyW !== "undefined") {
        par.style.width=\'\'+bodyW+\'px\';
      }
      if (typeof bodyH !== "undefined") {
        par.style.height=\'\'+bodyH+\'px\';
      } else {
        var bodyH = parseInt(document.body.offsetHeight);
        par.style.height = par.name = bodyH+"px";
      }
    }
  }
  addEvent(window, "load", relaodparentH);
</script>
';

//删除失效的中国天气网
if (file_exists('readonly/weather/www.nmc.gov.cn')) {
  require 'readonly/function/deldir.php';
  deldir('readonly/weather/www.nmc.gov.cn');
}
//删除失效的旧新浪天气网
if (file_exists('readonly/weather/weather.news.sina.com.cn')) {
  require 'readonly/function/deldir.php';
  deldir('readonly/weather/weather.news.sina.com.cn');
}
//删除过期缓存文件 9.3.5加
$date0 = gmdate('Ymd', filemtime('writable/__temp__/weather/0.php'));
$datet = gmdate('Ymd', time() + floatval($web['time_pos']) * 3600);
if ($datet != $date0) {
  if ($arr_d = @glob('writable/__temp__/weather/*', GLOB_ONLYDIR)) {
    foreach ($arr_d as $v_d){
      if ($arr_f = @glob($v_d.'/*.txt')) {
        foreach ($arr_f as $v_f){
          if (gmdate('Ymd', filemtime($v_f) + floatval($web['time_pos']) * 3600) != $datet){
	        @unlink($v_f);
	      }
        }
      }
    }
  }
  require 'readonly/function/write_file.php';
  write_file('writable/__temp__/weather/0.php', $datet);
}



@ ob_end_flush();
?>