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 )) {
0 commit comments