-
-
Save haileys/3523806 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once("../CoreSection/database.inc.php"); | |
require_once("RequireFunctions.php"); | |
function UpdateTimeStamp($TimeStamp, $TableName, $idCompany, $DatabaseObject) | |
{ | |
$QueryCondition = "idCompany = " . $idCompany ." and TableName = " . $TableName; | |
$SelectStatus = $DatabaseObject->select("tablestimestamp", "TimeStamp", $QueryCondition); | |
/* | |
$Error = $DatabaseObject->getError(); | |
if(isset($Error) && !empty($Error))//if(!$SelectStatus) | |
return NotificationLibrary(100, $DatabaseObject->getError()); | |
*/ | |
$SelectResult = $DatabaseObject->getResult(); | |
if (sizeof($SelectResult) > 1) { | |
$Querystring = "UPDATE tablestimestamp SET TimeStamp = '" . $TimeStamp . "' WHERE TableName = '" . $TableName . "'"; | |
$Values = array( | |
'TimeStamp' => $TimeStamp | |
); | |
$QueryCondition = array( | |
'TableName = ', | |
$TableName | |
); | |
$UpdateResult = $DatabaseObject->query($Querystring); | |
if(!$UpdateResult) | |
return NotificationLibrary(100, $DatabaseObject->getError()); | |
} else { | |
$Rows = "idCompany,TableName,TimeStamp"; | |
$Values = array( | |
$idCompany, | |
$TableName, | |
$TimeStamp | |
); | |
$InsertStatus = $DatabaseObject->insert("tablestimestamp", $Values, $Rows); | |
if(!$InsertStatus) | |
return NotificationLibrary(100, $DatabaseObject->getError() . "2"); | |
} | |
return true; | |
} | |
function NotificationLibrary($ErrorNumber, $Message) | |
{ | |
$ReturnData = array(); | |
switch ($ErrorNumber) { | |
case 240: | |
$ReturnData['ErrorCode'] = 240; | |
$ReturnData['ErrorMessage'] = "Username Password is Worng"; | |
$ReturnData['ErrorDesc'] = "نام کاربری یا گذرواژه اشتباه است"; | |
break; | |
case 100: | |
$ReturnMessage = "Database Error >> "; | |
if (!empty($Message)) | |
echo $ReturnMessage .= $Message; | |
$ReturnData['ErrorCode'] = 100; | |
$ReturnData['ErrorMessage'] = $ReturnMessage; | |
$ReturnData['ErrorDesc'] = "نام کاربری یا گذرواژه اشتباه است"; | |
break; | |
case 300: | |
$ReturnMessage = "Elements didnt Set "; | |
if (!empty($Message)) | |
echo $ReturnMessage .= $Message; | |
$ReturnData['ErrorCode'] = 100; | |
$ReturnData['ErrorMessage'] = $ReturnMessage; | |
$ReturnData['ErrorDesc'] = "نام کاربری یا گذرواژه اشتباه است"; | |
break; | |
} | |
return json_encode($ReturnData); | |
} | |
function InsertAgents($Data, $idCompany, $DatabaseObject) | |
{ | |
$DatabaseObject->emptyResult(); | |
if (!empty($Data['Visitors'])) { | |
foreach ($Data as $LevelOne) { | |
foreach ($LevelOne as $AgentsVal) { | |
$QueryCondition = "ERPVisitorID = " . $AgentsVal['VisitorERPCode']; | |
$SelectStatus = $DatabaseObject->select("agents", "Count(ERPVisitorID) as Count", $QueryCondition); | |
if (!$SelectStatus) | |
return NotificationLibrary(100, $DatabaseObject->getError()); | |
$ERPResult = $DatabaseObject->getResult(); | |
if ($ERPResult[0]['Count'] == 0) { | |
$Name = $AgentsVal['VisitorName']; | |
$UserName = $idCompany . "." . $AgentsVal['VisitorERPCode'] . "@ptcsfa.ir"; | |
$Password = $AgentsVal['VisitorPass']; | |
$TimeStamp = $AgentsVal['TS']; | |
//---------------- insert into userinfo ----------------// | |
$TableName = "userinfo"; | |
$Rows = "idCompany,UserType,Username,Password,FirstName"; | |
$Values = array( | |
$idCompany, | |
3, | |
$UserName, | |
$Password, | |
$Name | |
); | |
$InsertStatus = $DatabaseObject->insert($TableName, $Values, $Rows); | |
if (!$InsertStatus) | |
return NotificationLibrary(100, $DatabaseObject->getError()); | |
$idUser = mysql_insert_id(); | |
//---------------- insert into agents ----------------// | |
$TableName = "agents"; | |
$Rows = "idUser,idCompany,ERPVisitorID"; | |
$Values = array( | |
$idUser, | |
$idCompany, | |
$AgentsVal['VisitorERPCode'] | |
); | |
$InsertStatus = $DatabaseObject->insert($TableName, $Values, $Rows); | |
if (!$InsertStatus) | |
return NotificationLibrary(100, $DatabaseObject->getError()); | |
} | |
} | |
} | |
if(isset($TimeStamp) && !empty($TimeStamp)) | |
if (UpdateTimeStamp($TimeStamp, "Visitors", $idCompany, $DatabaseObject)) | |
return "Successful"; | |
} | |
} | |
function InsertTags($Data, $idCompany, $DatabaseObject) | |
{ | |
$DatabaseObject->emptyResult(); | |
if (!empty($Data['Tags'])) { | |
foreach ($Data as $LevelOne) { | |
foreach ($LevelOne as $TagsVal) { | |
$QueryCondition = "TagName = " . $TagsVal['Coding_Name'] . " and idCompany = " . $idCompany; | |
$SelectStatus = $DatabaseObject->select("tagsinfo", "Count(TagName) as Count", $QueryCondition); | |
if (!$SelectStatus) | |
return NotificationLibrary(100, $DatabaseObject->getError()); | |
$TagResult = $DatabaseObject->getResult(); | |
if ($TagResult[0]['Count'] == 0) { | |
$TagName = $TagsVal['Coding_Name']; | |
$ERPCode = $TagsVal['TagERPCode']; | |
//---------------- insert into userinfo ----------------// | |
$TableName = "tagsinfo"; | |
$Rows = "idCompany,TagName,ERPCode"; | |
$Values = array( | |
$idCompany, | |
$TagName, | |
$ERPCode | |
); | |
$InsertStatus = $DatabaseObject->insert($TableName, $Values, $Rows); | |
if (!$InsertStatus) | |
return NotificationLibrary(100, $DatabaseObject->getError()); | |
} | |
} | |
} | |
if(isset($TimeStamp) && !empty($TimeStamp)) | |
if (UpdateTimeStamp($TimeStamp, "Tags", $idCompany, $DatabaseObject)) | |
return "Successful"; | |
} | |
} | |
function InsertCustomers($Data, $idCompany, $DatabaseObject) | |
{ | |
$DatabaseObject->emptyResult(); | |
if (!empty($Data['Customers'])) { | |
foreach ($Data as $LevelOne) { | |
foreach ($LevelOne as $CustomersVal) { | |
$QueryCondition = "CustomerErpCode = " . $CustomersVal['CustomerErpCode'] . " and idCompany = " . $idCompany; | |
$SelectStatus = $DatabaseObject->select("customers", "Count(CustomerErpCode) as Count", $QueryCondition); | |
if (!$SelectStatus) | |
return NotificationLibrary(100, $DatabaseObject->getError()); | |
$CustomerResult = $DatabaseObject->getResult(); | |
if ($CustomerResult[0]['Count'] == 0) { | |
$CustomerName = (string)$CustomersVal['T_Name']; | |
$Credit = (string)$CustomersVal['T_Credit']; | |
$RFID = (string)$CustomersVal['RFID']; | |
$ReturnCheck = (string)$CustomersVal['HasReturnChk']; | |
$EldestDue = (string)$CustomersVal['EldestDue']; | |
$Address = (string)$CustomersVal['T_Address']; | |
$PhoneNumber = (string)$CustomersVal['T_Tel']; | |
$MobileNumber = (string)$CustomersVal['MobileNum']; | |
$LastOrderDate = (string)$CustomersVal['LastOrderDate']; | |
$RemainCredit = (string)$CustomersVal['CreditMondeh']; | |
$Balance = (string)$CustomersVal['ThMandeh']; | |
$LiquidationDate = (string)$CustomersVal['RaasDate']; | |
$PaymentType = (string)$CustomersVal['PayLength']; | |
$CustomerErpCode = (string)$CustomersVal['CustomerErpCode']; | |
$TimeStamp = $CustomersVal['timeStampGUID']; | |
//---------------- insert into customers ----------------// | |
$TableName = "customers"; | |
$Rows = "CustomerName,idCompany,Credit,RFID,ReturnCheck,EldestDue,Address,PhoneNumber,MobileNumber,LastOrderDate,RemainCredit,Balance,LiquidationDate,PaymentType,CustomerErpCode"; | |
$Values = array( | |
$CustomerName,$idCompany,$Credit,$RFID,$ReturnCheck,$EldestDue, | |
$Address,$PhoneNumber,$MobileNumber,$LastOrderDate, | |
$RemainCredit,$Balance,$LiquidationDate,$PaymentType,$CustomerErpCode | |
); | |
$InsertStatus = $DatabaseObject->insert($TableName, $Values, $Rows); | |
if (!$InsertStatus) | |
return NotificationLibrary(100, $DatabaseObject->getError()); | |
} | |
} | |
} | |
$TimeStamp = '19815706'; | |
if(isset($TimeStamp) && !empty($TimeStamp)) | |
if (UpdateTimeStamp($TimeStamp, "customers", $idCompany, $DatabaseObject)) | |
return "Successful"; | |
} | |
} | |
function ImportData($idCompany, $SessionString, $Data, $DatabaseObject) | |
{ | |
if (SessionCheck($SessionString, $DatabaseObject)) { | |
if (array_key_exists('Visitors', $Data)) { | |
$ReturnData = InsertAgents($Data, $idCompany, $DatabaseObject); | |
if ($ReturnData == "Successful") | |
return true; | |
else | |
return $ReturnData; | |
} | |
if (array_key_exists('Tags', $Data)) { | |
$ReturnData = InsertTags($Data, $idCompany, $DatabaseObject); | |
if ($ReturnData == "Successful") | |
return true; | |
else | |
return $ReturnData; | |
} | |
if (array_key_exists('Customers', $Data)) { | |
$ReturnData = InsertCustomers($Data, $idCompany, $DatabaseObject); | |
if ($ReturnData == "Successful") | |
return true; | |
else | |
return $ReturnData; | |
} | |
} | |
} | |
function getRandomCharString($length, $lower = true, $upper = true, $nums = true, $special = false) | |
{ | |
$pool_lower = 'abcdefghijklmopqrstuvwxyz'; | |
$pool_upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$pool_nums = '0123456789'; | |
$pool_special = '!$%^&*+#~/|'; | |
$pool = ''; | |
$res = ''; | |
if ($lower === true) { | |
$pool .= $pool_lower; | |
} | |
if ($upper === true) { | |
$pool .= $pool_upper; | |
} | |
if ($nums === true) { | |
$pool .= $pool_nums; | |
} | |
if ($special === true) { | |
$pool .= $pool_special; | |
} | |
if (($length < 0) || ($length == 0)) { | |
return $res; | |
} | |
srand((double) microtime() * 1000000); | |
for ($i = 0; $i < $length; $i++) { | |
$charidx = rand() % strlen($pool); | |
$char = substr($pool, $charidx, 1); | |
$res .= $char; | |
} | |
return $res; | |
} | |
function CreateSessionString($idCompany, $DatabaseObject) | |
{ | |
$SessionString = getRandomCharString(40, true, true, true, false); | |
$SessionString .= date('U'); | |
$ExpTimeStamp = 0; | |
$Rows = "SessionString,idCompany"; | |
$Values = array( | |
$SessionString, | |
$idCompany | |
); | |
$InsertStatus = $DatabaseObject->insert("missessionstring", $Values, $Rows); | |
if (!$InsertStatus) | |
return ("Error in inserting"); | |
else | |
return $SessionString; | |
} | |
function Login($Username, $Password, $DatabaseObject) | |
{ | |
$DatabaseObject->emptyResult(); | |
$QueryCondition = "Username = '" . $Username . "' and Password = '" . $Password . "' and UserType = 2"; | |
$SelectStatus = $DatabaseObject->select("userinfo", "idCompany", $QueryCondition); | |
if (!$SelectStatus) | |
return false; | |
$idCompany = $DatabaseObject->getResult(); | |
if (sizeof($idCompany) == 0) | |
return ("Error : 400"); | |
$idCompany = $idCompany[0]['idCompany']; | |
$SessionString = CreateSessionString($idCompany, $DatabaseObject); | |
if ($SessionString == "Error in inserting") | |
return "Error in inserting"; | |
$Return['idCompany'] = $idCompany; | |
$Return['SessionString'] = $SessionString; | |
return json_encode($Return); | |
} | |
function SessionCheck($SessionString, $DatabaseObject) | |
{ | |
$QueryCondition = "SessionString = '" . $SessionString . "'"; | |
$SelectStatus = $DatabaseObject->select("missessionstring", "idCompany", $QueryCondition); | |
if (!$SelectStatus) | |
return false; | |
$idCompany = $DatabaseObject->getResult(); | |
if (sizeof($idCompany) == 0) | |
return ("Error : 400"); | |
$idCompany = $idCompany[0]['idCompany']; | |
return (true); | |
} | |
function TSRequest($idCompany, $TableName, $DatabaseObject) | |
{ | |
$DatabaseObject->emptyResult(); | |
$Return = array(); | |
foreach ($TableName as $TableVal) { | |
$QueryString = "TableName = '" . $TableVal['tableName'] . "' and idCompany = " . $idCompany; | |
$SelectStatus = $DatabaseObject->select("tablestimestamp", "TimeStamp", $QueryString); | |
if (!$SelectStatus) | |
return ("Error in Selecting TimeStamp >> " . $DatabaseObject->getError()); | |
$SelectResult = $DatabaseObject->getResult(); | |
if (!empty($SelectResult)) { | |
$Return[$TableVal['tableName']] = $SelectResult[0]['TimeStamp']; | |
} else { | |
$Return[$TableVal['tableName']] = 0; | |
} | |
$DatabaseObject->emptyResult(); | |
} | |
return json_encode($Return); | |
} | |
if (isset($_POST['MISRequest'])) { | |
$MISRequest = json_decode($_POST['MISRequest'], true); | |
$DatabaseObject = new Database(); | |
$DatabaseObject->connect(); | |
switch ($MISRequest['Type']) { | |
case "Login": | |
if (!empty($MISRequest['Username']) and !empty($MISRequest['Password'])) { | |
$Username = $MISRequest['Username']; | |
$Password = $MISRequest['Password']; | |
$returnString = Login($Username, $Password, $DatabaseObject); | |
echo ($returnString); | |
} else { | |
die("Error : 115"); //Elements Didnt Set | |
} | |
break; | |
case "TSRequest": | |
var_dump($MISRequest);die(); | |
if (!empty($MISRequest['idCompany']) and !empty($MISRequest['SessionString'])) { | |
$SessionString = $MISRequest['SessionString']; | |
$idCompany = $MISRequest['idCompany']; | |
$TableName = $MISRequest['TableNames']; | |
if (SessionCheck($SessionString, $DatabaseObject)) { | |
$returnString = TSRequest($idCompany, $TableName, $DatabaseObject); | |
echo ($returnString); | |
} else { | |
echo ("Error : 911"); | |
} | |
} else { | |
die("Error : 115"); //Elements Didnt Set | |
} | |
break; | |
} | |
} else { | |
$ReturnString = json_encode(CreateMessage(201, false)); | |
die($ReturnString); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment