Jump to content

MySQL + Key Protection


 Share

Recommended Posts

I made a program that I would like to give out to some people but I just don't want it to spread out too much. So my goal is to keep it somewhat private. I've been looking around for methods to do this and I have tried a couple methods so far but for some reason they just aren't working for me. First I guess I should give a little explanation of what I'm going for.

I created a MySQL database on my web server that is hosted by another company so it's not a local database. I created the table with such things as keys, name, active, etc. The first thing I need help on is trying to just be able to query the database with autoit. I've tried two methods so far and each one just doesn't connect for some reason. The first I tried was from this post here:

http://www.autoitscript.com/forum/index.ph...89&hl=mysql

It gives me an error at the first IConnect section of just trying to go to the site saying it's unable to connect, however I can go to the php page I set up just fine and it prints what it's supposed to.

I then tried the UDF thing from here:

http://www.autoitscript.com/forum/index.ph...14&hl=mysql

On trying to run it it gives me the "$Objconn.open ("DRIVER=" & $sDriver & ";SERVER=" & $sServer & ";DATABASE=" & $sDatabase & ";UID=" & $sUsername & ";PWD=" & $sPassword & ";PORT="&$iPort)^ ERROR" error. I installed the ODBC driver as the post said properly.

So would anyone have any suggestions as to why these might not be working or some possible alternatives? I'm pretty sure I configured them correctly and I just can't seem to find out why it's not working properly.

My second thing I need help with after I get the first issue straightened out would be just having a popup textbox to enter the key code the first time the program is ran and then save it somewhere for that computer that they can't go back and change it or someone know where the variable is stored and throw it in there later.

Any help would greatly be appreciated, been messing with the MySQL stuff for like 5 hours now and each thing has lead to a dead end and I can't seem to figure it out.

Link to comment
Share on other sites

Usually remote webhosts will not allow you to access your mysql database remotly.

Try making a php script that connects to the database, and feed it info via GET inputs, then process the results (What the php script returns to you).

Link to comment
Share on other sites

Yeah I was looking for an example on how to do that. Like I had said above I tried the example posted in the other forum for doing something like that, but it was giving me the error it couldn't even connect to the site at all. I used the one from here with the variables:

http://www.autoitscript.com/forum/index.ph...89&hl=mysql

I get the error message from the first If statement on the $esocket = IConnect($ehost) part which seems odd considering it's pretty hard to simply type my web site address the wrong way... Was trying to see if maybe there was some other example scripts on doing this same sort of thing.

Link to comment
Share on other sites

Ok I was working with the script and looking at the UDF he took these functions from and saw that he didn't put the TCPStartup() thing in there which was causing it to not connect to the site at all. He then also used TimerStart instead of TimerInit by mistake or something. Anyways after fixing those two things I am getting an odd error that I'm not too sure of how to fix with this. Here's the code.

;   AutoIT to MySQL via PHP Example (passing variabls)
;
;   Example PHP Page: variable.php
;   02-27-07
;   By xwinterx@roadrunner.com
;
;   Summary: Retrieves and displays data from a MySQL database referencing a PHP page
;       stored on your webserver. This example passes two variables to be used by the
;       page to get the requested data.
;
;   Note: IPost, IConnect, IClose, IRead, and IEncodeString are simply the great HTTP UDFs
;       found in the AutoIT forums. There were renamed for my own personal references. I
;       did not make them nor do I take credit for them!
 
 
; Dims $phpdata as an array that will be used to retrieve your data
 Dim $phpdata
; This is the URL to your website
 Global $ehost = "www.13adkarma.com"
; This is the URL to your PHP page
 Global $epage = "/bot.php"
; This is the socket created by IConnect
 Global $esocket
; This is the "variable pass" variable if using variables
 Global $evars
; This is the name of the table which we want to get data from
 Global $bottype = "avbot";- $bottype declared in bot script
