<?php 
$action = $_POST['action'];

include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");

//7.1.2015 SQL injection
if(class_exists('convert'))
{
    //7.1.2015 SQL injection
    if(isset($_POST['id'])) $_POST['id'] = convert::toAlfaNum($_POST['id']);
    if(isset($_POST['id_node'])) $_POST['id_node'] = convert::toAlfaNum($_POST['id_node']);
    if(isset($_POST['name'])) $_POST['name'] = convert::toAlfaNum($_POST['name']);
    if(isset($_POST['project'])) $_POST['project'] = convert::toAlfaNum($_POST['project']);
    if(isset($_POST['sessId'])) $_POST['sessId'] = convert::toAlfaNum($_POST['sessId']);
    if(isset($_POST['treeName'])) $_POST['treeName'] = convert::toId($_POST['treeName']);
    if(isset($_POST['tree'])) $_POST['tree'] = convert::toId($_POST['tree']);
    if(isset($_POST['uniqueTabId'])) $_POST['uniqueTabId'] = convert::toId($_POST['uniqueTabId']);
    if(isset($_POST['uniqueTabUrl'])) $_POST['uniqueTabUrl'] = convert::toId($_POST['uniqueTabUrl']);
    if(isset($_POST['id_project'])) $_POST['id_project'] = convert::toId($_POST['id_project']);
    
    //7.5.2015 Za tole nisem čisto prepričan, kaj pomeni. Upam da ne bo večjih problemov
    if(isset($_POST['uniqueNotShow']))  $_POST['uniqueNotShow'] = convert::toInt($_POST['uniqueNotShow']);
    if(isset($_POST['tittle'])) $_POST['title'] = convert::toId($_POST['title']);

}

if ( $action == 'remove'  )
{
    //7.1.2015 zakomentiral include in jih dodal na vrh
	//include_once("_dirinfo.php");
	//include_once(PATH_TO_ROOT."init.php");
	$userId = $_SESSION['UserInfo']['id'];
	$tabId = $_POST['sessId'];
	unset($_SESSION['addedTabs'][ $tabId  ]  );
	$sql = "DELETE FROM ".DB_WARPIT_MAIN."._user_tab_state WHERE id_user = $userId AND id_tab = '$tabId'";
	
	$db->SQLexecute($sql);
	echo $db->GetError();
	//$_SESSION['addedTabs'][ $_POST['sessId'] ] = 0 ;
	exit;
}
if ( $action == 'remove_all'  )
{
	echo "mainTabPanel.removeAll();";
	exit;
}elseif($action == 'clean_tabs')
{
    //7.1.2015 zakomentiral include in jih dodal na vrh
   // include_once("_dirinfo.php");
   // include_once(PATH_TO_ROOT."init.php");
    $userId = $_SESSION['UserInfo']['id'];
    unset($_SESSION['addedTabs'][ $tabId  ]  );
    $sql = "DELETE FROM ".DB_WARPIT_MAIN."._user_tab_state WHERE id_user = $userId";
    
    $db->SQLexecute($sql);
    
    if(strlen($db->GetError()) > 0)
    {
        echo "Ext.Msg.show({ title:'Error', msg: '" . $db->getError() ."', icon: Ext.MessageBox.INFO, buttons: Ext.Msg.OK});";
    }
    {
        echo "Ext.Msg.show({ title:'Info', msg: 'Done!', icon: Ext.MessageBox.INFO, buttons: Ext.Msg.OK});";
    }
    //echo $db->GetError();
}
elseif ($action == 'add')
{
	$url = $_POST['uniqueTabUrl'];
	$id = $_POST['uniqueTabId'];

	$sessionHandling = true;
//7.1.2015 zakomentiral include in jih dodal na vrhu
	//include_once("_dirinfo.php");
	//include_once(PATH_TO_ROOT."init.php");
	
	if( !$UserSession->isLoggedin() )
	{
		
		$loginSessionType = 'tabManager';
		include '../_forms/window.sessionTimeout.php';
		
		exit;
	}
	if($_SESSION['addedTabs'][ $id ])
	{
		echo "if( Ext.getCmp('$id'))
		{
		var tmp = Ext.getCmp('$id');
		tmp.show();
		if(tmp.RMreopen)
			tmp.RMreopen(".json_encode($_POST).");
			//console.log('Tukaj');
		
		}
		else
		{
			 Ext.Ajax.request({
					url : '_support/tabManager.php',
					params:{action:'remove',sessId:'$id'},
					success:function(){
						addTabAuto('$id','$url',".json_encode($_POST)." );
					}
				  });
		}";
		exit;
	}
	$_SESSION['addedTabs'][ $id ] = 1;
		
	$jsonEncodedPOST = json_encode($_POST);
	/*ADDED BY Sandi Jersic 30.1.2014 If parameter in $_POST variable is JSON encoded array, it is not properly encoded. It throws
	error when reopening tabs after login ...*/
	//file_put_contents("/var/sandek/log/aheck.txt", print_r($jsonEncodedPOST,true)."\n", FILE_APPEND | LOCK_EX);
	//file_put_contents("/var/sandek/log/aheck.txt", $jsonEncodedPOST."\n", FILE_APPEND | LOCK_EX);
	$sql  = "INSERT IGNORE INTO ".DB_WARPIT_MAIN."._user_tab_state (id_user,id_tab,url,params,tab_parent) VALUES ( ". $_SESSION['UserInfo']['id']. ", '$id','$url' , '".$jsonEncodedPOST."' ,'mainTabPanel' )";
	$db->SQLexecute($sql);
	
	include '../'.$url;
	
	
	
}

?>