#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <FTPEx.au3>
#include <GuiListView.au3>

Global $sServerName = "", $sServerUser = "", $sServerPass = "", $sLocalPfad = @TempDir & "\programmupdate.ini" ;Alles richtig eintragen sonst gibts fehler

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Adminskript", 274, 282)
$ListView1 = GUICtrlCreateListView("IP-Adresse|Zeit|Zugang", 8, 8, 258, 150)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 54)
$Button1 = GUICtrlCreateButton("Aktualisieren", 8, 168, 123, 25, BitOR($BS_DEFPUSHBUTTON, $WS_GROUP))
$Group1 = GUICtrlCreateGroup("Zugang", 146, 168, 123, 105)
$Radio1 = GUICtrlCreateRadio("True (Erlaubt)", 154, 184, 81, 17)
$Radio2 = GUICtrlCreateRadio("False (nicht erlaubt)", 154, 208, 113, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$Button2 = GUICtrlCreateButton("Zugang ändern", 154, 240, 107, 25, $WS_GROUP)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button3 = GUICtrlCreateButton("update.ini downloaden", 8, 200, 123, 25, $WS_GROUP)
$Button4 = GUICtrlCreateButton("Schliessen", 8, 232, 123, 25, $WS_GROUP)
$Label1 = GUICtrlCreateLabel("Zuletzt aktualisiert: N/A", 3, 265, 140)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

_Refresh()

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			_Exit()
		Case $Button1
			_Refresh()
		Case $Button2
			_ChangeAccess()
		Case $Button3
			_GetUpdateINI()
		Case $Button4
			_Exit()
	EndSwitch
WEnd

Func _Refresh()
	Local $aRead, $sIP, $sTime, $sAccess, $lGet
	$lGet = InetGet("http://" & $sServerName & "/update.ini", $sLocalPfad, 1)
	If $lGet = 0 Then
		MsgBox(262144 + 16, "Fehler", "Die update.ini konnte nicht geladen werden bzw. ist leer!")
	Else
		Dim $aRead[_FileCountLines($sLocalPfad)]
		_FileReadToArray($sLocalPfad, $aRead)
		FileDelete($sLocalPfad)
		_ArrayDelete($aRead, 0)
		_GUICtrlListView_DeleteAllItems($ListView1)
		For $i = 0 To UBound($aRead) - 1 Step +3
			$sIP = StringTrimLeft($aRead[$i], 1)
			$sIP = StringTrimRight($sIP, 1)
			$sTime = StringReplace($aRead[$i + 1], "zeit=", "", 0, 1)
			$sAccess = StringReplace($aRead[$i + 2], "zugriff=", "", 0, 1)
			GUICtrlCreateListViewItem($sIP & "|" & $sTime & "|" & $sAccess, $ListView1)
		Next
		GUICtrlSetData($Label1, "Zuletzt aktualisiert: " & @HOUR & ":" & @MIN & " Uhr")
	EndIf
EndFunc   ;==>_Refresh

Func _ChangeAccess()
	Local $aReadSel, $sStatus, $liNetSession, $lConnSession
	InetGet("http://" & $sServerName & "/update.ini", $sLocalPfad, 1)
	$aReadSel = _GUICtrlListView_GetItemTextArray($ListView1)
	If $aReadSel[0] = 0 Then
		MsgBox(262144 + 16, "Fehler", "Sie haben keine IP ausgewählt!")
	Else
		_ArrayDelete($aReadSel, 0)
		If BitAND(GUICtrlRead($Radio1), $GUI_CHECKED) = $GUI_CHECKED Then
			$sStatus = "True"
		Else
			$sStatus = "False"
		EndIf
		IniWrite($sLocalPfad, $aReadSel[0], "zugriff", $sStatus)
		$liNetSession = _FTP_Open("Zugang ändern")
		$lConnSession = _FTP_Connect($liNetSession, $sServerName, $sServerUser, $sServerPass)
		_FTP_FilePut($lConnSession, $sLocalPfad, "update.ini")
		_FTP_Close($liNetSession)
		FileDelete($sLocalPfad)
		_Refresh()
	EndIf
EndFunc   ;==>_ChangeAccess

Func _GetUpdateINI()
	Local $sSaveTo, $lGet
	_Refresh()
	$sSaveTo = FileSaveDialog("Downloaden nach...", @WorkingDir, "Initialisierungsdatei (*.ini)", 16, "update.ini")
	If Not StringRight($sSaveTo, 4) = ".ini" Then $sSaveTo = $sSaveTo & ".ini"
	$lGet = InetGet("http://" & $sServerName & "/update.ini", $sSaveTo, 1)
	If $lGet = 0 Then
		MsgBox(262144 + 16, "update.ini-Download", "Die update.ini konnte nicht unter '" & $sSaveTo & "' gespeichert werden!")
	Else
		MsgBox(262144 + 64, "update.ini-Download", "Die update.ini wurde erfolgreich unter '" & $sSaveTo & "' gespeichert!")
	EndIf
	_Refresh()
EndFunc   ;==>_GetUpdateINI

Func _Exit()
	GUIDelete()
	Exit
EndFunc   ;==>_Exit
