<?php
class sourceFetcher extends sourceData{
	private $rss = '';
	private $rss_data = array();		// rss news from rss file
	
	private $feedCount = 0;
	
	private $isWorking = false;
	private $source = '';
	
	private $lastNews = 0;	// know where to start from
	
	public function __construct($source){
		if ($source->url != ''){
			$this->isWorking = true;
			$this->source = $source;
			parent::__construct($source);
			if ($this->useProxy == 1){
				include_once ('objects/rssFetcher/p_RSSReader.inc.php');
			}else{
				include_once ('objects/rssFetcher/RSSReader.inc.php');
			}
			
			if ($source->byWordFilter >0){
				$this->wf_words = explode("\n",$this->wordFilter->words);
				$this->wordFilter->words=str_replace("\r",'',$this->wordFilter->words);
				$this->wordFilter->words=str_replace("   ",' ',$this->wordFilter->words);
				$this->wordFilter->words=str_replace("  ",' ',$this->wordFilter->words);
				$this->wordFilter->words=str_replace('\\','/',$this->wordFilter->words);
				$this->wordFilter->words=str_replace('"','&quot;',$this->wordFilter->words);
				$this->wordFilter->words=str_replace("'",'&#039;',$this->wordFilter->words);
			}
			if ($source->blockFilter !=''){
				$source->blockFilter->words=str_replace("\r",'',$source->blockFilter->words);
				$source->blockFilter->words=str_replace("   ",' ',$source->blockFilter->words);
				$source->blockFilter->words=str_replace("  ",' ',$source->blockFilter->words);
				$source->blockFilter->words=str_replace('\\','/',$source->blockFilter->words);
				$source->blockFilter->words=str_replace('"','&quot;',$source->blockFilter->words);
				$source->blockFilter->words=str_replace("'",'&#039;',$source->blockFilter->words);
				$this->bf_words = explode("\n",$source->blockFilter->words);
			}
			
			
			if ($this->fetch()){
				$this->rssToArray();
				$this->organiseRss();
				$this->fetchLastNews();
				$this->generateNewsItems();
			}
			mysql_query("delete from rss_fetch_stack where (sourceId = '".$source->id."' and fetch_status = '1')");
			mysql_query("update rss_fetch_stack set fetched_ts='".date("U")."' , fetch_status = '1' where (sourceId = '".$source->id."' and fetch_status = '0')");
		}
	}
	
	public function fetch(){
		if (!$this->isWorking){return false;}
		$this->rss = new RSSReader($this->url);
		$this->feedCount = $this->rss->getNumberOfNews();
		
		if (count($this->rss->errors)>0){
			global $LDT,$_SETTING_server_id;
			$errorData->source_id = $this->source->id;
			$errorData->site_id = $this->source->site_id;
			$errorData->server_id = $_SETTING_server_id;
			$errorData->errors = $this->rss->errors;
			$LDT->addData('source_log','source_error',$errorData);
			$LDT->send(true);
			return false;
		}
		return true;
		//echo 'Found '.$this->feedCount.' News <br>';
	}//
/////////////////////////////////////////////////////////////
///////////////////// Private Functions /////////////////////
/////////////////////////////////////////////////////////////

	private function rssToArray(){
		for($i=0;$i<$this->feedCount;$i++){
			$feeder->link=$this->rss->getItemLink("",$i);
			$feeder->title=htmlspecialchars_decode($this->rss->getItemTitle("",$i));
			//$feeder->title=iconv('','UTF-8//IGNORE',$feeder->title);
			
			$feeder->pubDate=strtotime($this->rss->getItemPubdate("",$i));
			$reson = '';
			//echo 'parsing through '.$i.' '.$feeder->title.'<br>';
			// check for word filter
				$addIt = $this->checkWordFilter($feeder->title);
				if (!$addIt){$reson = 'word Filter<br>';}
			//check for block filter
				if ($addIt){
					$addIt = $this->checkBlockFilter($feeder->title);
					if (!$addIt){$reson = 'block Filter<br>';}
				}
			// dont copy news older than 2 days
				if ($feeder->pubDate > 0 && $feeder->pubDate<(date("U")-(3600*48))){
					$addIt = false;
				}
				
			
			if ($addIt){
				$this->rss_data[$i]=$feeder;
			}else{
				//echo 'Skipped '.$feeder->title.' for '.$reson;
			}
			unset($feeder);
		}
	}//
	
