<?php

//By FlySelf

define('_IN_JOHNCMS', 1);

$headmod = 'weather';
$textl = 'Погода';
require_once("../incfiles/core.php");
require_once('../incfiles/head.php');

$time_file = @ filemtime('cache/clean.dat');
if ($realtime > ($time_file + 3600*24)) {
	$dir_clean = opendir('cache');
	while ($file = readdir($dir_clean)) {
		if ($file != '.' && $file != '..'&& $file != '.htaccess') {
			@ unlink ('cache/' . $file);
		}
	}
	closedir($dir_clean);
	@ file_put_contents('cache/clean.dat', 1);
}

function contents($url) {
	global $home;
	$curl = curl_init($url);
	curl_setopt($curl, CURLOPT_TIMEOUT, 10);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: text/html, application/xml, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;', 'Accept-Language: ru, en, *;', 'Accept-Charset: iso-8859-1, utf-8, utf-16, *;', 'Accept-Encoding: identity;', 'Connection: close'));
	curl_setopt($curl, CURLOPT_USERAGENT, 'Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.6.22 Version/10.50');
	curl_setopt($curl, CURLOPT_REFERER, $home);
	$contents = curl_exec($curl);
	curl_close();
	return $contents;
}

$file_cahe = 'cache/' . md5($_SERVER['REQUEST_URI']) . '.dat';
if (file_exists($file_cahe)) {
	if ($realtime < (filemtime($file_cahe) + 3600)) {
		echo file_get_contents($file_cahe);
	    require_once ("../incfiles/end.php");
	    exit;
	}
	else
		@ unlink($file_cahe);
}

$array = array('forecast', 'weekend', 'citychange', 'search', 'near', 'citylist');
if (in_array($act, $array) && file_exists($act . '.php')) {
    require_once($act . '.php');
} else {
	$contents = contents('http://wap.weather.mail.ru' . ($id ? '?city=' . $id : ''));
	$contents = preg_replace('/<\!DOCTYPE(.*?)<div class=\"inner\">/si', '<div>', $contents);
	$contents = preg_replace('/<p>&#032;<\/p>(.*?)<\/html>/si', '', $contents);
	$contents = preg_replace('/\/([a-z0-9]+)\.xhtml\?city=([0-9]+)/si', '\1_\2.html', $contents);
	$contents = preg_replace('/<h1>(.*?)<\/h1>/si', '<div class="phdr"><b>\1</b></div>', $contents);
	$contents = preg_replace('/<h3>(.*?)<\/h3>/si', '<div class="bmenu"><b>\1</b></div>', $contents);
	$contents = strtr($contents, array('<div class="forecast">' => '<div class="list1">', 'forecasttext' => 'list2', 'inner' => 'phdr', '/img/' => 'http://wap.weather.mail.ru/img/', 'spec' => 'green', '<span>' => '<span class="red">'));
	echo $contents;
}

if($contents)
	@ file_put_contents($file_cahe, $contents);
else
	echo '<div class="rmenu"><p>Сервис временно не доступен!</p></div>';

require_once('../incfiles/end.php');

?>