<?php
/*
=====================
Модуль скачен с сайта:
Понедельников НЕТ! - Сайт альтернативной поддержки Slaed CMS! (http://www.ponedelnikov.net)
=====================
SHAKA1, 2009
e-mail: seregam007@mail.ru
=====================
*/
# include common lib and config file
include_once ("common.inc.php");


if (($fp = fopen(WEATHER_CURRENT_URL, "r")) !== false) {
	$file_content = fread($fp, 16384);
  $file_content = mb_convert_encoding($file_content, 'cp1251', 'UTF-8');

	# checking forecast version
	if (preg_match('/fullcurrent version="1.2"/', $file_content)) {

		while (!feof($fp)) {
			$file_content .= mb_convert_encoding(fread($fp, 16384), 'cp1251', 'UTF-8');

			# get all city forecast
			if (preg_match_all('@<current city="(\d+)">(.*?)</current>@is', $file_content, $current_found)) {

				foreach ($current_found[2] as $link_city_id => $inner_current_element) {
					$current = readXmlElements($inner_current_element, true);
					$current['city_id'] = $current_found[1][$link_city_id];

					$sql = sprintf("INSERT INTO `%s` (`city_id`, `date`, `cloud`, `t`, `t_flik`, `p`, `w`, `w_rumb`, `h`)
									VALUES (%d, '%s', %d, '%s', '%s', %d, %d, '%s', %d)
									ON DUPLICATE KEY UPDATE `date` = '%3\$s', `cloud` = '%4\$s', `t` = '%5\$s',
										`t_flik` = '%6\$s', `p` = '%7\$s', `w` = '%8\$s', `w_rumb` = '%9\$s',
										`h` = '%10\$d'",
							WEATHER_CURRENT_TABLE_NAME,
							$current['city_id'],
							$current['date'],
							$current['cloud'],
							$current['t'],
							$current['t_flik'],
							$current['p'],
							$current['w'],
							$current['w_rumb'],
							$current['h']
						);

					if (!mysql_ping()) {
						mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) OR DIE("Can't connect to a database server");
					}

					if (mysql_query($sql)) $added_count++;
					else die(mysql_error());

					$file_content = str_replace($current_found[0][$link_city_id], '', $file_content);
				}
			}
		}
		showMsg('Added '.$added_count.' current state of weather');
	}
	else showMsg('This is not 1.2 version of XML FullCurrent data');
}
else showMsg("Can't get XML FullCurrent data from '".WEATHER_FULL_FORECAST_URL."'");



?>