<?php

set_time_limit(0);

include("config.php");
include("common.class.php");
include("specific.class_new.php");

$web = new web();
extract($_GET);

if ($_POST) {
	extract($_POST);
	extract($_FILES);
	
	if ($_POST['emvideo'])
	{
		
		# a couple of server side validation
		if (!$gallery_title)
			$web->alert("You must enter your Gallery Title.",true,true);
		elseif (!$description)
			$web->alert("You must enter your Gallery 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);
		else {
			
			$username = $author_;
			$_POST[author] = $username;
			
			session_start();
			$_SESSION['post_values'] = $_POST;
			$_SESSION['embed'] = 1;
			header("Location: upload_complete_new.php");		
			
		}
		
	}
	else
	{
	
		# look up if there are fles to upload
		$has_content = 0;
		$invalid_file_in_content = 0;
		$file_validation_expression = '/\.(jpg|jpeg|gif|flv|wmv|mp4|mpg)$/i';
		
		# extract file contents
		$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;
		}
	
		# copy array onto another
		unset($_FILES);
		$_FILES = $FILE2;
		
		# check if the file extension is valid or not
		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;
		}
		
		# a couple of server side validation
		if (!$gallery_title)
			$web->alert("You must enter your Gallery Title.",true,true);
		elseif (!$description)
			$web->alert("You must enter your Gallery 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 (!$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);
		else {
			
			foreach ($_FILES[image][name] as $k=>$v) {
				if ($v) $count++;
			}
			
			# verify if they are video or photo, then assign new extensions.
			for($i=0; $i<$count;$i++) {
				$uploads[$i][name] 		= $_FILES[image][name][$i];
				$uploads[$i][type] 		= $_FILES[image][type][$i];
				$uploads[$i][tmp_name] 	= $_FILES[image][tmp_name][$i];
				$uploads[$i][error] 	= $_FILES[image][error][$i];
				$uploads[$i][size] 		= $_FILES[image][size][$i];
				if ($_FILES[image][type][$i] == "image/gif") $short_type = ".gif";
				elseif ($_FILES[image][type][$i] == "image/jpeg") $short_type = ".jpg";
				elseif ($_FILES[image][type][$i] == "image/pjpeg") $short_type = ".jpg";
				else $short_type = ".flv";
				$tmpfile_name = uniqid('peparazzi_');
				$uploads[$i][tmpfile_name1] = $tmpfile_name;
				$uploads[$i][filename] 	= $tmpfile_name . $short_type;
			}
			
			# file type assignment loop block/
			$i=0;
			foreach ($uploads as $k=>$v) {
				if (preg_match('/\.(jpg|jpeg|gif|pjpg)$/i',$v[name]))
					$uploads[$i][filetype] = 1;
				else 
					$uploads[$i][filetype] = 2;
				$i++;
			}
			
			# now extract cookie values for author field.
			$username = $author_;
			$_POST[author] = $username;
			if (!file_exists("media/$username/")) {
				mkdir("media/$username/",0777);
				mkdir("media/thumbs/$username/",0777);
			}
			
			# assign directory locations.
			$destination = "media/$username/";
			$destination2 = "media/original_videos/";
			$destination_thumb = "media/thumbs/$username/";
			
			# validate if each file is less than the maximum size.
			foreach ($uploads as $k=>$v) {
				if ($v[size] > "1000000") 
					if (preg_match('/\.(jpg|jpeg|gif|pjpg)$/i',$v[name]))
						$web->alert("Your file (".$v[name].") is too big.",true,true);
			}
			
			# copying, resizing and conversion begins.
			foreach ($uploads as $k => $v) {
				if ($v[filetype] == 1) {
					
					# now if the photo is too large, we will resize it to 500 px width.
					copy($v[tmp_name],$destination . $v[filename]) or die('cannot copy file');
					list($width_, $height_) = getimagesize($destination . $v[filename]);
					if ($width_ > 500) {
						$left_over_width = $width - 500;
						
						$newwidth_ = 500;
						$newheight_ = $height - $left_over_width;
						
						$x = $newwidth_; $y = $newheight_;
						$src = $username . "/" . $v[filename];
						$dest = $username . '/' . $v[filename];
						
						include('thumb2.php');
					}
					
					# now create those thumbnails
					$x = 105; $y = 75;
					$src = $username . '/' . $v[filename];
					$dest = $username . '/' . $v[filename];
					include('thumb.php');
					
				} else {
					
					# if it is a video type, lets convert them and save the file.
					if (preg_match('/\.(mpeg|mpg|rm|wmv|flv)$/i',$web->get_extension($v['name']))) {
						$uid = $v['tmpfile_name1'];
						$vidname = $uid . ".flv";
						$jpgname = $uid . ".jpg";
						$tempDest = $uid . $web->get_extension($v['name']);
						copy($v[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>";
					}
				}
			}
			session_start();
			$_SESSION['post_values'] = $_POST;
			$_SESSION['files'] = $uploads;
			header("Location: upload_complete_new.php");
			
			/*
			$web->connect_rw();
			$gid = $web->saveGallery($_POST,$uploads);
			echo "saved!...<br>";
			if ($gid)
				header("Location: " . DOCUMENT_ROOT . "update.php?id=$gid");
			else {
				
			}*/
		}
	}
} else {
	if ($ewall)
	{
		//var_dump($ewall);
		$smarty->assign('ewall', $ewall);	
	}
	$smarty->assign('page_name','peparazzi_default');
	$smarty->display("upload_new.html");
}

?>
