<?php
###############################################################
################# Générateur de présentation  #################
###################### Version 1.0 Béta #######################
##################### Auteur : Belzebuth ######################
################### Version du script : 1.0 ###################
############ Mail : fantasygraph@fantasy-graph.net ############
###############################################################

// Fonctions

// Récupération d'une page distante
function getContenuURL($url,$estImg='')
 {
	$contenuPage = '';

	// à preciser dans le header
	if (methodscan != 'curl') {
	$contenuPage = @file_get_contents($url);

	}else{
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_HEADER, 0);
	ob_start();
	curl_exec ($ch);
	curl_close ($ch);
	$contenuPage = ob_get_contents();
	ob_end_clean();
	}
	return $contenuPage;
 }

// Chercher expression
function getEpression($chaine,$expression)
{
	preg_match_all($expression,$chaine, $trouve);

	if( count($trouve[0])!=0 )
	{
		return($trouve[1]);
	}
	else return(false);
}

//---------------------------------------

	function getListDVDAmazon($url)
	{
	global $url_default;
		$erreur = '';
		$dvds = '';

		if( $contenuUrl = getContenuURL($url) )
		{
			// on isole le bloc de résultats
			if($resultats = getEpression($contenuUrl,'#<div id="rightContainerATF">(.*?)<div id="leftNav">#is'))
			{
			$resultats = $resultats[0];
				// on isole les lignes de résultats
				if(preg_match_all('#<div id="result_[^"]+" class="[^"]+" name="[^"]+">(.*?)<br clear="all">#is',$resultats, $lignesResultats))
				{
					$i = 0;
					// pour chaque ligne de resultat
					foreach($lignesResultats[0] as $unResultat)
					{
						// est un DVD
						if($estUnDvd=getEpression($unResultat,'#<div id="result_[^"]+" class="[^"]+" name="[^"]+">(.*?)<br clear="all">#is'))
						{
							if( $dvdsTmp = getEpression($unResultat,'#src="http://ecx.images-amazon.com/images/I/(.*?)"#is') )
							{
								$dvds[$i]['affiche'] = 'http://ecx.images-amazon.com/images/I/'.$dvdsTmp[0].'';
							}
							
							elseif( $dvdsTmp = getEpression($unResultat,'#src="http://ecx.images-amazon.com/images/I/(.*?)"#is') )
							{
								$dvds[$i]['affiche'] = 'http://ecx.images-amazon.com/images/I/'.$dvdsTmp[0];
							}
							else
							{
								$dvds[$i]['affiche'] = $url_default.'images/no_image.png';
							}

							if( $titreTmp = getEpression($unResultat,'#<span class="lrg bold">(.*?)</span>#is') )
							{
								$dvds[$i]['titre'] = strip_tags($titreTmp[0]);
			                    $dvds[$i]['titre'] = str_replace('[Blu-ray]', '', $dvds[$i]['titre']);

								
							}
							else
							{
								$dvds[$i]['titre'] = 'Aucune information ...';
							}

							if( $acteurTmp = getEpression($unResultat,'#<span class="starring">Distribution(.*?)</span>#is') )
							{
								$dvds[$i]['acteur'] = strip_tags($acteurTmp[0]);
								$dvds[$i]['acteur'] = str_replace('~ ','',$acteurTmp[0]);
							}
							else
							{
								$dvds[$i]['auteur'] = 'Aucune information ...';
							}

							if( $idTmp = getEpression($unResultat,'#<a href="/(.*?)">#is') )
							{
								$dvds[$i]['id'] = strip_tags($idTmp[0]);
							}

							if( $dateTmp = getEpression($unResultat,'#</span> -&nbsp;(.*?)</span>#is') )
							{
								$dvds[$i]['date'] = strip_tags($dateTmp[0]);
							    $dvds[$i]['date'] = str_replace(')','',$dateTmp[0]);

							}
							else
							{
								$dvds[$i]['date'] = 'Aucune information ...';
							}
							$i++;
						}
					}
				}
			}
		}
		else  $erreur[] = "Impossible de lire la page .";
		return(array($dvds,$erreur));
	}

