<?php
set_time_limit(60);
if(isset($_POST["url"]) AND $_POST["host"]=="facebook"){
	include("fb.core.php");
	$url = $_POST["url"];
	$fb = new fb($url);
	if($fb->fbvideoid($url) == null){
		sleep(1);
		echo getError("<div id='download-error'>Invalid Facebook URL</div>");
		exit;
	}
	$fb->get_url();
	if($fb->fb_sd == null){
		echo getError("<div id='download-error'>Download link not found.</div>");
		exit;
	}
?>
<div class="col-md-8 mx-auto">
	<div class="row">
	    <div class="col-md-6">
	        <img src="https://graph.facebook.com/<?php echo $fb->id; ?>/picture" class="img-thumbnail">
	    </div>
	    <div class="col-md-6">
	        <h4 class="title mt-4"><?php echo $fb->fb_title; ?></h4>
	    </div>
	</div>
</div>

<div class="row">
    <div class="col-md-12 table-container">
        <p class="table-container-heading">&nbsp;</p>
        <table class="table table-hover">
            <thead class="thead-dark">
                <tr>
                    <th>Quality</th>
                    <th>Size</th>
                    <th>Download</th>
                </tr>
            </thead>
            <tbody><?php echo $fb->fb_url; ?></tbody>
        </table>
    </div>
</div>
<?php
}elseif(isset($_POST["url"]) AND $_POST["host"]=="youtube"){
	include( "yt.core.php" );
	$url = $_POST["url"];
	$yt  = new YTDownloader( $url );
	if ( $yt->get_id( $url ) == false ) {
		sleep( 1 );
		echo getError("Invalid Youtube URL");
		exit;
	}
	$links = $yt->getDownloadLinks();
	if ( $yt->error == true ) {
		echo getError("Failed to get links, please try again.");
		exit;
	}

?>

<div class="row">
    <div class="col-md-6">
        <img src="https://i.ytimg.com/vi/<?php echo $yt->id; ?>/mqdefault.jpg" class="img-thumbnail">
    </div>
    <div class="col-md-6">
        <h4 class="title mt-4"><?php echo $yt->yt_title; ?></h4>
        <p class="mt-4"><strong>Duration:</strong> <?php echo $yt->duration; ?></p>
        
    </div>
</div>

<div class="row">
    <div class="col-md-12 table-container">
        <p class="table-container-heading">&nbsp;</p>
        <table class="table table-hover">
            <thead class="thead-dark">
                <tr>
                    <th>Quality</th>
                    <th>Size</th>
                    <th>Download</th>
                </tr>
            </thead>
            <tbody>
                <?php
                  for ( $i = 0; $i < count( $links ); $i ++ ) {
                  	if ($links[$i]['type']=="MP3") {
                  		$links[$i]["quality"] = $links[$i]["bitrate"];
                  	}
	                 echo "<tr><td>".$links[$i]["quality"]." <small>(".$links[$i]["type"].")</small></td><td>".$links[$i]["size"]."</td><td><form action='yt.php' method='POST' name='fb_yt_grabber_".$i."'><button type='submit' class='btn btn-outline-dark'>Download</button><input type='hidden' name='yt' value='".$links[$i]["url"]."' /></form></td></tr>";
                  }
                ?>
            </tbody>
        </table>
    </div>
</div>
<?php
}elseif ( isset( $_POST["url"] ) AND $_POST["host"] == "twitter" ) {
include("twitter.core.php");
$url = $_POST["url"];
$twitter = new twitter($url);
$twitter->get_tweet();
if($twitter->error==true){
	die($twitter->msg);
}
?>
<div class="row">
    <div class="col-md-6">
        <img src="<?php echo $twitter->image; ?>" class="img-thumbnail">
    </div>
    <div class="col-md-6">
        <h4 class="title mt-4"><?php echo $twitter->title; ?></h4>
        
    </div>
</div>

<div class="row">
    <div class="col-md-12 table-container">
        <p class="table-container-heading">&nbsp;</p>
        <table class="table table-hover">
            <thead class="thead-dark">
                <tr>
                    <th>Quality</th>
                    <th>Download</th>
                </tr>
            </thead>
            <tbody>
                <?php
				for($i=0;$i<count($twitter->links);$i++){
	                echo "<td>".$twitter->links[$i]["quality"]."</td><td><a class='btn btn-outline-dark' href='".$twitter->links[$i]["url"]."'>Download</a></td></tr>";
                  }
                ?>
            </tbody>
        </table>
    </div>
</div>
<?php
} else{
	sleep(1);
	echo getError("<div id='download-error'>We do not support downloading from this site.</div>");
	exit;
}
function getError($msg){
	return '<div class="alert alert-danger alert-dismissible fade show">'.$msg.'</div>';
}
?> 