<?php
require_once('includes/config.php');

//Titre de la page
$pagetitle = 'Liste des torrents';

//Stats : bbclone
define("_BBC_PAGE_NAME", $pagetitle);
define("_BBCLONE_DIR", "bbclone/");
define("COUNTER", _BBCLONE_DIR."mark_page.php");
if (is_readable(COUNTER)) include_once(COUNTER);

include_once('includes/header.php');
?>

<body>

<div id="container">

   	<?php
	   include_once('includes/header-logo.php');
	   include_once('includes/nav.php');
	?>

    	<div id="body">
	<div id="content">

	<?php include_once('includes/ariane.php'); ?>

	<br />

	<div style="text-align: center;"><span style="font-size: 14pt; font-weight: bold;">Liste des torrents :</span></div>
	<br />	
	
        <?php
        // On affiche : torrent ajouté ! 
        if(isset($_GET['action']) && $_GET['action'] == 'ajoute') {
                echo '<div class="alert-msg rnd8 success">Le torrent a été ajouté avec succès !</div>';
        }

	// Pas d'accès direct à la page download sans file ID
        if(isset($_GET['action']) && $_GET['action'] == 'nodirect') {
                echo '<div class="alert-msg rnd8 error">ERREUR :<br />Vous ne pouvez pas accéder directement à cette page sans préciser le torrent à télécharger...</div>';
        }

	// Pas d'accès à la page download si le file ID n'existe pas
        if(isset($_GET['action']) && $_GET['action'] == 'noexist') {
                echo '<div class="alert-msg rnd8 error">ERREUR :<br />Ce torrent n\'existe pas !</div>';
        }

	//On affiche le message de suppression
	if(isset($_GET['delpost'])){
		echo '<div class="alert-msg rnd8 success">Le torrent a été supprimé avec succès.</div>';
	}
	?>

        <table>
        <tr>
                <th style="width:50%;"><a style="color: #fff; text-decoration: none;" href="torrents.php?tri=postTitle&ordre=desc">&#x2191;</a>Nom<a style="color: #fff; text-decoration: none;" href="torrents.php?tri=postTitle&ordre=asc">&#x2193;</a></th>
		<th style="text-align: center; width: 8%;"><a style="color: #fff; text-decoration: none;" href="torrents.php?tri=postTaille&ordre=desc">&#x2191;</a>Taille<a style="color: #fff; text-decoration: none;" href="torrents.php?tri=postTaille&ordre=asc">&#x2193;</a></th>
		<th style="text-align: center; width: 8%;"><a style="color: #fff; text-decoration: none;" href="torrents.php?tri=postDate&ordre=desc">&#x2191;</a>Ajouté<a style="color: #fff; text-decoration: none;" href="torrents.php?tri=postDate&ordre=asc">&#x2193;</a></th>
		<!-- <th style="text-align: center;"><a style="color: #fff; text-decoration: none;" href="torrents.php?tri=postAuthor&ordre=desc">&#x2191;</a>Par<a style="color: #fff; text-decoration: none;" href="torrents.php?tri=postAuthor&ordre=asc">&#x2193;</a></th> -->
		<th style="text-align: center; width: 20%;">Catégorie(s)</th>
		<th style="text-align: center;"><a style="color: #fff; text-decoration: none;" href="torrents.php?tri=seeders&ordre=desc">&#x2191;</a>S<a style="color: #fff; text-decoration: none;" href="torrents.php?tri=seeders&ordre=asc">&#x2193;</a></th>
		<th style="text-align: center;"><a style="color: #fff; text-decoration: none;" href="torrents.php?tri=leechers&ordre=desc">&#x2191;</a>L<a style="color: #fff; text-decoration: none;" href="torrents.php?tri=leechers&ordre=asc">&#x2193;</a></th>
		<th style="text-align: center;"><a style="color: #fff; text-decoration: none;" href="torrents.php?tri=completed&ordre=desc">&#x2191;</a>T<a style="color: #fff; text-decoration: none;" href="torrents.php?tri=completed&ordre=asc">&#x2193;</a></th>
        </tr>
        <?php
                try {
			// On affiche 15 torrents par page
			$pages = new Paginator(NBTORRENTS,'page');

			$stmt = $db->query('SELECT postHash FROM blog_posts_seo');
			$pages->set_total($stmt->rowCount());


			// On met en place le tri--------------------------------------------
			if(isset($_GET['tri'])) {
				$tri = html($_GET['tri']);
			}
			else {
				$post_tri = 'postDate';
				$tri = html($post_tri);
			}

			if(isset($_GET['ordre'])) {
				$ordre = html($_GET['ordre']);
			}
			else {
				$ordre_tri = 'desc';
				$ordre = html($ordre_tri);
			}

			// Protection du tri -----------------------------------------------
			if (!empty($_GET['tri']) && !in_array($_GET['tri'], array('postID','postHash', 'postTitle', 'postViews', 'postTaille', 'postDate', 'postAuthor', 'seeders', 'leechers', 'completed'))) {
				header('Location: index.php');
				exit();
			}
			if (!empty($_GET['ordre']) && !in_array($_GET['ordre'], array('asc','desc','ASC','DESC'))) {
				header('Location: index.php');
				exit();
			}


                        $stmt = $db->query('SELECT * FROM blog_posts_seo b LEFT JOIN xbt_files x ON x.fid = b.postID ORDER BY '.$tri.' '.$ordre.' '.$pages->get_limit());

	                while($row = $stmt->fetch()){

				$stmt2 = $db->prepare('SELECT catTitle, catSlug FROM blog_cats, blog_post_cats WHERE blog_cats.catID = blog_post_cats.catID AND blog_post_cats.postID = :postID ORDER BY catTitle ASC');
				$stmt2->execute(array(':postID' => $row['postID']));
			
			$catRow = $stmt2->fetchAll(PDO::FETCH_ASSOC);

			echo '<tr>';
                        echo '<td style="font-size: 14px; font-weight: bold;"><img src="'.SITEURL.'/images/imgtorrents/'.$row['postImage'].'" style="width:25px; height:25px; vertical-align:middle;" alt="" /> <a style="text-decoration:none; color:black;" href="'.html($row['postSlug']).'">'.html($row['postTitle']).'</a>';
			echo '</td>';
			echo '<td style="text-align: center; font-size: 9px;">';
				echo html(makesize($row['postTaille']));
			echo '</td>';

			sscanf($row['postDate'], "%4s-%2s-%2s %2s:%2s:%2s", $annee, $mois, $jour, $heure, $minute, $seconde);
			echo '<td style="text-align: center; font-size: 7pt;">'.$jour.'-'.$mois.'-'.$annee.'</td>';
			
			//echo '<td style="font-size: 8pt; text-align: center;"><a style="text-decoration: none;" href="admin/profil.php?membre='.html($row['postAuthor']).'">'.html($row['postAuthor']).'</a></td>';

			$links = array();
			foreach ($catRow as $cat) {
                               	$links[] = '<a style="text-decoration: none;" href="c-'.$cat['catSlug'].'">'.$cat['catTitle'].'</a>';
                        }

			$max = 120;
			$chaine = implode(", ", $links);
			if (strlen($chaine) >= $max) {
				$chaine = substr($chaine, 0, $max);
				$espace = strrpos($chaine, ", ");
				$chaine = substr($chaine, 0, $espace).' ...';
			}

			echo '<td style="text-align: center; font-size: 10px; line-height: 120%;">'.$chaine.'</td>';

			$exa = '0x';
			$hash = $exa.$row['postHash'];
			
			$stmt3 = $db->prepare('SELECT * FROM blog_posts_seo,xbt_files WHERE blog_posts_seo.postHash = :postHash AND xbt_files.info_hash = '.$hash);
			$stmt3->execute(array(':postHash' => $row['postHash']));
			$xbt = $stmt3->fetch();

			echo '<td style="text-align:center;"><a style="text-decoration: none; color:green; font-size:14px; font-weight:bold;" href="peers.php?hash='.html($row['postHash']).'">'.$xbt['seeders'].'</a></td>';
			echo '<td style="text-align:center;"><a style="text-decoration: none; color:red; font-size:14px; font-weight:bold;" href="peers.php?hash='.html($row['postHash']).'">'.$xbt['leechers'].'</a></td>';
			echo '<td style="text-align:center; font-size: 8pt;">'.$xbt['completed'].'</td>';

			echo '</tr>';
                        }


                } catch(PDOException $e) {
                    echo $e->getMessage();
                }
        ?>
        </table>

	<br />

	<?php echo $pages->page_links('?tri='.$tri.'&ordre='.$ordre.'&'); ?>

	<br />
        <p class="edito" style="font-size: 10px; font-style: italic;"><span style="font-weight: bold;">Légende :</span> S = Nb de Seeders, L = Nb de Leechers, T = Nb de Téléchargements</p>

</div>

	<?php include_once('sidebar.php'); ?>
        
    	<div class="clear"></div>
    </div>
</div>

<div id="footer">
	<?php include_once('includes/footer.php'); ?>
</div>

</body>
</html>