Skip to content

Commit b99cd45

Browse files
Merge pull request #88 from GameServerPanel/copilot/move-update-ui-to-update-module
2 parents 9826497 + 1a75029 commit b99cd45

5 files changed

Lines changed: 164 additions & 29 deletions

File tree

modules/administration/administration.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,6 @@ function exec_ogp_module()
181181
"</table>\n";
182182
### END ICONS TO FRAMES
183183

184-
### PANEL UPDATES
185-
require_once(dirname(__FILE__) . '/panel_update.php');
186-
gsp_panel_update_section();
187-
### END PANEL UPDATES
188-
189184
### CHANGE MENU ORDER
190185

191186
if ( isset( $_POST['changeOrder'] ) )

modules/administration/panel_update.php

Lines changed: 136 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
*/
1717

1818
// Panel root is two directories up from this file (modules/administration/panel_update.php)
19-
define('GSP_PANEL_DIR', realpath(dirname(__FILE__) . '/../../'));
20-
define('GSP_BACKUP_BASE', GSP_PANEL_DIR . '/backups');
21-
define('GSP_UPDATE_LOG', GSP_PANEL_DIR . '/logs/panel_updates.log');
22-
define('GSP_VERSION_FILE', GSP_PANEL_DIR . '/includes/panel_version.php');
23-
define('GSP_VERSION_JSON', GSP_PANEL_DIR . '/version.json');
19+
defined('GSP_PANEL_DIR') || define('GSP_PANEL_DIR', realpath(dirname(__FILE__) . '/../../'));
20+
defined('GSP_BACKUP_BASE') || define('GSP_BACKUP_BASE', GSP_PANEL_DIR . '/backups');
21+
defined('GSP_UPDATE_LOG') || define('GSP_UPDATE_LOG', GSP_PANEL_DIR . '/logs/panel_updates.log');
22+
defined('GSP_VERSION_FILE') || define('GSP_VERSION_FILE', GSP_PANEL_DIR . '/includes/panel_version.php');
23+
defined('GSP_VERSION_JSON') || define('GSP_VERSION_JSON', GSP_PANEL_DIR . '/version.json');
2424

2525
// ---------------------------------------------------------------------------
2626
// Helper: write a line to the panel update log
@@ -35,6 +35,35 @@ function gsp_update_log($message)
3535
@file_put_contents(GSP_UPDATE_LOG, $line, FILE_APPEND | LOCK_EX);
3636
}
3737

38+
// ---------------------------------------------------------------------------
39+
// Helper: insert a row into gsp_panel_update_log (silently skips on failure)
40+
// ---------------------------------------------------------------------------
41+
function gsp_log_update_to_db($channel, $branch, $status, $message, $backup_path = null, $db_backup_path = null, $file_backup_path = null, $started_at = null, $finished_at = null)
42+
{
43+
global $db;
44+
if (!isset($db) || !is_object($db)) {
45+
return;
46+
}
47+
if ($started_at === null) {
48+
$started_at = date('Y-m-d H:i:s');
49+
}
50+
$channel = $db->real_escape_string((string) $channel);
51+
$branch = $branch !== null ? "'" . $db->real_escape_string((string) $branch) . "'" : 'NULL';
52+
$status = $db->real_escape_string((string) $status);
53+
$message_esc = $message !== null ? "'" . $db->real_escape_string((string) $message) . "'" : 'NULL';
54+
$backup_path_esc = $backup_path !== null ? "'" . $db->real_escape_string((string) $backup_path) . "'" : 'NULL';
55+
$db_backup_esc = $db_backup_path !== null ? "'" . $db->real_escape_string((string) $db_backup_path) . "'" : 'NULL';
56+
$file_backup_esc = $file_backup_path !== null ? "'" . $db->real_escape_string((string) $file_backup_path) . "'" : 'NULL';
57+
$started_esc = "'" . $db->real_escape_string($started_at) . "'";
58+
$finished_esc = $finished_at !== null ? "'" . $db->real_escape_string((string) $finished_at) . "'" : 'NULL';
59+
$db->query(
60+
"INSERT INTO OGP_DB_PREFIXpanel_update_log"
61+
. " (channel, branch, status, message, backup_path, db_backup_path, file_backup_path, started_at, finished_at)"
62+
. " VALUES ('{$channel}', {$branch}, '{$status}', {$message_esc},"
63+
. " {$backup_path_esc}, {$db_backup_esc}, {$file_backup_esc}, {$started_esc}, {$finished_esc})"
64+
);
65+
}
66+
3867
// ---------------------------------------------------------------------------
3968
// Helper: read the installed version / branch from panel_version.php
4069
// ---------------------------------------------------------------------------
@@ -836,7 +865,7 @@ function gsp_do_update($repo_owner, $repo_name, $ref, $update_type)
836865
}
837866

