<?php
/***********************************************************************************/
/*                                                                                 */
/* XP-Weather version 1.3                                                          */
/* 8/4/2002 - davidd                                                               */
/* - added unlimited to visibility                                                 */
/* - added new unavailable status                                                  */
/* XP-Weather version 1.1                                                          */
/* XP-Weather version 1.0                                                          */
/* XP-Weather version 0.99                                                         */
/*         Modified again by davidd (dk@axarosenberg.com)                          */
/*         http://www.axarosenberg.com                                             */
/*																				   */
/* 6/18/2002 - davidd															   */
/*				- Includes cleanup                                                 */
/*				- Added adjustable caching to this module						   */
/*																				   */
/* 6/13/2002 -                                                                     */
/*				- added conText weather condition text	 						   */
/*				- re-worked table output										   */
/*				- moved embeded French out to language/main.php file			   */
/*                                                                                 */
/***********************************************************************************/
/*                                                                                 */
/* XP-Weather version 0.71b                                                        */
/*         Converted by Bidou (bidou@lespace.org                                   */
/*         http://www.lespace.org                                                  */
/*                                                                                 */
/***********************************************************************************/
/*                                                                                 */
/* Based on MyWeather version 1.0                                                  */
/*         PHP and mySQL Code changes by Richard Benfield aka PcProphet            */
/*         http://www.pc-prophet.com                                               */
/*         http://www.benfield.ws                                                  */
/*                                                                                 */
/*         Html and Graphic work by Chris Myden                                    */
/*         http://www.chrismyden.com/                                              */
/*                                                                                 */
/* MyWeather version 1.0 based on World_Weather version 1.1                        */
/*         By NukeTest.com team                                                    */
/*         http://www.nuketest.com                                                 */
/*                                                                                 */
/***********************************************************************************/
/*                                                                                 */
/* Previously part of PHP-NUKE Add-On 5.0 : Weather AddOn                          */
/* ======================================================                          */
/* Brought to you by PHPNuke Add-On Team                                           */
/* Copyright (c) 2001 by Richard Tirtadji AKA King Richard (rtirtadji@hotmail.com) */
/* http://www.nukeaddon.com                                                        */
/*                                                                                 */
/***********************************************************************************/
/*                                                                                 */
/* Original code based on METEO live v1.0                                          */
/* By Martin Bolduc at martin.bolduc@littera.com                                   */
/* License : Free, do what you want, but please, let my name in reference.         */
/*                                                                                 */
/***********************************************************************************/

