<?php
// Filename: youtube.php
// Description: Redirects Youtube url to embedded-ready url
// Author: Daniël van de Giessen aka BlackHole
// URL: http://www.blackholestudios.nl/
// Copyright 2009 - All rights reserved
//

///////////////////////////
///// Input variables /////
///////////////////////////
//
// $url > The source url
// 	For example: "http%3A//www.youtube.com/watch%3Fv%3D8R9wQSvGQDA%26feature%3Dchannel"
// $red > The desired output url, where VIDEO_ID is replaced with the actual ID from $url
// 	For example: "http%3A//www.youtube-nocookie.com/v/VIDEO_ID%26hl%3Dnl%26fs%3D1%26rel%3D0%26color1%3D0x565656%26color2%3D0x565656"
//

/////////////////////////////
///// Using youtube.php /////
/////////////////////////////
//
// Example input: youtube.php?url={content}&red=http%3A//www.youtube-nocookie.com/v/VIDEO_ID%26hl%3Dnl%26fs%3D1%26rel%3D0%26color1%3D0x565656%26color2%3D0x565656
// Example redirect url: http://www.youtube-nocookie.com/v/8R9wQSvGQDA&hl=nl&fs=1&rel=0&color1=0x565656&color2=0x565656
//

//////////////////////////////////////////////
/////        Example embedded code       /////
///// For use with SMF Custom BBC plugin /////
//////////////////////////////////////////////
//
// <object width="480" height="385">
// <param name="movie" value="http://www.advancedcombatclan.nl/forum/youtube.php?url={content}&red=http%3A//www.youtube-nocookie.com/v/VIDEO_ID%26hl%3Dnl%26fs%3D1%26rel%3D0%26color1%3D0x565656%26color2%3D0x565656"></param>
// <param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
// <embed src="http://www.advancedcombatclan.nl/forum/youtube.php?url={content}&red=http%3A//www.youtube-nocookie.com/v/VIDEO_ID%26hl%3Dnl%26fs%3D1%26rel%3D0%26color1%3D0x565656%26color2%3D0x565656" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385">
// </embed></object>
//

if (isset($_GET['url'])) {
        if (isset($_GET['red'])) {
		// Set vars
		$url = $_GET['url'];
		$red = $_GET['red'];
		// Decode urls
		$url = urldecode($url);
		$red = urldecode($red);
		// Get id
		$id = stristr($url, "?v=");
		$id = str_replace("?v=", "", $id);
		$id = trim($id);
		if (strpos($id, "&") != false) $id = substr($id, 0, strpos($id, "&"));
		$id = trim($id);
		// Check $id
		if ($id == "") {
			// $id is empty, so $url is not valid
			header("HTTP/1.0 400 Bad Request");
			die("<h1>Error 400: Bad Request</h1>\r\nArgument <font color=\"#CC0000\">\$url</font> is not valid.");
		}
		// Check $red
		if (strpos($red, "VIDEO_ID") == false) {
			// $red is not valid
			header("HTTP/1.0 400 Bad Request");
			die("<h1>Error 400: Bad Request</h1>\r\nArgument <font color=\"#CC0000\">\$red</font> is not valid.");
		}

		// Set new url
		$newurl = str_replace("VIDEO_ID", $id, $red);
		// Redirect
		header("HTTP/1.0 200 OK");
		header("Location: $newurl");

	} else {
		// No $red given
		header("HTTP/1.0 400 Bad Request");
		die("<h1>Error 400: Bad Request</h1>\r\nArgument <font color=\"#CC0000\">\$red</font> was not provided.");
	}
} else {
	// No $url given
	header("HTTP/1.0 400 Bad Request");
	die("<h1>Error 400: Bad Request</h1>\r\nArgument <font color=\"#CC0000\">\$url</font> was not provided.");
}

?>
