<?php
/*
Plugin Name: YouTube Related Videos
Plugin URI: http://86400.es/youtube-related/
Description: This plugin provides integration with YouTube videos, to find related videos to your post
Version: 1.0
Author: Alex Dantart
Author URI: http://86400.es

For Installation Instructions, please visit:
http://86400.es/youtube-related/

 */

  // Global variables for function use.
  
  // IMPORTANT: if it's not working, please use your own dev_id creating one in this URL
  // http://www.youtube.com/my_profile_dev.php
  
$GLOBALS['dev_id'] = "e_xieum5R98";
  
$GLOBALS['videos_shown'] = 3;
  
$GLOBALS['url']  = false;
  
$GLOBALS['thumbnail_url'] = false;
  
$GLOBALS['counter']=0;
  
$GLOBALS['middle_html'] = "";
  
  
// function: startElement
  // Deals with the starting element
  
function ytr_startElement$parser$tagName$attrs ) {
    
// By setting global variable of tag name
    // I can determine which tag I am currently
    // parsing.

    
switch( $tagName ) {
      case 
'URL':           $GLOBALS['url'] = true;      break;
      case 
'THUMBNAIL_URL'$GLOBALS['thumbnail_url'] = true;     break;
    }
  }
 
  
// function: endElement
  // Deals with the ending element
  
function ytr_endElement$parser$tagName ) {

    
// By noticing the closing tag,
    // I can print out the data that I want.
    
switch( $tagName ) {
      case 
'URL':
         
$GLOBALS['linkcount']="<td><a href=\""$GLOBALS['linktext'] . "\" style=\"border-bottom: 0;\">";
        
$GLOBALS['url'] = false;
        break;
      case 
'THUMBNAIL_URL':
        if (
$GLOBALS['counter']<$GLOBALS['videos_shown']) {
        
$GLOBALS['middle_html'].=$GLOBALS['linkcount']."<img src='".$GLOBALS['thumbnailtext']."' border=0></td>\n";
    }
    
$GLOBALS['counter']++;
        
$GLOBALS['thumbnail_url'] = false;
    
$GLOBALS['linktext'] = false;
        break;
    }  
  }

  
// function: charElement
  // Deals with the character elements (text)
  
function ytr_charElement$parser$text ) {
    
// Verify the tag that text belongs to.
    // I set the global tag name to true
    // when I am in that tag.

    
if( $GLOBALS['url'] == true ) {
        
$GLOBALS['linktext'] .= trim$text );
    } else if( 
$GLOBALS['thumbnail_url'] == true ) {
        
$GLOBALS['thumbnailtext'] = trim$text );
    }
  }


function 
youtuberelated($thepostid$tag="") {
global 
$wpdb$tablepostmeta;

// This is the simple way to get the tag from Simple Tags
// This is what you can change to take the tag from your "other" plugin for tags

if (!$tag) {
$contenido $GLOBALS["wp_query"];
$contenido $contenido->post->post_content;
if (
ereg("\<tags\>"$contenido)) {
        
$tmp substr($contenidostrpos($contenido,"<tags>")+6);
        
$tmp substr($tmp0strpos($tmp,"</tags>"));
    
$tag = (ereg(",",$tmp))?substr($tmp,0,strpos($tmp,",")):$tmp;
}
}
echo 
"<!-- Grabbing YouTube Videos with tag: $tag //-->";
if (
$tag) {

$start_html '
<h2>Videos YouTube relacionados</h2>
<table bgcolor="#E7E7E7" cellpadding="3"><tr>
'
;

  
// Create an xml parser
  
$xmlParser xml_parser_create();

  
// Set up element handler
  
xml_set_element_handler$xmlParser"ytr_startElement""ytr_endElement" );

  
// Set up character handler
  
xml_set_character_data_handler$xmlParser"ytr_charElement" );

  
// Open connection to RSS XML file for parsing.
  
$feed="http://www.youtube.com/api2_rest?method=youtube.videos.list_by_tag&dev_id=".$GLOBALS['dev_id']."&tag=".urlencode($tag);
  
$fp fopen($feed"r" )
    or die( 
"Cannot read RSS data file." );

  
// Parse XML data from RSS file.
  
while( $data fread$fp4096 ) ) {
    
xml_parse$xmlParser$datafeof$fp ) );
  }

  
// Close file open handler
  
fclose$fp );
  
// Free xml parser from memory
  
xml_parser_free$xmlParser );


  
$end_html '</tr></table><a href="http://www.youtube.com/results.php?search='.urlencode($tag).'&search_videos=Search+Videos">M&aacute;s videos de '.$tag.' en YouTube</a><br /><br />';

if (
$GLOBALS['middle_html']) {
    echo 
$start_html;
    echo 
$GLOBALS['middle_html'];
    echo 
$end_html;
}

$GLOBALS['counter']=0;
$GLOBALS['out']="";

}


}

?>