#include once "window9.bi"
#include Once "crt/stdlib.bi" 

Function InetOpen(ByRef szUserAgent As USTRING, byval iType As Integer, ByRef szProxyName As USTRING, byref szProxyBypass As USTRING, byval iFlags As Integer) As HINTERNET export 
	#ifdef UNICODE
		Return InternetOpen(*szUserAgent, iType, *szProxyName, *szProxyBypass, iFlags)	
	#else	
		Return InternetOpen(StrPtr(szUserAgent), iType, StrPtr(szProxyName), StrPtr(szProxyBypass), iFlags)	
	#EndIf
End Function

Function OpenUrl(byval hInet As HINTERNET, ByRef szURL As USTRING, ByRef szHeaders As USTRING, byval iSizeHeaders As Integer, byval iFlags As Integer) As HINTERNET Export  
	#ifdef UNICODE
		Return InternetOpenUrl( hInet, *szUrl, *szHeaders,iSizeHeaders , iFlags, 0)
	#else	
		Return InternetOpenUrl( hInet, StrPtr(szUrl), StrPtr(szHeaders),iSizeHeaders , iFlags, 0)
	#EndIf
End Function

Function InetReadFile(byval hUrl As HINTERNET, byval psData As Any Ptr, byval iLenData As Integer) As Integer Export
	Dim As long iBytes
	InternetReadFile(hURL,psData,iLenData, @iBytes)
	Return iBytes
End Function

Sub InetFreeHandle(byval handle As HINTERNET) Export
	InternetCloseHandle(handle)
End Sub

Function GetHTTPHeader(byval hUrl As HINTERNET) As USTRING Export
	Dim As USTRING sOutBuffer
	Dim As USTRING sReturn
	Dim iSize As long	
	#ifdef UNICODE
		HttpQueryInfo(hUrl,HTTP_QUERY_RAW_HEADERS_CRLF,cast(any ptr , *sOutBuffer),@iSize,NULL)
		If iSize>0 Then
			sOutBuffer = WSpace(iSize)
			HttpQueryInfo(hUrl,HTTP_QUERY_RAW_HEADERS_CRLF,cast(any ptr , *sOutBuffer),@iSize,NULL)
			sOutBuffer = RTrim(sOutBuffer) 
			Return sOutBuffer
		End If  	
	#else	
		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)
		End If  	
	#EndIf
End Function

Function ReceiveHTTPFile(ByRef sUrl As USTRING, ByRef sFile As USTRING) As Integer Export	
	Dim hLib As Any Ptr = DylibLoad ( "urlmon.dll" )
	#ifdef UNICODE
		Dim fURLDownloadToFile As Function (ByVal pCaller As Long , _
		ByVal szURL As WString Ptr , _
		ByVal szFileName As WString Ptr , _
		ByVal dwReserved As Long , _
		ByVal lpfnCB As Long ) As Long 
		fURLDownloadToFile = DylibSymbol ( hLib , "URLDownloadToFileW" ) 
		Function = fURLDownloadToFile ( 0 , cast(any ptr , *sURL) , cast(any ptr , *sFile) , 0 , 0 ) 
	#else	
		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
		fURLDownloadToFile = DylibSymbol ( hLib , "URLDownloadToFileA" ) 
		Function = fURLDownloadToFile ( 0 , sURL , sFile , 0 , 0 )   	
	#EndIf
	DylibFree hLib
End Function

Function GetContentSize(byval hUrl As HINTERNET) As ULongInt Export
	#ifdef UNICODE
		Dim As wString*32 szData
		Dim As long iSize = 32
		HttpQueryInfo(hURL,HTTP_QUERY_CONTENT_LENGTH,@szData,@iSize,0) 	
	#else	
		Dim As ZString*32 szData
		Dim As long iSize = 32
		HttpQueryInfo(hURL,HTTP_QUERY_CONTENT_LENGTH,@szData,@iSize,0)  	
	#EndIf
	return atof(szData)
End Function