//---------------------------------------

	function getInfoDVDAmazon($url)
	{
		$erreur = '';
		// $date = getValeur("date","");

		if( $contenuUrl = getContenuURL($url) )
		{
			list($dvd,$erreurs) = scanContenuAmazon($contenuUrl);
			if(!empty($date))
			{
				$dvd['date'] = $date;
			}
			if(!empty($erreurs))
			{
				$erreur = array_merge($erreur,$erreurs);
			}
		}
		else $erreur[] = "impossible de lire la page";
		return(array($dvd,$erreur));
	}

//---------------------------------------

	function scanContenuAmazon($contenuUrl)
	{
	global $url_default;
		$erreur = "";

	// titre
		if( $titreTmp = getEpression($contenuUrl,'#<h1 class="parseasinTitle">(.*?)</h1>#is') )
		{
			$dvd['titre'] = strip_tags($titreTmp[0]);
			$dvd['titre'] = str_replace("\n", '', $dvd['titre']);
			$dvd['titre'] = str_replace("\t", '', $dvd['titre']);
			$dvd['titre'] = str_replace("\r", '', $dvd['titre']);
			$dvd['titre'] = str_replace('  ', '', $dvd['titre']);
		}
		elseif( $titreTmp = getEpression($contenuUrl,'#title: "(.*?)"#is') )
		{
			$dvd['titre'] = strip_tags($titreTmp[0]);
			$dvd['titre'] = str_replace("\n", '', $dvd['titre']);
			$dvd['titre'] = str_replace("\t", '', $dvd['titre']);
			$dvd['titre'] = str_replace("\r", '', $dvd['titre']);
			$dvd['titre'] = str_replace('  ', '', $dvd['titre']);
		}
		else
		{
			$dvd['titre'] = 'Aucune Information ...';
		}


			// on récupere l'url de l'image
		if( $imageTmp = getEpression($contenuUrl,'#<img src="http://ecx.images-amazon.com/images/I/(.*?)"#is') )
		{
			$dvd['image'] = 'http://ecx.images-amazon.com/images/I/' .$imageTmp[0]. '';
		}
		
		else
		{
			$dvd['image'] = $url_default.'images/no_image.png';
		}

		// on récupere le Format : PAL
		if( $palTmp = getEpression($contenuUrl,'#<li> <b>Format[^\:]+:</b>(.*?)</li>#is') )
		{
			$dvd['pal'] = strip_tags($palTmp[0]);
		}
		else
		{
			$dvd['pal'] = 'Aucune Information ...';
		}


		// acteur(s)
		if( $acteurTmp = getEpression($contenuUrl,'#<li> <b>Acteurs[^\:]+:</b>(.*?)</li>#is') )
		{
			$dvd['acteur'] = strip_tags(trim($acteurTmp[0]));
		}
		else
		{
			$dvd['acteur'] = 'Aucune Information ...';
		}

		// realisateur
		if( $realisateurTmp = getEpression($contenuUrl,'#<b>Réalisateurs[^\:]+:</b>(.*?)</li>#is') )
		{
			$dvd['real'] = strip_tags($realisateurTmp[0]);
		}
		else
		{
			$dvd['real'] = 'Aucune Information ...';
		}

		// language dispo par l'editeur
		if( $languageTmp = getEpression($contenuUrl,'#<li><b>Langue&nbsp;:</b>(.*?)</li>#is') )
		{
			$dvd['language'] = strip_tags($languageTmp[0]);
		}
		else
		{
			$dvd['language'] = 'Aucune Information ...';
		}

		// Studio
		if( $studioTmp = getEpression($contenuUrl,'#<li> <b>Studio&nbsp;:</b> (.*?)</li>#is') )
		{
			$dvd['studio'] = strip_tags($studioTmp[0]);
		}
		else
		{
			$dvd['studio'] = 'Aucune Information ...';
		}


	// on récupere la note
		if( $NoteTmp = getEpression($contenuUrl,'#<span class="swSprite s_star_(.*?) " ><span>[^\:]+ étoiles sur 5</span></span>#is') )
		{
			$dvd['Note'] = $url_default.'images/s_star'.$NoteTmp[0].'.png';
		}
		else
		{
			$dvd['Note_'] = '<img src="'.$url_default.'images/s_star_0_0.png" />';
			$dvd['Note-'] = '[img]'.$url_default.'images/s_star_0_0.png[/img]';
		}

		// Date de sortie du DVD
		if( $dateTmp = getEpression($contenuUrl,'#<li> <b>Date de sortie du DVD[^\:]+:</b>(.*?)</li>#is') )
		{
			$dvd['date'] = strip_tags($dateTmp[0]);
		}
		else
		{
			$dvd['date'] = 'Aucune Information ...';
		}

		// description
		if( $descriptionTmp = getEpression($contenuUrl,'#Synopsis</h3>(.*?)</div>#is') )
		{
			$dvd['description'] = strip_tags($descriptionTmp[0]);
			$dvd['description'] = strip_tags(trim(utf8_decode($descriptionTmp[0])));
			$dvd['description'] = str_replace("\n", '', $dvd['description']);
			$dvd['description'] = str_replace("\r", '', $dvd['description']);
			$dvd['description'] = str_replace("  ", '', $dvd['description']);
		}
		elseif( $descriptionTmp = getEpression($contenuUrl,'#Description</h3>(.*?)</div>#is') )
		{
			$dvd['description'] = strip_tags($descriptionTmp[0]);
			$dvd['description'] = strip_tags(trim(utf8_decode($descriptionTmp[0])));
			$dvd['description'] = str_replace("\n", '', $dvd['description']);
			$dvd['description'] = str_replace("\r", '', $dvd['description']);
			$dvd['description'] = str_replace("  ", '', $dvd['description']);
		}
		elseif( $descriptionTmp = getEpression($contenuUrl,'#Amazon.fr</h3>(.*?)</div>#is') )
		{
			$dvd['description'] = strip_tags($descriptionTmp[0]);
			$dvd['description'] = strip_tags(trim(utf8_decode($descriptionTmp[0])));
			$dvd['description'] = str_replace("\n", '', $dvd['description']);
			$dvd['description'] = str_replace("\r", '', $dvd['description']);
			$dvd['description'] = str_replace("  ", '', $dvd['description']);
		}
		elseif( $descriptionTmp = getEpression($contenuUrl,'#Pr&\#233;sentation de l\'&\#233;diteur</h3>(.*?)</#is') )
		{
			$dvd['description'] = strip_tags($descriptionTmp[0]);
			$dvd['description'] = strip_tags(trim(utf8_decode($descriptionTmp[0])));
			$dvd['description'] = str_replace("\n", '', $dvd['description']);
			$dvd['description'] = str_replace("\r", '', $dvd['description']);
			$dvd['description'] = str_replace("  ", '', $dvd['description']);
		}
		elseif( $descriptionTmp = getEpression($contenuUrl,'#<b>Amazon.fr</b><br />(.*?)<#is') )
		{
			$dvd['description'] = strip_tags($descriptionTmp[0]);
			$dvd['description'] = strip_tags(trim(utf8_decode($descriptionTmp[0])));
			$dvd['description'] = str_replace("\n", '', $dvd['description']);
			$dvd['description'] = str_replace("\r", '', $dvd['description']);
			$dvd['description'] = str_replace("  ", '', $dvd['description']);
		}
		elseif( $descriptionTmp = getEpression($contenuUrl,'#<b>Synopsis</b><br />(.*?)<#is') )
		{
			$dvd['description'] = strip_tags($descriptionTmp[0]);
			$dvd['description'] = strip_tags(trim(utf8_decode($descriptionTmp[0])));
			$dvd['description'] = str_replace("\n", '', $dvd['description']);
			$dvd['description'] = str_replace("\r", '', $dvd['description']);
			$dvd['description'] = str_replace("  ", '', $dvd['description']);
		}
		else
		{
			$dvd['description'] = 'Aucune Information ...';
		}

		// on récupere le nombre de disques
		if( $discTmp = getEpression($contenuUrl,'#<li> <b>Nombre de disques&nbsp;:</b> (.*?)</li>#is') )
		{
			$dvd['disc'] = strip_tags($discTmp[0]);
		}
		else
		{
			$dvd['disc'] = 'Aucune Information ...';
		}

		// on récupere le ASIN
		if( $asinTmp = getEpression($contenuUrl,'#<li><b>ASIN:</b> (.*?)</li>#is') )
		{
			$dvd['asin'] = strip_tags($asinTmp[0]);
		}
		else
		{
			$dvd['asin'] = 'Aucune Information ...';
		}

		if( $stockTmp = getEpression($contenuUrl,'#<span class="availGreen">(.*?)</span>#is') )
		{
			$dvd['stock'] = '<span style="color: rgb(0, 191, 0);"><b>'.strip_tags($stockTmp[0]).'</b></span>';
		}
		return(array($dvd,$erreur));
	}

?>