#include "fivewin.ch"
// File Attributes
#define FILE_ATTRIBUTE_READONLY 1
#define FILE_ATTRIBUTE_HIDDEN 2
#define FILE_ATTRIBUTE_SYSTEM 4
#define FILE_ATTRIBUTE_DIRECTORY 16
#define FILE_ATTRIBUTE_ARCHIVE 32
#define FILE_ATTRIBUTE_NORMAL 128
#define FILE_ATTRIBUTE_TEMPORARY 256
// Access Types for InternetOpen()
#define INTERNET_OPEN_TYPE_PRECONFIG 0 // use registry configuration
#define INTERNET_OPEN_TYPE_DIRECT 1 // direct to net
#define INTERNET_OPEN_TYPE_PROXY 3 // via named proxy
#define INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY 4 // prevent using java/script/INS
// Manifests
#define INTERNET_INVALID_PORT_NUMBER 0 // use the protocol-specific default
#define INTERNET_DEFAULT_FTP_PORT 21 // default for FTP servers
#define INTERNET_DEFAULT_GOPHER_PORT 70 // " " gopher "
#define INTERNET_DEFAULT_HTTP_PORT 80 // " " HTTP "
#define INTERNET_DEFAULT_HTTPS_PORT 443 // " " HTTPS "
#define INTERNET_DEFAULT_SOCKS_PORT 1080 // default for SOCKS firewall servers.
// Service Types for InternetConnect()
#define INTERNET_SERVICE_FTP 1
#define INTERNET_SERVICE_GOPHER 2
#define INTERNET_SERVICE_HTTP 3
// Flags for FTP
#define INTERNET_FLAG_TRANSFER_ASCII 1
#define INTERNET_FLAG_TRANSFER_BINARY 2
// File Access Types
#define GENERIC_READ 2147483648
#define GENERIC_WRITE 1073741824
STATIC cFtpSite, cFtpUserName, cFtpPassword, cDestination
FUNCTION Main()
LOCAL lOk := .F.
cFtpSite := "ftp.FtpSite.com"
cFtpUserName := "abcd@FtpSite.com"
cFtpPassword := "xyz@12345"
cDestination := "/MyDestinationFolder/"
* 1 Upload
lOk := UpLoadDownLoad(1, {"uplddnld.prg"}, cDestination, "UPLDDNLD.PRG")
* 2 Download
UpLoadDownLoad(2, {cDestination+"/uplddnld.prg"}, "C:\FWH\SAMPLES\", "UPLDDNLD.PRG")
RETURN nil
*******************************************************************************
*** FUNCTION UpLoadDownLoad(nOpt) - Upload / Download Data to/from FTP Site ***
*******************************************************************************
FUNCTION UpLoadDownLoad(nUpDown, aDataFiles, cDestination, cDataName, lAuto)
LOCAL oBrush, oDlg, oGroup, oGroup1, oGroup2
LOCAL oMeter1, oMeter2, nMeter1, nMeter2, cTitle
LOCAL aUpLoadFiles := {}, aDownLoadFiles := {}
LOCAL bAction, lOk := .F., lIsInternet := .F.
LOCAL aGradiate := { { .25, nRgb( 152,194,152 ), CLR_WHITE }, { .75,CLR_WHITE, nRgb( 152,194,152 ) } }
DEFAULT lAuto := .F.
MsgInfo("Trying to connect to Internet. Please wait...")
lIsInternet := IsInterNet()
IF !lIsInternet
RETURN .F.
ENDIF
IF nUpDown = 1
IF !lAuto
aUpLoadFiles := ACLONE(aDataFiles)
cTitle := "Uploading Data. Please wait..."
bAction := {||lOk := FtpUpLoad( cFtpSite, ;
cFtpUsername, ;
cFtpPassword, ;
oMeter1, ;
oMeter2, ;
aUpLoadFiles, ;
cDestination, ;
cDataName, ;
lAuto, oDlg)}
ELSE
aUpLoadFiles := ACLONE(aDataFiles)
lOk := FtpUpLoad( cFtpSite, cFtpUsername, cFtpPassword, ;
oMeter1, oMeter2, aUpLoadFiles, ;
cDestination, cDataName, lAuto)
ENDIF
ELSE
IF !lAuto
aDownLoadFiles := ACLONE(aDataFiles)
cTitle := "Downloading Data. Please wait..."
bAction := {||lOk := FtpDownLoad(cFtpSite, ;
cFtpUsername, ;
cFtpPassword, ;
oMeter1, ;
oMeter2, ;
aDownLoadFiles, ;
cDestination, ;
cDataName, ;
lAuto,oDlg)}
ELSE
lOk := FtpDownLoad(cFtpSite, cFtpUsername, cFtpPassword,;
oMeter1, oMeter2, aDownLoadFiles, ;
cDestination, cDataName, lAuto, oDlg)
ENDIF
ENDIF
IF !lAuto
DEFINE BRUSH oBrush GRADIENT aGradiate
DEFINE DIALOG oDlg RESOURCE "CONNECT" ;
COLORS CLR_BLACK, nRGB(250,239,247) ;
TRANSPARENT BRUSH oBrush
oDlg:cTitle := cTitle
REDEFINE GROUP oGroup ID 301 OF oDlg TRANSPARENT COLOR CLR_HRED
REDEFINE GROUP oGroup1 ID 302 OF oDlg TRANSPARENT COLOR CLR_HBLUE
REDEFINE METEREX oMeter1 VAR nMeter1 ID 101 OF oDlg
REDEFINE GROUP oGroup2 ID 303 OF oDlg TRANSPARENT COLOR CLR_HBLUE
REDEFINE METEREX oMeter2 VAR nMeter2 ID 102 OF oDlg ;
GRADIENT CHUNK { { 1/2, nRGB( 255, 251, 229 ), nRGB( 250, 223, 143 ) } ,;
{ 1/2, nRGB( 244, 194, 51 ), nRGB( 252, 235, 173 ) } } ;
GRADIENT TRACK { { 1/2, nRGB( 198, 203, 213 ), nRGB( 219, 224, 233 ) } ,;
{ 1/2, nRGB( 224, 238, 237 ), nRGB( 224, 238, 237 ) } } ;
* This block gets evaluated only the first time the DialogBox is painted !!!
oDlg:bStart := {||(Eval( bAction), SysWait(1), oDlg:End())}
ACTIVATE DIALOG oDlg CENTERED
RELEASE BRUSH oBrush
RETURN lOk
ENDIF
RETURN lOk
*******************************************************************************
*** STATIC FUNCTION UpLoad(oMeter, cSourceFile, cDestination) ***
*******************************************************************************
STATIC FUNCTION FTPUpLoad(cFtpSite, cUsername, cPassword, oMeter1, oMeter2, ;
aSourceFiles, cDestination, cDataName, lAuto, ;
oDlg)
LOCAL hInternet, hConnect, hSource, hDest, nRead
LOCAL cSourceFile, cData := SPACE( 32768 ) //cData := SPACE( 1024 )
LOCAL nPos := 0, n, lSucess := .T., nTotal := 0
hInternet := InternetOpen( "Anystring", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0 )
hConnect := InternetConnect( hInternet, cFtpSite, INTERNET_INVALID_PORT_NUMBER,;
cUsername, cPassword, INTERNET_SERVICE_FTP, 0, 0 )
IF !lAuto
oMeter1:Set( 0 )
oMeter1:nTotal := LEN(aSourceFiles)
ENDIF
FOR n = 1 TO LEN(aSourceFiles)
cSourceFile := SUBSTR(aSourceFiles[n],RAT("\",aSourceFiles[n])+1)
hDest := FtpOpenFile( hConnect, cDestination+cSourceFile, GENERIC_WRITE, 0, 0 )
nTotal := FSize(aSourceFiles[n])
IF !lAuto
oMeter2:Set( 0 )
oMeter2:nTotal := nTotal
ENDIF
hSource := FOpen(aSourceFiles[n])
WHILE .T.
nRead := FRead( hSource, @cData, LEN( cData ) )
IF nRead = 0
IF FERROR() # 0
lSucess := .F.
ENDIF
EXIT
ENDIF
IF !InternetWriteFile( hDest, @cData, nRead )
lSucess := .F.
EXIT
ELSE
nPos += LEN( cData )
IF !lAuto
oMeter2:Set( nPos )
ENDIF
ENDIF
ENDDO
IF !lAuto
oMeter1:Set( n )
ENDIF
FClose( hSource )
InternetCloseHandle( hDest )
SysRefresh()
IF .NOT. lSucess
EXIT
ENDIF
NEXT
InternetCloseHandle( hConnect )
InternetCloseHandle( hInternet )
SysRefresh()
IF .NOT. lAuto
IF lSucess
MsgInfo(cDataName+" has been Uploaded Successfully.")
ELSE
MsgInfo("Error in Uploading "+cDataName+".")
ENDIF
ENDIF
IF !lAuto
oMeter2:Set( 0 )
oMeter1:Set( 0 )
oDlg:End()
ENDIF
RETURN lSucess
*******************************************************************************
*** STATIC FUNCTION DownLoad( oMeter, cSourceFile, cDestFile ) ***
*******************************************************************************
STATIC FUNCTION FTPDownLoad(cFtpSite, cUsername, cPassword, oMeter1, oMeter2, ;
aSourceFiles, cDestFile, cDataName, lAuto, ;
oDlg)
LOCAL hInternet, hConnect, hSource, hDest, nRead
LOCAL cSourceFile, cData := SPACE( 32768 ) //cData := SPACE( 1024 )
LOCAL nPos := 0, n, lSucess := .T.
hInternet := InternetOpen( "Anystring", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0 )
hConnect := InternetConnect( hInternet, cFtpSite, INTERNET_INVALID_PORT_NUMBER,;
cUsername, cPassword, INTERNET_SERVICE_FTP, 0, 0 )
IF !lAuto
oMeter1:Set( 0 )
oMeter1:nTotal := LEN(aSourceFiles)
ENDIF
FOR n = 1 TO LEN(aSourceFiles)
cSourceFile := aSourceFiles[n]
hSource := FtpOpenFile( hConnect, cSourceFile, GENERIC_READ, 0, 0 )
IF !lAuto
oMeter2:Set( 0 )
oMeter2:nTotal := FtpGetFileSize( hSource )
ENDIF
hDest := FCreate( cDestFile+SUBSTR(cSourceFile,RAT("/",cSourceFile)+1) )
WHILE .T.
nRead := InternetReadFile( hSource, @cData )
IF nRead = -1
lSucess := .F.
EXIT
ENDIF
IF nRead = 0
EXIT
ENDIF
FWRITE( hDest, cData, nRead )
nPos += LEN(ALLTRIM(cData))
IF !lAuto
oMeter2:Set( nPos )
ENDIF
ENDDO
FClose( hDest )
InternetCloseHandle( hSource )
SysRefresh()
IF !lAuto
oMeter1:Set( n )
ENDIF
IF .NOT. lSucess
EXIT
ENDIF
NEXT
InternetCloseHandle( hConnect )
InternetCloseHandle( hInternet )
SysRefresh()
IF .NOT. lAuto
IF lSucess
MsgInfo(cDataName+" has been Downloaded Successfully.")
ELSE
MsgInfo("Error in Downloading "+cDataName+".")
ENDIF
ENDIF
IF !lAuto
oMeter2:Set( 0 )
oMeter1:Set( 0 )
oDlg:End()
ENDIF
RETURN lSucess
*******************************************************************************
*** FTP Upload/Download FUNCTION Wrappers of WININET.LIB ***
*******************************************************************************
#pragma BEGINDUMP
#include "windows.h"
#include "wininet.h"
#include "hbapi.h"
HB_FUNC( INTERNETOPEN )
{
hb_retnl( ( LONG ) InternetOpen( hb_parc( 1 ), hb_parnl( 2 ), hb_parc( 3 ), hb_parc( 4 ), hb_parnl( 5 ) ) );
}
//***********************
HB_FUNC( INTERNETCLOSEHANDLE )
{
hb_retl( InternetCloseHandle( ( HINTERNET ) hb_parnl( 1 ) ) );
}
//***********************
HB_FUNC( INTERNETCONNECT )
{
hb_retnl( ( LONG ) InternetConnect( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), ( INTERNET_PORT ) hb_parnl( 3 ), hb_parc( 4 ), hb_parc( 5 ), hb_parnl( 6 ), hb_parnl( 7 ), hb_parnl( 8 ) ) );
}
//***********************
HB_FUNC( FTPOPENFILE )
{
hb_retnl( ( LONG ) FtpOpenFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parnl( 3 ), hb_parnl( 4 ), hb_parnl( 5 ) ) );
}
//***********************
HB_FUNC( FTPGETFILESIZE )
{
DWORD nFileSizeHigh;
hb_retnl( ( LONG ) FtpGetFileSize( ( HINTERNET ) hb_parnl( 1 ), &nFileSizeHigh ) );
}
//***********************
HB_FUNC( INTERNETREADFILE )
{
DWORD nBytesRead;
BOOL lSuccess = InternetReadFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parclen( 2 ), &nBytesRead );
if ( !lSuccess )
hb_retnl( -1 );
else
hb_retnl( nBytesRead );
}
//***********************
HB_FUNC( INTERNETWRITEFILE )
{
DWORD nBytesWritten;
BOOL lSuccess = InternetWriteFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parnl( 3 ), &nBytesWritten );
hb_retl( lSuccess );
}
HB_FUNC( FTPGETFILE )
{
hb_retl( FtpGetFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parc( 3 ), hb_parl( 4 ), hb_parnl( 5 ), hb_parnl( 6 ), hb_parnl( 7 ) ) );
}
HB_FUNC( FTPPUTFILE )
{
hb_retl( FtpPutFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parc( 3 ), hb_parnl( 4 ), hb_parnl( 5 ) ) );
}
HB_FUNC( FTPDELETEFILE )
{
hb_retl( FtpDeleteFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ) ) );
}
HB_FUNC( FTPCREATEDIRECTORY )
{
hb_retl( FtpCreateDirectory( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ) ) );
}
HB_FUNC( FTPREMOVEDIRECTORY )
{
hb_retl( FtpRemoveDirectory( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ) ) );
}
HB_FUNC( FTPFINDFIRSTFILE )
{
hb_retnl( ( LONG ) FtpFindFirstFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), ( WIN32_FIND_DATA * ) hb_parc( 3 ), hb_parnl( 4 ), hb_parnl( 5 ) ) );
}
HB_FUNC( INTERNETFINDNEXTFILE )
{
hb_retl( InternetFindNextFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ) ) );
}
#pragma ENDDUMP
CONNECT DIALOG 25, 31, 255, 97
STYLE DS_MODALFRAME | 0x4L | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Connecting to Modem..."
FONT 8, "MS Sans Serif"
{
GROUPBOX "Upload/Download", 301, 8, 6, 239, 81, BS_GROUPBOX
GROUPBOX "Total Data", 302, 15, 15, 225, 31, BS_GROUPBOX
CONTROL "TMeterEx", 101, "TMeterEx", 0 | WS_CHILD | WS_VISIBLE, 22, 25, 210, 14
GROUPBOX "Individual Data", 303, 15, 49, 225, 31, BS_GROUPBOX
CONTROL "TMeterEx", 102, "TMeterEx", 0 | WS_CHILD | WS_VISIBLE, 22, 59, 210, 14
}
SFTP (SSH File Transfer Protocol), not to be confused with FTPS (Secure FTP), runs on top of the SSH (Secure Shell) protocol
Antonio Linares wrote:Paco,
thanks
I guess you mean FTPS and not SFTPSFTP (SSH File Transfer Protocol), not to be confused with FTPS (Secure FTP), runs on top of the SSH (Secure Shell) protocol
edited: I just tested Harbour hbtip connecting to a FTPS server and these lines worked as expected:
local oUrl := TUrl():New( "ftps://username:password@domainname.com" )
? oUrl:cProto
hmpaquito wrote:SFTP (SSH File Transfer Protocol) is a secure file protocol that is used to access, manage, and transfer files over an encrypted SSH transport. When compared with the traditional FTP protocol, SFTP offers all the functionality of FTP, but it is more secure and easier to configure.Antonio Linares wrote:Paco,
thanks
I guess you mean FTPS and not SFTPSFTP (SSH File Transfer Protocol), not to be confused with FTPS (Secure FTP), runs on top of the SSH (Secure Shell) protocol
edited: I just tested Harbour hbtip connecting to a FTPS server and these lines worked as expected:
local oUrl := TUrl():New( "ftps://username:password@domainname.com" )
? oUrl:cProto
METHOD EnviaFtp(cFileaEnviar,cFileComplet) CLASS TElot
LOCAL cUrl,cUL
LOCAL oSelf:=SELF
curl_global_init()
cUrl := curl_easy_init()
hb_default( @cUL, "ftp://107.180.57.119/"+cFileaEnviar )
curl_easy_setopt( curl, HB_CURLOPT_URL, cUL )
curl_easy_setopt( cUrl, HB_CURLOPT_UPLOAD )
curl_easy_setopt( Curl, HB_CURLOPT_DL_BUFF_SETUP )
curl_easy_setopt( curl, HB_CURLOPT_UL_FILE_SETUP, cFileComplet )
curl_easy_setopt( curl, HB_CURLOPT_INFILESIZE, hb_vfSize(cFileComplet) )
curl_easy_setopt( curl, HB_CURLOPT_USERPWD, "guaicarbackup@guaicar.com.ve:TuClaveFTP" )
curl_easy_setopt( Curl, HB_CURLOPT_FAILONERROR, .T. )
curl_easy_setopt( Curl, HB_CURLOPT_FILETIME, .T. )
curl_easy_setopt( curl, HB_CURLOPT_XFERINFOBLOCK, {| nPos, nLen | PaintCircularMeter( oSelf:oWndZion:oWndClient, {130,30,430,330}, 0.5, nPos, nLen, ;
{ RGB( 0, 68, 118 ),Rgb( 237,165,12 ) } ) ;
} )
curl_easy_setopt( Curl, HB_CURLOPT_NOPROGRESS, 0 )
curl_easy_setopt( Curl, HB_CURLOPT_VERBOSE, .F. )
curl_easy_perform( cUrl )
Return self
https://www.puttygen.com/psftp
FUNCTION SFTP( cSrv, cPrt, cUsr, cPsw, cFil )
LOCAL lOk
MEMOWRIT( "psftp.cmd", "put " + cFil, .F. )
lOk = WAITRUN( "psftp " + cSrv + " -P " + cPrt + " -l " + cUsr + " -pw " + cPsw + " -b psftp.cmd", SW_HIDE ) = 0
FERASE( "psftp.cmd" )
RETURN lOk
Antonio Linares wrote:Now we need a SFTP server...
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: Google [Bot] and 119 guests