<?php
global $orders;
global $ticket_txt;
global $dbconnection;
global $dbuser;
global $dbpassword;
global $CFG_PrintEnable; 
include_once '_ini/cfg.php';
//$dbconnection = "odbc:Driver={SQL Server};Server=localhost\sqlexpress,1433;Database=res_data;";
$dbuser="sa";
$dbpassword="densen916423330";
try
{
	if (isset($_REQUEST['logoff']) && $_REQUEST['logoff']==1 ){
		 
		// 把失效日期设置为一小时前
		@setcookie ("useraut", "", time() - 3600);
 		 die ("
	<form name='form1' method='post' action=''>
	  <label>clave
	  <input name='useraut' type='password' id='useraut' value=''>
	  </label>
	  <label>
	  <input type='submit' name='Submit' value='Login'>
	  </label>
	</form>");

	//GUID没设置就die
	}elseif (isset($_COOKIE["useraut"])){
		$user=$_COOKIE['useraut'];		
		//echo 'isset _COOKIE useraut ：'.$user;
	}elseif (isset($_REQUEST['useraut'])){
		$user=$_REQUEST['useraut'];
		//echo 'isset _REQUEST useraut '.$user;
		@setcookie("useraut", $user, time()+3600);
	}elseif (isset($_REQUEST['user1'])){
		$user=$_REQUEST['user1'];
		//echo 'isset _REQUEST user1 '.$user;
		@setcookie("useraut", $user, time()+3600);
	}else{
		$user='jingdu';
		@setcookie("useraut", $user, time()+3600);
	}

	try {
		//$filename="$dbdir/$dbdir.db3";
		//$orders = new PDO('sqlite:'.$filename);
		$orders = new PDO($dbconnection,$dbuser,$dbpassword);
		//$orders->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  //反射错误码和错误信息。 

	}
	//捕获异常
	catch (Exception $e) {
		//echo 'Message: ' .$e->getMessage();
	}
	
	$query_LoadCFG = "SELECT keyname,kevalue FROM configWeb";
	$values        = $orders->query($query_LoadCFG);
	if (is_iterable($values)) {
		foreach ($values as $row_LoadCFG) {
			$row_LoadCFG['kevalue'] = loadFromMssql($row_LoadCFG['kevalue']);
			if ($row_LoadCFG['keyname'] == 'dbName')
				$CFG_dbName = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'SiteName')
				$CFG_SiteName = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'Printer')
				$CFG_Printer = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'statedisabled')
				$CFG_statedisabled = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'tel')
				$CFG_tel = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'adress')
				$CFG_adress = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'adressMap')
				$CFG_adressMap = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'PrinterCook')
				$CFG_PrinterCook = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'PrinterCookII')
				$CFG_PrinterCookII = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'PrinterBar')
				$CFG_PrinterBar = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'ShowDesk')
				$CFG_ShowDesk = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'CookTicketPrnName2')
				$CFG_CookTicketPrnName2 = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'OrderHalfServing')
				$CFG_OrderHalfServing = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'PrintEnable')
				$CFG_PrintEnable = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'QRCodeDeskEnable')
				$CFG_QRCodeDeskEnable = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'QRCodeDeskTOKEN') //餐桌二维码TOKEN  
				$CFG_QRCodeDeskTOKEN = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'QRCodeInsideEnable')
				$CFG_QRCodeInsideEnable = $row_LoadCFG['kevalue'];
			elseif ($row_LoadCFG['keyname'] == 'QRCodeInsideTOKEN') // 店内二维码TOKEN
				$CFG_QRCodeInsideTOKEN = $row_LoadCFG['kevalue'];

		}
	}

		 foreach ($orders->query("select count(*) from sysobjects where id = object_id(N'[msg]') and OBJECTPROPERTY(id, N'IsUserTable') = 1") as $array) {
			$total1=$array[0];
		}
//$total1=0;
		if ($total1==0) {
			$sql="CREATE TABLE [msg] (
			[ID] INTEGER  NOT NULL PRIMARY KEY  IDENTITY(1,1),
			[username] nvarchar(128) NOT NULL,
			[MsgType] nvarchar(128) NOT NULL,
			[msg_txt] nvarchar(128) NULL,
			[AddTime] nvarchar(128)  NULL,
			[ctl] nvarchar(128) NULL, 
			[LastStatus]  nvarchar(128)  NULL
			);";
			$orders->exec($sql); 
		 }
 

		 foreach ($orders->query("select count(*) from sysobjects where id = object_id(N'[users]') and OBJECTPROPERTY(id, N'IsUserTable') = 1") as $array) {
			$total1=$array[0];
		}
//$total1=0;
		if ($total1==0) {

			$sql="CREATE TABLE [users] (
			[ID] INTEGER  NOT NULL PRIMARY KEY  IDENTITY(1,1),
			[sn] nvarchar(128) NOT NULL,
			[GUID] nvarchar(128) NOT NULL,
			[deviceToken] nvarchar(128)  NULL,
			[username] nvarchar(128) NOT NULL,
			[password] nvarchar(128) NOT NULL,
			[CreateDate] DATETIME  NULL,
			[Lang] nvarchar(128) NULL, 
			[last_operation] nvarchar(128) NULL,
			[ver] NUMERIC DEFAULT '1'  NULL,
			[ip] nvarchar(128)  NULL,
			[email] nvarchar(128)  NULL
			);";
			$orders->exec($sql); 
		 }
		 
		foreach ($orders->query("select count(*) from sysobjects where id = object_id(N'[configWeb]') and OBJECTPROPERTY(id, N'IsUserTable') = 1") as $array) {
			$total1=$array[0];
		}
//$total1=0;
		if ($total1==0) {
		$sql="CREATE TABLE configWeb (
	[ID] INTEGER NOT NULL PRIMARY KEY  IDENTITY(1,1),
	[keyname] TEXT NOT NULL,
	[kevalue]TEXT NULL, Descr TEXT);";
			$orders->exec($sql); 
		 }
}catch(Exception $e) {
//echo 'Message: ' .$e->getMessage();
}
function checkfilename($file)
{
	if (!$file) return "";
	$file = trim($file);
	$a = substr($file,-1);
	//$file = preg_replace("^[.\\\/]*","",$file);
	//$file = preg_replace("[.\\\/]*$","",$file);
	$arr = array("../","./","/","\\","..\\",".\\");
	$file = str_replace($arr,"",$file);
	return $file;
}
function getSQLresult($SQlStrLIST){
	global $dbconnection;
	global $dbuser;
	global $dbpassword;
	$result='';
	try {
	$orders = new PDO($dbconnection,$dbuser,$dbpassword);
	$values = $orders->query($SQlStrLIST);
	if (is_iterable($values)) {
		foreach ($values as $p) {
			$result  = loadFromMssql($p['0']);
			
		}
	}
	unset($values);
	unset($orders);
	}catch(Exception $e) {
	//echo 'Message: ' .$e->getMessage();
	}
	return $result;
}
//ob_end_flush(); 
?>
