<?php
	
	$actions = array('add_file');
	$add_err = '';

	(key_exists('action', $_GET) && $_GET['action']<>'' && in_array($_GET['action'], $actions) && session_is_registered('mobile_logged_in')) ? $action = $_GET['action'] : $action = '';
	(key_exists('category', $_POST) && $_POST['category']<>'' && session_is_registered('mobile_logged_in')) ? $category = $_POST['category'] : $category = '';
	
	if($action=='add_file' && key_exists('the_file', $_FILES) && $category!=0)
	{
		$file_tmp = $_FILES['the_file']['tmp_name'];
		$file = $_FILES['the_file']['name'];
		if(file_exists($file_tmp)) 
		{
			list($width, $height, $type, $attr) = getimagesize($file_tmp);
			$check_wh = ($width<=308 && $width>0 && $height<=320 && $height>0) ? true : false;
			if($check_wh)
			{
				$res=0;
				if($width==128 && $height==128) $res=1; 
				if($width==174 && $height==130) $res=2; 
				if($width==128 && $height==160) $res=3; 
				if($width==101 && $height==80) $res=4; 
				if($width==176 && $height==220) $res=5; 
				if($width==132 && $height==176) $res=6; 
				if($width==208 && $height==208) $res=7; 
			}
			else $add_err .= 'Neplatné rozmery obrázku! <br />';
			
			$check_format = false;
			$type = $_FILES['the_file']['type'];
//			$types = array('image/jpeg', 'image/bmp', 'image/gif');
			$types = array('image/jpeg', 'image/gif');
			if(in_array($type, $types)) $check_format = true;
			else $add_err .= 'Neplatný formát obrázku! <br />';
			
			$check_size=false;
			if(file_exists($file_tmp)) $size = round(filesize($file_tmp)/1024,2); 
			if($size<=50 && $size>0) $check_size = true;
			if(in_array($_SESSION['my_id'], $admins)) $check_size = true;
			$add_err .= 'Neplatná veľkosť obrázku! <br />';
			
			$dot_position = 0;
			if($file[strlen($file)-5]=='.') $dot_position = 4;
			if($file[strlen($file)-4]=='.') $dot_position = 3;
			
			if($dot_position!=0)
			{
				$ending = substr($_FILES['the_file']['name'], strlen($_FILES['the_file']['name'])-$dot_position, $dot_position);
				$xxx = md5($_FILES['the_file']['name'].time());
				$new_name = substr($xxx, 0, 12).'.'.$ending;
				
				$hash_string = md5_file($file_tmp);
				
				$query = "SELECT ID FROM mobile_pozadia WHERE hash='".$hash_string."'";
				$result = $dba->GetResult($query);
				($result) ?	$num = mysql_num_rows($result) : $num = 0;
				
				$unique = true;
				if($num>0)
				{
					$unique = false;
					$row = mysql_fetch_array($result);
					$image_id = $row['ID'];
				}		
				
				if(!$unique) $add_err='Pozadie, ktoré chcete pridať, sa už na 4mobile nachádza a má ID '.$image_id;
				
				if($check_wh && $check_format && $check_size && $unique)
				{
					$visible = (key_exists('visible', $_POST)) ? $_POST['visible'] : 0;
					$uploaddir = "/home/clients/peterpech.com/peterpech.com/4mobile/pozadia2/";
					$uploaddir2 = "/home/clients/peterpech.com/peterpech.com/4mobile/dev/4mobile2/pozadia2/";
					$uploadfile = $uploaddir . $new_name; 
					$uploadfile2 = $uploaddir2 . $new_name; 
					move_uploaded_file($file_tmp, $uploadfile);
					copy($uploadfile, $uploadfile2);
					$cat = $_POST["category"];
					$query = "INSERT INTO `mobile_pozadia` ( `ID` , `file` , `cat` , `res` , `hlasov` , `sucet` , `priemer`, `pridal`, `hash`, `visible` ) VALUES ('', '".$new_name."', '".$cat."', '".$res."', '0', '0', '0', '".$_SESSION["my_id"]."', '".$hash_string."', '".$visible."');";
					$dba->ExecuteQuery($query);
					$add_err = 'Obrázok vyhovuje kritériám a bol pridaný';
				}
			}
			else $add_err = 'Neplatná prípona súboru! <br />';
		}
		else $add_err = 'Zadaný súbor neexistuje! <br />';
	}
	elseif($category==0 && $action == 'add_file') $add_err .= 'Nevybrali ste kategóriu! <br />';
	if(!key_exists('the_file', $_FILES) && $action == 'add_file') $add_err .= 'Nevybrali ste žiadny súbor! <br />';

