2017-02-17 16:41:39 +01:00
|
|
|
/* μlogger
|
|
|
|
*
|
|
|
|
* Copyright(C) 2017 Bartek Fabiszewski (www.fabiszewski.net)
|
|
|
|
*
|
|
|
|
* This is free software; you can redistribute it and/or modify it under
|
2017-04-07 00:05:28 +02:00
|
|
|
* the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2017-02-17 16:41:39 +01:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
2017-04-07 00:05:28 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2017-02-17 16:41:39 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
function addUser() {
|
2017-04-12 20:16:39 +02:00
|
|
|
var form = '<form id="userForm" method="post" onsubmit="submitUser(\'add\'); return false">';
|
2017-04-07 18:37:17 +02:00
|
|
|
form += '<label><b>' + lang['username'] + '</b></label><input type="text" placeholder="' + lang['usernameenter'] + '" name="login" required>';
|
|
|
|
form += '<label><b>' + lang['password'] + '</b></label><input type="password" placeholder="' + lang['passwordenter'] + '" name="pass" required>';
|
|
|
|
form += '<label><b>' + lang['passwordrepeat'] + '</b></label><input type="password" placeholder="' + lang['passwordenter'] + '" name="pass2" required>';
|
|
|
|
form += '<div class="buttons"><button type="button" onclick="removeModal()">' + lang['cancel'] + '</button><button type="submit">' + lang['submit'] + '</button></div>';
|
2017-02-17 16:41:39 +01:00
|
|
|
form += '</form>';
|
|
|
|
showModal(form);
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:16:39 +02:00
|
|
|
function editUser() {
|
|
|
|
var userForm = document.getElementsByName('user')[0];
|
2017-08-07 18:29:13 +02:00
|
|
|
var userLogin = (userForm !== undefined) ? userForm.options[userForm.selectedIndex].text : auth;
|
2017-04-12 20:16:39 +02:00
|
|
|
if (userLogin == auth) {
|
2017-04-12 22:42:23 +02:00
|
|
|
alert(lang['selfeditwarn']);
|
2017-04-12 20:16:39 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-04-14 16:00:53 +02:00
|
|
|
var message = '<div style="float:left">' + sprintf(lang['editinguser'], '<b>' + htmlEncode(userLogin) + '</b>') + '</div>';
|
2017-04-12 22:42:23 +02:00
|
|
|
message += '<div class="red-button"><b><a href="javascript:void(0);" onclick="submitUser(\'delete\'); return false">' + lang['deluser'] + '</a></b></div>';
|
2017-04-12 20:16:39 +02:00
|
|
|
message += '<div style="clear: both; padding-bottom: 1em;"></div>';
|
|
|
|
|
|
|
|
var form = '<form id="userForm" method="post" onsubmit="submitUser(\'update\'); return false">';
|
2017-04-14 16:00:53 +02:00
|
|
|
form += '<input type="hidden" name="login" value="' + htmlEncode(userLogin) + '">';
|
2017-04-12 20:16:39 +02:00
|
|
|
form += '<label><b>' + lang['password'] + '</b></label><input type="password" placeholder="' + lang['passwordenter'] + '" name="pass" required>';
|
|
|
|
form += '<label><b>' + lang['passwordrepeat'] + '</b></label><input type="password" placeholder="' + lang['passwordenter'] + '" name="pass2" required>';
|
|
|
|
form += '<div class="buttons"><button type="button" onclick="removeModal()">' + lang['cancel'] + '</button><button type="submit">' + lang['submit'] + '</button></div>';
|
|
|
|
form += '</form>';
|
|
|
|
showModal(message + form);
|
|
|
|
}
|
|
|
|
|
|
|
|
function confirmedDelete(login) {
|
2017-04-14 17:24:09 +02:00
|
|
|
return confirm(sprintf(lang['userdelwarn'], '"' + login + '"'));
|
2017-04-12 20:16:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function submitUser(action) {
|
2017-02-17 16:41:39 +01:00
|
|
|
var form = document.getElementById('userForm');
|
2017-04-14 16:00:53 +02:00
|
|
|
var login = form.elements['login'].value.trim();
|
2017-04-12 20:16:39 +02:00
|
|
|
if (!login) {
|
2017-04-12 22:42:23 +02:00
|
|
|
alert(lang['allrequired']);
|
2017-04-12 20:16:39 +02:00
|
|
|
return;
|
2017-02-17 16:41:39 +01:00
|
|
|
}
|
2017-04-12 20:16:39 +02:00
|
|
|
var pass = null;
|
|
|
|
var pass2 = null;
|
|
|
|
if (action != 'delete') {
|
|
|
|
pass = form.elements['pass'].value;
|
|
|
|
pass2 = form.elements['pass2'].value;
|
|
|
|
if (!pass || !pass2) {
|
2017-04-12 22:42:23 +02:00
|
|
|
alert(lang['allrequired']);
|
2017-04-12 20:16:39 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pass != pass2) {
|
2017-04-12 22:42:23 +02:00
|
|
|
alert(lang['passnotmatch']);
|
2017-04-12 20:16:39 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-04-14 15:55:00 +02:00
|
|
|
if (!pass_regex.test(pass)) {
|
|
|
|
alert(lang['passlenmin'] + '\n' + lang['passrules']);
|
|
|
|
return;
|
|
|
|
}
|
2017-04-12 20:16:39 +02:00
|
|
|
} else {
|
|
|
|
if (!confirmedDelete(login)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-02-17 16:41:39 +01:00
|
|
|
}
|
|
|
|
var xhr = getXHR();
|
2017-04-12 20:16:39 +02:00
|
|
|
xhr.onreadystatechange = function() {
|
2017-05-19 10:59:27 +02:00
|
|
|
if (xhr.readyState == 4) {
|
|
|
|
var error = true;
|
2018-11-09 20:39:28 +01:00
|
|
|
var message = '';
|
2017-05-19 10:59:27 +02:00
|
|
|
if (xhr.status == 200) {
|
|
|
|
var xml = xhr.responseXML;
|
|
|
|
if (xml) {
|
|
|
|
var root = xml.getElementsByTagName('root');
|
|
|
|
if (root.length && getNode(root[0], 'error') == 0) {
|
|
|
|
removeModal();
|
|
|
|
alert(lang['actionsuccess']);
|
|
|
|
if (action == 'delete') {
|
|
|
|
// select current user in users form
|
|
|
|
var f = document.getElementsByName('user')[0];
|
|
|
|
f.remove(f.selectedIndex);
|
|
|
|
selectUser(f);
|
|
|
|
}
|
|
|
|
error = false;
|
|
|
|
} else if (root.length) {
|
|
|
|
errorMsg = getNode(root[0], 'message');
|
|
|
|
if (errorMsg) { message = errorMsg; }
|
2017-04-12 20:16:39 +02:00
|
|
|
}
|
2017-02-17 16:41:39 +01:00
|
|
|
}
|
|
|
|
}
|
2017-05-19 10:59:27 +02:00
|
|
|
if (error) {
|
|
|
|
alert(lang['actionfailure'] + '\n' + message);
|
|
|
|
}
|
2017-02-17 16:41:39 +01:00
|
|
|
xhr = null;
|
|
|
|
}
|
|
|
|
}
|
2017-04-12 20:16:39 +02:00
|
|
|
xhr.open('POST', 'utils/handleuser.php', true);
|
2017-02-17 16:41:39 +01:00
|
|
|
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
|
2017-04-12 20:16:39 +02:00
|
|
|
var params = 'action=' + action + '&login=' + encodeURIComponent(login) + '&pass=' + encodeURIComponent(pass);
|
|
|
|
params = params.replace(/%20/g, '+');
|
|
|
|
xhr.send(params);
|
2017-02-17 16:41:39 +01:00
|
|
|
return;
|
|
|
|
}
|