function disp_block_XPWeather() {
	global $module_name, $bgifdir, $tps, $tpc, $cache_time, $xoopsUser, $xoopsConfig, $xoopsDB;
    global $weather_url, $response_maxlength, $proxy_host, $proxy_port, $proxy_user;

	//ヘッダー呼び出し部変更
	//include_once(XOOPS_ROOT_PATH."/modules/XP-Weather/header.php");
	include_once(XOOPS_ROOT_PATH."/modules/XP-Weather/include/config.php");
	include_once(XOOPS_ROOT_PATH."/modules/XP-Weather/include/functions.php");
	include_once(XOOPS_ROOT_PATH."/modules/XP-Weather/include/weather.class.php");

	// Include the appropriate language file.
	if(file_exists(XOOPS_ROOT_PATH.'/modules/XP-Weather/language/'.$xoopsConfig['language'].'/main.php')){
		include_once(XOOPS_ROOT_PATH.'/modules/XP-Weather/language/'.$xoopsConfig['language'].'/main.php');
	}else{
		include_once(XOOPS_ROOT_PATH.'/modules/XP-Weather/language/english/main.php');
	}
	//ここまで

	$language = $xoopsConfig['language'];

	$block = array();
	$block['title'] = _BLOCKTITLE;
	$block['content'] = "";
	$mywd = new WeatherData;

	if ( $xoopsUser ) {
	    $username = $xoopsUser->uname();
		list($userid) = mysql_fetch_row(mysql_query("select uid from ".$xoopsDB->prefix("users")." where uname='$username'"));
		list($accid, $wcid, $statype, $tpc, $tps) = mysql_fetch_row(mysql_query("select accid, wcid, station_type, tpc, tps from ".$xoopsDB->prefix("userweather")." where userid='$userid'"));
	}
	if (!isset($accid)) {
		list($accid, $wcid, $statype, $tpc, $tps) = mysql_fetch_row(mysql_query("select accid, wcid, station_type, tpc, tps from ".$xoopsDB->prefix("userweather")." where userid='0'"));
	}

	$cache_file = "".XOOPS_ROOT_PATH."/modules/XP-Weather/cache/block_$language.$accid";
	$time	=	split(" ", microtime());
	srand((double)microtime()*1000000);
	$cache_time_rnd	=	300 - rand(0, 600);
	if ( (!(file_exists($cache_file))) || (((filectime($cache_file) + $cache_time - $time[1]) + $cache_time_rnd < 0) || (!(filesize($cache_file))) && cache_time != -1) ) {
		if ( IsSet($wcid) && ($statype == "TWC" || $statype == "WMO" || $statype == "JPN") ) {
			$weather_url .="?acid=$wcid";
		} else {
            $weather_url .= "?acid=$accid";
        }
		$mywd->maxlength = $response_maxlength;

		if ( !empty($proxy_host) ) {
			$mywd->proxyHost = $proxy_host;
			if ( !empty($proxy_port) ) { $mywd->proxyPort = $proxy_port; }
			if ( !empty($proxy_user) ) { $mywd->User = $proxy_user; }
			if ( !empty($proxy_pwd) ) { $mywd->Pass = $proxy_pwd; }
		}

		$mywd->fetchData($weather_url);
		if ( IsSet($mywd->feedError) && !empty($mywd->feedError) ) {
			$failuremessage = _UNAVAIL.": $mywd->feedResponse ($mywd->feedError)\n";
		} else {
			$mywd->processData();
			$v_City    = $mywd->v_City;
			$v_SubDiv  = $mywd->v_SubDiv;
			$v_Country = $mywd->v_Country;
			$v_Region  = $mywd->v_Region;
			$v_Temp    = $mywd->v_Temp;
			$v_CIcon   = $mywd->getIcon($mywd->v_CIcon);
			$v_WindS   = $mywd->v_WindS;
			$v_WindD   = $mywd->v_WindD;
			$v_Baro    = $mywd->v_Baro;
			$v_Humid   = $mywd->v_Humid;
			$v_Real    = $mywd->v_Real;
			$v_UV      = $mywd->v_UV;
			$v_Vis     = $mywd->v_Vis;
			$v_LastUp  = $mywd->v_LastUp;
			$v_ConText = $mywd->v_ConText;
			$v_Fore    = explode("|", $mywd->v_Fore);
			$v_Acid    = $mywd->v_Acid;
			//JapanOnly
			if ( $statype == "JPN" ){
				list($city_name) = mysql_fetch_row(mysql_query("SELECT city_name from ".$xoopsDB->prefix("xpweather_city_jp")." where station_name = '$v_City'"));
				$v_City = $city_name;
			}

			if ($v_Temp == "" and $v_CIcon == "") {
				$failuremessage = "<center><a href=\"".XOOPS_URL."/modules/XP-Weather/change.php\">";
				$failuremessage .= "<small>"._CHNGSET."</small></A></center><br />\n";
				if ( $v_City != "" ) {
					$failuremessage .= "<center><b>$v_City</b></center>\n<center>"._UNAVAIL."</center>\n";
                } else {
					$failuremessage .= "<center><b>$v_City</b></center>\n<center>"._UNAVAIL."</center> \n<center>"._NODATA."</center>\n";
				}
			} else {
				if ($v_Temp == "" || $v_Temp == "0") {
					$v_Temp = ConvTemp($v_Fore[20],$tpc);
					$v_CIcon = $mywd->getIcon($v_Fore[10]);
					$v_WindS = "n/a";
					$v_WindD = "n/a";
					$v_Baro = "n/a";
					$v_Humid = "n/a";
					$v_Real = $v_Temp;
					$v_UV = "n/a";
					$v_Vis = "n/a";
				}
				$fpwrite = fopen($cache_file, 'w');
				if(!$fpwrite) {
					$failuremessage = _WRITEFAIL." ".$cache_file;
				} else {
					if ( $v_SubDiv != "" ) {
						$v_SubDiv = ", ".$v_SubDiv;
					}
					if ( $v_Vis == "" ) {
						$v_Vis = _UNLIMITED;
					}
					$block['content'] .= "<center><b>$v_City$v_SubDiv</b></center>\n";
					$block['content'] .= "<center><a href=\"".XOOPS_URL."/modules/XP-Weather/change.php\">";
					$block['content'] .= "<small>"._CHNGSET."</small></A></center>\n";
					$block['content'] .= "<center><table><tr><td align=\"center\">\n";
					$block['content'] .="<small><b>"._CURCOND."</b></small>\n";
					$block['content'] .="<table cellspacing=\"2\" cellpadding=\"2\">\n";
					$block['content'] .="<tr><td align=\"center\" valign=\"top\"><small>" .$v_ConText. "</small>\n";
					$block['content'] .="<br/><br/><small>" ._WIND. ":</small>\n";
					$block['content'] .="</td><td align=\"center\" valign=\"top\"><a href=\"".XOOPS_URL."/modules/XP-Weather/index.php\"><img src=\"".XOOPS_URL."/modules/XP-Weather/images/$bgifdir/".$v_CIcon."\" border=\"0\"></a>\n";
					$block['content'] .="</td><td align=\"center\" valign=\"top\"><small> " . ConvTemp($v_Temp, $tpc) ."</small>\n";
//					if ( strlen($v_ConText) > 6 ) {
//						$block['content'] .= "<br/>";
//					}
					$block['content'] .= "<br/><br/><small> $v_WindD </small>";
					if ($v_WindS > 0) {
						$block['content'] .= "<small>" . ConvSpeed($v_WindS, $tps) . "</small>";
					}
					$block['content'] .="\n</td></tr><tr><td align=\"center\" colspan=\"3\">\n";
					$block['content'] .="<small>" ._REFE. " " . ConvReal($v_Real, $tpc) . "</small>\n";
					$block['content'] .="</td></tr></table>\n";
					$block['content'] .="</td></tr><tr>\n";
					$block['content'] .="<td align=\"center\"><a href=\"".XOOPS_URL."/modules/XP-Weather/index.php\">" ._WDETAILED. "...</a>\n";
					$block['content'] .="</td></tr><tr><td align=\"center\"><small>" ._LASTUP. " " .$v_LastUp. "</small>\n";
					$block['content'] .="</td></tr></table>\n";
					$block['content'] .="</center>\n";
					fputs($fpwrite, $block['content']);
//					$block['content'] .="<small>Cache-Miss</small>";
				}
				fclose($fpwrite);
			}
		}
    } Else {
    	if (file_exists($cache_file)) {
			$wfread	= fopen($cache_file, 'r');
			if(!$wfread) {
				$failuremessage = _READFAIL ." ". $cache_file;
			} else {
				$block['content'] .= fread($wfread,filesize($cache_file));
				fclose($wfread);
//				$block['content'] .= "<small>From Cache</small>";
			}
    	}
    }
    if (isset($failuremessage)) {
		$block['content'] = $failuremessage;
    }
    return $block;
}
?>