<?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.
// +------------------------------------------------------------------------+

session_start();
@header('Content-Type: text/html; charset=UTF-8;');

require('config.php');
require_once('include/functions.php');
require_once('include/user_functions.php');
require_once('include/islogged.php');

$illegal_chars = array(">", "<", "&", "'", '"');

$message = '';
$page	 = '';
$page	 = $_GET['p'];
$action  = $_GET['do'];

if ($page == '')
{
	exit();
}

switch ($page)
{
	case 'comments':
		
		if (isset($_GET['do']))
		{
			switch ($action)
			{
				case 'show_comments':
					$page 	 = (int) $_GET['page'];
					$video_id = $_GET['vid'];
					$video_id = str_replace($illegal_chars, "", $video_id);
					
					if ($page > 0 && preg_match('/([0-9a-zA-Z-]{5,20})/', $video_id) != 0)
					{
						$message = show_comments($video_id, $userdata['power'], $page);
						
						if ($message != 'empty')
						{
							echo $message;
						}
					}
					
				break;
				
				case 'onpage_delete_comment':
					
					$video_id 	= $_GET['vid'];
					$comment_id	= (int) $_GET['cid'];
						
					if (is_admin() || (is_moderator() && mod_can('manage_comments')) 
						&& strlen($video_id) > 0  
						&& $comment_id != 0)
					{	
						$video_id = str_replace($illegal_chars, "", $video_id);
						$video_id = secure_sql($video_id);
						
						$sql = "DELETE 
								FROM pm_comments 
								WHERE id = '". $comment_id ."' 
								  AND uniq_id = '". $video_id ."' 
								LIMIT 1";
						$result = @mysql_query($sql);
					}
				break;
				
				default: 
					exit();
				break;
			}
		}
	break;
	
	case 'video':
		
		switch ($action)
		{
			case 'request':
				
				$video_id 	= $_GET['vid'];
				$video_id 	= str_replace($illegal_chars, "", $video_id);
				$video 		= request_video($video_id, 'detail', true);
				
				if ( ! is_user_logged_in() && $video['restricted'] == '1')
				{
					$smarty->assign('lang', $lang);
					$smarty->display('restricted_video.tpl');
					exit();
				}

				if ( ! is_array($video))
				{
					exit();
				}
				
				echo $video['embed_code'];
			break;
			
			case 'report':
				
				$video_id 	= $_GET['vid'];
				$video_id 	= str_replace($illegal_chars, "", $video_id);
				
				if (preg_match('/([0-9a-zA-Z-]{5,20})/', $video_id) != 0)
				{
					$video 		= request_video($video_id, 'detail', true);
					if (is_array($video))
					{
						
						report_video($video['uniq_id'], '1', 'This video might be "dead".', 'PM Bot');
					}
				}

			break;
			
			default:
				exit();
			break;
		}
		
	break;
	
	case 'favorites':
	
		switch ($action)
		{
			case 'request':
			
				$video_id 	= $_GET['vid'];
				$video_id 	= str_replace($illegal_chars, "", $video_id);
				$video 		= request_video($video_id, 'favorites', true);
				$embed_code	= '';
				
				if ( ! is_array($video))
				{
					exit();
				}
				
				if ( ! is_user_logged_in() && $video['restricted'] == '1')
				{
					$smarty->assign('lang', $lang);
					$smarty->display('restricted_video.tpl');
					exit();
				}
				
				if ($video['video_player'] == 'flvplayer')
				{
					$embed_code  = '<embed src="'. _URL .'/player.swf?config=';
					$embed_code .= '{';
					$embed_code .= "embedded: true,
									showOnLoadBegin: true, 
									useHwScaling: false, 
									showStopButton: true, 
									menuItems: [false, false, true, true, true, false, false], 
									timeDisplayFontColor: '0x". _TIMECOLOR ."', 
									controlBarBackgroundColor: '0x". _BGCOLOR ."', 
									progressBarColor2: '0x000000', 
									progressBarColor1: '0xFFFFFF', 
									watermarkLinkUrl: '". _WATERMARKURL ."', 
									showWatermark: '". _WATERMARKSHOW ."', 
									watermarkUrl: '". _WATERMARKURL  ."', 
									controlsOverVideo: 'locked', 
									controlBarGloss: 'high', 
									useNativeFullScreen: true, 
									showPlayListButtons: false, 
									initialScale: 'fit', 
									hideControls: false, 
									loop: false, 
									bufferLength: 5, 
									startingBufferLength: 2, 
									autoBuffering: ". _AUTOBUFF .", 
									autoPlay: true, 
									baseURL: '', 
									useSmoothing: true,";
									
					$embed_code .= "playList: [ { overlayId: 'play', 
												  name: 'ClickToPlay'
												 }, 
												 {  linkWindow: '_blank', 
												 	linkUrl: '". _URL ."/musicvideo.php?vid=". $video['uniq_id'] ."', 
													url: '". _URL ."/videos.php?vid=". $video['uniq_id'] ."', 
													name: '". rawurlencode($video['artist'] .' - '. $video['video_title']) ."'
												}]";
					$embed_code .= '}");';
					
					$embed_code .= '" width="'. $config['player_w_favs'] .'" height="'. $config['player_h_favs'] . '"';
					$embed_code .= ' scale="noscale" bgcolor="'. _BGCOLOR .'"';
					$embed_code .= ' type="application/x-shockwave-flash" allowFullScreen="true" allowScriptAccess="always" ';
					$embed_code .= ' allowNetworking="all" pluginspage="http://www.macromedia.com/go/getflashplayer">';
					$embed_code .= '</embed>';
					$embed_code = str_replace( array("\n", "\r", "\t"), "", $embed_code);
				}
				else if ($video['video_player'] == 'jwplayer')
				{
					$embed_code  = '<embed src="'. _URL .'/jwplayer.swf" ';
					$embed_code .= ' width="'. $config['player_w_favs'] .'" height="'. $config['player_h_favs'] . '"';
					$embed_code .= ' scale="noscale" bgcolor="'. _BGCOLOR .'"';
					$embed_code .= ' type="application/x-shockwave-flash" allowFullScreen="true" ';
					$embed_code .= ' allowScriptAccess="always" wmode="transparent" ';
					$embed_code .= ' flashvars="';
					
					if ($video['source_id'] == 3)
					{
						$embed_code .= '&file='. urlencode($video['direct']);
						$embed_code .= '&type=youtube';
					}
					else
					{
						$embed_code .= '&file='. urlencode(_URL ."/videos.php?vid=". $video['uniq_id']);
						$embed_code .= '&type=video';
					}
					$embed_code .= '&backcolor='. _BGCOLOR;
					$embed_code .= '&frontcolor='. _TIMECOLOR;
					$embed_code .= '&screencolor=000000';
					$embed_code .= '&bufferlength=5';
					$embed_code .= '&controlbar=over';
					$embed_code .= '&autostart=true';
					$embed_code .= '&logo='. urlencode(_WATERMARKURL);
					$embed_code .= '&link='. urlencode(_WATERMARKLINK);
					$embed_code .= '&skin='. urlencode(_URL).'/skins/'._JWSKIN;
					$embed_code .= '&plugins=timeslidertooltipplugin-2';
					$embed_code .= '">';
					$embed_code .= '</embed>';
					$embed_code = str_replace( array("\n", "\r", "\t"), "", $embed_code);
				}
				else
				{
					$embed_code = $video['embed_code'];	
				}
				
				update_view_count($video['id']);
				
				echo $embed_code;
				
			break;
			
			case 'onpage_delete_favorite':
				
				if ($logged_in && is_int($userdata['id']) && $userdata['id'] > 0)
				{
					$video_id = $_GET['vid'];
					$video_id = str_replace($illegal_chars, "", $video_id);
					$video_id = secure_sql($video_id);
					
					$sql = "DELETE 
							FROM pm_favorites 
							WHERE uniq_id = '".$video_id."' 
							  AND user_id = '".$userdata['id']."'";
					$result =  @mysql_query($sql);
				}
				
			break;
			
			default:
				exit();
			break;
		}
		
	break;
	
	case 'users':
	
		if (is_admin() || (is_moderator() && mod_can('manage_users')))
		{
			$user_id = (int) $_GET['uid'];
			$user_id = abs($user_id);
			
			if ($user_id == 0)
			{
				exit();
			}
			
			if ($user_id == $userdata['id'])
			{
				echo '<span class="ipaddress">You?</span>';
				exit();
			}
	
			$banned = array();
					
			$sql = "SELECT * 
					FROM pm_users 
					WHERE id = '". $user_id ."'";
			$result = @mysql_query($sql);
			if ( !  $result)
			{
				log_error(secure_sql('MySQL Error: '. mysql_error() . '<br />File: '. __FILE__ .' on line '. __LINE__), 'User Management', '1');
				exit();
			}
			
			$user = mysql_fetch_assoc($result);
			mysql_free_result($result);
	
			$banned = banlist($user['id']);
	
			switch ($action)
			{
				case 'allow':
	
					if($banned['user_id'] == $user['id'])
					{
						$sql = "DELETE 
								FROM pm_banlist 
								WHERE user_id ='". $user['id'] ."'";
	
						$result = @mysql_query($sql);
						if ( ! $result)
						{
							echo '<span class="ban_user">Error</span>';
							log_error(secure_sql('MySQL Error: '. mysql_error() . '<br />File: '. __FILE__ .' on line '. __LINE__), 'User Management', '1');
						}
						else
						{
							echo '<span class="ipaddress">Done</span>'; 
						}
					}
	
				break;
				
				case 'ban':
				
					if ($user['power'] != U_ADMIN && $banned['user_id'] != $user['id'])
					{
						$sql = "INSERT INTO pm_banlist 
								SET user_id = '". $user['id'] ."', 
									reason = ''";
						$result = @mysql_query($sql);
						if ( ! $result)
						{
							echo '<span class="ban_user">Error</span>';
							log_error(secure_sql('MySQL Error: '. mysql_error() . '<br />File: '. __FILE__ .' on line '. __LINE__), 'User Management', '1');
						}
						else
						{
							echo '<span class="ban_user">Banned!</span>';
						}
					}
					
				
				break;
			
				default:
					exit();
				break;
			}
		}	//	end if user is admin
	break;
	
	case 'detail':
		
		$video_id 	= $_GET['vid'];
		$video_id 	= str_replace($illegal_chars, "", $video_id);
		$video 		= request_video($video_id, '', true);
		$embed_code	= '';
		
		if ( ! is_array($video))
		{
			exit();
		}
		
		switch ($action)
		{
			case 'show_more_best':
			
				$output =  show_more('best_in_category', $video['category'], 10);
					
			break;
			
			case 'show_more_artist':
			
				$output = show_more('same_artist', $video['artist'], 10);
			
			break;
			
			case 'show_more_related':
			
				$output = show_more('related', $video['category'], 10);
			
			break;
		}
	
		echo $output;
	break;
	
	default:
		exit();
	break;
}	//	end switch ($page)

exit();
?>