<?php
/// Добовление Голосование!
include 'config.php';
include 'sus.php';

$_POST = str_replace($interzis, $replace, $_POST);

$intrebare = $_POST['intrebare'];
$nume = $_POST['nume'];
$oponent1 = $_POST['oponent1'];
$oponent2 = $_POST['oponent2'];
$img1 = $_POST['img1'];
$img2 = $_POST['img2'];
$com1 = $_POST['com1'];
$com2 = $_POST['com2'];
$email = $u_email;
$ssd = $u_id;



$form = <<<HTML
<!-- NOT LOGGED -->
<div class="row">
	<div class="span12 offset2">
		<div class="page-header">
		</div>
		
				
		
		<form action="" method="post" name="add_form" id="add_form" accept-charset="utf-8" enctype="multipart/form-data">
		<input type="hidden" name="token" value="4073d1e8116fbd8d2e753b7e6a40cb2657b624ef" />

		<center><h2>Вопрос</h2></center>
		<input type="text" name="intrebare" id="text" />
		<div class="help-block">
			<strong>ВАЖНО! Указывайте вопрос ПОЛНОСТЬЮ!</strong><br />
							<span style="color: green">Пример №1:</span> &quot;Что круче: Луна или Солнце?&quot; <br />
							<span style="color: green">Пример №2:</span> &quot;Какая марка автомобиля надёжнее: BWM или Mercedes?&quot;		</div>

		<div class="row images">
			<div class="span5">
				<div>
					<h1>Левая <br/>Картинка</h1>
					<input type="file" name="file" />
				</div>
				<h4>Левый противник</h4>
				<input type="text" name="oponent1" id="title_1" />
			</div>
			<div class="span2">
				<h1>VS</h1>
			</div>
			<div class="span5">
				<div>
					<h1>Правая <br/>Картинка</h1>
					<input type="file" name="file1" />
				</div>
				<h4>Правый противник</h4>
				<input type="text" name="oponent2" id="title_2" />
			</div>
		</div>

		<div class="row">
			<div class="span5 offset4">
				<br /><br />
				<input type="submit" name="send" class="btn green" value="Добавить Битву" />
			</div>
		</div>

		</form>
	</div>

</div>

HTML;





