<?php
if (!defined('MODULE_FILE')) {
	Header("Location: ../../index.php");
	exit;
}
get_lang($conf['name']);
$pagetitle = "".$conf['defis']." "._WEAT."";
require_once("config/config_pogoda.php");
function readservis($link) {
$fd = fopen($link, "r");
$text="";
	if (!$fd) {
		return "Проблемка";
	} else {
		while (!feof ($fd)) {
			$text .= fgets($fd, 4096);
		}
	}
fclose ($fd);
return $text;
}
	$pagetitle = "$defis Погода";
	include("header.php");
	OpenTable();
	echo "<center><IMG border=0 SRC=\"modules/Pogoda/images/pogoda.jpg\"><br><font class='option'><b>в городе ".$city." на ближайшие дни</b></font></center>";
	CloseTable();
	OpenTable();
echo "<script language=\"JavaScript\" type=\"text/javascript\" src=\"javascripts/SwitchMenu.js\"></script>";
echo "<div id=\"cont\" onclick=\"SwitchMenu(&#039;news&#039;)\" style=\"cursor:pointer;\><span class=\"option\"><img src=\"images/arrow.gif\"><b>&nbsp;Заголовки новостей</b></span></div>";
echo "<div id=\"news\" style=\"display:none;\"><table width=\"100%\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\" class=\"bgcolor4\">"
."<tr><th>Последние новости</th><th>Популярные новости</th>"
."</tr><tr class=\"bgcolor1\" valign=\"top\" width=\"50%\">";
// Last News
echo "<td valign=\"top\" width=\"50%\" align=\"left\">";
$result = $db->sql_query("SELECT sid, time, title FROM ".$prefix."_stories WHERE time <= NOW() AND status='1' ORDER BY time DESC LIMIT $limit");
while(list($sid, $time, $title) = $db->sql_fetchrow($result)) {
	formatTimestamp($time);
	$linkstrip = cutstr($title, $strip);
	echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"3\" border=\"0\"><tr><td><img src=\"images/blocks/News.gif\" title=\"$title\"></td><td align=\"left\" width=\"100%\">$datetime <a href=\"index.php?name=News&op=view&id=$sid\" title=\"$title\">$linkstrip</a></td></tr></table>";
}
// Top News (по количеству просмотров)
echo "</td><td valign=\"top\" width=\"50%\" align=\"left\">";
$result = $db->sql_query("SELECT sid, time, title FROM ".$prefix."_stories WHERE time <= NOW() AND status='1' ORDER BY counter DESC LIMIT $limit");
while(list($sid, $time, $title) = $db->sql_fetchrow($result)) {
	formatTimestamp($time);
	$linkstrip = cutstr($title, $strip);
	echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"3\" border=\"0\"><tr><td><img src=\"images/blocks/News.gif\" title=\"$title\"></td><td align=\"left\" width=\"100%\">$datetime <a href=\"index.php?name=News&op=view&id=$sid\" title=\"$title\">$linkstrip</a></td></tr></table>";
}
echo "</td></tr></table></div>";
	CloseTable();
	OpenTable();
$kod = 4401;
$out = readservis("http://www.gismeteo.ru/city/daily/".$kod."/");
$out = iconv("UTF-8", "CP1251", $out);
preg_match_all('#<table summary=\"Карты погоды\">(.*?)</table>#si',$out,$tabl);
preg_match_all('#<tr(.*?)>(.*?)</tr>#si',$tabl[0][1],$tr);
$month=array("01" => "января", "02" => "февраля", "03" => "марта", "04" => "апреля", "05" => "мая", "06" => "июня", "07" => "июля", "08" => "августа", "09" => "сентября", 10 => "октября", 11 => "ноября", 12 => "декабря");
unset($tr[2][21]);unset($tr[2][22]);unset($tr[2][23]);unset($tr[2][24]);unset($tr[2][25]);unset($tr[2][26]);unset($tr[2][27]);

preg_match('#UTC\: (.*?) #', $tr[2][2], $tmp);
$tmp = explode('-', $tmp[1]);
$tr[2][0] = preg_replace ("#\.#", (intval($tmp[2]).' '.$month[$tmp[1]].' '.$tmp[0]), $tr[2][0]);

preg_match('#UTC\: (.*?) #', $tr[2][9], $tmp);
$tmp = explode('-', $tmp[1]);
$tr[2][7] = preg_replace ("#\.#", (intval($tmp[2]).' '.$month[$tmp[1]].' '.$tmp[0]), $tr[2][7]);

preg_match('#UTC\: (.*?) #', $tr[2][16], $tmp);
$tmp = explode('-', $tmp[1]);
$tr[2][14] = preg_replace ("#\.#", (intval($tmp[2]).' '.$month[$tmp[1]].' '.$tmp[0]), $tr[2][14]);

foreach($tr[2] as $key => $val) {
   $tr[2][$key] = preg_replace ("#img src=\"/images/ico/wind#", "img src=\"http://i.gismeteo.com/images/ico/wind", $tr[2][$key]);
   $tr[2][$key] = preg_replace ("#<!--(.*?)-->#", "", $tr[2][$key]);
}

echo "<table><tr>".(implode($tr[2], "</tr><tr>"))."</tr></table>";

/*
* Разработчикам: Используйте, что бы посмотреть массив
*/
/*
foreach($tr[2] as $key => $val) {
   echo $key.'<br>'.htmlspecialchars($tr[2][$key]).'<br><br><br><br>';
}
*/
	OpenTable();
	echo "Прогноз погоды предоставлен сервисом <a href=\"http://www.gismeteo.ru\">Gismeteo</a>.";
	CloseTable();
	CloseTable();
	include("footer.php");
?>