<?php

set_time_limit(0);

include("config.php");
include("common.class.php");
include("specific.class.php");

$web = new web();
extract($_GET);
																							// catches POST values 
if ($_POST) {
	
	extract($_POST);
	extract($_FILES);
	$has_content=0;
	$FILE2 = array();

    foreach ($_FILES[image][name] as $k=>$v) {
		if ($v) 
			$FILE2[image][name][] = $v;
	}
    foreach ($_FILES[image][type] as $k=>$v) {
		if ($v) 
		    $FILE2[image][type][] = $v;
	}

    foreach ($_FILES[image][tmp_name] as $k=>$v) {
		if ($v)
		    $FILE2[image][tmp_name][] = $v;
    }
    foreach ($_FILES[image][error] as $k=>$v) {
		if ($v == 0)
  	    	$FILE2[image][error][] = $v;
	}
	foreach ($_FILES[image][size] as $k=>$v) {
  		if ($v) 
			$FILE2[image][size][] = $v;
	}
	unset($_FILES);
	$_FILES = $FILE2;            

	foreach ($_FILES[image][name] as $k=>$v) {
		if ($v) $has_content = 1;
		if (!preg_match('/(jpg|jpeg|gif|pjpg|flv|wmv|mp4|mpg)$/i',$v) and $v) $invalid_file_in_content = 1;
	}           
	
	if (!$gallery_title)
		$web->alert("You must enter your Scoop Title.",true,true);
		
	//if($has_content==0)
	//		$web->alert("You haven\'t selected any image / video file(s) yet.",true,true);
	//else
	
	if (!$description)
		$web->alert("You must enter your Scoop Description.",true,true);
	elseif (!$keywords)
		$web->alert("You must enter your Gallery Search keywords.",true,true);
	elseif (strlen($keywords) < 4)
		$web->alert("Search keyword must be atleast 4 characters in length.",true,true);
	elseif (!$agree)
		$web->alert("You must agree to PEP\'s Terms of Agreement first.",true,true);
//	elseif (!$_FILES[image][name])
//		$web->alert("You haven\'t selected any image / video file(s) yet.",true,true);
//	elseif($invalid_file_in_content==1)
//		$web->alert("You have selected an invalid file. Please limit the upload to file types: jpg, gif, flv, mpg, wmv",true,true);
	else {
		
		$username = $author_;
		$_POST[author] = $username;
		
		if(!empty($embed)) {
			if(preg_match_all("/http:\/\/(www.youtube|youtube|[A-Za-z]{2}.youtube)\.com\/(watch\?v=|w\/\?v=|\?v=|v\/)([\w-]+)(.*?)/i", $embed, $embed_matches)) {
			} elseif(preg_match_all("/http:\/\/(www.dailymotion|dailymotion)\.com\/(.+)\/([0-9a-zA-Z]+)\_(.*?)/i", $embed, $embed_matches)) {
			} elseif(preg_match_all("/http:\/\/(www.metacafe|metacafe)\.com\/watch\/(.*?)\/(.*?)/i", $embed, $embed_matches)) {
			} elseif(preg_match_all("/http:\/\/(www.vimeo|vimeo)\.com(\/|\/clip:)(\d+)(.*?)/i", $embed, $embed_matches)) {
			}	else {
				$web->alert("Invalid video source/URL.",true,true);
			}
		} else {
			if (!$has_content)
				$web->alert("You haven\'t select any image / video file(s) yet.",true,true);
			elseif ($invalid_file_in_content) 
				$web->alert("You have selected an invalid file. Please limit the upload to file types: jpg, gif, flv, mpg, wmv",true,true);
		}

		$ctr=0;
		
		/*foreach($_FILES[image][name] as $k=>$v)
		{
			if(isset($v)) echo $v;
		}*/
		
		for($a=0;$a<10;$a++)
		{
			if($_FILES[image][name][$a])
			{
				if ($_FILES[image][type][$a] != "image/jpeg" and $_FILES[image][type][$a] != "image/gif" and $_FILES[image][type][$a] != "image/pjpeg" and !preg_match('/(jpg|jpeg|gif|pjpg|flv|wmv|mp4|mpg)$/i',$_FILES[image][name][$a]) )
					$web->alert("You have selected an invalid file. Please limit the upload to file types: jpg, gif, flv, mpg, wmv",true,true);
				
				$uploads[$a][name]=$_FILES[image][name][$a];
				$uploads[$a][type]=$_FILES[image][type][$a];
				$uploads[$a][error]=$_FILES[image][error][$a];
				$uploads[$a][tmp_name]=$_FILES[image][tmp_name];
				if ($_FILES[image][type][$a] == "image/gif") $short_type = ".gif";
				elseif ($_FILES[image][type][$a] == "image/jpeg") $short_type = ".jpg";
				elseif ($_FILES[image][type][$a] == "image/pjpeg") $short_type = ".jpg";
				else $short_type=".flv";
				if ($short_type==".jpg" or $short_type==".jpg" or $short_type==".gif")
					$uploads[$a][filetype]=1;
				else
					$uploads[$a][filetype]=2;
				$tmpfile_name = uniqid('peparazzi_');
				$uploads[$a][tmpfile_name1] = $tmpfile_name;
				$uploads[$a][filename] 	= $tmpfile_name . $short_type;
				$ctr++;
				
			}
				
		}
		
		
		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/";
		
		for($b=0;$b<$ctr;$b++)
		{
			if($uploads[$b][filetype]==1)
			{
				if($_FILES[image][name][$b])
				{
					copy($_FILES[image][tmp_name][$b],$destination . $uploads[$b][filename]);
					$x = 105; $y = 75;
					$src = $username . '/' . $uploads[$b][filename];
					$dest = $username . '/' .$uploads[$b][filename];
					include('thumb.php');
				}
			}
			else
			{
				// handle flash video conversion, exciting =P
				if (preg_match('/\.(mpeg|mpg|rm|wmv|flv)$/i',$web->get_extension($uploads[$b]['name']))) {
					// proceed with conversion !yay!
					$uid = $uploads[$b]['tmpfile_name1'];
					$vidname = $uid . ".flv";
					$jpgname = $uid . ".jpg";
					
					$tempDest = $uid . $web->get_extension($_FILES[image][name][$b]);
					copy($_FILES[image][tmp_name][$b],$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();
		$_POST['category'] = "fanscoop";
		$_POST["description"] = nl2br($_POST["description"]);
		$_POST["date"]	= date("Y-m-d");
		
		$gid = $web->saveGallery($_POST,$uploads);
		if ($gid) {
		
			echo "<script type=\"text/javascript\">alert('Successully uploaded.');</script>";
			echo "<script type=\"text/javascript\">document.location='".$_SERVER['HTTP_REFERER']."';</script>";
			exit();
		
			//header("Location: " . DOCUMENT_ROOT . "update_scoop.php?id=$gid");					// Redirect to notification
		}
		
	}
	
} else {
	$smarty->assign('page_name','peparazzi_default');
	$smarty->display("upload_entry.tpl");														// fireworks display!	
}

?>