if ($_POST['send'] == "") {
    echo $form;
} else {

if ($intrebare != "" && $oponent1 != "" && $oponent2 != "" && $_FILES['file']['name'] != "" && $_FILES['file1']['name'] != "") {



$fn = str_replace("php", "", $_FILES['file1']['name'], $xsq);
$xs = $xs+$xsq;

$fn = str_replace("html", "", $fn, $xsq);
$xs = $xs+$xsq;

$fn = str_replace("htm", "", $fn, $xsq);
$xs = $xs+$xsq;

$fn = str_replace("php", "", $_FILES['file']['name'], $xsq);
$xs = $xs+$xsq;

$fn = str_replace("html", "", $fn, $xsq);
$xs = $xs+$xsq;

$fn = str_replace("htm", "", $fn, $xsq);
$xs = $xs+$xsq;
echo $fn;
echo $xs;

if ($xs > 0) {
die("Eroare extensie incorecta!");
}

$com1="Проголосовал за $oponent1 !  Друзья проголосуйте тоже!";
$com2="Проголосовал за $oponent2 !  Друзья проголосуйте тоже!";
  
      $link = mysql_connect($baza['host'], $baza['user'], $baza['pass'])
        or die("Could not connect : " . mysql_error());
    mysql_select_db($baza['nume']) or die("Erorr ! Data base name incorect !");
$tb = $baza['table'];


$r = mysql_query("SELECT id FROM $tb") or die("Erorr ! Data base name incorect !");;
while ($col=mysql_fetch_array($r, MYSQL_ASSOC)) {
$nume=$col['id']+1;
}


error_reporting(0);

$change="";
$abc="";


 define ("MAX_SIZE","400");
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

 $errors=0;
  
 if($_SERVER["REQUEST_METHOD"] == "POST")
 {
 	$image =$_FILES["file"]["name"];
	$uploadedfile = $_FILES['file']['tmp_name'];
     
 
 	if ($image) 
 	{
 	
 		$filename = stripslashes($_FILES['file']['name']);
 	
  		$extension = getExtension($filename);
 		$extension = strtolower($extension);
		
		
 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
 		{
		
 			$change='<div class="msgdiv">Unknown Image extension </div> ';
 			$errors=1;
 		}
 		else
 		{

 $size=filesize($_FILES['file']['tmp_name']);


if ($size > MAX_SIZE*1024)
{
	$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
	$errors=1;
}


if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);

}
else 
{
$src = imagecreatefromgif($uploadedfile);
}

echo $scr;

list($width,$height)=getimagesize($uploadedfile);


$newwidth=200;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);


imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

$filename = "imgs/".time(). $_FILES['file']['name'];




imagejpeg($tmp,$filename,100);



imagedestroy($src);
imagedestroy($tmp);

}}

}


 function getExtension1($str1) {
         $i = strrpos($str1,".");
         if (!$i) { return ""; }
         $l = strlen($str1) - $i;
         $ext = substr($str1,$i+1,$l);
         return $ext;
 }

 $errors1=0;
  
 if($_SERVER["REQUEST_METHOD"] == "POST")
 {
 	$image1 =$_FILES["file"]["name"];
	$uploadedfile1 = $_FILES['file1']['tmp_name'];
     
 
 	if ($image1) 
 	{
 	
 		$filename1 = stripslashes($_FILES['file1']['name']);
 	
  		$extension1 = getExtension1($filename1);
 		$extension1 = strtolower($extension1);
		
		
 if (($extension1 != "jpg") && ($extension1 != "jpeg") && ($extension1 != "png") && ($extension1 != "gif")) 
 		{
		
 			$change1='<div class="msgdiv">Unknown Image extension </div> ';
 			$errors1=1;
 		}
 		else
 		{

 $size1=filesize($_FILES['file1']['tmp_name']);


if ($size1 > MAX_SIZE*1024)
{
	$change1='<div class="msgdiv">You have exceeded the size limit!</div> ';
	$errors1=1;
}


if($extension1=="jpg" || $extension1=="jpeg" )
{
$uploadedfile1 = $_FILES['file1']['tmp_name'];
$src1 = imagecreatefromjpeg($uploadedfile1);

}
else if($extension1=="png")
{
$uploadedfile1 = $_FILES['file1']['tmp_name'];
$src1 = imagecreatefrompng($uploadedfile1);

}
else 
{
$src1 = imagecreatefromgif($uploadedfile1);
}

echo $scr1;

list($width1,$height1)=getimagesize($uploadedfile1);


$newwidth1=260;
$newheight1=($height1/$width1)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);


imagecopyresampled($tmp1,$src1,0,0,0,0,$newwidth1,$newheight1,$width1,$height1);

$filename1 = "imgs/".time(). $_FILES['file1']['name'];




imagejpeg($tmp1,$filename1,100);



imagedestroy($src1);
imagedestroy($tmp1);

}}

}

$filename="http://".$_SERVER['HTTP_HOST']."/$filename";
$filename1="http://".$_SERVER['HTTP_HOST']."/$filename1";
$cat = $_POST['category'];
mysql_query("INSERT INTO $tb (id, nume, intrebare, oponent1, oponent2, com1, com2, email, pass, img1, img2, user, cat) 
VALUES ('', '$nume', '$intrebare', '$oponent1', '$oponent2', '$com1', '$com2', '$email', '$pass', '$filename', '$filename1', '$u_id', '$cat')");

$url = "http://".$_SERVER['SERVER_NAME']."/view_$nume";
$form2 = <<<HTML
<center>
<h2>Голосоваие Добавлено!!</h2><br/><a href="$url">Страница Голосование!</a>
Adresa: <input name="url" value="$url" size="75">
 </center><br /><br /><br />
 <hr />
HTML;
echo $form2;
} else {
echo 'Eroarea, nu ati completat toate spatiile.';
}
    
}
include 'jos.php';
?>