<?php
/*
# ------------------------------------------------------------------------
# Gear Youtube Playlist Player
# ------------------------------------------------------------------------
# Copyright (C) 2015 http://joomlagear.org/. All rights reserved.
# @license http://www.gnu.org/licenseses/gpl-3.0.html GNU/GPL
# Author: Joomla Gear
# Websites: http://joomlagear.org
# ------------------------------------------------------------------------
*/
// no direct access
//JError::raiseWarning(500, print_r($params,true));

defined('_JEXEC') or die('Restricted access');
abstract class modGearYoutubePlaylistHelper {
public static function getContent(&$params, $mid = '') {	
		$mainframe = JFactory::getApplication();
		$doc = JFactory::getDocument();
		//used for javascript
		$theId = $mid; 
		$theWrapper = $mid.'-wrapper';
		$thePlaceholder = $mid.'-placeholder';

		$width = $params->get('width','425');
		$height = $params->get('height','355');
		$width3 = $params->get('imagewidth','50');
		$numimage3  = $params->get('numimage','4');
		
		$height3  = floor($width3*3/4);
		$widths = $params->get('sl_imagewidth','50');
		$heights = floor($widths*3/4);
		$numimages = $params->get('sl_numimage','4');

		$allowfullscreen = $params->get('allowfullscreen','1');
		$randomplay = $params->get('randomvideoplay',1);	


		$autoplay = $params->get('autoplay',0);
		$disp_style = 'image'; //Display style - 0 number list, 1-Drop list, 2-image list, 3-image list slider
		$hd_jwplay_on = $params->get('hd_jwplay_on',0); //0: Normal 1: HD Playing	  
		$playlist = intval($params->get('playlist','1')); //0:video 1:Playlist 2:User
		$cached_time = $params->get('cached_time',0);
		$position = $params->get('position',0); //0 above; 1: Below; 2: Customize
	
		$playerskin = $params->get('playerskin',0);
		$playlist_position = $params->get('playlist_position','bottom');
		$playlist_size = $params->get('playlist_size','320');
		if($autoplay) $theAutostart = 'true'; else $theAutostart = 'false';
/******************************************************************************************************/		
		
		$videotype = array(0=>'video', 1 => 'playlist', 2 => 'user', 3=>'combination');
		$videolist = array(0=>'vid', 1=>'vpid', 2=>'vuid', 3=>'ytid');
		if(!$params->get($videolist[$playlist],0)) return JText::_('MISSING_ID');

		$theoptions['type'] = $videotype[$playlist];
		$theoptions['mid'] = $mid;
		$theoptions['source'] = $videolist[$playlist];
		$theoptions['autostart'] = $theAutostart;
		$theoptions['divid'] = $mid;
		$theoptions['playlist_items'] = $params->get('playlist_items','25'); //$playlist_items
		$theoptions['imagewidth'] = $params->get('imagewidth','50');

		//Built the list
		$youtube_list = modGearYoutubePlaylistHelper::built_video_list($params, $theoptions);		

/******************************************************************************************************/
		//default video for playing NOW
		$defaultvideo = new stdClass;
		$defaultvideo->type = $theoptions['type'];
		$i = 0;
		if($randomplay)
			do {
			$t_id = rand(0,$youtube_list->items-1); //0 Choose a video
			$defaultvideo->video = $youtube_list->videos[$t_id];
			$defaultvideo->title = $youtube_list->titles[$t_id];
			$defaultvideo->image = $youtube_list->images[$t_id];
			if(!is_null($youtube_list->types)) $defaultvideo->type = $youtube_list->types[$t_id];
			if($i++ == 5) break;
			} while ($defaultvideo->video == '...');
		else {
			$defaultvideo->video = $youtube_list->videos[$i];
			$defaultvideo->title = $youtube_list->titles[$i];
			$defaultvideo->image = $youtube_list->images[$i];
			if(!is_null($youtube_list->types)) $defaultvideo->type = $youtube_list->types[$i];
		}	
		$theoptions['search'] = $defaultvideo->video;
		$theoptions['type'] = $defaultvideo->type;
	/***********************************************************************************/
    //Number list - 0
	//Drop list - 1
	//built_control($params, $youtube, $theoptions)
	//image list
	//Load default script and Style	

		modGearYoutubePlaylistHelper::InitScriptAndStyle($theoptions);		
		
		$data_init = modGearYoutubePlaylistHelper::InitDefaultPlaying($params, $defaultvideo, $theoptions);
		$html = $data_init[0];
		$defaultInitPlayer  = $data_init[1];
/*****************************************************/
	$playid_list_p = $playid_list = '';	

	$playid_list .= modGearYoutubePlaylistHelper::built_control($params, $youtube_list, $theoptions);
	$playid_video_description = '<div id="'.$mid.'-youtubevideo-description">'.JText::_('PLAYING_DESCRIPTION').$defaultvideo->title.'</div>';

	$textb4 = $params->get('before_string');
	$textat = $params->get('after_string'); //nl2br

	switch($position){
		case 1: //below
			$playlist_ctrl = '';
			$html = $html .$playid_video_description.$playlist_ctrl. 
			'<div id="'.$mid.'-youtubevideo-playlist"><div class="mod_gear_youtube_playlist_player" id="'.$mid.'-playlist">Loading...</div>'. $playid_list.'</div>';	
		break;
		case 2:
			$ctrl_text = array('box_number', 'box_droplist', 'box_image', 'box_image_slider', 'box_image_hidden', 'playing_control_text_button','playing_control_image_button','video_description');
				for($i = 0; $i < 2; $i++){
				//Text before then after
					if($i == 0) $t = $textb4;else $t = $textat;
		

					foreach($ctrl_text as $item_text){
						$code = '{playlist_'.$item_text.'}';
						if(strpos($t, $code)!== false){
							switch($item_text){
								case 'box_number':
									$params->set('ytstyle','number');
									$t = str_replace($code,modGearYoutubePlaylistHelper::built_control($params, $youtube_list, $theoptions),$t);
								break;
								case 'box_droplist':
									$params->set('ytstyle','drop');
									$t = str_replace($code,modGearYoutubePlaylistHelper::built_control($params, $youtube_list, $theoptions),$t);
								break;
								case 'box_image':
									$params->set('ytstyle','image');
									$t = str_replace($code,modGearYoutubePlaylistHelper::built_control($params, $youtube_list, $theoptions),$t);
								break;	
								case 'video_description':
									//$playid_video_description
									$t = str_replace($code,$playid_video_description,$t);
								break;
								}
							}
						}
					if($i == 0) $textb4 = $t;
					else $textat = $t;
				}
		break;
		default: //0: above
			$playlist_ctrl = '';
			$html = '<div id="'.$mid.'-youtubevideo-playlist">' . $playid_list.$playid_list_p.'</div>'.
				$playlist_ctrl. $playid_video_description . $html;
			//$html = $playid_list . '<br />' . $html;	

	}
			if($textb4) {$textb4 = str_replace('{','<',$textb4);$textb4 = str_replace('}','>',$textb4);}
			if($textat) {$textat = str_replace('{','<',$textat);$textat = str_replace('}','>',$textat);}		
	return $textb4.$html.$textat.$defaultInitPlayer; //. modGearYoutubePlaylistHelper::justtest();
   }
	
