<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+

if(!defined('PHPMELODY'))
	die('Restricted Access!');


function get_info($url)
{
	$video_data = array();

	preg_match("/vimeo.com\/([^(\&|$)]*)/", $url, $matches);
	$vid_id = $matches[1];
	if(strstr($vid_id, "/") !== false)
	{
		$tmp = explode("/", $vid_id);
		$vid_id = $tmp[0];
		unset($tmp);
	}
	elseif(strstr($vid_id, "?") !== false)
	{
		$tmp = explode("?", $vid_id);
		$vid_id = $tmp[0];
		unset($tmp);
	}
	unset($matches);
	
	$target_url = "http://vimeo.com/moogaloop/load/clip:" . $vid_id;
	
	$error = 0;
	if(function_exists('curl_init'))
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $target_url);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
		$video_data = curl_exec($ch);
		$errormsg = curl_error($ch);
		curl_close($ch);
		
		if($errormsg != '')
		{
			echo $errormsg;
			return false;
		}
	}
	else if(ini_get('allow_url_fopen') == 1)
	{
		$video_data = @file($target_url);
		if($video_data === false)
			$error = 1;
	}
	
	if(!is_array($video_data))
	{
		$video_data = explode("\n", $video_data);
	}
	$total = count($video_data);
	$tmp = 0;
	for($i = 1; $i < $total; $i++)
	{
		if(trim($video_data[$i]) == "<stream_clips>")
		{
			$tmp = $i;
			break;
		}
	}
	if($tmp != 0)
	{
		for($i = $tmp; $i < $total-5; $i++)
		{
			unset($video_data[$i]);
		}
	}
	return $video_data;
}

function get_flv($video_data)
{
	$flv_link = "";
	
	$arr_length = count($video_data);

	$h = '';
	$t = '';
	$vid_id = '';
		
	foreach($video_data as $i => $data)
	{
		if(preg_match('/<nodeId>(.*?)<\/nodeId>/', $data, $matches) != 0)
		{
			$vid_id = $matches[1];
		}
		elseif(preg_match('/<request_signature>(.*?)<\/request_signature>/', $data, $matches) != 0)
		{	
			$h = $matches[1];
		}
		elseif(preg_match('/<request_signature_expires>(.*?)<\/request_signature_expires>/', $data, $matches) != 0)
		{	
			$t = $matches[1];
		}
	}
		
	$flv_link = 'http://www.vimeo.com/moogaloop/play/clip:'.$vid_id.'/'.$h.'/'.$t.'/video.flv?q=';
	$headers = fetch_headers($flv_link);
	
	if (count($header) < 10)
	{
		$flv_link = 'http://vimeo.com/moogaloop/play/clip:'.$vid_id.'/'.$h.'/'.$t.'?q=sd&type=local&embed_location=';
		$headers = fetch_headers($flv_link);
		
		$arr_length = count($headers);
		for($i = 0; $i < $arr_length; $i++)
		{
			if(strstr($headers[$i], "ocation:"))
			{
				$str1 = explode("ocation:", $headers[$i]);
				$flv_link = trim($str1[1]);
				break;
			}
		}
	}
	
	return $flv_link;
}

function get_thumb_link($video_data) 
{
	$thumb_link = '';
	
	$arr_length = count($video_data);

	for($i = 0; $i < $arr_length; $i++)
	{			
		if(preg_match('/<thumbnail>(.*?)<\/thumbnail>/', $video_data[$i], $matches))
		{	
			$thumb_link = $matches[1];
			break;
		}
	}
	
	$thumb_link = preg_replace("/_([0-9]+)x([0-9]+)\.jpg/", "_100x75.jpg", $thumb_link);
	
	return $thumb_link;
}

function fetch_headers($url)
{
	$headers = array();
	$url = trim($url);
	
	$error = 0;
	if(function_exists('get_headers'))
	{
		$url = str_replace(' ', '%20', $url);
		if( ! strstr($url, "http://"))
		{
			$url = "http://" . $url;
		}
		$headers = @get_headers($url, 0);
		if(!$headers)
		{
			$error = 1;
		}
	}
	
	if($error == 1 || function_exists('get_headers') === FALSE)
	{
		$error = 0;

		if(function_exists('curl_init'))
		{
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_HEADER, 1);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_NOBODY ,1);
			curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
			$data = curl_exec($ch);
			$errormsg = curl_error($ch);
			curl_close($ch);
			
			if($errormsg != '')
			{
				echo '<div class="info_msg_err">'.$errormsg.'</div>';
				return false;
			}				
			$headers = explode("\n", $data);
		}
		else if(ini_get('allow_url_fopen') == 1)
		{
			$fp = @fopen($url, "r");
			if(!$fp)
				$error = 1;
			else
			{
				if(function_exists('stream_get_meta_data'))
				{
					$data = @stream_get_meta_data($fp);
					$headers = $data['wrapper_data'];
				}
				else
				{
					$headers = $http_response_header;
				}
			}
			@fclose($fp);
		}
	}
	return $headers;
}

