<?php
// Public domain, blah blah blah, use it however you like
error_reporting(E_ALL E_STRICT);
header("Content-type: text/html; charset=utf-8");

// Don't judge me.
// TODO: Think of more "bro names"
$bros = <<<BRO
Broba Fett
Brobama
Brodysseus
Broseidon
Brosevelt
Broham
Brotato
Broseph
Brosky
Brosephiroth
Bromine
Broasaurus
Brosiris
Brok\xC3\xA9mon
Brotron
McBrohenheim
Brolax
Brobot
Broethius
Brosef Stalin
Brojob
Brosferatu
Brostradamus
Brohammad
Brometheus
Abroham Lincoln
Bromeister
Brohemoth
Broda
Bromer
Chadbro
BRO;
$bros explode("\n"$bros);

function 
bro() {
    global 
$bros;
    return 
$bros[array_rand($bros)];
}

try {
    
$db = new PDO("mysql:host=DATABASE SERVER;dbname=DATABASE NAME""DATABASE USER""USER PASSWORD");
} catch (
PDOException $e) {
    
printf("Sorry %s, but the database connection failed: %s\n"bro(), $e->getMessage());
    exit();
}

if (!empty(
$_GET["id"]) && ctype_digit($_GET["id"])) {
    
$video $db->query("SELECT * FROM videos WHERE id = " $_GET["id"]);
    
$video->execute();
    
$video->bindColumn("title"$titlePDO::PARAM_STR);
    
$video->bindColumn("file"$filePDO::PARAM_STR);
    
$video->bindColumn("flash"$flashPDO::PARAM_STR);
    
$video->bindColumn("width"$widthPDO::PARAM_INT);
    
$video->bindColumn("height"$heightPDO::PARAM_INT);
    
$video->bindColumn("views"$viewsPDO::PARAM_INT);

    if (!
$video->fetch(PDO::FETCH_BOUND)) {
        
printf("Sorry %s, but that video doesn't exist.\n"bro());
        exit();
    }

    if (!
file_exists($file)) {
        
printf("Sorry %s, but the video file doesn't exists. :\\\n"bro());
        exit();
    }

    
$image explode("."$file);
    
$container strtolower(array_pop($image));
    
$lowquality true;
    
$lowqualityvideo implode("."$image) . ".low." $container;
    
    if (!
file_exists($lowqualityvideo)) {
        
$lowquality false;
        
$lowqualityvideo $file;
    }
    
    
$downloadfile str_replace("%2F""/"htmlspecialchars(rawurlencode($file)));
    
$downloadlqfile str_replace("%2F""/"htmlspecialchars(rawurlencode($lowqualityvideo)));
    
    
$image implode("."$image) . ".jpg";
    
$db->exec("UPDATE videos SET views = " . ++$views " WHERE id = " $_GET["id"]);
    
$title htmlspecialchars($title);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title><?php echo $title?></title>
<?php
    
if ($container == "mov" || $container == "mp4") {
?>
        <script type="text/javascript" src="swfobject.js"></script>
<?php
    
}
?>
        <link rel="stylesheet" href="style.css" type="text/css" media="screen" />
        <!--[if lte IE 7]><link rel="stylesheet" href="ie.css" type="text/css" media="screen" /><![endif]-->

    </head>
    <body id="video">
        <h1><?php echo $title?></h1>
        <div id="videodiv">
<?php
    
switch ($container) {
        case 
"avi":
        case 
"divx":
            
$lowqualityvideo str_replace("%2F""/"htmlspecialchars(rawurlencode($lowqualityvideo)));
            
$image str_replace("%2F""/"htmlspecialchars(rawurlencode($image)));
?>
            <object classid="clsid:67DABFBF-D0AB-41fa-9C46-CC0F21721616" width="<?php echo $width?>" height="<?php echo $height 20?>" codebase="http://go.divx.com/plugin/DivXBrowserPlugin.cab">
                <param name="autoplay" value="false" />
                <param name="src" value="<?php echo $lowqualityvideo?>" />
                <param name="previewimage" value="<?php echo $image?>" />
                <embed type="video/divx" src="<?php echo $lowqualityvideo?>" previewimage="<?php echo $image?>" width="<?php echo $width?>" height="<?php echo $height 20?>" autoplay="false" pluginspage="http://go.divx.com/plugin/download/"></embed>
            </object>
        </div>
<?php
            
break;
        case 
"mov":
        case 
"mp4":
            
$lowqualityvideo addslashes($lowqualityvideo);
            
$image addslashes($image);
?>
            <div id="player"><a href="http://www.macromedia.com/go/getflashplayer">Get Flash!</a></div>
        </div>
        <script type="text/javascript"><!--
            swfobject.embedSWF("player.swf", "player", "<?php echo $width?>", "<?php echo $height?>", "9.0.98", null, {file: "<?php echo $lowqualityvideo?>", image: "<?php echo $image?>", width: "<?php echo $width?>", height: "<?php echo $height?>", controlbar: "over"});
        --></script>
<?php
            
break;
        default:
            break;
    }
?>
        <p id="views"><?php echo number_format($views) . " view" . ($views != "s" ""); ?></p>
<?php
    
if ($lowquality) {
?>
        <p><a href="<?php echo $downloadfile?>">Download High-Quality</a> | <a href="<?php echo $downloadlqfile?>">Download Low-Quality</a><?php if (!empty($flash)) echo " | <a href=\"$flash\">Low-Quality Mirror</a>"?></p>
<?php
    
} else {
?>
        <p><a href="<?php echo $downloadfile?>">Download</a><?php if (!empty($flash)) echo " | <a href=\"$flash\">Low-Quality Mirror</a>"?></p>
<?php
    
}
?>
        <p><a href="index.php">Video Index</a></p>
    </body>
</html>
<?php
} else {
    
$list $db->query("SELECT * FROM videos ORDER BY id ASC");
    
$list->bindColumn("id"$idPDO::PARAM_INT);
    
$list->bindColumn("title"$titlePDO::PARAM_STR);
    
$list->bindColumn("views"$viewsPDO::PARAM_INT);
    
$list->execute();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Toad King's Videos</title>
        <link rel="stylesheet" href="style.css" type="text/css" media="screen" />
    </head>
    <body>
        <h1>Toad King's Videos</h1>
        <dl id="videolist">
<?php
    
while ($list->fetch(PDO::FETCH_BOUND)) {
        
$title htmlspecialchars($title);
?>
            <dt><a href="index.php?id=<?php echo $id?>"><?php echo $title?></a></dt>
            <dd><?php echo number_format($views) . " view" . ($views != "s" ""); ?></dd>
<?php
    
}
?>
        </dl>
    </body>
</html>
<?php
}
?>