838867
gsp_update_log("Update to {$ref} (type={$update_type}) complete");
839-
return ['success' => true, 'files_copied' => $files_copied];
868+
return ['success' => true, 'files_copied' => $files_copied, 'backup_dir' => $backup['backup_dir']];
840869
}
841870

842871
// ---------------------------------------------------------------------------
@@ -1004,67 +1033,153 @@ function gsp_panel_update_section()
10041033
$user_label = htmlspecialchars($_SESSION['users_login'])
10051034
. ' (IP: ' . htmlspecialchars($_SERVER['REMOTE_ADDR']) . ')';
10061035

1007-
if ($action === 'update_release') {
1036+
if ($action === 'backup_only') {
1037+
$started_at = date('Y-m-d H:i:s');
1038+
$result = gsp_create_full_backup('backup-only', 'manual');
1039+
$finished_at = date('Y-m-d H:i:s');
1040+
if ($result['success']) {
1041+
$bk_dir = htmlspecialchars($result['backup_dir']);
1042+
print_success('Backup created successfully at <code>' . $bk_dir . '</code>.');
1043+
gsp_update_log("Admin {$user_label} created manual backup at {$result['backup_dir']}");
1044+
gsp_log_update_to_db(
1045+
'backup-only', null, 'success',
1046+
'Manual backup by ' . $_SESSION['users_login'],
1047+
$result['backup_dir'],
1048+
$result['backup_dir'] . '/database.sql',
1049+
$result['backup_dir'] . '/panel-files.tar.gz',
1050+
$started_at, $finished_at
1051+
);
1052+
} else {
1053+
print_failure('Backup failed: ' . htmlspecialchars($result['error']));
1054+
gsp_update_log("Admin {$user_label} manual backup FAILED: {$result['error']}");
1055+
gsp_log_update_to_db(
1056+
'backup-only', null, 'failed',
1057+
'Manual backup failed: ' . $result['error'],
1058+
null, null, null, $started_at, $finished_at
1059+
);
1060+
}
1061+
1062+
} elseif ($action === 'update_release') {
10081063
$version = isset($_POST['gsp_release_version']) ? trim($_POST['gsp_release_version']) : '';
10091064
if (!preg_match('/^[a-zA-Z0-9._\-]+$/', $version) || strlen($version) > 80) {
10101065
print_failure('Invalid release tag selected.');
10111066
} else {
1067+
$started_at = date('Y-m-d H:i:s');
10121068
$result = gsp_do_update($repo_owner, $repo_name, $version, 'release');
1069+
$finished_at = date('Y-m-d H:i:s');
10131070
if ($result['success']) {
10141071
print_success(
10151072
'Panel updated to release <strong>' . htmlspecialchars($version) . '</strong>. '
10161073
. intval($result['files_copied']) . ' file(s) updated. Source: <strong>GitHub Releases</strong>'
10171074
);
10181075
gsp_update_log("Admin {$user_label} updated panel to release {$version}");
1076+
gsp_log_update_to_db(
1077+
'release', $version, 'success',
1078+
'Updated to release ' . $version . ' by ' . $_SESSION['users_login'],
1079+
$result['backup_dir'] ?? null,
1080+
isset($result['backup_dir']) ? $result['backup_dir'] . '/database.sql' : null,
1081+
isset($result['backup_dir']) ? $result['backup_dir'] . '/panel-files.tar.gz': null,
1082+
$started_at, $finished_at
1083+
);
10191084
} else {
10201085
print_failure('Update failed: ' . htmlspecialchars($result['error']));
10211086
gsp_update_log("Admin {$user_label} update to release {$version} FAILED: {$result['error']}");
1087+
gsp_log_update_to_db(
1088+
'release', $version, 'failed',
1089+
'Update to release ' . $version . ' failed: ' . $result['error'],
1090+
null, null, null, $started_at, $finished_at
1091+
);
10221092
}
10231093
}
10241094

10251095
} elseif ($action === 'update_stable') {
1096+
$started_at = date('Y-m-d H:i:s');
10261097
$result = gsp_do_update($repo_owner, $repo_name, $stable_branch, 'development');
1098+
$finished_at = date('Y-m-d H:i:s');
10271099
if ($result['success']) {
10281100
print_success(
10291101
'Panel updated to development version (<strong>' . htmlspecialchars($stable_branch) . '</strong>). '
10301102
. intval($result['files_copied']) . ' file(s) updated. Source: <strong>'
10311103
. htmlspecialchars($stable_branch) . '</strong>'
10321104
);
10331105
gsp_update_log("Admin {$user_label} updated panel to stable branch {$stable_branch}");
1106+
gsp_log_update_to_db(
1107+
'development', $stable_branch, 'success',
1108+
'Updated to stable branch ' . $stable_branch . ' by ' . $_SESSION['users_login'],
1109+
$result['backup_dir'] ?? null,
1110+
isset($result['backup_dir']) ? $result['backup_dir'] . '/database.sql' : null,
1111+
isset($result['backup_dir']) ? $result['backup_dir'] . '/panel-files.tar.gz': null,
1112+
$started_at, $finished_at
1113+
);
10341114
} else {
10351115
print_failure('Update failed: ' . htmlspecialchars($result['error']));
10361116
gsp_update_log("Admin {$user_label} update to stable branch {$stable_branch} FAILED: {$result['error']}");
1117+
gsp_log_update_to_db(
1118+
'development', $stable_branch, 'failed',
1119+
'Update to stable branch ' . $stable_branch . ' failed: ' . $result['error'],
1120+
null, null, null, $started_at, $finished_at
1121+
);
10371122
}
10381123

10391124
} elseif ($action === 'update_unstable') {
1125+
$started_at = date('Y-m-d H:i:s');
10401126
$result = gsp_do_update($repo_owner, $repo_name, $unstable_branch, 'cutting-edge');
1127+
$finished_at = date('Y-m-d H:i:s');
10411128
if ($result['success']) {
10421129
print_success(
10431130
'Panel updated to cutting edge version (<strong>' . htmlspecialchars($unstable_branch) . '</strong>). '
10441131
. intval($result['files_copied']) . ' file(s) updated. Source: <strong>'
10451132
. htmlspecialchars($unstable_branch) . '</strong>'
10461133
);
10471134
gsp_update_log("Admin {$user_label} updated panel to unstable branch {$unstable_branch}");
1135+
gsp_log_update_to_db(
1136+
'cutting-edge', $unstable_branch, 'success',
1137+
'Updated to cutting-edge branch ' . $unstable_branch . ' by ' . $_SESSION['users_login'],
1138+
$result['backup_dir'] ?? null,
1139+
isset($result['backup_dir']) ? $result['backup_dir'] . '/database.sql' : null,
1140+
isset($result['backup_dir']) ? $result['backup_dir'] . '/panel-files.tar.gz': null,
1141+
$started_at, $finished_at
1142+
);
10481143
} else {
10491144
print_failure('Update failed: ' . htmlspecialchars($result['error']));
10501145
gsp_update_log("Admin {$user_label} update to unstable branch {$unstable_branch} FAILED: {$result['error']}");
1146+
gsp_log_update_to_db(
1147+
'cutting-edge', $unstable_branch, 'failed',
1148+
'Update to cutting-edge branch ' . $unstable_branch . ' failed: ' . $result['error'],
1149+
null, null, null, $started_at, $finished_at
1150+
);
10511151
}
10521152

10531153
} elseif ($action === 'revert') {
10541154
$backup_ts = isset($_POST['gsp_revert_backup']) ? trim($_POST['gsp_revert_backup']) : '';
10551155
if (!preg_match('/^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}$/', $backup_ts)) {
10561156
print_failure('Invalid backup timestamp selected.');
10571157
} else {
1158+
$started_at = date('Y-m-d H:i:s');
10581159
$result = gsp_do_revert($backup_ts);
1160+
$finished_at = date('Y-m-d H:i:s');
10591161
if ($result['success']) {
10601162
print_success(
10611163
'Panel reverted to backup from <strong>' . htmlspecialchars($backup_ts) . '</strong>. '
10621164
. intval($result['files_restored']) . ' file(s) restored.'
10631165
);
10641166
gsp_update_log("Admin {$user_label} reverted panel to backup {$backup_ts}");
1167+
gsp_log_update_to_db(
1168+
'revert', $backup_ts, 'success',
1169+
'Reverted to backup ' . $backup_ts . ' by ' . $_SESSION['users_login'],
1170+
GSP_BACKUP_BASE . '/' . $backup_ts,
1171+
GSP_BACKUP_BASE . '/' . $backup_ts . '/database.sql',
1172+
GSP_BACKUP_BASE . '/' . $backup_ts . '/panel-files.tar.gz',
1173+
$started_at, $finished_at
1174+
);
10651175
} else {
10661176
print_failure('Revert failed: ' . htmlspecialchars($result['error']));
10671177
gsp_update_log("Admin {$user_label} revert to backup {$backup_ts} FAILED: {$result['error']}");
1178+
gsp_log_update_to_db(
1179+
'revert', $backup_ts, 'failed',
1180+
'Revert to backup ' . $backup_ts . ' failed: ' . $result['error'],
1181+
null, null, null, $started_at, $finished_at
1182+
);
10681183
}
10691184
}
10701185
}
@@ -1130,6 +1245,20 @@ function gsp_panel_update_section()
11301245
}
11311246
echo "</table>\n<br>\n";
11321247

