<?php

include("config.php");
include("common.class.php");
include("specific.class.php");

$web = new web();
extract($_GET);

if ($_POST) {
	extract($_POST);
	extract($_FILES);
	
	
	$has_content = 0;
	$invalid_file_in_content = 0;
	$file_validation_expression = '/\.(jpg|jpeg|gif|flv|wmv|mp4|mpg)$/i';
	
	$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 Gallery Title.",true,true);
	elseif (!$description)
		$web->alert("Please tell us about your self.",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 {
		$web->connect_rw();
		foreach ($_FILES[image][name] as $k=>$v) {
			if ($v) $count++;
		}
		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;
		}
		
		$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++;
		}
		
		$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/";
		
		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);
		}
		foreach ($uploads as $k => $v) {
			if ($v[filetype] == 1) {
				copy($v[tmp_name],$destination . $v[filename]) or die('cannot copy file');
				list($width_, $height_) = getimagesize($destination . $v[filename]);
				if ($width_ > 500) {
					// calculations:
					$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');
				}
				$x = 105; $y = 75;
				$src = $username . '/' . $v[filename];
				$dest = $username . '/' . $v[filename];
				
				include('thumb.php');
			} else {
				// handle flash video conversion, exciting =P
				if (preg_match('/\.(mpeg|mpg|rm|wmv|flv)$/i',$web->get_extension($v['name']))) {
					// proceed with conversion !yay!
					$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>";
				}
			}
		}

		$gid = $web->saveGallery($_POST,$uploads);
		
		if ($gid)
			header("Location: " . DOCUMENT_ROOT . "update.php?id=$gid");					// Redirect to notification
		else 
			;//$web->catchError();															// Error catcher
	}
	
} else {
	$smarty->assign('page_name','peparazzi_default');
	$smarty->display("bethestar.html");														// fireworks display!	
}

?>
