if (typeof input === "undefined") {
var input = {
psBaseUri: "/",
_PS_MODE_DEV_: true,
PS_AUTOUP_BACKUP: true,
adminUrl: "http://test.com/admin",
adminDir: "/admin",
token: "asdadsasdasdasd",
txtError: [],
firstTimeParams: {},
ajaxUpgradeTabExists: true,
currentIndex: "page.php",
tab: input.tab,
channel: "major",
translation: {
confirmDeleteBackup: "Are you sure you want to delete this backup?",
delete: "Delete",
updateInProgress:
'An update is currently in progress... Click "OK" to abort.',
upgradingPrestaShop: "Upgrading PrestaShop",
upgradeComplete: "Upgrade complete",
upgradeCompleteWithWarnings:
"Upgrade complete, but warning notifications has been found.",
startingRestore: "Starting restoration...",
restoreComplete: "Restoration complete.",
cannotDownloadFile:
"Your server cannot download the file. Please upload it first by ftp in your admin/autoupgrade directory",
jsonParseErrorForAction:
"Javascript error (parseJSON) detected for action ",
endOfProcess: "End of process",
processCancelledCheckForRestore:
"Operation canceled. Checking for restoration...",
confirmRestoreBackup: "Do you want to restore SomeBackupName?",
processCancelledWithError: "Operation canceled. An error happened.",
missingAjaxUpgradeTab:
"[TECHNICAL ERROR] ajax-upgradetab.php is missing. Please reinstall the module.",
clickToRefreshAndUseNewConfiguration:
"Click to refresh the page and use the new configuration",
errorDetectedDuring: "Error detected during",
downloadTimeout:
"The request exceeded the max_time_limit. Please change your server configuration.",
seeOrHideList: "See or hide the list",
coreFiles: "Core file(s)",
mailFiles: "Mail file(s)",
translationFiles: "Translation file(s)",
linkAndMd5CannotBeEmpty: "Link and MD5 hash cannot be empty",
needToEnterArchiveVersionNumber:
"You must enter the full version number of the version you want to upgrade. The full version number can be present in the zip name (ex: 1.7.8.1, 8.0.0).",
noArchiveSelected: "No archive has been selected.",
needToEnterDirectoryVersionNumber:
"You need to enter the version number associated with the directory.",
confirmSkipBackup: "Please confirm that you want to skip the backup.",
confirmPreserveFileOptions:
"Please confirm that you want to preserve file options.",
lessOptions: "Less options",
moreOptions: "More options (Expert mode)",
filesWillBeDeleted: "These files will be deleted",
filesWillBeReplaced: "These files will be replaced",
noXmlSelected: "No XML file has been selected.",
noArchiveAndXmlSelected: "No archive and no XML file have been selected.",
},
};
}
var firstTimeParams = input.firstTimeParams.nextParams;
firstTimeParams.firstTime = "1";
function ucFirst(str) {
if (str.length > 0) {
return str[0].toUpperCase() + str.substring(1);
}
return str;
}
function cleanInfo() {
$("#infoStep").html("reset ");
}
function updateInfoStep(msg) {
if (msg) {
const infoStepElement = $("#infoStep");
infoStepElement.append(msg + '
');
infoStepElement.prop(
{ scrollTop: infoStepElement.prop("scrollHeight") },
1,
);
}
}
function addError(error) {
if (error && error.length) {
$("#errorDuringUpgrade").show();
const infoErrorElement = $("#infoError");
if (Array.isArray(error)) {
for (let i = 0; i < error.length; i++) {
infoErrorElement.append(error[i] + '');
}
} else {
infoErrorElement.append(error + '');
}
// Note: jquery 1.6 makes use of prop() instead of attr()
infoErrorElement.prop(
{ scrollTop: infoErrorElement.prop("scrollHeight") },
1,
);
}
}
function addQuickInfo(quickInfo) {
if (quickInfo && quickInfo.length) {
const quickInfoElement = $("#quickInfo");
quickInfoElement.show();
if (Array.isArray(quickInfo)) {
for (let i = 0; i < quickInfo.length; i++) {
quickInfoElement.append(quickInfo[i] + '');
}
} else {
quickInfoElement.append(quickInfo + '');
}
// Note : jquery 1.6 make uses of prop() instead of attr()
quickInfoElement.prop(
{ scrollTop: quickInfoElement.prop("scrollHeight") },
1,
);
}
}
// js initialization : prepare upgrade and rollback buttons
$(document).ready(function () {
$(".nobootstrap.no-header-toolbar")
.removeClass("nobootstrap")
.addClass("bootstrap");
$(document).on("click", "a.confirmBeforeDelete", function (e) {
if (!confirm(input.translation.confirmDeleteBackup)) {
e.preventDefault();
}
});
$("select[name=channel]").change(function (e) {
$(this)
.find("option")
.each(function () {
var $this = $(this);
$("#for-" + $this.attr("id")).toggle($this.is(":selected"));
});
refreshChannelInfos();
});
function refreshChannelInfos() {
var val = $("select[name=channel]").val();
$.ajax({
type: "POST",
url: input.adminUrl + "/autoupgrade/ajax-upgradetab.php",
async: true,
data: {
dir: input.adminDir,
token: input.token,
tab: input.tab,
action: "getChannelInfo",
ajaxMode: "1",
params: { channel: val },
},
success: function (res, textStatus, jqXHR) {
if (isJsonString(res)) {
res = $.parseJSON(res);
} else {
res = { nextParams: { status: "error" } };
}
var answer = res.nextParams.result;
if (typeof answer !== "undefined") {
var $channelInfos = $("#channel-infos");
$channelInfos.replaceWith(answer.div);
if (answer.available) {
$("#channel-infos .all-infos").show();
} else {
$channelInfos.html(answer.div);
$("#channel-infos .all-infos").hide();
}
}
},
error: function (res, textStatus, jqXHR) {
if (textStatus === "timeout" && action === "download") {
updateInfoStep(input.translation.cannotDownloadFile);
} else {
// technical error : no translation needed
$("#checkPrestaShopFilesVersion").html(
' Error Unable to check md5 files',
);
}
},
});
}
// the following prevents to leave the page at the inappropriate time
$.xhrPool = [];
$.xhrPool.abortAll = function () {
$.each(this, function (jqXHR) {
if (jqXHR && jqXHR.readystate !== 4) {
jqXHR.abort();
}
});
};
$(".upgradestep").click(function (e) {
e.preventDefault();
// $.scrollTo("#options")
});
// set timeout to 120 minutes (before aborting an ajax request)
$.ajaxSetup({ timeout: 7200000 });
// prepare available button here, without params ?
prepareNextButton("#upgradeNow", firstTimeParams);
/**
* reset rollbackParams js array (used to init rollback button)
*/
$("select[name=restoreName]").change(function () {
var val = $(this).val();
// show delete button if the value is not 0
if (val != 0) {
$("span#buttonDeleteBackup").html(
' ' +
input.translation.delete +
"",
);
}
if (val != 0) {
$("#rollback").removeAttr("disabled");
var rollbackParams = $.extend(true, {}, firstTimeParams);
delete rollbackParams.backupName;
delete rollbackParams.backupFilesFilename;
delete rollbackParams.backupDbFilename;
delete rollbackParams.restoreFilesFilename;
delete rollbackParams.restoreDbFilenames;
// init new name to backup
rollbackParams.restoreName = val;
prepareNextButton("#rollback", rollbackParams);
} else {
$("#rollback").attr("disabled", "disabled");
}
});
$("div[id|=for]").hide();
$("select[name=channel]").change();
if (!input.ajaxUpgradeTabExists) {
$("#checkPrestaShopFilesVersion").html(
'' +
input.translation.missingAjaxUpgradeTab,
);
}
});
function showConfigResult(msg, type) {
if (!type) {
type = "conf";
}
var $configResult = $("#configResult");
$configResult.html('
' + msg + "
").show();
if (type === "conf") {
$configResult.delay(3000).fadeOut("slow", function () {
location.reload();
});
}
}
// reuse previousParams, and handle xml returns to calculate next step
// (and the correct next param array)
// a case has to be defined for each requests that returns xml
function afterUpdateConfig(res) {
var params = res.nextParams;
var config = params.config;
var $oldChannel = $("select[name=channel] option.current");
if (config.channel != $oldChannel.val()) {
var $newChannel = $(
"select[name=channel] option[value=" + config.channel + "]",
);
$oldChannel.removeClass("current").html($oldChannel.html().substr(2));
$newChannel.addClass("current").html("* " + $newChannel.html());
}
if (res.error == 1) {
showConfigResult(res.next_desc, "error");
} else {
showConfigResult(res.next_desc);
}
$("#upgradeNow")
.unbind()
.replaceWith(
'' +
input.translation.clickToRefreshAndUseNewConfiguration +
"",
);
}
function startProcess(type) {
// hide useless divs, show activity log
$(
"#informationBlock,#comparisonBlock,#currentConfigurationBlock,#backupOptionsBlock,#upgradeOptionsBlock,#upgradeButtonBlock",
).slideUp("fast");
$(".autoupgradeSteps a").addClass("button");
$("#activityLogBlock").fadeIn("slow");
$(window).bind("beforeunload", function (e) {
if (confirm(input.translation.updateInProgress)) {
$.xhrPool.abortAll();
$(window).unbind("beforeunload");
return true;
} else {
if (type === "upgrade") {
e.returnValue = false;
e.cancelBubble = true;
if (e.stopPropagation) {
e.stopPropagation();
}
if (e.preventDefault) {
e.preventDefault();
}
}
}
});
}
function afterUpgradeNow(res) {
startProcess("upgrade");
$("#upgradeNow")
.unbind()
.replaceWith(
'' +
input.translation.upgradingPrestaShop +
" ...",
);
}
function afterUpgradeComplete(res) {
$("#pleaseWait").hide();
if (res.nextParams.warning_exists == "false") {
$("#infoStep").html(`
${input.translation.upgradeComplete}
`);
} else {
$("#infoStep").html(`
${input.translation.upgradeCompleteWithWarnings}
`);
}
$("#postUpdateChecklist").show();
$(window).unbind("beforeunload");
}
function afterError(res) {
var params = res.nextParams;
if (params.next === "") {
$(window).unbind("beforeunload");
}
$("#pleaseWait").hide();
addQuickInfo(["unbind :) "]);
}
function afterRollback(res) {
startProcess("rollback");
}
function afterRollbackComplete(res) {
$("#pleaseWait").hide();
$("#postRestoreChecklist").show();
$(window).unbind();
}
function afterRestoreDb(params) {
// $("#restoreBackupContainer").hide();
}
function afterRestoreFiles(params) {
// $("#restoreFilesContainer").hide();
}
function afterBackupFiles(res) {
var params = res.nextParams;
// if (params.stepDone)
}
/**
* afterBackupDb display the button
*/
function afterBackupDb(res) {
var params = res.nextParams;
if (res.stepDone && input.PS_AUTOUP_BACKUP === true) {
$("#restoreBackupContainer").show();
$("select[name=restoreName]")
.append(
'",
)
.val("")
.change();
}
}
function call_function(func) {
this[func].apply(this, Array.prototype.slice.call(arguments, 1));
}
function doAjaxRequest(action, nextParams) {
if (input._PS_MODE_DEV_ === true) {
addQuickInfo(["[DEV] ajax request : " + action]);
}
$("#pleaseWait").show();
$("#rollbackForm").hide();
var req = $.ajax({
type: "POST",
url: input.adminUrl + "/autoupgrade/ajax-upgradetab.php",
async: true,
data: {
dir: input.adminDir,
ajaxMode: "1",
token: input.token,
tab: input.tab,
action: action,
params: nextParams,
},
beforeSend: (jqXHR) => $.xhrPool.push(jqXHR),
complete: (jqXHR) => $.xhrPool.pop(),
success: (res, textStatus, jqXHR) =>
handleRequestSuccess(res, textStatus, jqXHR, action),
error: (jqXHR, textStatus, errorThrown) =>
handleRequestError(jqXHR, textStatus, errorThrown, action),
});
return req;
}
function handleRequestSuccess(res, textStatus, jqXHR, action) {
$("#pleaseWait").hide();
$("#rollbackForm").show();
try {
res = $.parseJSON(res);
} catch (e) {
addError(`${input.translation.jsonParseErrorForAction} [${action}].`);
return;
}
addQuickInfo(res.nextQuickInfo);
addError(res.nextErrors);
updateInfoStep(res.next_desc);
if (res.status !== "ok") {
addError(`${input.translation.errorDetectedDuring} [${action}].`);
return;
}
$("#" + action).addClass("done");
if (res.stepDone) {
$("#" + action).addClass("stepok");
}
// if a function "after[action name]" exists, it should be called now.
// This is used for enabling restore buttons for example
const funcName = "after" + ucFirst(action);
if (typeof window[funcName] === "function") {
call_function(funcName, res);
}
if (res.next !== "") {
// if next is rollback, prepare nextParams with rollbackDbFilename and rollbackFilesFilename
if (res.next === "rollback") {
res.nextParams.restoreName = "";
}
doAjaxRequest(res.next, res.nextParams);
} else {
// Way To Go, end of upgrade process
addQuickInfo(input.translation.endOfProcess);
}
}
function handleRequestError(jqXHR, textStatus, errorThrown, action) {
$("#pleaseWait").hide();
$("#rollbackForm").show();
if (textStatus === "timeout") {
if (action === "download") {
addError(input.translation.cannotDownloadFile);
} else {
addError(`[Server Error] Timeout: ${input.translation.downloadTimeout}`);
}
} else {
try {
const res = $.parseJSON(jqXHR.responseText);
addQuickInfo(res.nextQuickInfo);
addError(res.nextErrors);
updateInfoStep(res.next_desc);
} catch (e) {
addError(
`[Ajax / Server Error for action: ${action}] textStatus: ${textStatus}, errorThrown: ${errorThrown}, jqXHR: ${jqXHR.responseText}`,
);
}
}
}
/**
* prepareNextButton make the button button_selector available, and update the nextParams values
*
* @param button_selector $button_selector
* @param nextParams $nextParams
* @return void
*/
function prepareNextButton(button_selector, nextParams) {
if (button_selector === "#rollback") {
$("#postUpdateChecklist").hide();
}
$(button_selector)
.unbind()
.click(function (e) {
e.preventDefault();
$("#currentlyProcessing").show();
var action = button_selector.substr(1);
doAjaxRequest(action, nextParams);
});
}
// ajax to check md5 files
function addModifiedFileList(title, fileList, css_class, container) {
var subList = $('