<?php 
// YouTube Favorites RSS with video enclosures made by Walter Rafelsberger (http://www.rafelsberger.at) using Dapper & PHP
// This script is free. However, it would be nice if you let me know where you use it.

// For this to work you need:
// The Dapper API
require_once(dirname(__FILE__).'/dapp/Dapp.php');
// - "Favorites YouTube Videos 2" from Dapper (http://www.dapper.net/dapp-howto-use.php?dappName=FavoriteYouTubeVideos2)
$dapp = "FavoriteYouTubeVideos3";
// - Set a default username (may be overriden with "?user=..." in the URL
$username= "walterra";
// - If you're hosting this yourself please adapt the webmaster info
$webmaster = "walter@rafelsberger.at (Walter Rafelsberger)";


if (isset($_GET["user"])) {
    // check user for possible exploits
            $user = trim(stripslashes(strip_tags($_GET["user"])));
    $exploits = "/(content-type|bcc:|cc:|document.cookie|onclick|onload)/i";
    if (preg_match($exploits, $user)) die();
    $username = $user;
}


date_default_timezone_set('UTC');
header('Content-type: application/xml');
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <link>http://www.youtube.com/profile_favorites?user=<?php echo $username; ?></link>
	<atom:link href="http://www.metaportaldermedienpolemik.net/youtube_favorites.php?user=<?php echo $username; ?>" rel="self" type="application/rss+xml" />       
	<title><?php echo $username; ?>'s Favorite YouTube Videos</title>
        <description><?php echo $username; ?>'s Favorite YouTube Videos</description>
        <webMaster><?php echo $webmaster; ?></webMaster>
<?php

// The Dapp constructor throws Exceptions - so we should catch it
try {
  // $dappO = new Dapp($dapp, null, array($username));
  $dapp_url = "http://www.dapper.net/RunDapp?dappName=FavoriteYouTubeVideos3&v=1&v_username=".$username;
  //$xml = new SimpleXMLElement($dappO->getXML());
  $xml = simplexml_load_file($dapp_url);
  // var_dump($dappO);
  foreach ($xml->Favorite_Movie as $movie) {
        $video = explode("?v=", $movie->Thumbnail["href"]);
        $video = $video[1];
        $length = explode(":", $movie->Length);
        $length = ($length[0] * 60 + $length[1]) * 32768;
        
        echo "<item>\n<title>".$movie->Movie_Title."</title>\n";
        echo "<link>".$movie->Thumbnail["href"]."</link>\n";
        echo "<guid>".$movie->Thumbnail["href"]."</guid>\n";
        echo "<description>&lt;a href=\"http://www.youtube.com/v/".$video."\"&gt;&lt;img src=\"".$movie->Thumbnail["src"]."\" /&gt;&lt;/a&gt;</description>\n";
        echo "<enclosure url=\"http://youtube.com/v/".$video.".swf\" length=\"".$length."\" type=\"application/x-shockwave-flash\" />\n";
        echo "</item>\n";
  }
}
catch (Exception $e) {
  echo 'Exception: '.get_class($e)."\nMessage: ".$e->getMessage()."\n";
}

?>
</channel>
</rss>