<?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 (nouvelle fonction plus rapide)
function getContenuURL($url,$estImg='')
{
	$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 getListEbookAmazon($url)
	{
	global $url_default;
		$erreur = '';
		$Ebooks = '';
		if( $contenuUrl = getContenuURL($url) )
		{
			// on isole le bloc de résultats
			if($resultats = getEpression($contenuUrl,'#<div id="rightContainerATF">(.*?)<div id="leftNavContainer">#is'))
			{
			$resultats = $resultats[0];
				// on isole les lignes de résultats
				if(preg_match_all('#<div id="result_[0-9]+" class="[^"]+" name="[^"]+">(.*?)<br clear="all">#is',$resultats, $lignesResultats))
				{
				$i = 0;
					// pour chaque ligne de resultat
					foreach($lignesResultats[0] as $unResultat)
					{
					// est un ebook si broché
					if($estUnEbook=getEpression($unResultat,'#<div id="result_[0-9]+" class="[^"]+" name="[^"]+">(.*?)<br clear="all">#is'))
					{
						if( $afficheTmp = getEpression($unResultat,'#src="http://ecx.images-amazon.com/images/I/(.*?)"#is') )
						{
							$Ebooks[$i]['affiche'] = 'http://ecx.images-amazon.com/images/I/'.$afficheTmp[0];
						}
						else
						{
							$Ebooks[$i]['affiche'] = $url_default.'images/no_image.png';
						}

						if( $titreTmp = getEpression($unResultat,'#<span class="lrg bold">(.*?)</span>#is') )
						{
							$Ebooks[$i]['titre'] = strip_tags($titreTmp[0]);
							$Ebooks[$i]['titre'] = str_replace(array(">"),array(''),$Ebooks[$i]['titre']);
						}
						else
						{
							$Ebooks[$i]['titre'] = 'Aucune information ...';
						}

						if( $auteurTmp = getEpression($unResultat,'#<span class="med reg">de (.*?)</a>#is') )
						{
							$Ebooks[$i]['auteur'] = strip_tags(utf8_decode($auteurTmp[0]));
						}
						else
						{
							$Ebooks[$i]['auteur'] = 'Aucune information ...';
						}

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

						if( $dateTmp = getEpression($unResultat,'# <span class="binding">[^"]+</span> -(.*?)\)</span>#is') )
						{
							$Ebooks[$i]['date'] = strip_tags($dateTmp[0]);
							$Ebooks[$i]['date'] = str_replace(array("(",")"),array("",""),$Ebooks[$i]['date']);
						}
						else
						{
							$Ebooks[$i]['date'] = 'Aucune information ...';
						}
						$i++;
					}

						// mais est un ebook aussi si poche
						elseif($estUnEbook=getEpression($unResultat,'#<span class="binding">Poche(.*?)</span>#is'))
						{
							if( $afficheTmp = getEpression($unResultat,'#src="http://ecx.images-amazon.com/images/I/(.*?)"#is') )
							{
								$Ebooks[$i]['affiche'] = 'http://ecx.images-amazon.com/images/I/'.$afficheTmp[0];
							}
							else
							{
								$Ebooks[$i]['affiche'] = 'http://g-ecx.images-amazon.com/images/G/08/x-site/icons/no-img-lg._V45690800_.gif';
							}

							if( $titreTmp = getEpression($unResultat,'#<span class="srTitle">(.*?)</span>#is') )
							{
								$Ebooks[$i]['titre'] = strip_tags($titreTmp[0]);
							}
							else
							{
								$Ebooks[$i]['titre'] = 'Aucune information ...';
							}

							if( $auteurTmp = getEpression($unResultat,'#</span></a>(.*?)<span class="bindingBlock">#is') )
							{
								$Ebooks[$i]['auteur'] = strip_tags(utf8_decode($auteurTmp[0]));
							}
							else
							{
								$Ebooks[$i]['auteur'] = 'Aucune information ...';
							}

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

							if( $dateTmp = getEpression($unResultat,'#<span class="bindingBlock">(.*?)</span></td>#is') )
							{
								$Ebooks[$i]['date'] = strip_tags($dateTmp[0]);
							}
							else
							{
								$Ebooks[$i]['date'] = 'Aucune information ...';
							}
							$i++;
						}

						// mais est un ebook aussi si relié
						elseif($estUnEbook=getEpression($unResultat,'#<span class="binding">Relié(.*?)</span>#is'))
						{
							if( $afficheTmp = getEpression($unResultat,'#src="http://ecx.images-amazon.com/images/I/(.*?)"#is') )
							{
								$Ebooks[$i]['affiche'] = 'http://ecx.images-amazon.com/images/I/'.$afficheTmp[0];
							}
							else
							{
								$Ebooks[$i]['affiche'] = 'http://g-ecx.images-amazon.com/images/G/08/x-site/icons/no-img-lg._V45690800_.gif';
							}

							if( $titreTmp = getEpression($unResultat,'#<span class="srTitle">(.*?)</span>#is') )
							{
								$Ebooks[$i]['titre'] = strip_tags($titreTmp[0]);
							}
							else
							{
								$Ebooks[$i]['titre'] = 'Aucune information ...';
							}

							if( $auteurTmp = getEpression($unResultat,'#</span></a>(.*?)<span class="bindingBlock">#is') )
							{
								$Ebooks[$i]['auteur'] = strip_tags(utf8_decode($auteurTmp[0]));
							}
							else
							{
								$Ebooks[$i]['auteur'] = 'Aucune information ...';
							}

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

							if( $dateTmp = getEpression($unResultat,'#<span class="bindingBlock">(.*?)</span></td>#is') )
							{
								$Ebooks[$i]['date'] = strip_tags($dateTmp[0]);
							}
							else
							{
								$Ebooks[$i]['date'] = 'Aucune information ...';
							}
							$i++;
						}

						// mais est un ebook aussi si Reliure inconnue
						elseif($estUnEbook=getEpression($unResultat,'#<span class="binding">Reliure inconnue(.*?)</span>#is'))
						{
							if( $afficheTmp = getEpression($unResultat,'#src="http://ecx.images-amazon.com/images/I/(.*?)"#is') )
							{
								$Ebooks[$i]['affiche'] = 'http://ecx.images-amazon.com/images/I/'.$afficheTmp[0];
							}
							else
							{
								$Ebooks[$i]['affiche'] = 'http://g-ecx.images-amazon.com/images/G/08/x-site/icons/no-img-lg._V45690800_.gif';
							}

							if( $titreTmp = getEpression($unResultat,'#<span class="srTitle">(.*?)</span>#is') )
							{
								$Ebooks[$i]['titre'] = strip_tags($titreTmp[0]);
							}
							else
							{
								$Ebooks[$i]['titre'] = 'Aucune information ...';
							}

							if( $auteurTmp = getEpression($unResultat,'#</span></a>(.*?)<span class="bindingBlock">#is') )
							{
								$Ebooks[$i]['auteur'] = strip_tags(utf8_decode($auteurTmp[0]));
							}
							else
							{
								$Ebooks[$i]['auteur'] = 'Aucune information ...';
							}

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

							if( $dateTmp = getEpression($unResultat,'#<span class="bindingBlock">(.*?)</span></td>#is') )
							{
								$Ebooks[$i]['date'] = strip_tags($dateTmp[0]);
							}
							else
							{
								$Ebooks[$i]['date'] = 'Aucune information ...';
							}
							$i++;
						}

						// mais est un ebook aussi si cartonné
						elseif($estUnEbook=getEpression($unResultat,'#<span class="binding">Cartonné(.*?)</span>#is'))
						{
							if( $afficheTmp = getEpression($unResultat,'#src="http://ecx.images-amazon.com/images/I/(.*?)"#is') )
							{
								$Ebooks[$i]['affiche'] = 'http://ecx.images-amazon.com/images/I/'.$afficheTmp[0];
							}
							else
							{
								$Ebooks[$i]['affiche'] = 'http://g-ecx.images-amazon.com/images/G/08/x-site/icons/no-img-lg._V45690800_.gif';
							}

							if( $titreTmp = getEpression($unResultat,'#<span class="srTitle">(.*?)</span>#is') )
							{
								$Ebooks[$i]['titre'] = strip_tags($titreTmp[0]);
							}
							else
							{
								$Ebooks[$i]['titre'] = 'Aucune information ...';
							}

							if( $auteurTmp = getEpression($unResultat,'#</span></a>(.*?)<span class="bindingBlock">#is') )
							{
								$Ebooks[$i]['auteur'] = strip_tags(utf8_decode($auteurTmp[0]));
							}
							else
							{
								$Ebooks[$i]['auteur'] = 'Aucune information ...';
							}

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

							if( $dateTmp = getEpression($unResultat,'#<span class="bindingBlock">(.*?)</span></td>#is') )
							{
								$Ebooks[$i]['date'] = strip_tags($dateTmp[0]);
							}
							else
							{
								$Ebooks[$i]['date'] = 'Aucune information ...';
							}
							$i++;
						}
					}	
				}
			}
		}
		else  $erreur[] = "impossible de lire la page";
		return(array($Ebooks,$erreur));
	}

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

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

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

function scanContenuAmazon($contenuUrl)
{
	global $url_default;
	$erreur = "";
		// titre
		if( $titreTmp = getEpression($contenuUrl,'#<h1 class="parseasinTitle">(.*?)</h1>#is') )
		{
			$ebook['titre'] = strip_tags($titreTmp[0]);
		}
		else
		{
			$ebook['titre'] = 'Aucune Information ...';
		}

		// on récupere l'url de l'image
		if( $imageTmp = getEpression($contenuUrl,'#registerImage\("original_image"\, "http://ecx.images-amazon.com/images/I/(.*?).jpg"#is') )
		{
			$ebook['image'] = 'http://ecx.images-amazon.com/images/I/'.$imageTmp[0] .'.jpg';
		}
		elseif( $imageTmp = getEpression($contenuUrl,'#registerImage\("original_image"\, "http://g-ecx.images-amazon.com/images/I/(.*?).jpg#is') )
		{
			$ebook['image'] = 'http://g-ecx.images-amazon.com/images/I/'.$imageTmp[0] .'.jpg';
		}
		elseif( $imageTmp = getEpression($contenuUrl,'#"http://ecx.images-amazon.com/images/I/(.*?).jpg"#is') )
		{
			$ebook['image'] = 'http://g-ecx.images-amazon.com/images/I/'.$imageTmp[0].'.jpg';
		}
		elseif( $imageTmp = getEpression($contenuUrl,'#"http://g-ecx.images-amazon.com/images/I/(.*?).jpg"#is') )
		{
			$ebook['image'] = 'http://g-ecx.images-amazon.com/images/I/'.$imageTmp[0].'.jpg';
		}
		elseif( $imageTmp = getEpression($contenuUrl,'#registerImage\("original_image"\, "http://g-ecx.images-amazon.com/images/I/(.*?).gif#is') )
		{
			$ebook['image'] = 'http://g-ecx.images-amazon.com/images/I/'.$imageTmp[0] .'.gif';
		}
		elseif( $imageTmp = getEpression($contenuUrl,'#registerImage\("original_image"\, "http://ecx.images-amazon.com/images/I/(.*?).gif#is') )
		{
			$ebook['image'] = 'http://ecx.images-amazon.com/images/I/'.$imageTmp[0] .'.gif';
		}
		else
		{
			$ebook['image'] = 'http://g-ecx.images-amazon.com/images/G/08/nav2/dp/no-image-avail-img-map._V23182730_AA192_.gif';
		}

		// on récupere le résumé
			if( $resumeTmp = getEpression($contenuUrl,'#<h2>Descriptions du produit</h2>(.*?)<a name="productDetails" id="productDetails"></a>#is') )
			{
				$ebook['resume'] = $resumeTmp[0];
				$ebook['resume'] = trim($ebook['resume']);
				$ebook['resume'] = preg_replace('#<h3 class="productDescriptionSource">#is','<b>',$ebook['resume']);
				$ebook['resume'] = preg_replace('#</h3>#is','</b><br />',$ebook['resume']);
				$ebook['resume'] = preg_replace('#<div class="(.*?)">#is','',$ebook['resume']);
				$ebook['resume'] = preg_replace('#<a href="(.*?)</a>#is','',$ebook['resume']);
				$ebook['resume'] = preg_replace('#</div>#is','',$ebook['resume']);
				$ebook['resume'] = preg_replace('#<img src="http://g-ecx.images-amazon.com/images/G/08/x-locale/common/orange-arrow._V42752349_.gif" width="10" class="icon" height="9" border="0" />#is','',$ebook['resume']);
				$ebook['resume'] = str_replace(array("  ","      ","<div class=\"content\">","\n","<p>","</p>","<P>","</P>"),array("","","","","","","<br />","","<br />",""),$ebook['resume']);
				
				//description bbcode
				$ebook['resume_'] = str_replace(array("<B>","</B>","<b>","</b>","<br />","<br>","<BR>","<i>","<I>","</i>","</I>","<em>","<EM>","</em>","</EM>"),array("[b]","[/b]","[b]","[/b]","\n","\n","\n","","","","","","","",""),$ebook['resume']);
			}else{
			    $ebook['resume'] = 'Aucune Description de la part de Amazon.fr ...';
				$ebook['resume_'] = 'Aucune Description de la part de Amazon.fr ...';
			}

		// éditeur
		if( $auteurTmp = getEpression($contenuUrl,'#</h1>(.*?)<br />#is') )
		{
			$ebook['auteur'] = strip_tags(trim($auteurTmp[0]));
		}
		else
		{
			$ebook['auteur'] = 'Aucune Information ...';
		}

		// éditeur
		if( $editeurTmp = getEpression($contenuUrl,'#<li><b>Editeur[^\:]+:</b>(.*?)</li>#is') )
		{
			$ebook['editeur'] = strip_tags($editeurTmp[0]);
		//remplacement caractère
			$ebook['editeur'] = str_replace(array("\n",':','Editeur','Édition&#160;','Collection&#160;','Édition&nbsp;:'),array('','','','<br><b>Édition :</b>','<br><b>Collection :</b>','Édition&nbsp;<br />'),$ebook['editeur']);
		//remplacement caractère bbcode
			$ebook['editeurbbcode'] = str_replace(array('<br><b>Édition :</b>','<br><b>Collection :</b>'),array("\n".'[b][u]Édition[/u] : [/b] [i]','[/i]'."\n".'[b][u]Collection[/u] : [/b] [i]'),$ebook['editeur']);
		}
		else
		{
			$ebook['editeur'] = 'Aucune Information ...';
			$ebook['editeurbbcode'] = 'Aucune Information ...';
		}

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

		// nbrpages

		// nb page broché
		if( $brocheTmp = getEpression($contenuUrl,'#<li><b>Broché:</b>(.*?)</li>#is') )
		{
			$ebook['broche'] = strip_tags($brocheTmp[0]);
		}
		else
		{
			$ebook['broche'] = '0';
		}

		// nb page relié
		if( $ReliesTmp = getEpression($contenuUrl,'#<li><b>Relié:</b>(.*?)</li>#is') )
		{
			$ebook['Relies'] = strip_tags($ReliesTmp[0]);
		}
		else
		{
			$ebook['Relies'] = '0';
		}

		// nb page cartonné
		if( $cartonneTmp = getEpression($contenuUrl,'#<li><b>Cartonné:</b>(.*?)</li>#is') )
		{
			$ebook['cartonne'] = strip_tags($cartonneTmp[0]);
		}
		else
		{
			$ebook['cartonne'] = '0';
		}

		// nb page Poche
		if( $pocheTmp = getEpression($contenuUrl,'#<li><b>Poche:</b>(.*?)</li>#is') )
		{
			$ebook['poche'] = strip_tags($pocheTmp[0]);
		}
		else
		{
			$ebook['poche'] = '0';
		}

		// ISBN-10
		if( $isbn10Tmp = getEpression($contenuUrl,'#<li><b>ISBN-10:</b>(.*?)</li>#is') )
		{
			$ebook['isbn10'] = strip_tags($isbn10Tmp[0]);
		}
		else
		{
			$ebook['isbn10'] = 'Aucune Information ...';
		}

		// ISBN-13
		if( $isbn13Tmp = getEpression($contenuUrl,'#<li><b>ISBN-13:</b>(.*?)</li>#is') )
		{
			$ebook['isbn13'] = strip_tags($isbn13Tmp[0]);
		}
		else
		{
			$ebook['isbn13'] = 'Aucune Information ...';
		}
		return(array($ebook,$erreur));
	}

?>