1248+
// ---- Backup Only --------------------------------------------------------
1249+
echo "<h3>Create Backup</h3>\n";
1250+
echo "<form method='POST'>\n";
1251+
echo "<input type='hidden' name='gsp_update_action' value='backup_only'>\n";
1252+
echo "<input type='hidden' name='gsp_update_csrf' value='" . htmlspecialchars($csrf_token) . "'>\n";
1253+
echo "<button type='submit'"
1254+
. " onclick='return confirm(\"Create a backup of panel files and the database now (no update). Continue?\");'>"
1255+
. "Create Backup Now</button>\n";
1256+
echo "<span style='margin-left:10px;color:#666;'>Saves to: <code>"
1257+
. htmlspecialchars(GSP_BACKUP_BASE) . "</code></span>\n";
1258+
echo "</form>\n";
1259+
1260+
echo "<br>\n";
1261+
11331262
// ---- Numbered Releases --------------------------------------------------
11341263
echo "<h3>Numbered Releases</h3>\n";
11351264
if (is_array($releases) && !empty($releases)) {

modules/update/module.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
// Module general information
2626
$module_title = "Update";
27-
$module_version = "1.1";
28-
$db_version = 2; // avoid 'duplicate table' error message.
27+
$module_version = "1.2";
28+
$db_version = 3; // avoid 'duplicate table' error message.
2929
$module_required = TRUE;
3030
$module_menus = array(
3131
array( 'subpage' => '', 'name'=>'Update', 'group'=>'admin' )
@@ -38,13 +38,28 @@
3838
`file_path` VARCHAR(1000) UNIQUE NOT NULL
3939
) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
4040
$install_queries[2] = array(
41-
"DELETE FROM ".OGP_DB_PREFIX."update_blacklist
41+
"DELETE FROM ".OGP_DB_PREFIX."update_blacklist
4242
WHERE file_path IN (SELECT *
4343
FROM (SELECT file_path FROM ".OGP_DB_PREFIX."update_blacklist
4444
GROUP BY file_path HAVING (COUNT(*) > 1)
4545
) AS A
4646
);",
4747
"ALTER TABLE ".OGP_DB_PREFIX."update_blacklist MODIFY file_path VARCHAR(1000);",
48-
"ALTER TABLE ".OGP_DB_PREFIX."update_blacklist ADD UNIQUE (file_path);"
48+
"ALTER TABLE ".OGP_DB_PREFIX."update_blacklist ADD UNIQUE (file_path);"
49+
);
50+
$install_queries[3] = array(
51+
"CREATE TABLE IF NOT EXISTS `".OGP_DB_PREFIX."panel_update_log` (
52+
`id` int(11) NOT NULL AUTO_INCREMENT,
53+
`channel` varchar(64) NOT NULL,
54+
`branch` varchar(128) DEFAULT NULL,
55+
`status` varchar(32) NOT NULL,
56+
`message` text DEFAULT NULL,
57+
`backup_path` varchar(255) DEFAULT NULL,
58+
`db_backup_path` varchar(255) DEFAULT NULL,
59+
`file_backup_path` varchar(255) DEFAULT NULL,
60+
`started_at` datetime NOT NULL,
61+
`finished_at` datetime DEFAULT NULL,
62+
PRIMARY KEY (`id`)
63+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
4964
);
5065
?>

modules/update/update.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,14 @@
2222
*
2323
*/
2424

25-
// todo, make checking and updating functions for updateing on the background.
26-
// todo, more specified updates in smaller packages
2725
function exec_ogp_module()
2826
{
29-
global $db, $settings;
30-
define('REPONAME', 'OGP-Website');
31-
32-
if ($_SESSION['users_group'] != "admin")
33-
{
34-
print_failure(get_lang('no_access'));
35-
return;
36-
}
37-
echo "To update the panel, visit our git at http://git.iaregamer.com:3000, download the panel and replace your files.";
27+
if ($_SESSION['users_group'] !== 'admin') {
28+
print_failure(get_lang('no_access'));
29+
return;
30+
}
3831

32+
require_once(dirname(__FILE__) . '/../administration/panel_update.php');
33+
gsp_panel_update_section();
3934
}
35+
?>

modules/user_games/edit_home.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ function exec_ogp_module()
715715
echo "<tr><td class='right'>". get_lang("game_control_password") .":</td><td class='left'>";
716716
echo "<form action='?m=user_games&p=edit&home_id=".$home_id."' method='post'>";
717717
echo "<input type='hidden' name='home_id' value=\"$home_id\" />\n";
718-
echo "<input type='text' size='30' name='control_password' value=\"".str_replace('"', "&quot;", $home_info['control_password'])."\" />";
718+
echo "<input type='password' size='30' name='control_password' value=\"".str_replace('"', "&quot;", $home_info['control_password'])."\" />";
719719
echo "<input type='submit' name='change_control_password' value='". get_lang("change_control_password") ."' />";
720720
echo "</form></td></tr>";
721721
echo "<tr><td colspan='2' class='info'>". get_lang("change_control_password_info") ."</td></tr>";
@@ -754,7 +754,7 @@ function exec_ogp_module()
754754
// Form to edit control ftp password
755755
echo "<tr><td class='right'>". get_lang("server_ftp_password") .":</td><td class='left'>";
756756
echo "<form action='?m=user_games&p=edit&home_id=".$home_id."' method='post'>";
757-
echo "<input type='text' size='30' name='ftp_password' value=\"".str_replace('"', "&quot;", $home_info['ftp_password'])."\" />";
757+
echo "<input type='password' size='30' name='ftp_password' value=\"".str_replace('"', "&quot;", $home_info['ftp_password'])."\" />";
758758
echo "<input type='submit' name='change_ftp_password' value='". get_lang("change_ftp_password") ."' />";
759759
echo "</form></td></tr>";
760760
echo "<tr><td colspan='2' class='info'>". get_lang("change_ftp_password_info") ."</td></tr>";

0 commit comments

Comments
 (0)