-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzen-backup.php
More file actions
241 lines (213 loc) · 7.66 KB
/
Copy pathzen-backup.php
File metadata and controls
241 lines (213 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* Plugin Name: Zen Backup
* Plugin URI: https://wordpress.org/plugins/zen-backup/
* Description: Production-grade WordPress backup plugin with streaming archives,
* AES-256-GCM encryption, and Action Scheduler pipeline.
* Version: 1.0.1
* Author: Themexplosion
* Author URI: https://github.com/arafatjamil01
* License: GPL-2.0-or-later
* Text Domain: zen-backup
* Domain Path: /languages
*/
// phpcs:disable PSR12.Files.SideEffects
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Define constants.
define( 'ZENBU_VERSION', '1.0.2' );
define( 'ZENBU_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'ZENBU_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'ZENBU_KEY_DERIVATION_ITERATIONS', 100000 );
define( 'ZENBU_ENCRYPTION_CHUNK_SIZE', 1048576 ); // 1MB
define( 'ZENBU_MANIFEST_SAVE_INTERVAL', 100 );
define( 'ZENBU_MAX_ENUM_FILES', 500000 );
define( 'ZENBU_MAX_CONCURRENT_OPS', 5 );
$zenbu_upload_dir = wp_upload_dir();
define( 'ZENBU_BASE_PATH', $zenbu_upload_dir['basedir'] . '/zen-backup' );
define( 'ZENBU_STORAGE_PATH', ZENBU_BASE_PATH . '/storage' );
define( 'ZENBU_BACKUPS_PATH', ZENBU_BASE_PATH . '/backups' );
define( 'ZENBU_MANIFESTS_PATH', ZENBU_BASE_PATH . '/manifests' );
unset( $zenbu_upload_dir );
define( 'ZENBU_ENCRYPTION_MODE_ARCHIVE', 'archive' );
define( 'ZENBU_ENCRYPTION_MODE_NONE', 'none' );
define( 'ZENBU_MAX_BACKUP_SIZE', 1073741824 ); // 1GB
// Minimum requirements check.
if ( PHP_VERSION_ID < 70400 ) {
add_action(
'admin_notices',
function () {
echo '<div class="notice notice-error"><p>';
esc_html_e(
'Zen Backup requires PHP 7.4 or higher. Please upgrade your PHP version.',
'zen-backup'
);
echo '</p></div>';
}
);
return;
}
require_once ZENBU_PLUGIN_DIR . 'includes/exceptions/class-storageexception.php';
require_once ZENBU_PLUGIN_DIR . 'includes/exceptions/class-archiveexception.php';
require_once ZENBU_PLUGIN_DIR . 'includes/exceptions/class-encryptionexception.php';
require_once ZENBU_PLUGIN_DIR . 'includes/services/class-pathvalidator.php';
require_once ZENBU_PLUGIN_DIR . 'includes/services/class-status.php';
require_once ZENBU_PLUGIN_DIR . 'includes/services/class-filechunker.php';
require_once ZENBU_PLUGIN_DIR . 'includes/services/class-wpfilesystemadapter.php';
require_once ZENBU_PLUGIN_DIR . 'includes/services/class-manifeststorage.php';
require_once ZENBU_PLUGIN_DIR . 'includes/services/class-fileenumerator.php';
require_once ZENBU_PLUGIN_DIR . 'includes/services/class-pclziparchiver.php';
require_once ZENBU_PLUGIN_DIR . 'includes/services/class-ziparchiver.php';
require_once ZENBU_PLUGIN_DIR . 'includes/services/class-encryptionservice.php';
require_once ZENBU_PLUGIN_DIR . 'includes/services/class-storageguard.php';
require_once ZENBU_PLUGIN_DIR . 'includes/services/class-backuplock.php';
require_once ZENBU_PLUGIN_DIR . 'includes/services/class-ratelimiter.php';
require_once ZENBU_PLUGIN_DIR . 'includes/services/class-backupnamer.php';
require_once ZENBU_PLUGIN_DIR . 'includes/services/class-downloadtoken.php';
require_once ZENBU_PLUGIN_DIR . 'includes/pipeline/steps/class-exportinit.php';
require_once ZENBU_PLUGIN_DIR . 'includes/pipeline/steps/class-exportenumeratecontent.php';
require_once ZENBU_PLUGIN_DIR . 'includes/pipeline/steps/class-exportcontent.php';
require_once ZENBU_PLUGIN_DIR . 'includes/pipeline/steps/class-exportchecksum.php';
require_once ZENBU_PLUGIN_DIR . 'includes/pipeline/steps/class-exportdownload.php';
require_once ZENBU_PLUGIN_DIR . 'includes/pipeline/steps/class-exportclean.php';
require_once ZENBU_PLUGIN_DIR . 'includes/pipeline/class-pipelinerunner.php';
require_once ZENBU_PLUGIN_DIR . 'includes/rest/class-exportcontroller.php';
require_once ZENBU_PLUGIN_DIR . 'includes/rest/class-importcontroller.php';
require_once ZENBU_PLUGIN_DIR . 'includes/rest/class-backupcontroller.php';
require_once ZENBU_PLUGIN_DIR . 'includes/addons/interface-addon.php';
require_once ZENBU_PLUGIN_DIR . 'includes/addons/class-addonregistry.php';
require_once ZENBU_PLUGIN_DIR . 'includes/addons/class-addonloader.php';
require_once ZENBU_PLUGIN_DIR . 'includes/addons/class-addoncontroller.php';
require_once ZENBU_PLUGIN_DIR . 'includes/addons/functions.php';
// Admin notice if no archiving engine is available.
if ( ! class_exists( 'ZipArchive' ) && ! file_exists( ABSPATH . 'wp-admin/includes/class-pclzip.php' ) ) {
add_action(
'admin_notices',
function () {
echo '<div class="notice notice-error"><p>';
esc_html_e(
'Zen Backup requires either the ZipArchive PHP extension or the PclZip library. Neither was found on your server.',
'zen-backup'
);
echo '</p></div>';
}
);
}
add_action(
'plugins_loaded',
function () {
if ( ! get_transient( 'zenbu_storage_cleanup_lock' ) ) {
set_transient( 'zenbu_storage_cleanup_lock', 1, HOUR_IN_SECONDS );
ZenBackup\StorageGuard::clean_stale();
}
},
999
);
add_action(
'admin_menu',
function () {
add_menu_page(
__( 'Zen Backup', 'zen-backup' ),
__( 'Zen Backup', 'zen-backup' ),
'manage_options',
'zen-backup',
'zenbu_admin_page',
'dashicons-database-view',
80
);
}
);
add_action(
'admin_enqueue_scripts',
function ( $hook ) {
if ( 'toplevel_page_zen-backup' !== $hook ) {
return;
}
wp_enqueue_style( 'zenbu-admin', ZENBU_PLUGIN_URL . 'assets/css/admin.css', array(), ZENBU_VERSION );
wp_enqueue_script( 'zenbu-admin', ZENBU_PLUGIN_URL . 'assets/js/admin.js', array(), ZENBU_VERSION, true );
wp_localize_script(
'zenbu-admin',
'zenbuApi',
array(
'root' => esc_url_raw( rest_url( 'zen-backup/v1/' ) ),
'nonce' => wp_create_nonce( 'wp_rest' ),
)
);
}
);
ZenBackup\AddonLoader::init();
add_action(
'rest_api_init',
function () {
ZenBackup\ExportController::register_routes();
ZenBackup\ImportController::register_routes();
ZenBackup\BackupController::register_routes();
ZenBackup\AddonController::register_routes();
}
);
function zenbu_admin_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
include ZENBU_PLUGIN_DIR . 'includes/admin-page.php';
}
// Cron handler for pipeline execution.
add_action(
'zenbu_pipeline_run',
function ( $job_id, $params = array() ) {
$run_params = ! empty( $params ) ? $params : array();
ZenBackup\PipelineRunner::run( $job_id, $run_params );
},
10,
2
);
// Handle download token requests on frontend.
add_action(
'template_redirect',
function () {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['zenbu_download'] ) && isset( $_GET['zenbu_file'] ) ) {
ZenBackup\BackupController::handle_download();
}
}
);
// Activation — migrate old folders, then ensure directories exist.
register_activation_hook(
__FILE__,
function () {
// Migrate from old wp-content/zenbu location to uploads/zen-backup.
$legacy_base = WP_CONTENT_DIR . '/zenbu';
if ( is_dir( $legacy_base ) ) {
wp_mkdir_p( ZENBU_BASE_PATH );
$subfolders = array( 'storage', 'backups' );
foreach ( $subfolders as $sub ) {
$old = $legacy_base . '/' . $sub;
$new = ZENBU_BASE_PATH . '/' . $sub;
if ( is_dir( $old ) && ! is_dir( $new ) ) {
rename( $old, $new );
}
}
}
// Migrate pre-zenbu/ legacy top-level folders.
$legacy = array(
WP_CONTENT_DIR . '/zenbu-storage' => ZENBU_STORAGE_PATH,
WP_CONTENT_DIR . '/zenbu-backups' => ZENBU_BACKUPS_PATH,
WP_CONTENT_DIR . '/zenbu-manifests' => ZENBU_MANIFESTS_PATH,
);
foreach ( $legacy as $old => $new ) {
if ( is_dir( $old ) && ! is_dir( $new ) ) {
wp_mkdir_p( dirname( $new ) );
rename( $old, $new );
}
}
ZenBackup\StorageGuard::ensure_dirs();
}
);
// Deactivation — clean up cron.
register_deactivation_hook(
__FILE__,
function () {
wp_clear_scheduled_hook( 'zenbu_storage_cleanup' );
}
);