	public static function built_video_list(& $params, $theoptions){
		
		$videolist = $types = $videos = $titles = $images = array();
		$thevideolist = $params->get($theoptions['source']);
		$cached_time = $params->get('cached_time',0);
		$playlistitems = $params->get('playlist_items',30);
		$theparams = '';	

		$type 	= 	$theoptions['type'];
		$mid 	=	$theoptions['mid'];
		
		$videolist = preg_split('/\n|<br \/>/', $thevideolist); //[\n,] tach cho ca \n va , 
		//Get video list
		
		
		//$cached_time = 0;
		if($theoptions['type'] == 'combination'){
			$seq = 3;
			$seq_type = 1;
			$seq_id = 1; //combination
			$seq_title = 2;
		}else {
			$seq = 2;
			$seq_type = 0;
			$seq_id = 0;//id items
			$seq_title = 1;			
		}	
		
		$nitems = 0;		
			for ($i = 0; $i < count($videolist); $i++) {
				$temp = explode($params->get('split_letter','|'),trim($videolist[$i]),$seq);
				
				if($seq_type) $type = @$temp[0];				
				
				$vid_id = modGearYoutubePlaylistHelper::getPattern(@$temp[$seq_id],$type); //type: video, playlist, user					
				if(empty($vid_id)) continue;
					switch ($type) {
						case 'video':
							$video_info = modGearYoutubePlaylistHelper::getVideoInfo($vid_id);
							if(!isset($temp[$seq_title])) $temp[$seq_title] = $video_info['title'].' ('.$video_info['length'].')';
							$image_url = $video_info['thumbnail'];
							break;
						case 'playlist':
							$video_info = modGearYoutubePlaylistHelper::getpVideoInfo($vid_id);
							if(!isset($temp[$seq_title])) $temp[$seq_title] = $video_info['title'].' ('.$video_info['length'].' videos)';
							$image_url = $video_info['thumbnail'];
							break;
						case 'user':
							$t_ = modGearYoutubePlaylistHelper::getUserInfo($vid_id, 'title');
							if(!isset($temp[$seq_title])) $temp[$seq_title] = JText::_('User: ').$vid_id." ({$t_}) - ".JText::_('CLICK_TO_PLAY');
							$image_url = modGearYoutubePlaylistHelper::getUserInfo($vid_id, 'thumb');
							break;
						default:
							$temp[$seq_title] = JText::_('CLICK_TO_PLAY');
							$image_url = 'http://www.youtube.com/img/pic_youtubelogo_123x63.gif';
					}

				if($seq_type) $types[] = $type;
				$videos[]= $vid_id;
				
				$t_ = htmlspecialchars_decode ($temp[$seq_title]);
				
				$titles[] = htmlspecialchars ($t_,ENT_COMPAT); //special chars: ", &, <, >
				
				$nitems++;
				if($image_url) $images[] = $image_url;
				else { //If image not found, the playlist ID there is no video.
					array_pop($titles); 
					array_pop($videos);
					if($seq_type) array_pop($types); 
					$nitems--;
					}
			}
			$youtube = new stdClass;
			$youtube->videos = $videos;
			$youtube->titles = $titles;
			$youtube->images = $images;
			if($seq_type) //$theoptions['type'] == 'combination'
				$youtube->types = $types;
				else $youtube->types = null;
			$youtube->type = $theoptions['type'];
			
			$youtube->items = $nitems;
		
		$items = $youtube->items;
		$randomitems = $params->get('randomitems',0);
		$rand_list = ($params->get('randomlist',0)==1) && ($randomitems>0) && ($randomitems < $items);
		if($rand_list){
			$rand_keys = array_rand($youtube->videos, $randomitems);
			$t = array();
			$t = $youtube->videos;
			$youtube->videos = array();
			foreach($rand_keys as $key)$youtube->videos[] = $t[$key];
			
			$t = $youtube->titles;
			$youtube->titles = array();
			foreach($rand_keys as $key)$youtube->titles[] = $t[$key];
			
			$t = $youtube->images;
			$youtube->images = array();
			foreach($rand_keys as $key)$youtube->images[] = $t[$key];
			
			if($youtube->type == 'combination'){
				$t = $youtube->types;
				$youtube->types = array();
				foreach($rand_keys as $key)$youtube->types[] = $t[$key];
			}
			$youtube->items = $randomitems;
		}

			
		return $youtube;
	}

