.reg or .txt file on windows

1

How can I in PHP create a .reg or .txt file in a local eg. (HKEY_LOCAL_MACHINE), and insert the value of two string variables $ myVarReg01 and $ myVarReg02 into it.

And later open this same file via PHP also and compare the values inside that file with another string string $ myVarComparaReg01 and $ myVarComparaReg01.

?

    
asked by anonymous 03.08.2017 / 06:50

1 answer

1

Unfortunately, you can not do this through PHP. Try VBScript.

Idea:

    VBScript will create a task (if it does not exist) in the Task Scheduler that will execute the same X in X (hours / minutes / seconds)

  • In the same file a HTTPRequest will be done with your service.

    • If this is the first time you run this request it will return the values to be inserted in the Registry or its .txt file.
    • Otherwise, the Registry or .txt data will be passed in this request, which will be dealt with later in php.

I found a very basic version below:

  

Note: This code has not been tested.

Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."

Function post(url, data)
    'Faz o request para o servidor com parametros POST e retorna a resposta
    Dim restReq
    Set restReq = CreateObject ("Msxml2.XMLHttp.6.0")
    restReq.open "POST", url, False
    restReq.setRequestHeader "content-type", "application/x-www-form-urlencoded"
    restReq.send(data)
    post = restReq.responseText
End Function

Function cwrite(path, filename, data)
    'Escreve um ficheiro numa determinada pasta, caso nao exista a pasta ou o ficheiro cria os dois
    Dim objFile, file
    Set objFSO=CreateObject("Scripting.FileSystemObject")

    file = path & filename
    If Not objFSO.FolderExists(path) = True Then  objFSO.CreateFolder path
    If objFSO.FileExists(file) = True Then objFSO.deletefile file, True
    Set objFile = objFSO.CreateTextFile(file,True)
    objFile.Write data
    objFile.Close
End Function

Function existsFile(file)
    Set objFSO=CreateObject("Scripting.FileSystemObject")
    existsFile = objFSO.FileExists(file)
End Function

Function MakeTask(schedule, modifier, title, action)
    'Cria uma tarefa usando SchTasks 
    Set wShell = CreateObject("Wscript.Shell")
    wShell.Run "SchTasks /Create /SC " & schedule & " /MO " & modifier & " /TN """ & title & """ /TR """ & action & """ /F", 0
End Function

Function getRegistryValue(strKeyPath, strValueName)
    'Busca o valor de um determinado registo
    Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\" & _
    strComputer & "\root\default:StdRegProv")

    oReg.GetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,regValue
    Set oReg = Nothing
    If Not regValue = 0 Then
        getRegistryValue = regValue
    Else
        getRegistryValue = Nothing
    End If
End Function

'###
'INICIO DA APLICAÇÃO

Set wShell = CreateObject("Wscript.Shell")

Sys_Drive = wShell.ExpandEnvironmentStrings("%SystemDrive%")
SERVICE_URL = "http://url_do_serviço.com/serviço.php"
FILE_PATH = Sys_Drive & "\"

If Not existsFile(FILE_PATH & "ficheiro.txt") Then
    'EFECTUA O REQUEST PARA O SERVIÇO
    responseBody = post(SERVICE_URL, "data1=value1&data2=value2")
    'ESCREVE O FICHEIRO "ficheiro.txt" COM O VALOR "responseBody"
    cwrite FILE_PATH, "ficheiro.txt", responseBody
    ' CRIA A TAREFA PARA SER EXECUTADA DE 15 EM 15 MINUTOS MakeTask([HOURLY|MINUTE|SECOND], [NUMERIC], [TITULO], [AÇAO])
    MakeTask "MINUTE", 15, "A MINHA TAREFA", "caminho_para_o_ficheiro_vbs/ping.vbs"

Else
    'CASO O FICHEIRO EXISTA ....
End If

DO NOT RUN THE CODE ABOVE HERE LIKE THIS IS VBSCRIPT

    
03.08.2017 / 12:47