diff --git a/package.json b/package.json index d42a81a5..fd89542c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "meshcentral", - "version": "0.4.5-h", + "version": "0.4.5-i", "keywords": [ "Remote Management", "Intel AMT", diff --git a/public/translator.htm b/public/translator.htm index cbb85239..31e44af2 100644 --- a/public/translator.htm +++ b/public/translator.htm @@ -8,6 +8,7 @@ + @@ -30,13 +40,18 @@
-
+
+
+
MeshCentral Translator
+
+
- - + + + + + Show Location
-
+
-
+
- - + +
@@ -111,13 +128,20 @@ document.ondragleave = ondragleave; document.onkeypress = onkeypress; updateMasterList(); + loadServerTranslations(); } - function langSelectorChange() { selectedLanguage = Q('langSelector').value; updateMasterList(); } + function langSelectorChange() { + selectedLanguage = Q('langSelector').value; + updateMasterList(); + onSearchChanged(true); + } function cleanup() { translationFile = null; updateMasterList(); + Q('searchInput').value = ''; + currentSearchFilter = ''; } function ondrop(e) { @@ -190,7 +214,10 @@ if ((file != null) && (typeof file.strings == 'object') && (file.strings.length > 0)) { translations = file.strings; translations.sort(enSort); + Q('searchInput').value = ''; + currentSearchFilter = ''; updateMasterList(); + select(0, true); } }; fr.readAsText(translationFile); @@ -205,23 +232,37 @@ var target = ''; if (translations[i][selectedLanguage] != null) { target = EscapeHtml(translations[i][selectedLanguage]); } var comment = ''; - x.push('
' + source + '
' + target + '
' + comment + '
'); + // + x.push('
' + source + '
' + target + '
' + comment + '
'); } } QH('masterListArea', x.join('')); updateButtons(); } - function select(i) { + function select(i, scroll) { + Q('nx' + selectedItem).classList.remove('listItemSel'); + Q('nx' + selectedItem).classList.add('listItem'); selectedItem = i; - QH('defaultTextArea', translations[i].en); - if (translations[i][selectedLanguage] != null) { QH('translatedTextArea', translations[i][selectedLanguage]); } else { QH('translatedTextArea', ''); } + Q('nx' + selectedItem).classList.remove('listItem'); + Q('nx' + selectedItem).classList.add('listItemSel'); + if (scroll === true) { + var x = (Q('masterListArea').clientHeight / 2) - (Q('nx' + selectedItem).clientHeight / 2); + if (x < 0) { x = 0; } + Q('masterListArea').scrollTop = Q('nx' + selectedItem).offsetTop - x; + } + onLocChanged(); + if (translations[i][selectedLanguage] != null) { + QH('translatedTextArea', translations[selectedItem][selectedLanguage]); + } else { + QH('translatedTextArea', ''); + } Q('translatedTextArea').focus(); updateButtons(); } - function next() { select(selectedItem + 1); } - function prev() { select(selectedItem - 1); } + function next() { select(selectedItem + 1, true); } + function prev() { select(selectedItem - 1, true); } function copySource() { QH('translatedTextArea', translations[selectedItem].en); Q('translatedTextArea').focus(); } function updateButtons() { @@ -240,6 +281,82 @@ function enSort(a, b) { if (a.en.toLowerCase() > b.en.toLowerCase()) return 1; if (a.en.toLowerCase() < b.en.toLowerCase()) return -1; return 0; } + var currentSearchFilter = ''; + function onSearchChanged(force) { + if ((force != true) && (currentSearchFilter == Q('searchInput').value)) return; + currentSearchFilter = Q('searchInput').value; + if (translations != null) { + for (var i in translations) { + if (currentSearchFilter == '') { + QV('nx' + i, true); + } else { + QV('nx' + i, ((translations[i][selectedLanguage] != null) && (translations[i][selectedLanguage].indexOf(currentSearchFilter) >= 0)) || (translations[i]['en'].indexOf(currentSearchFilter) >= 0)); + } + } + } + } + + function onLocChanged() { + if (Q('showLocCheck').checked) { + if ((translations[selectedItem].xloc != null) && (translations[selectedItem].xloc.length > 0)) { + if (translations[selectedItem].xloc.length == 1) { + QH('defaultTextArea', translations[selectedItem]['en'] + '\r\n\r\nLocation:\r\n' + translations[selectedItem].xloc.join('\r\n')); + } else { + QH('defaultTextArea', translations[selectedItem]['en'] + '\r\n\r\nLocations:\r\n' + translations[selectedItem].xloc.join('\r\n')); + } + } else { + QH('defaultTextArea', translations[selectedItem]['en'] + '\r\n\r\nNo locations.'); + } + } else { + QH('defaultTextArea', translations[selectedItem]['en']); + } + } + + // Poll the server, if it responds, refresh the page. + function loadServerTranslations() { + var xdr = null; + try { xdr = new XDomainRequest(); } catch (e) { } + if (!xdr) xdr = new XMLHttpRequest(); + xdr.open('POST', window.location.origin + '/translations'); + xdr.timeout = 30000; + xdr.onload = function () { + var x = null; + try { x = JSON.parse(this.responseText); } catch (ex) { } + if ((x == null) || (typeof x.strings != 'object')) { messagebox('Translations', 'ERROR: Unable to parse translations from the server.'); return; } + translations = x.strings; + translations.sort(enSort); + Q('searchInput').value = ''; + currentSearchFilter = ''; + updateMasterList(); + select(0, true); + }; + xdr.onerror = function () { + messagebox('Translations', 'ERROR: Unable to load translations from the server.'); + }; + xdr.send('{ "action":"getTranslations" }'); + } + + // Save to server + function saveServerTranslations() { + var xdr = null; + try { xdr = new XDomainRequest(); } catch (e) { } + if (!xdr) xdr = new XMLHttpRequest(); + xdr.open('POST', window.location.origin + '/translations'); + xdr.timeout = 30000; + xdr.onload = function () { + var x = null; + try { x = JSON.parse(this.responseText); } catch (ex) { } + if ((x == null) || (typeof x.strings != 'object')) { messagebox('Translations', 'ERROR: Unable to parse server response.'); return; } + // x + }; + xdr.onerror = function () { messagebox('Translations', 'ERROR: Unable to save translations to server.'); }; + xdr.send(JSON.stringify({ 'action': 'setTranslations', strings: translations })); + } + + function saveToFile() { + saveAs(data2blob(JSON.stringify({ strings: translations })), 'translate.json'); + } + // // POPUP DIALOG // @@ -276,8 +393,8 @@ if (((b & 8) || x) && f) f(x, t); } - function messagebox(t, m) { setSessionActivity(); QH('id_dialogMessage', m); setDialogMode(1, t, 1); } - function statusbox(t, m) { setSessionActivity(); QH('id_dialogMessage', m); setDialogMode(1, t); } + function messagebox(t, m) { QH('id_dialogMessage', m); setDialogMode(1, t, 1); } + function statusbox(t, m) { QH('id_dialogMessage', m); setDialogMode(1, t); } function haltEvent(e) { if (e.preventDefault) e.preventDefault(); if (e.stopPropagation) e.stopPropagation(); return false; } function pad2(num) { var s = '00' + num; return s.substr(s.length - 2); } function format(format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function (match, number) { return typeof args[number] != 'undefined' ? args[number] : match; }); }; diff --git a/webserver.js b/webserver.js index c0673b2b..59c27c2d 100644 --- a/webserver.js +++ b/webserver.js @@ -1937,6 +1937,50 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) { } } + // Handle translation request + function handleTranslationsRequest(req, res) { + const domain = checkUserIpAddress(req, res); + //if ((domain.loginkey != null) && (domain.loginkey.indexOf(req.query.key) == -1)) { res.sendStatus(404); return; } // Check 3FA URL key + if ((obj.userAllowedIp != null) && (checkIpAddressEx(req, res, obj.userAllowedIp, false) === false)) { return; } // Check server-wide IP filter only. + + var user = null; + if (obj.args.user != null) { + // A default user is active + user = obj.users['user/' + domain.id + '/' + obj.args.user]; + if (!user) { parent.debug('web', 'handleTranslationsRequest: user not found.'); res.sendStatus(401); return; } + } else { + // Check if the user is logged and we have all required parameters + if (!req.session || !req.session.userid) { parent.debug('web', 'handleTranslationsRequest: failed checks (2).'); res.sendStatus(401); return; } + + // Get the current user + user = obj.users[req.session.userid]; + if (!user) { parent.debug('web', 'handleTranslationsRequest: user not found.'); res.sendStatus(401); return; } + if (user.siteadmin != 0xFFFFFFFF) { parent.debug('web', 'handleTranslationsRequest: user not site administrator.'); res.sendStatus(401); return; } + } + + var data = ''; + req.setEncoding('utf8'); + req.on('data', function (chunk) { data += chunk; }); + req.on('end', function () { + try { data = JSON.parse(data); } catch (ex) { data = null; } + if (data == null) { res.sendStatus(404); return; } + if (data.action == 'getTranslations') { + if (obj.fs.existsSync(obj.path.join(obj.parent.datapath, 'translate.json'))) { + // Return the translation file (JSON) + try { res.sendFile(obj.path.join(obj.parent.datapath, 'translate.json')); } catch (ex) { res.sendStatus(404); } + } else if (obj.fs.existsSync(obj.path.join(__dirname, 'translate', 'translate.json'))) { + // Return the default translation file (JSON) + try { res.sendFile(obj.path.join(__dirname, 'translate', 'translate.json')); } catch (ex) { res.sendStatus(404); } + } else { res.sendStatus(404); } + } else if (data.action == 'setTranslations') { + obj.fs.writeFile(obj.path.join(obj.parent.datapath, 'translate.json'), JSON.stringify({ strings: data.strings } ), function (err) { res.send(JSON.stringify({ response:err })); }); + } else { + // Unknown request + res.sendStatus(404); + } + }); + } + // Handle welcome image request function handleWelcomeImageRequest(req, res) { const domain = checkUserIpAddress(req, res); @@ -3395,6 +3439,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) { obj.app.ws(url + 'webider.ashx', function (ws, req) { PerformWSSessionAuth(ws, req, false, function (ws1, req1, domain, user, cookie) { obj.meshIderHandler.CreateAmtIderSession(obj, obj.db, ws1, req1, obj.args, domain, user); }); }); obj.app.ws(url + 'control.ashx', function (ws, req) { PerformWSSessionAuth(ws, req, false, function (ws1, req1, domain, user, cookie) { obj.meshUserHandler.CreateMeshUser(obj, obj.db, ws1, req1, obj.args, domain, user); }); }); obj.app.get(url + 'logo.png', handleLogoRequest); + obj.app.post(url + 'translations', handleTranslationsRequest); obj.app.get(url + 'welcome.jpg', handleWelcomeImageRequest); obj.app.ws(url + 'amtactivate', handleAmtActivateWebSocket); if (parent.pluginHandler != null) {