<?php 
	require_once("mysqlconnect.php");
	require_once("global/functions.php");
	require_once("global/constants.php");
	require_once("../conf/definitions.php");
	session_start();
	
	$u=$_GET['u'];
	
	if(!$_SESSION["mobile_admin_logged_in"] || $u<>$uhash) header('location: index.php');
	
	$action=$_GET["action"];
	
	if($action=='add_file')
	{
		$nazov=$_POST["nazov"];
		$nazov=eregi_replace(' ', '', $nazov);	
	}
		
	if($action=='add_file' && $_FILES['the_file']<>'' && $nazov<>'')
	{
		$file_tmp=$_FILES['the_file']['tmp_name'];
		$file=$_FILES['the_file']['name'];
		$check_format=false;
		$str=$file;
		$lenght=strlen($str);
		$new_str=substr($str, $lenght-3, 3);
		$new_str=strtoupper($new_str);
		$melody_types = array
		(
			'audio/wav',
			'audio/x-wav',
			'audio/mid',
			'audio/midi',
			'audio/mpeg',
			'application/octet-stream',
			'application/force-download',
			'audio/amr',
		);
		if(in_array(strtolower($_FILES['the_file']['type']), $melody_types))
		{
			$check_format=true;
		}
		$check_size=false;
		if(file_exists($file_tmp)){$size=round(filesize($file_tmp)/1024,2);}
		if($size<=1500 && $size>0){$check_size=true;}
		$err='';
		if($check_format==false){$err.='Neplatný formát zvonenia<br>';}
		if($check_size==false){$err.='Neplatná veľkosť zvonenia<br>';}
		if($check_format==true && $check_size==true)
		{
			$uploaddir = "/home/clients/peterpech.com/peterpech.com/4mobile/melody/";
			$uploadfile = $uploaddir . $file;
			move_uploaded_file($file_tmp, $uploadfile);
			copy('/home/clients/peterpech.com/peterpech.com/4mobile/melody/'.$file, '/home/clients/peterpech.com/peterpech.com/4mobile/samples/'.$file);
			$poznamka=$_POST["poznamka"];
			$poznamka=htmlspecialchars($poznamka);
			$alter=$_POST["alter"];
			$alter=htmlspecialchars($alter);
			$interpret=$_POST["interpret"];
			$nazov=$_POST["nazov"];
			$hlasy=$_POST["hlasy"];
			$novinka = (isset($_POST['novinka']) && $_POST['novinka'] == 'a') ? 1 : 0;
			$genre = (isset($_POST['genre'])) ? $_POST['genre'] : 0;
			if($hlasy=='' && $new_str=='MID'){$hlasy='0';}
			if($hlasy=='' && $new_str<>'MID'){$hlasy=$new_str;}
			$query="INSERT INTO `mobile_melody` ( `ID` , `interpret` , `nazov` , `file` , `hlasy` , `poznamka`, `alternative`, `new`, `genre`) VALUES ('', '".$interpret."', '".$nazov."', '".$file."', '".$hlasy."', '".$poznamka."', '".$alter."', '".$novinka."', '".$genre."')";
			mysql_query($query, $link);
			$err='Zvonenie vyhovuje kritáriám a bolo pridané';
			//header("location: melody_add.php?u=$u&action=form&err=$err");
		}
		else 
		{
			if(!file_exists($file_tmp) && $action=='add_file')$err='Zadaný súbor neexistuje';
		}
	}	
	else 
	{
		if($action=='add_file')
		{
			$err='Nezadal si názov zvonenia';
	 		header("location: melody_add.php?u=$u&action=form&err=$err");
		}
	}

	?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//SK" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>4mobile - všetko pre Váš mobil</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<link rel="stylesheet" href="style.css">
