#Include Once "window9.bi"
#Include Once "crt/stdlib.bi" 

Function InetOpen(szUserAgent As String = "FB", iType As Integer = INTERNET_OPEN_TYPE_DIRECT , szProxyName As String = "" ,szProxyBypass As String="", iFlags As Integer = 0) As HINTERNET export

	Return InternetOpen(StrPtr(szUserAgent), iType, StrPtr(szProxyName), StrPtr(szProxyBypass), iFlags)

End Function

Function OpenUrl(hInet As HINTERNET, szURL As String ,szHeaders As String = "",iSizeHeaders As Integer = 0, iFlags As Integer = INTERNET_FLAG_RELOAD) As HINTERNET Export

	Return InternetOpenUrl( hInet, StrPtr(szUrl), StrPtr(szHeaders),iSizeHeaders , iFlags, 0)

End Function

Function InetReadFile(hUrl As HINTERNET, psData As Any Ptr, iLenData As Integer) As Integer Export

	Dim As Integer iBytes

	InternetReadFile(hURL,psData,iLenData, @iBytes)

	Return iBytes

End Function

Sub InetFreeHandle(handle As HINTERNET) Export

	InternetCloseHandle(handle)

End Sub

Function GetHTTPHeader(hUrl As HINTERNET) As String Export

	Dim As String sOutBuffer

	Dim As String sReturn

	Dim iSize As Integer

	HttpQueryInfo(hUrl,HTTP_QUERY_RAW_HEADERS_CRLF,@sOutBuffer,@iSize,NULL)

	If iSize>0 Then

		sOutBuffer = Space(iSize)

		HttpQueryInfo(hUrl,HTTP_QUERY_RAW_HEADERS_CRLF,StrPtr(sOutBuffer),@iSize,NULL)

		Return RTrim(sOutBuffer)

	EndIf

End Function

Function ReceiveHTTPFile(sUrl As String, sFile As String) As Integer Export
		
	Dim fURLDownloadToFile As Function ( _
	ByVal pCaller As Long , _
	ByVal szURL As ZString Ptr , _
	ByVal szFileName As ZString Ptr , _
	ByVal dwReserved As Long , _
	ByVal lpfnCB As Long ) As Long
	
	Dim hLib As Any Ptr = DylibLoad ( "urlmon.dll" )

   fURLDownloadToFile = DylibSymbol ( hLib , "URLDownloadToFileA" ) 
   
   Function = fURLDownloadToFile ( 0 , sURL , sFile , 0 , 0 ) 
   
   DylibFree hLib

End Function

Function GetContentSize(hUrl As HINTERNET) As ULongInt Export
	Dim As ZString*32 szData
	Dim As Integer iSize = 32
	HttpQueryInfo(hURL,HTTP_QUERY_CONTENT_LENGTH,@szData,@iSize,0)
	return atof(szData)
End Function

Function ftpinfo.FtpExamineDirectory(ByVal hConnect As HINTERNET, ByVal DirectoryName As String,ByVal Pattern As String, dwFlags As Integer = 0) As HINTERNET Export
	Var strPattern = DirectoryName + Pattern
	Return FtpFindFirstFile(hConnect,strPattern, @FileInformation,dwFlags,0)
End Function

Function FTPINFO.FtpNextDirectoryEntry(ByVal hFind As HINTERNET) As Integer Export
	Return InternetFindNextFile(hFind, @FileInformation)
End Function

Function FtpFinishDirectory(ByVal hFind As HINTERNET) As Integer Export
	Return InternetCloseHandle(hFind)
End Function

Function FtpConnect(hInet As HINTERNET, ServerName As String , UserName As String , UserPassword As String , ServerPort As Integer = 21, Flags As Integer = 0) As HINTERNET Export
	Return InternetConnect( hInet,ServerName,ServerPort,UserName,UserPassword,INTERNET_SERVICE_FTP,Flags,0) 'INTERNET_FLAG_PASSIVE
End Function

