Files
Facilitor/UTILS/PutOrders/puo_connector.js
Jos Groot Lipman b746ba4de7 FCLT#84701 Standaardkoppelingen via interface te beheren en instellen savepoint
svn path=/Website/trunk/; revision=70469
2025-09-30 13:44:20 +00:00

304 lines
14 KiB
JavaScript
Raw Blame History

// ******************************************
// * $Id$
// *
// * connect()
// * disconnect()
// *
// * Maak een verbinding voor File, FTP, SFTP, HTTP, HTTPS of Mail.
// * Zet hierbij ook de variabelen:
// * Connected
// * ConnectionType
// * Hostname
// * Username
// * Password
// * Port
// * Proxy
// * Subfolder
// * isFile
// * isFTP
// * isSFTP
// * isHTTP
// * isHTTPS
// * isMail
// *
// * Uses: scr="./puo_shared.js"
// * scr="./puo_settings.js"
// * scr="./CrackURL.js"
// * scr="../wsf_shared.js"
// *
// ******************************************
// puo_connector.js
//
function ConnectorCls()
{
// Private variabelen
var CONNECTION_NONE = 0;
var CONNECTION_FILE = 1;
var CONNECTION_FTP = 2;
var CONNECTION_HTTP = 3;
var CONNECTION_HTTPS = 4;
var CONNECTION_MAIL = 5;
var CONNECTION_SFTP = 6;
// Public variabelen
this.Connected = false;
this.isFile = false;
this.isFTP = false;
this.isSFTP = false;
this.isHTTP = false;
this.isHTTPS = false;
this.isMail = false;
this.ConnectionType = CONNECTION_NONE;
this.CurrentAddress = "";
// Public functies
this.connect = _connect;
this.disconnect = _disconnect;
function _connect(p_bedrijfadres) //BEDRIJF_ORDER_ADRES
{
// voorbeeld url's:
// file: url = "d:\\websites\\default web site\\facilitor5iwork\\cust\\xxxx\\flexfiles\\" DEPRECATED!
// ftp: url = "ftp://example.com/folder/"
// mail: url = "mailto:helpdesk@facilitor.nl"
// http: url = "http://sgf12/facilitor5iwork/appl/exp/exp_soap.asp"
// Let op: username/password moet je
this.url = p_bedrijfadres.url;
try
{
this.disconnect();
Log2File(1, "Connecting to " + this.url);
if (S("puo_forceorderaddress"))
{
if (p_bedrijfadres.orgurl && p_bedrijfadres.orgurl.substring(0, 1) == '@')
{
Log2File(1, "(Internal bedrijfadres {0} detected. It is NOT overruled.)".format(p_bedrijfadres.orgurl));
}
else
{
this.url = S("puo_forceorderaddress");
Log2File(1, "effective: " + this.url);
}
}
var URLAdres = this.url;
var URLParts = parseUrl(this.url);
if (URLParts.user && URLParts.password) // Dit hoop ik met FSN#34131 weer te kunnen verwijderen
{
p_bedrijfadres.username = URLParts.user;
p_bedrijfadres.password = URLParts.password;
URLAdres = URLAdres.replace(URLParts.user + ":" + URLParts.password + "@", ""); // strip die er uit
}
//
if (!URLAdres)
{
this.ConnectionType = CONNECTION_NONE;
}
else
{
if (!URLParts.protocol && this.url.indexOf("@") > 0)
{
this.url = "mailto:" + this.url;
URLParts.protocol = "mailto";
Log2File(1, "Configuration error: missing protocol in url. Assuming mailto:");
}
switch (URLParts.protocol.toLowerCase())
{
case "mailto":
{
this.ConnectionType = CONNECTION_MAIL;
// Let op dat mailto:jan@example.com;klaas@example.com goed blijft gaan!
this.mailto = this.url.split(":")[1]; // alles achter de mailto
//
Log2File(2, "protocol: mailto");
this.Connected = true;
if (S("puo_lcl_friendly_mailto"))
this.friendlyurl = S("puo_lcl_friendly_mailto").format(this.mailto);
break;
}
case "http":
{
this.ConnectionType = CONNECTION_HTTP;
//
Log2File(2, "protocol: http");
this.Connected = true;
if (S("puo_lcl_friendly_http"))
this.friendlyurl = S("puo_lcl_friendly_http").format(URLParts.protocol + "://" + URLParts.host);
break;
}
case "https":
{
this.ConnectionType = CONNECTION_HTTPS;
//
Log2File(2, "protocol: https");
this.Connected = true;
if (S("puo_lcl_friendly_http"))
this.friendlyurl = S("puo_lcl_friendly_http").format(URLParts.protocol + "://" + URLParts.host);
break;
}
case "ftp": // deprecated voor SaaS maar RWSN#63637 gebruikt het nog
{ // voorbeeld: "ftp://example.com/folder/"
this.ConnectionType = CONNECTION_FTP;
Log2File(2, "Creating ftp connector");
var about = new ActiveXObject("SLNKDWF.About");
Log2File(2, "Ge<47>nstalleerde SLNKDWF Versie: " + about.VersionString);
Log2File(2, about.All);
//
this.ftpconnection = new ActiveXObject("SLNKDWF.FTP");
this.ftpconnection.Hostname = URLParts.host;
this.ftpconnection.Username = p_bedrijfadres.username;
this.ftpconnection.Password = p_bedrijfadres.password;
this.ftpconnection.Port = URLParts.port || 21;
this.ftpconnection.Proxyname = (S("puo_useproxy") ? S("puo_proxyserveripaddress") : "");
this.Subfolder = URLParts.directory;
this.ftpconnection.Flags = S("puo_connectionflag")||0;
//
Log2File(2, "protocol: ftp");
Log2File(2, "FTPServer: " + this.ftpconnection.Hostname);
Log2File(2, "FTPPort: " + this.ftpconnection.Port);
Log2File(2, "FTPProxy: " + this.ftpconnection.Proxyname);
Log2File(2, "FTPDir: " + this.Subfolder);
Log2File(2, "Flags: " + this.ftpconnection.Flags);
//
if (S("puo_lcl_friendly_ftp"))
this.friendlyurl = S("puo_lcl_friendly_ftp").format(URLParts.host, URLParts.directory, URLParts.port);
try
{
// Probeer nu de FTP verbinding tot stand te brengen.
this.ftpconnection.Open();
// Change FTP dir
if (URLParts.directory)
{
try
{
this.ftpconnection.SetCurrentDir(URLParts.directory);
this.Connected = true;
}
catch(err)
{
Log2File(0, "Error in changing directory to: " + URLParts.directory);
}
}
else
{
this.Connected = true;
}
}
catch(err)
{
Log2File(0, "Error in InternetOpen: " + err.message);
}
break;
}
case "sftp":
{ // voorbeeld: "sftp://example.com/folder/"
this.ConnectionType = CONNECTION_SFTP;
Log2File(2, "Creating sftp connector");
var glob = new ActiveXObject("Chilkat_9_5_0.Global")
var success = glob.UnlockBundle(S("puo_chilkat_secret"));
if (success != 1)
throw { description: glob.LastErrorText };
this.sftpconnection = new ActiveXObject("Chilkat_9_5_0.SFtp");
// Set some timeouts, in milliseconds:
this.sftpconnection.ConnectTimeoutMs = 5000;
this.sftpconnection.IdleTimeoutMs = 10000;
Log2File(2, "protocol: sftp");
if (S("puo_lcl_friendly_sftp"))
this.friendlyurl = S("puo_lcl_friendly_sftp").format(URLParts.host, URLParts.port, URLParts.directory);
try
{
// Probeer nu de SFTP verbinding tot stand te brengen.
success = this.sftpconnection.Connect(URLParts.host, URLParts.port || 22);
if (success != 1)
throw { description: this.sftpconnection.LastErrorText };
success = this.sftpconnection.AuthenticatePw(p_bedrijfadres.username, p_bedrijfadres.password);
if (success != 1)
throw { description: this.sftpconnection.LastErrorText };
success = this.sftpconnection.InitializeSftp();
if (success != 1)
throw { description: this.sftpconnection.LastErrorText };
// Change FTP dir
this.directory = URLParts.directory || "";
this.Connected = true;
Log2File(0, "Connected to: " + this.sftpconnection.ServerIdentifier);
}
catch(err)
{
Log2File(0, "Error in SFTP Open: " + (err.message || err.description));
}
break;
}
case "file":
{ // voorbeeld: "file:c:\\folder\\"
this.ConnectionType = CONNECTION_FILE;
Log2File(2, "Creating file connector");
URLAdres = this.url.substr(this.url.indexOf(":")+1); // Alles achter file:
if (!URLAdres.match(/\\\\/))
{
URLAdres = URLAdres + "\\"; // aan het eind moet een "\" staan
}
var fso = new ActiveXObject("Scripting.FileSystemObject");
if (fso.FolderExists(URLAdres))
{
this.Connected = true;
if (S("puo_lcl_friendly_file"))
this.friendlyurl = S("puo_lcl_friendly_file").format(URLAdres);
}
else
{
Log2File(0, "Error: directory " + URLAdres + " does not exist");
}
break;
}
default:
{
this.ConnectionType = CONNECTION_NONE;
Log2File(2, "protocol: none");
}
} // switch
}
}
catch(err)
{
Log2File(0, "Error connecting to " + this.url + ": " + err.message);
}
finally
{
if (this.Connected)
{
this.CurrentAddress = URLAdres;
this.isFile = (this.ConnectionType == CONNECTION_FILE);
this.isFTP = (this.ConnectionType == CONNECTION_FTP);
this.isSFTP = (this.ConnectionType == CONNECTION_SFTP);
this.isHTTP = (this.ConnectionType == CONNECTION_HTTP);
this.isHTTPS = (this.ConnectionType == CONNECTION_HTTPS);
this.isMail = (this.ConnectionType == CONNECTION_MAIL);
}
}
}
function _disconnect()
{
if (this.ftpconnection)
{
try
{
Log2File(2, "Disconnect");
this.ftpconnection.Close(); // Let op: effectief sluit deze alleen de SLNKDWF.FTP verbinding.
this.Connected = false;
}
catch(err)
{
Log2File(0, "Error during disconnection: " + err.message);
}
}
}
}