<?
	$g_path = '../my/';
	
	include $g_path."u_globals.php";
	include $g_path."html/f.php";
	include $g_path."u_spoj_na_bazu.php";
	
	include $g_path."u_headers.php";
	
	if(!$_SESSION['__id']){
		//exit;
	}
	
	if($_POST){
		if(strstr($_SERVER['HTTP_REFERER'], $g_path_sl.'res/file_upload')){
			$l_file = $_FILES['imgFile']['tmp_name'];
			$l_size = $_FILES['imgFile']['size'];
			$l_name = $_FILES['imgFile']['name'];
			
			if($l_file && $l_name){
				$l_dest_name = "";
				
				$la_name = explode(".", $l_name);
				for($i = 0; $i < sizeof($la_name) - 1; $i++){
					$l_dest_name .= $la_name[$i];
				}
				$l_dest_name .= "-".u_datum(0, 1).u_sat().u_minuta().u_sekunda().rand(10000000000, 99999999999).".".$la_name[sizeof($la_name) - 1];
				
				if($_GET['id'] && $_GET['id'] != '-1'){
					$l_person_id = get_item('select rbr_users_outer_db from zaposlenik where guid = "'.$_GET['id'].'" ');
				}
				if(!$l_person_id){
					$l_person_id = $_SESSION['__id'];
				}
				
				$l_dest = '../res/files/'.mk_f_name($l_person_id).'/'.$l_dest_name;
				
				/*
				print $_GET['id']." -- <br>";
				print $l_person_id." -- <br>";
				print $l_dest." -- <br>";
				exit;
				*/
				
				$is_ok = move_uploaded_file($l_file, $l_dest);
				
				header('location:file_upload.php?upload=done&rbr='.$_GET['rbr'].'&uploaded='.substr($l_dest, 3).'&is_redak_tab='.$_GET['is_redak_tab'].
					'&type='.$_GET['type'].'&file_flag='.$_GET['file_flag']);
			}else{
				header('location:file_upload.php?upload=error&file_flag='.$_GET['file_flag']);
			}
			
			exit;
		}else{
			print "error 9874";
			exit;
		}
	}

	
	if($_GET['upload'] == 'done'){
?>
		<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
		<html>
		<head>
			<link rel='stylesheet' type='text/css' href="css/file_upload.css">
			<script type="text/javascript" src="<?=$g_path_sl?>res/js/f-file-upload-2.js"></script>
		</head>
		<body>
			Upload is done.
<?
			if($_GET['type'] == 1){
?>
				<a href="#" onclick="new_file_close(<?=$_GET['rbr']?>, '<?=$_GET['uploaded']?>', '<?=$_GET['is_redak_tab']
					?>', <?=$_GET['type']?>, <?=$_GET['file_flag']?>); return false;">Potvrdi unos dokumenta i zatvori prozor</a>
					
					<br>
					<br>
					<br>
<?
			}
			if($_GET['type'] == 2){
?>
				<a href="#" onclick="new_file_close(<?=$_GET['rbr']?>, '<?=$_GET['uploaded']?>', '<?=$_GET['is_redak_tab']
					?>', <?=$_GET['type']?>, <?=$_GET['file_flag']?>); return false;">Potvrdi unos slike i zatvori prozor</a><br>
				<br>
				<img src='<?=$g_path_sl?><?=$_GET['uploaded']?>' border='0' class="imgUploaded"><br>
<?
			}
?>
			<br>
			<br>
		</body>
		</html>
<?
	}else{
		if($_GET['upload'] == 'error'){
?>
			<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
			<html>
			<head>
				<link rel='stylesheet' type='text/css' href="css/file_upload.css">
				<script type="text/javascript" src="<?=$g_path_sl?>res/js/f-file-upload-2.js"></script>
			</head>
			<body>
				Upload nije uspio.
				<a href="#" onclick="new_file_close(-1, '', '<?=$_GET['is_redak_tab']?>', 0, <?=$_GET['file_flag']?>); return false;">Zatvori prozor</a>
				<br>
				<br>
			</body>
			</html>
<?
		}else{
			ob_start();
?>
				<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
				<html>
				<head>
					<link rel='stylesheet' type='text/css' href="css/file_upload.css">
					<script type="text/javascript" src="<?=$g_path_sl?>res/js/f-file-upload-2.js"></script>
				</head>
				<body>
					<form name='fuForma' method='post' action='file_upload.php?rbr=<?=$_GET['rbr']?>&is_redak_tab=<?=$_GET['is_redak_tab']
						?>&type=<?=$_GET['type']?>&id=<?=$_GET['id']?>&file_flag=<?=$_GET['file_flag']?>' enctype='multipart/form-data'>
						<input type="hidden" name="MAX_FILE_SIZE" value="4194304">
						Odaberite datoteku koju želite dodati<br>
						<br>
						<input type='file' name='imgFile' value=''><br>
						<br>
						<center><button title="Upload image" onclick="document.fuForma.submit()">Upload</button></center>
						<br><br>
						<a href="#" onclick="new_file_close(-1, '', '<?=$_GET['is_redak_tab']?>', 0, <?=$_GET['file_flag']?>); return false;">Zatvori prozor</a>
					</form>
				</body>
				</html>
<?
			$output = ob_get_contents();
			ob_end_clean();
			print  $output;
		}
	}
?>