Download aus htaccess geschützem Verzeichnis

  • Hallo,

    ich muss mal wieder nerven, mit meinm ssl-htaccess-Gedöns :rolleyes: ...

    In folgendem Thread

    [ gelöst ] Datei via SSL an ein htaccess-geschütztes PHP-Skript senden

    habe ich es - dank der Hilfe einiger kompetenter und hilfsbereiter Leute - ja schon geschafft eine Datei ssl-verschlüsselt an eine php-Datei in einem htaccess-geschützten Verzeichnis zu schicken.

    WOW, das war echt heftig. "Andersherum" - so dachte ich - wäre es einfacher. Aber jetzt hänge ich wieder fest :( ...

    Hat vielleicht jemand einen Ansatz, wie man (z.B.) eine Zip-Datei ssl-verschlüsselt aus einem htaccess-geschützten Verzeichnis heraus runterladen kann? Mit InetGet() geht es zwar ssl-verschlüsselt, aber halt ohne SetCredentials. FTP ginge zwar am htaccess-Schutz vorbei, aber Klartextübertragung kommt natürlich nicht in Frage (Whireshark etc. macht's möglich :D ).

    Bitte, bitte ihr Profis :rolleyes: ...

    Ich sag schon mal Danke für's Lesen.
    Gruß Trainer

    Einmal editiert, zuletzt von ip_trainer (3. Mai 2011 um 12:16)

  • Also: mit meiner "selbstgebastelten" Upload-Funktion klappt es immerhin schon mal, eine Text-Datei herunterzuladen, indem man anstelle eines empfangendes PHP-Skripts ($script) die zu ladende Datei angibt. Mit FileWrite kann ich das ganze dann in eine Text-Datei schreiben. Das klappt. Bei einer Zip-Datei treten allerdings Probleme auf. Als Antwort vom Server bekomme ich "seltsame Zeichen" (z.B. PK...), die ich versucht habe mit StringToBinary umzuwandeln und zu schreiben - aber das klappt nicht. Außerdem kommen zu wenig Daten an (Datei ist viel zu klein).

    Vielleicht hilft es ja...

    Gruß Trainer

    Spoiler anzeigen
    [autoit]

    #include-once
    #include "WinHTTP.au3"

    [/autoit] [autoit][/autoit] [autoit]

    Func http_talk ($host, $script, $post_fields="", $upload_file="", $use_ssl=False, $htaccess_user="", $htaccess_pass="", $proxy="auto")

    [/autoit] [autoit][/autoit] [autoit]

    ;===========================================================================
    ;Beispiel
    ;===========================================================================

    [/autoit] [autoit][/autoit] [autoit]

    ;$host = "irgendeine-domain.de"
    ;$script = "/verzeichnis/script.php"

    [/autoit] [autoit][/autoit] [autoit]

    ;--------------- Ab hier ist alles optional (es kann auch einfach ein Skript ohne Informationsaustausch angestoßen werden) ---------------------

    [/autoit] [autoit][/autoit] [autoit]

    ;POST-Daten:
    ;Dim $post_fields[3][2] (die erste Dimension (hier: 3) muss an die Anzahl der verwendeten Felder angepasst werden)

    [/autoit] [autoit][/autoit] [autoit]

    ;$post_fields[0][0] = "FeldName_1"
    ;$post_fields[0][1] = "FeldWert_1"

    [/autoit] [autoit][/autoit] [autoit]

    ;$post_fields[1][0] = "FeldName_2"
    ;$post_fields[1][1] = "FeldWert_2"

    [/autoit] [autoit][/autoit] [autoit]

    ;$post_fields[2][0] = "FeldName_3"
    ;$post_fields[2][1] = "FeldWert_3"

    [/autoit] [autoit][/autoit] [autoit]

    ;Attachment:
    ;$upload_file = "datei.zip"

    [/autoit] [autoit][/autoit] [autoit]

    ;$use_ssl = True / False (*Default)

    [/autoit] [autoit][/autoit] [autoit]

    ;$htaccess_user = "benutzer_name"
    ;$htaccess_pass = "benutzer_passwort"

    [/autoit] [autoit][/autoit] [autoit]

    ;$proxy = "auto" (holt sich - falls vorhanden - die IE-Einstellungen) / explizite Angabe (z.B. 192.168.178.1:8080)

    [/autoit] [autoit][/autoit] [autoit]

    ;----------------------------------------------------------------

    [/autoit] [autoit][/autoit] [autoit]

    ;AUFRUF: $ergebnis = http_talk ($host, $script [, $post_fields] [, $upload_file] [, $use_ssl] [, $htaccess_user] [, $htaccess_pass] [, $proxy])
    ;ERGEBNIS: Rückgabewert der Funktion (von der Bildschirmausgabe des aufgerufenen Skripts abhängig)

    [/autoit] [autoit][/autoit] [autoit]

    ;BEMERKUNG: Upload-Dateien landen in $_FILES['file_upload']

    [/autoit] [autoit][/autoit] [autoit]

    ;===========================================================================

    [/autoit] [autoit][/autoit] [autoit]

    $boundary = "--------WinHTTPMultiPaRtEXAMPLE"
    $header = "Content-Type: multipart/form-data; boundary=" & $boundary & @CRLF
    $data = "--" & $boundary & @CRLF

    [/autoit] [autoit][/autoit] [autoit]

    ;POST-Datenfelder
    If $post_fields <> "" Then

    [/autoit] [autoit][/autoit] [autoit]

    ;Array durchlaufen
    For $i = 0 To UBound($post_fields,1) -1

    [/autoit] [autoit][/autoit] [autoit]

    If ($post_fields[$i][0] <> "" AND $post_fields[$i][1] <> "") Then

    [/autoit] [autoit][/autoit] [autoit]

    $data &= 'Content-Disposition: form-data; name="' & $post_fields[$i][0] & '"' & @CRLF & @CRLF & $post_fields[$i][1] & @CRLF & @CRLF
    $data &= "--" & $boundary & @CRLF

    [/autoit] [autoit][/autoit] [autoit]

    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Next

    [/autoit] [autoit][/autoit] [autoit]

    EndIf

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;Attachment
    If $upload_file <> "" Then

    [/autoit] [autoit][/autoit] [autoit]

    $data &= __WinHttpFileContent("", "file_upload", $upload_file)

    [/autoit] [autoit][/autoit] [autoit]

    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    $data &= "--" & $boundary & "--" & @CRLF

    [/autoit] [autoit][/autoit] [autoit]

    ;---------------------------------------------------------------------

    [/autoit] [autoit][/autoit] [autoit]

    ;Proxy-Konfiguration
    If($proxy = "auto") Then

    [/autoit] [autoit][/autoit] [autoit]

    ;Internet-Explorer-Einstellungen laden
    $proxy_config = _WinHttpGetIEProxyConfigForCurrentUser()

    [/autoit] [autoit][/autoit] [autoit]

    ;Eine Angabe für alle Proxies vorhanden --> einfach
    If $proxy_config[2] <> "" And Not StringInStr($proxy_config[2], "=") Then
    $proxy = $proxy_config[2]
    $open = _WinHttpOpen(Default, $WINHTTP_ACCESS_TYPE_NAMED_PROXY, $proxy)

    [/autoit] [autoit][/autoit] [autoit]

    ;Ausdifferenzierte Proxy-Angaben vorhanden (passenden Proxy herausfiltern)
    ElseIf StringInStr($proxy_config[2], "=") Then

    [/autoit] [autoit][/autoit] [autoit]

    $all_proxies =StringSplit($proxy_config[2], ";")

    [/autoit] [autoit][/autoit] [autoit]

    ;SSL wird verwendet --> nach https-Proxy suchen
    If $use_ssl = True Then
    ;Anagbe für https-Proxy existiert --> diese nehemen
    If StringInStr($proxy_config[2], "https=") Then
    For $i = 1 To UBound($all_proxies) -1
    If StringInStr($all_proxies[$i], "https=") Then
    $proxy= StringTrimLeft($all_proxies[$i], 6)
    $open = _WinHttpOpen(Default, $WINHTTP_ACCESS_TYPE_NAMED_PROXY, $proxy)
    EndIf
    Next

    [/autoit] [autoit][/autoit] [autoit]

    ;Keine gesonderte Angabe--> keinen Proxy verwenden
    Else
    $proxy = ""
    $open = _WinHttpOpen()
    EndIf

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;SSL wird NICHT verwendet --> nach http-Proxy suchen
    Else
    ;Angabe für http-Proxy existiert --> diese nehemen
    If StringInStr($proxy_config[2], "http=") Then
    For $i = 1 To UBound($all_proxies) -1
    If StringInStr($all_proxies[$i], "http=") Then
    $proxy= StringTrimLeft($all_proxies[$i], 5)
    $open = _WinHttpOpen(Default, $WINHTTP_ACCESS_TYPE_NAMED_PROXY, $proxy)
    EndIf
    Next

    [/autoit] [autoit][/autoit] [autoit]

    ;Keine gesonderte Angabe--> keinen Proxy verwenden
    Else
    $proxy = ""
    $open = _WinHttpOpen()
    EndIf
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ;Automatische Einstellung (aber kein Proxy vorhanden)
    Else
    $proxy = ""
    $open = _WinHttpOpen()

    [/autoit] [autoit][/autoit] [autoit]

    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ;Adresse explizit angegeben
    Else
    $open = _WinHttpOpen(Default, $WINHTTP_ACCESS_TYPE_NAMED_PROXY, $proxy)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ;Verbindung herstellen
    $connect = _WinHttpConnect($open, $host)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;SSL-Handshake (oder auch nicht - je nach Konfiguration)
    If $use_ssl = True Then
    Local $request = _WinHttpOpenRequest($connect, "POST", $script, Default, $WINHTTP_NO_REFERER, Default, BitOR($WINHTTP_FLAG_SECURE, $WINHTTP_FLAG_ESCAPE_DISABLE))
    Else
    Local $request = _WinHttpOpenRequest($connect, "POST", $script, Default, $WINHTTP_NO_REFERER)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If Not $request Then
    _WinHttpCloseHandle($connect)
    _WinHttpCloseHandle($open)
    Return 0
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ;htaccess-Authentifizierung (falls gewünscht)
    If $htaccess_user <> "" And $htaccess_pass <> "" Then _WinHttpSetCredentials($request, $WINHTTP_AUTH_TARGET_SERVER, $WINHTTP_AUTH_SCHEME_BASIC, $htaccess_user, $htaccess_pass)

    [/autoit] [autoit][/autoit] [autoit]

    ;Anfrage starten
    _WinHttpSendRequest($request, $header, $data)
    If @error Then
    _WinHttpCloseHandle($request)
    _WinHttpCloseHandle($connect)
    _WinHttpCloseHandle($open)
    Return 0
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ;Antwort empfangen
    _WinHttpReceiveResponse($request)
    If @error Then
    _WinHttpCloseHandle($request)
    _WinHttpCloseHandle($connect)
    _WinHttpCloseHandle($open)
    Return 0
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ;Antwort auswerten
    Local $answer = _WinHttpSimpleReadData($request, 0)

    [/autoit] [autoit][/autoit] [autoit]

    _WinHttpCloseHandle($request)
    _WinHttpCloseHandle($connect)
    _WinHttpCloseHandle($open)

    [/autoit] [autoit][/autoit] [autoit]

    Return $answer

    [/autoit] [autoit][/autoit] [autoit]

    EndFunc

    [/autoit]

    Einmal editiert, zuletzt von ip_trainer (3. Mai 2011 um 12:35) aus folgendem Grund: Fehler im Quellcode korrigiert.

  • Hallo,

    ich hab's rausgefunden...

    Wenn man in obenstehendem Skript in Zeile 189:

    [autoit]

    Local $answer = _WinHttpSimpleReadData($request, 0)

    [/autoit]


    das Flag von 0 auf 2 setzt (also binäre Daten statt ASCII empfangen), dann erhält man als Antwort einen Hex-Wert, z.B. "0x504B03040A00000000000215A33E58290BB50E0000000E00 [...]".

    Diesen Wert kann man nun binär in eine Datei schreiben:

    [autoit]

    $bin = "0x504B03040A00000000000215A33E58290BB50E0000000E00 [...] "
    $hFile = FileOpen("test.zip", BitOR(16, 2))
    FileWrite($hFile, $bin)
    FileClose($hFile)

    [/autoit]

    :D Gruß Trainer

    Einmal editiert, zuletzt von ip_trainer (3. Mai 2011 um 12:15)

  • Hi,

    ich habe das ganze mal noch in eine Funktion gepackt, die alles für einen erledigt:

    Spoiler anzeigen
    [autoit]

    Func download_data ($host, $remote_file, $local_file="", $use_ssl=False, $htaccess_user="", $htaccess_pass="", $proxy="auto")

    [/autoit] [autoit][/autoit] [autoit]

    ;===========================================================================
    ;Beispiel
    ;===========================================================================

    [/autoit] [autoit][/autoit] [autoit]

    ;$host = "irgendeine-domain.de"
    ;$remote_file = "/verzeichnis/gewuenschte_download_datei.zip"
    ;$local_file = Pfad und Dateiname unter welchem die Datei gespeichert werden soll (falls leer, wird @ScriptDir + Remote-Dateiname verwendet)

    [/autoit] [autoit][/autoit] [autoit]

    ;$use_ssl = True / False (*Default)

    [/autoit] [autoit][/autoit] [autoit]

    ;$htaccess_user = "benutzer_name"
    ;$htaccess_pass = "benutzer_passwort"

    [/autoit] [autoit][/autoit] [autoit]

    ;$proxy = "auto" (holt sich - falls vorhanden - die IE-Einstellungen) / explizite Angabe (z.B. 192.168.178.1:8080)

    [/autoit] [autoit][/autoit] [autoit]

    ;----------------------------------------------------------------

    [/autoit] [autoit][/autoit] [autoit]

    ;AUFRUF: $ergebnis = download_data ($host, $remote_file [, $local_file] [, $use_ssl] [, $htaccess_user] [, $htaccess_pass] [, $proxy])
    ;ERGEBNIS: Pfad+Dateiname der geschriebenen Datei (als String) bzw. False

    [/autoit] [autoit][/autoit] [autoit]

    ;===========================================================================

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;---------------------------------------------------------------------

    [/autoit] [autoit][/autoit] [autoit]

    ;Proxy-Konfiguration
    If($proxy = "auto") Then

    [/autoit] [autoit][/autoit] [autoit]

    ;Internet-Explorer-Einstellungen laden
    $proxy_config = _WinHttpGetIEProxyConfigForCurrentUser()

    [/autoit] [autoit][/autoit] [autoit]

    ;Eine Angabe für alle Proxies vorhanden --> einfach
    If $proxy_config[2] <> "" And Not StringInStr($proxy_config[2], "=") Then
    $proxy = $proxy_config[2]
    $open = _WinHttpOpen(Default, $WINHTTP_ACCESS_TYPE_NAMED_PROXY, $proxy)

    [/autoit] [autoit][/autoit] [autoit]

    ;Ausdifferenzierte Proxy-Angaben vorhanden (passenden Proxy herausfiltern)
    ElseIf StringInStr($proxy_config[2], "=") Then

    [/autoit] [autoit][/autoit] [autoit]

    $all_proxies =StringSplit($proxy_config[2], ";")

    [/autoit] [autoit][/autoit] [autoit]

    ;SSL wird verwendet --> nach https-Proxy suchen
    If $use_ssl = True Then
    ;Anagbe für https-Proxy existiert --> diese nehemen
    If StringInStr($proxy_config[2], "https=") Then
    For $i = 1 To UBound($all_proxies) -1
    If StringInStr($all_proxies[$i], "https=") Then
    $proxy= StringTrimLeft($all_proxies[$i], 6)
    $open = _WinHttpOpen(Default, $WINHTTP_ACCESS_TYPE_NAMED_PROXY, $proxy)
    EndIf
    Next

    [/autoit] [autoit][/autoit] [autoit]

    ;Keine gesonderte Angabe--> keinen Proxy verwenden
    Else
    $proxy = ""
    $open = _WinHttpOpen()
    EndIf

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;SSL wird NICHT verwendet --> nach http-Proxy suchen
    Else
    ;Angabe für http-Proxy existiert --> diese nehemen
    If StringInStr($proxy_config[2], "http=") Then
    For $i = 1 To UBound($all_proxies) -1
    If StringInStr($all_proxies[$i], "http=") Then
    $proxy= StringTrimLeft($all_proxies[$i], 5)
    $open = _WinHttpOpen(Default, $WINHTTP_ACCESS_TYPE_NAMED_PROXY, $proxy)
    EndIf
    Next

    [/autoit] [autoit][/autoit] [autoit]

    ;Keine gesonderte Angabe--> keinen Proxy verwenden
    Else
    $proxy = ""
    $open = _WinHttpOpen()
    EndIf
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ;Automatische Einstellung (aber kein Proxy vorhanden)
    Else
    $proxy = ""
    $open = _WinHttpOpen()

    [/autoit] [autoit][/autoit] [autoit]

    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ;Adresse explizit angegeben
    Else
    $open = _WinHttpOpen(Default, $WINHTTP_ACCESS_TYPE_NAMED_PROXY, $proxy)
    EndIf

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;Verbindung herstellen
    $connect = _WinHttpConnect($open, $host)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;SSL-Handshake (oder auch nicht - je nach Konfiguration)
    If $use_ssl = True Then
    Local $request = _WinHttpOpenRequest($connect, "POST", $remote_file, Default, $WINHTTP_NO_REFERER, Default, BitOR($WINHTTP_FLAG_SECURE, $WINHTTP_FLAG_ESCAPE_DISABLE))
    Else
    Local $request = _WinHttpOpenRequest($connect, "POST", $remote_file, Default, $WINHTTP_NO_REFERER)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If Not $request Then
    _WinHttpCloseHandle($connect)
    _WinHttpCloseHandle($open)
    Return 0
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ;htaccess-Authentifizierung (falls gewünscht)
    If $htaccess_user <> "" And $htaccess_pass <> "" Then _WinHttpSetCredentials($request, $WINHTTP_AUTH_TARGET_SERVER, $WINHTTP_AUTH_SCHEME_BASIC, $htaccess_user, $htaccess_pass)

    [/autoit] [autoit][/autoit] [autoit]

    ;Anfrage starten
    _WinHttpSendRequest($request)
    If @error Then
    _WinHttpCloseHandle($request)
    _WinHttpCloseHandle($connect)
    _WinHttpCloseHandle($open)
    Return 0
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ;Antwort empfangen
    _WinHttpReceiveResponse($request)
    If @error Then
    _WinHttpCloseHandle($request)
    _WinHttpCloseHandle($connect)
    _WinHttpCloseHandle($open)
    Return 0
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ;Antwort auswerten
    Local $answer = _WinHttpSimpleReadData($request, 2)

    [/autoit] [autoit][/autoit] [autoit]

    _WinHttpCloseHandle($request)
    _WinHttpCloseHandle($connect)
    _WinHttpCloseHandle($open)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; ++++++++ Anwort in Datei schreiben +++++++++++

    [/autoit] [autoit][/autoit] [autoit]

    ;Remote-Dateiname übernehmen?
    If $local_file = "" Then
    $local_file = StringSplit($remote_file, "/", 1)
    $local_file = @ScriptDir & "\" & $local_file[$local_file[0]]
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Local $file_to_write = FileOpen($local_file, BitOR(16, 2))

    [/autoit] [autoit][/autoit] [autoit]

    ;Datei konnte nicht zum Schreiben geöffnet werden
    If $file_to_write = -1 Then
    Return False
    Else
    FileWrite($file_to_write, $answer)
    FileClose($file_to_write)
    Return $local_file
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    EndFunc

    [/autoit]

    Viel Spass damit :) ...

    Gruß
    Trainer