Skip to content

Commit ed6abdd

Browse files
author
joaosraposo
committed
Improve database query preparation and error handling in backup processes
1 parent cbb2d93 commit ed6abdd

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

trunk/HDB.php

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ static function get_logs($backup_id = null, $limit = 100, $offset = 0) {
676676
$prepare_values[] = $limit;
677677
$prepare_values[] = $offset;
678678

679+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
679680
return $wpdb->get_results(
680681
$wpdb->prepare(
681682
"SELECT l.*, s.name as backup_name
@@ -693,6 +694,7 @@ static function get_logs($backup_id = null, $limit = 100, $offset = 0) {
693694
static function clean_logs($days_to_keep = 30) {
694695
global $wpdb;
695696

697+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
696698
$deleted = $wpdb->query(
697699
$wpdb->prepare(
698700
"DELETE FROM {$wpdb->prefix}hejbit_logs
@@ -704,6 +706,8 @@ static function clean_logs($days_to_keep = 30) {
704706
}
705707
};
706708

709+
// phpcs:enable WordPress.DB
710+
707711
// Admin view
708712
$save_to_nextcloud = new hejbit_save_to_nextcloud();
709713
register_activation_hook(__FILE__, array($save_to_nextcloud, 'activate'));
@@ -953,6 +957,7 @@ function hejbit_get_all_saves()
953957
$result = array();
954958

955959
// Execute the query to retrieve all backups
960+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
956961
$allSaves = $wpdb->get_results(
957962
"SELECT * FROM {$wpdb->prefix}hejbit_saveInProgress"
958963
);
@@ -1197,11 +1202,15 @@ function hejbit_savetonextcloud_param()
11971202
// Logs page
11981203
function hejbit_logs_page() {
11991204
// Handle log cleanup if requested
1200-
if (isset($_POST['clear_old_logs']) && wp_verify_nonce($_POST['hejbit_logs_nonce'], 'hejbit_clear_logs')) {
1201-
$days = intval($_POST['days_to_keep']);
1202-
$deleted = hejbit_save_to_nextcloud::clean_logs($days);
1203-
echo '<div class="notice notice-success"><p>' . sprintf('Deleted %d old log entries.', $deleted) . '</p></div>';
1204-
}
1205+
if (
1206+
isset($_POST['clear_old_logs']) &&
1207+
isset($_POST['hejbit_logs_nonce']) &&
1208+
wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['hejbit_logs_nonce'])), 'hejbit_clear_logs')
1209+
) {
1210+
$days = isset($_POST['days_to_keep']) ? intval($_POST['days_to_keep']) : 30;
1211+
$deleted = hejbit_save_to_nextcloud::clean_logs($days);
1212+
echo '<div class="notice notice-success"><p>' . sprintf('Deleted %d old log entries.', esc_html($deleted)) . '</p></div>';
1213+
}
12051214

12061215
// Pagination
12071216
$page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
@@ -1212,9 +1221,19 @@ function hejbit_logs_page() {
12121221
$logs = hejbit_save_to_nextcloud::get_logs(null, $per_page, $offset);
12131222

12141223
// Get total count for pagination
1215-
global $wpdb;
1216-
$total_logs = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}hejbit_logs");
1217-
$total_pages = ceil($total_logs / $per_page);
1224+
global $wpdb;
1225+
// Try to get cached value first
1226+
$cache_key = 'hejbit_total_logs_count';
1227+
$total_logs = wp_cache_get($cache_key, 'hejbit_logs');
1228+
if (false === $total_logs) {
1229+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
1230+
$total_logs = $wpdb->get_var(
1231+
"SELECT COUNT(*) FROM {$wpdb->prefix}hejbit_logs"
1232+
);
1233+
wp_cache_set($cache_key, $total_logs, 'hejbit_logs', 300); // Cache for 5 minutes
1234+
}
1235+
1236+
$total_pages = ceil($total_logs / $per_page);
12181237
?>
12191238

12201239
<div class="wrap">
@@ -1297,14 +1316,17 @@ function hejbit_logs_page() {
12971316
<div class="tablenav bottom">
12981317
<div class="tablenav-pages">
12991318
<?php
1300-
echo paginate_links(array(
1301-
'base' => add_query_arg('paged', '%#%'),
1302-
'format' => '',
1303-
'prev_text' => '&laquo;',
1304-
'next_text' => '&raquo;',
1305-
'total' => $total_pages,
1306-
'current' => $page
1307-
));
1319+
$pagination_links = paginate_links(array(
1320+
'base' => add_query_arg('paged', '%#%'),
1321+
'format' => '',
1322+
'prev_text' => '&laquo;',
1323+
'next_text' => '&raquo;',
1324+
'total' => $total_pages,
1325+
'current' => $page
1326+
));
1327+
if ($pagination_links) {
1328+
echo wp_kses_post($pagination_links);
1329+
}
13081330
?>
13091331
</div>
13101332
</div>
@@ -1318,5 +1340,4 @@ function hejbit_logs_page() {
13181340
</style>
13191341
<?php
13201342
}
1321-
13221343
?>

trunk/inc/CreateDB.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
$wp_filesystem->put_contents($dbfile, $drop_table_sql, FS_CHMOD_FILE | FILE_APPEND);
8989

9090
// Retrieve the table creation script
91-
$createTable = $wpdb->get_row("SHOW CREATE TABLE `" . esc_sql($table) . "`", ARRAY_N);
91+
$createTable = $wpdb->get_row($wpdb->prepare("SHOW CREATE TABLE `%s`", $table), ARRAY_N);
9292
$wp_filesystem->put_contents($dbfile, $createTable[1] . ";\n\n", FS_CHMOD_FILE | FILE_APPEND);
9393

9494
// Retrieve the table data

trunk/inc/SendChunk.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
)
6565
);
6666

67+
if ($thisChunk === false) {
68+
hejbit_save_to_nextcloud::log('Failed to read file chunk', 'ERROR', 'SEND_CHUNK');
69+
return;
70+
}
71+
6772
// While the file is not completely read
6873
if (!empty($thisChunk)) {
6974

0 commit comments

Comments
 (0)