; This is the key we are using to determine which row to get data from
 Global $version = "1";- Declared in bot script
 
; This is the array that receives the data from the webpage
 Global $erecv
 
 TCPStartup()
; Variables used by the HTTP UDFs
 Global $_HTTPUserAgent = "AutoItScript/"&@AutoItVersion
 Global $_HTTPLastSocket = -1
 Global $_HTTPRecvTimeout = 5000
 
; Opens Socket to host
 $esocket = IConnect($ehost)
 If @error Then
     MsgBox(4096, "Connection Error", "Unable to connect to: " & $ehost)
     Exit
 Else
; These variables are appended to the end of your total url. These values are "passed"
; to the PHP page to determine table name and id. Here we are using "table_name" for our
; table name and "1" as our id. This means we want the row from the table "table_name"
; that has "1" in the id (vid) field.
     $evars = "bot=" & $bottype & "&version=" & $version
; Encodes the passed variables for a more reliable reference
     $evars = IEncodeString($evars)
; POSTs to website for data retrieval
     IPost($ehost, $epage, $esocket, $evars)
     If @error Then
         MsgBox(4096, "Connection Error", "Unable to connect to: " & $ehost)
         Exit
     Else
; Recieves Return from IPost and assigns it to $erecv
         $erecv = IRead($esocket,1)
; Splits our data that was echoed by our webpage
         $phpdata = StringSplit($erecv[4], "~")
; $phpdata[2] => id field
; $phpdata[3] => name field
; $phpdata[4] => email field
         MsgBox(4096, "Results", "Version: " & $phpdata[2] & @CRLF _
             & "Active?: " & $phpdata[3])
     EndIf
 EndIf
 
 Func IConnect($host, $port = 80)
     Dim $ip = TCPNameToIP ( $host )
     Dim $socket = TCPConnect ( $ip, 80 )
 
     If ($socket == -1) Then
         SetError(1, @error)
         Return -1
     EndIf
     
     $_HTTPLastSocket = $socket
     SetError(0)
     Return $socket
 EndFunc
 
 Func IClose($socket = -1)
     If $socket == -1 Then
         If $_HTTPLastSocket == -1 Then
             SetError(1)
             Return 0
         EndIf
         $socket = $_HTTPLastSocket
     EndIf
     TCPCloseSocket($socket)
     
     SetError(0)
     Return 1
 EndFunc
 
 Func IPost($host, $page, $socket = -1, $data = "")
     Dim $command
     
     If $socket == -1 Then
         If $_HTTPLastSocket == -1 Then
             SetError(1)
             Return
         EndIf
         $socket = $_HTTPLastSocket
     EndIf
     
     Dim $datasize = StringLen($data)
     
     $command = "POST "&$page&" HTTP/1.1"&@CRLF
     $command &= "Host: " &$host&@CRLF
     $command &= "User-Agent: "&$_HTTPUserAgent&@CRLF
     $command &= "Connection: close"&@CRLF
     $command &= "Content-Type: application/x-www-form-urlencoded"&@CRLF
     $command &= "Content-Length: "&$datasize&@CRLF
     $command &= ""&@CRLF
     $command &= $data&@CRLF
     
     Dim $bytessent = TCPSend($socket, $command)
     
     If $bytessent == 0 Then
         SetExtended(@error)
         SetError(2)
         return 0
     EndIf
     
     SetError(0)
     Return $bytessent
 EndFunc
 
 Func IRead($socket = -1, $flag = 0)
     If $socket == -1 Then
         If $_HTTPLastSocket == -1 Then
             SetError(1)
             Return
         EndIf
         $socket = $_HTTPLastSocket
     EndIf
     
     Dim $timer = TimerInit()
     Dim $performancetimer = TimerInit()
     Dim $downloadtime = 0
     
     Dim $headers[1][2]; An Array of the headers found
     Dim $numheaders = 0; The number of headers found
     Dim $body = ""; The body of the message
     Dim $HTTPVersion; The HTTP version of the server (almost always 1.1)
     Dim $HTTPResponseCode; The HTTP response code like 200, or 404
     Dim $HTTPResponseReason; The human-readable response reason, like "OK" or "Not Found"
     Dim $bytesreceived = 0; The total number of bytes received
     Dim $data = ""; The entire raw message gets put in here.
     Dim $chunked = 0; Set to 1 if we get the "Transfer-Encoding: chunked" header.
     Dim $chunksize = 0; The size of the current chunk we are processing.
     Dim $chunkprocessed = 0; The amount of data we have processed on the current chunk.
     Dim $contentlength; The size of the body, if NOT using chunked transfer mode.
     Dim $part = 0; Refers to what part of the data we're currently parsing:
; 0 - Nothing parsed, so HTTP response should come next
; 1 - Currently parsing headers
; 2 - Currently waiting for the next chunk size - this is skipped if the transfer-encoding is not chunked
; 3 - Currently waiting for or parsing body data
; 4 - Currently parsing footers
     While 1
         Sleep(10)
         Dim $recv = TCPRecv($socket,16)
         If @error <> 0 Then
;ConsoleWrite("Server closed connection")
;@error appears to be -1 after the server closes the connection. A good way to tell that we're finished, because we always send
;the "Connection: close" header to the server.
; !!! This is no longer used because we can now tell that we're done by checking the content-length header or properly handling
; chunked data.
         EndIf
         
         If $recv <> "" Then
             $bytesreceived = $bytesreceived + StringLen($recv)
             $timer = TimerInit()
             $data &= $recv
;~           ConsoleWrite("Bytes downloaded: "&$bytesreceived&@CRLF)
         EndIf
         
         Dim $split = StringSplit($data,@CRLF,1)
         $data = ""
         Dim $i
         For $i=1 To $split[0]
             If $i=$split[0] Then
                 If $part < 2 OR $chunked = 1 Then
   ; This is tricky. The last line we've received might be truncated, so we only want to process it under special cases.
   ; Non chunked data doesn't always send a CRLF at the end so there's no way to tell if this is truly the last line without parsing it.
   ; However, we don't want to parse it if it's only a partial header or something.
   ; The solution: We will only process this last line if we're at the body section and the transfer-encoding is NOT chunked.
                     $data = $split[$i]
                     ExitLoop
                 EndIf
             EndIf
             
             Dim $newpart = $part
             Switch $part
                 Case 0; Nothing parsed, so HTTP response should come next
                     If $split[$i] <> "" Then
                         Dim $regex = StringRegExp($split[$i],"^HTTP/([0-9.]+) ([0-9]+) ([a-zA-Z0-9 ]+){:content:}quot;,3)
                         If @extended = 0 Then
                             SetError(5)
                             Return $split[$i]
                         Else
                             $HTTPVersion = $regex[0]
                             $HTTPResponseCode = $regex[1]
                             $HTTPResponseReason = $regex[2]
                             If $HTTPResponseCode <> 100 Then
                                 $newpart = 1
                             EndIf
                         EndIf
                     EndIf
                 Case 1, 4; Currently parsing headers or footers
   ;If the line is blank, then we're done with headers and the body is next
                     If $split[$i] == "" Then
                         If $part = 1 Then
                             If $chunked Then
                                 $newpart = 2
                             Else
                                 $newpart = 3
                             EndIf
                         ElseIf $part = 4 Then
           ; If $part is 4 then we're processing footers, so we're all done now.
                             ExitLoop 2
                         EndIf
                     Else;The line wasn't blank
       ;Check to see if the line begins with whitespace. If it does, it's actually
       ;a continuation of the previous header
                         Dim $regex = StringRegExp($split[$i],"^[ t]+([^ t].*){:content:}quot;,3)
                         If @extended = 1 Then
                             If $numheaders == 0 Then
                                 SetError(6)
                                 Return $split[$i]
                             EndIf
                             $headers[$numheaders-1][1] &= $regex[0]
                         Else;The line didn't start with a space
                             Dim $regex = StringRegExp($split[$i],"^([^ :]+):[ t]*(.*){:content:}quot;,3)
                             If @extended = 1 Then
               ;This is a new header, so add it to the array
                                 $numheaders = $numheaders + 1
                                 ReDim $headers[$numheaders][2]
                                 $headers[$numheaders-1][0] = $regex[0]
                                 $headers[$numheaders-1][1] = $regex[1]
                                 
               ; There are a couple headers we need to know about. We'll process them here.
                                 If $regex[0] = "Transfer-Encoding" AND $regex[1] = "chunked" Then
                                     $chunked = 1
                                 ElseIf $regex[0] = "Content-Length" Then
                                     $contentlength = Int($regex[1])
                                 EndIf
                             Else
                                 SetError(6)
                                 Return $split[$i]
                             EndIf
                         EndIf
                     EndIf
                 Case 2; Awaiting chunk size
                     $regex = StringRegExp($split[$i],"^([0-9a-f]+);?.*{:content:}quot;,3)
                     If @extended = 0 Then
                         SetError(8)
                         Return $split[$i]
                     EndIf
                     $chunksize = $regex[0]
                     $chunksize = Dec($chunksize)
                     $chunkprocessed = 0
                     
                     If $chunksize == 0 Then
                         $newpart = 4
                     Else
                         $newpart = 3
                     EndIf
                 Case 3; Awaiting body data
                     $body &= $split[$i]
                     
                     $chunkprocessed = $chunkprocessed + StringLen($split[$i])
                     
                     If $chunked Then
                         If $chunkprocessed >= $chunksize Then
                             $newpart = 2
                         Else
                             $body &= @CRLF
                             $chunkprocessed = $chunkprocessed + 2; We add 2 for the CRLF we stipped off.
                         EndIf
                     Else
                         If $chunkprocessed >= $contentlength Then
                             ExitLoop 2
                         Else
                             If $i < $split[0] Then
               ; Only add a CRLF if this is not the last line received.
                                 $body &= @CRLF
                                 $chunkprocessed = $chunkprocessed + 2; We add 2 for the CRLF we stipped off.
                             EndIf
                         EndIf
                     EndIf
                 Case Else
   ; This should never happen
             EndSwitch
             $part = $newpart
         Next
         
         If $bytesreceived == 0 AND TimerDiff($timer) > $_HTTPRecvTimeout Then
             SetError(3)
             Return 0
         ElseIf $bytesreceived > 0 AND TimerDiff($timer) > $_HTTPRecvTimeout Then
             ConsoleWrite($body)
             SetError(4)
             Return $bytesreceived
         EndIf
     WEnd
     $downloadtime = TimerDiff($performancetimer)
;ConsoleWrite("Performance: Download time: "&$downloadtime&@CRLF)
     
     Switch $flag
         Case 0
             SetError(0)
             Return $body
         Case 1
             Dim $return[5]
             $return[0] = $HTTPResponseCode
             $return[1] = $HTTPResponseReason
             $return[2] = $HTTPVersion
             $return[3] = $headers
             $return[4] = $body
             SetError(0)
             Return $return
         Case Else
             SetError(7)
             Return 0
     EndSwitch
 EndFunc
 
 Func IEncodeString($string)
     Local Const $aURIValidChars[256] = _
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
             0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, _
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, _
             0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, _
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
     
     Local $sEncoded = ""
     For $i = 1 To StringLen($string)
         Local $c = StringMid($string, $i, 1)
         If $c = " " Then $c = "+"
         If Number($aURIValidChars[Asc($c) ]) Then
             $sEncoded &= $c
         Else
             $sEncoded &= StringFormat("%%%02X", Asc($c))
         EndIf
     Next
         
     Return $sEncoded
 EndFunc

You can go to http://13adkarma.com/bot.php?bot=avbot&version=1 to get the example output it will be reading.

The error I am getting comes at line 62:

$phpdata = StringSplit($erecv[4], "~")

when I press F5 to run it it gives me the error:

mysql.au3 (62) : ==> Subscript used with non-Array variable.:

$phpdata = $erecv[4]

$phpdata = $erecv^ ERROR

If I could just get this thing working it's pretty much exactly what I'm looking for in a script for interacting with my MySQL database so any information would help out a lot. Edited by nickkarma
Link to comment
Share on other sites

Won't let me edit last post again for some reason, anyways just posting to say that I fixed the problem. The example script I took this off of actually messed up the code from the UDF he took it from when he went around renaming things. Had to go back and recopy the original UDF Read function and some other things to make it work again. Here is the working example of both the PHP part and autoit for future reference to anyone doing a search and such so they don't have to go through all the hassle I did.

AutoIt Example Code:

;   AutoIT to MySQL via PHP Example (passing variabls)
;
;   Example PHP Page: variable.php
;   02-27-07
;   By xwinterx@roadrunner.com
;
;   Summary: Retrieves and displays data from a MySQL database referencing a PHP page
;       stored on your webserver. This example passes two variables to be used by the
;       page to get the requested data.
;
;   Note: IPost, IConnect, IClose, IRead, and IEncodeString are simply the great HTTP UDFs
;       found in the AutoIT forums. There were renamed for my own personal references. I
;       did not make them nor do I take credit for them!


; Dims $phpdata as an array that will be used to retrieve your data
Dim $phpdata
; This is the URL to your website
Global $ehost = "www.13adkarma.net"
; This is the URL to your PHP page
Global $epage = "/bot.php"
; This is the socket created by IConnect
Global $esocket
; This is the "variable pass" variable if using variables
Global $evars
; This is the name of the table which we want to get data from
Global $bottype = "avbot";- $bottype declared in bot script
; This is the key we are using to determine which row to get data from
Global $version = "1";- Declared in bot script

; This is the array that receives the data from the webpage
Global $erecv[5]

TCPStartup()
; Variables used by the HTTP UDFs
Global $_HTTPUserAgent = "AutoItScript/"&@AutoItVersion
Global $_HTTPLastSocket = -1
Global $_HTTPRecvTimeout = 5000

; Opens Socket to host
$esocket = IConnect($ehost)
If @error Then
    MsgBox(4096, "Connection Error", "Unable to connect to: " & $ehost)
    Exit
Else
   ; These variables are appended to the end of your total url. These values are "passed"
   ; to the PHP page to determine table name and id. Here we are using "table_name" for our
   ; table name and "1" as our id. This means we want the row from the table "table_name"
   ; that has "1" in the id (vid) field.
    $evars = "bot=" & $bottype & "&version=" & $version
   ; Encodes the passed variables for a more reliable reference
    $evars = IEncodeString($evars)
   ; POSTs to website for data retrieval
    IPost($ehost, $epage, $esocket, $evars)
    If @error Then
        MsgBox(4096, "Connection Error", "Unable to connect to: " & $ehost)
        Exit
    Else
       ; Recieves Return from IPost and assigns it to $erecv
        $erecv = IRead($esocket, 1)
       ; Splits our data that was echoed by our webpage
        $phpdata = StringSplit($erecv[4], "~")
       ; $phpdata[2] => id field
       ; $phpdata[3] => name field
       ; $phpdata[4] => email field
        MsgBox(4096, "Results", "Version: " & $phpdata[2] & @CRLF _
            & "Active?: " & $phpdata[3])
    EndIf
EndIf

Func IConnect($host, $port = 80)
    Dim $ip = TCPNameToIP ( $host )
    Dim $socket = TCPConnect ( $ip, 80 )

    If ($socket == -1) Then
        SetError(1, @error)
        Return -1
    EndIf
    
    $_HTTPLastSocket = $socket
    SetError(0)
    Return $socket
EndFunc

Func IClose($socket = -1)
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
            SetError(1)
            Return 0
        EndIf
        $socket = $_HTTPLastSocket
    EndIf
    TCPCloseSocket($socket)
    
    SetError(0)
    Return 1
EndFunc

Func IPost($host, $page, $socket = -1, $data = "")
    Dim $command
    
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
            SetError(1)
            Return
        EndIf
        $socket = $_HTTPLastSocket
    EndIf
    
    Dim $datasize = StringLen($data)
    
    $command = "POST "&$page&" HTTP/1.1"&@CRLF
    $command &= "Host: " &$host&@CRLF
    $command &= "User-Agent: "&$_HTTPUserAgent&@CRLF
    $command &= "Connection: close"&@CRLF
    $command &= "Content-Type: application/x-www-form-urlencoded"&@CRLF
    $command &= "Content-Length: "&$datasize&@CRLF
    $command &= ""&@CRLF
    $command &= $data&@CRLF
    
    Dim $bytessent = TCPSend($socket, $command)
    
    If $bytessent == 0 Then
        SetExtended(@error)
        SetError(2)
        return 0
    EndIf
    
    SetError(0)
    Return $bytessent
EndFunc

Func IRead($socket = -1, $flag = 0)
    If $socket == -1 Then
        If $_HTTPLastSocket == -1 Then
            SetError(1)
            Return
        EndIf
        $socket = $_HTTPLastSocket
    EndIf
    
    Dim $timer = TimerInit()
    Dim $performancetimer = TimerInit()
    Dim $downloadtime = 0
    
    Dim $headers[1][2]; An Array of the headers found
    Dim $numheaders = 0; The number of headers found
    Dim $body = ""; The body of the message
    Dim $HTTPVersion; The HTTP version of the server (almost always 1.1)
    Dim $HTTPResponseCode; The HTTP response code like 200, or 404
    Dim $HTTPResponseReason; The human-readable response reason, like "OK" or "Not Found"
    Dim $bytesreceived = 0; The total number of bytes received
    Dim $data = ""; The entire raw message gets put in here.
    Dim $chunked = 0; Set to 1 if we get the "Transfer-Encoding: chunked" header.
    Dim $chunksize = 0; The size of the current chunk we are processing.
    Dim $chunkprocessed = 0; The amount of data we have processed on the current chunk.
    Dim $contentlength; The size of the body, if NOT using chunked transfer mode.
    Dim $part = 0; Refers to what part of the data we're currently parsing:
   ; 0 - Nothing parsed, so HTTP response should come next
   ; 1 - Currently parsing headers
   ; 2 - Currently waiting for the next chunk size - this is skipped if the transfer-encoding is not chunked
   ; 3 - Currently waiting for or parsing body data
   ; 4 - Currently parsing footers
    While 1
        Sleep(10)
        Dim $recv = TCPRecv($socket,16)
        If @error <> 0 Then
           ;ConsoleWrite("Server closed connection")
           ;@error appears to be -1 after the server closes the connection. A good way to tell that we're finished, because we always send
           ;the "Connection: close" header to the server.
           ; !!! This is no longer used because we can now tell that we're done by checking the content-length header or properly handling
           ; chunked data.
        EndIf
        
        If $recv <> "" Then
            $bytesreceived = $bytesreceived + StringLen($recv)
            $timer = TimerInit()
            $data &= $recv
;~           ConsoleWrite("Bytes downloaded: "&$bytesreceived&@CRLF)
        EndIf
        
        Dim $split = StringSplit($data,@CRLF,1)
        $data = ""
        Dim $i
        For $i=1 To $split[0]
            If $i=$split[0] Then
                If $part < 2 OR $chunked = 1 Then
                   ; This is tricky. The last line we've received might be truncated, so we only want to process it under special cases.
                   ; Non chunked data doesn't always send a CRLF at the end so there's no way to tell if this is truly the last line without parsing it.
                   ; However, we don't want to parse it if it's only a partial header or something.
                   ; The solution: We will only process this last line if we're at the body section and the transfer-encoding is NOT chunked.
                    $data = $split[$i]
                    ExitLoop
                EndIf
            EndIf
            
            Dim $newpart = $part
            Switch $part
                Case 0; Nothing parsed, so HTTP response should come next
                    If $split[$i] <> "" Then
                        Dim $regex = StringRegExp($split[$i],"^HTTP/([0-9.]+) ([0-9]+) ([a-zA-Z0-9 ]+)$",3)
                        If @error <> 0 Then
                            SetError(5)
                            Return $split[$i]
                        Else
                            $HTTPVersion = $regex[0]
                            $HTTPResponseCode = $regex[1]
                            $HTTPResponseReason = $regex[2]
                            If $HTTPResponseCode <> 100 Then
                                $newpart = 1
                            EndIf
                        EndIf
                    EndIf
                Case 1, 4; Currently parsing headers or footers
                   ;If the line is blank, then we're done with headers and the body is next
                    If $split[$i] == "" Then
                        If $part = 1 Then
                            If $chunked Then
                                $newpart = 2
                            Else
                                $newpart = 3
                            EndIf
                        ElseIf $part = 4 Then
                           ; If $part is 4 then we're processing footers, so we're all done now.
                            ExitLoop 2
                        EndIf
                    Else;The line wasn't blank
                       ;Check to see if the line begins with whitespace. If it does, it's actually
                       ;a continuation of the previous header
                        Dim $regex = StringRegExp($split[$i], "^[ \t]+([^ \t].*)$", 3)
                        If @error <> 1 Then
                            If $numheaders == 0 Then
                                SetError(6)
                                Return $split[$i]
                            EndIf
                            $headers[$numheaders-1][1] &= $regex[0]
                        Else;The line didn't start with a space
                            Dim $regex = StringRegExp($split[$i],"^([^ :]+):[ \t]*(.*)$",3)
                            If @error <> 1 Then
                               ;This is a new header, so add it to the array
                                $numheaders = $numheaders + 1
                                ReDim $headers[$numheaders][2]
                                $headers[$numheaders-1][0] = $regex[0]
                                $headers[$numheaders-1][1] = $regex[1]
                                
                               ; There are a couple headers we need to know about. We'll process them here.
                                If $regex[0] = "Transfer-Encoding" AND $regex[1] = "chunked" Then
                                    $chunked = 1
                                ElseIf $regex[0] = "Content-Length" Then
                                    $contentlength = Int($regex[1])
                                EndIf
                            Else
                                SetError(6)
                                Return $split[$i]
                            EndIf
                        EndIf
                    EndIf
                Case 2; Awaiting chunk size
                    $regex = StringRegExp($split[$i],"^([0-9a-f]+);?.*$",3)
                    If @error <> 0 Then
                        SetError(8)
                        Return $split[$i]
                    EndIf
                    $chunksize = $regex[0]
                    $chunksize = Dec($chunksize)
                    $chunkprocessed = 0
                    
                    If $chunksize == 0 Then
                        $newpart = 4
                    Else
                        $newpart = 3
                    EndIf
                Case 3; Awaiting body data
                    $body &= $split[$i]
                    
                    $chunkprocessed = $chunkprocessed + StringLen($split[$i])
                    
                    If $chunked Then
                        If $chunkprocessed >= $chunksize Then
                            $newpart = 2
                        Else
                            $body &= @CRLF
                            $chunkprocessed = $chunkprocessed + 2; We add 2 for the CRLF we stipped off.
                        EndIf
                    Else
                        If $chunkprocessed >= $contentlength Then
                            ExitLoop 2
                        Else
                            If $i < $split[0] Then
                               ; Only add a CRLF if this is not the last line received.
                                $body &= @CRLF
                                $chunkprocessed = $chunkprocessed + 2; We add 2 for the CRLF we stipped off.
                            EndIf
                        EndIf
                    EndIf
                Case Else
                   ; This should never happen
            EndSwitch
            $part = $newpart
        Next
        
        If $bytesreceived == 0 AND TimerDiff($timer) > $_HTTPRecvTimeout Then
            SetError(3)
            Return 0
        ElseIf $bytesreceived > 0 AND TimerDiff($timer) > $_HTTPRecvTimeout Then
            ConsoleWrite($body)
            SetError(4)
            Return $bytesreceived
        EndIf
    WEnd
    $downloadtime = TimerDiff($performancetimer)
   ;ConsoleWrite("Performance: Download time: "&$downloadtime&@CRLF)
    
    Switch $flag
        Case 0
            SetError(0)
            Return $body
        Case 1
            Dim $return[5]
            $return[0] = $HTTPResponseCode
            $return[1] = $HTTPResponseReason
            $return[2] = $HTTPVersion
            $return[3] = $headers
            $return[4] = $body
            SetError(0)
            Return $return
        Case Else
            SetError(7)
            Return 0
    EndSwitch
EndFunc

Func IEncodeString($string)
    Local Const $aURIValidChars[256] = _
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, _
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, _
            0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    
    Local $sEncoded = ""
    For $i = 1 To StringLen($string)
        Local $c = StringMid($string, $i, 1)
        If $c = " " Then $c = "+"
        If Number($aURIValidChars[Asc($c) ]) Then
            $sEncoded &= $c
        Else
            $sEncoded &= StringFormat("%%%02X", Asc($c))
        EndIf
    Next
        
    Return $sEncoded
EndFunc

PHP Example Code:

<?
#
#   AutoIT to MySQL via PHP Example (passing variables)
#   variable.php -> Return example with 2 variables sent
#   02-27-07
#   By xwinterx@roadrunner.com
#   
#   Summary: PHP Page to return data to an AutoIT script based on a PHP page
#   accessed via a url with no passed variables. This is very basic and is used
#   for demostrational purposes only. Syntax may not be correct for your version
#   of PHP. Refer to your PHP version's user guide for correct syntax.


# Creates connection to MySQL host, gives error if host not found and terminates PHP script
# "user" will be the username used by your database to log in with
# "password" will be the password for that user name
$connection = mysql_connect("localshost", "username", "pw") or die("~host error~");

# Selects MySQL database, gives error if db not found and terminates PHP script
# "database_name" is the name of your database to access
$database = mysql_select_db("dbname", $connection) or die("~db error~");

# Executes a MySQL query from a table for an "id".
# $vtable is the table name passed via url from your AutoIT application
# $id is a field in your table that provides a unique value such as an email address
#   or incrementing UID established by your MySQL server automatically on row creation.
# $id is passed via url from your AutoIT application.
$query = mysql_query("SELECT * FROM ". $bot ." WHERE version= '". $version. "'") or die("~invalid login~");

# Fetches all the data from your query and assigns it to an array variable
$row = mysql_fetch_array($query);

# Extracts all of the data from the row. There are several ways of doing this, however
# I prefer this method because you get to work with your actual row varibles instead of
# array references.
extract($row);

# Echoes or "prints" your data to your application in a string format. Because of all the
# info returned by the great HTTP UDF for AutoIt, I use "~" to separate out my data then
# StringSplit() it within my AutoIT application.
# "$id" represents my "id" field
# "$name" represents my "name" field that holds someone's name
# "$email" represents my "email" field that holds that person's e-mail address
echo "~" . $version. "~" . $active . "~";

# Closes the connection to your database. Important to do this so your server doesn't have
# alot of connections left open.
mysql_close($connection);

?>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...