From 9d2283ecf769ee8fcf7311cc29d7b7b03bb1e42e Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Wed, 4 Feb 2015 23:51:12 -0500 Subject: [PATCH] Wire up migrations and actual database transactions. Things are looking good! --- install/mods-for-hesk/ajax/database-ajax.php | 31 ++++++++-- install/mods-for-hesk/ajax/task-ajax.php | 17 +++-- install/mods-for-hesk/modsForHeskSql.php | 46 ++++++++++++-- js/modsForHesk-javascript.js | 65 ++++++++++++++++++-- 4 files changed, 138 insertions(+), 21 deletions(-) diff --git a/install/mods-for-hesk/ajax/database-ajax.php b/install/mods-for-hesk/ajax/database-ajax.php index bf7918ee..81c74c62 100644 --- a/install/mods-for-hesk/ajax/database-ajax.php +++ b/install/mods-for-hesk/ajax/database-ajax.php @@ -1,10 +1,33 @@ 0) { - $users = getUsers(); $jsonToSend['status'] = 'ATTENTION'; $jsonToSend['users'] = array(); - while ($row = hesk_dbFetchAssoc($users)) { - $jsonToSend['users'][$row['id']] = $row['name']; + $users = getUsers(); + foreach ($users as $user) { + array_push($jsonToSend['users'], $user); } } else { $jsonToSend['status'] = 'SUCCESS'; } - return json_encode($jsonToSend); + print json_encode($jsonToSend); +} elseif ($task == 'migrate-bans') { + migrateBans($_POST['user']); +} else { + $response = 'The task "'.$task.'" was not recognized. Check your spelling and try again.'; + print $response; + http_response_code(400); } \ No newline at end of file diff --git a/install/mods-for-hesk/modsForHeskSql.php b/install/mods-for-hesk/modsForHeskSql.php index 0ff8dcc8..6bc886c6 100644 --- a/install/mods-for-hesk/modsForHeskSql.php +++ b/install/mods-for-hesk/modsForHeskSql.php @@ -2,7 +2,44 @@ require(HESK_PATH . 'hesk_settings.inc.php'); function executeQuery($sql) { - return hesk_dbQuery($sql); + global $hesk_last_query; + global $hesk_db_link; + if ( function_exists('mysqli_connect') ) { + + if ( ! $hesk_db_link && ! hesk_dbConnect()) + { + return false; + } + + $hesk_last_query = $sql; + + if ($res = @mysqli_query($hesk_db_link, $sql)) + { + return $res; + } else + { + print "Could not execute query: $sql. MySQL said: ".mysqli_error($hesk_db_link); + http_response_code(500); + die(); + } + } else { + if ( ! $hesk_db_link && ! hesk_dbConnect()) + { + return false; + } + + $hesk_last_query = $sql; + + if ($res = @mysql_query($sql, $hesk_db_link)) + { + return $res; + } else + { + print "Could not execute query: $sql. MySQL said: ".mysql_error(); + http_response_code(500); + die(); + } + } } // Version 1.0.0 - <1.4.0 @@ -251,20 +288,17 @@ function getUsers() { $users = array(); $usersRS = executeQuery("SELECT `id`, `name` FROM `".hesk_dbEscape($hesk_settings['db_pfix'])."users` WHERE `active` = '1' ORDER BY `name`"); while ($row = hesk_dbFetchAssoc($usersRS)) { - $users[$row['id']] = $row['name']; + array_push($users, $row); } return $users; } -function migrateBans() { +function migrateBans($creator) { global $hesk_settings; hesk_dbConnect(); - // Get the ID of the creator - $creator = $_POST['user']; - // Insert the email bans $emailBanRS = executeQuery("SELECT `Email` FROM `".hesk_dbEscape($hesk_settings['db_pfix'])."denied_emails`"); while ($row = hesk_dbFetchAssoc($emailBanRS)) { diff --git a/js/modsForHesk-javascript.js b/js/modsForHesk-javascript.js index 9afb2b01..04100faa 100644 --- a/js/modsForHesk-javascript.js +++ b/js/modsForHesk-javascript.js @@ -136,7 +136,7 @@ function appendToInstallConsole(text) { function processUpdates(startingVersion) { if (startingVersion < 1) { startVersionUpgrade('p140'); - executeUpdate(1, 'p140'); + executeUpdate(1000, 'p140'); } else if (startingVersion < 140) { startVersionUpgrade('140'); executeUpdate(140, '140'); @@ -170,30 +170,83 @@ function executeUpdate(version, cssclass) { markUpdateAsSuccess(cssclass); if (version == 200) { - migrateIpEmailBans(); + migrateIpEmailBans('banmigrate', cssclass); } processUpdates(version); }, - error: function() { + error: function(data) { + appendToInstallConsole('ERROR: ' + data.responseText); markUpdateAsFailure(cssclass); } }); } -function migrateIpEmailBans(cssclass) { +function migrateIpEmailBans(version, cssclass) { + startVersionUpgrade(version); $.ajax({ type: 'POST', url: 'ajax/task-ajax.php', data: { task: 'ip-email-bans' }, success: function(data) { - console.info(data); + var parsedData = $.parseJSON(data); + console.info(parsedData); + if (parsedData.status == 'ATTENTION') { + markUpdateAsAttention(version); + prepareAttentionPanel(getContentForMigratePrompt(parsedData.users)); + } else { + markUpdateAsSuccess(version); + } //if ask user, markUpdateAsAttention and append to Attention! div //otherwise, mark success and move to completion script. }, - error: function() { + error: function(data) { + appendToInstallConsole('ERROR: ' + data.responseText); markUpdateAsFailure(cssclass); } }); } +function getContentForMigratePrompt(users) { + var beginningText = '

Migrating IP / E-mail Bans

Mods for HESK has detected that you have added IP address ' + + 'and/or email bans using Mods for HESK. As part of the upgrade process, Mods for HESK will migrate these bans ' + + 'for you to HESK 2.6.0\'s IP/email ban feature. Select the user below that will be the "creator" of the bans, ' + + 'then click "Submit".

'; + var selectMarkup = '
User:
' + + '

'; + var submitMarkup = '
' + + 'Don\'t Migrate
'; + + return beginningText + selectMarkup + submitMarkup; +} + +function prepareAttentionPanel(content) { + $('#attention-body').html(content); + $('#attention-row').show(); +} + +function runMigration() { + // Get user ID that is selected + var userId = $('#user-dropdown').val(); + // Hide the div, switch back to in progress + $('#attention-row').hide(); + startVersionUpgrade('banmigrate'); + $.ajax({ + type: 'POST', + url: 'ajax/task-ajax.php', + data: { task: 'migrate-bans', user: userId }, + success: function(data) { + markUpdateAsSuccess('banmigrate'); + //TODO cover entire install area with success message. + }, + error: function(data) { + appendToInstallConsole('ERROR: ' + data.responseText); + markUpdateAsFailure('banmigrate'); + } + }) +} + jQuery(document).ready(loadJquery);