diff --git a/index.php b/index.php index b4048a7b..c47392a2 100644 --- a/index.php +++ b/index.php @@ -1828,6 +1828,15 @@ function escapeHTML(s) { return s.toString().split('&').join('&').split('<').join('<').split('>').join('>').split('"').join('"').split('\'').join('''); } + function errorResponse(response) { + var text = escapeHTML(response['response']); + if (response['detailedResponseText'] !== '') { // Include additional details (raw response) when available + text += '
Show detailed response
' +
+					escapeHTML(response['detailedResponseText']) + '
'; + } + return text; + } + var done = false; var started = false; var updaterStepStart = parseInt(document.getElementById('updater-step-start').value); @@ -1835,7 +1844,7 @@ function escapeHTML(s) { function addStepText(id, text) { var el = document.getElementById(id); var output = el.getElementsByClassName('output')[0]; - if(typeof text === 'object') { + if (typeof text === 'object') { text = JSON.stringify(text); } output.innerHTML = output.innerHTML + text; @@ -1890,32 +1899,47 @@ function waitingStep(id) { function performStep(number, callback) { started = true; - var httpRequest = new XMLHttpRequest(); - httpRequest.open('POST', window.location.href); + const url = window.location.href; + const httpRequest = new XMLHttpRequest(); + httpRequest.open('POST', url); httpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); httpRequest.setRequestHeader('X-Updater-Auth', document.getElementById('updater-access-key').value); + httpRequest.onerror = function () { + const response = { + proceed: false, + response: 'Error accessing ' + url + ' (' + (httpRequest.statusText || 'Network error') + ')', + detailedResponseText: httpRequest.responseText, // Probably empty but won't hurt + }; + callback(response); + }; httpRequest.onreadystatechange = function () { - if (httpRequest.readyState != 4) { // 4 - request done + if (httpRequest.readyState === 4) { // Request done + if (httpRequest.status === 200) { // Ensure step was successful + try { // Ensure response contains a valid JSON object + const data = JSON.parse(httpRequest.ResponseText); + if (typeof data.response === 'object') { + callback(data); // Valid + } + } catch (error) { // Invalid JSON + // probably SyntaxError; handled by next line + } + const response = { // SyntaxError / unexpected non-object response + proceed: false, + response: 'Syntax error or unexpected response from server. See details for clues.', + detailedResponseText: httpRequest.responseText, + }; + callback(response); + } else { // Request not successful (i.e. got something other than HTTP 200 OK) + const response = { + proceed: false, + response: 'Invalid HTTP code received from server ( ' + httpRequest.status + ' ' + httpRequest.statusText + '.', + detailedResponseText: httpRequest.responseText, + }; + callback(response); + } + } else { // Request not done return; } - - if (httpRequest.status != 200) { - // failure - } - - if(httpRequest.responseText.substr(0,1) !== '{') { - // it seems that this is not a JSON object - var response = { - processed: false, - response: 'Parsing response failed.', - detailedResponseText: httpRequest.responseText, - }; - callback(response); - } else { - // parse JSON - callback(JSON.parse(httpRequest.responseText)); - } - }; httpRequest.send("step="+number); } @@ -1927,7 +1951,7 @@ function performStep(number, callback) { performStep(1, performStepCallbacks[1]); }, 1: function(response) { - if(response.proceed === true) { + if (response.proceed === true) { successStep('step-check-files'); currentStep('step-check-permissions'); performStep(2, performStepCallbacks[2]); @@ -1936,9 +1960,7 @@ function performStep(number, callback) { var text = ''; if (typeof response['response'] === 'string') { - text = escapeHTML(response['response']); - text += '
Show detailed response
' +
-							escapeHTML(response['detailedResponseText']) + '
'; + text = errorResponse(response); } else { text = 'Unknown files detected within the installation folder. This can be fixed by manually removing (or moving) these files. The following extra files have been found: