<?php
//<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
//sigurnost:
if (!defined('SIGURICA')) die('Nedozvoljen pristup!');
if ($prava_pristup_admin!=1) die('Nedozvoljen pristup!');

// list expected fields
$expected = array('q_ime', 'q_username', 'q_passwd', 'q_email', 'q_tel');
// set required fields
$required = array('q_ime', 'q_username', 'q_passwd');
// create empty array for any missing fields
$missing = array();

// process the $_POST variables
foreach ($_POST as $key => $value) {
	// assign to temporary variable and strip whitespace if not an array
	$temp = is_array($value) ? $value : trim($value);
	// if empty and required, add to $missing array
	if (empty($temp) && in_array($key, $required)) {
		array_push($missing, $key);
	}
	// otherwise, assign to a variable of the same name as $key
	elseif (in_array($key, $expected)) {
		//${$key} = $temp;
		${$key} = mysql_real_escape_string($temp);
	}
}
$error='';
$uspjeh='';
if (empty($missing))  {
	//check if username već postoji u bazi
	$q_passwd=sha1($q_passwd);
	$sql99="select username from b7_user where username='$q_username' and passwd='$q_passwd'";
	$rez99=mysql_query($sql99,$conn);
	if ($rez99) $num99=mysql_num_rows($rez99);
	if ($num99>0) {
		$error.='U bazi već postoji korisnik sa istim korisničkim imenom i lozinkom!<br />';		
	} else {
		//insert new user in database
		//uredi tekst
		$q_ime=uredi_var($q_ime);
		$result = mysql_query("insert into b7_user values ('','$q_username','$q_passwd','$q_ime','$q_tel','$q_email')");
		if (!$result) {
			$error.='Dogodila se greška pri zapisu u bazu: '.mysql_error();
		} else {
			$new_admin_id = mysql_insert_id();
			
			$uspjeh.='Uspješno dodan zapis u bazu:<br />';
			$uspjeh.='Ime i prezime: '.$q_ime.'<br />';
			$uspjeh.='Korisničko ime: '.$q_username.'<br />';
			$uspjeh.='Lozinka: '.$q_passwd.'<br />';
			$uspjeh.='Telefon: '.$q_tel.'<br />';
			$uspjeh.='e-mail: '.$q_email.'<br />';
		}
		//create user rights		
		$result = mysql_query("insert into b7_users_prava values ('$new_admin_id','','','','','','','','','','','','','')");
		if (!$result) {
			$error.='Dogodila se greška pri zapisu u bazu: '.mysql_error();
		} else {
			$uspjeh.='Nemojte zaboraviti korisniku dodjeliti prava pristupa!<br />';
		}	
	}
} else {
	$error.='U formi nisu ispunjena sva potrebna polja za unos podataka u bazu!';
}
?>
<div id="main_content">
<h1>Spremanje podataka u bazu</h1>
<?php
if ($error) echo '<div class="greska">'.$error.'</div>'."\n";
if ($uspjeh) echo '<div class="uspjeh">'.$uspjeh.'</div>'."\n";
?>
<br />
<br />
<a class="gray_normal toggleopacity" href="glavna.php?modul=admin"><img src="slike/action_back.gif" width="16" height="16" alt="" border="0" /> <b>Povratak</b></a>
<br />
<br />
</div>