<style type="text/css">
<!--
.form_name {  font-family: Arial, Helvetica, sans-serif; font-size: 8pt; font-weight: bold; color: #666666; text-decoration: none; text-align: left;}
.form_tlac {  font-family: Arial, Helvetica, sans-serif; font-size: 8pt; font-weight: bold; color: #666666; background-color: #FFFFFF; border: 1px solid #CCCCCC}
.form_text { font-family: Arial, Helvetica, sans-serif; font-size: 8pt; font-weight: normal; color: #666666; text-decoration: none ; text-align: justify; background-color: #FFFFFF; border: #CCCCCC; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px}
.form_text2 { font-family: Arial, Helvetica, sans-serif; font-size: 8pt; font-weight: bold; color: #666666; text-decoration: none ; text-align: justify }
.msg { font-family: Verdana; font-size: 7pt; color:#999999; }
.field1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8pt; color: #CC0000; background-color: #CCCCCC; border: #000000; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px}

-->
</style>
</head>

<body bgcolor="black">
<? include('menu.php'); ?>
<div align='center'>
<? 
	$action=$_GET["action"];
		echo"<div width=100%>
		<form ENCTYPE=\"multipart/form-data\" name=\"q\" action=\"melody_add.php?u=$u&action=add_file\" method=POST>
			<table width=400 border=0 align=center cellspacing=1>
				<tr bgcolor=#222222>
					<td class=form_name>&nbsp;Súbor:</td>
					<td class=form_name style=\" padding-top:3px; padding-bottom:3px;\"><input name=the_file type=file size=29 class=form_text></td>
				</tr>
				<tr bgcolor=#222222>
					<td class=form_name>&nbsp;Interpret:</td>
					<td class=form_name><input type=text name=interpret size=40 class=field1 maxlength=40></td>
				</tr>
				<tr bgcolor=#222222>
					<td class=form_name>&nbsp;*Názov:</td>
					<td class=form_name><input type=text name=nazov size=40 class=field1 maxlength=50></td>
				</tr>
				<tr bgcolor=#222222>
					<td class=form_name>&nbsp;Poznámka:</td>
					<td class=form_name><textarea rows=6 cols=30 name=poznamka style=\"scrolling:auto; scrollbar-face-color: #CCCCCC; scrollbar-shadow-color:#CCCCCC;
	  					scrollbar-highlight-color:#CCCCCC ; 
	  					scrollbar-3dlight-color: #CCCCCC;
	  					scrollbar-darkshadow-color: #CCCCCC; 
	  					scrollbar-track-color: #cccccc;
	  					scrollbar-arrow-color: #CC0000;\" class=field1></textarea></td>
				</tr>
				<tr bgcolor=#222222>
					<td class=form_name>&nbsp;Alternatívny text pre vyhľadávanie:</td>
					<td class=form_name><input type=text name=alter size=40 class=field1 maxlength=150></td>
				</tr>
				<tr bgcolor=#222222>
					<td class=form_name>&nbsp;Formát/hlasy:</td>
					<td class=form_name><input type=text name=hlasy size=3 class=field1 maxlength=3></td>
				</tr>	
				<tr bgcolor=#222222>
					<td class=form_name>&nbsp;Žáner:</td>
					<td class=form_name><select name='genre' class='field1'>";
		  			$melody_genres[15] = 'vianocne';
					foreach ($melody_genres as $kg => $g_name)
					{
						echo "<option value='$kg'>$g_name</option>";
					}
				echo "</select>
				</tr>	
				<tr bgcolor=#222222>
					<td class=form_name>&nbsp;Novinka:</td>
					<td class=form_name><input type='checkbox' name='novinka' class=field1 value='a'></td>
				</tr>			
				<tr bgcolor=#222222>
					<td colspan=2 align=center class=msg> Povolené sú wav, mid, mp3, amr. Pri mid zvonení zadaj počet hlasov, pri ostatných nechaj pole Formát prázdne. Max veľkosť zvonenia je 200 kB.</td>
					
				</tr>
				<tr bgcolor=#222222>
					<td colspan=2 align=center><input type=submit value=OK class=form_tlac style=\"cursor:hand\";>  
          				<input type=reset value=Vymazať class=form_tlac style=\"cursor:hand\";></td>
					
				</tr>
			</table>
		</form></div>
		";
echo"<div align=center class=msg><b>".$err."</b></div>";		
echo"<div align=center class=msg><b>".$_FILES['the_file']['type']."</b></div>";		
			
	
	
	?>

</div>
</body>
</html>