	private function organiseRss(){
		usort ( $this->rss_data , 'order_feed_array' );
	}//
	
	private function fetchLastNews(){
		global $PDT;
		
		$LastNewsData->source = $this->source->id;
		$LastNewsData->data = $this->rss_data;
		
		
		
		/*
		*/
		
		$c = $PDT->addData('get','last_news_of_source',$LastNewsData);
				
		$this->lastNews = $PDT->send(false);
		$url = $this->lastNews;
		
		$id = 0;
		foreach($this->rss_data as $key=>$val)
		{
			if($this->source->id == '143')
			{
				file_put_contents("data/val_".$LastNewsData->source,print_r($val,true));
				file_put_contents("data/key_".$LastNewsData->source,$key);
			}
			
			$pos = strpos($url,$val->link);
			
			if($pos === false)
			{
				
			
			}else{
			     
				$var = $key;
			}
			
			$id++;
		}
		$this->lastNews = $var;
		
		if($LastNewsData->source == '143')
		{
			file_put_contents("data/LastNewsData_".$LastNewsData->source,print_r($LastNewsData,true));
			file_put_contents("data/var_".$LastNewsData->source,$this->lastNews);
			file_put_contents("data/url_".$LastNewsData->source,$url);
		}
		/*
		$this->lastNews = 0;
		*/
	}//
	
	private function generateNewsItems(){
		$fetched_count = 0;
		for($i=$this->lastNews;$i<$this->feedCount;$i++){
			
			if ($this->rss_data[$i]->title!=''){
				if ($this->source->id == '143'){
					file_put_contents("data/nid_".$this->rss_data[$i]->title,$this->rss_data[$i]->title."\n".$this->rss_data[$i]->link);
				}
		
				$x = new fetchedData($this->source , $this->rss_data[$i]);
				$xer = $x->save();
				if ($xer !== false){
					$fetched_count++;
				}else{
					//$erf .=mysql_error().$xer."<Br>";
				}
			}
		}
		global $LDT,$_SETTING_server_id;
		$dat->count = 0;
		$dat->server_id = $_SETTING_server_id;
		if ($fetched_count > 0){
			$dat->count = $fetched_count;
		}
		$LDT->addData('log','fetch_count',$dat);
		$LDT->send(true);
		if ($this->source->id == '3964'){
			//file_put_contents('yasater.txt',$erf);
		}
	}//
	
	private function checkWordFilter($title){
		if ($this->source->byWordFilter >0 && count($this->wf_words)>0){
			//echo '<br>got into loop for '.$this->title.'<br>';
			foreach ($this->wf_words as $wf_word){
				$wf_word=str_replace("\r",'',$wf_word);
				$wf_word=str_replace("\n",'',$wf_word);
				$wf_word=str_replace('"','&quot;',$wf_word);
				$wf_word=str_replace("'",'&#039;',$wf_word);
				$wf_word=trim($wf_word);
				//file_put_contents('words',$wf_word);
				if ($wf_word!='' && strpos(' '.strtolower($title).' ',' '.strtolower($wf_word).' ')!==false){
					return true;
				}
			}
		}else{
			return true;
		}
		return false;
	}//
	
	private function checkBlockFilter($title){
		if (count($this->bf_words)>0){
			foreach ($this->bf_words as $wf_word){
				$wf_word=str_replace("\r",'',$wf_word);
				$wf_word=str_replace("\n",'',$wf_word);
				$wf_word=str_replace('"','&quot;',$wf_word);
				$wf_word=str_replace("'",'&#039;',$wf_word);
				$wf_word=trim($wf_word);
				if ($wf_word!='' && strpos(' '.strtolower($title).' ',' '.strtolower($wf_word).' ')!==false){
					return false;
				}
			}
		}else{
			 return true;
		}
		return true;
	}//
}// end class


function order_feed_array($a, $b){
	if ($a->pubDate == $b->pubDate) {
        return 0;
    }
    return ($a->pubDate < $b->pubDate) ? -1 : 1;
}