<? 
require('CURL.php');
class Blogs_last_post extends CURL{

private $_result;

/*private $listeImages = array(
	0 => 0, // penelope
	1 => 12, //explosm
	2 => 0, // margaux
);

private $nomImages = array(
	0 => 'PenelopeBagieu',
	1 => 'CyanideAndHapiness',
	2 => 'MargauxMotin',
);

public $link = array(
	0 => 'http://www.penelope-jolicoeur.com/',
	1 => 'http://www.explosm.net/comics/',
	2 => 'http://margauxmotin.typepad.fr/',
);*/
private $link = array();


function getResult()
{
	return $this->_result;
}

public function getLinks(){
	if($linksXML = simplexml_load_file('db/blog_links.xml')){
		foreach($linksXML->link as $individualLink){
			$this->link[] = array('name'=>$individualLink->name, 'url'=>$individualLink->url, 'number'=>$individualLink->number);
		}
		return $this->link;
	}
	else
		return;
}

function getTitles()
{
	$xhtml = "";
	try{
		foreach($this->exec() as $result)
		{
			$xhtml .= $this->getTitle($result);
			$xhtml .= '<br/>';
		}
	}catch(Exception $e)
	{
		$xhtml .= $this->error();
	}
	return $xhtml;
	
}
	
function getTitle($result = null, $url = null)
{
	if(isset($result))
	{
		preg_match( "/<h3 class=\"entry-header\">(.*)<\/h3>/i", $result, $match );
		
		if(isset($match[1]))
		return utf8_decode(strip_tags($match[1])); 
		else{
			preg_match( "/<title>(.*)<\/title>/i", $result, $title );
			if(isset($title[1]))
				return $title[1];
			else
			return 'Erreur : pas de titre de blog trouv�.';
		}
		
	}
	//TODO en fonction de l'url et non du resultat du cURL
}

function getPost()
{

}

function createThumbnail($result, $title = 0)
{
	if(isset($result))
	{
		
		preg_match_all( "#<img[^>]+src=['|\"](.*?)['|\"][^>]*>#i", $result, $match );

		/*$ret = file_put_contents('match.txt', var_export($match,true), FILE_APPEND);
		if ($ret === false)
		{
			echo 'erreur';
		}*/
		$number = $this->link[$title]['number'];
		$title 	= $this->link[$title]['name'];
		if(isset($match[1][(int)$number]))
		{
			$source = @imagecreatefromjpeg($match[1][(int)$number]);
			if($source == false)
				$source = @imagecreatefrompng($match[1][(int)$number]);

			$wSource = @imagesx($source);
			$hSource = @imagesy($source);
			
			$destination = imagecreatetruecolor(50, 50);
			@imagecopyresampled($destination, $source, 0, 0, 0, 0, 50, 50, $wSource, $hSource);
			@imagepng($destination, 'images/blogs/'.$title.'.png');
			return 'images/blogs/'.$title.'.png';
		}
	
	}
}

function getImages($notFromGetImage = false)
{
	if($notFromGetImage){
		//TODO stuff
	}else{
		$xhtml = "";
		$i = 0;
		try{
			foreach($this->exec() as $result)
			{
				$xhtml .= '<img src="'.$this->createThumbnail($result, $i).'" />';
				$xhtml .= '<br/>';
				$i++;
			}
		}catch(Exception $e)
		{
			$xhtml .= $this->error();
		}
		return $xhtml;
	}
}

function getAllImagesToChoose($result,$notFromGetImage = false)
{
	if($notFromGetImage){
		//TODO stuff
	}else{
		preg_match_all( "#<img[^>]+src=['|\"](.*?)['|\"][^>]*>#i", $result, $match );
		$nbImages = count($match[1]);
		$xhtml = 'Liste d\'images : <br/>';
		
		for($i = 0; $i<$nbImages; $i++){
			$xhtml .= '<img src="'.$match[1][$i].'" id="n-'.$i.'" class="choose"/><br/>';
		}
		return $xhtml;
	}
}

function getEverything()
{
		$temps_debut = microtime(true);
		$xhtml = "";
		$i = 0;
		try{
			foreach($this->exec() as $result)
			{
				$xhtml .= '<a href="'.$this->link[$i]['url'].'" target="_blank" class="blogLinks"><img src="'.$this->createThumbnail($result, $i).'" />&nbsp;&nbsp;'.utf8_encode($this->getTitle($result))."</a>";
				$xhtml .= '<br/>';
				$i++;
			}
		}catch(Exception $e)
		{
			$xhtml .= $this->error();
		}
		$temps_fin = microtime(true);
		$xhtml .= 'Temps d\'execution : '.round($temps_fin - $temps_debut, 4).'s';
		return $xhtml;
}


}