<?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");

$added_count = 0;
if (($fp = fopen(WEATHER_COUNTRY_URL, "r")) !== false) {
	$file_content = fread($fp, 4096);
  $file_content = mb_convert_encoding($file_content, 'cp1251', 'UTF-8');
	# checking version
	if (preg_match('/country version="1.2"/', $file_content)) {

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

			# get all country
			if (preg_match_all('/<country id="(\d+)">(.*?)<\/country>/is', $file_content, $found_element)) {

				foreach ($found_element[2] as $link_country_id => $inner_element_data) {
					$country = readXmlElements($inner_element_data, true);
					$country['id'] = $found_element[1][$link_country_id];
					$sql = sprintf("INSERT INTO `%s` (`id`, `iso2`, `iso3`, `name`) VALUES (%d, '%s', '%s', '%s')
									ON DUPLICATE KEY UPDATE `iso2` = '%3\$s', `iso3` = '%4\$s', `name` = '%5\$s'",
							WEATHER_COUNTRY_TABLE_NAME,
							$country['id'],
							$country['ISO2'],
							$country['ISO3'],
							$country['name']
						);

					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++;
						$file_content = str_replace($found_element[0][$link_country_id], '', $file_content);
					}
					else die(mysql_error());
				}
			}
		}
		showMsg('Added '.$added_count.' countries');
	}
	else showMsg('This is not 1.2 version of XML Country data');
}
else showMsg("Can't get XML Country data from '".WEATHER_FULL_FORECAST_URL."'");



# get city data
$added_count = 0;
if (($fp = fopen(WEATHER_CITY_URL, "r")) !== false) {
	$file_content = fread($fp, 4096);
  $file_content = mb_convert_encoding($file_content, 'cp1251', 'UTF-8');

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

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

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

				foreach ($found_element[2] as $link_city_id => $inner_element_data) {
					$city = readXmlElements($inner_element_data, true);
					$city['id'] = $found_element[1][$link_city_id];

					$sql = sprintf("INSERT INTO `%s` (`id`, `name`, `region`, `country`, `country_id`) VALUES (%d, '%s', '%s', '%s', %d)
									ON DUPLICATE KEY UPDATE `name` = '%3\$s', `region` = '%4\$s', `country` = '%5\$s', `country_id` = %6\$d",
							WEATHER_CITY_TABLE_NAME,
							$city['id'],
							$city['name'],
							$city['region'],
							$city['country'],
							$city['country_id']
						);

					if (mysql_query($sql)) {
						$added_count++;
						$file_content = str_replace($found_element[0][$link_city_id], '', $file_content);
					}
					else die(mysql_error());
				}
			}
		}
		showMsg('Added '.$added_count.' cities');
	}
	else showMsg('This is not 1.2 version of XML City data');
}
else showMsg("Can't get XML City data from '".WEATHER_FULL_FORECAST_URL."'");


?>