<?php require("check.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<title>Administratoriaus prisijungimas</title>
<link href="stilius.css" rel="stylesheet" type="text/css" />

<?php
	require("configas.php");
	global $dbhost, $dbuname, $dbpass, $dbname; 
	mysql_connect($dbhost, $dbuname, $dbpass); 

	mysql_query("SET NAMES utf8");
	@mysql_select_db($dbname) or die ("Unable to select database !"); 
?>
</head>
<body>

<?php

if (isset($_POST['ikelti'])) {

$pav = $_POST['pav'];

$target_path = "../mp3/";

$target_path = $target_path . basename( $_FILES['file']['name']); 
$remote_file = $_FILES['file']['name'];

$ext = substr($remote_file, strrpos($remote_file, '.') + 1);
//if  (($ext == 'mp3') && ($_FILES['file']['type'] == 'audio/mpeg')){
	if ($_FILES['file']['size'] / 1024 < 15000){
		if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
			$raides  = array("ą","č","ę","ė","į","š","ū","ų","ž","Ą","Č","Ė","Ę","Į","Š","Ų","Ū","Ž");
			$keisti    = array("a","c","e","e","i","s","u","u","z","A","C","E","E","I","S","U","U","Z");
			$pavadinimas = str_replace($raides, $keisti, $pav);
			$uzklausa = "INSERT INTO flamplayer_musics (title_music, filename_music) VALUES ('$pavadinimas', '$remote_file')";
			$rezultatas = mysql_query($uzklausa) or die(mysql_error());

			chmod("../mp3/".$_FILES['file']['name'], 0777);
			$ats = "Daina ".  basename( $_FILES['file']['name']). 
			" &#303;kelta.";
		} else{
			$ats = "Klaida &#303;keliant dain&#261;!";
		}
	}else { $ats = "Dainos dydis negali vir&#353;yti 15mb!"; }
//}else { $ats = "&#303;kelti galima tik mp3 formato dainas!"; }

/*
$ftp_server = "127.0.0.1";
$ftp_user = "user";
$ftp_pass = "user";



$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
	
	// jungiamasi prie ftp
	@ftp_login($conn_id, $ftp_user, $ftp_pass); 
	
			$remote_file = $_FILES['file']['name'];
			$file = $_FILES['file']['tmp_name'];
		//tikrinamos ar tenkinamos salygo ir ne/ikelia faila
		if  ($_FILES["file"]["name"] != null){
			$ext = substr($remote_file, strrpos($remote_file, '.') + 1);
			if  (($ext == 'mp3') && ($_FILES['file']['type'] == 'audio/mpeg')){
				if ($_FILES['file']['size'] / 1024 < 20000){
					if (!file_exists("mp3/" . $_FILES["file"]["name"])) {
						ftp_put($conn_id, "mp3/".$remote_file, $file, FTP_BINARY);
						$ats ="Perkeltas failas: " . $_FILES["file"]["name"];
						echo "<meta http-equiv='refresh' content='5' />";
						
						$uzklausa = "INSERT INTO flamplayer_musics (title_music, filename_music) VALUES ('$pav', '$remote_file')";
						$rezultatas = mysql_query($uzklausa) or die(mysql_error());
						
						}else{
						$ats = $_FILES["file"]["name"] . " failas jau yra. ";
						}
				}else{
				$ats = "Maksimalus failo dydis 20Mb";
				echo "<meta http-equiv='refresh' content='5' />";
				}
			}else {
			$ats = "Galima &#303;kelti tik mp3 formato dainas!";
			}			
		}else {
		$ats = "Nenurod&#279;te failo!";
		echo "<meta http-equiv='refresh' content='5' />";
		}
		//ftp_close($conn_id);
	//}
*/
}
?>
<table id="pagr" align="center">
	<tr valign="top">
		<td class="meniu">
			<table class="meniu2" width="200px">
				<?php require("meniu.php"); ?>
			</table>			
		</td>
		<td class="turinys">
			<table border="0" class="turinys2">
				<tr>
					<td class="t1">Dain&#371; talpinimas puslapyje</td>
				</tr>
				<tr>
					<td class="t2" align="center">
						<form action="<? echo $php_self ?>" method="post" enctype="multipart/form-data"><!--failu ikelimo forma-->
						<table align="center" border="0" class="lentele">
							<tr>
								<td colspan="2" align="center"><? echo $ats; ?></td>
							</tr>
								<td><label for="file">Daina:</label></td>
								<td><input type="file" name="file" id="file" /></td>
							</tr>
							</tr>
								<td><label for="koment">Dainos pavadinimas:</label></td>
								<td><input type="text" style="width: 225px" name="pav" id="pav"/></td>
							</tr>
							<tr>
								<td colspan="2" align="center"><input class='button' type="submit" name="ikelti" value="Kopijuoti" /></td>
							</tr>
						</table>
					</form>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>

</body>
</html>