Your IP : 216.73.216.201


Current Path : /home/balossw/www/temp/content/includes/
Upload File :
Current File : /home/balossw/www/temp/content/includes/weather.inc.php

<?php
/*---------------------------------------------------------------------------
		File name ............ includes/weather.inc.php
		Author ............... Antoine Lemoine
		Creation date ........ 07.06.2010
		Modification date .... 31.08.2010
---------------------------------------------------------------------------*/

class Weather {
	private $xml;
	private $loaded;
	private $city;

	function Weather($city) {
		$this->city = $city;
		$this->loaded = false;
		$cache_file = $this->cacheFilename();
		if (file_exists($cache_file) && time()-filemtime($cache_file)<5*60) {
			$this->xml = @simplexml_load_string(unserialize(file_get_contents($cache_file)));
			if ($this->xml!==false) $this->loaded = true;
		}
	}
	
	function cacheFilename() {
		global $global;
		return $global->path('var/weather/'.md5($global->lang.$this->city));
	}
	
	function load() {
		global $global;
		$mylang = __('{google.weather-api.lang}');
		if ($mylang=='{google.weather-api.lang}') $mylang = 'fr';
		$content = @file_get_contents('http://www.google.fr/ig/api?hl='.$mylang.'&weather='.urlencode($this->city));
		if ($content) {
			$this->xml = @simplexml_load_string(utf8_encode($content));
			if (!isset($this->xml->weather->problem_cause['data']) || $this->xml->weather->problem_cause['data']=='') {
				$cache_file = $this->cacheFilename();
				@file_put_contents($cache_file,serialize(utf8_encode($content)));
				
				$data = @unserialize(file_get_contents('var/tmp/google.weather.txt'));
				$change = false;
				foreach (array(-1,0,1,2,3) as $day) {
					$icon = $this->getIcon($day,true);
					$icon = explode('/',$icon);
					$icon = $icon[count($icon)-1];
					$text = $this->getCondition($day);
					if (isset($icon) && $icon!='' || isset($text) && $text!='') {
						if (!isset($data[$icon]['texts']) || !in_array($global->lang.':'.$text,$data[$icon]['texts'])) {
							$data[$icon]['texts'][] = $global->lang.':'.$text;
							$data[$icon]['elems'][] = array('icon'=>$icon,'date'=>date('Y-m-d H:i:s'),'lang'=>$global->lang,'text'=>$text);
							$change = true;
						}
					}
				}
				
				if ($change) {
					ksort($data);

					$output = '';
					foreach ($data as $icon => $array) {
						$output .= $icon."\n";
						foreach ($array['elems'] as $index => $elem) {
							if ($elem['date']>'2010-09-06 20:00:00') $output .= "\t".$elem['date'].";\t".$elem['lang'].";\t".$elem['text']."\n";
						}
					}
					@file_put_contents('var/tmp/google.weather.txt',serialize($data));
					@file_put_contents('var/tmp/google.weather.inline.txt',$output);
				}

				$this->loaded = true;
			}
		}
	}
	
	function loaded() {
		return $this->loaded;
	}
	
	function getCity() {
		return utf8_decode($this->xml->weather->forecast_information->city['data']);
	}
	
	function getCondition($inc=-1) {
		return $this->getConditionData('condition',$inc);
	}

	function getTemperature($inc=-1) {
		return $this->getConditionData('temp_c',$inc);
	}

	function getHumidity($inc=-1) {
		return $this->getConditionData('humidity',$inc);
	}

	function getWind($inc=-1) {
		return $this->getConditionData('wind_condition',$inc);
	}

	function getLow($inc=-1) {
		return $this->getConditionData('low',$inc);
	}

	function getHigh($inc=-1) {
		return $this->getConditionData('high',$inc);
	}

	function getDayOfWeek($inc=-1) {
		return $this->getConditionData('day_of_week',$inc);
	}

	function getIcon($inc=-1, $original=false) {
		global $global;
		$icon = $this->getConditionData('icon',$inc);
		if ($original) return 'http://www.google.fr/'.$icon;
		$icon = explode('/',$icon);
		$icon = $icon[count($icon)-1];
		$icon = str_replace('.gif','.png',$icon);
		if ($this->getCondition($inc)==__('Vent') && $icon=='cloudy.png') $icon = 'wind.png';
		return $icon;
		//return $global->path('media/images/weather/'.$icon);
	}
	
	function getConditionData($key, $inc) {
		if ($inc==-1) return $this->getData('current_conditions',$key);
		else return $this->getData('forecast_conditions',$key,$inc);
	}

	function getData($group, $key, $index=-1) {
		if ($index!=-1) {
			$group_node = $this->xml->weather->$group;
			$node = $group_node[$index]->$key;
		}
		else $node = $this->xml->weather->$group->$key;
		return utf8_decode($node['data']);
	}
}

?>