<?php
/*
 * Created on 11.02.2012
 * Oliver Soop(osoop1@ut.ee)
 */
 
 include_once("../model/DownloadedXML.php");
 include_once("../model/RSSBaseTagger.php");
 class BaseCrawler {
 	protected $url;
 	protected $channelId;
 	protected $languageId;
 	protected $tagCollection;
 	private static $allowedLanguages = array('et', 'en');

 	/**
 	 * Funktsioon, mis kasutab HTTP päringu tegemiseks cURL moodulit ja tagastab päringu sisu
 	 * (Milleks üldjuhul HTML leht)
 	 */
 	protected function getWebPage($url) {
 		$options = array(
			CURLOPT_RETURNTRANSFER => true,     // return web page
			CURLOPT_HEADER         => false,    // don't return headers
			CURLOPT_FOLLOWLOCATION => true,     // follow redirects
			CURLOPT_ENCODING       => "",       // handle all encodings
			CURLOPT_USERAGENT      => "spider", // who am i
			CURLOPT_AUTOREFERER    => true,     // set referer on redirect
			CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
			CURLOPT_TIMEOUT        => 120,      // timeout on response
			CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
		);
	
	
		$ch  = curl_init( $url );
		if(!$ch) {
			return null;
		}
		curl_setopt_array( $ch, $options );
		$content = curl_exec( $ch );
		$err     = curl_errno( $ch );
		$errmsg  = curl_error( $ch );
		$header  = curl_getinfo( $ch );
		curl_close( $ch );
	
		$xml = new DownloadedXML($err, $errmsg, $content);
		return $xml;
 	}

 	
 	/**
 	 * Funktsioon, mille abil saab kontrollida, kas RSS-voo keel on lubatud keelte seas
 	 */
 	protected function checkFeedLanguage($document) {
 		$languageItems = $document->getElementsByTagName(RSSBaseTagger::$languageTag);
 		
 		foreach($languageItems as $item) {
 			if(in_array($item->nodeValue, self::$allowedLanguages)){
 				return true;
 			}
 		}
 		return false;
 	}
 	
 }
?>
