<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.6 ( 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();
require('config.php');
require_once('include/functions.php');
require_once('include/user_functions.php');
require_once('include/islogged.php');

if( !is_user_logged_in() )
{
	header("Location: "._URL. "/login."._FEXT);
}

// define meta tags
$meta_title = $lang['suggest'];
$meta_description = '';
$form_action = 'suggest.'._FEXT;

$sources	  =	fetch_video_sources();
$temp = array();

foreach ($sources as $id => $src)
{
	if (is_int($id) && $id > 2 && $id != 14)
	{
		if ($src['source_name'] != 'quicktime' && $src['source_name'] != 'windows media player')
		{
			$temp[$id] = $src;
		}
	}
}
$sources = $temp;
unset($temp);

//	generate categories list;
$countries_list = array();

$sql = "SELECT * FROM pm_categories WHERE parent_id=0 ORDER BY position ASC";
$result = mysql_query($sql);
if(!$result) { 
	echo $lang['ep_msg9']; 
}
while($row = mysql_fetch_assoc($result)){
	$categories_list[$row['id']] = $row['name'];
	
	$subcats = mysql_query("SELECT * FROM pm_categories WHERE parent_id=".$row['id']." AND parent_id !=0 ORDER BY position ASC");
		while($r = mysql_fetch_array($subcats)) {
		$categories_list[$r['id']] = "-".$r['name'];
		}
}
	

mysql_free_result($result);


if(isset($_POST['Submit'])) { // Submit new video
	$required_fields = array('yt_id' => "URL",
							 'category' => $lang['category'], 
							 'artist' => $lang['artist'], 
							 'video_title' => $lang['video'], 
							 );
	foreach( $_POST as $key => $value) {
		$value = trim($value);
		if(array_key_exists(strtolower($key), $required_fields) && empty($value) )
			$errors[$key] = $required_fields[$key]." ".$lang['register_err_msg8'];
	}
	if($_POST['category'] == '-1') {
		$errors['category'] = $lang['choose_category'];
	}
	//	check if the source is available
	$url = trim($_POST['yt_id']);
	//$sources	  =	fetch_video_sources();
	$use_this_src = -1;
	
	foreach($sources as $src_id => $source)
	{
		if($source['source_name'] != 'localhost' && $source['source_name'] != 'other')
		{
			if(@preg_match($source['source_rule'], $url))
			{
				$use_this_src = $source['source_id'];
				break;
			}
		}
	}
	
	if($url != '' && $use_this_src == -1)
	{
		$errors['yt_id'] = $lang['suggest_msg5'];
	}
	
	if( count($errors) == 0 )
	{
	// prepare everything for mysql
	$url = secure_sql($url);
	$description = trim($_POST['description']);
	$description = nl2br($description);
	$description = removeEvilTags($description);
	$description = secure_sql($description);

	if(_STOPBADCOMMENTS == '1') {
	$description = search_bad_words($description);
	}
	$description = word_wrap_pass($description);
	
	$artist = 		secure_sql($_POST['artist']);
	$artist =		str_replace( array("<", ">"), '', $artist);
	$video_title = 		secure_sql($_POST['video_title']);
	$video_title = 		str_replace( array("<", ">"), '', $video_title);
	$submitted = secure_sql($userdata['username']);
	$category = secure_sql($_POST['category']);
	$yt_id = specialchars($yt_id, 0);
	$artist = specialchars($artist, 0);
	$video_title = specialchars($video_title, 0); 
	$user_id = $userdata['id'];
	$tags = removeEvilTags($_POST['tags']);
	$tags = secure_sql($tags);

	//	Lookup this URL in the database, check for existence to avoid duplication.
	$query = mysql_query("SELECT uniq_id FROM pm_videos_urls WHERE direct = '".$url."'");
	$count = mysql_num_rows($query);

	$query2 = mysql_query("SELECT id FROM pm_temp WHERE url = '".$url."'");
	$count2 = mysql_num_rows($query2);

	@mysql_free_result($query);
	@mysql_free_result($query2);
	
	if($count > 0) 
	{
		$smarty->assign('success', 3); // Already exists in database
	}
	elseif($count2 > 0) 
	{
		$smarty->assign('success', 4); // Already exists in pm_temp table - waiting for approval
	} 
	else 
	{
		$sql = "INSERT INTO pm_temp (url, artist, video_title, description, tags, category, username, user_id, added, source_id, language) 
											VALUES ('".$url."', '".$artist."', '".$video_title."', '".$description."', '".$tags."', '".$category."', '".$submitted."', '".$user_id."', '".time()."', '".$use_this_src."', '1')";

		$query = @mysql_query($sql);
		$smarty->assign('success', 1);
	}
    }
	else
	{
		$_POST['artist'] = str_replace('"', '', $_POST['artist']);
		$_POST['video_title'] = str_replace('"', '', $_POST['video_title']);
		$_POST['tags'] = str_replace('"', '', $_POST['tags']);
		$smarty->assign('success', 0);
		$smarty->assign('errors', $errors);
	}
}

$smarty->assign('form_action', $form_action);
$smarty->assign('errors', $errors);
$smarty->assign('categories_list', $categories_list);
$smarty->assign('sources', $sources); 

// --- DEFAULT SYSTEM FILES - DO NOT REMOVE --- //
$smarty->assign('meta_title', $meta_title);
$smarty->assign('meta_description', $meta_description);
$smarty->assign('template_dir', $template_f);
$smarty->display('suggest.tpl');
?>