vn-access/src/main.js

589 lines
14 KiB
JavaScript
Executable File

var Conf = {
appName: 'Verdnatura'
,dsName: 'verdnatura'
,dsPath: 'HKCU\\Software\\ODBC\\ODBC.INI\\verdnatura'
,regPath: 'HKCU\\Software\\Verdnatura\\vn-access'
,remoteUrl: 'https://cdn.verdnatura.es/vn-access'
,dbHost: 'db.verdnatura.es'
,defaultModule: 'tpv'
,defaultLocale: 'es'
,defaultBranch: 'master'
,dbName: 'vn2008'
,maxCorruptSize: 600
,odbcDriver: 'MySQL ODBC 8.0 Unicode Driver'
,driverPath: '\\MySQL\\Connector ODBC 8.0\\myodbc8w.dll'
,version: 3
};
var Locale = {
es: {
"Enter a user name":
"Introduce un nombre de usuario"
,"Enter a password":
"Introduce una contraseña"
,"Server can't be reached":
"No se ha podido conectar con el servidor"
,"Updating":
"Actualizando"
,"Bad login":
"Usuario o contraseña incorrectos"
,"Application it's already open":
"La aplicación ya está abierta"
,"Loading":
"Cargando"
,"Error while updating":
"Error al actualizar"
,"Microsoft Access 2003 is not installed":
"Microsoft Access 2003 no está instalado en el sistema"
,"MDB file not found":
"No se encontró el fichero MDB"
,"Cache files have been deleted":
"Se han borrado todos los ficheros de la caché"
}
};
var App = {
shell: new ActiveXObject('WScript.Shell'),
fso: new ActiveXObject('scripting.filesystemobject'),
init: function() {
var width = 420;
var height = 525;
window.resizeTo(width, height);
window.moveTo((screen.width - width) / 2, (screen.height - height) / 2);
},
onUnload: function() {
this.disableUi(false);
},
onLoad: function() {
// Initializes the global variables
var split = Verdnatura.commandLine.match(/(?:[^\s"]+|"[^"]*")+/g);
if (split.length > 1)
this.module = split[1].replace(/^"+|"+$/g, '');
if (!this.module)
this.module = Conf.defaultModule;
this.appDir = this.getEnv('ProgramFiles') +'\\'+ Conf.appName;
this.moduleDir = this.shell.SpecialFolders('AppData') +'\\'+ Conf.appName;
this.compressFile = this.getEnv('TEMP') +'\\'+ this.module +'.7z';
this.mdbFile = this.moduleDir +'\\'+ this.module +'.mdb';
this.lockFile = this.moduleDir +'\\'+ this.module +'.ldb';
this.certFile = this.appDir +'\\cacert.pem';
// Creates the necessary registry entries
var configured = this.regRead(Conf.regPath, 'configured');
if (!configured || configured < Conf.version) {
var path;
// Creates the Access configuration entries
path = 'HKCU\\Software\\Microsoft\\Office\\11.0\\Access\\Settings';
this.regWrites(path, {
'Confirm Document Deletions' : false,
'Confirm Action Queries' : false,
'Confirm Record Changes' : false
});
path = 'HKCU\\Software\\Microsoft\\Office\\11.0\\Access\\Security';
this.regWrite(path, 'Level', 1);
// Creates the MySQL ODBC connection
var driverPath = this.getEnv('ProgramFiles') + Conf.driverPath;
var params = {
Driver : driverPath,
DESCRIPTION : Conf.appName,
SERVER : Conf.dbHost,
DATABASE : Conf.dbName,
SSLCA : this.certFile,
SSLMODE : 'VERIFY_IDENTITY',
SSLCIPHER : 'AES256-SHA',
AUTO_RECONNECT : '1',
NO_PROMPT : '1',
ENABLE_CLEARTEXT_PLUGIN : '1'
};
this.createOdbc(
Conf.dsName,
Conf.odbcDriver,
params
);
// Marks the application as configured
this.regWrite(Conf.regPath, 'configured', Conf.version);
}
// Loads the form data
var user = this.regRead(Conf.dsPath, 'UID');
var password = this.regRead(Conf.dsPath, 'PWD');
var remember = this.regRead(Conf.regPath, 'remember');
this.$('branch').value = this.getBranch();
this.onChangeBranchChange();
if (user)
this.$('user').value = user;
if (remember && password) {
this.$('password').value = password;
this.$('remember').checked = true;
this.onEnterClick();
} else
this.resetForm(true);
},
resetForm: function(clearPassword) {
if (clearPassword)
this.$('password').value = '';
this.$('user').focus();
this.$('user').select();
},
createOdbc: function(dsName, driverName, params) {
var odbcPath = 'HKCU\\Software\\ODBC\\ODBC.INI\\';
this.regWrites(odbcPath + dsName, params);
this.regWrite(odbcPath + 'ODBC Data Sources', dsName, driverName);
},
disableUi: function(disabled, loadMessage) {
if (disabled)
this.hideMessage();
else
loadMessage = '';
this.$('loading-message').innerHTML = loadMessage;
this.$('user').disabled = disabled;
this.$('password').disabled = disabled;
this.$('remember').disabled = disabled;
this.$('enter').disabled = disabled;
var display = disabled ? 'block' : 'none';
this.$('background').style.display = display;
this.$('spinner').style.display = display;
},
onCleanCacheClick: function() {
setTimeout(function() { App.cleanCache(); });
},
onShowOptionsClick: function() {
var style = this.$("branchSelector").style;
style.display = style.display == 'none' || !style.display
? 'inline'
: 'none';
},
onChangeBranchChange: function() {
this.$("branchButton").className = this.$("branch").value;
this.$("branch").className = this.$("branch").value;
this.$('user').focus();
},
cleanCache: function() {
if (this.fso.folderExists(this.moduleDir)) {
var folder = this.fso.getFolder(this.moduleDir);
var files = new Enumerator(folder.files);
for (; !files.atEnd(); files.moveNext()) {
var file = files.item();
if (/\.mdb$/.test(file.name) && file.name != 'config.mdb')
try {
file.Delete();
} catch (e) {}
}
}
this.showMessage(_('Cache files have been deleted'), 'notice');
},
onKeyPress: function(event) {
switch (event.keyCode) {
case 13: // Enter
this.onEnterPress(event);
break;
case 27: // Esc
window.close();
break;
}
},
onEnterPress: function(event) {
var target = event.target || event.srcElement;
if (target && target.id == 'user' && this.$('password').value == '') {
this.$('password').focus();
return;
}
this.onEnterClick();
},
onEnterClick: function() {
this.disableUi(true, _('Loading'));
setTimeout(function() { App.login(); });
},
login: function() {
try {
var user = this.$('user').value;
var password = this.$('password').value;
var branch = this.$('branch').value;
if (!user || user === '')
throw new Error(_('Enter a user name'));
if (!password || password === '')
throw new Error(_('Enter a password'));
if (!password || password === '')
throw new Error(_('Select a branch'));
this.regWrite(Conf.dsPath, 'UID', user);
this.regWrite(Conf.dsPath, 'PWD', password);
var version = this.fetchVersion();
if (version !== null) {
this.disableUi(true, _('Updating'));
var remoteFile = version
? '.archive/'+ this.module +'/'+ version +'.7z'
: this.module +'.7z?'+ new Date().getTime();
remoteFile = Conf.remoteUrl +'/'+ remoteFile;
var request = new ActiveXObject("MSXML2.XMLHTTP");
request.open('GET', remoteFile, true);
request.onreadystatechange = function() {
App.onRequestReady(request);
};
request.send();
} else
this.openMdb();
} catch (err) {
this.catchError(err);
}
},
/**
* Gets information about the version to download.
* cmdle
* @return {Number|Boolean} Version number, %false if cannot
* fetch or %null if local is up-to-date
*/
fetchVersion: function() {
// Gets the last version number using the MySQL ODBC connection
var mysqlConn = new ActiveXObject('ADODB.Connection');;
try {
mysqlConn.open(this.getOdbcString({
Driver : '{'+ Conf.odbcDriver +'}',
Server : Conf.dbHost,
Database : Conf.dbName,
Uid : this.$('user').value,
Pwd : this.$('password').value,
Sslca : this.certFile,
SslMode : 'VERIFY_IDENTITY',
SslCipher : 'AES256-SHA',
ENABLE_CLEARTEXT_PLUGIN : 1
}));
} catch (err) {
var dbErrors = mysqlConn && mysqlConn.errors;
if (dbErrors && dbErrors.count > 0) {
var dbError = dbErrors.item(0);
switch (dbError.NativeError) {
case 1045: // Access denied
clearPassword = true;
err = new Error(_('Bad login'));
err.name = 'BadLogin';
break;
case 2003: // Can't connect
err = new Error(_('Server can\'t be reached'));
break;
default:
err = new Error(dbError.description);
}
dbErrors.clear();
}
throw err;
}
// revisar var sql = "SELECT version FROM versiones WHERE programa = '"+ this.module +"'";
var sql = "SELECT version " +
" FROM vn.mdbVersion " +
" WHERE app = '"+ this.module +"' " +
" AND branchFk = '" + this.$('branch').value + "'";
// var sql = "SELECT version FROM versiones WHERE programa = '"+ this.module +"'";
var rs = mysqlConn.execute(sql);
var version = rs.EOF ? false : parseInt(rs.fields(0).value);
rs.close();
mysqlConn.close();
if (this.$('previous-version').checked && version > 1)
version -= 1;
// Checks if it's already open
if (this.fso.fileExists(this.lockFile))
try {
this.fso.deleteFile(this.lockFile);
return version;
} catch (e) {
throw new Error(_('Application it\'s already open'));
}
// Checks if MDB exists
if (!this.fso.fileExists(this.mdbFile))
return version;
// If it's abnormaly bigger, maybe is corrupted, so force download
var file = this.fso.getFile(this.mdbFile);
if (file.size > Conf.maxCorruptSize * 1024 * 1024)
return version;
// Obtains the local version number from the MDB file
var localVersion = this.mdbGetValue(
'SELECT Version FROM tblVariables',
'Version', parseInt
);
if (!localVersion) localVersion = false;
// Determines if should download
return !localVersion || version === false || localVersion != version
? version
: null;
},
mdbGetValue: function(query, field, parseFn) {
var value;
try {
if (this.fso.fileExists(this.mdbFile)) {
var mdbConn = new ActiveXObject('ADODB.Connection');
mdbConn.open(this.getOdbcString({
'Provider': 'Microsoft.Jet.OLEDB.4.0',
'Data Source': this.mdbFile
}));
try {
var rs = new ActiveXObject('ADODB.Recordset');
rs.Open(query, mdbConn);
value = rs.EOF ? null : parseFn(rs(field));
rs.close();
} catch (e) {}
mdbConn.close();
}
} catch (e) {}
return value;
},
getOdbcString: function(options) {
var optionsArray = [];
for (var option in options)
optionsArray.push(option +'='+ options[option]);
return optionsArray.join(';');
},
onRequestReady: function(request) {
if (request.readyState !== 4)
return;
try {
if (request.status !== 200)
throw new Error('HTTP: '+ request.statusText);
if (this.fso.fileExists(this.compressFile))
this.fso.deleteFile(this.compressFile);
var stream = new ActiveXObject('ADODB.Stream');
stream.open();
stream.Type = 1; //adTypeBinary
stream.write(request.responseBody);
stream.Position = 0;
stream.saveToFile(this.compressFile, 2);
stream.close();
if (this.fso.fileExists(this.mdbFile))
this.fso.deleteFile(this.mdbFile);
this.run('7za e "'+ this.compressFile +'" -o"'+ this.moduleDir +'"', true);
this.fso.deleteFile(this.compressFile);
} catch (e) {
alert(_('Error while updating') +': '+ e.message);
}
try {
if (!this.fso.fileExists(this.mdbFile))
throw new Error(_('MDB file not found'));
this.openMdb();
} catch (e) {
this.catchError(e);
}
},
openMdb: function() {
var remember = !!this.$('remember').checked;
this.regWrite(Conf.regPath, 'remember', remember);
var programFiles = this.getEnv('ProgramFiles');
var accessBin = programFiles +'\\Microsoft Office\\OFFICE11\\MSACCESS.EXE';
if (!this.fso.fileExists(accessBin))
throw new Error(_('Microsoft Access 2003 is not installed'));
this.shell.exec('"'+ accessBin +'" "'+ this.mdbFile +'"');
window.close();
},
catchError: function(err) {
var clearPassword = err.name == 'BadLogin';
if (!this.$('remember').checked || clearPassword)
this.regWrite(Conf.dsPath, 'PWD', '');
this.disableUi(false);
this.showMessage(err.message, 'error');
this.resetForm(clearPassword);
},
/**
* Displays a non-intrusive message.
*
* @param {String} message Message to display
* @param {String<error|notice>} className Message type
*/
showMessage: function(message, className) {
setTimeout(function() {
App.showMessageAsync(message, className);
});
},
showMessageAsync: function(message, className) {
if (this.messageTimeout)
clearTimeout(this.messageTimeout);
var messageDiv = this.$('message');
messageDiv.className = className;
messageDiv.innerHTML = message;
messageDiv.style.display = 'block';
this.messageTimeout = setTimeout(function() {
App.hideMessage();
}, 10000);
},
onBodyClick: function() {
this.hideMessage();
},
/**
* Hides the last displayed non-intrusive message.
*/
hideMessage: function() {
if (this.messageTimeout) {
this.$('message').style.display = 'none';
clearTimeout(this.messageTimeout);
this.messageTimeout = null;
}
},
/**
* Obtains a DOM element by it's identifier.
*
* @param {String} id The element id
*/
$: function(id) {
return document.getElementById(id);
},
run: function(command, wait) {
if (!wait)
wait = false;
this.shell.run(command, 0, wait);
},
getEnv: function(varName) {
return this.shell.expandEnvironmentStrings('%'+ varName +'%');
},
regRead: function(path, key) {
try {
var value = this.shell.regRead(path +'\\'+ key);
} catch (e) {
var value = null;
}
return value;
},
regWrite: function(path, key, value, type) {
if (!type)
switch (typeof (value))
{
case 'boolean':
type = 'REG_DWORD';
value = value ? 1 : 0;
break;
case 'number':
type = 'REG_DWORD';
break;
default:
type = 'REG_SZ';
}
this.shell.regWrite(path +'\\'+ key, value.toString(), type);
},
regWrites: function(path, values, type) {
for(var key in values)
this.regWrite(path, key, values[key], type);
},
regDelete: function(path) {
try {
this.shell.regDelete(path);
} catch (e) {}
},
/**
* Gets information about the branch config in access
*
* @return {string} Branch name, master if cannot
*/
getBranch: function() {
var branch = this.mdbGetValue(
'SELECT branch FROM tblVariables',
'branch', String
);
return branch || Conf.defaultBranch;
}
};
App.init();
function _(string) {
var translation = Locale[Conf.defaultLocale][string];
return translation ? translation : string;
}