	public static function built_control(&$params, $youtube, $theoptions, $style = '') {
		$doc = JFactory::getDocument();
		
		$count = $youtube->items - 1;
		$mid = $theoptions['mid'];
		$page = $theoptions['type'];
		
		
		
		$width	= $params->get('width','425');
		$height = $params->get('height','355');
		$theSkin =  $params->get('playerskin',0);
		$playing_list = '';
		if($style == '') $style = $params->get('ytstyle','number');
		switch ($style){
		case 'number':
			$playid_list_title = JText::_('PLAYLIST');
			if($count>=0)
			for ($i = 0; $i <= $count; $i ++) {
				if(!is_null($youtube->types)) $page = $youtube->types[$i];
				$playing_list .='<a href="javascript:void(0)" onclick="initPlayer(\''.$mid.'\',\''.$page.'\',\''. $youtube->videos[$i] .'\',\''.$width.'\',\''. $height.'\',true); document.getElementById(\''.$mid.'-youtubevideo-description\').innerHTML=\''.htmlspecialchars(JText::_('PLAYING_DESCRIPTION'),ENT_QUOTES).'\' + this.title;  return false;" title="'.$youtube->titles[$i].'">'.$i.'</a> ';
				if($i < $count) $playing_list .='| ';
			}
			$playing_list = $playid_list_title . $playing_list;
			break;
		case 'drop':
			
			$playing_list ='<form name="'.$mid.'youtube-droplist" action="#" method="get"> 
			<select name="droplist" onchange="'.
			'initPlayer(\''.$mid.'\',\''. $page.'\',this.form.droplist.value,\''.$width.'\',\''. $height.'\',true);document.getElementById(\''.$mid.'-youtubevideo-description\').innerHTML=\''.htmlspecialchars(JText::_('PLAYING_DESCRIPTION'),ENT_QUOTES).'\' + this.form.droplist.options[this.form.droplist.selectedIndex].text;">
			<option value="...">'.JText::_('PLAYLIST_SELECTION').'</option>'; 
			for ($i = 0; $i <= $count; $i ++) {
				if(!is_null($youtube->types)) $pagex = '|'.$youtube->types[$i]; else $pagex = '';
				$playing_list .="\n".
				'<option value="'.$youtube->videos[$i].$pagex.'">'.$youtube->titles[$i].'</option> ';
			}
			$playing_list .="\n".'</select>';
			$playing_list .="\n".'</form>'."\n";
			break;
		case 'image':
		
			$width3 = $params->get('imagewidth','50');
			$numimage3  = $params->get('numimage',4);
			$height3  = floor($width3*3/4);
			$width2 = $height2 = $params->get('user_imagewidth','50');
			$divid =' '.$theoptions['divid'];
			if($numimage3==0) $numimage3 =  $count + 1;
			

			$playing_list ='<div class="mod_gear_youtube_playlist_player">';
			for ($i = 0; $i <= $count; $i ++) {
				if(!is_null($youtube->types)) $page = $youtube->types[$i];
					else $page = $theoptions['type'];
				if($page == 'user') $bstyle = " style=\"height: {$height2}px; width: {$width2}px;\" ";
				else $bstyle = " style=\"height: {$height3}px; width: {$width3}px;\" ";
				$video_id = $youtube->videos[$i];
				$title = str_replace("'", "\'",$youtube->titles[$i]);
				
				$playing_list .='<a href="javascript:void(0)" onclick="initPlayer(\''.$mid.'\',\''. $page.'\',\''.$video_id.'\',\''.$width.'\',\''. $height.'\',true); document.getElementById(\''.$mid.'-youtubevideo-description\').innerHTML=\''.htmlspecialchars(JText::_('PLAYING_DESCRIPTION'),ENT_QUOTES).$title.'\'; return false;" title="'.$title.'"><img'.$bstyle.'src="'.$youtube->images[$i].'" alt="" /></a>'; //alt = "" 
			 $k = $i + 1;
			if(($k % $numimage3) == 0) $playing_list .='<br />'; 
			}
			$playing_list .='</div>';
			break;
			}
		return $playing_list;
			
	}

  
public static function InitScriptAndStyle($theoptions){
	$mainframe = JFactory::getApplication();
	$doc = JFactory::getDocument();
		$playlist_items = $theoptions['playlist_items']; //$playlist_items
		$width3 = $theoptions['imagewidth'];
		$height3 = floor($width3*3/4);
	JHTML::script( 'modules/mod_gear_youtube_playlist_player/js/player.js');
	JHTML::stylesheet( 'modules/mod_gear_youtube_playlist_player/css/style.css');
	$mid = $theoptions['mid'];
	$myscript = "\nvar theOptions = Array();\nvar theCurrentPlayingId = Array();\nvar ajaxcontentId = '';";
	$myscript .= "\ntheOptions['image'] = '" . JURI::base() . 'modules/mod_gear_youtube_playlist_player/images/\';';
	if(!$mainframe->get( 'load_youtubeplaylist', false )){
		//Fixed for multi modules
		$doc->addScriptDeclaration($myscript);
		$mainframe->set( 'load_youtubeplaylist', true );
	}
	$myscript = "\ntheCurrentPlayingId['".$theoptions['mid']."']= '".$theoptions['search']."';\ntheOptions['{$mid}iwidth'] = {$width3};\ntheOptions['{$mid}iheight'] = {$height3};";
	if($theoptions['type']!='video'){
		$myscript .= "\ntheOptions['".$theoptions['mid']."ypage']= 1;";
		$myscript .= "\ntheOptions['".$theoptions['mid']."yresult']= ".$theoptions['playlist_items'].";";
		$myscript .= "\ntheOptions['".$theoptions['mid']."showNP']= 0;";
	}
	$doc->addScriptDeclaration($myscript);			
}
public static function InitDefaultPlaying(&$params, $defaultvideo, $theoptions){ 
		$doc = JFactory::getDocument(); 
		
		$theId = $theoptions['mid'];
		$width = $params->get('width');
		$height = $params->get('height');
		$theAutostart = $theoptions['autostart'];
		$theWrapper = $theId.'-wrapper';
		$thePlaceholder = $theId.'-placeholder';
		$playlist_position = $params->get('playlist_position','bottom');
		$playlist_size = $params->get('playlist_size','320');
		
		$html = array();
	    $html[] = "\n\t<div id=\"{$theWrapper}\">\n\t\t<div id=\"{$thePlaceholder}\">".
		JText::_('Loading Player...')
		."\n\t\t</div>\n\t</div>";
		if($defaultvideo->type != 'video'){			
			$myscript = "\ntheOptions['".$theId."playlist_position']='{$playlist_position}';";
			$myscript .= "\ntheOptions['".$theId."playlist_size']='{$playlist_size}';\n";
			$doc->addScriptDeclaration($myscript);
			}

			$html[] = "\n\t<script type=\"text/javascript\">initPlayer('{$theId}', '{$defaultvideo->type}', '{$defaultvideo->video}', '{$width}', '{$height}','{$theAutostart}','init');</script>\n";
		return $html;
}
public static function get_1stthumbnail($page, $playlistid){
	if($page == 'user' || $page == 'user-playlist') $url = "http://gdata.youtube.com/feeds/api/users/{$playlistid}";
	else $url = "http://gdata.youtube.com/feeds/api/playlists/{$playlistid}?v=2";
	$html = modGearYoutubePlaylistHelper::loadhtml($url);
	$returnValue = preg_match('@media:thumbnail url=\'(.*?)\'@', $html, $matches);
	unset($html);
	if($matches) return $matches[1];
	else return 'http://www.youtube.com/img/pic_youtubelogo_123x63.gif';
}

public static function getPattern($url, $type){
		switch($type){
		//0:video 1:Playlist 2:User 3:Combination
		case 'video':
			$id = modGearYoutubePlaylistHelper::getPatternFromUrl($url);
			break;
		case 'playlist':
			$id =  modGearYoutubePlaylistHelper::getPatternFromPUrl($url);
			break;	
		case 'user':
		case 'user-playlist':	
			$id = modGearYoutubePlaylistHelper::getPatternFromUserUrl($url);
			break;
		default:
			$id = '...';
		}
	return $id;
}
protected function getPatternFromUrl($url)
{
	$url = $url.'&';
	//http://www.youtube.com/watch?v=wsP6P5B05A4&feature=feedu
	//http://www.youtube.com/watch?v=vxaGIMKdOfw&feature=player_embedded#!
	$pattern = '/v=(.+?)&+/';
	preg_match($pattern, $url, $matches);
	if(!@$matches[1]) { 
		//http://www.youtube.com/user/VOALearningEnglish#p/c/46706B4B3D2E6C5E/3/wsP6P5B05A4
		$pattern = '@&(.+?)\/+@';
		preg_match($pattern, strrev($url), $matches);
			if(!@$matches[1]) { 
				//wsP6P5B05A4
				$url = 'v='.$url;
				$pattern = '/v=(.+?)&+/';				
				preg_match($pattern, $url, $matches);
				}
			else $matches[1] = strrev ($matches[1]);
		}
	//echo $matches[1]; die;
	return (@$matches[1]);
}

protected function getPatternFromPUrl($url){
	//$url = str_replace('https','http', $url);
	$findme   = '://';
	$pos = strpos($url, $findme);
	$data = $url;
	if ($pos !== false) {
		$url .= '/';
		$pattern = '@c\/(.+?)\/+@';
	
		preg_match($pattern, $url, $matches);
		if(!@$matches[1]) {
			$pattern = '@p=(.+?)\/+@';
			preg_match($pattern, $url, $matches);
					if(!@$matches[1]) {
					//http://www.youtube.com/playlist?list=PLF9D072436E8F2DB8&feature=viewall
					$pattern = '@list=PL(.+?)&+@';
					preg_match($pattern, $url, $matches);
						if(!@$matches[1]) {
						//http://www.youtube.com/watch?v=vIs87n8cYNY&playnext=1&list=PL6EF8A28A655B431C
						$pattern = '@list=PL(.+?)\/+@';
							preg_match($pattern, $url, $matches);						
								if(!@$matches[1]) {
								//http://www.youtube.com/user/SuperSimpleSongs#grid/user/B532F4C0AEFCEC20
									$pattern = '@grid/user/(.+?)\/+@';
									preg_match($pattern, $url, $matches);
								}							
						}
				}						
			}
			if(@$matches[1] && ((strlen($matches[1])==16)||(strlen($matches[1])==32))) $data = 'PL'.$matches[1];			
	}
	return $data;
} 

public static function getPatternFromUserUrl($url){
	$findme   = 'http://';
	$pos = strpos($url, $findme);
	$data = $url;
	if ($pos !== false) {
		$url = strtr($url,'?','#'); //for ******
		if(strpos($url, '#')=== false) $url .= '#gdata';
		if(strpos($url, 'profile?user=')=== false) $pattern = '@user\/(.+?)\#+@';
		else $pattern = '@user=(.+?)\#+@';


		preg_match($pattern, $url, $matches);
		if(@$matches[1]) $data = $matches[1];
	}
	//echo $matches[1]; die;
	return $data;
} 	
public static function getUserInfo($userid, $info='title') {
//title|channel|thumb
//modGearYoutubePlaylistHelper::getUserInfo($userid, $info='title')
	$url = 'https://www.googleapis.com/youtube/v3/channels?forUsername='.$userid.'&part=snippet&key=AIzaSyDgUkAEPr-DkH9tmzJafc_td9S_c5EmT2U';
	//https://developers.google.com/youtube/v3/docs/channels
	$data = modGearYoutubePlaylistHelper::loadhtml($url);
	$thedata = json_decode($data);
	$content  = '';
	switch ($info) {
		case 'title':
			//$content  =  $thedata->entry->title->{'$t'};
			$content  =  $thedata->items[0]->snippet->title;
			break;
		case 'channel':
			$pieces = explode("/", @$thedata->entry->link[0]->href);
			$content = end($pieces);
			break;
		case 'thumb':
			//$content  =  $thedata->entry->{'media$thumbnail'}->url;
			$content  =  $thedata->items[0]->snippet->thumbnails->default->url;
			break;
	}	
	return $content;
}
public static function loadhtml($url) {
	if (ini_get('allow_url_fopen') == true) {
		$html = file_get_contents ($url);
	} else
		if (function_exists('curl_init')) {
			$curl = curl_init($url);
			//Turn off for Bluehost
			//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
			$html = curl_exec($curl);
			curl_close($curl);
		}
	else {
      throw new Exception("Can't load data.");
    }
	return $html;
  }

public static function sec2hms($youtube_time){
	$start = new DateTime('@0'); // Unix epoch
    $start->add(new DateInterval($youtube_time));
    if (strlen($youtube_time)>8)
    {
		return $start->format('g:i:s');
	} else {
		return $start->format('i:s');
		}
 }
  
public static function getVideoInfo($vidID) { 
        $feedURL = 'https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id='.$vidID.'&key=AIzaSyDgUkAEPr-DkH9tmzJafc_td9S_c5EmT2U';

        $entry = json_decode(modGearYoutubePlaylistHelper::loadhtml($feedURL));
            $video["title"] = $entry->items[0]->snippet->title;
			$video["description"] = stripslashes($entry->items[0]->snippet->description); 
			$video["thumbnail"] = $entry->items[0]->snippet->thumbnails->default->url;
            $video["length"] = modGearYoutubePlaylistHelper::sec2hms($entry->items[0]->contentDetails->duration); 
        return $video; 
    }
public static function getpVideoInfo($pvidID) { 
       
        $feedURL = 'https://www.googleapis.com/youtube/v3/playlists?part=snippet,contentDetails&id='.$pvidID.'&key=AIzaSyDgUkAEPr-DkH9tmzJafc_td9S_c5EmT2U';		   
        $entry = json_decode(modGearYoutubePlaylistHelper::loadhtml($feedURL));
            $video["title"] = $entry->items[0]->snippet->title;
			$video["description"] = stripslashes($entry->items[0]->snippet->description); 
			$video["thumbnail"] = $entry->items[0]->snippet->thumbnails->default->url;
            $video["length"] = $entry->items[0]->contentDetails->itemCount; //Number of Videos
        return $video; 
    }
protected function get_tag($theparams,$tag, $defaultvalue = null) {
	$passed = preg_match_all("|{$tag}[\s\v]*=[\s\v]*['\"](.*)['\"]|Ui",$theparams,$matches,PREG_PATTERN_ORDER);
	if ( $passed == 0) $passed = preg_match_all("|{$tag}[\s\v]*=[\s\v]*([\S]+)[\s\v]|Ui",$theparams,$matches,PREG_PATTERN_ORDER);
	if($passed) $defaultvalue = $matches[1][0];
	return $defaultvalue;
}
}
?>