Function ftpinfo.FtpExamineDirectory(ByVal hConnect As HINTERNET, ByRef DirectoryName As USTRING, ByRef Pattern As USTRING, byval dwFlags As Integer) As HINTERNET Export
	dim as USTRING strPattern = DirectoryName + Pattern 
	#ifdef UNICODE
		Return FtpFindFirstFile(hConnect,*strPattern, @FileInformation,dwFlags,0)
	#else	
		Return FtpFindFirstFile(hConnect,strPattern, @FileInformation,dwFlags,0)
	#EndIf
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(byval hInet As HINTERNET, ByRef ServerName As USTRING, ByRef UserName As USTRING, ByRef UserPassword As USTRING, byval ServerPort As Integer, byval Flags As Integer) As HINTERNET Export
	#ifdef UNICODE
		Return InternetConnect( hInet,*ServerName,ServerPort,*UserName,*UserPassword,INTERNET_SERVICE_FTP,Flags,0) 'INTERNET_FLAG_PASSIVE
	#else	
		Return InternetConnect( hInet,ServerName,ServerPort,UserName,UserPassword,INTERNET_SERVICE_FTP,Flags,0) 'INTERNET_FLAG_PASSIVE
	#EndIf
End Function

Function FtpFileGet(byval hConnect As HINTERNET, ByRef RemoteFile As USTRING, ByRef LocalFile As USTRING, byval fFailExists As Integer, byval dwFlagAttributes As Integer, byval dwFlags As Integer) As Integer Export 
	#ifdef UNICODE
		Return FtpGetFile(hConnect, *RemoteFile, *LocalFile,fFailExists, dwFlagAttributes, dwFlags, 0)
	#else	
		Return FtpGetFile(hConnect, RemoteFile, LocalFile,fFailExists, dwFlagAttributes, dwFlags, 0)
	#EndIf
End Function

Function FtpFilePut(byval hInet As HINTERNET, byref LocalFile As USTRING, ByRef RemoteFile As USTRING, byval dwFlags As Integer) As Integer Export
	#ifdef UNICODE
		Return FtpPutFile( hInet,*LocalFile,*RemoteFile,dwFlags, 0)
	#else	
		Return FtpPutFile( hInet,LocalFile,RemoteFile,dwFlags, 0)
	#EndIf 
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 USTRING Export
	Dim As FILETIME fttime
	Dim As FILETIME ftlocal
	Dim As SYSTEMTIME SYS_TIME
	Dim As USTRING 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
	day_ = day_ & "." & month_ & "." & SYS_TIME.wyear & " " & hour_ & ":" & minute_ & ":" & second_
	Return day_
End Function

Function FTPINFO.FtpDirectoryEntryName() As USTRING Export
	dim as USTRING sRet = FileInformation.cFileName
	Return sRet
End Function

Function FtpSetDirectory(byval hConnect As HINTERNET, ByRef sDirectory As USTRING) As Integer Export
	#ifdef UNICODE
		Return FtpSetCurrentDirectory(hConnect,*sDirectory)
	#else
		Return FtpSetCurrentDirectory(hConnect,sDirectory)
	#EndIf
End Function

Function FtpGetDirectory(byval hConnect As HINTERNET) As USTRING Export
	Dim As long Size = 256
	#ifdef UNICODE
		Dim sDirectory As USTRING = wspace(256)
		FtpGetCurrentDirectory(hConnect,cast(any ptr , *sDirectory),@Size)  	
	#else
		Dim sDirectory As String*256
		FtpGetCurrentDirectory(hConnect,sDirectory,@Size)  	
	#EndIf
	Return sDirectory
End Function

Function FtpFileOpen(byval hConnect As HINTERNET, byref File As USTRING, byval dwFlags As Integer, byval dwAccess As Integer) As HINTERNET Export
	#ifdef UNICODE
		Return FtpOpenFile(hConnect,*File,dwAccess,dwFlags,INTERNET_FLAG_RELOAD)
	#else	
		Return FtpOpenFile(hConnect,File,dwAccess,dwFlags,INTERNET_FLAG_RELOAD)
	#EndIf
End Function

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

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

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