From 52a36c2957ae707a8941137bff09f6b6b0621354 Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Fri, 19 May 2017 10:59:27 +0200 Subject: [PATCH] Better handle ajax errors --- auth.php | 16 +++++++++------- css/main.css | 4 ++++ helpers/config.php | 2 +- js/admin.js | 38 ++++++++++++++++++++++---------------- js/pass.js | 28 +++++++++++++++++----------- js/track.js | 44 +++++++++++++++++++++++++------------------- 6 files changed, 78 insertions(+), 54 deletions(-) diff --git a/auth.php b/auth.php index ed7db2e..f3e858d 100755 --- a/auth.php +++ b/auth.php @@ -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')))

- ' . (($force_login == 1) ? "" : "") . ' + ' . (($force_login) ? ' +
' . $lang["cancel"] . '
' : '') . ' -
' . (($auth_error == 1) ? $lang["authfail"] : "") . '
+
' . (($auth_error) ? $lang["authfail"] : "") . '
'; @@ -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')])) { diff --git a/css/main.css b/css/main.css index f753241..6552d08 100755 --- a/css/main.css +++ b/css/main.css @@ -278,6 +278,10 @@ button { margin-right: 5px; } +#cancel { + margin-top: 0.5em; +} + .red-button { color: white; float: right; diff --git a/helpers/config.php b/helpers/config.php index cff56c9..6a62f7a 100644 --- a/helpers/config.php +++ b/helpers/config.php @@ -28,7 +28,7 @@ */ class uConfig { // version number - static $version = "0.2"; + static $version = "0.3-beta"; // default map drawing framework static $mapapi = "openlayers"; diff --git a/js/admin.js b/js/admin.js index 1c8c98c..cc3c9e0 100644 --- a/js/admin.js +++ b/js/admin.js @@ -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; } } diff --git a/js/pass.js b/js/pass.js index 9c7b284..95723d7 100644 --- a/js/pass.js +++ b/js/pass.js @@ -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; } } diff --git a/js/track.js b/js/track.js index fa411b2..6146e82 100644 --- a/js/track.js +++ b/js/track.js @@ -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; } }