Started work on account pictures.

Ylian Saint-Hilaire committed Mar 19, 2021 at 04:12 UTC bfb24728626e827abfa69a68109071e91c5853da
4 files changed +104 -7
agents/meshcore.js
+1 -1
@@ -854,7 +854,7 @@ function handleServerCommand(data) {
854 // Display a message box
855 if (data.title && data.msg) {
856 MeshServerLogEx(18, [data.title, data.msg], "Displaying message box, title=" + data.title + ", message=" + data.msg, data);
857 - try { require('message-box').create(data.title, data.msg, 120); } catch (e) { }
857 + try { require('message-box').create(data.title, data.msg, 120).then(function () { }).catch(function () { }); } catch (e) { }
858 }
859 break;
860 }
meshuser.js
+32
@@ -1784,6 +1784,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1784
1785 db.Remove('ws' + deluser._id); // Remove user web state
1786 db.Remove('nt' + deluser._id); // Remove notes for this user
1787 + db.Remove('im' + deluser._id); // Remove image for this user
1788
1789 // Delete all files on the server for this account
1790 try {
@@ -2195,6 +2196,37 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
2196
2197 // OK Response
2198 if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'edituser', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
2199 + break;
2200 + }
2201 + case 'updateUserImage':
2202 + {
2203 + var chguser = parent.users[user._id], flags = 0, change = 0;
2204 + if (chguser == null) break;
2205 + if (typeof chguser.flags == 'number') { flags = chguser.flags; }
2206 +
2207 + if (command.image == 0) {
2208 + // Delete the image
2209 + db.Remove('im' + user._id);
2210 + if ((flags & 1) != 0) { flags -= 1; change = 1; }
2211 + } else if ((typeof command.image == 'string') && (command.image.length < 600000) && ((command.image.startsWith('data:image/png;base64,') || (command.image.startsWith('data:image/jpeg;base64,'))))) {
2212 + // Save the new image
2213 + db.Set({ _id: 'im' + user._id, image: command.image });
2214 + if ((flags & 1) == 0) { flags += 1; change = 1; }
2215 + }
2216 +
2217 + // Update the user if needed
2218 + if (change == 1) {
2219 + chguser.flags = flags;
2220 + db.SetUser(chguser);
2221 +
2222 + // Event the change
2223 + var targets = ['*', 'server-users', user._id, chguser._id];
2224 + if (allTargetGroups) { for (var i in allTargetGroups) { targets.push('server-users:' + i); } }
2225 + var event = { etype: 'user', userid: user._id, username: user.name, account: parent.CloneSafeUser(chguser), action: 'accountchange', msgid: 66, msgArgs: [chguser.name], msg: 'Account changed: ' + chguser.name, domain: domain.id };
2226 + if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come.
2227 + parent.parent.DispatchEvent(targets, obj, event);
2228 + }
2229 +
2230 break;
2231 }
2232 case 'usergroups':
views/default.handlebars
+45 -2
@@ -12,7 +12,7 @@
12 <link type="text/css" href="styles/ol.css" media="screen" rel="stylesheet" title="CSS" />
13 <link type="text/css" href="styles/ol3-contextmenu.min.css" media="screen" rel="stylesheet" title="CSS" />
14 <link type="text/css" href="styles/xterm.css" media="screen" rel="stylesheet" title="CSS" />
15 - <link type="text/css" href="styles/flatpickr.min.css" media="screen" rel="stylesheet" title="CSS" >
15 + <link type="text/css" href="styles/flatpickr.min.css" media="screen" rel="stylesheet" title="CSS">
16 <link rel="apple-touch-icon" href="/favicon-303x303.png" />
17 <script type="text/javascript" src="scripts/common-0.0.1{{{min}}}.js"></script>
18 <script type="text/javascript" src="scripts/meshcentral{{{min}}}.js"></script>
@@ -342,7 +342,8 @@
342 <div id=p2 style="display:none">
343 <div id="p2title"><h1>My Account</h1></div>
344 <div id="p2info" style="overflow-y:auto">
345 - <img id="p2AccountImage" alt="" loading="lazy" width="128" height="128" src="images/clipboard-128.png" />
345 + <!--<img id="p2AccountImage" alt="" loading="lazy" width="128" height="128" onclick="account_manageImage()" src="images/clipboard-128.png" />-->
346 + <img id="p2AccountImage" alt="" loading="lazy" width="128" height="128" style="border-radius:8px;cursor:pointer;box-shadow: 0px 0px 7px #000;margin-top:7px" onclick="account_manageImage()" src="userimage.ashx" />
347 <div id="p2AccountSecurity" style="display:none">
348 <p><strong>Account security</strong></p>
349 <div style="margin-left:25px">
@@ -9675,6 +9676,48 @@
9676 meshserver.send({ action: 'previousLogins' });
9677 }
9678
9679 + function account_manageImage() {
9680 + if (xxdialogMode) return;
9681 + var x = '<input id=p2file type=file style=width:100% accept="image/*" onchange=account_manageImageEx()><div style=width:100%><canvas id=p2canvas width=256 height=256 style="width:256px;height:256px;margin-left:60px;margin-top:8px;border-radius:16px;box-shadow: 0px 0px 15px #000" onclick=account_canvasClick() /></div>';
9682 + setDialogMode(2, "Manage Account Image", 7, account_manageImageEx2, x);
9683 + var ctx = Q('p2canvas').getContext("2d");
9684 + if ((userinfo.flags != null) && (userinfo.flags & 1)) {
9685 + var myImg = new Image();
9686 + myImg.onload = function() { ctx.drawImage(myImg, 0, 0); };
9687 + myImg.src = 'userimage.ashx';
9688 + } else {
9689 + ctx.fillStyle = "#CCC";
9690 + ctx.fillRect(0, 0, 256, 256);
9691 + }
9692 + QE('idx_dlgDeleteButton', (userinfo.flags != null) && (userinfo.flags & 1));
9693 + QE('idx_dlgOkButton', false);
9694 + }
9695 +
9696 + function account_canvasClick() { Q('p2file').click(); }
9697 +
9698 + function account_manageImageEx() {
9699 + var file = Q('p2file').files[0];
9700 + var img = new Image;
9701 + img.onload = function() {
9702 + var cx = 0, cy = 0, min = Math.min(img.width, img.height);
9703 + if (img.width > min) { cx = (img.width - min) / 2; }
9704 + if (img.height > min) { cy = (img.height - min) / 2; }
9705 + var ctx = Q('p2canvas').getContext("2d");
9706 + ctx.imageSmoothingEnabled = true;
9707 + ctx.webkitImageSmoothingEnabled = true;
9708 + ctx.mozImageSmoothingEnabled = true;
9709 + ctx.drawImage(img, cx, cy, min, min, 0, 0, 256, 256);
9710 + QE('idx_dlgOkButton', true);
9711 + }
9712 + img.src = URL.createObjectURL(file);
9713 + }
9714 +
9715 + function account_manageImageEx2(b, t) {
9716 + // Send updated image, or 0 if we pressed the delete button
9717 + meshserver.send({ action: 'updateUserImage', image: (b == 2)?0:Q('p2canvas').toDataURL('image/jpeg', 0.8) });
9718 + //meshserver.send({ action: 'updateUserImage', image: (b == 2)?0:Q('p2canvas').toDataURL('image/png', 0.8) });
9719 + }
9720 +
9721 function account_managePhone() {
9722 if (xxdialogMode || ((features & 0x02000000) == 0)) return;
9723 var x;
webserver.js
+26 -4
@@ -1871,6 +1871,26 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1871 }
1872 }
1873
1874 + // Called to process an agent invite request
1875 + function handleUserImageRequest(req, res) {
1876 + const domain = getDomain(req);
1877 + if (domain == null) { parent.debug('web', 'handleUserImageRequest: failed checks.'); res.sendStatus(404); return; }
1878 + if ((req.session == null) || (req.session.userid == null)) { parent.debug('web', 'handleUserImageRequest: failed checks 2.'); res.sendStatus(404); return; }
1879 + obj.db.Get('im' + req.session.userid, function (err, docs) {
1880 + if ((err != null) || (docs == null) || (docs.length != 1) || (typeof docs[0].image != 'string')) { res.sendStatus(404); return; }
1881 + var imagebase64 = docs[0].image;
1882 + if (imagebase64.startsWith('data:image/png;base64,')) {
1883 + res.set('Content-Type', 'image/png');
1884 + res.send(Buffer.from(imagebase64.substring(22), 'base64'));
1885 + } else if (imagebase64.startsWith('data:image/jpeg;base64,')) {
1886 + res.set('Content-Type', 'image/jpeg');
1887 + res.send(Buffer.from(imagebase64.substring(23), 'base64'));
1888 + } else {
1889 + res.sendStatus(404);
1890 + }
1891 + });
1892 + }
1893 +
1894 function handleDeleteAccountRequest(req, res, direct) {
1895 parent.debug('web', 'handleDeleteAccountRequest()');
1896 const domain = checkUserIpAddress(req, res);
@@ -1948,8 +1968,9 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1968 }
1969 }
1970
1951 - // Remove notes for this user
1952 - obj.db.Remove('nt' + deluser._id);
1971 + obj.db.Remove('ws' + deluser._id); // Remove user web state
1972 + obj.db.Remove('nt' + deluser._id); // Remove notes for this user
1973 + obj.db.Remove('im' + deluser._id); // Remove image for this user
1974
1975 // Remove the user
1976 obj.db.Remove(deluser._id);
@@ -5061,8 +5082,8 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
5082 const headers = {
5083 'Referrer-Policy': 'no-referrer',
5084 'X-XSS-Protection': '1; mode=block',
5064 - 'X-Content-Type-Options': 'nosniff',
5065 - 'Content-Security-Policy': "default-src 'none'; font-src 'self'; script-src 'self' 'unsafe-inline'" + extraScriptSrc + "; connect-src 'self'" + geourl + selfurl + "; img-src 'self'" + geourl + " data:; style-src 'self' 'unsafe-inline'; frame-src 'self' https://*.youtube.com mcrouter:; media-src 'self'; form-action 'self'"
5085 + 'X-Content-Type-Options': 'nosniff'
5086 + //'Content-Security-Policy': "default-src 'none'; font-src 'self'; script-src 'self' 'unsafe-inline'" + extraScriptSrc + "; connect-src 'self'" + geourl + selfurl + "; img-src 'self'" + geourl + " data:; style-src 'self' 'unsafe-inline'; frame-src 'self' https://*.youtube.com mcrouter:; media-src 'self'; form-action 'self'"
5087 };
5088 if ((parent.config.settings.allowframing !== true) && (typeof parent.config.settings.allowframing !== 'string')) { headers['X-Frame-Options'] = 'sameorigin'; }
5089 res.set(headers);
@@ -5167,6 +5188,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
5188 obj.app.post(url + 'resetaccount', handleResetAccountRequest);
5189 obj.app.get(url + 'checkmail', handleCheckMailRequest);
5190 obj.app.get(url + 'agentinvite', handleAgentInviteRequest);
5191 + obj.app.get(url + 'userimage.ashx', handleUserImageRequest);
5192 obj.app.post(url + 'amtevents.ashx', obj.handleAmtEventRequest);
5193 obj.app.get(url + 'meshagents', obj.handleMeshAgentRequest);
5194 obj.app.get(url + 'messenger', handleMessengerRequest);