Better handle ajax errors

This commit is contained in:
Bartek Fabiszewski 2017-05-19 10:59:27 +02:00
parent 4dd920ae41
commit 52a36c2957
6 changed files with 78 additions and 54 deletions

View File

@ -32,7 +32,7 @@ session_start();
$sid = session_id();
// check for forced login to authorize admin in case of public access
$force_login = (isset($_REQUEST['force_login']) ? $_REQUEST['force_login'] : false);
$force_login = isset($_REQUEST['force_login']) ? $_REQUEST['force_login'] : false;
if ($force_login) {
uConfig::$require_authentication = true;
}
@ -41,10 +41,10 @@ $user = new uUser();
$user->getFromSession();
if (!$user->isValid && (uConfig::$require_authentication || defined('client'))) {
/* authentication */
$login = (isset($_REQUEST['user']) ? $_REQUEST['user'] : NULL);
$pass = (isset($_REQUEST['pass']) ? $_REQUEST['pass'] : NULL);
$ssl = ((!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "" || $_SERVER['HTTPS'] == "off") ? "http" : "https");
$auth_error = (isset($_REQUEST['auth_error']) ? $_REQUEST['auth_error'] : 0);
$login = isset($_REQUEST['user']) ? $_REQUEST['user'] : NULL;
$pass = isset($_REQUEST['pass']) ? $_REQUEST['pass'] : NULL;
$ssl = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "" || $_SERVER['HTTPS'] == "off") ? "http" : "https";
$auth_error = isset($_REQUEST['auth_error']) ? $_REQUEST['auth_error'] : false;
if (!$login) {
// not authenticated and username not submited
@ -87,9 +87,10 @@ if (!$user->isValid && (uConfig::$require_authentication || defined('client')))
<input type="password" name="pass"><br>
<br>
<input type="submit" value="' . $lang["login"] . '">
' . (($force_login == 1) ? "<input type=\"hidden\" name=\"force_login\" value=\"1\">" : "") . '
' . (($force_login) ? '<input type="hidden" name="force_login" value="1">
<div id="cancel"><a href="index.php">' . $lang["cancel"] . '</a></div>' : '') . '
</form>
<div id="error">' . (($auth_error == 1) ? $lang["authfail"] : "") . '</div>
<div id="error">' . (($auth_error) ? $lang["authfail"] : "") . '</div>
</div>
</body>
</html>';
@ -114,6 +115,7 @@ if (!$user->isValid && (uConfig::$require_authentication || defined('client')))
} else {
// unsuccessful
$error = "?auth_error=1";
if ($force_login) { $error .= "&force_login=1"; }
// destroy session
$_SESSION = NULL;
if (isset($_COOKIE[session_name('ulogger')])) {

View File

@ -278,6 +278,10 @@ button {
margin-right: 5px;
}
#cancel {
margin-top: 0.5em;
}
.red-button {
color: white;
float: right;

View File

@ -28,7 +28,7 @@
*/
class uConfig {
// version number
static $version = "0.2";
static $version = "0.3-beta";
// default map drawing framework
static $mapapi = "openlayers";

View File

@ -81,26 +81,32 @@ function submitUser(action) {
}
var xhr = getXHR();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var xml = xhr.responseXML;
if (xhr.readyState == 4) {
var error = true;
var message = "";
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);
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; }
}
return;
}
errorMsg = getNode(root[0], 'message');
if (errorMsg) { message = errorMsg; }
}
alert(lang['actionfailure'] + '\n' + message);
if (error) {
alert(lang['actionfailure'] + '\n' + message);
}
xhr = null;
}
}

View File

@ -46,20 +46,26 @@ function submitPass() {
var xhr = getXHR();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var xml = xhr.responseXML;
if (xhr.readyState == 4) {
var error = true;
var message = "";
if (xml) {
var root = xml.getElementsByTagName('root');
if (root.length && getNode(root[0], 'error') == 0) {
removeModal();
alert(lang["actionsuccess"]);
return;
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"]);
error = false;
} else if (root.length) {
errorMsg = getNode(root[0], 'message');
if (errorMsg) { message = errorMsg; }
}
}
errorMsg = getNode(root[0], 'message');
if (errorMsg) { message = errorMsg; }
}
alert(lang['actionfailure'] + '\n' + message);
if (error) {
alert(lang['actionfailure'] + '\n' + message);
}
xhr = null;
}
}

View File

@ -65,29 +65,35 @@ function submitTrack(action) {
}
var xhr = getXHR();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var xml = xhr.responseXML;
if (xhr.readyState == 4) {
var error = true;
var message = "";
if (xml) {
var root = xml.getElementsByTagName('root');
if (root.length && getNode(root[0], 'error') == 0) {
removeModal();
alert(lang['actionsuccess']);
var f = document.getElementsByName('track')[0];
if (action == 'delete') {
// select current track in tracks form
f.remove(f.selectedIndex);
clearMap();
selectTrack(f);
} else {
f.options[f.selectedIndex].innerHTML = htmlEncode(trackName);
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']);
var f = document.getElementsByName('track')[0];
if (action == 'delete') {
// select current track in tracks form
f.remove(f.selectedIndex);
clearMap();
selectTrack(f);
} else {
f.options[f.selectedIndex].innerHTML = htmlEncode(trackName);
}
error = false;
} else if (root.length) {
errorMsg = getNode(root[0], 'message');
if (errorMsg) { message = errorMsg; }
}
return;
}
errorMsg = getNode(root[0], 'message');
if (errorMsg) { message = errorMsg; }
}
alert(lang['actionfailure'] + '\n' + message);
if (error) {
alert(lang['actionfailure'] + '\n' + message);
}
xhr = null;
}
}