#include <GUIConstants.au3>
#include <EditConstants.au3>
Global $url, $gui, $input1, $input2, $input3, $input4, $btn1
Global $sLogin, $sPassword, $bAuthStatus = False
Local $iError = 0
Opt("GUICloseOnESC", 0)
$gui = GUICreate("ВК Сокращалка ссылок (version 2)", 400, 300)
GUICtrlCreateLabel("Логин", 10, 10, 90, 30)
$input1 = GUICtrlCreateInput("", 110, 10, 190, 30)
GUICtrlSetLimit($input1, 70)
GUICtrlCreateLabel("Пароль", 10, 50, 90, 30)
$input2 = GUICtrlCreateInput("", 110, 50, 190, 30, $ES_PASSWORD)
GUICtrlSetLimit($input2, 25)
GUICtrlCreateLabel("URL", 10, 160, 90, 30)
$input3 = GUICtrlCreateInput("", 110, 160, 190, 30)
$btn1 = GUICtrlCreateButton("Получить короткую ссылку", 50, 200, 170, 30)
GUICtrlSetState($btn1, $GUI_DEFBUTTON)
GUICtrlCreateLabel("Короткая ссылка", 10, 250, 140, 30)
$input4 = GUICtrlCreateInput("", 160, 250, 160, 30, $ES_READONLY)
GUISetState(@SW_SHOW, $gui)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $btn1
If Not $bAuthStatus Or $sLogin = "" Or $sPassword = "" Then
$sLogin = GUICtrlRead($input1)
$sPassword = GUICtrlRead($input2)
EndIf
If StringLen(GUICtrlRead($input1)) > 5 And StringLen(GUICtrlRead($input2)) > 5 And StringLen(GUICtrlRead($input3)) > 5 Then
If ($bAuthStatus) And ($sLogin <> GUICtrlRead($input1) Or $sPassword <> GUICtrlRead($input2)) Then
If MsgBox(4, "Сменить аккаунт?", "Введённые логин и пароль отличаются от введённых ранее." & @CRLF & "Вы хотите отменить текущую авторизацию?", 0, $gui) = 6 Then
$sLogin = GUICtrlRead($input1)
$sPassword = GUICtrlRead($input2)
$bAuthStatus = False
__cookieDestroy()
Else
GUICtrlSetData($input1, $sLogin)
GUICtrlSetData($input2, $sPassword)
$bAuthStatus = True
EndIf
EndIf
GUISetState(@SW_DISABLE, $gui)
$url = _VKShortURL(GUICtrlRead($input1), GUICtrlRead($input2), GUICtrlRead($input3))
$iError = @error
If Not $iError Then
$bAuthStatus = True
GUISetState(@SW_ENABLE, $gui)
GUICtrlSetData($input4, $url)
GUICtrlSetState($input4, $GUI_FOCUS)
Else
GUISetState(@SW_ENABLE, $gui)
MsgBox(16, "error" & $iError, "error code: " & $iError, 10, $gui)
GUICtrlSetState($input1, $GUI_FOCUS)
EndIf
Else
MsgBox(16, "error", "error", 2, $gui)
EndIf
EndSwitch
WEnd
GUIDelete($gui)
Func _VKShortURL($sLogin, $sPassword, $sURL)
Local $oReq, $sSendStr, $sSendStr2, $Cookie, $sAction, $sURLM, $sURLAuthCheck, $sURLCC, $sURLActivation
Local $sResponse, $aStringSplit, $sLink, $aTmp
Local $sCaptcha_sid = "0", $sCaptcha_code = "0", $sCaptcha_path = "", $sCode = "0"
$oReq = ObjCreate('WinHttp.WinHttpRequest.5.1')
If Not IsObj($oReq) Then Return SetError(1, 0, 0)
$sURLCC = "https://vk.com/cc"
$sURLM = "https://m.vk.com/"
$sURLAuthCheck = "https://m.vk.com/login?act=authcheck"
$sURLActivation = "https://vk.com/activation.php"
$sUserAgent = 'Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0'
__SetCookieParse($Cookie)
;1.
$oReq = __MakeRequest("GET", $sURLM, "", "", $Cookie, $sUserAgent, 0, "")
If @error Then Return SetError(@error, 0, 0)
__SetCookieParse($Cookie, $oReq.GetAllResponseHeaders())
$sResponse = $oReq.ResponseText
If StringInStr($sResponse, '://login.vk.com/?act=login') Then
$sAction = StringRegExpReplace($sResponse, "(?si).*?\<form.*?action\=['""](.*?)['""].*?\>.*", "$1")
If Not _isFullURL($sAction) Then $sAction = _build_URL("https", "m.vk.com", $sAction)
;2.
$sSendStr = 'email=' & __encodeURIComponent($sLogin) & '&pass=' & __encodeURIComponent($sPassword) & ''
$oReq = __MakeRequest("POST", $sAction, $sURLM, $sSendStr, $Cookie, $sUserAgent, 0, "")
If @error Then Return SetError(@error, 0, 0)
__SetCookieParse($Cookie, $oReq.GetAllResponseHeaders())
EndIf
$sResponse = $oReq.ResponseText
If StringInStr($sResponse, "=authcheck") > 0 Then
$sAction = StringRegExpReplace($sResponse, "(?si).*?\<form.*?action\=['""](.*?authcheck.*?)['""].*?\>.*", "$1")
If Not _isFullURL($sAction) Then $sAction = _build_URL("https", "m.vk.com", $sAction)
$sCode = _getCode()
If @error Then Return SetError(6, 0, 0)
;3.
For $c = 1 To 3
$sSendStr = "_ajax=1&code=" & __encodeURIComponent($sCode) & "&remember=1"
$oReq = __MakeRequest("POST", $sAction, $sURLAuthCheck, $sSendStr, $Cookie, $sUserAgent, 1, "")
__SetCookieParse($Cookie, $oReq.GetAllResponseHeaders())
$sResponse = $oReq.responseText
If StringRegExp($sResponse, '(?si).*?(div.*?class.*?service_msg_warning.*?[>]).*') Then
MsgBox(16, "Ошибка", "Неверный код." & @CRLF & "Проверьте правильность ввода кода.", 10)
Else
ExitLoop
EndIf
Next
$sAction = StringRegExpReplace($sResponse, '\[.*?["](.*?login.*?)["].*?\]', '$1')
$sAction = StringReplace($sAction, '\', '')
If Not _isFullURL($sAction) Then $sAction = _build_URL("https", "m.vk.com", $sAction)
If Not @error And $sAction <> "" Then $oReq = __MakeRequest("GET", $sAction, $sURLAuthCheck, "", $Cookie, $sUserAgent, 1, "")
__SetCookieParse($Cookie, $oReq.GetAllResponseHeaders())
EndIf
;5.
$oReq = __MakeRequest("GET", $sURLCC, "", "", $Cookie, $sUserAgent, 0, "")
If @error Then Return SetError(@error, 0, 0)
__SetCookieParse($Cookie, $oReq.GetAllResponseHeaders())
;6.
For $r = 1 To 2
$sSendStr = 'act=shorten&al=1&link=' & __encodeURIComponent($sURL)
If $r = 2 And $sCaptcha_sid <> "0" Then
$sCaptcha_path = @TempDir & "\captcha_vk_" & $sCaptcha_sid & ".jpg"
ConsoleWrite("captcha file path: " & $sCaptcha_path & @CRLF)
;7.
$oReq = __MakeRequest("GET", "https://vk.com/captcha.php?sid=" & $sCaptcha_sid & "&s=1", $sURLCC, "", $Cookie, $sUserAgent, 0, "")
If Not @error Then
$hCaptcha_file = FileOpen($sCaptcha_path, 16 + 2)
FileWrite($hCaptcha_file, $oReq.ResponseBody)
FileClose($hCaptcha_file)
EndIf
$sCaptcha_code = _getCaptchaCode($sCaptcha_path)
If FileExists($sCaptcha_path) Then FileDelete($sCaptcha_path)
ConsoleWrite("captcha key: " & $sCaptcha_code & @CRLF)
$sSendStr &= "&captcha_key=" & $sCaptcha_code & "&captcha_sid=" & $sCaptcha_sid
EndIf
$oReq = __MakeRequest("POST", $sURLCC, $sURLCC, $sSendStr, $Cookie, $sUserAgent, 1, "")
If @error Then Return SetError(@error, 0, 0)
__SetCookieParse($Cookie, $oReq.GetAllResponseHeaders())
$sResponse = $oReq.responseText
ConsoleWrite("POST_REQUEST: " & $sSendStr & @CRLF)
ConsoleWrite("RESPONSE: " & $sResponse & @CRLF)
$aStringSplit = StringSplit($sResponse, "<!>", 1)
If @error Then Return SetError(@error, @extended, 0)
If $r = 1 And $aStringSplit[$aStringSplit[0]] = "0" And $aStringSplit[$aStringSplit[0] - 1] <> "0" Then
$sCaptcha_sid = $aStringSplit[$aStringSplit[0] - 1]
ConsoleWrite("captcha sid: " & $sCaptcha_sid & @CRLF)
EndIf
If $r = 1 And $aStringSplit[$aStringSplit[0] - 1] = "11" And StringLeft($aStringSplit[$aStringSplit[0]], 4) <> "http" And StringLen($aStringSplit[$aStringSplit[0]]) > 5 Then
ConsoleWrite("11: mobile validation needed" & @CRLF)
$sCaptcha_hash = $aStringSplit[$aStringSplit[0]]
ConsoleWrite("captcha hash: " & $sCaptcha_hash & @CRLF)
;8.
$sSendStr2 = "act=validate_box&al=1&captcha=1&hash=" & $sCaptcha_hash & "&skip_push="
$oReq = __MakeRequest("POST", $sURLActivation, $sURLCC, $sSendStr2, $Cookie, $sUserAgent, 1, "")
If @error Then Return SetError(@error, 0, 0)
__SetCookieParse($Cookie, $oReq.GetAllResponseHeaders())
$sResponse = $oReq.responseText
$aTmp = StringRegExp($sResponse, "(?s)extend\(cur.*?validationCsid\:.*?['""](.*?)['""]", 3)
If IsArray($aTmp) Then $sCaptcha_sid = $aTmp[0]
ConsoleWrite("captcha sid: " & $sCaptcha_sid & @CRLF)
EndIf
$sLink = $aStringSplit[$aStringSplit[0]]
If StringLen($sLink) < 5 Or StringLeft($sLink, 4) <> "http" Then
$sLink = "0"
Else
ExitLoop
EndIf
Next
If $sLink = "0" Then Return SetError(9, 0, 0)
Return $sLink
EndFunc ;==>_VKShortURL
Func __MakeRequest($_method = "GET", $_url = "", $_sReferer = "", $_str = "", $_sCookie = "", $_sUA = "", $_isAjax = 0, $_sProxy = "", $nLocation = True, $iMaxLocations = 5, $_oReq = 0)
Local $_sLocation, $_oError, $iCountRequests = 1
$_oError = ObjEvent('AutoIt.Error', '__ErrorFunc')
If Not IsObj($_oReq) Then $_oReq = ObjCreate('WinHttp.WinHttpRequest.5.1')
If Not IsObj($_oReq) Then Return SetError(1, 0, 0)
While 1
$_oReq.SetTimeouts(5000, 5000, 5000, 5000)
$_oReq.Open($_method, $_url, False)
$_oReq.Option(6) = 0; WinHttpRequestOption_EnableRedirects
If $_sProxy <> "" Then $_oReq.SetProxy(2, $_sProxy)
If $_method = "POST" Then $_oReq.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
If $_sUA <> "" Then $_oReq.SetRequestHeader('User-Agent', $_sUA)
If $_sCookie <> "" Then $_oReq.SetRequestHeader('Cookie', $_sCookie)
If $_sReferer <> "" Then $_oReq.SetRequestHeader('Referer', $_sReferer)
If $_isAjax Then $_oReq.SetRequestHeader('X-Requested-With', 'XMLHttpRequest')
$_oReq.SetRequestHeader('Cache-Controle', 'no-cach')
$_oReq.SetRequestHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
$_oReq.SetRequestHeader('Accept-Language', 'ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3')
;$_oReq.SetRequestHeader('Accept-Encoding', 'gzip, deflate');
$_oReq.SetRequestHeader('Connection', 'keep-alive')
$_oReq.Send($_str)
__SetCookieParse($_sCookie, $_oReq.GetAllResponseHeaders())
If Not $nLocation Then ExitLoop
If $iCountRequests >= $iMaxLocations Then ExitLoop
$_method = "GET"
$_sLocation = $_oReq.GetResponseHeader("Location")
If $_sLocation == "" Then ExitLoop
If Not _isFullURL($_sLocation) Then $_sLocation = _build_URL("https", "m.vk.com", $_sLocation)
$_url = $_sLocation
If $_method = "GET" Then $_str = ""
$iCountRequests += 1
WEnd
$_oError = 0
Return $_oReq
EndFunc ;==>__MakeRequest
Func ErrorFunc()
ConsoleWrite("COM ERROR" & @CRLF)
EndFunc ;==>ErrorFunc
Func __encodeURIComponent($sTxt)
$sTxt = StringReplace($sTxt, @CRLF, '\r\n')
$sTxt = StringReplace($sTxt, @CR, '\r')
$sTxt = StringReplace($sTxt, @LF, '\n')
$sTxt = StringReplace($sTxt, "'", "\'")
$sTxt = StringReplace($sTxt, '"', '\"')
$oSC = ObjCreate("ScriptControl")
If Not IsObj($oSC) Then Return SetError(1)
$oSC.Language = "JavaScript"
Return $oSC.Eval('encodeURIComponent("' & $sTxt & '");')
EndFunc ;==>__encodeURIComponent
Func __decodeURIComponent($sTxt)
$sTxt = StringReplace($sTxt, @CRLF, '\r\n')
$sTxt = StringReplace($sTxt, @CR, '\r')
$sTxt = StringReplace($sTxt, @LF, '\n')
$sTxt = StringReplace($sTxt, "'", "\'")
$sTxt = StringReplace($sTxt, '"', '\"')
$oSC = ObjCreate("ScriptControl")
If Not IsObj($oSC) Then Return SetError(1)
$oSC.Language = "JavaScript"
Return $oSC.Eval('decodeURIComponent("' & $sTxt & '");')
EndFunc ;==>__decodeURIComponent
Func __SetCookieParse(ByRef $__sCookies, $__sHeaders = "")
If Not IsDeclared("__oDict") Then Global $__oDict
If Not IsObj($__oDict) Then $__oDict = ObjCreate('Scripting.Dictionary')
If Not IsObj($__oDict) Then Return SetError(1, 0, 0)
Local $__aHeaders, $__sStr, $__sStart = 'Set-Cookie:', $__aTmp, $__sTmp, $__sTime
Local $__sName, $__sValue
If StringLen($__sHeaders) > 10 Then
$__aHeaders = StringSplit($__sHeaders, @CRLF, 1)
If @error Then $__aHeaders = StringSplit($__sHeaders, @CR, 1)
If @error Then $__aHeaders = StringSplit($__sHeaders, @LF, 1)
For $__i = 1 To $__aHeaders[0]
If StringLeft($__aHeaders[$__i], StringLen($__sStart)) <> $__sStart Then ContinueLoop
$__sTmp = StringMid($__aHeaders[$__i], StringLen($__sStart) + 1)
$__aTmp = StringRegExp($__sTmp, "([^=]+)[=]([^;]+)", 3)
If Not IsArray($__aTmp) Then ContinueLoop
$__sName = StringStripWS($__aTmp[0], 7)
$__sValue = StringStripWS($__aTmp[1], 7)
$__aTmp = StringRegExp($__sTmp, ".*?expires[=]([^;]+)", 3)
If IsArray($__aTmp) Then $__sTime = $__aTmp[0]
If Not IsArray($__aTmp) Then $__sTime = ""
If $__sTime <> "" And __GMT_DateConvert($__sTime) < __GMT_DateConvert() Then
If $__oDict.Exists($__sName) Then $__oDict.Remove($__sName); COOKIE EXPIRE
ContinueLoop
EndIf
If Not $__oDict.Exists($__sName) Then $__oDict.Add($__sName, ObjCreate('Scripting.Dictionary'))
$__oDict.Item($__sName).RemoveAll()
$__oDict.Item($__sName).Add(0, $__sName)
$__oDict.Item($__sName).Add(1, $__sValue)
$__oDict.Item($__sName).Add(2, $__sTime)
Next
EndIf
$__sStr = __cookieObjToStr()
$__sCookies = $__sStr
Return 1
EndFunc ;==>__SetCookieParse
Func __cookieObjToStr()
Local $__str = ""
If Not IsDeclared("__oDict") Then Global $__oDict
If Not IsObj($__oDict) Then $__oDict = ObjCreate('Scripting.Dictionary')
If Not IsObj($__oDict) Then Return SetError(1, 0, 0)
If $__oDict.Count <= 0 Then Return ""
$__str = ""
For $__nKey In $__oDict.Keys
If Not $__oDict.Exists($__nKey) Then ContinueLoop
If $__oDict.Item($__nKey).Item(2) <> "" And __GMT_DateConvert($__oDict.Item($__nKey).Item(2)) < __GMT_DateConvert() Then
;$__oDict.Remove($__nKey); COOKIE EXPIRE
ContinueLoop
EndIf
$__str &= (StringFormat("%s=%s; ", $__oDict.Item($__nKey).Item(0), $__oDict.Item($__nKey).Item(1)))
Next
$__str = StringTrimRight($__str, 2)
Return $__str
EndFunc ;==>__cookieObjToStr
Func __cookieDestroy()
If Not IsDeclared("__oDict") Then Global $__oDict
If IsObj($__oDict) Then $__oDict.RemoveAll()
$__oDict = 0
Return 1
EndFunc ;==>__cookieDestroy
Func __GMT_DateConvert($__sStr = "")
Local $__sCode, $__sValidDateStr, $__oSC, $__iResult
If StringRegExp($__sStr, '[\r\n\t"''\\\[\]\{\}]') Then Return SetError(1, 0, 0)
If $__sStr Then
$__sValidDateStr = StringRegExpReplace($__sStr, "(?i)^([a-z]{3}), (\d{2})-([a-z]{3})-(\d{4}) (\d{2}):(\d{2}):(\d{2}) gmt$", "$1 $3 $2 $4 $5:$6:$7")
$__sCode = 'new Date("' & $__sValidDateStr & '").getTime()/1000;'
Else
$__sCode = 'new Date(new Date()).getTime()/1000;'
EndIf
$__oSC = ObjCreate("ScriptControl")
If Not IsObj($__oSC) Then Return SetError(1)
$__oSC.Language = "JavaScript"
$__iResult = $__oSC.Eval($__sCode)
If Not StringIsDigit($__iResult) Then Return SetError(2, 0, 0)
Return $__iResult
EndFunc ;==>__GMT_DateConvert
Func _getCode()
Local $_c_gui, $_c_input1, $_c_btn1, $_sCode
Local $opt_GUICloseOnESC, $opt_GUIOnEventMode
$opt_GUICloseOnESC = Opt("GUICloseOnESC", 0)
$opt_GUIOnEventMode = Opt("GUIOnEventMode", 0)
$_c_gui = GUICreate("code", 300, 200)
GUICtrlCreateLabel("Введите код," & @CRLF & "отправленный вам в СМС, либо введите резервный код", 10, 10, 190, 90)
$_c_input1 = GUICtrlCreateInput("", 40, 110, 150, 30)
GUICtrlSetLimit($_c_input1, 20)
$_c_btn1 = GUICtrlCreateButton("Отправить код", 60, 150, 170, 30)
GUICtrlSetState($_c_btn1, $GUI_DEFBUTTON)
GUISetState(@SW_SHOW, $_c_gui)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
$_sCode = 0
ExitLoop
Case $_c_btn1
$_sCode = StringStripWS(GUICtrlRead($_c_input1), 8)
If StringLen($_sCode) < 3 Then
MsgBox(16, "error", "error", 1, $_c_gui)
GUICtrlSetState($_c_input1, $GUI_FOCUS)
ContinueLoop
EndIf
ExitLoop
EndSwitch
WEnd
GUIDelete($_c_gui)
Opt("GUICloseOnESC", $opt_GUICloseOnESC)
Opt("GUIOnEventMode", $opt_GUIOnEventMode)
If $_sCode = 0 Then Return SetError(1, 0, 0)
Return $_sCode
EndFunc ;==>_getCode
Func _build_URL($_sScheme, $_sHost = "", $_sURI = "")
Local $_aRegexp
$_sScheme = StringStripWS($_sScheme, 8)
$_sHost = StringStripWS($_sHost, 8)
$_sURI = StringStripWS($_sURI, 8)
If StringLeft($_sURI, 1) = '/' Then $_sURI = StringTrimLeft($_sURI, 1)
$_sHost = StringRegExpReplace($_sHost, '[:/\\]', '')
$_aRegexp = StringRegExp($_sScheme, '(?i)([a-z0-9]+)[:][/]{2}([^/]+)[/](.*)', 3)
If Not @error And IsArray($_aRegexp) Then
If $_sHost <> "" Then $_aRegexp[1] = $_sHost
If $_sURI <> "" Then $_aRegexp[2] = $_sURI
Return StringFormat('%s://%s/%s', $_aRegexp[0], $_aRegexp[1], $_aRegexp[2])
EndIf
If StringLen($_sScheme) < 3 Then $_sScheme = "http:"
$_sScheme = StringRegExpReplace($_sScheme, '[:/\\]', '')
Return StringFormat('%s://%s/%s', $_sScheme, $_sHost, $_sURI)
EndFunc ;==>_build_URL
Func _isFullURL($_sURL)
If StringLeft($_sURL, 5) = "http:" Or StringLeft($_sURL, 6) = "https:" Then Return True
Return False
EndFunc ;==>_isFullURL
Func _getCaptchaCode($_cc_image_file_path)
If Not FileExists($_cc_image_file_path) Then Return SetError(1, 0, 0)
Local $_cc_gui, $_cc_input1, $_cc_btn1, $_sCode
Local $opt_GUICloseOnESC, $opt_GUIOnEventMode
$opt_GUICloseOnESC = Opt("GUICloseOnESC", 1)
$opt_GUIOnEventMode = Opt("GUIOnEventMode", 0)
$_cc_gui = GUICreate("captcha code", 200, 200)
GUICtrlCreateLabel("Введите код с картинки" & @CRLF & "Введите код сюда", 10, 10, 140, 30)
$_cc_input1 = GUICtrlCreateInput("", 40, 42, 90, 30)
GUICtrlSetLimit($_cc_input1, 10)
$_cc_pic1 = GUICtrlCreatePic($_cc_image_file_path, 20, 80, 130, 50)
$_cc_btn1 = GUICtrlCreateButton("Отправить", 60, 150, 120, 30)
GUICtrlSetState($_cc_btn1, $GUI_DEFBUTTON)
GUISetState(@SW_SHOW, $_cc_gui)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
$_sCode = "0"
ExitLoop
Case $_cc_btn1
$_sCode = StringStripWS(GUICtrlRead($_cc_input1), 8)
If StringLen($_sCode) < 2 Then
GUICtrlSetState($_cc_input1, $GUI_FOCUS)
ContinueLoop
EndIf
ExitLoop
EndSwitch
WEnd
GUIDelete($_cc_gui)
Opt("GUICloseOnESC", $opt_GUICloseOnESC)
Opt("GUIOnEventMode", $opt_GUIOnEventMode)
If $_sCode = "0" Then Return SetError(1, 0, 0)
Return $_sCode
EndFunc ;==>_getCaptchaCode