PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.1
6.2.2 6.2.1 6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 All 155 releases
← All changes | class/class-mainwp-backup-handler.php +1092 -1054 5.0.1 → 6.2.1 View file →
@@ -6,1200 +6,1238 @@
6 6 */
7 7
8 8 namespace MainWP\Dashboard;
9 9
10 +// Exit if accessed directly.
11 +if ( ! defined( 'ABSPATH' ) ) {
12 + exit;
13 +}
14 +
10 15 /**
11 16 * Class MainWP_Backup_Handler
12 17 *
13 18 * @package MainWP\Dashboard
14 19 */
15 -class MainWP_Backup_Handler {
20 +class MainWP_Backup_Handler { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
16 21
17 - /**
18 - * Method get_class_name()
19 - *
20 - * Get Class Name.
21 - *
22 - * @return string __CLASS__
23 - */
24 - public static function get_class_name() {
25 - return __CLASS__;
26 - }
22 + /**
23 + * Method get_class_name()
24 + *
25 + * Get Class Name.
26 + *
27 + * @return string __CLASS__
28 + */
29 + public static function get_class_name() {
30 + return __CLASS__;
31 + }
27 32
28 - /**
29 - * Method backup_site()
30 - *
31 - * Backup Child Site.
32 - *
33 - * @param mixed $siteid Child Site ID.
34 - * @param mixed $pTask Task to perform.
35 - * @param mixed $subfolder Sub folder to place backup.
36 - *
37 - * @gobal object $wp_filesystem WordPress filesystem instance.
38 - *
39 - * @return mixed $backup_result
40 - * @throws MainWP_Exception Error message.
41 - *
42 - * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
43 - * @uses \MainWP\Dashboard\MainWP_Connect::download_to_file()
44 - * @uses \MainWP\Dashboard\MainWP_Connect::get_get_data_authed()
45 - * @uses \MainWP\Dashboard\MainWP_DB_Backup::get_backup_task_progress()
46 - * @uses \MainWP\Dashboard\MainWP_DB_Backup::add_backup_task_progress()
47 - * @uses \MainWP\Dashboard\MainWP_DB_Backup::update_backup_task_progress()
48 - * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
49 - * @uses \MainWP\Dashboard\MainWP_Exception
50 - * @uses \MainWP\Dashboard\MainWP_Logger::debug_for_website()
51 - * @uses \MainWP\Dashboard\MainWP_Logger::warning_for_website()
52 - * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
53 - * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
54 - * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_specific_dir()
55 - * @uses \MainWP\Dashboard\MainWP_Utility::sanitize()
56 - * @uses \MainWP\Dashboard\MainWP_Utility::date()
57 - * @uses \MainWP\Dashboard\MainWP_Utility::remove_preslash_spaces()
58 - * @uses \MainWP\Dashboard\MainWP_Utility::normalize_filename()
59 - * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
60 - * @uses \MainWP\Dashboard\self::get_real_extension()
61 - */
62 - public static function backup_site( $siteid, $pTask, $subfolder ) {
63 - // phpcs: ignore -- Current complexity is the only way to achieve desired results, pull request solutions appreciated.
64 - if ( ! get_option( 'mainwp_enableLegacyBackupFeature' ) ) {
65 - return false;
66 - }
33 + /**
34 + * Method backup_site()
35 + *
36 + * Backup Child Site.
37 + *
38 + * @param mixed $siteid Child Site ID.
39 + * @param mixed $pTask Task to perform.
40 + * @param mixed $subfolder Sub folder to place backup.
41 + *
42 + * @gobal object $wp_filesystem WordPress filesystem instance.
43 + *
44 + * @return mixed $backup_result
45 + * @throws MainWP_Exception Error message.
46 + *
47 + * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
48 + * @uses \MainWP\Dashboard\MainWP_Connect::download_to_file()
49 + * @uses \MainWP\Dashboard\MainWP_Connect::get_get_data_authed()
50 + * @uses \MainWP\Dashboard\MainWP_DB_Backup::get_backup_task_progress()
51 + * @uses \MainWP\Dashboard\MainWP_DB_Backup::add_backup_task_progress()
52 + * @uses \MainWP\Dashboard\MainWP_DB_Backup::update_backup_task_progress()
53 + * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
54 + * @uses \MainWP\Dashboard\MainWP_Exception
55 + * @uses \MainWP\Dashboard\MainWP_Logger::debug_for_website()
56 + * @uses \MainWP\Dashboard\MainWP_Logger::warning_for_website()
57 + * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
58 + * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
59 + * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_specific_dir()
60 + * @uses \MainWP\Dashboard\MainWP_Utility::sanitize()
61 + * @uses \MainWP\Dashboard\MainWP_Utility::date()
62 + * @uses \MainWP\Dashboard\MainWP_Utility::remove_preslash_spaces()
63 + * @uses \MainWP\Dashboard\static::normalize_filename()
64 + * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
65 + * @uses \MainWP\Dashboard\static::get_real_extension()
66 + */
67 + public static function backup_site( $siteid, $pTask, $subfolder ) {
68 + // phpcs: ignore -- Current complexity is the only way to achieve desired results, pull request solutions appreciated.
69 + if ( ! get_option( 'mainwp_enableLegacyBackupFeature' ) ) {
70 + return false;
71 + }
67 72
68 - $userid = $pTask->userid;
69 - $type = $pTask->type;
70 - $exclude = $pTask->exclude;
71 - $taskId = $pTask->id;
72 - $excludebackup = $pTask->excludebackup;
73 - $excludecache = $pTask->excludecache;
74 - $excludenonwp = $pTask->excludenonwp;
75 - $excludezip = $pTask->excludezip;
76 - $pFilename = $pTask->filename;
73 + $userid = $pTask->userid;
74 + $type = $pTask->type;
75 + $exclude = $pTask->exclude;
76 + $taskId = $pTask->id;
77 + $excludebackup = $pTask->excludebackup;
78 + $excludecache = $pTask->excludecache;
79 + $excludenonwp = $pTask->excludenonwp;
80 + $excludezip = $pTask->excludezip;
81 + $pFilename = $pTask->filename;
77 82
78 - if ( '' === trim( $pFilename ) ) {
79 - $pFilename = null;
80 - }
83 + if ( '' === trim( $pFilename ) ) {
84 + $pFilename = null;
85 + }
81 86
82 - $backup_result = array();
87 + $backup_result = array();
83 88
84 - // Creating a backup.
85 - $website = MainWP_DB::instance()->get_website_by_id( $siteid );
86 - $subfolder = str_replace( '%sitename%', MainWP_Utility::sanitize( $website->name ), $subfolder );
87 - $subfolder = str_replace( '%url%', MainWP_Utility::sanitize( MainWP_Utility::get_nice_url( $website->url ) ), $subfolder );
88 - $subfolder = str_replace( '%type%', $type, $subfolder );
89 - $subfolder = str_replace( '%date%', MainWP_Utility::date( 'Ymd' ), $subfolder );
90 - $subfolder = str_replace( '%task%', '', $subfolder );
91 - $subfolder = str_replace( '%', '', $subfolder );
92 - $subfolder = MainWP_Utility::remove_preslash_spaces( $subfolder );
93 - $subfolder = MainWP_Utility::normalize_filename( $subfolder );
89 + // Creating a backup.
90 + $website = MainWP_DB::instance()->get_website_by_id( $siteid );
91 + $subfolder = str_replace( '%sitename%', MainWP_Utility::sanitize( $website->name ), $subfolder );
92 + $subfolder = str_replace( '%url%', MainWP_Utility::sanitize( MainWP_Utility::get_nice_url( $website->url ) ), $subfolder );
93 + $subfolder = str_replace( '%type%', $type, $subfolder );
94 + $subfolder = str_replace( '%date%', MainWP_Utility::date( 'Ymd' ), $subfolder );
95 + $subfolder = str_replace( '%task%', '', $subfolder );
96 + $subfolder = str_replace( '%', '', $subfolder );
97 + $subfolder = MainWP_Utility::remove_preslash_spaces( $subfolder );
98 + $subfolder = static::normalize_filename( $subfolder );
94 99
95 - if ( ! MainWP_System::instance()->is_single_user() && ( $userid !== $website->userid ) ) {
96 - throw new MainWP_Exception( 'Undefined error.' );
97 - }
100 + if ( ! MainWP_System::instance()->is_single_user() && ( $userid !== $website->userid ) ) {
101 + throw new MainWP_Exception( 'Undefined error.' );
102 + }
98 103
99 - $websiteCleanUrl = $website->url;
100 - if ( '/' === substr( $websiteCleanUrl, - 1 ) ) {
101 - $websiteCleanUrl = substr( $websiteCleanUrl, 0, - 1 );
102 - }
103 - $websiteCleanUrl = str_replace(
104 - array( 'http://', 'https://', '/' ),
105 - array( '', '', '-' ),
106 - $websiteCleanUrl
107 - );
104 + $websiteCleanUrl = $website->url;
105 + if ( '/' === substr( $websiteCleanUrl, - 1 ) ) {
106 + $websiteCleanUrl = substr( $websiteCleanUrl, 0, - 1 );
107 + }
108 + $websiteCleanUrl = str_replace(
109 + array( 'http://', 'https://', '/' ),
110 + array( '', '', '-' ),
111 + $websiteCleanUrl
112 + );
108 113
109 - if ( 'db' === $type ) {
110 - $ext = '.sql.' . self::get_current_archive_extension( $website, $pTask );
111 - } else {
112 - $ext = '.' . self::get_current_archive_extension( $website, $pTask );
113 - }
114 + if ( 'db' === $type ) {
115 + $ext = '.sql.' . static::get_current_archive_extension( $website, $pTask );
116 + } else {
117 + $ext = '.' . static::get_current_archive_extension( $website, $pTask );
118 + }
114 119
115 - $file = str_replace(
116 - array(
117 - '%sitename%',
118 - '%url%',
119 - '%date%',
120 - '%time%',
121 - '%type%',
122 - ),
123 - array(
124 - MainWP_Utility::sanitize( $website->name ),
125 - $websiteCleanUrl,
126 - MainWP_Utility::date( 'm-d-Y' ),
127 - MainWP_Utility::date( 'G\hi\ms\s' ),
128 - $type,
129 - ),
130 - $pFilename
131 - );
132 - $file = str_replace( '%', '', $file );
133 - $file = MainWP_Utility::normalize_filename( $file );
120 + $file = str_replace(
121 + array(
122 + '%sitename%',
123 + '%url%',
124 + '%date%',
125 + '%time%',
126 + '%type%',
127 + ),
128 + array(
129 + MainWP_Utility::sanitize( $website->name ),
130 + $websiteCleanUrl,
131 + MainWP_Utility::date( 'm-d-Y' ),
132 + MainWP_Utility::date( 'G\hi\ms\s' ),
133 + $type,
134 + ),
135 + $pFilename
136 + );
137 + $file = str_replace( '%', '', $file );
138 + $file = static::normalize_filename( $file );
134 139
135 - if ( ! empty( $file ) ) {
136 - $file .= $ext;
137 - }
140 + if ( ! empty( $file ) ) {
141 + $file .= $ext;
142 + }
138 143
139 - if ( 'zip' === $pTask->archiveFormat ) {
140 - $loadFilesBeforeZip = (int) $pTask->loadFilesBeforeZip;
141 - } elseif ( '' === $pTask->archiveFormat || 'site' === $pTask->archiveFormat ) {
142 - $loadFilesBeforeZip = (int) $website->loadFilesBeforeZip;
143 - } else {
144 - $loadFilesBeforeZip = 1;
145 - }
144 + if ( 'zip' === $pTask->archiveFormat ) {
145 + $loadFilesBeforeZip = (int) $pTask->loadFilesBeforeZip;
146 + } elseif ( '' === $pTask->archiveFormat || 'site' === $pTask->archiveFormat ) {
147 + $loadFilesBeforeZip = (int) $website->loadFilesBeforeZip;
148 + } else {
149 + $loadFilesBeforeZip = 1;
150 + }
146 151
147 - if ( 1 === $loadFilesBeforeZip ) {
148 - $loadFilesBeforeZip = get_option( 'mainwp_options_loadFilesBeforeZip' );
149 - $loadFilesBeforeZip = ( 1 === (int) $loadFilesBeforeZip || false === $loadFilesBeforeZip );
150 - } else {
151 - $loadFilesBeforeZip = ( 2 === $loadFilesBeforeZip );
152 - }
152 + if ( 1 === $loadFilesBeforeZip ) {
153 + $loadFilesBeforeZip = get_option( 'mainwp_options_loadFilesBeforeZip' );
154 + $loadFilesBeforeZip = ( 1 === (int) $loadFilesBeforeZip || false === $loadFilesBeforeZip );
155 + } else {
156 + $loadFilesBeforeZip = ( 2 === $loadFilesBeforeZip );
157 + }
153 158
154 - if ( ( 'zip' === $pTask->archiveFormat ) && ( 1 === (int) $pTask->maximumFileDescriptorsOverride ) ) {
155 - $maximumFileDescriptorsAuto = ( 1 === (int) $pTask->maximumFileDescriptorsAuto );
156 - $maximumFileDescriptors = $pTask->maximumFileDescriptors;
157 - } elseif ( ( '' === $pTask->archiveFormat || 'site' === $pTask->archiveFormat ) && ( 1 === (int) $website->maximumFileDescriptorsOverride ) ) {
158 - $maximumFileDescriptorsAuto = ( 1 === (int) $website->maximumFileDescriptorsAuto );
159 - $maximumFileDescriptors = $website->maximumFileDescriptors;
160 - } else {
161 - $maximumFileDescriptorsAuto = get_option( 'mainwp_maximumFileDescriptorsAuto' );
162 - $maximumFileDescriptors = get_option( 'mainwp_maximumFileDescriptors' );
163 - $maximumFileDescriptors = ( false === $maximumFileDescriptors ? 150 : $maximumFileDescriptors );
164 - }
159 + if ( ( 'zip' === $pTask->archiveFormat ) && ( 1 === (int) $pTask->maximumFileDescriptorsOverride ) ) {
160 + $maximumFileDescriptorsAuto = ( 1 === (int) $pTask->maximumFileDescriptorsAuto );
161 + $maximumFileDescriptors = $pTask->maximumFileDescriptors;
162 + } elseif ( ( '' === $pTask->archiveFormat || 'site' === $pTask->archiveFormat ) && ( 1 === (int) $website->maximumFileDescriptorsOverride ) ) {
163 + $maximumFileDescriptorsAuto = ( 1 === (int) $website->maximumFileDescriptorsAuto );
164 + $maximumFileDescriptors = $website->maximumFileDescriptors;
165 + } else {
166 + $maximumFileDescriptorsAuto = get_option( 'mainwp_maximumFileDescriptorsAuto' );
167 + $maximumFileDescriptors = get_option( 'mainwp_maximumFileDescriptors' );
168 + $maximumFileDescriptors = ( false === $maximumFileDescriptors ? 150 : $maximumFileDescriptors );
169 + }
165 170
166 - $information = false;
167 - $backupTaskProgress = MainWP_DB_Backup::instance()->get_backup_task_progress( $taskId, $website->id );
168 - if ( empty( $backupTaskProgress ) || ( $backupTaskProgress->dtsFetched < $pTask->last_run ) ) {
169 - $start = microtime( true );
170 - try {
171 - $pid = time();
171 + $information = false;
172 + $backupTaskProgress = MainWP_DB_Backup::instance()->get_backup_task_progress( $taskId, $website->id );
173 + if ( empty( $backupTaskProgress ) || ( $backupTaskProgress->dtsFetched < $pTask->last_run ) ) {
174 + $start = microtime( true );
175 + try {
176 + $pid = time();
172 177
173 - if ( empty( $backupTaskProgress ) ) {
174 - MainWP_DB_Backup::instance()->add_backup_task_progress( $taskId, $website->id, array() );
175 - }
178 + if ( empty( $backupTaskProgress ) ) {
179 + MainWP_DB_Backup::instance()->add_backup_task_progress( $taskId, $website->id, array() );
180 + }
176 181
177 - MainWP_DB_Backup::instance()->update_backup_task_progress(
178 - $taskId,
179 - $website->id,
180 - array(
181 - 'dtsFetched' => time(),
182 - 'fetchResult' => wp_json_encode( array() ),
183 - 'downloadedDB' => '',
184 - 'downloadedDBComplete' => 0,
185 - 'downloadedFULL' => '',
186 - 'downloadedFULLComplete' => 0,
187 - 'removedFiles' => 0,
188 - 'attempts' => 0,
189 - 'last_error' => '',
190 - 'pid' => $pid,
191 - )
192 - );
182 + MainWP_DB_Backup::instance()->update_backup_task_progress(
183 + $taskId,
184 + $website->id,
185 + array(
186 + 'dtsFetched' => time(),
187 + 'fetchResult' => wp_json_encode( array() ),
188 + 'downloadedDB' => '',
189 + 'downloadedDBComplete' => 0,
190 + 'downloadedFULL' => '',
191 + 'downloadedFULLComplete' => 0,
192 + 'removedFiles' => 0,
193 + 'attempts' => 0,
194 + 'last_error' => '',
195 + 'pid' => $pid,
196 + )
197 + );
193 198
194 - $params = array(
195 - 'type' => $type,
196 - 'exclude' => $exclude,
197 - 'excludebackup' => $excludebackup,
198 - 'excludecache' => $excludecache,
199 - 'excludenonwp' => $excludenonwp,
200 - 'excludezip' => $excludezip,
201 - 'ext' => self::get_current_archive_extension( $website, $pTask ),
202 - 'file_descriptors_auto' => $maximumFileDescriptorsAuto,
203 - 'file_descriptors' => $maximumFileDescriptors,
204 - 'loadFilesBeforeZip' => $loadFilesBeforeZip,
205 - 'pid' => $pid,
206 - 'f' => $file,
207 - );
199 + $params = array(
200 + 'type' => $type,
201 + 'exclude' => $exclude,
202 + 'excludebackup' => $excludebackup,
203 + 'excludecache' => $excludecache,
204 + 'excludenonwp' => $excludenonwp,
205 + 'excludezip' => $excludezip,
206 + 'ext' => static::get_current_archive_extension( $website, $pTask ),
207 + 'file_descriptors_auto' => $maximumFileDescriptorsAuto,
208 + 'file_descriptors' => $maximumFileDescriptors,
209 + 'loadFilesBeforeZip' => $loadFilesBeforeZip,
210 + 'pid' => $pid,
211 + 'f' => $file,
212 + );
208 213
209 - MainWP_Logger::instance()->debug_for_website( $website, 'backup', 'Requesting backup: ' . MainWP_Utility::value_to_string( $params, 1 ) );
214 + MainWP_Logger::instance()->debug_for_website( $website, 'backup', 'Requesting backup: ' . MainWP_Utility::value_to_string( $params, 1 ) );
210 215
211 - $information = MainWP_Connect::fetch_url_authed( $website, 'backup', $params, false, false, false );
212 - } catch ( MainWP_Exception $e ) {
213 - MainWP_Logger::instance()->warning_for_website( $website, 'backup', 'ERROR: ' . $e->getMessage() . ' (' . $e->get_message_extra() . ')' );
214 - $stop = microtime( true );
215 - // Bigger then 30 seconds means a timeout.
216 - if ( 30 < ( $stop - $start ) ) {
217 - MainWP_DB_Backup::instance()->update_backup_task_progress(
218 - $taskId,
219 - $website->id,
220 - array(
221 - 'last_error' => wp_json_encode(
222 - array(
223 - 'message' => $e->getMessage(),
224 - 'extra' => $e->get_message_extra(),
225 - )
226 - ),
227 - )
228 - );
216 + $information = MainWP_Connect::fetch_url_authed( $website, 'backup', $params, false, false, false );
217 + } catch ( MainWP_Exception $e ) {
218 + MainWP_Logger::instance()->warning_for_website( $website, 'backup', 'ERROR: ' . $e->getMessage() . ' (' . $e->get_message_extra() . ')' );
219 + $stop = microtime( true );
220 + // Bigger then 30 seconds means a timeout.
221 + if ( 30 < ( $stop - $start ) ) {
222 + MainWP_DB_Backup::instance()->update_backup_task_progress(
223 + $taskId,
224 + $website->id,
225 + array(
226 + 'last_error' => wp_json_encode(
227 + array(
228 + 'message' => $e->getMessage(),
229 + 'extra' => $e->get_message_extra(),
230 + )
231 + ),
232 + )
233 + );
229 234
230 - return false;
231 - }
235 + return false;
236 + }
232 237
233 - throw $e;
234 - }
238 + throw $e;
239 + }
235 240
236 - if ( isset( $information['error'] ) && stristr( $information['error'], 'Another backup process is running' ) ) {
237 - return false;
238 - }
241 + if ( isset( $information['error'] ) && stristr( $information['error'], 'Another backup process is running' ) ) {
242 + return false;
243 + }
239 244
240 - $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'fetchResult' => wp_json_encode( $information ) ) );
241 - } elseif ( empty( $backupTaskProgress->fetchResult ) ) {
242 - try {
243 - $temp = MainWP_Connect::fetch_url_authed( $website, 'backup_checkpid', array( 'pid' => $backupTaskProgress->pid ) );
244 - } catch ( \Exception $e ) {
245 - // ok!
246 - }
245 + $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'fetchResult' => wp_json_encode( $information ) ) );
246 + } elseif ( empty( $backupTaskProgress->fetchResult ) ) {
247 + try {
248 + $temp = MainWP_Connect::fetch_url_authed( $website, 'backup_checkpid', array( 'pid' => $backupTaskProgress->pid ) );
249 + } catch ( \Exception $e ) {
250 + // ok!
251 + }
247 252
248 - if ( ! empty( $temp ) ) {
249 - if ( 'stalled' === $temp['status'] ) {
250 - if ( 5 > $backupTaskProgress->attempts ) {
251 - $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'attempts' => $backupTaskProgress->attempts ++ ) );
253 + if ( ! empty( $temp ) ) {
254 + if ( 'stalled' === $temp['status'] ) {
255 + if ( 5 > $backupTaskProgress->attempts ) {
256 + $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'attempts' => $backupTaskProgress->attempts++ ) );
252 257
253 - try {
254 - $information = MainWP_Connect::fetch_url_authed(
255 - $website,
256 - 'backup',
257 - array(
258 - 'type' => $type,
259 - 'exclude' => $exclude,
260 - 'excludebackup' => $excludebackup,
261 - 'excludecache' => $excludecache,
262 - 'excludenonwp' => $excludenonwp,
263 - 'excludezip' => $excludezip,
264 - 'ext' => self::get_current_archive_extension( $website, $pTask ),
265 - 'file_descriptors_auto' => $maximumFileDescriptorsAuto,
266 - 'file_descriptors' => $maximumFileDescriptors,
267 - 'loadFilesBeforeZip' => $loadFilesBeforeZip,
268 - 'pid' => $backupTaskProgress->pid,
269 - 'append' => '1',
270 - 'f' => $temp['file'],
271 - ),
272 - false,
273 - false,
274 - false
275 - );
258 + try {
259 + $information = MainWP_Connect::fetch_url_authed(
260 + $website,
261 + 'backup',
262 + array(
263 + 'type' => $type,
264 + 'exclude' => $exclude,
265 + 'excludebackup' => $excludebackup,
266 + 'excludecache' => $excludecache,
267 + 'excludenonwp' => $excludenonwp,
268 + 'excludezip' => $excludezip,
269 + 'ext' => static::get_current_archive_extension( $website, $pTask ),
270 + 'file_descriptors_auto' => $maximumFileDescriptorsAuto,
271 + 'file_descriptors' => $maximumFileDescriptors,
272 + 'loadFilesBeforeZip' => $loadFilesBeforeZip,
273 + 'pid' => $backupTaskProgress->pid,
274 + 'append' => '1',
275 + 'f' => $temp['file'],
276 + ),
277 + false,
278 + false,
279 + false
280 + );
276 281
277 - if ( isset( $information['error'] ) && stristr( $information['error'], 'Another backup process is running' ) ) {
278 - MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'attempts' => ( $backupTaskProgress->attempts - 1 ) ) );
282 + if ( isset( $information['error'] ) && stristr( $information['error'], 'Another backup process is running' ) ) {
283 + MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'attempts' => ( $backupTaskProgress->attempts - 1 ) ) );
279 284
280 - return false;
281 - }
282 - } catch ( MainWP_Exception $e ) {
283 - return false;
284 - }
285 + return false;
286 + }
287 + } catch ( MainWP_Exception $e ) {
288 + return false;
289 + }
285 290
286 - $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'fetchResult' => wp_json_encode( $information ) ) );
287 - } else {
288 - throw new MainWP_Exception( 'Backup failed after 5 retries.' );
289 - }
290 - } elseif ( 'invalid' === $temp['status'] ) {
291 - $error = json_decode( $backupTaskProgress->last_error );
291 + $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'fetchResult' => wp_json_encode( $information ) ) );
292 + } else {
293 + throw new MainWP_Exception( 'Backup failed after 5 retries.' );
294 + }
295 + } elseif ( 'invalid' === $temp['status'] ) {
296 + $error = json_decode( $backupTaskProgress->last_error );
292 297
293 - if ( ! is_array( $error ) ) {
294 - throw new MainWP_Exception( 'Backup failed.' );
295 - } else {
296 - throw new MainWP_Exception( $error['message'], $error['extra'] ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
297 - }
298 - } elseif ( 'busy' === $temp['status'] ) {
299 - return false;
300 - } elseif ( 'done' === $temp['status'] ) {
301 - if ( 'full' === $type ) {
302 - $information['full'] = $temp['file'];
303 - $information['db'] = false;
304 - } else {
305 - $information['full'] = false;
306 - $information['db'] = $temp['file'];
307 - }
298 + if ( ! is_array( $error ) ) {
299 + throw new MainWP_Exception( 'Backup failed.' );
300 + } else {
301 + throw new MainWP_Exception( $error['message'], $error['extra'] ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
302 + }
303 + } elseif ( 'busy' === $temp['status'] ) {
304 + return false;
305 + } elseif ( 'done' === $temp['status'] ) {
306 + if ( 'full' === $type ) {
307 + $information['full'] = $temp['file'];
308 + $information['db'] = false;
309 + } else {
310 + $information['full'] = false;
311 + $information['db'] = $temp['file'];
312 + }
308 313
309 - $information['size'] = $temp['size'];
314 + $information['size'] = $temp['size'];
310 315
311 - $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'fetchResult' => wp_json_encode( $information ) ) );
312 - }
313 - } elseif ( 5 > $backupTaskProgress->attempts ) {
314 - $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'attempts' => $backupTaskProgress->attempts ++ ) );
315 - } else {
316 - throw new MainWP_Exception( 'Backup failed after 5 retries.' );
317 - }
318 - }
316 + $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'fetchResult' => wp_json_encode( $information ) ) );
317 + }
318 + } elseif ( 5 > $backupTaskProgress->attempts ) {
319 + $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'attempts' => $backupTaskProgress->attempts++ ) );
320 + } else {
321 + throw new MainWP_Exception( 'Backup failed after 5 retries.' );
322 + }
323 + }
319 324
320 - if ( false === $information ) {
321 - $information = $backupTaskProgress->fetchResult;
322 - }
325 + if ( false === $information ) {
326 + $information = $backupTaskProgress->fetchResult;
327 + }
323 328
324 - if ( isset( $information['error'] ) ) {
325 - throw new MainWP_Exception( esc_html( $information['error'] ) );
326 - } elseif ( 'db' === $type && ! $information['db'] ) {
327 - throw new MainWP_Exception( 'Database backup failed.' );
328 - } elseif ( 'full' === $type && ! $information['full'] ) {
329 - throw new MainWP_Exception( 'Full backup failed.' );
330 - } elseif ( isset( $information['db'] ) ) {
331 - $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
329 + if ( isset( $information['error'] ) ) {
330 + throw new MainWP_Exception( esc_html( $information['error'] ) );
331 + } elseif ( 'db' === $type && ! $information['db'] ) {
332 + throw new MainWP_Exception( 'Database backup failed.' );
333 + } elseif ( 'full' === $type && ! $information['full'] ) {
334 + throw new MainWP_Exception( 'Full backup failed.' );
335 + } elseif ( isset( $information['db'] ) ) {
336 + $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
332 337
333 - /**
334 - * WordPress filesystem instance.
335 - *
336 - * @global object
337 - */
338 - global $wp_filesystem;
338 + /**
339 + * WordPress filesystem instance.
340 + *
341 + * @global object
342 + */
343 + global $wp_filesystem;
339 344
340 - $dir = MainWP_System_Utility::get_mainwp_specific_dir( $website->id );
345 + $dir = MainWP_System_Utility::get_mainwp_specific_dir( $website->id );
341 346
342 - $wp_filesystem->mkdir( $dir, 0777 );
347 + $wp_filesystem->mkdir( $dir, 0750 ); // MWP-1558: tightened from 0777; per-site backup dumps.
343 348
344 - if ( ! $wp_filesystem->exists( $dir . 'index.php' ) ) {
345 - $wp_filesystem->touch( $dir . 'index.php' );
346 - }
349 + if ( ! $wp_filesystem->exists( $dir . 'index.php' ) ) {
350 + $wp_filesystem->touch( $dir . 'index.php' );
351 + }
347 352
348 - // Clean old backups from our system.
349 - $maxBackups = get_option( 'mainwp_backupsOnServer' );
350 - if ( false === $maxBackups ) {
351 - $maxBackups = 1;
352 - }
353 + // Clean old backups from our system.
354 + $maxBackups = get_option( 'mainwp_backupsOnServer' );
355 + if ( false === $maxBackups ) {
356 + $maxBackups = 1;
357 + }
353 358
354 - if ( 1 !== $backupTaskProgress->removedFiles ) {
355 - $dbBackups = array();
356 - $fullBackups = array();
357 - if ( $wp_filesystem->exists( $dir ) ) {
358 - $dh = opendir( $dir );
359 - if ( $dh ) {
360 - while ( false !== ( $file = readdir( $dh ) ) ) {
361 - if ( '.' !== $file && '..' !== $file ) {
362 - $theFile = $dir . $file;
363 - if ( $information['db'] && self::is_sql_file( $file ) ) {
364 - $dbBackups[ filemtime( $theFile ) . $file ] = $theFile;
365 - }
359 + if ( 1 !== $backupTaskProgress->removedFiles ) {
360 + $dbBackups = array();
361 + $fullBackups = array();
362 + if ( $wp_filesystem->exists( $dir ) ) {
363 + $dh = opendir( $dir );
364 + if ( $dh ) {
365 + while ( false !== ( $file = readdir( $dh ) ) ) {
366 + if ( '.' !== $file && '..' !== $file ) {
367 + $theFile = $dir . $file;
368 + if ( $information['db'] && static::is_sql_file( $file ) ) {
369 + $dbBackups[ filemtime( $theFile ) . $file ] = $theFile;
370 + }
366 371
367 - if ( $information['full'] && self::is_archive( $file ) && ! self::is_sql_archive( $file ) ) {
368 - $fullBackups[ filemtime( $theFile ) . $file ] = $theFile;
369 - }
370 - }
371 - }
372 - closedir( $dh );
373 - }
374 - }
375 - krsort( $dbBackups );
376 - krsort( $fullBackups );
372 + if ( $information['full'] && static::is_archive( $file ) && ! static::is_sql_archive( $file ) ) {
373 + $fullBackups[ filemtime( $theFile ) . $file ] = $theFile;
374 + }
375 + }
376 + }
377 + closedir( $dh );
378 + }
379 + }
380 + krsort( $dbBackups );
381 + krsort( $fullBackups );
377 382
378 - $cnt = 0;
379 - foreach ( $dbBackups as $key => $dbBackup ) {
380 - ++$cnt;
381 - if ( $cnt >= $maxBackups ) {
382 - $wp_filesystem->delete( $dbBackup );
383 - }
384 - }
383 + $cnt = 0;
384 + foreach ( $dbBackups as $key => $dbBackup ) {
385 + ++$cnt;
386 + if ( $cnt >= $maxBackups ) {
387 + $wp_filesystem->delete( $dbBackup );
388 + }
389 + }
385 390
386 - $cnt = 0;
387 - foreach ( $fullBackups as $key => $fullBackup ) {
388 - ++$cnt;
389 - if ( $cnt >= $maxBackups ) {
390 - $wp_filesystem->delete( $fullBackup );
391 - }
392 - }
393 - $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'removedFiles' => 1 ) );
394 - }
391 + $cnt = 0;
392 + foreach ( $fullBackups as $key => $fullBackup ) {
393 + ++$cnt;
394 + if ( $cnt >= $maxBackups ) {
395 + $wp_filesystem->delete( $fullBackup );
396 + }
397 + }
398 + $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'removedFiles' => 1 ) );
399 + }
395 400
396 - $localBackupFile = null;
401 + $localBackupFile = null;
397 402
398 - $fm_date = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'date_format' ) ) );
399 - $fm_time = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'time_format' ) ) );
403 + $fm_date = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'date_format' ) ) );
404 + $fm_time = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'time_format' ) ) );
400 405
401 - $what = null;
402 - $regexBackupFile = null;
406 + $what = null;
407 + $regexBackupFile = null;
403 408
404 - if ( $information['db'] ) {
405 - $what = 'db';
406 - $regexBackupFile = 'db-' . $websiteCleanUrl . '-(.*)-(.*).sql(\.zip|\.tar|\.tar\.gz|\.tar\.bz2)?';
407 - if ( '' === $backupTaskProgress->downloadedDB ) {
408 - $localBackupFile = $dir . 'db-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time;
409 + if ( $information['db'] ) {
410 + $what = 'db';
411 + $regexBackupFile = 'db-' . $websiteCleanUrl . '-(.*)-(.*).sql(\.zip|\.tar|\.tar\.gz|\.tar\.bz2)?';
412 + if ( '' === $backupTaskProgress->downloadedDB ) {
413 + $localBackupFile = $dir . 'db-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time;
409 414
410 - if ( null !== $pFilename ) {
411 - $filename = str_replace(
412 - array(
413 - '%sitename%',
414 - '%url%',
415 - '%date%',
416 - '%time%',
417 - '%type%',
418 - ),
419 - array(
420 - MainWP_Utility::sanitize( $website->name ),
421 - $websiteCleanUrl,
422 - $fm_date,
423 - $fm_time,
424 - $what,
425 - ),
426 - $pFilename
427 - );
428 - $filename = str_replace( '%', '', $filename );
429 - $localBackupFile = $dir . $filename;
430 - }
431 - $localBackupFile .= self::get_real_extension( $information['db'] );
415 + if ( null !== $pFilename ) {
416 + $filename = str_replace(
417 + array(
418 + '%sitename%',
419 + '%url%',
420 + '%date%',
421 + '%time%',
422 + '%type%',
423 + ),
424 + array(
425 + MainWP_Utility::sanitize( $website->name ),
426 + $websiteCleanUrl,
427 + $fm_date,
428 + $fm_time,
429 + $what,
430 + ),
431 + $pFilename
432 + );
433 + $filename = str_replace( '%', '', $filename );
434 + $localBackupFile = $dir . $filename;
435 + }
436 + $localBackupFile .= static::get_real_extension( $information['db'] );
432 437
433 - $localBackupFile = MainWP_Utility::normalize_filename( $localBackupFile );
438 + $localBackupFile = static::normalize_filename( $localBackupFile );
434 439
435 - $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedDB' => $localBackupFile ) );
436 - } else {
437 - $localBackupFile = $backupTaskProgress->downloadedDB;
438 - }
440 + $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedDB' => $localBackupFile ) );
441 + } else {
442 + $localBackupFile = $backupTaskProgress->downloadedDB;
443 + }
439 444
440 - if ( 0 === (int) $backupTaskProgress->downloadedDBComplete ) {
441 - MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $information['db'], 'fdl' ), $localBackupFile, $information['size'], $website->http_user, $website->http_pass );
442 - $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedDBComplete' => 1 ) );
443 - }
444 - }
445 + if ( 0 === (int) $backupTaskProgress->downloadedDBComplete ) {
446 + // MWP-1548: decrypt at the boundary so download_to_file
447 + // sees plaintext credentials.
448 + $http_user_plain = MainWP_Credential_Storage::decrypt_credential( $website->http_user );
449 + $http_pass_plain = MainWP_Credential_Storage::decrypt_credential( $website->http_pass );
450 + MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $information['db'], 'fdl' ), $localBackupFile, $information['size'], $http_user_plain, $http_pass_plain );
451 + $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedDBComplete' => 1 ) );
452 + }
453 + }
445 454
446 - if ( $information['full'] ) {
447 - $realExt = self::get_real_extension( $information['full'] );
448 - $what = 'full';
449 - $regexBackupFile = 'full-' . $websiteCleanUrl . '-(.*)-(.*).(zip|tar|tar.gz|tar.bz2)';
450 - if ( '' === $backupTaskProgress->downloadedFULL ) {
451 - $localBackupFile = $dir . 'full-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . $realExt;
455 + if ( $information['full'] ) {
456 + $realExt = static::get_real_extension( $information['full'] );
457 + $what = 'full';
458 + $regexBackupFile = 'full-' . $websiteCleanUrl . '-(.*)-(.*).(zip|tar|tar.gz|tar.bz2)';
459 + if ( '' === $backupTaskProgress->downloadedFULL ) {
460 + $localBackupFile = $dir . 'full-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . $realExt;
452 461
453 - if ( null !== $pFilename ) {
454 - $filename = str_replace(
455 - array(
456 - '%sitename%',
457 - '%url%',
458 - '%date%',
459 - '%time%',
460 - '%type%',
461 - ),
462 - array(
463 - MainWP_Utility::sanitize( $website->name ),
464 - $websiteCleanUrl,
465 - $fm_date,
466 - $fm_time,
467 - $what,
468 - ),
469 - $pFilename
470 - );
471 - $filename = str_replace( '%', '', $filename );
472 - $localBackupFile = $dir . $filename . $realExt;
473 - }
462 + if ( null !== $pFilename ) {
463 + $filename = str_replace(
464 + array(
465 + '%sitename%',
466 + '%url%',
467 + '%date%',
468 + '%time%',
469 + '%type%',
470 + ),
471 + array(
472 + MainWP_Utility::sanitize( $website->name ),
473 + $websiteCleanUrl,
474 + $fm_date,
475 + $fm_time,
476 + $what,
477 + ),
478 + $pFilename
479 + );
480 + $filename = str_replace( '%', '', $filename );
481 + $localBackupFile = $dir . $filename . $realExt;
482 + }
474 483
475 - $localBackupFile = MainWP_Utility::normalize_filename( $localBackupFile );
484 + $localBackupFile = static::normalize_filename( $localBackupFile );
476 485
477 - $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedFULL' => $localBackupFile ) );
478 - } else {
479 - $localBackupFile = $backupTaskProgress->downloadedFULL;
480 - }
486 + $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedFULL' => $localBackupFile ) );
487 + } else {
488 + $localBackupFile = $backupTaskProgress->downloadedFULL;
489 + }
481 490
482 - if ( 0 === (int) $backupTaskProgress->downloadedFULLComplete ) {
483 - if ( file_exists( $localBackupFile ) ) {
484 - $time = filemtime( $localBackupFile );
491 + if ( 0 === (int) $backupTaskProgress->downloadedFULLComplete ) {
492 + if ( file_exists( $localBackupFile ) ) {
493 + $time = filemtime( $localBackupFile );
485 494
486 - $minutes = gmdate( 'i', time() );
487 - $seconds = gmdate( 's', time() );
495 + $minutes = gmdate( 'i', time() );
496 + $seconds = gmdate( 's', time() );
488 497
489 - $file_minutes = gmdate( 'i', $time );
490 - $file_seconds = gmdate( 's', $time );
498 + $file_minutes = gmdate( 'i', $time );
499 + $file_seconds = gmdate( 's', $time );
491 500
492 - $minuteDiff = $minutes - $file_minutes;
493 - if ( 59 === $minuteDiff ) {
494 - $minuteDiff = 1;
495 - }
496 - $secondsdiff = ( $minuteDiff * 60 ) + $seconds - $file_seconds;
501 + $minuteDiff = $minutes - $file_minutes;
502 + if ( 59 === $minuteDiff ) {
503 + $minuteDiff = 1;
504 + }
505 + $secondsdiff = ( $minuteDiff * 60 ) + $seconds - $file_seconds;
497 506
498 - if ( 60 > $secondsdiff ) {
499 - // still downloading.
500 - return false;
501 - }
502 - }
507 + if ( 60 > $secondsdiff ) {
508 + // still downloading.
509 + return false;
510 + }
511 + }
503 512
504 - MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $information['full'], 'fdl' ), $localBackupFile, $information['size'], $website->http_user, $website->http_pass );
505 - MainWP_Connect::fetch_url_authed( $website, 'delete_backup', array( 'del' => $information['full'] ) );
506 - $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedFULLComplete' => 1 ) );
507 - }
508 - }
513 + // MWP-1548: decrypt at the boundary.
514 + $http_user_plain = MainWP_Credential_Storage::decrypt_credential( $website->http_user );
515 + $http_pass_plain = MainWP_Credential_Storage::decrypt_credential( $website->http_pass );
516 + MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $information['full'], 'fdl' ), $localBackupFile, $information['size'], $http_user_plain, $http_pass_plain );
517 + MainWP_Connect::fetch_url_authed( $website, 'delete_backup', array( 'del' => $information['full'] ) );
518 + $backupTaskProgress = MainWP_DB_Backup::instance()->update_backup_task_progress( $taskId, $website->id, array( 'downloadedFULLComplete' => 1 ) );
519 + }
520 + }
509 521
510 - $unique = $pTask->last_run;
522 + $unique = $pTask->last_run;
511 523
512 - do_action( 'mainwp_postprocess_backup_site', $localBackupFile, $what, $subfolder, $regexBackupFile, $website, $taskId, $unique );
513 - $extra_result = apply_filters( 'mainwp_postprocess_backup_sites_feedback', array(), $unique );
514 - if ( is_array( $extra_result ) ) {
515 - foreach ( $extra_result as $key => $value ) {
516 - $backup_result[ $key ] = $value;
517 - }
518 - }
519 - } else {
520 - throw new MainWP_Exception( 'Database backup failed due to an undefined error.' );
521 - }
524 + do_action( 'mainwp_postprocess_backup_site', $localBackupFile, $what, $subfolder, $regexBackupFile, $website, $taskId, $unique );
525 + $extra_result = apply_filters( 'mainwp_postprocess_backup_sites_feedback', array(), $unique );
526 + if ( is_array( $extra_result ) ) {
527 + foreach ( $extra_result as $key => $value ) {
528 + $backup_result[ $key ] = $value;
529 + }
530 + }
531 + } else {
532 + throw new MainWP_Exception( 'Database backup failed due to an undefined error.' );
533 + }
522 534
523 - return $backup_result;
524 - }
535 + return $backup_result;
536 + }
525 537
526 - /**
527 - * Method backup_download_file()
528 - *
529 - * Download backup file.
530 - *
531 - * @param mixed $pSiteId Child Site ID.
532 - * @param mixed $pType full|db Type of backup.
533 - * @param mixed $pUrl Backup location.
534 - * @param mixed $pFile Backup File.
535 - *
536 - * @return bool true|false
537 - * @throws MainWP_Exception Error message.
538 - *
539 - * @uses \MainWP\Dashboard\MainWP_Connect::get_get_data_authed()
540 - * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
541 - * @uses \MainWP\Dashboard\MainWP_Connect::download_to_file()
542 - * @uses \MainWP\Dashboard\MainWP_Utility::end_session()
543 - */
544 - public static function backup_download_file( $pSiteId, $pType, $pUrl, $pFile ) {
545 - $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
538 + /**
539 + * Method backup_download_file()
540 + *
541 + * Download backup file.
542 + *
543 + * @param mixed $pSiteId Child Site ID.
544 + * @param mixed $pType full|db Type of backup.
545 + * @param mixed $pUrl Backup location.
546 + * @param mixed $pFile Backup File.
547 + *
548 + * @return bool true|false
549 + * @throws MainWP_Exception Error message.
550 + *
551 + * @uses \MainWP\Dashboard\MainWP_Connect::get_get_data_authed()
552 + * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
553 + * @uses \MainWP\Dashboard\MainWP_Connect::download_to_file()
554 + * @uses \MainWP\Dashboard\MainWP_Utility::end_session()
555 + */
556 + public static function backup_download_file( $pSiteId, $pType, $pUrl, $pFile ) {
557 + $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
546 558
547 - /**
548 - * WordPress filesystem instance.
549 - *
550 - * @global object
551 - */
552 - global $wp_filesystem;
559 + /**
560 + * WordPress filesystem instance.
561 + *
562 + * @global object
563 + */
564 + global $wp_filesystem;
553 565
554 - $dir = dirname( $pFile ) . '/';
555 - $wp_filesystem->mkdir( $dir, 0777 );
556 - if ( ! $wp_filesystem->exists( $dir . 'index.php' ) ) {
557 - $wp_filesystem->touch( $dir . 'index.php' );
558 - }
559 - // Clean old backups from our system.
560 - $maxBackups = get_option( 'mainwp_backupsOnServer' );
561 - if ( false === $maxBackups ) {
562 - $maxBackups = 1;
563 - }
566 + $dir = dirname( $pFile ) . '/';
567 + $wp_filesystem->mkdir( $dir, 0750 ); // MWP-1558: tightened from 0777; backup download destination.
568 + if ( ! $wp_filesystem->exists( $dir . 'index.php' ) ) {
569 + $wp_filesystem->touch( $dir . 'index.php' );
570 + }
571 + // Clean old backups from our system.
572 + $maxBackups = get_option( 'mainwp_backupsOnServer' );
573 + if ( false === $maxBackups ) {
574 + $maxBackups = 1;
575 + }
564 576
565 - $dbBackups = array();
566 - $fullBackups = array();
577 + $dbBackups = array();
578 + $fullBackups = array();
567 579
568 - if ( file_exists( $dir ) ) {
569 - $dh = opendir( $dir );
570 - if ( $dh ) {
571 - while ( false !== ( $file = readdir( $dh ) ) ) {
572 - if ( '.' !== $file && '..' !== $file ) {
573 - $theFile = $dir . $file;
574 - if ( 'db' === $pType && self::is_sql_file( $file ) ) {
575 - $dbBackups[ filemtime( $theFile ) . $file ] = $theFile;
576 - }
580 + if ( file_exists( $dir ) ) {
581 + $dh = opendir( $dir );
582 + if ( $dh ) {
583 + while ( false !== ( $file = readdir( $dh ) ) ) {
584 + if ( '.' !== $file && '..' !== $file ) {
585 + $theFile = $dir . $file;
586 + if ( 'db' === $pType && static::is_sql_file( $file ) ) {
587 + $dbBackups[ filemtime( $theFile ) . $file ] = $theFile;
588 + }
577 589
578 - if ( 'full' === $pType && self::is_archive( $file ) && ! self::is_sql_archive( $file ) ) {
579 - $fullBackups[ filemtime( $theFile ) . $file ] = $theFile;
580 - }
581 - }
582 - }
583 - closedir( $dh );
584 - }
585 - }
586 - krsort( $dbBackups );
587 - krsort( $fullBackups );
590 + if ( 'full' === $pType && static::is_archive( $file ) && ! static::is_sql_archive( $file ) ) {
591 + $fullBackups[ filemtime( $theFile ) . $file ] = $theFile;
592 + }
593 + }
594 + }
595 + closedir( $dh );
596 + }
597 + }
598 + krsort( $dbBackups );
599 + krsort( $fullBackups );
588 600
589 - $cnt = 0;
590 - foreach ( $dbBackups as $key => $dbBackup ) {
591 - ++$cnt;
592 - if ( $cnt >= $maxBackups ) {
593 - $wp_filesystem->delete( $dbBackup );
594 - }
595 - }
601 + $cnt = 0;
602 + foreach ( $dbBackups as $key => $dbBackup ) {
603 + ++$cnt;
604 + if ( $cnt >= $maxBackups ) {
605 + $wp_filesystem->delete( $dbBackup );
606 + }
607 + }
596 608
597 - $cnt = 0;
598 - foreach ( $fullBackups as $key => $fullBackup ) {
599 - ++$cnt;
600 - if ( $cnt >= $maxBackups ) {
601 - $wp_filesystem->delete( $fullBackup );
602 - }
603 - }
609 + $cnt = 0;
610 + foreach ( $fullBackups as $key => $fullBackup ) {
611 + ++$cnt;
612 + if ( $cnt >= $maxBackups ) {
613 + $wp_filesystem->delete( $fullBackup );
614 + }
615 + }
604 616
605 - $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
606 - MainWP_Utility::end_session();
617 + $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
618 + MainWP_Utility::end_session();
607 619
608 - $what = null;
609 - if ( 'db' === $pType ) {
610 - MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $pUrl, 'fdl' ), $pFile, false, $website->http_user, $website->http_pass );
611 - }
620 + // MWP-1548: decrypt once at function boundary; both branches use
621 + // the same credentials.
622 + $http_user_plain = MainWP_Credential_Storage::decrypt_credential( $website->http_user );
623 + $http_pass_plain = MainWP_Credential_Storage::decrypt_credential( $website->http_pass );
612 624
613 - if ( 'full' === $pType ) {
614 - MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $pUrl, 'fdl' ), $pFile, false, $website->http_user, $website->http_pass );
615 - }
625 + if ( 'db' === $pType ) {
626 + MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $pUrl, 'fdl' ), $pFile, false, $http_user_plain, $http_pass_plain );
627 + }
616 628
617 - return true;
618 - }
629 + if ( 'full' === $pType ) {
630 + MainWP_Connect::download_to_file( MainWP_Connect::get_get_data_authed( $website, $pUrl, 'fdl' ), $pFile, false, $http_user_plain, $http_pass_plain );
631 + }
619 632
620 - /**
621 - * Method backup_delete_file().
622 - *
623 - * Delete backup file.
624 - *
625 - * @param mixed $pSiteId Child Site ID.
626 - * @param mixed $pFile File to delete.
627 - *
628 - * @return bool true
629 - *
630 - * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
631 - * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
632 - */
633 - public static function backup_delete_file( $pSiteId, $pFile ) {
634 - $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
635 - MainWP_Connect::fetch_url_authed( $website, 'delete_backup', array( 'del' => $pFile ) );
633 + return true;
634 + }
636 635
637 - return true;
638 - }
636 + /**
637 + * Method backup_delete_file().
638 + *
639 + * Delete backup file.
640 + *
641 + * @param mixed $pSiteId Child Site ID.
642 + * @param mixed $pFile File to delete.
643 + *
644 + * @return bool true
645 + *
646 + * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
647 + * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
648 + */
649 + public static function backup_delete_file( $pSiteId, $pFile ) {
650 + $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
651 + MainWP_Connect::fetch_url_authed( $website, 'delete_backup', array( 'del' => $pFile ) );
652 + return true;
653 + }
639 654
640 - /**
641 - * Method backup_check_pid()
642 - *
643 - * Check backup pid.
644 - *
645 - * @param mixed $pSiteId Child Site id.
646 - * @param mixed $pid Backup pid.
647 - * @param mixed $type full|db Type of backup.
648 - * @param mixed $subfolder Sub folder for backup.
649 - * @param mixed $pFilename Backups filename.
650 - *
651 - * @return array $status, $result.
652 - * @throws \Exception Error message.
653 - *
654 - * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_specific_dir()
655 - * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
656 - * @uses \MainWP\Dashboard\MainWP_Utility::end_session()
657 - * @uses \MainWP\Dashboard\MainWP_Utility::sanitize()
658 - * @uses \MainWP\Dashboard\MainWP_Utility::get_nice_url()
659 - * @uses \MainWP\Dashboard\MainWP_Utility::date()
660 - * @uses \MainWP\Dashboard\MainWP_Utility::remove_preslash_spaces()
661 - * @uses \MainWP\Dashboard\MainWP_Utility::normalize_filename()
662 - * @uses \MainWP\Dashboard\MainWP_Utility::sanitize_file_name()
663 - * @uses \MainWP\Dashboard\self::get_real_extension()
664 - */
665 - public static function backup_check_pid( $pSiteId, $pid, $type, $subfolder, $pFilename ) {
666 - $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
655 + /**
656 + * Method backup_check_pid()
657 + *
658 + * Check backup pid.
659 + *
660 + * @param mixed $pSiteId Child Site id.
661 + * @param mixed $pid Backup pid.
662 + * @param mixed $type full|db Type of backup.
663 + * @param mixed $subfolder Sub folder for backup.
664 + * @param mixed $pFilename Backups filename.
665 + *
666 + * @return array $status, $result.
667 + * @throws \MainWP_Exception Error message.
668 + *
669 + * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_specific_dir()
670 + * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
671 + * @uses \MainWP\Dashboard\MainWP_Utility::end_session()
672 + * @uses \MainWP\Dashboard\MainWP_Utility::sanitize()
673 + * @uses \MainWP\Dashboard\MainWP_Utility::get_nice_url()
674 + * @uses \MainWP\Dashboard\MainWP_Utility::date()
675 + * @uses \MainWP\Dashboard\MainWP_Utility::remove_preslash_spaces()
676 + * @uses \MainWP\Dashboard\static::normalize_filename()
677 + * @uses \MainWP\Dashboard\MainWP_Utility::sanitize_file_name()
678 + * @uses \MainWP\Dashboard\static::get_real_extension()
679 + */
680 + public static function backup_check_pid( $pSiteId, $pid, $type, $subfolder, $pFilename ) {
681 + $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
667 682
668 - MainWP_Utility::end_session();
669 - $information = MainWP_Connect::fetch_url_authed( $website, 'backup_checkpid', array( 'pid' => $pid ) );
683 + MainWP_Utility::end_session();
684 + $information = MainWP_Connect::fetch_url_authed( $website, 'backup_checkpid', array( 'pid' => $pid ) );
670 685
671 - $status = $information['status'];
686 + $status = $information['status'];
672 687
673 - $result = isset( $information['file'] ) ? array( 'file' => $information['file'] ) : array();
674 - if ( 'done' === $status ) {
675 - $result['file'] = $information['file'];
676 - $result['size'] = $information['size'];
688 + $result = isset( $information['file'] ) ? array( 'file' => $information['file'] ) : array();
689 + if ( 'done' === $status ) {
690 + $result['file'] = $information['file'];
691 + $result['size'] = $information['size'];
677 692
678 - $subfolder = str_replace( '%sitename%', MainWP_Utility::sanitize( $website->name ), $subfolder );
679 - $subfolder = str_replace( '%url%', MainWP_Utility::sanitize( MainWP_Utility::get_nice_url( $website->url ) ), $subfolder );
680 - $subfolder = str_replace( '%type%', $type, $subfolder );
681 - $subfolder = str_replace( '%date%', MainWP_Utility::date( 'Ymd' ), $subfolder );
682 - $subfolder = str_replace( '%task%', '', $subfolder );
683 - $subfolder = str_replace( '%', '', $subfolder );
684 - $subfolder = MainWP_Utility::remove_preslash_spaces( $subfolder );
685 - $subfolder = MainWP_Utility::normalize_filename( $subfolder );
693 + $subfolder = str_replace( '%sitename%', MainWP_Utility::sanitize( $website->name ), $subfolder );
694 + $subfolder = str_replace( '%url%', MainWP_Utility::sanitize( MainWP_Utility::get_nice_url( $website->url ) ), $subfolder );
695 + $subfolder = str_replace( '%type%', $type, $subfolder );
696 + $subfolder = str_replace( '%date%', MainWP_Utility::date( 'Ymd' ), $subfolder );
697 + $subfolder = str_replace( '%task%', '', $subfolder );
698 + $subfolder = str_replace( '%', '', $subfolder );
699 + $subfolder = MainWP_Utility::remove_preslash_spaces( $subfolder );
700 + $subfolder = static::normalize_filename( $subfolder );
686 701
687 - $result['subfolder'] = $subfolder;
702 + $result['subfolder'] = $subfolder;
688 703
689 - $websiteCleanUrl = $website->url;
690 - if ( '/' === substr( $websiteCleanUrl, - 1 ) ) {
691 - $websiteCleanUrl = substr( $websiteCleanUrl, 0, - 1 );
692 - }
693 - $websiteCleanUrl = str_replace(
694 - array( 'http://', 'https://', '/' ),
695 - array(
696 - '',
697 - '',
698 - '-',
699 - ),
700 - $websiteCleanUrl
701 - );
704 + $websiteCleanUrl = $website->url;
705 + if ( '/' === substr( $websiteCleanUrl, - 1 ) ) {
706 + $websiteCleanUrl = substr( $websiteCleanUrl, 0, - 1 );
707 + }
708 + $websiteCleanUrl = str_replace(
709 + array( 'http://', 'https://', '/' ),
710 + array(
711 + '',
712 + '',
713 + '-',
714 + ),
715 + $websiteCleanUrl
716 + );
702 717
703 - $dir = MainWP_System_Utility::get_mainwp_specific_dir( $pSiteId );
718 + $dir = MainWP_System_Utility::get_mainwp_specific_dir( $pSiteId );
704 719
705 - $fm_date = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'date_format' ) ) );
706 - $fm_time = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'time_format' ) ) );
720 + $fm_date = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'date_format' ) ) );
721 + $fm_time = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'time_format' ) ) );
707 722
708 - if ( 'db' === $type ) {
709 - $localBackupFile = $dir . 'db-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . self::get_real_extension( $information['file'] );
710 - $localRegexFile = 'db-' . $websiteCleanUrl . '-(.*)-(.*).sql(\.zip|\.tar|\.tar\.gz|\.tar\.bz2)?';
711 - } else {
712 - $localBackupFile = $dir . 'full-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . self::get_real_extension( $information['file'] );
713 - $localRegexFile = 'full-' . $websiteCleanUrl . '-(.*)-(.*).(zip|tar|tar.gz|tar.bz2)';
714 - }
723 + if ( 'db' === $type ) {
724 + $localBackupFile = $dir . 'db-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . static::get_real_extension( $information['file'] );
725 + $localRegexFile = 'db-' . $websiteCleanUrl . '-(.*)-(.*).sql(\.zip|\.tar|\.tar\.gz|\.tar\.bz2)?';
726 + } else {
727 + $localBackupFile = $dir . 'full-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . static::get_real_extension( $information['file'] );
728 + $localRegexFile = 'full-' . $websiteCleanUrl . '-(.*)-(.*).(zip|tar|tar.gz|tar.bz2)';
729 + }
715 730
716 - if ( null !== $pFilename ) {
717 - $filename = str_replace(
718 - array(
719 - '%sitename%',
720 - '%url%',
721 - '%date%',
722 - '%time%',
723 - '%type%',
724 - ),
725 - array(
726 - MainWP_Utility::sanitize( $website->name ),
727 - $websiteCleanUrl,
728 - $fm_date,
729 - $fm_time,
730 - $type,
731 - ),
732 - $pFilename
733 - );
734 - $filename = str_replace( '%', '', $filename );
735 - $localBackupFile = $dir . $filename;
736 - $localBackupFile = MainWP_Utility::normalize_filename( $localBackupFile );
731 + if ( null !== $pFilename ) {
732 + $filename = str_replace(
733 + array(
734 + '%sitename%',
735 + '%url%',
736 + '%date%',
737 + '%time%',
738 + '%type%',
739 + ),
740 + array(
741 + MainWP_Utility::sanitize( $website->name ),
742 + $websiteCleanUrl,
743 + $fm_date,
744 + $fm_time,
745 + $type,
746 + ),
747 + $pFilename
748 + );
749 + $filename = str_replace( '%', '', $filename );
750 + $localBackupFile = $dir . $filename;
751 + $localBackupFile = static::normalize_filename( $localBackupFile );
737 752
738 - if ( 'db' === $type ) {
739 - $localBackupFile .= self::get_real_extension( $information['file'] );
740 - } else {
741 - $localBackupFile .= self::get_real_extension( $information['file'] );
742 - }
743 - }
753 + if ( 'db' === $type ) {
754 + $localBackupFile .= static::get_real_extension( $information['file'] );
755 + } else {
756 + $localBackupFile .= static::get_real_extension( $information['file'] );
757 + }
758 + }
744 759
745 - $result['local'] = $localBackupFile;
746 - $result['regexfile'] = $localRegexFile;
747 - }
760 + $result['local'] = $localBackupFile;
761 + $result['regexfile'] = $localRegexFile;
762 + }
748 763
749 - return array(
750 - 'status' => $status,
751 - 'result' => $result,
752 - );
753 - }
764 + return array(
765 + 'status' => $status,
766 + 'result' => $result,
767 + );
768 + }
754 769
755 - /**
756 - * Method backup()
757 - *
758 - * Backup Child Site.
759 - *
760 - * @param mixed $pSiteId Child Site ID.
761 - * @param mixed $pType full|db Typ of backup.
762 - * @param mixed $pSubfolder Sub folder to store backup.
763 - * @param mixed $pExclude Exclusion list.
764 - * @param mixed $excludebackup Exclude backup files.
765 - * @param mixed $excludecache Exclude cache files.
766 - * @param mixed $excludenonwp Exclude no WP Files.
767 - * @param mixed $excludezip Exclude Zip files.
768 - * @param null $pFilename Name of backup file.
769 - * @param string $pFileNameUID File name unique ID.
770 - * @param bool $pArchiveFormat Archive format.
771 - * @param bool $pMaximumFileDescriptorsOverride Override maximum file descriptors.
772 - * @param bool $pMaximumFileDescriptorsAuto Auto maximum file descriptors.
773 - * @param bool $pMaximumFileDescriptors Maximum file descriptors.
774 - * @param bool $pLoadFilesBeforeZip Load files before zip.
775 - * @param bool $pid Backup pid.
776 - * @param bool $append Append to backup.
777 - *
778 - * @return mixed $backup_result
779 - * @throws MainWP_Exception Error message.
780 - *
781 - * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
782 - * @uses \MainWP\Dashboard\MainWP_Exception
783 - * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
784 - * @uses \MainWP\Dashboard\MainWP_Logger::debug_for_website()
785 - * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
786 - * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_specific_dir()
787 - * @uses \MainWP\Dashboard\MainWP_Utility::sanitize()
788 - * @uses \MainWP\Dashboard\MainWP_Utility::date()
789 - * @uses \MainWP\Dashboard\MainWP_Utility::remove_preslash_spaces()
790 - * @uses \MainWP\Dashboard\MainWP_Utility::normalize_filename()
791 - * @uses \MainWP\Dashboard\MainWP_Utility::sanitize_file_name()
792 - * @uses \MainWP\Dashboard\MainWP_Utility::end_session()
793 - * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
794 - * @uses \MainWP\Dashboard\self::get_real_extension()
795 - */
796 - public static function backup(
797 - $pSiteId,
798 - $pType,
799 - $pSubfolder,
800 - $pExclude,
801 - $excludebackup,
802 - $excludecache,
803 - $excludenonwp,
804 - $excludezip,
805 - $pFilename = null,
806 - $pFileNameUID = '',
807 - $pArchiveFormat = false,
808 - $pMaximumFileDescriptorsOverride = false,
809 - $pMaximumFileDescriptorsAuto = false,
810 - $pMaximumFileDescriptors = false,
811 - $pLoadFilesBeforeZip = false,
812 - $pid = false,
813 - $append = false
814 - ) {
770 + /**
771 + * Method backup()
772 + *
773 + * Backup Child Site.
774 + *
775 + * @param mixed $pSiteId Child Site ID.
776 + * @param mixed $pType full|db Typ of backup.
777 + * @param mixed $pSubfolder Sub folder to store backup.
778 + * @param mixed $pExclude Exclusion list.
779 + * @param mixed $excludebackup Exclude backup files.
780 + * @param mixed $excludecache Exclude cache files.
781 + * @param mixed $excludenonwp Exclude no WP Files.
782 + * @param mixed $excludezip Exclude Zip files.
783 + * @param null $pFilename Name of backup file.
784 + * @param string $pFileNameUID File name unique ID.
785 + * @param bool $pArchiveFormat Archive format.
786 + * @param bool $pMaximumFileDescriptorsOverride Override maximum file descriptors.
787 + * @param bool $pMaximumFileDescriptorsAuto Auto maximum file descriptors.
788 + * @param bool $pMaximumFileDescriptors Maximum file descriptors.
789 + * @param bool $pLoadFilesBeforeZip Load files before zip.
790 + * @param bool $pid Backup pid.
791 + * @param bool $append Append to backup.
792 + *
793 + * @return mixed $backup_result
794 + * @throws MainWP_Exception Error message.
795 + *
796 + * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
797 + * @uses \MainWP\Dashboard\MainWP_Exception
798 + * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
799 + * @uses \MainWP\Dashboard\MainWP_Logger::debug_for_website()
800 + * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
801 + * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_specific_dir()
802 + * @uses \MainWP\Dashboard\MainWP_Utility::sanitize()
803 + * @uses \MainWP\Dashboard\MainWP_Utility::date()
804 + * @uses \MainWP\Dashboard\MainWP_Utility::remove_preslash_spaces()
805 + * @uses \MainWP\Dashboard\static::normalize_filename()
806 + * @uses \MainWP\Dashboard\MainWP_Utility::sanitize_file_name()
807 + * @uses \MainWP\Dashboard\MainWP_Utility::end_session()
808 + * @uses \MainWP\Dashboard\MainWP_Utility::value_to_string()
809 + * @uses \MainWP\Dashboard\static::get_real_extension()
810 + */
811 + public static function backup(
812 + $pSiteId,
813 + $pType,
814 + $pSubfolder,
815 + $pExclude,
816 + $excludebackup,
817 + $excludecache,
818 + $excludenonwp,
819 + $excludezip,
820 + $pFilename = null,
821 + $pFileNameUID = '',
822 + $pArchiveFormat = false,
823 + $pMaximumFileDescriptorsOverride = false,
824 + $pMaximumFileDescriptorsAuto = false,
825 + $pMaximumFileDescriptors = false,
826 + $pLoadFilesBeforeZip = false,
827 + $pid = false,
828 + $append = false
829 + ) {
815 830
816 - if ( ! get_option( 'mainwp_enableLegacyBackupFeature' ) ) {
817 - return false;
818 - }
831 + if ( ! get_option( 'mainwp_enableLegacyBackupFeature' ) ) {
832 + return false;
833 + }
819 834
820 - if ( '' === trim( $pFilename ) ) {
821 - $pFilename = null;
822 - }
835 + if ( '' === trim( $pFilename ) ) {
836 + $pFilename = null;
837 + }
823 838
824 - $backup_result = array();
839 + $backup_result = array();
825 840
826 - $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
827 - $subfolder = str_replace( '%sitename%', MainWP_Utility::sanitize( $website->name ), $pSubfolder );
828 - $subfolder = str_replace( '%url%', MainWP_Utility::sanitize( MainWP_Utility::get_nice_url( $website->url ) ), $subfolder );
829 - $subfolder = str_replace( '%type%', $pType, $subfolder );
830 - $subfolder = str_replace( '%date%', MainWP_Utility::date( 'Ymd' ), $subfolder );
831 - $subfolder = str_replace( '%task%', '', $subfolder );
832 - $subfolder = str_replace( '%', '', $subfolder );
833 - $subfolder = MainWP_Utility::remove_preslash_spaces( $subfolder );
834 - $subfolder = MainWP_Utility::normalize_filename( $subfolder );
841 + $website = MainWP_DB::instance()->get_website_by_id( $pSiteId );
842 + $subfolder = str_replace( '%sitename%', MainWP_Utility::sanitize( $website->name ), $pSubfolder );
843 + $subfolder = str_replace( '%url%', MainWP_Utility::sanitize( MainWP_Utility::get_nice_url( $website->url ) ), $subfolder );
844 + $subfolder = str_replace( '%type%', $pType, $subfolder );
845 + $subfolder = str_replace( '%date%', MainWP_Utility::date( 'Ymd' ), $subfolder );
846 + $subfolder = str_replace( '%task%', '', $subfolder );
847 + $subfolder = str_replace( '%', '', $subfolder );
848 + $subfolder = MainWP_Utility::remove_preslash_spaces( $subfolder );
849 + $subfolder = static::normalize_filename( $subfolder );
835 850
836 - if ( MainWP_System_Utility::is_suspended_site( $website ) ) {
837 - throw new MainWP_Exception( 'Suspended site.', '', 'SUSPENDED_SITE' );
838 - }
851 + if ( MainWP_System_Utility::is_suspended_site( $website ) ) {
852 + throw new MainWP_Exception( 'Suspended site.', '', 'SUSPENDED_SITE' );
853 + }
839 854
840 - if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
841 - throw new MainWP_Exception( 'You are not allowed to backup this site' );
842 - }
855 + if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
856 + throw new MainWP_Exception( 'You are not allowed to backup this site' );
857 + }
843 858
844 - $websiteCleanUrl = $website->url;
845 - if ( '/' === substr( $websiteCleanUrl, - 1 ) ) {
846 - $websiteCleanUrl = substr( $websiteCleanUrl, 0, - 1 );
847 - }
848 - $websiteCleanUrl = str_replace(
849 - array( 'http://', 'https://', '/' ),
850 - array( '', '', '-' ),
851 - $websiteCleanUrl
852 - );
859 + $websiteCleanUrl = $website->url;
860 + if ( '/' === substr( $websiteCleanUrl, - 1 ) ) {
861 + $websiteCleanUrl = substr( $websiteCleanUrl, 0, - 1 );
862 + }
863 + $websiteCleanUrl = str_replace(
864 + array( 'http://', 'https://', '/' ),
865 + array( '', '', '-' ),
866 + $websiteCleanUrl
867 + );
853 868
854 - if ( false === $pMaximumFileDescriptorsOverride ) {
855 - if ( 1 === (int) $website->maximumFileDescriptorsOverride ) {
856 - $maximumFileDescriptorsAuto = ( 1 === (int) $website->maximumFileDescriptorsAuto );
857 - $maximumFileDescriptors = $website->maximumFileDescriptors;
858 - } else {
859 - $maximumFileDescriptorsAuto = get_option( 'mainwp_maximumFileDescriptorsAuto' );
860 - $maximumFileDescriptors = get_option( 'mainwp_maximumFileDescriptors' );
861 - $maximumFileDescriptors = ( false === $maximumFileDescriptors ? 150 : $maximumFileDescriptors );
862 - }
863 - } elseif ( ( 'global' !== $pArchiveFormat ) && ( 1 === (int) $pMaximumFileDescriptorsOverride ) ) {
864 - $maximumFileDescriptorsAuto = ( 1 === $pMaximumFileDescriptorsAuto );
865 - $maximumFileDescriptors = $pMaximumFileDescriptors;
866 - } else {
867 - $maximumFileDescriptorsAuto = get_option( 'mainwp_maximumFileDescriptorsAuto' );
868 - $maximumFileDescriptors = get_option( 'mainwp_maximumFileDescriptors' );
869 - $maximumFileDescriptors = ( false === $maximumFileDescriptors ? 150 : $maximumFileDescriptors );
870 - }
869 + if ( false === $pMaximumFileDescriptorsOverride ) {
870 + if ( 1 === (int) $website->maximumFileDescriptorsOverride ) {
871 + $maximumFileDescriptorsAuto = ( 1 === (int) $website->maximumFileDescriptorsAuto );
872 + $maximumFileDescriptors = $website->maximumFileDescriptors;
873 + } else {
874 + $maximumFileDescriptorsAuto = get_option( 'mainwp_maximumFileDescriptorsAuto' );
875 + $maximumFileDescriptors = get_option( 'mainwp_maximumFileDescriptors' );
876 + $maximumFileDescriptors = ( false === $maximumFileDescriptors ? 150 : $maximumFileDescriptors );
877 + }
878 + } elseif ( ( 'global' !== $pArchiveFormat ) && ( 1 === (int) $pMaximumFileDescriptorsOverride ) ) {
879 + $maximumFileDescriptorsAuto = ( 1 === $pMaximumFileDescriptorsAuto );
880 + $maximumFileDescriptors = $pMaximumFileDescriptors;
881 + } else {
882 + $maximumFileDescriptorsAuto = get_option( 'mainwp_maximumFileDescriptorsAuto' );
883 + $maximumFileDescriptors = get_option( 'mainwp_maximumFileDescriptors' );
884 + $maximumFileDescriptors = ( false === $maximumFileDescriptors ? 150 : $maximumFileDescriptors );
885 + }
871 886
872 - $file = str_replace(
873 - array(
874 - '%sitename%',
875 - '%url%',
876 - '%date%',
877 - '%time%',
878 - '%type%',
879 - ),
880 - array(
881 - MainWP_Utility::sanitize( $website->name ),
882 - $websiteCleanUrl,
883 - MainWP_Utility::date( 'm-d-Y' ),
884 - MainWP_Utility::date( 'G\hi\ms\s' ),
885 - $pType,
886 - ),
887 - $pFilename
888 - );
889 - $file = str_replace( '%', '', $file );
890 - $file = MainWP_Utility::normalize_filename( $file );
887 + $file = str_replace(
888 + array(
889 + '%sitename%',
890 + '%url%',
891 + '%date%',
892 + '%time%',
893 + '%type%',
894 + ),
895 + array(
896 + MainWP_Utility::sanitize( $website->name ),
897 + $websiteCleanUrl,
898 + MainWP_Utility::date( 'm-d-Y' ),
899 + MainWP_Utility::date( 'G\hi\ms\s' ),
900 + $pType,
901 + ),
902 + $pFilename
903 + );
904 + $file = str_replace( '%', '', $file );
905 + $file = static::normalize_filename( $file );
891 906
892 - // Normal flow: check site settings & fallback to global.
893 - if ( false === $pLoadFilesBeforeZip ) {
894 - $loadFilesBeforeZip = $website->loadFilesBeforeZip;
895 - if ( 1 === $loadFilesBeforeZip ) {
896 - $loadFilesBeforeZip = get_option( 'mainwp_options_loadFilesBeforeZip' );
897 - $loadFilesBeforeZip = ( 1 === $loadFilesBeforeZip || false === $loadFilesBeforeZip );
898 - } else {
899 - $loadFilesBeforeZip = ( 2 === $loadFilesBeforeZip );
900 - }
901 - } elseif ( 'global' === $pArchiveFormat || 1 === $pLoadFilesBeforeZip ) { // Overridden flow: only fallback to global.
902 - $loadFilesBeforeZip = get_option( 'mainwp_options_loadFilesBeforeZip' );
903 - $loadFilesBeforeZip = ( 1 === $loadFilesBeforeZip || false === $loadFilesBeforeZip );
904 - } else {
905 - $loadFilesBeforeZip = ( 2 === $pLoadFilesBeforeZip );
906 - }
907 + // Normal flow: check site settings & fallback to global.
908 + if ( false === $pLoadFilesBeforeZip ) {
909 + $loadFilesBeforeZip = $website->loadFilesBeforeZip;
910 + if ( 1 === $loadFilesBeforeZip ) {
911 + $loadFilesBeforeZip = get_option( 'mainwp_options_loadFilesBeforeZip' );
912 + $loadFilesBeforeZip = ( 1 === $loadFilesBeforeZip || false === $loadFilesBeforeZip );
913 + } else {
914 + $loadFilesBeforeZip = ( 2 === $loadFilesBeforeZip );
915 + }
916 + } elseif ( 'global' === $pArchiveFormat || 1 === $pLoadFilesBeforeZip ) { // Overridden flow: only fallback to global.
917 + $loadFilesBeforeZip = get_option( 'mainwp_options_loadFilesBeforeZip' );
918 + $loadFilesBeforeZip = ( 1 === $loadFilesBeforeZip || false === $loadFilesBeforeZip );
919 + } else {
920 + $loadFilesBeforeZip = ( 2 === $pLoadFilesBeforeZip );
921 + }
907 922
908 - // Normal flow: check site settings & fallback to global.
909 - if ( false === $pArchiveFormat ) {
910 - $archiveFormat = self::get_current_archive_extension( $website );
911 - } elseif ( 'global' === $pArchiveFormat ) {
912 - $archiveFormat = self::get_current_archive_extension();
913 - } else {
914 - $archiveFormat = $pArchiveFormat;
915 - }
923 + // Normal flow: check site settings & fallback to global.
924 + if ( false === $pArchiveFormat ) {
925 + $archiveFormat = static::get_current_archive_extension( $website );
926 + } elseif ( 'global' === $pArchiveFormat ) {
927 + $archiveFormat = static::get_current_archive_extension();
928 + } else {
929 + $archiveFormat = $pArchiveFormat;
930 + }
916 931
917 - MainWP_Utility::end_session();
932 + MainWP_Utility::end_session();
918 933
919 - $params = array(
920 - 'type' => $pType,
921 - 'exclude' => $pExclude,
922 - 'excludebackup' => $excludebackup,
923 - 'excludecache' => $excludecache,
924 - 'excludenonwp' => $excludenonwp,
925 - 'excludezip' => $excludezip,
926 - 'ext' => $archiveFormat,
927 - 'file_descriptors_auto' => $maximumFileDescriptorsAuto,
928 - 'file_descriptors' => $maximumFileDescriptors,
929 - 'loadFilesBeforeZip' => $loadFilesBeforeZip,
930 - 'f' => $file,
931 - 'fileUID' => $pFileNameUID,
932 - 'pid' => $pid,
933 - 'append' => ( $append ? 1 : 0 ),
934 - );
934 + $params = array(
935 + 'type' => $pType,
936 + 'exclude' => $pExclude,
937 + 'excludebackup' => $excludebackup,
938 + 'excludecache' => $excludecache,
939 + 'excludenonwp' => $excludenonwp,
940 + 'excludezip' => $excludezip,
941 + 'ext' => $archiveFormat,
942 + 'file_descriptors_auto' => $maximumFileDescriptorsAuto,
943 + 'file_descriptors' => $maximumFileDescriptors,
944 + 'loadFilesBeforeZip' => $loadFilesBeforeZip,
945 + 'f' => $file,
946 + 'fileUID' => $pFileNameUID,
947 + 'pid' => $pid,
948 + 'append' => ( $append ? 1 : 0 ),
949 + );
935 950
936 - MainWP_Logger::instance()->debug_for_website( $website, 'backup', 'Requesting backup: ' . MainWP_Utility::value_to_string( $params, 1 ) );
951 + MainWP_Logger::instance()->debug_for_website( $website, 'backup', 'Requesting backup: ' . MainWP_Utility::value_to_string( $params, 1 ) );
937 952
938 - $information = MainWP_Connect::fetch_url_authed( $website, 'backup', $params, false, false, false );
939 - do_action( 'mainwp_managesite_backup', $website, array( 'type' => $pType ), $information );
953 + $information = MainWP_Connect::fetch_url_authed( $website, 'backup', $params, false, false, false );
954 + do_action( 'mainwp_managesite_backup', $website, array( 'type' => $pType ), $information );
940 955
941 - if ( isset( $information['error'] ) ) {
942 - throw new MainWP_Exception( esc_html( $information['error'] ) ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
943 - } elseif ( 'db' === $pType && ! $information['db'] ) {
944 - throw new MainWP_Exception( 'Database backup failed.' );
945 - } elseif ( 'full' === $pType && ! $information['full'] ) {
946 - throw new MainWP_Exception( 'Full backup failed.' );
947 - } elseif ( isset( $information['db'] ) ) {
948 - if ( false !== $information['db'] ) {
949 - $backup_result['url'] = $information['db'];
950 - $backup_result['type'] = 'db';
951 - } elseif ( false !== $information['full'] ) {
952 - $backup_result['url'] = $information['full'];
953 - $backup_result['type'] = 'full';
954 - }
956 + if ( isset( $information['error'] ) ) {
957 + throw new MainWP_Exception( esc_html( $information['error'] ) ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
958 + } elseif ( 'db' === $pType && ! $information['db'] ) {
959 + throw new MainWP_Exception( 'Database backup failed.' );
960 + } elseif ( 'full' === $pType && ! $information['full'] ) {
961 + throw new MainWP_Exception( 'Full backup failed.' );
962 + } elseif ( isset( $information['db'] ) || isset( $information['full'] ) ) {
963 + if ( ! empty( $information['db'] ) ) {
964 + $backup_result['url'] = $information['db'];
965 + $backup_result['type'] = 'db';
966 + } elseif ( ! empty( $information['full'] ) ) {
967 + $backup_result['url'] = $information['full'];
968 + $backup_result['type'] = 'full';
969 + }
955 970
956 - if ( isset( $information['size'] ) ) {
957 - $backup_result['size'] = $information['size'];
958 - }
959 - $backup_result['subfolder'] = $subfolder;
971 + if ( isset( $information['size'] ) ) {
972 + $backup_result['size'] = $information['size'];
973 + }
974 + $backup_result['subfolder'] = $subfolder;
960 975
961 - $dir = MainWP_System_Utility::get_mainwp_specific_dir( $pSiteId );
976 + $dir = MainWP_System_Utility::get_mainwp_specific_dir( $pSiteId );
962 977
963 - $fm_date = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'date_format' ) ) );
964 - $fm_time = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'time_format' ) ) );
978 + $fm_date = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'date_format' ) ) );
979 + $fm_time = MainWP_Utility::sanitize_file_name( MainWP_Utility::date( get_option( 'time_format' ) ) );
965 980
966 - if ( 'db' === $pType ) {
967 - $localBackupFile = $dir . 'db-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . self::get_real_extension( $information['db'] );
968 - $localRegexFile = 'db-' . $websiteCleanUrl . '-(.*)-(.*).sql(\.zip|\.tar|\.tar\.gz|\.tar\.bz2)?';
969 - } else {
970 - $localBackupFile = $dir . 'full-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . self::get_real_extension( $information['full'] );
971 - $localRegexFile = 'full-' . $websiteCleanUrl . '-(.*)-(.*).(zip|tar|tar.gz|tar.bz2)';
972 - }
981 + if ( 'db' === $pType ) {
982 + $localBackupFile = $dir . 'db-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . static::get_real_extension( $information['db'] );
983 + $localRegexFile = 'db-' . $websiteCleanUrl . '-(.*)-(.*).sql(\.zip|\.tar|\.tar\.gz|\.tar\.bz2)?';
984 + } else {
985 + $localBackupFile = $dir . 'full-' . $websiteCleanUrl . '-' . $fm_date . '-' . $fm_time . static::get_real_extension( $information['full'] );
986 + $localRegexFile = 'full-' . $websiteCleanUrl . '-(.*)-(.*).(zip|tar|tar.gz|tar.bz2)';
987 + }
973 988
974 - if ( null !== $pFilename ) {
975 - $filename = str_replace(
976 - array(
977 - '%sitename%',
978 - '%url%',
979 - '%date%',
980 - '%time%',
981 - '%type%',
982 - ),
983 - array(
984 - MainWP_Utility::sanitize( $website->name ),
985 - $websiteCleanUrl,
986 - $fm_date,
987 - $fm_time,
988 - $pType,
989 - ),
990 - $pFilename
991 - );
992 - $filename = str_replace( '%', '', $filename );
993 - $localBackupFile = $dir . $filename;
994 - $localBackupFile = MainWP_Utility::normalize_filename( $localBackupFile );
989 + if ( null !== $pFilename ) {
990 + $filename = str_replace(
991 + array(
992 + '%sitename%',
993 + '%url%',
994 + '%date%',
995 + '%time%',
996 + '%type%',
997 + ),
998 + array(
999 + MainWP_Utility::sanitize( $website->name ),
1000 + $websiteCleanUrl,
1001 + $fm_date,
1002 + $fm_time,
1003 + $pType,
1004 + ),
1005 + $pFilename
1006 + );
1007 + $filename = str_replace( '%', '', $filename );
1008 + $localBackupFile = $dir . $filename;
1009 + $localBackupFile = static::normalize_filename( $localBackupFile );
995 1010
996 - if ( 'db' === $pType ) {
997 - $localBackupFile .= self::get_real_extension( $information['db'] );
998 - } else {
999 - $localBackupFile .= self::get_real_extension( $information['full'] );
1000 - }
1001 - }
1011 + if ( 'db' === $pType ) {
1012 + $localBackupFile .= static::get_real_extension( $information['db'] );
1013 + } else {
1014 + $localBackupFile .= static::get_real_extension( $information['full'] );
1015 + }
1016 + }
1002 1017
1003 - $backup_result['local'] = $localBackupFile;
1004 - $backup_result['regexfile'] = $localRegexFile;
1018 + $backup_result['local'] = $localBackupFile;
1019 + $backup_result['regexfile'] = $localRegexFile;
1005 1020
1006 - return $backup_result;
1007 - } else {
1008 - throw new MainWP_Exception( 'Database backup failed due to an undefined error' );
1009 - }
1010 - }
1021 + return $backup_result;
1022 + } else {
1023 + throw new MainWP_Exception( 'Database backup failed due to an undefined error' );
1024 + }
1025 + }
1011 1026
1012 - /**
1013 - * Method is_archive()
1014 - *
1015 - * Check if Archive.
1016 - *
1017 - * @param mixed $pFileName File to check.
1018 - * @param string $pPrefix File prefix.
1019 - * @param string $pSuffix File suffix.
1020 - *
1021 - * @return mixed Files that are archives.
1022 - */
1023 - public static function is_archive( $pFileName, $pPrefix = '', $pSuffix = '' ) {
1024 - return preg_match( '/' . $pPrefix . '(.*).(zip|tar|tar.gz|tar.bz2)' . $pSuffix . '$/', $pFileName );
1025 - }
1027 + /**
1028 + * Method normalize_filename()
1029 + *
1030 + * Normalize filename.
1031 + *
1032 + * @param mixed $s Filename to normalize.
1033 + *
1034 + * @return string $s Normalised filename.
1035 + */
1036 + public static function normalize_filename( $s ) {
1037 + $s = preg_replace( '@\x{00c4}@u', 'A', $s );
1038 + $s = preg_replace( '@\x{00d6}@u', 'O', $s );
1039 + $s = preg_replace( '@\x{00dc}@u', 'U', $s );
1040 + $s = preg_replace( '@\x{00cb}@u', 'E', $s );
1041 + $s = preg_replace( '@\x{00e4}@u', 'a', $s );
1042 + $s = preg_replace( '@\x{00f6}@u', 'o', $s );
1043 + $s = preg_replace( '@\x{00fc}@u', 'u', $s );
1044 + $s = preg_replace( '@\x{00eb}@u', 'e', $s );
1045 + $s = preg_replace( '@\x{00f1}@u', 'n', $s );
1046 + $s = preg_replace( '@\x{00ff}@u', 'y', $s );
1047 + return $s;
1048 + }
1026 1049
1027 - /**
1028 - * Method is_sql_file()
1029 - *
1030 - * Check if file is SQL.
1031 - *
1032 - * @param mixed $pFileName File to check.
1033 - *
1034 - * @return mixed SQL Files.
1035 - */
1036 - public static function is_sql_file( $pFileName ) {
1037 - return preg_match( '/(.*).sql$/', $pFileName ) || self::is_sql_archive( $pFileName );
1038 - }
1050 + /**
1051 + * Method is_archive()
1052 + *
1053 + * Check if Archive.
1054 + *
1055 + * @param mixed $pFileName File to check.
1056 + * @param string $pPrefix File prefix.
1057 + * @param string $pSuffix File suffix.
1058 + *
1059 + * @return mixed Files that are archives.
1060 + */
1061 + public static function is_archive( $pFileName, $pPrefix = '', $pSuffix = '' ) {
1062 + return preg_match( '/' . $pPrefix . '(.*).(zip|tar|tar.gz|tar.bz2)' . $pSuffix . '$/', $pFileName );
1063 + }
1039 1064
1040 - /**
1041 - * Method is_sql_archive()
1042 - *
1043 - * Check if is SQL Archive.
1044 - *
1045 - * @param mixed $pFileName File to check.
1046 - *
1047 - * @return mixed SQL archive.
1048 - */
1049 - public static function is_sql_archive( $pFileName ) {
1050 - return preg_match( '/(.*).sql.(zip|tar|tar.gz|tar.bz2)$/', $pFileName );
1051 - }
1065 + /**
1066 + * Method is_sql_file()
1067 + *
1068 + * Check if file is SQL.
1069 + *
1070 + * @param mixed $pFileName File to check.
1071 + *
1072 + * @return mixed SQL Files.
1073 + */
1074 + public static function is_sql_file( $pFileName ) {
1075 + return preg_match( '/(.*).sql$/', $pFileName ) || static::is_sql_archive( $pFileName );
1076 + }
1052 1077
1053 - /**
1054 - * Method get_current_archive_extension()
1055 - *
1056 - * Get extension of current Archive.
1057 - *
1058 - * @param bool $website true|false.
1059 - * @param bool|string $task true|false|global|site.
1060 - *
1061 - * @return mixed $archiveFormat Format of Archive.
1062 - *
1063 - * @uses \MainWP\Dashboard\MainWP_DB_Backup::get_website_backup_settings()
1064 - */
1065 - public static function get_current_archive_extension( $website = false, $task = false ) {
1066 - $useSite = true;
1067 - if ( false !== $task ) {
1068 - if ( 'global' === $task->archiveFormat ) {
1069 - $useGlobal = true;
1070 - $useSite = false;
1071 - } elseif ( '' === $task->archiveFormat || 'site' === $task->archiveFormat ) {
1072 - $useGlobal = false;
1073 - $useSite = true;
1074 - } else {
1075 - $archiveFormat = $task->archiveFormat;
1076 - $useGlobal = false;
1077 - $useSite = false;
1078 - }
1079 - }
1078 + /**
1079 + * Method is_sql_archive()
1080 + *
1081 + * Check if is SQL Archive.
1082 + *
1083 + * @param mixed $pFileName File to check.
1084 + *
1085 + * @return mixed SQL archive.
1086 + */
1087 + public static function is_sql_archive( $pFileName ) {
1088 + return preg_match( '/(.*).sql.(zip|tar|tar.gz|tar.bz2)$/', $pFileName );
1089 + }
1080 1090
1081 - if ( $useSite ) {
1082 - if ( false === $website ) {
1083 - $useGlobal = true;
1084 - } else {
1085 - $backupSettings = MainWP_DB_Backup::instance()->get_website_backup_settings( $website->id );
1086 - $archiveFormat = $backupSettings->archiveFormat;
1087 - $useGlobal = ( 'global' === $archiveFormat );
1088 - }
1089 - }
1091 + /**
1092 + * Method get_current_archive_extension()
1093 + *
1094 + * Get extension of current Archive.
1095 + *
1096 + * @param bool $website true|false.
1097 + * @param bool|string $task true|false|global|site.
1098 + *
1099 + * @return mixed $archiveFormat Format of Archive.
1100 + *
1101 + * @uses \MainWP\Dashboard\MainWP_DB_Backup::get_website_backup_settings()
1102 + */
1103 + public static function get_current_archive_extension( $website = false, $task = false ) {
1104 + $useSite = true;
1105 + if ( false !== $task ) {
1106 + if ( 'global' === $task->archiveFormat ) {
1107 + $useGlobal = true;
1108 + $useSite = false;
1109 + } elseif ( '' === $task->archiveFormat || 'site' === $task->archiveFormat ) {
1110 + $useGlobal = false;
1111 + $useSite = true;
1112 + } else {
1113 + $archiveFormat = $task->archiveFormat;
1114 + $useGlobal = false;
1115 + $useSite = false;
1116 + }
1117 + }
1090 1118
1091 - if ( $useGlobal ) {
1092 - $archiveFormat = get_option( 'mainwp_archiveFormat' );
1093 - if ( false === $archiveFormat ) {
1094 - $archiveFormat = 'tar.gz';
1095 - }
1096 - }
1119 + if ( $useSite ) {
1120 + if ( false === $website ) {
1121 + $useGlobal = true;
1122 + } else {
1123 + $backupSettings = MainWP_DB_Backup::instance()->get_website_backup_settings( $website->id );
1124 + $archiveFormat = $backupSettings->archiveFormat;
1125 + $useGlobal = ( 'global' === $archiveFormat );
1126 + }
1127 + }
1097 1128
1098 - return $archiveFormat;
1099 - }
1129 + if ( $useGlobal ) {
1130 + $archiveFormat = get_option( 'mainwp_archiveFormat' );
1131 + if ( false === $archiveFormat ) {
1132 + $archiveFormat = 'tar.gz';
1133 + }
1134 + }
1100 1135
1101 - /**
1102 - * Method get_real_extension()
1103 - *
1104 - * Get full file extension.
1105 - *
1106 - * @param mixed $path Path to file.
1107 - *
1108 - * @return mixed $check|pathinfo()
1109 - */
1110 - public static function get_real_extension( $path ) {
1111 - $checks = array( '.sql.zip', '.sql.tar', '.sql.tar.gz', '.sql.tar.bz2', '.tar.gz', '.tar.bz2' );
1112 - foreach ( $checks as $check ) {
1113 - if ( MainWP_Utility::ends_with( $path, $check ) ) {
1114 - return $check;
1115 - }
1116 - }
1136 + return $archiveFormat;
1137 + }
1117 1138
1118 - return '.' . pathinfo( $path, PATHINFO_EXTENSION );
1119 - }
1139 + /**
1140 + * Method get_real_extension()
1141 + *
1142 + * Get full file extension.
1143 + *
1144 + * @param mixed $path Path to file.
1145 + *
1146 + * @return mixed $check|pathinfo()
1147 + */
1148 + public static function get_real_extension( $path ) {
1149 + $checks = array( '.sql.zip', '.sql.tar', '.sql.tar.gz', '.sql.tar.bz2', '.tar.gz', '.tar.bz2' );
1150 + foreach ( $checks as $check ) {
1151 + if ( MainWP_Utility::ends_with( $path, $check ) ) {
1152 + return $check;
1153 + }
1154 + }
1120 1155
1156 + return '.' . pathinfo( $path, PATHINFO_EXTENSION );
1157 + }
1121 1158
1122 - /**
1123 - * Method handle_settings_post()
1124 - *
1125 - * Update Child Site Settings.
1126 - *
1127 - * @return bool true|false
1128 - *
1129 - * @uses \MainWP\Dashboard\MainWP_System_Utility::is_admin()
1130 - * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
1131 - */
1132 - public static function handle_settings_post() {
1133 - if ( MainWP_System_Utility::is_admin() ) {
1134 - if ( isset( $_POST['submit'] ) && isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'Settings' ) ) {
1135 - if ( isset( $_POST['mainwp_options_backupOnServer'] ) && 0 < $_POST['mainwp_options_backupOnServer'] ) {
1136 - MainWP_Utility::update_option( 'mainwp_backupsOnServer', ( isset( $_POST['mainwp_options_backupOnServer'] ) ? intval( $_POST['mainwp_options_backupOnServer'] ) : 1 ) );
1137 - }
1138 - if ( isset( $_POST['mainwp_options_maximumFileDescriptors'] ) && - 1 < $_POST['mainwp_options_maximumFileDescriptors'] ) {
1139 - MainWP_Utility::update_option( 'mainwp_maximumFileDescriptors', ( isset( $_POST['mainwp_options_maximumFileDescriptors'] ) ? intval( $_POST['mainwp_options_maximumFileDescriptors'] ) : 1 ) );
1140 - }
1141 - MainWP_Utility::update_option( 'mainwp_maximumFileDescriptorsAuto', ( ! isset( $_POST['mainwp_maximumFileDescriptorsAuto'] ) ? 0 : 1 ) );
1142 - if ( ! empty( $_POST['mainwp_options_backupOnExternalSources'] ) && 0 <= $_POST['mainwp_options_backupOnExternalSources'] ) {
1143 - MainWP_Utility::update_option( 'mainwp_backupOnExternalSources', ( isset( $_POST['mainwp_options_backupOnExternalSources'] ) ? intval( $_POST['mainwp_options_backupOnExternalSources'] ) : 1 ) );
1144 - }
1145 - MainWP_Utility::update_option( 'mainwp_archiveFormat', ( isset( $_POST['mainwp_archiveFormat'] ) ? sanitize_text_field( wp_unslash( $_POST['mainwp_archiveFormat'] ) ) : '' ) );
1146 1159
1147 - $old_primaryBackup = get_option( 'mainwp_primaryBackup' );
1148 - $old_enableLegacyBackup = get_option( 'mainwp_enableLegacyBackupFeature' );
1149 - $updated_enableLegacyBackup = false;
1160 + /**
1161 + * Method handle_settings_post()
1162 + *
1163 + * Update Child Site Settings.
1164 + *
1165 + * @return bool true|false
1166 + *
1167 + * @uses \MainWP\Dashboard\MainWP_System_Utility::is_admin()
1168 + * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
1169 + */
1170 + public static function handle_settings_post() {
1171 + if ( MainWP_System_Utility::is_admin() ) {
1172 + if ( isset( $_POST['submit'] ) && isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'Settings' ) ) {
1173 + if ( isset( $_POST['mainwp_options_backupOnServer'] ) && 0 < $_POST['mainwp_options_backupOnServer'] ) {
1174 + MainWP_Utility::update_option( 'mainwp_backupsOnServer', ( isset( $_POST['mainwp_options_backupOnServer'] ) ? intval( $_POST['mainwp_options_backupOnServer'] ) : 1 ) );
1175 + }
1176 + if ( isset( $_POST['mainwp_options_maximumFileDescriptors'] ) && - 1 < $_POST['mainwp_options_maximumFileDescriptors'] ) {
1177 + MainWP_Utility::update_option( 'mainwp_maximumFileDescriptors', ( isset( $_POST['mainwp_options_maximumFileDescriptors'] ) ? intval( $_POST['mainwp_options_maximumFileDescriptors'] ) : 1 ) );
1178 + }
1179 + MainWP_Utility::update_option( 'mainwp_maximumFileDescriptorsAuto', ( ! isset( $_POST['mainwp_maximumFileDescriptorsAuto'] ) ? 0 : 1 ) );
1180 + if ( ! empty( $_POST['mainwp_options_backupOnExternalSources'] ) && 0 <= $_POST['mainwp_options_backupOnExternalSources'] ) {
1181 + MainWP_Utility::update_option( 'mainwp_backupOnExternalSources', ( isset( $_POST['mainwp_options_backupOnExternalSources'] ) ? intval( $_POST['mainwp_options_backupOnExternalSources'] ) : 1 ) );
1182 + }
1183 + MainWP_Utility::update_option( 'mainwp_archiveFormat', ( isset( $_POST['mainwp_archiveFormat'] ) ? sanitize_text_field( wp_unslash( $_POST['mainwp_archiveFormat'] ) ) : '' ) );
1150 1184
1151 - if ( isset( $_POST['mainwp_primaryBackup'] ) ) {
1152 - if ( ! empty( $_POST['mainwp_primaryBackup'] ) ) { // not default backup method.
1153 - MainWP_Utility::update_option( 'mainwp_notificationOnBackupFail', 0 );
1154 - MainWP_Utility::update_option( 'mainwp_notificationOnBackupStart', 0 );
1155 - MainWP_Utility::update_option( 'mainwp_chunkedBackupTasks', 0 );
1156 - if ( empty( $old_primaryBackup ) ) {
1157 - MainWP_Utility::update_option( 'mainwp_enableLegacyBackupFeature', 0 );
1158 - $updated_enableLegacyBackup = true;
1159 - }
1160 - }
1161 - MainWP_Utility::update_option( 'mainwp_primaryBackup', sanitize_text_field( wp_unslash( $_POST['mainwp_primaryBackup'] ) ) );
1162 - }
1185 + $old_primaryBackup = get_option( 'mainwp_primaryBackup' );
1186 + $old_enableLegacyBackup = get_option( 'mainwp_enableLegacyBackupFeature' );
1187 + $updated_enableLegacyBackup = false;
1163 1188
1164 - if ( ! isset( $_POST['mainwp_primaryBackup'] ) || empty( $_POST['mainwp_primaryBackup'] ) ) {
1165 - MainWP_Utility::update_option( 'mainwp_options_loadFilesBeforeZip', ( ! isset( $_POST['mainwp_options_loadFilesBeforeZip'] ) ? 0 : 1 ) );
1166 - MainWP_Utility::update_option( 'mainwp_notificationOnBackupFail', ( ! isset( $_POST['mainwp_options_notificationOnBackupFail'] ) ? 0 : 1 ) );
1167 - MainWP_Utility::update_option( 'mainwp_notificationOnBackupStart', ( ! isset( $_POST['mainwp_options_notificationOnBackupStart'] ) ? 0 : 1 ) );
1168 - MainWP_Utility::update_option( 'mainwp_chunkedBackupTasks', ( ! isset( $_POST['mainwp_options_chunkedBackupTasks'] ) ? 0 : 1 ) );
1169 - }
1189 + if ( isset( $_POST['mainwp_primaryBackup'] ) ) {
1190 + if ( ! empty( $_POST['mainwp_primaryBackup'] ) ) { // not default backup method.
1191 + MainWP_Utility::update_option( 'mainwp_notificationOnBackupFail', 0 );
1192 + MainWP_Utility::update_option( 'mainwp_notificationOnBackupStart', 0 );
1193 + MainWP_Utility::update_option( 'mainwp_chunkedBackupTasks', 0 );
1194 + if ( empty( $old_primaryBackup ) ) {
1195 + MainWP_Utility::update_option( 'mainwp_enableLegacyBackupFeature', 0 );
1196 + $updated_enableLegacyBackup = true;
1197 + }
1198 + }
1199 + MainWP_Utility::update_option( 'mainwp_primaryBackup', sanitize_text_field( wp_unslash( $_POST['mainwp_primaryBackup'] ) ) );
1200 + }
1170 1201
1171 - $enableLegacyBackup = ( isset( $_POST['mainwp_options_enableLegacyBackupFeature'] ) && ! empty( $_POST['mainwp_options_enableLegacyBackupFeature'] ) ) ? 1 : 0;
1172 - if ( $enableLegacyBackup && empty( $old_enableLegacyBackup ) ) {
1173 - MainWP_Utility::update_option( 'mainwp_primaryBackup', '' );
1174 - }
1202 + if ( ! isset( $_POST['mainwp_primaryBackup'] ) || empty( $_POST['mainwp_primaryBackup'] ) ) {
1203 + MainWP_Utility::update_option( 'mainwp_options_loadFilesBeforeZip', ( ! isset( $_POST['mainwp_options_loadFilesBeforeZip'] ) ? 0 : 1 ) );
1204 + MainWP_Utility::update_option( 'mainwp_notificationOnBackupFail', ( ! isset( $_POST['mainwp_options_notificationOnBackupFail'] ) ? 0 : 1 ) );
1205 + MainWP_Utility::update_option( 'mainwp_notificationOnBackupStart', ( ! isset( $_POST['mainwp_options_notificationOnBackupStart'] ) ? 0 : 1 ) );
1206 + MainWP_Utility::update_option( 'mainwp_chunkedBackupTasks', ( ! isset( $_POST['mainwp_options_chunkedBackupTasks'] ) ? 0 : 1 ) );
1207 + }
1175 1208
1176 - if ( ! $updated_enableLegacyBackup ) {
1177 - MainWP_Utility::update_option( 'mainwp_enableLegacyBackupFeature', $enableLegacyBackup );
1178 - }
1179 - return true;
1180 - }
1181 - }
1182 - return false;
1183 - }
1209 + $enableLegacyBackup = ( isset( $_POST['mainwp_options_enableLegacyBackupFeature'] ) && ! empty( $_POST['mainwp_options_enableLegacyBackupFeature'] ) ) ? 1 : 0;
1210 + if ( $enableLegacyBackup && empty( $old_enableLegacyBackup ) ) {
1211 + MainWP_Utility::update_option( 'mainwp_primaryBackup', '' );
1212 + }
1184 1213
1185 - /**
1186 - * Method backup_get_file_size()
1187 - *
1188 - * Get file size.
1189 - *
1190 - * @param mixed $pFile Backup file.
1191 - *
1192 - * @return false|int
1193 - *
1194 - * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_specific_dir()
1195 - */
1196 - public static function backup_get_file_size( $pFile ) {
1197 - $dir = MainWP_System_Utility::get_mainwp_specific_dir();
1214 + if ( ! $updated_enableLegacyBackup ) {
1215 + MainWP_Utility::update_option( 'mainwp_enableLegacyBackupFeature', $enableLegacyBackup );
1216 + }
1217 + return true;
1218 + }
1219 + }
1220 + return false;
1221 + }
1198 1222
1199 - if ( stristr( $pFile, $dir ) && file_exists( $pFile ) ) {
1200 - return filesize( $pFile );
1201 - }
1223 + /**
1224 + * Method backup_get_file_size()
1225 + *
1226 + * Get file size.
1227 + *
1228 + * @param mixed $pFile Backup file.
1229 + *
1230 + * @return false|int
1231 + *
1232 + * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_specific_dir()
1233 + */
1234 + public static function backup_get_file_size( $pFile ) {
1235 + $dir = MainWP_System_Utility::get_mainwp_specific_dir();
1202 1236
1203 - return 0;
1204 - }
1237 + if ( stristr( $pFile, $dir ) && file_exists( $pFile ) ) {
1238 + return filesize( $pFile );
1239 + }
1240 +
1241 + return 0;
1242 + }
1205 1243 }