function video_details($video_data, $url, &$info) 
{

	$arr_length = count($video_data);

	for($i = 0; $i < $arr_length; $i++)
	{
		//	video title
		if(preg_match("/<caption>(.*?)<\/caption>/", $video_data[$i], $matches) != 0)
		{	
			$info['video_title'] = str_replace('"', '', $matches[1]);
		}
	
		//	direct link
		if(preg_match("/<url_clean>(.*?)<\/url_clean>/", $video_data[$i], $matches) != 0)
		{
			$info['direct'] = $matches[1];
		}
		//	video id
		if(preg_match("/<nodeId>(.*?)<\/nodeId>/", $video_data[$i], $matches) != 0)
		{
			$info['yt_id'] = $matches[1];
		}
	}
	//	flv
	$info['url_flv'] = get_flv($video_data);
	
	//	thumbnail link
	$info['yt_thumb'] = get_thumb_link($video_data);
}

function download_thumb($thumbnail_link, $upload_path, $video_uniq_id) {
	
	$last_ch = substr($upload_path, strlen($upload_path)-1, strlen($upload_path));
	if($last_ch != "/")
		$upload_path .= "/"; 

	$ext = ".jpg";
	
	$thumb_name = $video_uniq_id . "-1" . $ext;
	
	if(is_file( $upload_path . $thumb_name )) {
		return FALSE;
	}
	
	$error = 0;

	if ( function_exists('curl_init') ) 
	{

		$ch = curl_init();
		$timeout = 0;
		curl_setopt ($ch, CURLOPT_URL, $thumbnail_link);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		
		// Getting binary data
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
		
		$image = curl_exec($ch);
		curl_close($ch);
		
		//	create & save image;
		$img_res = @imagecreatefromstring($image);
		if($img_res === false)
			return FALSE;
		
		$img_width = imagesx($img_res);
		$img_height = imagesy($img_res);
		
		$resource = @imagecreatetruecolor($img_width, $img_height);
		
		if( function_exists('imageantialias'))
		{
			@imageantialias($resource, true); 
		}
		
		@imagecopyresampled($resource, $img_res, 0, 0, 0, 0, $img_width, $img_height, $img_width, $img_height);
		@imagedestroy($img_res);
	
		switch($ext)
		{
			case ".gif":
				//GIF
				@imagegif($resource, $upload_path . $thumb_name);
			break;
			case ".jpg":
				//JPG
				@imagejpeg($resource, $upload_path . $thumb_name);
			break;  
			case ".png":
				//PNG
				@imagepng($resource, $upload_path . $thumb_name);
			break;
		}
	}
	else if( ini_get('allow_url_fopen') == 1 )
	{
		// try copying it... if it fails, go to backup method.
		if(!copy($thumbnail_link, $upload_path . $thumb_name ))
		{
			//	create a new image
			list($img_width, $img_height, $img_type, $img_attr) = @getimagesize($thumbnail_link);

			$image = '';

			switch($img_type)
			{
				case 1:
					//GIF
					$image = imagecreatefromgif($thumbnail_link);
					$ext = ".gif";
				break;
				case 2:
					//JPG
					$image = imagecreatefromjpeg($thumbnail_link);
					$ext = ".jpg";
				break;  
				case 3:
					//PNG
					$image = imagecreatefrompng($thumbnail_link);
					$ext = ".png";
				break;
			}
			
			$resource = @imagecreatetruecolor($img_width, $img_height);
			if( function_exists('imageantialias'))
			{
				@imageantialias($resource, true); 
			}
			
			@imagecopyresampled($resource, $image, 0, 0, 0, 0, $img_width, $img_height, $img_width, $img_height);
			@imagedestroy($image);
		}
		
		$thumb_name = $video_uniq_id . "-1" . $ext;
		
		$img_type = 2;
		switch($img_type)
		{
			default:
			case 1:
				//GIF
				@imagegif($resource, $upload_path . $thumb_name);
			break;
			case 2:
				//JPG
				@imagejpeg($resource, $upload_path . $thumb_name);
			break;  
			case 3:
				//PNG
				@imagepng($resource, $upload_path . $thumb_name);
			break;
		}
		
		if($resource === '')
			$error = 1;
	} 

	return $upload_path . $thumb_name;
}

function do_main(&$video_details, $url)
{
	$video_data = get_info($url);
	if($video_data != false)
	{
		video_details($video_data, $url, $video_details);
	}
	else
	{
		$video_details = array();
	}
}

?>