<?php
include("config.php");
include('common.class.php');
include('specific.class.php');

$web = new web();

if($_POST)
{
	
	extract($_POST);
	
	if(!$greeting_title)
	{
		$web->alert('Please enter greeting title',true,true);
	}
	elseif(!$greeting)
	{
		$web->alert('Please enter your greeting',true,true);
	}
	elseif(!$keywords)
	{
		$web->alert('Please enter search keywords',true,true);
	}
	elseif(!$_FILES[image][name])
	{
		$web->alert('Please select file to upload',true,true);
	}
	elseif(!$agree)
	{
		$web->alert('Please read PEP\'S Terms of Use',true,true);
	}
	elseif(!preg_match('/(jpg|jpeg|gif|pjpg|flv|wmv|mp4|mpg)$/i',$_FILES[image][name]) and $_FILES[image][name]) 
	{
		$web->alert('Invalid file type',true,true);
	}	
	else
	{
		$username=$author_;
		
		$_POST[author] = $username;
		if(!file_exists('media/'.$username.'/'))
		{
			mkdir('media/'.$username.'/',0777);
			mkdir('media/thumbs/'.$username.'/',0777);
		}
		$destination = "media/".$username."/";
		$destination2 = "media/original_videos/";
		$destination_thumb = "media/thumbs/".$username."/";
		$_POST['destination'] = $username."/";
		$_POST['destination2'] = $username."/";
		
		if ($_FILES[image][type] == "image/gif") $short_type = ".gif";
		elseif ($_FILES[image][type] == "image/jpeg") $short_type = ".jpg";
		elseif ($_FILES[image][type] == "image/pjpeg") $short_type = ".jpg";
		else $short_type=".flv";
		
		$filename_temp = 'peparazzi_'.substr(md5(uniqid(rand(),1)),0,12);
		$filename=$filename_temp.$short_type;
		
		if ($short_type==".gif" or $short_type==".jpg")
			$filetype = 1;
		else 
			$filetype = 2;
		
		$_POST['filename'] = $filename;
		$_POST['filetype'] = $filetype;
		if($filetype==1)
		{
			copy($_FILES[image][tmp_name], $destination.$filename);
			$x = 105; $y = 75;
			$src = $username . '/' . $filename;
			$dest = $username . '/' . $filename;
			include('thumb.php');
		}
		else
		{
			if (preg_match('/\.(mpeg|mpg|rm|wmv|flv)$/i',$web->get_extension($filename))) {
					$uid = $filename_temp;
					$vidname = $uid . ".flv";
					$jpgname = $uid . ".jpg";
					$tempDest = $uid . $web->get_extension($_FILES[image]['name']);
					copy($_FILES[image][tmp_name],$destination . $tempDest)or die('cannot copy file');
					$web->convert_flv($destination . $tempDest,$destination . $vidname, $destination_thumb . $jpgname, 'jpg', $vidname);
				} else {
					echo ".";
					echo "<script>";
					echo "alert('Sorry, you can only upload MPG, WMV (windows media video) and FLV (flash video) files');";
					echo "history.go(-1);";
					echo "exit();";
					echo "</script>";
				}
		}
		$web->connect_rw();
		$gid=$web->saveIdolGreeting($_POST,$_FILES,$destination2,$filename);
		if($gid)
			header("Location: " . DOCUMENT_ROOT . "update_greeting.php?id=$gid");					// Redirect to notification
		
	}
}

$smarty->assign('page_name','peparazzi_default');
$smarty->display('writegreeting.html');
?>
