Analyseur de fichiers .torrent

Description

Ce script permet l'analyse d'un fichier torrent en y récupérant des informations comme:
-Le nom du fichier a télécharger
-Le calcul du hash du fichier torrent
-Le nombre de fichiers qui composent le fichier a télécharger
-La taille du(des) fichier(s) a télécharger
-L'url du tracker indexant le torrent
-La date de création du fichier torrent

Source / Exemple :


<?php

require_once ("BDecode.php");
require_once ("BEncode.php");

//*********************************************//functions
         function calcfilesize($bytes)
        {
        if ($bytes < 1000 * 1024)
            return number_format($bytes / 1024, 2) . " kB";
        elseif ($bytes < 1000 * 1048576)
            return number_format($bytes / 1048576, 2) . " MB";
        elseif ($bytes < 1000 * 1073741824)
            return number_format($bytes / 1073741824, 2) . " GB";
        else
            return number_format($bytes / 1099511627776, 2) . " TB";
        }
		
		
		 function checktorrent($alltorrent)
		{
			$debutTorrent = "";
			for ($i=0;$i<11;$i++)
			{
				$debutTorrent .= $alltorrent[$i];
			}
			if($debutTorrent == "d8:announce")
			{
				//echo $debutTorrent;
				echo " Le fichier est bien un torrent <br />";
			}
			else
			{
				//echo $debutTorrent;
				exit (" Erreur le fichier n'est pas un torrent");
			}
		}		
		 function calchash($alltorrent)
		{
			$array = BDecode($alltorrent);
			if (!$array)
			{
				echo "<p class=\"error\">There was an error handling your uploaded torrent. The parser didn't like it.</p>";
				endOutput();
				exit;
			}
			$hash = @sha1(BEncode($array["info"]));
			
			echo "<b>Hash du torrent : </b>$hash <br />";
		}
		 function parcourirtorrent($filesTorrentSize,$alltorrent)
		{
			$compt=0;
			$tailleFichier = '';
			$urlfound = false;
			$namefound = false;
			$datefound = false;
			for ($i=0;$i<$filesTorrentSize;$i++)
			{
				//************//nom du torrent
				if ($alltorrent[$i] == "n" and $namefound !=true)
				{
					$a = $i;
					$name = $alltorrent[$a].$alltorrent[$a+1].$alltorrent[$a+2].$alltorrent[$a+3];
					if ($name == "name")
					{
						$index = $alltorrent[$a+4].$alltorrent[$a+5];
						
							$index = $a+7+$index;
							$nameTorrent = "";
							for ($j=$a+7;$j<$index;$j++)
							{
								$nameTorrent .= $alltorrent[$j];
								
							}
							echo " <b>Nom du torrent :</b> $nameTorrent <br />";
							$namefound = true;
					}
					
				}
				//************//trouver l'url  du tracker
				if ($alltorrent[$i] == "e" and $urlfound != true)
				{
					$b = $i;
					$index = $alltorrent[$b+1] . $alltorrent[$b+2];
					$index = $b+4+$index;
					$urlTracker = "";
					for ($j=$b+4;$j<$index;$j++)
					{
						$urlTracker .= $alltorrent[$j];
						
					}
					echo " <b>URL du tracker :</b> $urlTracker <br />";
					$urlfound = true;
					
				}
				//************//trouver la date création du torrent 
				if ($alltorrent[$i] == "d" and $datefound != true)
				{
					$d = $i;
					$datei = $alltorrent[$d] . $alltorrent[$d+1] . $alltorrent[$d+2] . $alltorrent[$d+3].  $alltorrent[$d+4];
					if ($datei == "datei")
					{
	 					$indexdepart = $d+4;
						$timestamp = "";
						for ($j=$indexdepart+1;$j<$indexdepart+11;$j++)
						{
							$timestamp .= $alltorrent[$j];	
						}
						$date = date('d/m/Y à H:i',$timestamp);
						echo "<b>Date de création du .torrent:</b> $date<br/>";
						$datefound = true;
					}
					
				}
				//************//trouver la taille total du fichier
				if ($alltorrent[$i] == 'l')
				{
					$c = $i;
					$checklength = $alltorrent[$c].$alltorrent[$c+1].$alltorrent[$c+2].$alltorrent[$c+3].$alltorrent[$c+4].$alltorrent[$c+5];
					//echo "<b>doit contenir lenght:</b> $checklength";
					if ($checklength == "length")
					{
						
						for ($d=$c+7;$d<$filesTorrentSize;$d++)
						{	
						
						
							//echo " <b>variable parcoure : </b>$alltorrent[$d]";
							if ($alltorrent[$d] == 'e')
							{
								$compt++;
								$tabTailleFichier[$compt]=$tailleFichier;
								$tailleFichier = '';
								break;
							}
							else
							{
								$tailleFichier .= $alltorrent[$d];
								//echo " <b>!!!!taille fichier!!!!!:</b> $tailleFichier";
								
							}
						}					
					}
				}
			}
			$globalPoid=0;
			for ($b=1;$b<=$compt;$b++)
			{
				//echo " >>Fichiers $b et sa tailles : $tabTailleFichier[$b]";
				$globalPoid =  $globalPoid+$tabTailleFichier[$b];
			}
			$calcfilesize = calcfilesize($globalPoid);
			echo " <b>Poids total du dossier :</b> $calcfilesize <br />";
			echo " <b>Nombre de fichiers :</b> $compt <br />";
		}
		

		
		
//*********************************************//main()	
	
if (isset($_FILES["torrent"]))
{
		//is_uploaded_file($_FILES["torrent"]["tmp_name"]) or die("<p class=\"error\">File upload error 2</p>\n");
		$filesTorrent = fopen($_FILES["torrent"]["tmp_name"], "rb") or die(" File check error");
		$filesTorrentSize = filesize($_FILES["torrent"]["tmp_name"]);
		$alltorrent = fread($filesTorrent, $filesTorrentSize);
		
		//************//Verif si le fichier est un torrent
		checktorrent($alltorrent);
		//************//calcul du hash 
		calchash($alltorrent);
		//************//Debut
		parcourirtorrent($filesTorrentSize,$alltorrent);
		//************//End
}

?>

Conclusion :


Ce script utilise pour le calcul du hash, les fonctions BDecode et BEncode écrites par Bram Cohen (le créateur de BitTorrent), réecrites en PHP.
Je debute en php et je suis conscient que la methode utilisée pour le traitement du fichier torrent n'est surement pas la meilleur. Je pense qu'il doit exister des fonctions pour le traitement des chaines de caractères dans les fichiers de données (Byte). Je suis ouvert à toutes les critiques, je pense qu'il y en aura ...
Dernier point pour la création de ce script, je me suis appuyé sur un très bon tutorial disponible à cette adresse : http://www.run.montefiore.ulg.ac.be/~martin/resources/BitTorrentTutoriel.html

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.