?>

	<div id="register_form01">
	  <div id="menu01_head">:: pridať pozadie</div>
<?php

if(isset($add_err)) echo '<div id="form_popis02">'.$add_err.'</div>';

	echo"
	<form ENCTYPE=\"multipart/form-data\" name=\"q\" action=\"index.php?module=pozadia_add&action=add_file\" method=POST>
			<table width=\"350\" border=\"0\" align=\"center\" cellspacing=\"1\">
				<tr bgcolor=\"#222222\">
					<td colspan=\"2\" style=\"padding-top:3px; padding-bottom:3px;\">&nbsp;Súbor: <input id=\"form_field02\" name=\"the_file\" type=\"file\" size=\"29\"></td>
				</tr>
				<tr bgcolor=\"#222222\">
					<td colspan=\"2\">&nbsp;Kategória:</td>
				</tr>
				<tr bgcolor=\"#222222\">
					<td width=\"50%\" valign=\"top\" style=\"padding-bottom:3px;\">
						<input name=\"category\" type=\"radio\" value=\"1\"> auto-moto<br />
						<input name=\"category\" type=\"radio\" value=\"2\"> erotika<br />
						<input name=\"category\" type=\"radio\" value=\"3\"> modelky<br />
						<input name=\"category\" type=\"radio\" value=\"4\"> autá & ženy<br />
						<input name=\"category\" type=\"radio\" value=\"5\"> funny<br />
						<input name=\"category\" type=\"radio\" value=\"6\"> animované<br />
						<input name=\"category\" type=\"radio\" value=\"7\"> bábätká<br />
						<input name=\"category\" type=\"radio\" value=\"8\"> pre ženy<br />
						<input name=\"category\" type=\"radio\" value=\"9\"> filmy<br />
						<input name=\"category\" type=\"radio\" value=\"10\"> príroda<br />
					<td valign=\"top\">
						<input name=\"category\" type=\"radio\" value=\"11\"> mix<br />
						<input name=\"category\" type=\"radio\" value=\"12\"> láska<br />
						<input name=\"category\" type=\"radio\" value=\"13\"> zvieratká<br />
						<input name=\"category\" type=\"radio\" value=\"14\"> šport<br />
						<input name=\"category\" type=\"radio\" value=\"15\"> art<br />
						<input name=\"category\" type=\"radio\" value=\"16\"> logá<br />
						<input name=\"category\" type=\"radio\" value=\"17\"> celebrity<br />
						<input name=\"category\" type=\"radio\" value=\"18\"> Vianoce<br />
						<input name=\"category\" type=\"radio\" value=\"19\"> hudba<br />
						<input name=\"category\" type=\"radio\" value=\"20\"> rozprávky<br /></td>
				</tr>
				<tr bgcolor=\"#222222\">
					<td colspan=\"2\">&nbsp;Zobraziť všetkým používateľom: <input name=\"visible\" type=\"checkbox\" value=\"1\" checked></td>
				</tr>
				<tr bgcolor=\"#222222\">
					<td colspan=\"2\" align=\"center\"><br />Ak nie je označené \"Zobraziť všetkým používateľom\", bude pozadie viditeľné len pre Vás v časti moje pozadia.<br />Maximálna veľkosť pozadia je 50 kB.<br /> Maximálne rozmery pozadia sú 308x320px.<br />
						Podporované formáty sú JPG a GIF.<br /><br /></td>
					
				</tr>
				<tr bgcolor=\"#222222\">
					<td colspan=\"2\" align=\"center\" style=\"padding: 4px;\"><input id=\"form_button01\" type=\"submit\" value=\"OK\" style=\"cursor:hand\";>  
          				<input id=\"form_button01\" type=\"reset\" value=\"Vymazať\" style=\"cursor:hand\";></td>
					
				</tr>
			</table>
		</form>";
		
?>

	</div>