Function FtpFileGet(hConnect As HINTERNET, RemoteFile As String , LocalFile As String , fFailExists As Integer = 0, dwFlagAttributes As Integer = 0, dwFlags As Integer = FTP_TRANSFER_TYPE_BINARY ) As Integer Export
	Return FtpGetFile(hConnect, RemoteFile, LocalFile,fFailExists, dwFlagAttributes, dwFlags, 0)
End Function

Function FtpFilePut(hInet As HINTERNET, LocalFile As String , RemoteFile As String , dwFlags As Integer = FTP_TRANSFER_TYPE_BINARY) As Integer Export
	Return FtpPutFile( hInet,LocalFile,RemoteFile,dwFlags, 0)
End Function

Function FTPINFO.FtpDirectoryEntryAttributes() As Integer Export
	Return FileInformation.dwFileAttributes
End Function

Function FTPINFO.FtpDirectoryEntrySize() As ULongInt Export
  Return CULngInt( cuLngInt(FileInformation.nFileSizeLow) or (CULngInt(FileInformation.nFileSizeHigh) shl 32) )
End Function

Function FTPINFO.FtpDirectoryEntryDate() As String Export
	Dim As FILETIME fttime
	Dim As FILETIME ftlocal
	Dim As SYSTEMTIME SYS_TIME
	Dim As String hour_,minute_,second_,month_,Day_
	fttime=FileInformation.ftLastWriteTime

	FileTimeToLocalFileTime(@fttime,@ftlocal)
	FileTimeToSystemTime(@ftlocal,@SYS_TIME)
	If SYS_TIME.wday<10 Then
		Day_="0" & SYS_TIME.wday
	Else
		Day_=Str( SYS_TIME.wday)
	EndIf
	If SYS_TIME.wmonth<10 Then
		month_="0" & SYS_TIME.wmonth
	Else
		month_=Str( SYS_TIME.wmonth)
	EndIf
	If SYS_TIME.whour<10 Then
		hour_="0" & SYS_TIME.whour
	Else
		hour_=Str( SYS_TIME.whour)
	EndIf
	If SYS_TIME.wminute<10 Then
		minute_="0" & SYS_TIME.wminute
	Else
		minute_=Str( SYS_TIME.wminute)
	EndIf
	If SYS_TIME.wsecond<10 Then
		second_="0" & SYS_TIME.wsecond
	Else
		second_=Str(SYS_TIME.wsecond)
	EndIf
	Return day_ & "." & month_ & "." & SYS_TIME.wyear & " " & hour_ & ":" & minute_ & ":" & second_
End Function

Function FTPINFO.FtpDirectoryEntryName() As String Export
	Return FileInformation.cFileName
End Function

Function FtpSetDirectory(hConnect As HINTERNET, Directory As String) As Integer Export
	Return FtpSetCurrentDirectory(hConnect,Directory)
End Function

Function FtpGetDirectory(hConnect As HINTERNET) As String Export
	Dim As Integer Size = 256
	Dim Directory As String*256
	FtpGetCurrentDirectory(hConnect,Directory,@Size)
	Return Directory
End Function

Function FtpFileOpen(hConnect As HINTERNET, File As String , dwFlags As Integer = FTP_TRANSFER_TYPE_BINARY, dwAccess As Integer = GENERIC_WRITE) As HINTERNET Export
	Return FtpOpenFile(hConnect,File,dwAccess,dwFlags,INTERNET_FLAG_RELOAD)
End Function

Function FtpFileClose(hFile As HINTERNET) As Integer Export
	Return InternetCloseHandle(hFile)
End Function

Function FtpWriteFile(hFile As HINTERNET, Buffer As Any ptr , nBuffer As Integer ) As Integer Export
	Dim As Integer iBytes
	InternetWriteFile(hFile,Buffer,nBuffer,@iBytes)
	Return iBytes
End Function

Function FtpGetSizeFile(hFile As HINTERNET) As ULongint Export
	Dim As UInteger iBytesHigh,iBytesLow
	iBytesLow = FtpGetFileSize(hFile,@iBytesHigh)
	Return CULngInt( cuLngInt(iBytesLow) or (CULngInt(iBytesHigh) shl 32) )
End Function
