;Lizens.au3

;~ #include-once <Crypt.au3>

Global $sAPPName = "AufmaßCreater"
Global $sPassAPP = "FKC0D!nG2021"

func _Lizens_GET_UserID()
	$sUser = IniRead(@ScriptDir & "\daten\conf.ini", "Firma", "User", "NA")
	$id = @ComputerName &"_" & @UserName&"_" &  @CPUArch & @OSArch & @OSVersion
;~ 	ConsoleWrite($id & @CRLF)
	$IDMD5 = _md5($sAPPName & $id & $sPassAPP)
;~ 	ConsoleWrite($IDMD5 & @CRLF)
	IniWrite(@ScriptDir & "\daten\conf.ini", "Firma", "User", $IDMD5)
	Return $IDMD5
EndFunc


Func _creatLizenDatei()
	$sSerial = _license_userid()
	_register_serial($sSerial)
	_encryptLicensFile()
	FileDelete(@ScriptDir & "\" & _licenseFile(True))
EndFunc

func _license_userid()
	$id = @ComputerName &"_" & @UserName&"_" &  @CPUArch & @OSArch & @OSVersion
;~ 	ConsoleWrite($id & @CRLF)
	$IDMD5 = _md5($id)
;~ 	ConsoleWrite($IDMD5 & @CRLF)
	Return $IDMD5
EndFunc

Func _md5($str) ; md5 encryption
	Return StringTrimLeft(_Crypt_HashData($str, $CALG_MD5), 2)
EndFunc

Func _register_serial($sSerial)
	IniWrite(@ScriptDir & "\" & _licenseFile(True), "license", "serial", $sSerial)
EndFunc

Func _licenseFile($returnAppData = True) ; returns license file location
	$file = _md5($sAPPName & _license_userid() & $sPassAPP) & ".dat"
;~ 	$file = @ScriptDir & "\" & $file
	If $returnAppData Then 
	EndIf 
	Return $file
EndFunc

func _encryptLicensFile()
	 _Crypt_EncryptFile(@ScriptDir & "\" & _licenseFile(True), @ScriptDir & "\daten\" & _licenseFile(True), $sPassAPP, $CALG_RC4)
EndFunc

func _decryptLicensFile()
	_Crypt_DecryptFile(@ScriptDir & "\daten\" & _licenseFile(True), @ScriptDir & "\" & _licenseFile(True), $sPassAPP, $CALG_RC4)
	$sLizensLocal =  IniRead(@ScriptDir & "\" & _licenseFile(True), "license", "serial", "")
	FileDelete(@ScriptDir & "\" & _licenseFile(True))
	Return $sLizensLocal
EndFunc

func _UserHash2Ini()
	$sUser = _licenseFile(True)
	$aUser = StringRegExp($sUser, '(.*).dat', 3)
	if IsArray($aUser) Then 
		IniWrite(@ScriptDir & "\daten\conf.ini", "Firma","User",$aUser[0])
		Return $aUser[0]
	Else 
		IniWrite(@ScriptDir & "\daten\conf.ini", "Firma","User","N/A")
	EndIf
EndFunc

func _getSerialFromServer()
	Global $hSession = _WinHttpOpen("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0")

	Global $aHosts[2][2]
	$aHosts[1][0] = "creater.fk-coding.de"
	$aHosts[1][1] = _WinHttpConnect($hSession, $aHosts[1][0])
	Global $sHeader = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" & @CRLF & "Accept-Language: de,en-US;q=0.7,en;q=0.3" & @CRLF & "Accept-Encoding: gzip, deflate, br" & @CRLF

	$sLink = IniRead(@ScriptDir & "\daten\conf.ini", "Firma", "Data", "xxx")
	$sUser = IniRead(@ScriptDir & "\daten\conf.ini", "Firma", "User", "xxx") 

	$sHTML = _send_Winhttp_GET_low($aHosts[1][1], "/FA/" &$sLink &"/" &$sUser &".dat" , "https://local-Tool/")
;~ 	ConsoleWrite($sHTML& @CRLF)
;~ 	MsgBox(64, "", "")
	$aServerLizens = StringRegExp($sHTML, 'serial=(.*)', 3)
	if IsArray($aServerLizens) Then 
		$sServerLizens = $aServerLizens[0]
		Return $sServerLizens
	else 
		return "noSerieal"
	EndIf
	
EndFunc

Func _send_Winhttp_GET_low($sHost, $sLink, $sReferer)
	Local $sHeader = "Content-Type: application/x-www-form-urlencoded"
	$aHTML = _WinHttpSimpleRequest($sHost, "GET", $sLink, $sReferer, Default, $sHeader, True, 1)
;~ 	_Write_to_Console("laden| " & $aHosts[1][0] & $sLink)
	If IsArray($aHTML) Then
		$aHTML[1] = StringReplace($aHTML[1], "amp;", "")
		$aHTML[1] = BinaryToString($aHTML[1], 4)
		
		Return $aHTML[1]
	Else
		$aHTML = _WinHttpSimpleRequest($sHost, "GET", $sLink, $sReferer)
		$aHTML = StringReplace($aHTML, "amp;", "")
		$aHTML = BinaryToString($aHTML, 4)
		Return $aHTML
	EndIf
EndFunc   ;==>_send_Winhttp_GET

Func _send_Winhttp_GET_DatUm($sHost, $sLink, $sReferer)
	Local $sHeader = "Content-Type: application/x-www-form-urlencoded"
	$aHTML = _WinHttpSimpleRequest($sHost, "GET", $sLink, $sReferer, Default, $sHeader, True, 1)
;~ 	_Write_to_Console("laden| " & $aHosts[1][0] & $sLink)
	If IsArray($aHTML) Then
		$aHTML[1] = StringReplace($aHTML[1], "amp;", "")
;~ 		$aHTML[1] = BinaryToString($aHTML[1], 4)
		
		Return $aHTML[1]
	Else
		$aHTML = _WinHttpSimpleRequest($sHost, "GET", $sLink, $sReferer)
		$aHTML = StringReplace($aHTML, "amp;", "")
;~ 		$aHTML = BinaryToString($aHTML, 4)
		Return $aHTML
	EndIf
EndFunc   ;==>_send_Winhttp_GET



