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

$showm = '2';
include('header.php');

define('PHPMELODY', true);

$step = 2;

$inputs = array();

if ($_POST['submit'] != '')
{
	$return_msg = '';
	
	foreach ($_POST as $k => $v)
	{
		if ( ! is_array($_POST[$k]))
			$_POST[$k] = stripslashes(trim($v));
	}
	
	if (strlen($_POST['artist']) == 0)
	{
		$return_msg = 'Please type the artist name';
	}
	else if (strlen($_POST['video_title']) == 0)
	{
		$return_msg = 'Insert the video title';
	}
	else if (strlen($_POST['embed_code']) == 0)
	{
		$return_msg = 'You forgot to paste in the embed code.';
	}
	else if ((is_array($_POST['category']) && count($_POST['category']) == 0) || ( ! isset($_POST['category'])))
	{
		$return_msg = 'Please select a category for this video';
	}
	
	if ($return_msg == '')
	{
		$video_details = array(	'uniq_id' => '',
								'artist' => '',
								'video_title' => '',
								'description' => '',
								'yt_id' => '',
								'category' => '',
								'submitted' => '',
								'source_id' => 0,
								'language' => 1,
								'age_verification' => 0,
								'url_flv' => '',
								'yt_thumb' => '',
								'mp4' => '',
								'direct' => '',
								'tags' => '', 
								'featured' => 0,
								'restricted' => 0 
							);
						
		$video_details['submitted']   = $userdata['username'];
		$video_details['souce_id'] 	  = 0;
		$video_details['featured'] 	  = (int) $_POST['featured'];
		$video_details['description'] = $_POST['description'];
		$video_details['yt_thumb'] 	  = $_POST['yt_thumb'];
		$video_details['artist'] 	  = $_POST['artist'];
		$video_details['video_title'] = $_POST['video_title'];
		$video_details['category'] 	  = (is_array($_POST['category'])) ? implode(',', $_POST['category']) : $_POST['category'];
		$video_details['tags'] 		  = $_POST['tags'];
		$video_details['embed_code']  = $_POST['embed_code'];
		$video_details['direct']	  = $_POST['direct'];
		$video_details['restricted']  = $_POST['restricted'];
		
		$video_details['embed_code'] = str_replace(array("'", "\n", "\r"), array('"', '', ''), $video_details['embed_code']);
		
		//	remove extra html tags
		$video_details['embed_code'] = strip_tags($video_details['embed_code'], '<iframe><embed><object><param>');
		
		//	remove left-overs
		if (strpos($video_details['embed_code'], '<object') !== false)
		{
			$video_details['embed_code'] = preg_replace('/\/object>(.*)/i', '/object>', $video_details['embed_code']);
		}

		//	replace width, height and wmode values with variables
		$video_details['embed_code'] = preg_replace('/width="([0-9]+)"/i', 'width="%%player_w%%"', $video_details['embed_code']);
		$video_details['embed_code'] = preg_replace('/height="([0-9]+)"/i', 'height="%%player_h%%"', $video_details['embed_code']);
		$video_details['embed_code'] = preg_replace('/value="(window|opaque|transparent)"/i', 'value="%%player_wmode%%"', $video_details['embed_code']);
		$video_details['embed_code'] = preg_replace('/wmode="(.*?)"/i', 'wmode="%%player_wmode%%"', $video_details['embed_code']);
		$video_details['embed_code'] = preg_replace('/width=([0-9]+)/i', 'width=%%player_w%%', $video_details['embed_code']);
		$video_details['embed_code'] = preg_replace('/height=([0-9]+)/i', 'height=%%player_h%%', $video_details['embed_code']);
		
		$video_details['embed_code'] = secure_sql($video_details['embed_code']);
		
		//	generate unique id;
		$found = 0;
		$uniq_id = '';
		$i = 0;
		do
		{
			$found = 0;
			$str = md5(time());
			$uniq_id = substr($str, 0, 9);
			if(count_entries('pm_videos', 'uniq_id', $uniq_id) > 0)
				$found = 1;
		} while($found === 1);

		$video_details['uniq_id'] = $uniq_id;
		$video_details['yt_id'] = $uniq_id;
		
		//	upload or download thumbnail picture.
		if($_FILES['thumb']['name'] != '')
		{
			require_once('img.resize.php');
			$img = new resize_img();
			$img->sizelimit_x = 130;
			$img->sizelimit_y = 130;
			
			$new_thumb_name = $uniq_id . "-1";
			
			//	resize image and save it
			if($img->resize_image($_FILES['thumb']['tmp_name']) === false)
			{
				$return_msg .= $img->error;
			}
			else
			{
				$img->save_resizedimage(_THUMBS_DIR_PATH, $new_thumb_name);
			}
			$video_details['yt_thumb'] = $new_thumb_name . "." . strtolower($img->output);
		}
		else if (strlen($video_details['yt_thumb']) > 0)
		{
			//	download thumbnail
			require_once( "./src/other.php");

			$img = download_thumb($video_details['yt_thumb'], _THUMBS_DIR_PATH, $uniq_id);
			if($img === false)
			{
				$return_msg .= "An error occurred while downloading the thumbnail!<br />";
			}
		}

		if (strlen($return_msg) == 0)
		{
			
			$new_video = insert_new_video($video_details);
			if($new_video !== true)
			{
				$return_msg = "<em>A problem occurred! Couldn't add the new video in your database;</em><br /><strong>MySQL Reports:</strong> ".$new_video[0]."<br /><strong>Error Number:</strong> ".$new_video[1]."<br />";
			}
			else
			{
				//	tags?
				if($video_details['tags'] != '')
				{
					$tags = explode(",", $_POST['tags']);
					foreach($tags as $k => $tag)
					{
						$tags[$k] = stripslashes(trim($tag));
					}
					//	remove duplicates and 'empty' tags
					$temp = array();
					for($i = 0; $i < count($tags); $i++)
					{
						if($tags[$i] != '')
							if($i <= (count($tags)-1))
							{
								$found = 0;
								for($j = $i + 1; $j < count($tags); $j++)
								{
									if(strcmp($tags[$i], $tags[$j]) == 0)
										$found++;
								}
								if($found == 0)
									$temp[] = $tags[$i];
							}
					}
					$tags = $temp;
					//	insert tags
					if(count($tags) > 0)
						insert_tags($uniq_id, $tags);
				}
				
				$step = 3;
				$return_msg = '<b>SUCCESS!</b> The video has been added to your database.';
			}
		}
	}	//	endif $return_msg == ''

	$inputs = $_POST;
}

?>
<div id="sub_menu">
<li><a href="addvideo.php">Add new video</a></li>
<li><a href="embedvideo.php">Embed new video</a></li>
<li><a href="import.php">Import from Youtube</a></li>
<li><a href="import_user.php">Import from Youtube User</a></li>
<li><a href="reports.php">Reported Videos</a></li>
<li><a href="approve.php">Approve Videos</a></li>
</div>
<div id="content">
<h2>Embed new video</h2>

	<br />
	<div id="add_video_progress">
		<a href="addvideo.php"><img src="images/ico_addvideo.gif" alt="Add video" width="102" height="24" align="absmiddle" border="0" /></a> 
		<span class="steps">1. Choose video</span>
		<span class="<?php echo ($step == 2) ? 'steps_selected' : 'steps'; ?>">2. Add details</span>
		<span class="<?php echo ($step == 3) ? 'steps_selected' : 'steps'; ?>">3. Finish</span>
	</div>
	
<?php 
	
	if ($step == 2)
	{
		if (strlen($return_msg) > 0)
		{
			echo '<div class="info_msg_err">'. $return_msg .'</div>';
		}
?>	
	
	<form method="post" enctype="multipart/form-data" action="embedvideo.php" name="embed_video" onsubmit="return validateFormOnSubmit(this, 'Please fill in the required fields (highlighted)')">
	<table cellpadding="5" cellspacing="0">
	<tr class="table_row1">	
		<td><strong>Singer/Band</strong></td> 
		<td><input name="artist" type="text" id="must" value="<?php echo $inputs['artist']; ?>" style="width: 500px;" /></td>
	</tr>
	<tr class="table_row1">
		<td><strong>Video Title</strong></td>
		<td><input name="video_title" type="text" id="must" value="<?php echo $inputs['video_title']; ?>" style="width: 500px;" /></td>
	  </tr>
	<tr class="table_row1">
		<td><strong>Category</strong></td>
		<td>
		<?php
			$categories_dropdown_options = array(
												'attr_name' => 'category[]',
												'attr_id' => 'main_select_category',
												'select_all_option' => false,
												'spacer' => '&mdash;',
												'selected' => 0,
												'other_attr' => 'multiple="multiple"'
												);
			echo categories_dropdown($categories_dropdown_options);
			?>
		</td>
	  </tr>
	<tr class="table_row1">
		<td><strong>Featured</strong></td>
		<td>
		 <label>
		  <input type="checkbox" name="featured" value="1" /> Yes, mark as <span class="b_featured">featured</span> 
		 </label>
		</td>
	</tr>
	  <tr class="table_row1">
		<td><strong>Direct link to video page</strong><br /><small>Optional field</small></td>
		<td>
		<input type="text" name="direct" value="<?php echo $inputs['direct']; ?>" style="width: 500px;" />
		</td>
	  </tr>
	  <tr class="table_row1">
		<td valign="top"><strong>Embed code</strong></td>
		<td>
		 <textarea name="embed_code" id="must" style="height: 80px; width: 500px;"><?php echo $inputs['embed_code']; ?></textarea>
		 <small>Accepted HTML tags: &lt;iframe&gt;, &lt;embed&gt;, &lt;object&gt; and &lt;param&gt; (no Javascript)</small>
		 <br />
		</td>
	  </tr>
	<tr>
		<td valign="top"><strong>Description</strong></td>
		<td>
		<textarea name="description" rows="10" id="textarea-WYSIWYG" style="height: 150px; width: 500px;"><?php echo $inputs['description']; ?></textarea>
		<script language="javascript1.2">
			if(navigator.userAgent.toLowerCase().indexOf('chrome/1') == -1)
			{
			// Overwrite WYSIWYG Width and Height
			wysiwygWidth = '100%';
			wysiwygHeight = 150;
			generate_wysiwyg('textarea-WYSIWYG');
			}
		</script>
		<script type="text/javascript">
			//	Editor not loaded?
			if ($("#wysiwygtextarea-WYSIWYG").length == 0)
			{
				// restore textarea
				$("#textarea-WYSIWYG").show();
			}
		</script>
		</td>
	  </tr>
	<tr class="table_row1">
		<td><strong>Tags</strong></td>
		<td><input name="tags" type="text" value="<?php echo $inputs['tags']; ?>" style="width: 500px;" /><br />
			<small>(e.g. madonna, justin timberlake, live)</small>				</td>
	  </tr>
	<tr class="table_row1">
		<td><strong>Thumbnail location</strong></td>
		<td><input type="text" name="yt_thumb" id="yt_thumb" value="<?php echo $inputs['yt_thumb']; ?>" style="width: 500px;" /></td>
	  </tr>
	  <tr class="table_row1">
		<td align="right"><small>...or upload one</small></td>
		<td>
		<input type="file" name="thumb" onChange="javascript: if(this.value!='') yt_thumb.disabled='disabled'; else yt_thumb.disabled=''"/>
		</td>
	  </tr>
	  <tr class="table_row1">
		<td><strong>Restrict access</strong></td>		
		<td>
			<label><input type="radio" name="restricted" value="1" <?php echo ($inputs['restricted'] == '1') ? 'checked="checked"' : '';?> /> Yes</label> 
			<label><input type="radio" name="restricted" value="0" <?php echo ($inputs['restricted'] != '1') ? 'checked="checked"' : '';?> /> No</label>
		</td>
	  </tr>
	  <tr>
		<td align="right"></td>
		<td>
		<input type="hidden" name="language" value="1" />
		<input type="hidden" name="source_id" value="0" />
		<input type="hidden" name="age_verification" value="0" />
		<input type="submit" name="submit" value="Submit" class="inputbutton" />
		</td>
	  </tr>
	</table>
	</form>
	<br />
<?php
	}	//	endif step == 2
	else if ($step == 3)
	{
		echo '<div class="info_msg_ok">'. $return_msg .'</div>';
		
		echo '<br /><input name="embed_new" type="button" value="&laquo; Embed another video" onClick="location.href=\'embedvideo.php\'" class="inputbutton" />';
		echo '<input name="add_new" type="button" value="Add / upload new video" onClick="location.href=\'addvideo.php?step=1\'" class="inputbutton" />';
		echo '<input name="import_new" type="button" value="Import from YouTube" onClick="location.href=\'import.php\'" class="inputbutton" />';
	}
?>	
</div>
<?php
include('footer.php');
?>