BackWPup – WordPress Backup Plugin - Version 3.3.4

Version Description

Download this release

Release Info

Developer Bueltge
Plugin Icon 128x128 BackWPup – WordPress Backup Plugin
Version 3.3.4
Comparing to
See all releases

Code changes from version 3.3.3 to 3.3.4

assets/images/banner-de.jpg ADDED
Binary file
assets/images/banner-en.jpg ADDED
Binary file
backwpup.php CHANGED
@@ -5,7 +5,7 @@
5
  * Description: WordPress Backup Plugin
6
  * Author: Inpsyde GmbH
7
  * Author URI: http://inpsyde.com
8
- * Version: 3.3.3
9
  * Text Domain: backwpup
10
  * Domain Path: /languages/
11
  * Network: true
@@ -33,8 +33,8 @@
33
 
34
  if ( ! class_exists( 'BackWPup' ) ) {
35
 
36
- // Don't activate on anything less than PHP 5.2.7 or WordPress 3.8
37
- if ( version_compare( PHP_VERSION, '5.2.7', '<' ) || version_compare( get_bloginfo( 'version' ), '3.8', '<' ) || ! function_exists( 'spl_autoload_register' ) ) {
38
  require_once ABSPATH . 'wp-admin/includes/plugin.php';
39
  deactivate_plugins( __FILE__ );
40
  die( 'BackWPup requires PHP version 5.2.7 with spl extension or greater and WordPress 3.8 or greater.' );
@@ -73,6 +73,10 @@ if ( ! class_exists( 'BackWPup' ) ) {
73
  if ( get_site_option( 'backwpup_version' ) !== self::get_plugin_data( 'Version' ) || ! wp_next_scheduled( 'backwpup_check_cleanup' ) ) {
74
  BackWPup_Install::activate();
75
  }
 
 
 
 
76
  //WP-Cron
77
  if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
78
  if ( ! empty( $_GET[ 'backwpup_run' ] ) && class_exists( 'BackWPup_Job' ) ) {
5
  * Description: WordPress Backup Plugin
6
  * Author: Inpsyde GmbH
7
  * Author URI: http://inpsyde.com
8
+ * Version: 3.3.4
9
  * Text Domain: backwpup
10
  * Domain Path: /languages/
11
  * Network: true
33
 
34
  if ( ! class_exists( 'BackWPup' ) ) {
35
 
36
+ // Don't activate on anything less than PHP 5.2.7 or WordPress 3.9
37
+ if ( version_compare( PHP_VERSION, '5.2.7', '<' ) || version_compare( get_bloginfo( 'version' ), '3.9', '<' ) || ! function_exists( 'spl_autoload_register' ) ) {
38
  require_once ABSPATH . 'wp-admin/includes/plugin.php';
39
  deactivate_plugins( __FILE__ );
40
  die( 'BackWPup requires PHP version 5.2.7 with spl extension or greater and WordPress 3.8 or greater.' );
73
  if ( get_site_option( 'backwpup_version' ) !== self::get_plugin_data( 'Version' ) || ! wp_next_scheduled( 'backwpup_check_cleanup' ) ) {
74
  BackWPup_Install::activate();
75
  }
76
+ //load pro features
77
+ if ( class_exists( 'BackWPup_Pro' ) ) {
78
+ BackWPup_Pro::get_instance();
79
+ }
80
  //WP-Cron
81
  if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
82
  if ( ! empty( $_GET[ 'backwpup_run' ] ) && class_exists( 'BackWPup_Job' ) ) {
inc/class-destination-s3.php CHANGED
@@ -2,6 +2,7 @@
2
  // Amazon S3 SDK v2.8.27
3
  // http://aws.amazon.com/de/sdkforphp2/
4
  // https://github.com/aws/aws-sdk-php
 
5
 
6
  /**
7
  * Documentation: http://docs.amazonwebservices.com/aws-sdk-php-2/latest/class-Aws.S3.S3Client.html
@@ -9,55 +10,6 @@
9
  class BackWPup_Destination_S3 extends BackWPup_Destinations {
10
 
11
 
12
- /**
13
- * @param $s3region
14
- * @param string $s3base_url
15
- * @return string
16
- */
17
- protected function get_s3_base_url( $s3region, $s3base_url = '' ) {
18
-
19
- if ( ! empty( $s3base_url ) )
20
- return $s3base_url;
21
-
22
- switch ( $s3region ) {
23
- case 'us-east-1':
24
- return 'https://s3.amazonaws.com';
25
- case 'us-west-1':
26
- return 'https://s3-us-west-1.amazonaws.com';
27
- case 'us-west-2':
28
- return 'https://s3-us-west-2.amazonaws.com';
29
- case 'eu-west-1':
30
- return 'https://s3-eu-west-1.amazonaws.com';
31
- case 'eu-central-1':
32
- return 'https://s3-eu-central-1.amazonaws.com';
33
- case 'ap-northeast-1':
34
- return 'https://s3-ap-northeast-1.amazonaws.com';
35
- case 'ap-northeast-2':
36
- return 'https://s3-ap-northeast-2.amazonaws.com';
37
- case 'ap-southeast-1':
38
- return 'https://s3-ap-southeast-1.amazonaws.com';
39
- case 'ap-southeast-2':
40
- return 'https://s3-ap-southeast-2.amazonaws.com';
41
- case 'sa-east-1':
42
- return 'https://s3-sa-east-1.amazonaws.com';
43
- case 'cn-north-1':
44
- return 'https:/cn-north-1.amazonaws.com';
45
- case 'google-storage':
46
- return 'https://storage.googleapis.com';
47
- case 'google-storage-us':
48
- return 'https://storage.googleapis.com';
49
- case 'google-storage-asia':
50
- return 'https://storage.googleapis.com';
51
- case 'dreamhost':
52
- return 'https://objects-us-west-1.dream.io';
53
- case 'greenqloud':
54
- return 'http://s.greenqloud.com';
55
- default:
56
- return '';
57
- }
58
-
59
- }
60
-
61
  /**
62
  * @return array
63
  */
@@ -66,7 +18,6 @@ class BackWPup_Destination_S3 extends BackWPup_Destinations {
66
  return array( 's3accesskey' => '', 's3secretkey' => '', 's3bucket' => '', 's3region' => 'us-east-1', 's3base_url' => '', 's3ssencrypt' => '', 's3storageclass' => '', 's3dir' => trailingslashit( sanitize_file_name( get_bloginfo( 'name' ) ) ), 's3maxbackups' => 15, 's3syncnodelete' => TRUE, 's3multipart' => TRUE );
67
  }
68
 
69
-
70
  /**
71
  * @param $jobid
72
  */
@@ -85,6 +36,7 @@ class BackWPup_Destination_S3 extends BackWPup_Destinations {
85
  <option value="us-west-2" <?php selected( 'us-west-2', BackWPup_Option::get( $jobid, 's3region' ), TRUE ) ?>><?php esc_html_e( 'Amazon S3: US West (Oregon)', 'backwpup' ); ?></option>
86
  <option value="eu-west-1" <?php selected( 'eu-west-1', BackWPup_Option::get( $jobid, 's3region' ), TRUE ) ?>><?php esc_html_e( 'Amazon S3: EU (Ireland)', 'backwpup' ); ?></option>
87
  <option value="eu-central-1" <?php selected( 'eu-central-1', BackWPup_Option::get( $jobid, 's3region' ), TRUE ) ?>><?php esc_html_e( 'Amazon S3: EU (Germany)', 'backwpup' ); ?></option>
 
88
  <option value="ap-northeast-1" <?php selected( 'ap-northeast-1', BackWPup_Option::get( $jobid, 's3region' ), TRUE ) ?>><?php esc_html_e( 'Amazon S3: Asia Pacific (Tokyo)', 'backwpup' ); ?></option>
89
  <option value="ap-northeast-2" <?php selected( 'ap-northeast-2', BackWPup_Option::get( $jobid, 's3region' ), TRUE ) ?>><?php esc_html_e( 'Amazon S3: Asia Pacific (Seoul)', 'backwpup' ); ?></option>
90
  <option value="ap-southeast-1" <?php selected( 'ap-southeast-1', BackWPup_Option::get( $jobid, 's3region' ), TRUE ) ?>><?php esc_html_e( 'Amazon S3: Asia Pacific (Singapore)', 'backwpup' ); ?></option>
@@ -214,6 +166,132 @@ class BackWPup_Destination_S3 extends BackWPup_Destinations {
214
  <?php
215
  }
216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
 
218
  /**
219
  * @param $jobid
@@ -599,7 +677,6 @@ class BackWPup_Destination_S3 extends BackWPup_Destinations {
599
  return TRUE;
600
  }
601
 
602
-
603
  /**
604
  * @param $job_settings array
605
  * @return bool
@@ -658,80 +735,4 @@ class BackWPup_Destination_S3 extends BackWPup_Destinations {
658
  </script>
659
  <?php
660
  }
661
-
662
- /**
663
- * @param string $args
664
- */
665
- public function edit_ajax( $args = '' ) {
666
-
667
- $error = '';
668
- $buckets_list = array();
669
-
670
- if ( is_array( $args ) ) {
671
- $ajax = FALSE;
672
- }
673
- else {
674
- if ( ! current_user_can( 'backwpup_jobs_edit' ) ) {
675
- wp_die( -1 );
676
- }
677
- check_ajax_referer( 'backwpup_ajax_nonce' );
678
- $args[ 's3accesskey' ] = sanitize_text_field( $_POST[ 's3accesskey' ] );
679
- $args[ 's3secretkey' ] = sanitize_text_field( $_POST[ 's3secretkey' ] );
680
- $args[ 's3bucketselected' ] = sanitize_text_field( $_POST[ 's3bucketselected' ] );
681
- $args[ 's3base_url' ] = esc_url_raw( $_POST[ 's3base_url' ] );
682
- $args[ 's3region' ] = sanitize_text_field( $_POST[ 's3region' ] );
683
- $ajax = TRUE;
684
- }
685
- echo '<span id="s3bucketerror" style="color:red;">';
686
-
687
- if ( ! empty( $args[ 's3accesskey' ] ) && ! empty( $args[ 's3secretkey' ] ) ) {
688
- try {
689
- $s3 = Aws\S3\S3Client::factory( array( 'key' => $args[ 's3accesskey' ],
690
- 'secret' => BackWPup_Encryption::decrypt( $args[ 's3secretkey' ] ),
691
- 'region' => $args[ 's3region' ],
692
- 'base_url' => $this->get_s3_base_url( $args[ 's3region' ], $args[ 's3base_url' ]),
693
- 'scheme' => 'https',
694
- 'ssl.certificate_authority' => BackWPup::get_plugin_data( 'cacert' )
695
- ) );
696
-
697
- $buckets = $s3->listBuckets();
698
- if ( ! empty( $buckets['Buckets'] ) ) {
699
- $buckets_list = $buckets['Buckets'];
700
- }
701
- while ( ! empty( $vaults['Marker'] ) ) {
702
- $buckets = $s3->listBuckets( array( 'marker' => $buckets['Marker'] ) );
703
- if ( ! empty( $buckets['Buckets'] ) ) {
704
- $buckets_list = array_merge( $buckets_list, $buckets['Buckets'] );
705
- }
706
- }
707
- }
708
- catch ( Exception $e ) {
709
- $error = $e->getMessage();
710
- }
711
- }
712
-
713
- if ( empty( $args[ 's3accesskey' ] ) )
714
- _e( 'Missing access key!', 'backwpup' );
715
- elseif ( empty( $args[ 's3secretkey' ] ) )
716
- _e( 'Missing secret access key!', 'backwpup' );
717
- elseif ( ! empty( $error ) && $error == 'Access Denied' )
718
- echo '<input type="text" name="s3bucket" id="s3bucket" value="' . esc_attr( $args[ 's3bucketselected' ] ) . '" >';
719
- elseif ( ! empty( $error ) )
720
- echo esc_html( $error );
721
- elseif ( ! isset( $buckets ) || count( $buckets['Buckets'] ) < 1 )
722
- _e( 'No bucket found!', 'backwpup' );
723
- echo '</span>';
724
-
725
- if ( !empty( $buckets_list ) ) {
726
- echo '<select name="s3bucket" id="s3bucket">';
727
- foreach ( $buckets_list as $bucket ) {
728
- echo "<option " . selected( $args[ 's3bucketselected' ], esc_attr( $bucket['Name'] ), FALSE ) . ">" . esc_attr( $bucket['Name'] ) . "</option>";
729
- }
730
- echo '</select>';
731
- }
732
-
733
- if ( $ajax ) {
734
- die();
735
- }
736
- }
737
  }
2
  // Amazon S3 SDK v2.8.27
3
  // http://aws.amazon.com/de/sdkforphp2/
4
  // https://github.com/aws/aws-sdk-php
5
+ // http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
6
 
7
  /**
8
  * Documentation: http://docs.amazonwebservices.com/aws-sdk-php-2/latest/class-Aws.S3.S3Client.html
10
  class BackWPup_Destination_S3 extends BackWPup_Destinations {
11
 
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  /**
14
  * @return array
15
  */
18
  return array( 's3accesskey' => '', 's3secretkey' => '', 's3bucket' => '', 's3region' => 'us-east-1', 's3base_url' => '', 's3ssencrypt' => '', 's3storageclass' => '', 's3dir' => trailingslashit( sanitize_file_name( get_bloginfo( 'name' ) ) ), 's3maxbackups' => 15, 's3syncnodelete' => TRUE, 's3multipart' => TRUE );
19
  }
20
 
 
21
  /**
22
  * @param $jobid
23
  */
36
  <option value="us-west-2" <?php selected( 'us-west-2', BackWPup_Option::get( $jobid, 's3region' ), TRUE ) ?>><?php esc_html_e( 'Amazon S3: US West (Oregon)', 'backwpup' ); ?></option>
37
  <option value="eu-west-1" <?php selected( 'eu-west-1', BackWPup_Option::get( $jobid, 's3region' ), TRUE ) ?>><?php esc_html_e( 'Amazon S3: EU (Ireland)', 'backwpup' ); ?></option>
38
  <option value="eu-central-1" <?php selected( 'eu-central-1', BackWPup_Option::get( $jobid, 's3region' ), TRUE ) ?>><?php esc_html_e( 'Amazon S3: EU (Germany)', 'backwpup' ); ?></option>
39
+ <option value="ap-south-1" <?php selected( 'ap-south-1', BackWPup_Option::get( $jobid, 's3region' ), TRUE ) ?>><?php esc_html_e( 'Amazon S3: Asia Pacific (Mumbai)', 'backwpup' ); ?></option>
40
  <option value="ap-northeast-1" <?php selected( 'ap-northeast-1', BackWPup_Option::get( $jobid, 's3region' ), TRUE ) ?>><?php esc_html_e( 'Amazon S3: Asia Pacific (Tokyo)', 'backwpup' ); ?></option>
41
  <option value="ap-northeast-2" <?php selected( 'ap-northeast-2', BackWPup_Option::get( $jobid, 's3region' ), TRUE ) ?>><?php esc_html_e( 'Amazon S3: Asia Pacific (Seoul)', 'backwpup' ); ?></option>
42
  <option value="ap-southeast-1" <?php selected( 'ap-southeast-1', BackWPup_Option::get( $jobid, 's3region' ), TRUE ) ?>><?php esc_html_e( 'Amazon S3: Asia Pacific (Singapore)', 'backwpup' ); ?></option>
166
  <?php
167
  }
168
 
169
+ /**
170
+ * @param string $args
171
+ */
172
+ public function edit_ajax( $args = '' ) {
173
+
174
+ $error = '';
175
+ $buckets_list = array();
176
+
177
+ if ( is_array( $args ) ) {
178
+ $ajax = FALSE;
179
+ }
180
+ else {
181
+ if ( ! current_user_can( 'backwpup_jobs_edit' ) ) {
182
+ wp_die( -1 );
183
+ }
184
+ check_ajax_referer( 'backwpup_ajax_nonce' );
185
+ $args[ 's3accesskey' ] = sanitize_text_field( $_POST[ 's3accesskey' ] );
186
+ $args[ 's3secretkey' ] = sanitize_text_field( $_POST[ 's3secretkey' ] );
187
+ $args[ 's3bucketselected' ] = sanitize_text_field( $_POST[ 's3bucketselected' ] );
188
+ $args[ 's3base_url' ] = esc_url_raw( $_POST[ 's3base_url' ] );
189
+ $args[ 's3region' ] = sanitize_text_field( $_POST[ 's3region' ] );
190
+ $ajax = TRUE;
191
+ }
192
+ echo '<span id="s3bucketerror" style="color:red;">';
193
+
194
+ if ( ! empty( $args[ 's3accesskey' ] ) && ! empty( $args[ 's3secretkey' ] ) ) {
195
+ try {
196
+ $s3 = Aws\S3\S3Client::factory( array( 'key' => $args[ 's3accesskey' ],
197
+ 'secret' => BackWPup_Encryption::decrypt( $args[ 's3secretkey' ] ),
198
+ 'region' => $args[ 's3region' ],
199
+ 'base_url' => $this->get_s3_base_url( $args[ 's3region' ], $args[ 's3base_url' ]),
200
+ 'scheme' => 'https',
201
+ 'ssl.certificate_authority' => BackWPup::get_plugin_data( 'cacert' )
202
+ ) );
203
+
204
+ $buckets = $s3->listBuckets();
205
+ if ( ! empty( $buckets['Buckets'] ) ) {
206
+ $buckets_list = $buckets['Buckets'];
207
+ }
208
+ while ( ! empty( $vaults['Marker'] ) ) {
209
+ $buckets = $s3->listBuckets( array( 'marker' => $buckets['Marker'] ) );
210
+ if ( ! empty( $buckets['Buckets'] ) ) {
211
+ $buckets_list = array_merge( $buckets_list, $buckets['Buckets'] );
212
+ }
213
+ }
214
+ }
215
+ catch ( Exception $e ) {
216
+ $error = $e->getMessage();
217
+ }
218
+ }
219
+
220
+ if ( empty( $args[ 's3accesskey' ] ) )
221
+ _e( 'Missing access key!', 'backwpup' );
222
+ elseif ( empty( $args[ 's3secretkey' ] ) )
223
+ _e( 'Missing secret access key!', 'backwpup' );
224
+ elseif ( ! empty( $error ) && $error == 'Access Denied' )
225
+ echo '<input type="text" name="s3bucket" id="s3bucket" value="' . esc_attr( $args[ 's3bucketselected' ] ) . '" >';
226
+ elseif ( ! empty( $error ) )
227
+ echo esc_html( $error );
228
+ elseif ( ! isset( $buckets ) || count( $buckets['Buckets'] ) < 1 )
229
+ _e( 'No bucket found!', 'backwpup' );
230
+ echo '</span>';
231
+
232
+ if ( !empty( $buckets_list ) ) {
233
+ echo '<select name="s3bucket" id="s3bucket">';
234
+ foreach ( $buckets_list as $bucket ) {
235
+ echo "<option " . selected( $args[ 's3bucketselected' ], esc_attr( $bucket['Name'] ), FALSE ) . ">" . esc_attr( $bucket['Name'] ) . "</option>";
236
+ }
237
+ echo '</select>';
238
+ }
239
+
240
+ if ( $ajax ) {
241
+ die();
242
+ }
243
+ }
244
+
245
+ /**
246
+ * @param $s3region
247
+ * @param string $s3base_url
248
+ * @return string
249
+ */
250
+ protected function get_s3_base_url( $s3region, $s3base_url = '' ) {
251
+
252
+ if ( ! empty( $s3base_url ) )
253
+ return $s3base_url;
254
+
255
+ switch ( $s3region ) {
256
+ case 'us-east-1':
257
+ return 'https://s3.amazonaws.com';
258
+ case 'us-west-1':
259
+ return 'https://s3-us-west-1.amazonaws.com';
260
+ case 'us-west-2':
261
+ return 'https://s3-us-west-2.amazonaws.com';
262
+ case 'eu-west-1':
263
+ return 'https://s3-eu-west-1.amazonaws.com';
264
+ case 'eu-central-1':
265
+ return 'https://s3-eu-central-1.amazonaws.com';
266
+ case 'ap-south-1':
267
+ return 'https://s3-ap-south-1.amazonaws.com';
268
+ case 'ap-northeast-1':
269
+ return 'https://s3-ap-northeast-1.amazonaws.com';
270
+ case 'ap-northeast-2':
271
+ return 'https://s3-ap-northeast-2.amazonaws.com';
272
+ case 'ap-southeast-1':
273
+ return 'https://s3-ap-southeast-1.amazonaws.com';
274
+ case 'ap-southeast-2':
275
+ return 'https://s3-ap-southeast-2.amazonaws.com';
276
+ case 'sa-east-1':
277
+ return 'https://s3-sa-east-1.amazonaws.com';
278
+ case 'cn-north-1':
279
+ return 'https://cn-north-1.amazonaws.com';
280
+ case 'google-storage':
281
+ return 'https://storage.googleapis.com';
282
+ case 'google-storage-us':
283
+ return 'https://storage.googleapis.com';
284
+ case 'google-storage-asia':
285
+ return 'https://storage.googleapis.com';
286
+ case 'dreamhost':
287
+ return 'https://objects-us-west-1.dream.io';
288
+ case 'greenqloud':
289
+ return 'http://s.greenqloud.com';
290
+ default:
291
+ return '';
292
+ }
293
+
294
+ }
295
 
296
  /**
297
  * @param $jobid
677
  return TRUE;
678
  }
679
 
 
680
  /**
681
  * @param $job_settings array
682
  * @return bool
735
  </script>
736
  <?php
737
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
738
  }
inc/class-job.php CHANGED
@@ -43,10 +43,6 @@ final class BackWPup_Job {
43
  * @var float Timestamp of last update off .running file
44
  */
45
  public $timestamp_last_update = 0;
46
- /**
47
- * @var float Timestamp of script start
48
- */
49
- private $timestamp_script_start = 0;
50
  /**
51
  * @var int Number of warnings
52
  */
@@ -115,25 +111,25 @@ final class BackWPup_Job {
115
  * @var int count of affected folders
116
  */
117
  public $count_folder = 0;
118
-
119
  /**
120
  * If job aborted from user
121
  * @var bool
122
  */
123
  public $user_abort = false;
124
-
125
- /**
126
- * Stores data that will only used in a single run
127
- * @var array
128
- */
129
- private $run = array();
130
-
131
  /**
132
  * A uniqid ID uniqid('', true); to identify process
133
  * @var string
134
  */
135
  public $uniqid = '';
136
-
 
 
 
 
 
 
 
 
137
  /**
138
  * @var string logging level (normal|normal_untranslated|debug|debug_untranslated)
139
  */
@@ -144,14 +140,101 @@ final class BackWPup_Job {
144
  */
145
  private $signal = 0;
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
  /**
149
- * Delete some data on cloned objects
 
 
 
150
  */
151
- public function __clone() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
- $this->temp = array();
154
- $this->run = array();
155
  }
156
 
157
  /**
@@ -445,455 +528,594 @@ final class BackWPup_Job {
445
  $this->steps_done[] = 'CREATE';
446
  }
447
 
448
-
449
  /**
 
 
 
450
  *
451
- * Get a url to run a job of BackWPup
452
- *
453
- * @param string $starttype Start types are 'runnow', 'runnowlink', 'cronrun', 'runext', 'restart', 'restartalt', 'test'
454
- * @param int $jobid The id of job to start else 0
455
- *
456
- * @return array|object [url] is the job url [header] for auth header or object form wp_remote_get()
457
  */
458
- public static function get_jobrun_url( $starttype, $jobid = 0 ) {
459
 
460
- $authentication = get_site_option( 'backwpup_cfg_authentication', array(
461
- 'method' => '',
462
- 'basic_user' => '',
463
- 'basic_password' => '',
464
- 'user_id' => 0,
465
- 'query_arg' => ''
466
- ) );
467
- $url = site_url( 'wp-cron.php' );
468
- $header = array( 'Cache-Control' => 'no-cache' );
469
- $authurl = '';
470
- $query_args = array(
471
- '_nonce' => substr( wp_hash( wp_nonce_tick() . 'backwpup_job_run-' . $starttype, 'nonce' ), - 12, 10 ),
472
- 'doing_wp_cron' => sprintf( '%.22F', microtime( true ) )
 
 
 
 
 
 
473
  );
474
 
475
- if ( in_array( $starttype, array( 'restart', 'runnow', 'cronrun', 'runext', 'test' ), true ) ) {
476
- $query_args['backwpup_run'] = $starttype;
477
  }
478
 
479
- if ( in_array( $starttype, array( 'runnowlink', 'runnow', 'cronrun', 'runext' ), true ) && ! empty( $jobid ) ) {
480
- $query_args['jobid'] = $jobid;
 
 
481
  }
482
 
483
- if ( ! empty( $authentication['basic_user'] ) && ! empty( $authentication['basic_password'] ) && $authentication['method'] == 'basic' ) {
484
- $header['Authorization'] = 'Basic ' . base64_encode( $authentication['basic_user'] . ':' . BackWPup_Encryption::decrypt( $authentication['basic_password'] ) );
485
- $authurl = urlencode( $authentication['basic_user'] ) . ':' . urlencode( BackWPup_Encryption::decrypt( $authentication['basic_password'] ) ) . '@';
486
- }
487
 
488
- if ( ! empty( $authentication['query_arg'] ) && $authentication['method'] == 'query_arg' ) {
489
- $url .= '?' . $authentication['query_arg'];
490
- }
 
 
 
 
 
491
 
492
- if ( $starttype === 'runext' ) {
493
- $query_args['_nonce'] = get_site_option( 'backwpup_cfg_jobrunauthkey' );
494
- $query_args['doing_wp_cron'] = null;
495
- if ( ! empty( $authurl ) ) {
496
- $url = str_replace( 'https://', 'https://' . $authurl, $url );
497
- $url = str_replace( 'http://', 'http://' . $authurl, $url );
498
- }
499
- }
500
 
501
- if ( $starttype === 'runnowlink' && ( ! defined( 'ALTERNATE_WP_CRON' ) || ! ALTERNATE_WP_CRON ) ) {
502
- $url = wp_nonce_url( network_admin_url( 'admin.php' ), 'backwpup_job_run-' . $starttype );
503
- $query_args['page'] = 'backwpupjobs';
504
- $query_args['action'] = 'runnow';
505
- $query_args['doing_wp_cron'] = null;
506
- unset( $query_args['_nonce'] );
507
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
508
 
509
- if ( $starttype === 'runnowlink' && defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
510
- $query_args['backwpup_run'] = 'runnowalt';
511
- $query_args['_nonce'] = substr( wp_hash( wp_nonce_tick() . 'backwpup_job_run-runnowalt', 'nonce' ), - 12, 10 );
512
- $query_args['doing_wp_cron'] = null;
513
- }
514
 
515
- if ( $starttype === 'restartalt' && defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
516
- $query_args['backwpup_run'] = 'restart';
517
- $query_args['_nonce'] = null;
518
- }
519
 
520
- if ( $starttype === 'restart' || $starttype === 'test' ) {
521
- $query_args['_nonce'] = null;
522
- }
523
 
524
- if ( ! empty( $authentication['user_id'] ) && $authentication['method'] === 'user' ) {
525
- //cache cookies for auth some
526
- $cookies = get_site_transient( 'backwpup_cookies' );
527
- if ( empty( $cookies ) ) {
528
- $wp_admin_user = get_users( array( 'role' => 'administrator', 'number' => 1 ) );
529
- if ( empty( $wp_admin_user ) ) {
530
- $wp_admin_user = get_users( array( 'role' => 'backwpup_admin', 'number' => 1 ) );
531
- }
532
- if ( ! empty( $wp_admin_user[0]->ID ) ) {
533
- $expiration = time() + ( 356 * DAY_IN_SECONDS );
534
- $manager = WP_Session_Tokens::get_instance( $wp_admin_user[0]->ID );
535
- $token = $manager->create( $expiration );
536
- $cookies[ LOGGED_IN_COOKIE ] = wp_generate_auth_cookie( $wp_admin_user[0]->ID, $expiration, 'logged_in', $token );
537
- }
538
- set_site_transient( 'backwpup_cookies', $cookies, HOUR_IN_SECONDS - 30 );
539
- }
540
- } else {
541
- $cookies = '';
542
- }
543
 
544
- $cron_request = array(
545
- 'url' => add_query_arg( $query_args, $url ),
546
- 'key' => $query_args['doing_wp_cron'],
547
- 'args' => array(
548
- 'blocking' => false,
549
- 'sslverify' => false,
550
- 'timeout' => 0.01,
551
- 'headers' => $header,
552
- 'user-agent' => BackWPup::get_plugin_data( 'User-Agent' )
553
- )
554
- );
555
 
556
- if ( ! empty( $cookies ) ) {
557
- foreach ( $cookies as $name => $value ) {
558
- $cron_request['args']['cookies'][] = new WP_Http_Cookie( array( 'name' => $name, 'value' => $value ) );
559
- }
560
  }
 
561
 
562
- $cron_request = apply_filters( 'cron_request', $cron_request );
 
 
 
 
 
 
 
 
 
 
563
 
564
- if ( $starttype === 'test' ) {
565
- $cron_request['args']['timeout'] = 15;
566
- $cron_request['args']['blocking'] = true;
567
  }
568
 
569
- if ( ! in_array( $starttype, array( 'runnowlink', 'runext', 'restartalt' ), true ) ) {
570
- delete_transient( 'doing_cron' );
571
-
572
- return wp_remote_post( $cron_request['url'], $cron_request['args'] );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
573
  }
574
 
575
- return $cron_request;
576
- }
 
 
577
 
 
 
 
 
 
 
 
 
578
 
579
- /**
580
- *
581
- */
582
- public static function start_http( $starttype, $jobid = 0 ) {
583
 
584
- //load text domain
585
- $log_level = get_site_option( 'backwpup_cfg_loglevel', 'normal_translated' );
586
- if ( strstr( $log_level, 'translated' ) ) {
587
- BackWPup::load_text_domain();
588
- } else {
589
- add_filter( 'override_load_textdomain', '__return_true' );
590
- $GLOBALS['l10n'] = array();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
591
  }
592
 
593
- if ( $starttype !== 'restart' ) {
594
 
595
- //check job id exists
596
- if ( $jobid !== BackWPup_Option::get( $jobid, 'jobid' ) ) {
597
- return false;
 
 
 
 
 
 
 
 
598
  }
 
 
 
 
 
599
 
600
- //check folders
601
- $log_folder = get_site_option( 'backwpup_cfg_logfolder' );
602
- $folder_message_log = BackWPup_File::check_folder( BackWPup_File::get_absolute_path( $log_folder ) );
603
- $folder_message_temp = BackWPup_File::check_folder( BackWPup::get_plugin_data( 'TEMP' ), true );
604
- if ( ! empty( $folder_message_log ) || ! empty( $folder_message_temp ) ) {
605
- BackWPup_Admin::message( $folder_message_log, true );
606
- BackWPup_Admin::message( $folder_message_temp, true );
607
- return false;
608
- }
609
  }
 
610
 
611
- // redirect
612
- if ( $starttype === 'runnowalt' ) {
613
- ob_start();
614
- wp_redirect( add_query_arg( array( 'page' => 'backwpupjobs' ), network_admin_url( 'admin.php' ) ) );
615
- echo ' ';
616
- flush();
617
- if ( $level = ob_get_level() ) {
618
- for ( $i = 0; $i < $level; $i ++ ) {
619
- ob_end_clean();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
620
  }
621
  }
622
  }
623
 
624
- // Should be preventing doubled running job's on http requests
625
- $random = mt_rand( 10, 90 ) * 10000;
626
- usleep( $random );
627
 
628
- //check running job
629
- $backwpup_job_object = self::get_working_data();
630
- //start class
631
- if ( ! $backwpup_job_object && in_array( $starttype, array( 'runnow', 'runnowalt', 'runext', 'cronrun' ), true ) && $jobid ) {
632
- //schedule restart event
633
- wp_schedule_single_event( time() + 60, 'backwpup_cron', array( 'id' => 'restart' ) );
634
- //start job
635
- $backwpup_job_object = new self();
636
- $backwpup_job_object->create( $starttype, $jobid );
637
- }
638
- if ( $backwpup_job_object ) {
639
- $backwpup_job_object->run();
640
- }
641
  }
642
 
643
  /**
644
- * @param $jobid
 
 
645
  */
646
- public static function start_cli( $jobid ) {
647
 
648
- if ( php_sapi_name() != 'cli' ) {
649
- return;
650
- }
651
 
652
- //define DOING_CRON to prevent caching
653
- if ( ! defined( 'DOING_CRON' ) ) {
654
- define( 'DOING_CRON', true );
655
- }
 
 
 
 
 
656
 
657
- //load text domain
658
- $log_level = get_site_option( 'backwpup_cfg_loglevel', 'normal_translated' );
659
- if ( strstr( $log_level, 'translated' ) ) {
660
- BackWPup::load_text_domain();
661
- } else {
662
- add_filter( 'override_load_textdomain', '__return_true' );
663
- $GLOBALS['l10n'] = array();
664
- }
665
 
666
- $jobid = absint( $jobid );
 
 
 
667
 
668
- //Logs Folder
669
- $log_folder = get_site_option( 'backwpup_cfg_logfolder' );
670
- $log_folder = BackWPup_File::get_absolute_path( $log_folder );
671
 
672
- //check job id exists
673
- $jobids = BackWPup_Option::get_job_ids();
674
- if ( ! in_array( $jobid, $jobids, true ) ) {
675
- die( __( 'Wrong BackWPup JobID', 'backwpup' ) );
676
- }
677
- //check folders
678
- $log_folder_message = BackWPup_File::check_folder( $log_folder );
679
- if ( ! empty( $log_folder_message ) ) {
680
- die( $log_folder_message );
681
- }
682
- $log_folder_message = BackWPup_File::check_folder( BackWPup::get_plugin_data( 'TEMP' ), true );
683
- if ( ! empty( $log_folder_message ) ) {
684
- die( $log_folder_message );
685
- }
686
- //check running job
687
- if ( file_exists( BackWPup::get_plugin_data( 'running_file' ) ) ) {
688
- die( __( 'A BackWPup job is already running', 'backwpup' ) );
689
- }
690
 
691
- //start class
692
- $backwpup_job_object = new self();
693
- $backwpup_job_object->create( 'runcli', (int) $jobid );
694
- $backwpup_job_object->run();
695
  }
696
 
697
-
698
  /**
699
- * disable caches
 
 
700
  */
701
- public static function disable_caches() {
702
 
703
- //Special settings
704
- @putenv( 'nokeepalive=1' );
705
- @ini_set( 'zlib.output_compression', 'Off' );
706
 
707
- // deactivate caches
708
- if ( ! defined( 'DONOTCACHEDB' ) ) {
709
- define( 'DONOTCACHEDB', true );
710
- }
711
- if ( ! defined( 'DONOTCACHEPAGE' ) ) {
712
- define( 'DONOTCACHEPAGE', true );
713
  }
714
- }
715
 
 
 
716
 
717
  /**
718
- * Run baby run
 
 
 
 
719
  */
720
- public function run() {
721
  global $wpdb;
722
- /* @var wpdb $wpdb */
723
 
724
- //disable output buffering
725
- if ( $level = ob_get_level() ) {
726
- for ( $i = 0; $i < $level; $i ++ ) {
727
- ob_end_clean();
728
- }
729
  }
730
 
731
- // Job can't run it is not created
732
- if ( empty( $this->steps_todo ) || empty( $this->logfile ) ) {
733
- $running_file = BackWPup::get_plugin_data( 'running_file' );
734
- if ( file_exists( $running_file ) ) {
735
- unlink( $running_file );
736
- }
737
 
 
 
 
738
  return;
739
  }
740
 
741
- //Check double running and inactivity
742
- $last_update = microtime( true ) - $this->timestamp_last_update;
743
- if ( ! empty( $this->pid ) && $last_update > 300 ) {
744
- $this->log( __( 'Job restarts due to inactivity for more than 5 minutes.', 'backwpup' ), E_USER_WARNING );
745
- } elseif ( ! empty( $this->pid ) ) {
746
- return;
747
  }
748
- // set timestamp of script start
749
- $this->timestamp_script_start = microtime( true );
750
- //set Pid
751
- $this->pid = self::get_pid();
752
- $this->uniqid = uniqid( '', true );
753
- //Early write new working file
754
- $this->write_running_file();
755
- if ( $this->is_debug() ) {
756
- @ini_set( 'error_log', $this->logfile );
757
- error_reporting( - 1 );
 
 
758
  }
759
- @ini_set( 'display_errors', '0' );
760
- @ini_set( 'log_errors', '1' );
761
- @ini_set( 'html_errors', '0' );
762
- @ini_set( 'report_memleaks', '1' );
763
- @ini_set( 'zlib.output_compression', '0' );
764
- @ini_set( 'implicit_flush', '0' );
765
- //set temp folder
766
- $can_set_temp_env = true;
767
- $protected_env_vars = explode( ',', ini_get( 'safe_mode_protected_env_vars' ) ); //removed in php 5.4.0
768
- foreach ( $protected_env_vars as $protected_env ) {
769
- if ( strtoupper( trim( $protected_env ) ) == 'TMPDIR' ) {
770
- $can_set_temp_env = false;
771
  }
 
 
 
772
  }
773
- if ( $can_set_temp_env ) {
774
- @putenv( 'TMPDIR=' . BackWPup::get_plugin_data( 'TEMP' ) );
775
- }
776
- //Write Wordpress DB errors to log
777
- $wpdb->suppress_errors( false );
778
- $wpdb->hide_errors();
779
- //set wp max memory limit
780
- @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
781
- //set error handler
782
- if ( ! empty( $this->logfile ) ) {
783
- if ( $this->is_debug() ) {
784
- set_error_handler( array( $this, 'log' ) );
785
- } else {
786
- set_error_handler( array( $this, 'log' ), E_ALL ^ E_NOTICE );
787
- }
788
  }
789
- set_exception_handler( array( $this, 'exception_handler' ) );
790
- // execute function on job shutdown register_shutdown_function( array( $this, 'shutdown' ) );
791
- add_action( 'shutdown', array( $this, 'shutdown' ) );
792
 
793
- if ( function_exists( 'pcntl_signal' ) ) {
794
- $signals = array(
795
- 'SIGHUP', //Term
796
- 'SIGINT', //Term
797
- 'SIGQUIT', //Core
798
- 'SIGILL', //Core
799
- //'SIGTRAP', //Core
800
- 'SIGABRT', //Core
801
- 'SIGBUS', //Core
802
- 'SIGFPE', //Core
803
- //'SIGKILL', //Term
804
- 'SIGSEGV', //Core
805
- //'SIGPIPE', Term
806
- //'SIGALRM', Term
807
- 'SIGTERM', //Term
808
- 'SIGSTKFLT', //Term
809
- 'SIGUSR1',//Term
810
- 'SIGUSR2', //Term
811
- //'SIGCHLD', //Ign
812
- //'SIGCONT', //Cont
813
- //'SIGSTOP', //Stop
814
- //'SIGTSTP', //Stop
815
- //'SIGTTIN', //Stop
816
- //'SIGTTOU', //Stop
817
- //'SIGURG', //Ign
818
- 'SIGXCPU', //Core
819
- 'SIGXFSZ', //Core
820
- //'SIGVTALRM', //Term
821
- //'SIGPROF', //Term
822
- //'SIGWINCH', //Ign
823
- //'SIGIO', //Term
824
- 'SIGPWR', //Term
825
- 'SIGSYS' //Core
826
- );
827
- $signals = apply_filters( 'backwpup_job_signals_to_handel', $signals );
828
- declare( ticks = 1 );
829
- $this->signal = 0;
830
- foreach ( $signals as $signal ) {
831
- if ( defined( $signal ) ) {
832
- pcntl_signal( constant( $signal ), array( $this, 'signal_handler' ), false );
833
- }
834
  }
 
835
  }
836
- $job_types = BackWPup::get_job_types();
837
- //go step by step
838
- foreach ( $this->steps_todo as $this->step_working ) {
839
- //Check if step already done
840
- if ( in_array( $this->step_working, $this->steps_done, true ) ) {
841
- continue;
842
- }
843
- //calc step percent
844
- if ( count( $this->steps_done ) > 0 ) {
845
- $this->step_percent = round( count( $this->steps_done ) / count( $this->steps_todo ) * 100 );
846
- } else {
847
- $this->step_percent = 1;
848
- }
849
- // do step tries
850
- while ( true ) {
851
- if ( $this->steps_data[ $this->step_working ]['STEP_TRY'] >= get_site_option( 'backwpup_cfg_jobstepretry' ) ) {
852
- $this->log( __( 'Step aborted: too many attempts!', 'backwpup' ), E_USER_ERROR );
853
- $this->temp = array();
854
- $this->steps_done[] = $this->step_working;
855
- $this->substeps_done = 0;
856
- $this->substeps_todo = 0;
857
- $this->do_restart();
858
- break;
859
- }
860
 
861
- $this->steps_data[ $this->step_working ]['STEP_TRY'] ++;
862
- $done = false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
863
 
864
- //executes the methods of job process
865
- if ( $this->step_working == 'CREATE_ARCHIVE' ) {
866
- $done = $this->create_archive();
867
- } elseif ( $this->step_working == 'CREATE_MANIFEST' ) {
868
- $done = $this->create_manifest();
869
- } elseif ( $this->step_working == 'END' ) {
870
- $this->end();
871
- break 2;
872
- } elseif ( strstr( $this->step_working, 'JOB_' ) ) {
873
- $done = $job_types[ str_replace( 'JOB_', '', $this->step_working ) ]->job_run( $this );
874
- } elseif ( strstr( $this->step_working, 'DEST_SYNC_' ) ) {
875
- $done = BackWPup::get_destination( str_replace( 'DEST_SYNC_', '', $this->step_working ) )->job_run_sync( $this );
876
- } elseif ( strstr( $this->step_working, 'DEST_' ) ) {
877
- $done = BackWPup::get_destination( str_replace( 'DEST_', '', $this->step_working ) )->job_run_archive( $this );
878
- } elseif ( ! empty( $this->steps_data[ $this->step_working ]['CALLBACK'] ) ) {
879
- $done = $this->steps_data[ $this->step_working ]['CALLBACK']( $this );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
880
  }
 
 
 
 
 
881
 
882
- // set step as done
883
- if ( $done === true ) {
884
- $this->temp = array();
885
- $this->steps_done[] = $this->step_working;
886
- $this->substeps_done = 0;
887
- $this->substeps_todo = 0;
888
- $this->update_working_data( true );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
889
  }
890
- if ( count( $this->steps_done ) < count( $this->steps_todo ) - 1 ) {
891
- $this->do_restart();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
892
  }
893
- if ( $done === true ) {
894
- break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
895
  }
896
  }
 
897
  }
898
  }
899
 
@@ -959,672 +1181,809 @@ final class BackWPup_Job {
959
  }
960
 
961
  /**
962
- * Do a job restart
963
  *
964
- * @param bool $do_restart_now should time restart now be done
965
  *
966
- * @return int remaining time
 
 
 
967
  */
968
- public function do_restart_time( $do_restart_now = false ) {
969
-
970
- //do restart after signal is send
971
- if ( $this->signal !== 0 ) {
972
- $this->steps_data[ $this->step_working ]['SAVE_STEP_TRY'] = $this->steps_data[ $this->step_working ]['STEP_TRY'];
973
- $this->steps_data[ $this->step_working ]['STEP_TRY'] -= 1;
974
- $this->do_restart( true );
975
- }
976
 
977
- $job_max_execution_time = get_site_option( 'backwpup_cfg_jobmaxexecutiontime' );
 
 
 
 
 
 
 
 
 
 
 
 
 
978
 
979
- if ( empty( $job_max_execution_time ) ) {
980
- return 300;
981
  }
982
 
983
- $execution_time = microtime( true ) - $this->timestamp_script_start;
984
-
985
- // do restart 3 sec. before max. execution time
986
- if ( $do_restart_now || $execution_time >= ( $job_max_execution_time - 3 ) ) {
987
- $this->steps_data[ $this->step_working ]['SAVE_STEP_TRY'] = $this->steps_data[ $this->step_working ]['STEP_TRY'];
988
- $this->steps_data[ $this->step_working ]['STEP_TRY'] -= 1;
989
- $this->do_restart( true );
990
  }
991
 
992
- return $job_max_execution_time - $execution_time;
 
 
 
993
 
994
- }
 
 
995
 
996
- /**
997
- * Get job restart time
998
- *
999
- * @return int remaining time
1000
- */
1001
- public function get_restart_time() {
1002
-
1003
- $job_max_execution_time = get_site_option( 'backwpup_cfg_jobmaxexecutiontime' );
1004
-
1005
- if ( empty( $job_max_execution_time ) ) {
1006
- return 300;
1007
  }
1008
 
1009
- $execution_time = microtime( true ) - $this->timestamp_script_start;
1010
-
1011
- return $job_max_execution_time - $execution_time - 3;
1012
- }
1013
-
1014
- /**
1015
- *
1016
- * Get data off a working job
1017
- *
1018
- * @return bool|object BackWPup_Job Object or Bool if file not exits
1019
- */
1020
- public static function get_working_data() {
1021
 
1022
- if ( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
1023
- clearstatcache( true, BackWPup::get_plugin_data( 'running_file' ) );
1024
- } else {
1025
- clearstatcache();
1026
  }
1027
 
1028
- if ( ! file_exists( BackWPup::get_plugin_data( 'running_file' ) ) ) {
1029
- return false;
 
1030
  }
1031
 
1032
- $file_data = file_get_contents( BackWPup::get_plugin_data( 'running_file' ), false, null, 8 );
1033
- if ( empty( $file_data ) ) {
1034
- return false;
1035
  }
1036
 
1037
- if ( $job_object = unserialize( $file_data ) ) {
1038
- if ( $job_object instanceof BackWPup_Job ) {
1039
- return $job_object;
 
 
 
 
 
 
 
 
 
 
 
 
1040
  }
 
 
1041
  }
1042
 
1043
- return false;
1044
-
1045
- }
1046
-
1047
- /**
1048
- *
1049
- * Reads a BackWPup logfile header and gives back a array of information
1050
- *
1051
- * @param string $logfile full logfile path
1052
- *
1053
- * @return array|bool
1054
- */
1055
- public static function read_logheader( $logfile ) {
1056
-
1057
- $usedmetas = array(
1058
- "date" => "logtime",
1059
- "backwpup_logtime" => "logtime", //old value of date
1060
- "backwpup_errors" => "errors",
1061
- "backwpup_warnings" => "warnings",
1062
- "backwpup_jobid" => "jobid",
1063
- "backwpup_jobname" => "name",
1064
- "backwpup_jobtype" => "type",
1065
- "backwpup_jobruntime" => "runtime",
1066
- "backwpup_backupfilesize" => "backupfilesize"
1067
  );
1068
 
1069
- //get metadata of logfile
1070
- $metas = array();
1071
- if ( is_readable( $logfile ) ) {
1072
- if ( '.gz' == substr( $logfile, - 3 ) ) {
1073
- $metas = (array) get_meta_tags( 'compress.zlib://' . $logfile );
1074
- } else {
1075
- $metas = (array) get_meta_tags( $logfile );
1076
  }
1077
  }
1078
 
1079
- //only output needed data
1080
- foreach ( $usedmetas as $keyword => $field ) {
1081
- if ( isset( $metas[ $keyword ] ) ) {
1082
- $joddata[ $field ] = $metas[ $keyword ];
1083
- } else {
1084
- $joddata[ $field ] = '';
1085
- }
1086
- }
1087
 
1088
- //convert date
1089
- if ( isset( $metas['date'] ) ) {
1090
- $joddata['logtime'] = strtotime( $metas['date'] ) + ( get_option( 'gmt_offset' ) * 3600 );
1091
  }
1092
 
1093
- //use file create date if none
1094
- if ( empty( $joddata['logtime'] ) ) {
1095
- $joddata['logtime'] = filectime( $logfile );
 
1096
  }
1097
 
1098
- return $joddata;
1099
  }
1100
 
1101
  /**
1102
- * Signal handler
1103
- * @param $signal_send
1104
  */
1105
- public function signal_handler( $signal_send ) {
1106
-
1107
- //known signals
1108
- $signals = array(
1109
- 'SIGHUP' => array(
1110
- 'description' => _x( 'Hangup detected on controlling terminal or death of controlling process', 'SIGHUP: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1111
- 'error' => E_USER_ERROR
1112
- ),
1113
- 'SIGINT' => array(
1114
- 'description' => _x( 'Interrupt from keyboard', 'SIGINT: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1115
- 'error' => E_USER_ERROR
1116
- ),
1117
- 'SIGQUIT' => array(
1118
- 'description' => _x( 'Quit from keyboard', 'SIGQUIT: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1119
- 'error' => E_USER_ERROR
1120
- ),
1121
- 'SIGILL' => array(
1122
- 'description' => _x( 'Illegal Instruction', 'SIGILL: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1123
- 'error' => E_USER_ERROR
1124
- ),
1125
- 'SIGABRT' => array(
1126
- 'description' => _x( 'Abort signal from abort(3)', 'SIGABRT: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1127
- 'error' => E_USER_NOTICE
1128
- ),
1129
- 'SIGBUS' => array(
1130
- 'description' => _x( 'Bus error (bad memory access)', 'SIGBUS: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1131
- 'error' => E_USER_ERROR
1132
- ),
1133
- 'SIGFPE' => array(
1134
- 'description' => _x( 'Floating point exception', 'SIGFPE: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1135
- 'error' => E_USER_ERROR
1136
- ),
1137
- 'SIGSEGV' => array(
1138
- 'description' => _x( 'Invalid memory reference', 'SIGSEGV: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1139
- 'error' => E_USER_ERROR
1140
- ),
1141
- 'SIGTERM' => array(
1142
- 'description' => _x( 'Termination signal', 'SIGTERM: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1143
- 'error' => E_USER_WARNING
1144
- ),
1145
- 'SIGSTKFLT' => array(
1146
- 'description' => _x( 'Stack fault on coprocessor', 'SIGSTKFLT: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1147
- 'error' => E_USER_ERROR
1148
- ),
1149
- 'SIGUSR1' => array(
1150
- 'description' => _x( 'User-defined signal 1', 'SIGUSR1: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1151
- 'error' => E_USER_NOTICE
1152
- ),
1153
- 'SIGUSR2' => array(
1154
- 'description' => _x( 'User-defined signal 2', 'SIGUSR2: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1155
- 'error' => E_USER_NOTICE
1156
- ),
1157
- 'SIGURG' => array(
1158
- 'description' => _x( 'Urgent condition on socket', 'SIGURG: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1159
- 'error' => E_USER_NOTICE
1160
- ),
1161
- 'SIGXCPU' => array(
1162
- 'description' => _x( 'CPU time limit exceeded', 'SIGXCPU: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1163
- 'error' => E_USER_ERROR
1164
- ),
1165
- 'SIGXFSZ' => array(
1166
- 'description' => _x( 'File size limit exceeded', 'SIGXFSZ: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1167
- 'error' => E_USER_ERROR
1168
- ),
1169
- 'SIGPWR' => array(
1170
- 'description' => _x( 'Power failure', 'SIGPWR: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1171
- 'error' => E_USER_ERROR
1172
- ),
1173
- 'SIGSYS' => array(
1174
- 'description' => _x( 'Bad argument to routine', 'SIGSYS: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
1175
- 'error' => E_USER_ERROR
1176
- ),
1177
- );
1178
 
1179
- foreach ( $signals as $signal => $config ) {
1180
- if ( defined( $signal ) && $signal_send === constant( $signal ) ) {
1181
- $this->log( sprintf( __( 'Signal "%1$s" (%2$s) is sent to script!', 'backwpup' ), $signal, $config['description'] ), $config['error'] );
1182
- $this->signal = $signal_send;
1183
- break;
1184
  }
1185
  }
1186
 
1187
- }
1188
-
1189
- /**
1190
- *
1191
- * Shutdown function is call if script terminates try to make a restart if needed
1192
- *
1193
- * Prepare the job for start
1194
- *
1195
- * @internal param int the signal that terminates the job
1196
- */
1197
- public function shutdown() {
1198
 
1199
- //Put last error to log if one
1200
- $lasterror = error_get_last();
1201
- if ( $lasterror['type'] === E_ERROR || $lasterror['type'] === E_PARSE || $lasterror['type'] === E_CORE_ERROR || $lasterror['type'] === E_CORE_WARNING || $lasterror['type'] === E_COMPILE_ERROR || $lasterror['type'] === E_COMPILE_WARNING ) {
1202
- $this->log( $lasterror['type'], $lasterror['message'], $lasterror['file'], $lasterror['line'] );
1203
  }
1204
 
1205
- $error = false;
1206
- if ( function_exists( 'pcntl_get_last_error' ) ) {
1207
- $error = pcntl_get_last_error();
1208
- if ( ! empty( $error ) ) {
1209
- $error_msg = pcntl_strerror( $error );
1210
- if ( ! empty( $error_msg ) ) {
1211
- $error = '(' . $error . ') ' . $error_msg;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1212
  }
1213
  }
1214
- if ( ! empty( $error ) ) {
1215
- $this->log( sprintf( __( 'System: %s', 'backwpup' ), $error ), E_USER_ERROR );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1216
  }
1217
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1218
 
1219
- if ( function_exists( 'posix_get_last_error' ) && ! $error ) {
1220
- $error = posix_get_last_error();
1221
- if ( ! empty( $error ) ) {
1222
- $error_msg = posix_strerror( $error );
1223
- if ( ! empty( $error_msg ) ) {
1224
- $error = '(' . $error . ') ' . $error_msg;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1225
  }
1226
  }
1227
- if ( ! empty( $error ) ) {
1228
- $this->log( sprintf( __( 'System: %s', 'backwpup' ), $error ), E_USER_ERROR );
1229
- }
1230
  }
1231
 
1232
- $this->do_restart( true );
1233
  }
1234
 
1235
  /**
 
1236
  *
1237
- * The uncouth exception handler
1238
- *
1239
- * @param object $exception
1240
  */
1241
- public function exception_handler( $exception ) {
1242
 
1243
- $this->log( sprintf( __( 'Exception caught in %1$s: %2$s', 'backwpup' ), get_class( $exception ), $exception->getMessage() ), E_USER_ERROR, $exception->getFile(), $exception->getLine() );
 
 
 
 
 
 
 
 
 
 
 
 
1244
  }
1245
 
1246
  /**
1247
- * Write messages to log file
1248
  *
1249
- * @param string $message the error message
1250
- * @param int $type the error number (E_USER_ERROR,E_USER_WARNING,E_USER_NOTICE, ...)
1251
- * @param string $file the full path of file with error (__FILE__)
1252
- * @param int $line the line in that is the error (__LINE__)
1253
  *
1254
- * @return bool true
1255
  */
1256
- public function log( $message, $type = E_USER_NOTICE, $file = '', $line = 0 ) {
1257
 
1258
- // if error has been suppressed with an @
1259
- if ( error_reporting() == 0 ) {
1260
- return true;
1261
  }
1262
 
1263
- //if first the type an second the message switch it on user errors
1264
- if ( ! is_int( $type ) && is_int( $message ) && in_array( $message, array(
1265
- 1,
1266
- 2,
1267
- 4,
1268
- 8,
1269
- 16,
1270
- 32,
1271
- 64,
1272
- 128,
1273
- 256,
1274
- 512,
1275
- 1024,
1276
- 2048,
1277
- 4096,
1278
- 8192,
1279
- 16384
1280
- ), true )
1281
- ) {
1282
- $temp = $message;
1283
- $message = $type;
1284
- $type = $temp;
1285
  }
1286
 
1287
- //json message if array or object
1288
- if ( is_array( $message ) || is_object( $message ) ) {
1289
- $message = json_encode( $message );
1290
- }
1291
 
1292
- //if not set line and file get it
1293
- if ( $this->is_debug() ) {
1294
- if ( empty( $file ) || empty( $line ) ) {
1295
- $debug_info = debug_backtrace();
1296
- $file = $debug_info[0]['file'];
1297
- $line = $debug_info[0]['line'];
1298
- }
1299
  }
1300
 
1301
- $error = false;
1302
- $warning = false;
1303
 
1304
- switch ( $type ) {
1305
- case E_NOTICE:
1306
- case E_USER_NOTICE:
1307
- break;
1308
- case E_WARNING:
1309
- case E_CORE_WARNING:
1310
- case E_COMPILE_WARNING:
1311
- case E_USER_WARNING:
1312
- $this->warnings ++;
1313
- $warning = true;
1314
- $message = __( 'WARNING:', 'backwpup' ) . ' ' . $message;
1315
- break;
1316
- case E_ERROR:
1317
- case E_PARSE:
1318
- case E_CORE_ERROR:
1319
- case E_COMPILE_ERROR:
1320
- case E_USER_ERROR:
1321
- $this->errors ++;
1322
- $error = true;
1323
- $message = __( 'ERROR:', 'backwpup' ) . ' ' . $message;
1324
- break;
1325
- case 8192: //E_DEPRECATED comes with php 5.3
1326
- case 16384: //E_USER_DEPRECATED comes with php 5.3
1327
- $message = __( 'DEPRECATED:', 'backwpup' ) . ' ' . $message;
1328
- break;
1329
- case E_STRICT:
1330
- $message = __( 'STRICT NOTICE:', 'backwpup' ) . ' ' . $message;
1331
- break;
1332
- case E_RECOVERABLE_ERROR:
1333
- $this->errors ++;
1334
- $error = true;
1335
- $message = __( 'RECOVERABLE ERROR:', 'backwpup' ) . ' ' . $message;
1336
- break;
1337
- default:
1338
- $message = $type . ': ' . $message;
1339
- break;
1340
  }
1341
 
1342
- $in_file = $this->get_destination_path_replacement( $file );
 
1343
 
1344
- //print message to cli
1345
- if ( defined( 'WP_CLI' ) && WP_CLI ) {
1346
- $output_message = str_replace( array( '&hellip;', '&#160;' ), array( '...', ' ' ), esc_html( $message ) );
1347
- if ( ! call_user_func( array( '\cli\Shell', 'isPiped' ) ) ) {
1348
- if ( $error ) {
1349
- $output_message = '%r' . $output_message . '%n';
1350
- }
1351
- if ( $warning ) {
1352
- $output_message = '%y' . $output_message . '%n';
1353
- }
1354
- $output_message = call_user_func( array( '\cli\Colors', 'colorize' ), $output_message, true );
1355
- }
1356
- WP_CLI::line( $output_message );
1357
- } elseif ( php_sapi_name() == 'cli' && defined( 'STDOUT' ) ) {
1358
- $output_message = str_replace( array( '&hellip;', '&#160;' ), array( '...', ' ' ), esc_html( $message ) ) . PHP_EOL;
1359
- fwrite( STDOUT, $output_message );
1360
- }
1361
 
1362
- //timestamp for log file
1363
- $debug_info = '';
1364
- if ( $this->is_debug() ) {
1365
- $debug_info = ' title="[Type: ' . $type . '|Line: ' . $line . '|File: ' . $in_file . '|Mem: ' . size_format( @memory_get_usage( true ), 2 ) . '|Mem Max: ' . size_format( @memory_get_peak_usage( true ), 2 ) . '|Mem Limit: ' . ini_get( 'memory_limit' ) . '|PID: ' . self::get_pid() . ' | UniqID: ' . $this->uniqid . '|Query\'s: ' . get_num_queries() . ']"';
1366
- }
1367
- $timestamp = '<span datetime="' . date( 'c' ) . '" ' . $debug_info . '>[' . date( 'd-M-Y H:i:s', current_time( 'timestamp' ) ) . ']</span> ';
1368
 
1369
- //set last Message
1370
- if ( $error ) {
1371
- $output_message = '<span style="background-color:#ff6766;color:black;padding:0 2px;">' . esc_html( $message ) . '</span>';
1372
- $this->lasterrormsg = $output_message;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1373
  }
1374
- elseif ( $warning ) {
1375
- $output_message = '<span style="background-color:#ffc766;color:black;padding:0 2px;">' . esc_html( $message ) . '</span>';
1376
- $this->lasterrormsg = $output_message;
 
 
 
 
 
 
 
 
 
 
 
 
 
1377
  }
1378
- else {
1379
- $output_message = esc_html( $message );
1380
- $this->lastmsg = $output_message;
1381
  }
1382
- //write log file
1383
- if ( $this->logfile ) {
1384
- if ( ! file_put_contents( $this->logfile, $timestamp . $output_message . '<br />' . PHP_EOL, FILE_APPEND ) ) {
1385
- $this->logfile = '';
1386
- restore_error_handler();
1387
- trigger_error( esc_html( $message ), $type );
1388
- }
1389
 
1390
- //write new log header
1391
- if ( ( $error || $warning ) && $this->logfile ) {
1392
- if ( $fd = fopen( $this->logfile, 'r+' ) ) {
1393
- $file_pos = ftell( $fd );
1394
- while ( ! feof( $fd ) ) {
1395
- $line = fgets( $fd );
1396
- if ( $error && stripos( $line, '<meta name="backwpup_errors" content="' ) !== false ) {
1397
- fseek( $fd, $file_pos );
1398
- fwrite( $fd, str_pad( '<meta name="backwpup_errors" content="' . $this->errors . '" />', 100 ) . PHP_EOL );
1399
- break;
1400
- }
1401
- if ( $warning && stripos( $line, '<meta name="backwpup_warnings" content="' ) !== false ) {
1402
- fseek( $fd, $file_pos );
1403
- fwrite( $fd, str_pad( '<meta name="backwpup_warnings" content="' . $this->warnings . '" />', 100 ) . PHP_EOL );
1404
- break;
1405
- }
1406
- $file_pos = ftell( $fd );
1407
- }
1408
- fclose( $fd );
1409
- }
1410
- }
1411
  }
 
1412
 
1413
- //write working data
1414
- $this->update_working_data( $error || $warning );
 
 
 
 
 
1415
 
1416
- //true for no more php error handling.
1417
  return true;
1418
  }
1419
 
1420
  /**
1421
- *
1422
- * Write the Working data to display the process or that i can executes again
1423
- * The write will only done every second
1424
- *
1425
- * @param bool $must
1426
  */
1427
- public function update_working_data( $must = false ) {
1428
- global $wpdb;
1429
-
1430
- //to reduce server load
1431
- if ( get_site_option( 'backwpup_cfg_jobwaittimems' ) > 0 && get_site_option( 'backwpup_cfg_jobwaittimems' ) <= 500000 ) {
1432
- usleep( get_site_option( 'backwpup_cfg_jobwaittimems' ) );
1433
- }
1434
-
1435
- //check free memory
1436
- $this->need_free_memory( '10M' );
1437
 
1438
- //only run every 1 sec.
1439
- $time_to_update = microtime( true ) - $this->timestamp_last_update;
1440
- if ( $time_to_update < 1 && ! $must ) {
1441
  return;
1442
  }
1443
 
1444
- //FCGI must have a permanent output so that it not broke
1445
- if ( get_site_option( 'backwpup_cfg_jobdooutput' ) && ! defined( 'STDOUT' ) ) {
1446
- echo str_repeat( ' ', 12 );
1447
- flush();
1448
- }
1449
-
1450
- //set execution time again for 5 min
1451
- @set_time_limit( 300 );
1452
-
1453
- //calc sub step percent
1454
- if ( $this->substeps_todo > 0 && $this->substeps_done > 0 ) {
1455
- $this->substep_percent = round( $this->substeps_done / $this->substeps_todo * 100 );
1456
- } else {
1457
- $this->substep_percent = 1;
1458
  }
1459
 
1460
- //check if job aborted
1461
- if ( ! file_exists( BackWPup::get_plugin_data( 'running_file' ) ) ) {
1462
- if ( $this->step_working !== 'END' ) {
1463
- $this->end();
1464
- }
1465
  } else {
1466
- $this->timestamp_last_update = microtime( true ); //last update of working file
1467
- $this->write_running_file();
1468
- }
1469
-
1470
- if ( $this->signal !== 0 ) {
1471
- $this->do_restart();
1472
  }
1473
- }
1474
 
1475
- private function write_running_file() {
1476
 
1477
- $clone = clone $this;
1478
- $data = '<?php //' . serialize( $clone );
 
1479
 
1480
- $write = file_put_contents( BackWPup::get_plugin_data( 'running_file' ), $data );
1481
- if ( ! $write || $write < strlen( $data ) ) {
1482
- unlink( BackWPup::get_plugin_data( 'running_file' ) );
1483
- $this->log( __( 'Cannot write progress to working file. Job will be aborted.', 'backwpup' ), E_USER_ERROR );
 
 
 
 
 
 
 
 
 
 
 
 
 
1484
  }
 
 
 
 
 
1485
  }
1486
 
1487
  /**
1488
- *
1489
- * Called on job stop makes cleanup and terminates the script
1490
- *
1491
  */
1492
- private function end() {
1493
-
1494
- $this->step_working = 'END';
1495
- $this->substeps_todo = 1;
1496
 
1497
- if ( ! file_exists( BackWPup::get_plugin_data( 'running_file' ) ) ) {
1498
- $this->log( __( 'Aborted by user!', 'backwpup' ), E_USER_ERROR );
1499
- }
1500
 
1501
- //delete old logs
1502
- if ( get_site_option( 'backwpup_cfg_maxlogs' ) ) {
1503
- $log_file_list = array();
1504
- $log_folder = trailingslashit( dirname( $this->logfile ) );
1505
- if ( is_readable( $log_folder ) && $dir = opendir( $log_folder ) ) { //make file list
1506
- while ( ( $file = readdir( $dir ) ) !== false ) {
1507
- if ( strpos( $file, 'backwpup_log_' ) == 0 && false !== strpos( $file, '.html' ) ) {
1508
- $log_file_list[ filemtime( $log_folder . $file ) ] = $file;
1509
- }
1510
- }
1511
- closedir( $dir );
1512
- }
1513
- if ( sizeof( $log_file_list ) > 0 ) {
1514
- krsort( $log_file_list, SORT_NUMERIC );
1515
- $num_delete_files = 0;
1516
- $i = - 1;
1517
- foreach ( $log_file_list AS $log_file ) {
1518
- $i ++;
1519
- if ( $i < get_site_option( 'backwpup_cfg_maxlogs' ) ) {
1520
- continue;
1521
- }
1522
- unlink( $log_folder . $log_file );
1523
- $num_delete_files ++;
1524
- }
1525
- if ( $num_delete_files > 0 ) {
1526
- $this->log( sprintf( _n( 'One old log deleted', '%d old logs deleted', $num_delete_files, 'backwpup' ), $num_delete_files ) );
1527
- }
1528
- }
1529
  }
1530
-
1531
- //Display job working time
1532
- if ( $this->errors > 0 ) {
1533
- $this->log( sprintf( __( 'Job has ended with errors in %s seconds. You must resolve the errors for correct execution.', 'backwpup' ), current_time( 'timestamp' ) - $this->start_time ), E_USER_ERROR );
1534
- } elseif ( $this->warnings > 0 ) {
1535
- $this->log( sprintf( __( 'Job finished with warnings in %s seconds. Please resolve them for correct execution.', 'backwpup' ), current_time( 'timestamp' ) - $this->start_time ), E_USER_WARNING );
1536
- } else {
1537
- $this->log( sprintf( __( 'Job done in %s seconds.', 'backwpup' ), current_time( 'timestamp' ) - $this->start_time ) );
1538
  }
 
1539
 
1540
- //Update job options
1541
- $this->job['lastruntime'] = current_time( 'timestamp' ) - $this->start_time;
1542
- BackWPup_Option::update( $this->job['jobid'], 'lastruntime', $this->job['lastruntime'] );
1543
-
1544
-
1545
- //write header info
1546
- if ( ! empty( $this->logfile ) ) {
1547
-
1548
- if ( $fd = fopen( $this->logfile, 'r+' ) ) {
1549
- $filepos = ftell( $fd );
1550
- $found = 0;
1551
- while ( ! feof( $fd ) ) {
1552
- $line = fgets( $fd );
1553
- if ( stripos( $line, '<meta name="backwpup_jobruntime"' ) !== false ) {
1554
- fseek( $fd, $filepos );
1555
- fwrite( $fd, str_pad( '<meta name="backwpup_jobruntime" content="' . $this->job['lastruntime'] . '" />', 100 ) . PHP_EOL );
1556
- $found ++;
1557
- }
1558
- if ( stripos( $line, '<meta name="backwpup_backupfilesize"' ) !== false ) {
1559
- fseek( $fd, $filepos );
1560
- fwrite( $fd, str_pad( '<meta name="backwpup_backupfilesize" content="' . $this->backup_filesize . '" />', 100 ) . PHP_EOL );
1561
- $found ++;
1562
- }
1563
- if ( $found >= 2 ) {
1564
- break;
1565
- }
1566
- $filepos = ftell( $fd );
1567
- }
1568
- fclose( $fd );
1569
- }
1570
-
1571
- //Send mail with log
1572
- $sendmail = false;
1573
- if ( $this->job['mailaddresslog'] ) {
1574
- $sendmail = true;
1575
- }
1576
- if ( $this->errors === 0 && $this->job['mailerroronly'] ) {
1577
- $sendmail = false;
1578
- }
1579
- if ( $sendmail ) {
1580
- //special subject
1581
- $status = __( 'SUCCESSFUL', 'backwpup' );
1582
- if ( $this->warnings > 0 ) {
1583
- $status = __( 'WARNING', 'backwpup' );
1584
- }
1585
- if ( $this->errors > 0 ) {
1586
- $status = __( 'ERROR', 'backwpup' );
1587
- }
1588
-
1589
- $subject = sprintf( __( '[%3$s] BackWPup log %1$s: %2$s', 'backwpup' ), date_i18n( 'd-M-Y H:i', $this->start_time, true ), esc_attr( $this->job['name'] ), $status );
1590
- $headers = array();
1591
- $headers[] = 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' );
1592
- if ( $this->job['mailaddresssenderlog'] ) {
1593
- $this->job['mailaddresssenderlog'] = str_replace( array( '&lt;', '&gt;' ), array( '<', '>' ), $this->job['mailaddresssenderlog'] );
1594
 
1595
- $bracket_pos = strpos( $this->job['mailaddresssenderlog'], '<' );
1596
- $at_pos = strpos( $this->job['mailaddresssenderlog'], '@' );
1597
- if ( $bracket_pos === false || $at_pos === false ) {
1598
- $this->job['mailaddresssenderlog'] = str_replace( array( '<', '>' ), '', $this->job['mailaddresssenderlog'] ) . ' <' . get_bloginfo( 'admin_email' ) . '>';
1599
- }
 
 
 
 
 
 
1600
 
1601
- $headers[] = 'From: ' . $this->job['mailaddresssenderlog'];
1602
- }
1603
- wp_mail( $this->job['mailaddresslog'], $subject, file_get_contents( $this->logfile ), $headers );
 
 
 
 
1604
  }
1605
  }
1606
 
1607
- //set done
1608
- $this->substeps_done = 1;
1609
- $this->steps_done[] = 'END';
1610
-
1611
- //clean up temp
1612
- self::clean_temp_folder();
1613
-
1614
- //remove shutdown action
1615
- remove_action( 'shutdown', array( $this, 'shutdown' ) );
1616
- restore_exception_handler();
1617
- restore_error_handler();
1618
 
1619
- //logfile end
1620
- file_put_contents( $this->logfile, "</body>" . PHP_EOL . "</html>", FILE_APPEND );
 
 
1621
 
1622
- BackWPup_Cron::check_cleanup();
 
 
 
1623
 
1624
- exit();
1625
  }
1626
 
1627
-
1628
  public static function user_abort() {
1629
 
1630
  /* @var $job_object BackWPup_Job */
@@ -1645,80 +2004,6 @@ final class BackWPup_Job {
1645
 
1646
  }
1647
 
1648
- /**
1649
- *
1650
- * Increase automatically the memory that is needed
1651
- *
1652
- * @param int|string $memneed of the needed memory
1653
- */
1654
- public function need_free_memory( $memneed ) {
1655
-
1656
- //need memory
1657
- $needmemory = @memory_get_usage( true ) + self::convert_hr_to_bytes( $memneed );
1658
- // increase Memory
1659
- if ( $needmemory > self::convert_hr_to_bytes( ini_get( 'memory_limit' ) ) ) {
1660
- $newmemory = round( $needmemory / 1024 / 1024 ) + 1 . 'M';
1661
- if ( $needmemory >= 1073741824 ) {
1662
- $newmemory = round( $needmemory / 1024 / 1024 / 1024 ) . 'G';
1663
- }
1664
- @ini_set( 'memory_limit', $newmemory );
1665
- }
1666
- }
1667
-
1668
-
1669
- /**
1670
- *
1671
- * Converts hr to bytes
1672
- *
1673
- * @param $size
1674
- *
1675
- * @return int
1676
- */
1677
- public static function convert_hr_to_bytes( $size ) {
1678
- $size = strtolower( $size );
1679
- $bytes = (int) $size;
1680
- if ( strpos( $size, 'k' ) !== false ) {
1681
- $bytes = intval( $size ) * 1024;
1682
- } elseif ( strpos( $size, 'm' ) !== false ) {
1683
- $bytes = intval( $size ) * 1024 * 1024;
1684
- } elseif ( strpos( $size, 'g' ) !== false ) {
1685
- $bytes = intval( $size ) * 1024 * 1024 * 1024;
1686
- }
1687
-
1688
- return $bytes;
1689
- }
1690
-
1691
- /**
1692
- *
1693
- * Callback for the CURLOPT_READFUNCTION that submit the transferred bytes
1694
- * to build the process bar
1695
- *
1696
- * @param $curl_handle
1697
- * @param $file_handle
1698
- * @param $read_count
1699
- *
1700
- * @return string
1701
- * @internal param $out
1702
- */
1703
- public function curl_read_callback( $curl_handle, $file_handle, $read_count ) {
1704
-
1705
- $data = null;
1706
- if ( ! empty( $file_handle ) && is_numeric( $read_count ) ) {
1707
- $data = fread( $file_handle, $read_count );
1708
- }
1709
-
1710
- if ( $this->job['backuptype'] == 'sync' ) {
1711
- return $data;
1712
- }
1713
-
1714
- $length = ( is_numeric( $read_count ) ) ? $read_count : strlen( $read_count );
1715
- $this->substeps_done = $this->substeps_done + $length;
1716
- $this->update_working_data();
1717
-
1718
- return $data;
1719
- }
1720
-
1721
-
1722
  /**
1723
  *
1724
  * Get the mime type of a file
@@ -1939,334 +2224,217 @@ final class BackWPup_Job {
1939
  return 'application/octet-stream';
1940
  }
1941
 
1942
-
1943
  /**
 
1944
  *
1945
- * Get back a array of files to backup in the selected folder
1946
- *
1947
- * @param string $folder the folder to get the files from
1948
- *
1949
- * @return array files to backup
1950
- */
1951
- public function get_files_in_folder( $folder ) {
1952
-
1953
- $files = array();
1954
- $folder = trailingslashit( $folder );
1955
-
1956
- if ( ! is_dir( $folder ) ) {
1957
- $this->log( sprintf( _x( 'Folder %s not exists', 'Folder name', 'backwpup' ), $folder ), E_USER_WARNING );
1958
-
1959
- return $files;
1960
- }
1961
-
1962
- if ( ! is_readable( $folder ) ) {
1963
- $this->log( sprintf( _x( 'Folder %s not readable', 'Folder name', 'backwpup' ), $folder ), E_USER_WARNING );
1964
-
1965
- return $files;
1966
- }
1967
-
1968
- if ( $dir = opendir( $folder ) ) {
1969
- while ( false !== ( $file = readdir( $dir ) ) ) {
1970
- if ( in_array( $file, array( '.', '..' ), true ) || is_dir( $folder . $file ) ) {
1971
- continue;
1972
- }
1973
- foreach ( $this->exclude_from_backup as $exclusion ) { //exclude files
1974
- $exclusion = trim( $exclusion );
1975
- if ( false !== stripos( $folder . $file, trim( $exclusion ) ) && ! empty( $exclusion ) ) {
1976
- continue 2;
1977
- }
1978
- }
1979
- if ( $this->job['backupexcludethumbs'] && strpos( $folder, BackWPup_File::get_upload_dir() ) !== false && preg_match( "/\-[0-9]{1,4}x[0-9]{1,4}.+\.(jpg|png|gif)$/i", $file ) ) {
1980
- continue;
1981
- }
1982
- if ( is_link( $folder . $file ) ) {
1983
- $this->log( sprintf( __( 'Link "%s" not following.', 'backwpup' ), $folder . $file ), E_USER_WARNING );
1984
- } elseif ( ! is_readable( $folder . $file ) ) {
1985
- $this->log( sprintf( __( 'File "%s" is not readable!', 'backwpup' ), $folder . $file ), E_USER_WARNING );
1986
- } else {
1987
- $file_size = filesize( $folder . $file );
1988
- if ( ! is_int( $file_size ) || $file_size < 0 || $file_size > 2147483647 ) {
1989
- $this->log( sprintf( __( 'File size of “%s” cannot be retrieved. File might be too large and will not be added to queue.', 'backwpup' ), $folder . $file . ' ' . $file_size ), E_USER_WARNING );
1990
- continue;
1991
- }
1992
- $files[] = $folder . $file;
1993
- }
1994
- }
1995
- closedir( $dir );
1996
- }
1997
-
1998
- return $files;
1999
- }
2000
-
2001
- /**
2002
- * create manifest file
2003
  * @return bool
2004
  */
2005
- public function create_manifest() {
2006
-
2007
- $this->substeps_todo = 3;
2008
-
2009
- $this->log( sprintf( __( '%d. Trying to generate a manifest file&#160;&hellip;', 'backwpup' ), $this->steps_data[ $this->step_working ]['STEP_TRY'] ) );
2010
-
2011
- //build manifest
2012
- $manifest = array();
2013
- // add blog information
2014
- $manifest['blog_info']['url'] = home_url();
2015
- $manifest['blog_info']['wpurl'] = site_url();
2016
- $manifest['blog_info']['prefix'] = $GLOBALS['wpdb']->prefix;
2017
- $manifest['blog_info']['description'] = get_option( 'blogdescription' );
2018
- $manifest['blog_info']['stylesheet_directory'] = get_template_directory_uri();
2019
- $manifest['blog_info']['activate_plugins'] = wp_get_active_and_valid_plugins();
2020
- $manifest['blog_info']['activate_theme'] = wp_get_theme()->get( 'Name' );
2021
- $manifest['blog_info']['admin_email'] = get_option( 'admin_email' );
2022
- $manifest['blog_info']['charset'] = get_bloginfo( 'charset' );
2023
- $manifest['blog_info']['version'] = BackWPup::get_plugin_data( 'wp_version' );
2024
- $manifest['blog_info']['backwpup_version'] = BackWPup::get_plugin_data( 'version' );
2025
- $manifest['blog_info']['language'] = get_bloginfo( 'language' );
2026
- $manifest['blog_info']['name'] = get_bloginfo( 'name' );
2027
- $manifest['blog_info']['abspath'] = ABSPATH;
2028
- $manifest['blog_info']['uploads'] = wp_upload_dir( null, false, true );
2029
- $manifest['blog_info']['contents']['basedir'] = WP_CONTENT_DIR;
2030
- $manifest['blog_info']['contents']['baseurl'] = WP_CONTENT_URL;
2031
- $manifest['blog_info']['plugins']['basedir'] = WP_PLUGIN_DIR;
2032
- $manifest['blog_info']['plugins']['baseurl'] = WP_PLUGIN_URL;
2033
- $manifest['blog_info']['themes']['basedir'] = get_theme_root();
2034
- $manifest['blog_info']['themes']['baseurl'] = get_theme_root_uri();
2035
- // add job settings
2036
- $manifest['job_settings'] = $this->job;
2037
- // add archive info
2038
- foreach ( $this->additional_files_to_backup as $file ) {
2039
- $manifest['archive']['extra_files'][] = basename( $file );
2040
- }
2041
- if ( isset( $this->steps_data['JOB_FILE'] ) ) {
2042
- if ( $this->job['backuproot'] ) {
2043
- $manifest['archive']['abspath'] = trailingslashit( $this->get_destination_path_replacement( ABSPATH ) );
2044
- }
2045
- if ( $this->job['backupuploads'] ) {
2046
- $manifest['archive']['uploads'] = trailingslashit( $this->get_destination_path_replacement( BackWPup_File::get_upload_dir() ) );
2047
- }
2048
- if ( $this->job['backupcontent'] ) {
2049
- $manifest['archive']['contents'] = trailingslashit( $this->get_destination_path_replacement( WP_CONTENT_DIR ) );
2050
- }
2051
- if ( $this->job['backupplugins'] ) {
2052
- $manifest['archive']['plugins'] = trailingslashit( $this->get_destination_path_replacement( WP_PLUGIN_DIR ) );
2053
- }
2054
- if ( $this->job['backupthemes'] ) {
2055
- $manifest['archive']['themes'] = trailingslashit( $this->get_destination_path_replacement( get_theme_root() ) );
2056
- }
2057
- }
2058
 
2059
- if ( ! file_put_contents( BackWPup::get_plugin_data( 'TEMP' ) . 'manifest.json', json_encode( $manifest ) ) ) {
 
2060
  return false;
2061
  }
2062
- $this->substeps_done = 1;
2063
 
2064
- //Create backwpup_readme.txt
2065
- $readme_text = __( 'You may have noticed the manifest.json file in this archive.', 'backwpup' ) . PHP_EOL;
2066
- $readme_text .= __( 'manifest.json might be needed for later restoring a backup from this archive.', 'backwpup' ) . PHP_EOL;
2067
- $readme_text .= __( 'Please leave manifest.json untouched and in place. Otherwise it is safe to be ignored.', 'backwpup' ) . PHP_EOL;
2068
- if ( ! file_put_contents( BackWPup::get_plugin_data( 'TEMP' ) . 'backwpup_readme.txt', $readme_text ) ) {
2069
  return false;
2070
  }
2071
- $this->substeps_done = 2;
2072
 
2073
- //add file to backup files
2074
- if ( is_readable( BackWPup::get_plugin_data( 'TEMP' ) . 'manifest.json' ) ) {
2075
- $this->additional_files_to_backup[] = BackWPup::get_plugin_data( 'TEMP' ) . 'manifest.json';
2076
- $this->additional_files_to_backup[] = BackWPup::get_plugin_data( 'TEMP' ) . 'backwpup_readme.txt';
2077
- $this->log( sprintf( __( 'Added manifest.json file with %1$s to backup file list.', 'backwpup' ), size_format( filesize( BackWPup::get_plugin_data( 'TEMP' ) . 'manifest.json' ), 2 ) ) );
2078
  }
2079
- $this->substeps_done = 3;
2080
 
2081
  return true;
 
2082
  }
2083
 
2084
  /**
2085
- * Creates the backup archive
2086
  */
2087
- private function create_archive() {
2088
-
2089
- //load folders to backup
2090
- $folders_to_backup = $this->get_folders_to_backup();
2091
 
2092
- $this->substeps_todo = $this->count_folder + 1;
 
 
2093
 
2094
- //initial settings for restarts in archiving
2095
- if ( ! isset( $this->steps_data[ $this->step_working ]['on_file'] ) ) {
2096
- $this->steps_data[ $this->step_working ]['on_file'] = '';
2097
- }
2098
- if ( ! isset( $this->steps_data[ $this->step_working ]['on_folder'] ) ) {
2099
- $this->steps_data[ $this->step_working ]['on_folder'] = '';
2100
- }
2101
 
2102
- if ( $this->steps_data[ $this->step_working ]['on_folder'] == '' && $this->steps_data[ $this->step_working ]['on_file'] == '' && is_file( $this->backup_folder . $this->backup_file ) ) {
2103
- unlink( $this->backup_folder . $this->backup_file );
2104
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2105
 
2106
- if ( $this->steps_data[ $this->step_working ]['SAVE_STEP_TRY'] != $this->steps_data[ $this->step_working ]['STEP_TRY'] ) {
2107
- $this->log( sprintf( __( '%d. Trying to create backup archive &hellip;', 'backwpup' ), $this->steps_data[ $this->step_working ]['STEP_TRY'] ), E_USER_NOTICE );
 
 
 
 
2108
  }
2109
 
2110
- try {
2111
- $backup_archive = new BackWPup_Create_Archive( $this->backup_folder . $this->backup_file );
2112
 
2113
- //show method for creation
2114
- if ( $this->substeps_done == 0 ) {
2115
- $this->log( sprintf( _x( 'Compressing files as %s. Please be patient, this may take a moment.', 'Archive compression method', 'backwpup' ), $backup_archive->get_method() ) );
2116
- }
 
 
 
 
 
2117
 
2118
- //add extra files
2119
- if ( $this->substeps_done == 0 ) {
2120
- if ( ! empty( $this->additional_files_to_backup ) && $this->substeps_done == 0 ) {
2121
- if ( $this->is_debug() ) {
2122
- $this->log( __( 'Adding Extra files to Archive', 'backwpup' ) );
2123
- }
2124
- foreach ( $this->additional_files_to_backup as $file ) {
2125
- if ( $backup_archive->add_file( $file, basename( $file ) ) ) {
2126
- ;
2127
- $this->count_files ++;
2128
- $this->count_files_size = $this->count_files_size + filesize( $file );
2129
- $this->update_working_data();
2130
- } else {
2131
- $backup_archive->close();
2132
- $this->steps_data[ $this->step_working ]['on_file'] = '';
2133
- $this->steps_data[ $this->step_working ]['on_folder'] = '';
2134
- $this->log( __( 'Cannot create backup archive correctly. Aborting creation.', 'backwpup' ), E_USER_ERROR );
2135
 
2136
- return false;
2137
- }
2138
- }
 
 
 
 
2139
  }
2140
- $this->substeps_done ++;
2141
  }
2142
-
2143
- //add normal files
2144
- while ( $folder = array_shift( $folders_to_backup ) ) {
2145
- //jump over already done folders
2146
- if ( in_array( $this->steps_data[ $this->step_working ]['on_folder'], $folders_to_backup, true ) ) {
2147
- continue;
2148
- }
2149
- if ( $this->is_debug() ) {
2150
- $this->log( sprintf( __( 'Archiving Folder: %s', 'backwpup' ), $folder ) );
2151
- }
2152
- $this->steps_data[ $this->step_working ]['on_folder'] = $folder;
2153
- $files_in_folder = $this->get_files_in_folder( $folder );
2154
- //add empty folders
2155
- if ( empty( $files_in_folder ) ) {
2156
- $folder_name_in_archive = trim( ltrim( $this->get_destination_path_replacement( $folder ), '/' ) );
2157
- if ( ! empty ( $folder_name_in_archive ) ) {
2158
- $backup_archive->add_empty_folder( $folder, $folder_name_in_archive );
2159
- }
2160
- continue;
2161
- }
2162
- //add files
2163
- while ( $file = array_shift( $files_in_folder ) ) {
2164
- //jump over already done files
2165
- if ( in_array( $this->steps_data[ $this->step_working ]['on_file'], $files_in_folder, true ) ) {
2166
- continue;
2167
- }
2168
- $this->steps_data[ $this->step_working ]['on_file'] = $file;
2169
- //restart if needed
2170
- $restart_time = $this->get_restart_time();
2171
- if ( $restart_time <= 0 ) {
2172
- unset( $backup_archive );
2173
- $this->do_restart_time( true );
2174
-
2175
- return false;
2176
- }
2177
- //generate filename in archive
2178
- $in_archive_filename = ltrim( $this->get_destination_path_replacement( $file ), '/' );
2179
- //add file to archive
2180
- if ( $backup_archive->add_file( $file, $in_archive_filename ) ) {
2181
- $this->count_files ++;
2182
- $this->count_files_size = $this->count_files_size + filesize( $file );
2183
- $this->update_working_data();
2184
- } else {
2185
- $backup_archive->close();
2186
- unset( $backup_archive );
2187
- $this->steps_data[ $this->step_working ]['on_file'] = '';
2188
- $this->steps_data[ $this->step_working ]['on_folder'] = '';
2189
- $this->substeps_done = 0;
2190
- $this->backup_filesize = filesize( $this->backup_folder . $this->backup_file );
2191
- if ( $this->backup_filesize === false ) {
2192
- $this->backup_filesize = PHP_INT_MAX;
2193
- }
2194
- $this->log( __( 'Cannot create backup archive correctly. Aborting creation.', 'backwpup' ), E_USER_ERROR );
2195
-
2196
- return false;
2197
- }
2198
- }
2199
- $this->steps_data[ $this->step_working ]['on_file'] = '';
2200
- $this->substeps_done ++;
2201
  }
2202
- $backup_archive->close();
2203
- unset( $backup_archive );
2204
- $this->log( __( 'Backup archive created.', 'backwpup' ), E_USER_NOTICE );
2205
- } catch ( Exception $e ) {
2206
- $this->log( $e->getMessage(), E_USER_ERROR, $e->getFile(), $e->getLine() );
2207
- unset( $backup_archive );
2208
-
2209
- return false;
2210
  }
2211
 
2212
- $this->backup_filesize = filesize( $this->backup_folder . $this->backup_file );
2213
- if ( $this->backup_filesize === false ) {
2214
- $this->backup_filesize = PHP_INT_MAX;
 
 
 
 
 
 
 
 
2215
  }
2216
 
2217
- if ( $this->backup_filesize >= PHP_INT_MAX ) {
2218
- $this->log( __( 'The Backup archive will be too large for file operations with this PHP Version. You might want to consider splitting the backup job in multiple jobs with less files each.', 'backwpup' ), E_USER_ERROR );
2219
- $this->end();
2220
- } else {
2221
- $this->log( sprintf( __( 'Archive size is %s.', 'backwpup' ), size_format( $this->backup_filesize, 2 ) ), E_USER_NOTICE );
2222
- }
2223
 
2224
- $this->log( sprintf( __( '%1$d Files with %2$s in Archive.', 'backwpup' ), $this->count_files, size_format( $this->count_files_size, 2 ) ), E_USER_NOTICE );
 
 
 
 
 
 
2225
 
2226
- return true;
2227
  }
2228
 
2229
  /**
2230
- * @param $name
2231
- * @param string $suffix
2232
- * @param bool $delete_temp_file
 
 
 
 
2233
  *
2234
  * @return string
 
2235
  */
2236
- public function generate_filename( $name, $suffix = '', $delete_temp_file = true ) {
2237
-
2238
- $local_time = current_time( 'timestamp' );
2239
-
2240
- $datevars = array( '%d', '%j', '%m', '%n', '%Y', '%y', '%a', '%A', '%B', '%g', '%G', '%h', '%H', '%i', '%s' );
2241
- $datevalues = array(
2242
- date( 'd', $local_time ),
2243
- date( 'j', $local_time ),
2244
- date( 'm', $local_time ),
2245
- date( 'n', $local_time ),
2246
- date( 'Y', $local_time ),
2247
- date( 'y', $local_time ),
2248
- date( 'a', $local_time ),
2249
- date( 'A', $local_time ),
2250
- date( 'B', $local_time ),
2251
- date( 'g', $local_time ),
2252
- date( 'G', $local_time ),
2253
- date( 'h', $local_time ),
2254
- date( 'H', $local_time ),
2255
- date( 'i', $local_time ),
2256
- date( 's', $local_time )
2257
- );
2258
 
2259
- if ( $suffix ) {
2260
- $suffix = '.' . trim( $suffix, '. ' );
 
2261
  }
2262
 
2263
- $name = str_replace( $datevars, $datevalues, self::sanitize_file_name( $name ) );
2264
- $name .= $suffix;
2265
- if ( $delete_temp_file && is_writeable( BackWPup::get_plugin_data( 'TEMP' ) . $name ) && ! is_dir( BackWPup::get_plugin_data( 'TEMP' ) . $name ) && ! is_link( BackWPup::get_plugin_data( 'TEMP' ) . $name ) ) {
2266
- unlink( BackWPup::get_plugin_data( 'TEMP' ) . $name );
2267
  }
2268
 
2269
- return $name;
 
 
 
 
2270
  }
2271
 
2272
  /**
@@ -2325,73 +2493,6 @@ final class BackWPup_Job {
2325
  return false;
2326
  }
2327
 
2328
- /**
2329
- * Sanitizes a filename, replacing whitespace with underscores.
2330
- *
2331
- * @param $filename
2332
- *
2333
- * @return mixed
2334
- */
2335
- public static function sanitize_file_name( $filename ) {
2336
-
2337
- $filename = trim( $filename );
2338
-
2339
- $special_chars = array(
2340
- "?",
2341
- "[",
2342
- "]",
2343
- "/",
2344
- "\\",
2345
- "=",
2346
- "<",
2347
- ">",
2348
- ":",
2349
- ";",
2350
- ",",
2351
- "'",
2352
- "\"",
2353
- "&",
2354
- "$",
2355
- "#",
2356
- "*",
2357
- "(",
2358
- ")",
2359
- "|",
2360
- "~",
2361
- "`",
2362
- "!",
2363
- "{",
2364
- "}",
2365
- chr( 0 )
2366
- );
2367
-
2368
- $filename = str_replace( $special_chars, '', $filename );
2369
-
2370
- $filename = str_replace( array( ' ', '%20', '+' ), '_', $filename );
2371
- $filename = str_replace( array( "\n", "\t", "\r" ), '-', $filename );
2372
- $filename = trim( $filename, '.-_' );
2373
-
2374
- return $filename;
2375
- }
2376
-
2377
- /**
2378
- * Get the Process id of working script
2379
- *
2380
- * @return int
2381
- */
2382
- private static function get_pid() {
2383
-
2384
- if ( function_exists( 'posix_getpid' ) ) {
2385
-
2386
- return posix_getpid();
2387
- } elseif ( function_exists( 'getmypid' ) ) {
2388
-
2389
- return getmypid();
2390
- }
2391
-
2392
- return - 1;
2393
- }
2394
-
2395
  /**
2396
  * For storing and getting data in/from a extra temp file
2397
  *
@@ -2420,37 +2521,6 @@ final class BackWPup_Job {
2420
  return $data;
2421
  }
2422
 
2423
- /**
2424
- * Get list of Folder for backup
2425
- *
2426
- * @return array folder list
2427
- */
2428
- public function get_folders_to_backup() {
2429
-
2430
- $file = BackWPup::get_plugin_data( 'temp' ) . 'backwpup-' . BackWPup::get_plugin_data( 'hash' ) . '-folder.php';
2431
-
2432
- if ( ! file_exists( $file ) ) {
2433
- return array();
2434
- }
2435
-
2436
- $folders = array();
2437
-
2438
- $file_data = file( $file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
2439
-
2440
- foreach ( $file_data as $folder ) {
2441
- $folder = trim( str_replace( array( '<?php', '//' ), '', $folder ) );
2442
- if ( ! empty( $folder ) && is_dir( $folder ) ) {
2443
- $folders[] = $folder;
2444
- }
2445
- }
2446
- $folders = array_unique( $folders );
2447
- sort( $folders );
2448
- $this->count_folder = count( $folders );
2449
-
2450
- return $folders;
2451
- }
2452
-
2453
-
2454
  /**
2455
  * Add a Folders to Folder list that should be backup
2456
  *
@@ -2479,88 +2549,4 @@ final class BackWPup_Job {
2479
  }
2480
  }
2481
 
2482
- /**
2483
- * Check whether exec has been disabled.
2484
- *
2485
- * @access public
2486
- * @static
2487
- * @return bool
2488
- */
2489
- public static function is_exec() {
2490
-
2491
- // Is function avail
2492
- if ( ! function_exists( 'exec' ) ) {
2493
- return false;
2494
- }
2495
-
2496
- // Is shell_exec disabled?
2497
- if ( in_array( 'exec', array_map( 'trim', explode( ',', @ini_get( 'disable_functions' ) ) ), true ) ) {
2498
- return false;
2499
- }
2500
-
2501
- // Can we issue a simple echo command?
2502
- $output = exec( 'echo backwpupechotest' );
2503
- if ( $output != 'backwpupechotest' ) {
2504
- return false;
2505
- }
2506
-
2507
- return true;
2508
-
2509
- }
2510
-
2511
- /**
2512
- * Cleanup Temp Folder
2513
- */
2514
- public static function clean_temp_folder() {
2515
-
2516
- $temp_dir = BackWPup::get_plugin_data( 'TEMP' );
2517
- $do_not_delete_files = array( '.htaccess', 'nginx.conf', 'index.php', '.', '..', '.donotbackup' );
2518
-
2519
- if ( is_writable( $temp_dir ) && $dir = opendir( $temp_dir ) ) {
2520
- while ( false !== ( $file = readdir( $dir ) ) ) {
2521
- if ( in_array( $file, $do_not_delete_files, true ) || is_dir( $temp_dir . $file ) || is_link( $temp_dir . $file ) ) {
2522
- continue;
2523
- }
2524
- if ( is_writeable( $temp_dir . $file ) ) {
2525
- unlink( $temp_dir . $file );
2526
- }
2527
- }
2528
- closedir( $dir );
2529
- }
2530
- }
2531
-
2532
- /**
2533
- * Is debug log active
2534
- *
2535
- * @return bool
2536
- */
2537
- public function is_debug() {
2538
-
2539
- return strstr( $this->log_level, 'debug' ) ? true : false;
2540
- }
2541
-
2542
- /**
2543
- * Change path of a given path
2544
- * for better storing in archives or on sync destinations
2545
- *
2546
- * @param $path string path to change to wp default path
2547
- *
2548
- * @return string
2549
- */
2550
- public function get_destination_path_replacement( $path ) {
2551
-
2552
- $path = str_replace( '\\', '/', $path );
2553
-
2554
- $abs_path = realpath( ABSPATH );
2555
- if ( $this->job['backupabsfolderup'] ) {
2556
- $abs_path = dirname( $abs_path );
2557
- }
2558
-
2559
- $abs_path = trailingslashit( str_replace( '\\', '/', $abs_path ) );
2560
-
2561
- $path = str_replace( $abs_path, '/', $path );
2562
-
2563
- return $path;
2564
- }
2565
-
2566
  }
43
  * @var float Timestamp of last update off .running file
44
  */
45
  public $timestamp_last_update = 0;
 
 
 
 
46
  /**
47
  * @var int Number of warnings
48
  */
111
  * @var int count of affected folders
112
  */
113
  public $count_folder = 0;
 
114
  /**
115
  * If job aborted from user
116
  * @var bool
117
  */
118
  public $user_abort = false;
 
 
 
 
 
 
 
119
  /**
120
  * A uniqid ID uniqid('', true); to identify process
121
  * @var string
122
  */
123
  public $uniqid = '';
124
+ /**
125
+ * @var float Timestamp of script start
126
+ */
127
+ private $timestamp_script_start = 0;
128
+ /**
129
+ * Stores data that will only used in a single run
130
+ * @var array
131
+ */
132
+ private $run = array();
133
  /**
134
  * @var string logging level (normal|normal_untranslated|debug|debug_untranslated)
135
  */
140
  */
141
  private $signal = 0;
142
 
143
+ /**
144
+ *
145
+ */
146
+ public static function start_http( $starttype, $jobid = 0 ) {
147
+
148
+ //load text domain
149
+ $log_level = get_site_option( 'backwpup_cfg_loglevel', 'normal_translated' );
150
+ if ( strstr( $log_level, 'translated' ) ) {
151
+ BackWPup::load_text_domain();
152
+ } else {
153
+ add_filter( 'override_load_textdomain', '__return_true' );
154
+ $GLOBALS['l10n'] = array();
155
+ }
156
+
157
+ if ( $starttype !== 'restart' ) {
158
+
159
+ //check job id exists
160
+ if ( $jobid !== BackWPup_Option::get( $jobid, 'jobid' ) ) {
161
+ return false;
162
+ }
163
+
164
+ //check folders
165
+ $log_folder = get_site_option( 'backwpup_cfg_logfolder' );
166
+ $folder_message_log = BackWPup_File::check_folder( BackWPup_File::get_absolute_path( $log_folder ) );
167
+ $folder_message_temp = BackWPup_File::check_folder( BackWPup::get_plugin_data( 'TEMP' ), true );
168
+ if ( ! empty( $folder_message_log ) || ! empty( $folder_message_temp ) ) {
169
+ BackWPup_Admin::message( $folder_message_log, true );
170
+ BackWPup_Admin::message( $folder_message_temp, true );
171
+ return false;
172
+ }
173
+ }
174
+
175
+ // redirect
176
+ if ( $starttype === 'runnowalt' ) {
177
+ ob_start();
178
+ wp_redirect( add_query_arg( array( 'page' => 'backwpupjobs' ), network_admin_url( 'admin.php' ) ) );
179
+ echo ' ';
180
+ flush();
181
+ if ( $level = ob_get_level() ) {
182
+ for ( $i = 0; $i < $level; $i ++ ) {
183
+ ob_end_clean();
184
+ }
185
+ }
186
+ }
187
+
188
+ // Should be preventing doubled running job's on http requests
189
+ $random = mt_rand( 10, 90 ) * 10000;
190
+ usleep( $random );
191
+
192
+ //check running job
193
+ $backwpup_job_object = self::get_working_data();
194
+ //start class
195
+ if ( ! $backwpup_job_object && in_array( $starttype, array( 'runnow', 'runnowalt', 'runext', 'cronrun' ), true ) && $jobid ) {
196
+ //schedule restart event
197
+ wp_schedule_single_event( time() + 60, 'backwpup_cron', array( 'id' => 'restart' ) );
198
+ //start job
199
+ $backwpup_job_object = new self();
200
+ $backwpup_job_object->create( $starttype, $jobid );
201
+ }
202
+ if ( $backwpup_job_object ) {
203
+ $backwpup_job_object->run();
204
+ }
205
+ }
206
 
207
  /**
208
+ *
209
+ * Get data off a working job
210
+ *
211
+ * @return bool|object BackWPup_Job Object or Bool if file not exits
212
  */
213
+ public static function get_working_data() {
214
+
215
+ if ( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
216
+ clearstatcache( true, BackWPup::get_plugin_data( 'running_file' ) );
217
+ } else {
218
+ clearstatcache();
219
+ }
220
+
221
+ if ( ! file_exists( BackWPup::get_plugin_data( 'running_file' ) ) ) {
222
+ return false;
223
+ }
224
+
225
+ $file_data = file_get_contents( BackWPup::get_plugin_data( 'running_file' ), false, null, 8 );
226
+ if ( empty( $file_data ) ) {
227
+ return false;
228
+ }
229
+
230
+ if ( $job_object = unserialize( $file_data ) ) {
231
+ if ( $job_object instanceof BackWPup_Job ) {
232
+ return $job_object;
233
+ }
234
+ }
235
+
236
+ return false;
237
 
 
 
238
  }
239
 
240
  /**
528
  $this->steps_done[] = 'CREATE';
529
  }
530
 
 
531
  /**
532
+ * @param $name
533
+ * @param string $suffix
534
+ * @param bool $delete_temp_file
535
  *
536
+ * @return string
 
 
 
 
 
537
  */
538
+ public function generate_filename( $name, $suffix = '', $delete_temp_file = true ) {
539
 
540
+ $local_time = current_time( 'timestamp' );
541
+
542
+ $datevars = array( '%d', '%j', '%m', '%n', '%Y', '%y', '%a', '%A', '%B', '%g', '%G', '%h', '%H', '%i', '%s' );
543
+ $datevalues = array(
544
+ date( 'd', $local_time ),
545
+ date( 'j', $local_time ),
546
+ date( 'm', $local_time ),
547
+ date( 'n', $local_time ),
548
+ date( 'Y', $local_time ),
549
+ date( 'y', $local_time ),
550
+ date( 'a', $local_time ),
551
+ date( 'A', $local_time ),
552
+ date( 'B', $local_time ),
553
+ date( 'g', $local_time ),
554
+ date( 'G', $local_time ),
555
+ date( 'h', $local_time ),
556
+ date( 'H', $local_time ),
557
+ date( 'i', $local_time ),
558
+ date( 's', $local_time )
559
  );
560
 
561
+ if ( $suffix ) {
562
+ $suffix = '.' . trim( $suffix, '. ' );
563
  }
564
 
565
+ $name = str_replace( $datevars, $datevalues, self::sanitize_file_name( $name ) );
566
+ $name .= $suffix;
567
+ if ( $delete_temp_file && is_writeable( BackWPup::get_plugin_data( 'TEMP' ) . $name ) && ! is_dir( BackWPup::get_plugin_data( 'TEMP' ) . $name ) && ! is_link( BackWPup::get_plugin_data( 'TEMP' ) . $name ) ) {
568
+ unlink( BackWPup::get_plugin_data( 'TEMP' ) . $name );
569
  }
570
 
571
+ return $name;
572
+ }
 
 
573
 
574
+ /**
575
+ * Sanitizes a filename, replacing whitespace with underscores.
576
+ *
577
+ * @param $filename
578
+ *
579
+ * @return mixed
580
+ */
581
+ public static function sanitize_file_name( $filename ) {
582
 
583
+ $filename = trim( $filename );
 
 
 
 
 
 
 
584
 
585
+ $special_chars = array(
586
+ "?",
587
+ "[",
588
+ "]",
589
+ "/",
590
+ "\\",
591
+ "=",
592
+ "<",
593
+ ">",
594
+ ":",
595
+ ";",
596
+ ",",
597
+ "'",
598
+ "\"",
599
+ "&",
600
+ "$",
601
+ "#",
602
+ "*",
603
+ "(",
604
+ ")",
605
+ "|",
606
+ "~",
607
+ "`",
608
+ "!",
609
+ "{",
610
+ "}",
611
+ chr( 0 )
612
+ );
613
 
614
+ $filename = str_replace( $special_chars, '', $filename );
 
 
 
 
615
 
616
+ $filename = str_replace( array( ' ', '%20', '+' ), '_', $filename );
617
+ $filename = str_replace( array( "\n", "\t", "\r" ), '-', $filename );
618
+ $filename = trim( $filename, '.-_' );
 
619
 
620
+ return $filename;
621
+ }
 
622
 
623
+ private function write_running_file() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
624
 
625
+ $clone = clone $this;
626
+ $data = '<?php //' . serialize( $clone );
 
 
 
 
 
 
 
 
 
627
 
628
+ $write = file_put_contents( BackWPup::get_plugin_data( 'running_file' ), $data );
629
+ if ( ! $write || $write < strlen( $data ) ) {
630
+ unlink( BackWPup::get_plugin_data( 'running_file' ) );
631
+ $this->log( __( 'Cannot write progress to working file. Job will be aborted.', 'backwpup' ), E_USER_ERROR );
632
  }
633
+ }
634
 
635
+ /**
636
+ * Write messages to log file
637
+ *
638
+ * @param string $message the error message
639
+ * @param int $type the error number (E_USER_ERROR,E_USER_WARNING,E_USER_NOTICE, ...)
640
+ * @param string $file the full path of file with error (__FILE__)
641
+ * @param int $line the line in that is the error (__LINE__)
642
+ *
643
+ * @return bool true
644
+ */
645
+ public function log( $message, $type = E_USER_NOTICE, $file = '', $line = 0 ) {
646
 
647
+ // if error has been suppressed with an @
648
+ if ( error_reporting() == 0 ) {
649
+ return true;
650
  }
651
 
652
+ //if first the type an second the message switch it on user errors
653
+ if ( ! is_int( $type ) && is_int( $message ) && in_array( $message, array(
654
+ 1,
655
+ 2,
656
+ 4,
657
+ 8,
658
+ 16,
659
+ 32,
660
+ 64,
661
+ 128,
662
+ 256,
663
+ 512,
664
+ 1024,
665
+ 2048,
666
+ 4096,
667
+ 8192,
668
+ 16384
669
+ ), true )
670
+ ) {
671
+ $temp = $message;
672
+ $message = $type;
673
+ $type = $temp;
674
  }
675
 
676
+ //json message if array or object
677
+ if ( is_array( $message ) || is_object( $message ) ) {
678
+ $message = json_encode( $message );
679
+ }
680
 
681
+ //if not set line and file get it
682
+ if ( $this->is_debug() ) {
683
+ if ( empty( $file ) || empty( $line ) ) {
684
+ $debug_info = debug_backtrace();
685
+ $file = $debug_info[0]['file'];
686
+ $line = $debug_info[0]['line'];
687
+ }
688
+ }
689
 
690
+ $error = false;
691
+ $warning = false;
 
 
692
 
693
+ switch ( $type ) {
694
+ case E_NOTICE:
695
+ case E_USER_NOTICE:
696
+ break;
697
+ case E_WARNING:
698
+ case E_CORE_WARNING:
699
+ case E_COMPILE_WARNING:
700
+ case E_USER_WARNING:
701
+ $this->warnings ++;
702
+ $warning = true;
703
+ $message = __( 'WARNING:', 'backwpup' ) . ' ' . $message;
704
+ break;
705
+ case E_ERROR:
706
+ case E_PARSE:
707
+ case E_CORE_ERROR:
708
+ case E_COMPILE_ERROR:
709
+ case E_USER_ERROR:
710
+ $this->errors ++;
711
+ $error = true;
712
+ $message = __( 'ERROR:', 'backwpup' ) . ' ' . $message;
713
+ break;
714
+ case 8192: //E_DEPRECATED comes with php 5.3
715
+ case 16384: //E_USER_DEPRECATED comes with php 5.3
716
+ $message = __( 'DEPRECATED:', 'backwpup' ) . ' ' . $message;
717
+ break;
718
+ case E_STRICT:
719
+ $message = __( 'STRICT NOTICE:', 'backwpup' ) . ' ' . $message;
720
+ break;
721
+ case E_RECOVERABLE_ERROR:
722
+ $this->errors ++;
723
+ $error = true;
724
+ $message = __( 'RECOVERABLE ERROR:', 'backwpup' ) . ' ' . $message;
725
+ break;
726
+ default:
727
+ $message = $type . ': ' . $message;
728
+ break;
729
  }
730
 
731
+ $in_file = $this->get_destination_path_replacement( $file );
732
 
733
+ //print message to cli
734
+ if ( defined( 'WP_CLI' ) && WP_CLI ) {
735
+ $output_message = str_replace( array( '&hellip;', '&#160;' ), array( '...', ' ' ), esc_html( $message ) );
736
+ if ( ! call_user_func( array( '\cli\Shell', 'isPiped' ) ) ) {
737
+ if ( $error ) {
738
+ $output_message = '%r' . $output_message . '%n';
739
+ }
740
+ if ( $warning ) {
741
+ $output_message = '%y' . $output_message . '%n';
742
+ }
743
+ $output_message = call_user_func( array( '\cli\Colors', 'colorize' ), $output_message, true );
744
  }
745
+ WP_CLI::line( $output_message );
746
+ } elseif ( php_sapi_name() == 'cli' && defined( 'STDOUT' ) ) {
747
+ $output_message = str_replace( array( '&hellip;', '&#160;' ), array( '...', ' ' ), esc_html( $message ) ) . PHP_EOL;
748
+ fwrite( STDOUT, $output_message );
749
+ }
750
 
751
+ //timestamp for log file
752
+ $debug_info = '';
753
+ if ( $this->is_debug() ) {
754
+ $debug_info = ' title="[Type: ' . $type . '|Line: ' . $line . '|File: ' . $in_file . '|Mem: ' . size_format( @memory_get_usage( true ), 2 ) . '|Mem Max: ' . size_format( @memory_get_peak_usage( true ), 2 ) . '|Mem Limit: ' . ini_get( 'memory_limit' ) . '|PID: ' . self::get_pid() . ' | UniqID: ' . $this->uniqid . '|Query\'s: ' . get_num_queries() . ']"';
 
 
 
 
 
755
  }
756
+ $timestamp = '<span datetime="' . date( 'c' ) . '" ' . $debug_info . '>[' . date( 'd-M-Y H:i:s', current_time( 'timestamp' ) ) . ']</span> ';
757
 
758
+ //set last Message
759
+ if ( $error ) {
760
+ $output_message = '<span style="background-color:#ff6766;color:black;padding:0 2px;">' . esc_html( $message ) . '</span>';
761
+ $this->lasterrormsg = $output_message;
762
+ }
763
+ elseif ( $warning ) {
764
+ $output_message = '<span style="background-color:#ffc766;color:black;padding:0 2px;">' . esc_html( $message ) . '</span>';
765
+ $this->lasterrormsg = $output_message;
766
+ }
767
+ else {
768
+ $output_message = esc_html( $message );
769
+ $this->lastmsg = $output_message;
770
+ }
771
+ //write log file
772
+ if ( $this->logfile ) {
773
+ if ( ! file_put_contents( $this->logfile, $timestamp . $output_message . '<br />' . PHP_EOL, FILE_APPEND ) ) {
774
+ $this->logfile = '';
775
+ restore_error_handler();
776
+ trigger_error( esc_html( $message ), $type );
777
+ }
778
+
779
+ //write new log header
780
+ if ( ( $error || $warning ) && $this->logfile ) {
781
+ if ( $fd = fopen( $this->logfile, 'r+' ) ) {
782
+ $file_pos = ftell( $fd );
783
+ while ( ! feof( $fd ) ) {
784
+ $line = fgets( $fd );
785
+ if ( $error && stripos( $line, '<meta name="backwpup_errors" content="' ) !== false ) {
786
+ fseek( $fd, $file_pos );
787
+ fwrite( $fd, str_pad( '<meta name="backwpup_errors" content="' . $this->errors . '" />', 100 ) . PHP_EOL );
788
+ break;
789
+ }
790
+ if ( $warning && stripos( $line, '<meta name="backwpup_warnings" content="' ) !== false ) {
791
+ fseek( $fd, $file_pos );
792
+ fwrite( $fd, str_pad( '<meta name="backwpup_warnings" content="' . $this->warnings . '" />', 100 ) . PHP_EOL );
793
+ break;
794
+ }
795
+ $file_pos = ftell( $fd );
796
+ }
797
+ fclose( $fd );
798
  }
799
  }
800
  }
801
 
802
+ //write working data
803
+ $this->update_working_data( $error || $warning );
 
804
 
805
+ //true for no more php error handling.
806
+ return true;
 
 
 
 
 
 
 
 
 
 
 
807
  }
808
 
809
  /**
810
+ * Is debug log active
811
+ *
812
+ * @return bool
813
  */
814
+ public function is_debug() {
815
 
816
+ return strstr( $this->log_level, 'debug' ) ? true : false;
817
+ }
 
818
 
819
+ /**
820
+ * Change path of a given path
821
+ * for better storing in archives or on sync destinations
822
+ *
823
+ * @param $path string path to change to wp default path
824
+ *
825
+ * @return string
826
+ */
827
+ public function get_destination_path_replacement( $path ) {
828
 
829
+ $path = str_replace( '\\', '/', $path );
 
 
 
 
 
 
 
830
 
831
+ $abs_path = realpath( ABSPATH );
832
+ if ( $this->job['backupabsfolderup'] ) {
833
+ $abs_path = dirname( $abs_path );
834
+ }
835
 
836
+ $abs_path = trailingslashit( str_replace( '\\', '/', $abs_path ) );
 
 
837
 
838
+ $path = str_replace( $abs_path, '/', $path );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
839
 
840
+ return $path;
 
 
 
841
  }
842
 
 
843
  /**
844
+ * Get the Process id of working script
845
+ *
846
+ * @return int
847
  */
848
+ private static function get_pid() {
849
 
850
+ if ( function_exists( 'posix_getpid' ) ) {
 
 
851
 
852
+ return posix_getpid();
853
+ } elseif ( function_exists( 'getmypid' ) ) {
854
+
855
+ return getmypid();
 
 
856
  }
 
857
 
858
+ return - 1;
859
+ }
860
 
861
  /**
862
+ *
863
+ * Write the Working data to display the process or that i can executes again
864
+ * The write will only done every second
865
+ *
866
+ * @param bool $must
867
  */
868
+ public function update_working_data( $must = false ) {
869
  global $wpdb;
 
870
 
871
+ //to reduce server load
872
+ if ( get_site_option( 'backwpup_cfg_jobwaittimems' ) > 0 && get_site_option( 'backwpup_cfg_jobwaittimems' ) <= 500000 ) {
873
+ usleep( get_site_option( 'backwpup_cfg_jobwaittimems' ) );
 
 
874
  }
875
 
876
+ //check free memory
877
+ $this->need_free_memory( '10M' );
 
 
 
 
878
 
879
+ //only run every 1 sec.
880
+ $time_to_update = microtime( true ) - $this->timestamp_last_update;
881
+ if ( $time_to_update < 1 && ! $must ) {
882
  return;
883
  }
884
 
885
+ //FCGI must have a permanent output so that it not broke
886
+ if ( get_site_option( 'backwpup_cfg_jobdooutput' ) && ! defined( 'STDOUT' ) ) {
887
+ echo str_repeat( ' ', 12 );
888
+ flush();
 
 
889
  }
890
+
891
+ // check WPDB connection. WP will do it after a query that will cause messages.
892
+ $wpdb->check_connection( false );
893
+
894
+ //set execution time again for 5 min
895
+ @set_time_limit( 300 );
896
+
897
+ //calc sub step percent
898
+ if ( $this->substeps_todo > 0 && $this->substeps_done > 0 ) {
899
+ $this->substep_percent = round( $this->substeps_done / $this->substeps_todo * 100 );
900
+ } else {
901
+ $this->substep_percent = 1;
902
  }
903
+
904
+ //check if job aborted
905
+ if ( ! file_exists( BackWPup::get_plugin_data( 'running_file' ) ) ) {
906
+ if ( $this->step_working !== 'END' ) {
907
+ $this->end();
 
 
 
 
 
 
 
908
  }
909
+ } else {
910
+ $this->timestamp_last_update = microtime( true ); //last update of working file
911
+ $this->write_running_file();
912
  }
913
+
914
+ if ( $this->signal !== 0 ) {
915
+ $this->do_restart();
 
 
 
 
 
 
 
 
 
 
 
 
916
  }
917
+ }
 
 
918
 
919
+ /**
920
+ *
921
+ * Increase automatically the memory that is needed
922
+ *
923
+ * @param int|string $memneed of the needed memory
924
+ */
925
+ public function need_free_memory( $memneed ) {
926
+
927
+ //need memory
928
+ $needmemory = @memory_get_usage( true ) + self::convert_hr_to_bytes( $memneed );
929
+ // increase Memory
930
+ if ( $needmemory > self::convert_hr_to_bytes( ini_get( 'memory_limit' ) ) ) {
931
+ $newmemory = round( $needmemory / 1024 / 1024 ) + 1 . 'M';
932
+ if ( $needmemory >= 1073741824 ) {
933
+ $newmemory = round( $needmemory / 1024 / 1024 / 1024 ) . 'G';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
934
  }
935
+ @ini_set( 'memory_limit', $newmemory );
936
  }
937
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
938
 
939
+ /**
940
+ *
941
+ * Converts hr to bytes
942
+ *
943
+ * @param $size
944
+ *
945
+ * @return int
946
+ */
947
+ public static function convert_hr_to_bytes( $size ) {
948
+ $size = strtolower( $size );
949
+ $bytes = (int) $size;
950
+ if ( strpos( $size, 'k' ) !== false ) {
951
+ $bytes = intval( $size ) * 1024;
952
+ } elseif ( strpos( $size, 'm' ) !== false ) {
953
+ $bytes = intval( $size ) * 1024 * 1024;
954
+ } elseif ( strpos( $size, 'g' ) !== false ) {
955
+ $bytes = intval( $size ) * 1024 * 1024 * 1024;
956
+ }
957
 
958
+ return $bytes;
959
+ }
960
+
961
+ /**
962
+ *
963
+ * Called on job stop makes cleanup and terminates the script
964
+ *
965
+ */
966
+ private function end() {
967
+
968
+ $this->step_working = 'END';
969
+ $this->substeps_todo = 1;
970
+
971
+ if ( ! file_exists( BackWPup::get_plugin_data( 'running_file' ) ) ) {
972
+ $this->log( __( 'Aborted by user!', 'backwpup' ), E_USER_ERROR );
973
+ }
974
+
975
+ //delete old logs
976
+ if ( get_site_option( 'backwpup_cfg_maxlogs' ) ) {
977
+ $log_file_list = array();
978
+ $log_folder = trailingslashit( dirname( $this->logfile ) );
979
+ if ( is_readable( $log_folder ) && $dir = opendir( $log_folder ) ) { //make file list
980
+ while ( ( $file = readdir( $dir ) ) !== false ) {
981
+ if ( strpos( $file, 'backwpup_log_' ) == 0 && false !== strpos( $file, '.html' ) ) {
982
+ $log_file_list[ filemtime( $log_folder . $file ) ] = $file;
983
+ }
984
+ }
985
+ closedir( $dir );
986
+ }
987
+ if ( sizeof( $log_file_list ) > 0 ) {
988
+ krsort( $log_file_list, SORT_NUMERIC );
989
+ $num_delete_files = 0;
990
+ $i = - 1;
991
+ foreach ( $log_file_list AS $log_file ) {
992
+ $i ++;
993
+ if ( $i < get_site_option( 'backwpup_cfg_maxlogs' ) ) {
994
+ continue;
995
+ }
996
+ unlink( $log_folder . $log_file );
997
+ $num_delete_files ++;
998
  }
999
+ if ( $num_delete_files > 0 ) {
1000
+ $this->log( sprintf( _n( 'One old log deleted', '%d old logs deleted', $num_delete_files, 'backwpup' ), $num_delete_files ) );
1001
+ }
1002
+ }
1003
+ }
1004
 
1005
+ //Display job working time
1006
+ if ( $this->errors > 0 ) {
1007
+ $this->log( sprintf( __( 'Job has ended with errors in %s seconds. You must resolve the errors for correct execution.', 'backwpup' ), current_time( 'timestamp' ) - $this->start_time ), E_USER_ERROR );
1008
+ } elseif ( $this->warnings > 0 ) {
1009
+ $this->log( sprintf( __( 'Job finished with warnings in %s seconds. Please resolve them for correct execution.', 'backwpup' ), current_time( 'timestamp' ) - $this->start_time ), E_USER_WARNING );
1010
+ } else {
1011
+ $this->log( sprintf( __( 'Job done in %s seconds.', 'backwpup' ), current_time( 'timestamp' ) - $this->start_time ) );
1012
+ }
1013
+
1014
+ //Update job options
1015
+ $this->job['lastruntime'] = current_time( 'timestamp' ) - $this->start_time;
1016
+ BackWPup_Option::update( $this->job['jobid'], 'lastruntime', $this->job['lastruntime'] );
1017
+
1018
+
1019
+ //write header info
1020
+ if ( ! empty( $this->logfile ) ) {
1021
+
1022
+ if ( $fd = fopen( $this->logfile, 'r+' ) ) {
1023
+ $filepos = ftell( $fd );
1024
+ $found = 0;
1025
+ while ( ! feof( $fd ) ) {
1026
+ $line = fgets( $fd );
1027
+ if ( stripos( $line, '<meta name="backwpup_jobruntime"' ) !== false ) {
1028
+ fseek( $fd, $filepos );
1029
+ fwrite( $fd, str_pad( '<meta name="backwpup_jobruntime" content="' . $this->job['lastruntime'] . '" />', 100 ) . PHP_EOL );
1030
+ $found ++;
1031
+ }
1032
+ if ( stripos( $line, '<meta name="backwpup_backupfilesize"' ) !== false ) {
1033
+ fseek( $fd, $filepos );
1034
+ fwrite( $fd, str_pad( '<meta name="backwpup_backupfilesize" content="' . $this->backup_filesize . '" />', 100 ) . PHP_EOL );
1035
+ $found ++;
1036
+ }
1037
+ if ( $found >= 2 ) {
1038
+ break;
1039
+ }
1040
+ $filepos = ftell( $fd );
1041
  }
1042
+ fclose( $fd );
1043
+ }
1044
+
1045
+ //Send mail with log
1046
+ $sendmail = false;
1047
+ if ( $this->job['mailaddresslog'] ) {
1048
+ $sendmail = true;
1049
+ }
1050
+ if ( $this->errors === 0 && $this->job['mailerroronly'] ) {
1051
+ $sendmail = false;
1052
+ }
1053
+ if ( $sendmail ) {
1054
+ //special subject
1055
+ $status = __( 'SUCCESSFUL', 'backwpup' );
1056
+ if ( $this->warnings > 0 ) {
1057
+ $status = __( 'WARNING', 'backwpup' );
1058
  }
1059
+ if ( $this->errors > 0 ) {
1060
+ $status = __( 'ERROR', 'backwpup' );
1061
+ }
1062
+
1063
+ $subject = sprintf( __( '[%3$s] BackWPup log %1$s: %2$s', 'backwpup' ), date_i18n( 'd-M-Y H:i', $this->start_time, true ), esc_attr( $this->job['name'] ), $status );
1064
+ $headers = array();
1065
+ $headers[] = 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' );
1066
+ if ( $this->job['mailaddresssenderlog'] ) {
1067
+ $this->job['mailaddresssenderlog'] = str_replace( array( '&lt;', '&gt;' ), array( '<', '>' ), $this->job['mailaddresssenderlog'] );
1068
+
1069
+ $bracket_pos = strpos( $this->job['mailaddresssenderlog'], '<' );
1070
+ $at_pos = strpos( $this->job['mailaddresssenderlog'], '@' );
1071
+ if ( $bracket_pos === false || $at_pos === false ) {
1072
+ $this->job['mailaddresssenderlog'] = str_replace( array( '<', '>' ), '', $this->job['mailaddresssenderlog'] ) . ' <' . get_bloginfo( 'admin_email' ) . '>';
1073
+ }
1074
+
1075
+ $headers[] = 'From: ' . $this->job['mailaddresssenderlog'];
1076
+ }
1077
+ wp_mail( $this->job['mailaddresslog'], $subject, file_get_contents( $this->logfile ), $headers );
1078
+ }
1079
+ }
1080
+
1081
+ //set done
1082
+ $this->substeps_done = 1;
1083
+ $this->steps_done[] = 'END';
1084
+
1085
+ //clean up temp
1086
+ self::clean_temp_folder();
1087
+
1088
+ //remove shutdown action
1089
+ remove_action( 'shutdown', array( $this, 'shutdown' ) );
1090
+ restore_exception_handler();
1091
+ restore_error_handler();
1092
+
1093
+ //logfile end
1094
+ file_put_contents( $this->logfile, "</body>" . PHP_EOL . "</html>", FILE_APPEND );
1095
+
1096
+ BackWPup_Cron::check_cleanup();
1097
+
1098
+ exit();
1099
+ }
1100
+
1101
+ /**
1102
+ * Cleanup Temp Folder
1103
+ */
1104
+ public static function clean_temp_folder() {
1105
+
1106
+ $temp_dir = BackWPup::get_plugin_data( 'TEMP' );
1107
+ $do_not_delete_files = array( '.htaccess', 'nginx.conf', 'index.php', '.', '..', '.donotbackup' );
1108
+
1109
+ if ( is_writable( $temp_dir ) && $dir = opendir( $temp_dir ) ) {
1110
+ while ( false !== ( $file = readdir( $dir ) ) ) {
1111
+ if ( in_array( $file, $do_not_delete_files, true ) || is_dir( $temp_dir . $file ) || is_link( $temp_dir . $file ) ) {
1112
+ continue;
1113
+ }
1114
+ if ( is_writeable( $temp_dir . $file ) ) {
1115
+ unlink( $temp_dir . $file );
1116
  }
1117
  }
1118
+ closedir( $dir );
1119
  }
1120
  }
1121
 
1181
  }
1182
 
1183
  /**
 
1184
  *
1185
+ * Get a url to run a job of BackWPup
1186
  *
1187
+ * @param string $starttype Start types are 'runnow', 'runnowlink', 'cronrun', 'runext', 'restart', 'restartalt', 'test'
1188
+ * @param int $jobid The id of job to start else 0
1189
+ *
1190
+ * @return array|object [url] is the job url [header] for auth header or object form wp_remote_get()
1191
  */
1192
+ public static function get_jobrun_url( $starttype, $jobid = 0 ) {
 
 
 
 
 
 
 
1193
 
1194
+ $authentication = get_site_option( 'backwpup_cfg_authentication', array(
1195
+ 'method' => '',
1196
+ 'basic_user' => '',
1197
+ 'basic_password' => '',
1198
+ 'user_id' => 0,
1199
+ 'query_arg' => ''
1200
+ ) );
1201
+ $url = site_url( 'wp-cron.php' );
1202
+ $header = array( 'Cache-Control' => 'no-cache' );
1203
+ $authurl = '';
1204
+ $query_args = array(
1205
+ '_nonce' => substr( wp_hash( wp_nonce_tick() . 'backwpup_job_run-' . $starttype, 'nonce' ), - 12, 10 ),
1206
+ 'doing_wp_cron' => sprintf( '%.22F', microtime( true ) )
1207
+ );
1208
 
1209
+ if ( in_array( $starttype, array( 'restart', 'runnow', 'cronrun', 'runext', 'test' ), true ) ) {
1210
+ $query_args['backwpup_run'] = $starttype;
1211
  }
1212
 
1213
+ if ( in_array( $starttype, array( 'runnowlink', 'runnow', 'cronrun', 'runext' ), true ) && ! empty( $jobid ) ) {
1214
+ $query_args['jobid'] = $jobid;
 
 
 
 
 
1215
  }
1216
 
1217
+ if ( ! empty( $authentication['basic_user'] ) && ! empty( $authentication['basic_password'] ) && $authentication['method'] == 'basic' ) {
1218
+ $header['Authorization'] = 'Basic ' . base64_encode( $authentication['basic_user'] . ':' . BackWPup_Encryption::decrypt( $authentication['basic_password'] ) );
1219
+ $authurl = urlencode( $authentication['basic_user'] ) . ':' . urlencode( BackWPup_Encryption::decrypt( $authentication['basic_password'] ) ) . '@';
1220
+ }
1221
 
1222
+ if ( ! empty( $authentication['query_arg'] ) && $authentication['method'] == 'query_arg' ) {
1223
+ $url .= '?' . $authentication['query_arg'];
1224
+ }
1225
 
1226
+ if ( $starttype === 'runext' ) {
1227
+ $query_args['_nonce'] = get_site_option( 'backwpup_cfg_jobrunauthkey' );
1228
+ $query_args['doing_wp_cron'] = null;
1229
+ if ( ! empty( $authurl ) ) {
1230
+ $url = str_replace( 'https://', 'https://' . $authurl, $url );
1231
+ $url = str_replace( 'http://', 'http://' . $authurl, $url );
1232
+ }
 
 
 
 
1233
  }
1234
 
1235
+ if ( $starttype === 'runnowlink' && ( ! defined( 'ALTERNATE_WP_CRON' ) || ! ALTERNATE_WP_CRON ) ) {
1236
+ $url = wp_nonce_url( network_admin_url( 'admin.php' ), 'backwpup_job_run-' . $starttype );
1237
+ $query_args['page'] = 'backwpupjobs';
1238
+ $query_args['action'] = 'runnow';
1239
+ $query_args['doing_wp_cron'] = null;
1240
+ unset( $query_args['_nonce'] );
1241
+ }
 
 
 
 
 
1242
 
1243
+ if ( $starttype === 'runnowlink' && defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
1244
+ $query_args['backwpup_run'] = 'runnowalt';
1245
+ $query_args['_nonce'] = substr( wp_hash( wp_nonce_tick() . 'backwpup_job_run-runnowalt', 'nonce' ), - 12, 10 );
1246
+ $query_args['doing_wp_cron'] = null;
1247
  }
1248
 
1249
+ if ( $starttype === 'restartalt' && defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
1250
+ $query_args['backwpup_run'] = 'restart';
1251
+ $query_args['_nonce'] = null;
1252
  }
1253
 
1254
+ if ( $starttype === 'restart' || $starttype === 'test' ) {
1255
+ $query_args['_nonce'] = null;
 
1256
  }
1257
 
1258
+ if ( ! empty( $authentication['user_id'] ) && $authentication['method'] === 'user' ) {
1259
+ //cache cookies for auth some
1260
+ $cookies = get_site_transient( 'backwpup_cookies' );
1261
+ if ( empty( $cookies ) ) {
1262
+ $wp_admin_user = get_users( array( 'role' => 'administrator', 'number' => 1 ) );
1263
+ if ( empty( $wp_admin_user ) ) {
1264
+ $wp_admin_user = get_users( array( 'role' => 'backwpup_admin', 'number' => 1 ) );
1265
+ }
1266
+ if ( ! empty( $wp_admin_user[0]->ID ) ) {
1267
+ $expiration = time() + ( 356 * DAY_IN_SECONDS );
1268
+ $manager = WP_Session_Tokens::get_instance( $wp_admin_user[0]->ID );
1269
+ $token = $manager->create( $expiration );
1270
+ $cookies[ LOGGED_IN_COOKIE ] = wp_generate_auth_cookie( $wp_admin_user[0]->ID, $expiration, 'logged_in', $token );
1271
+ }
1272
+ set_site_transient( 'backwpup_cookies', $cookies, HOUR_IN_SECONDS - 30 );
1273
  }
1274
+ } else {
1275
+ $cookies = '';
1276
  }
1277
 
1278
+ $cron_request = array(
1279
+ 'url' => add_query_arg( $query_args, $url ),
1280
+ 'key' => $query_args['doing_wp_cron'],
1281
+ 'args' => array(
1282
+ 'blocking' => false,
1283
+ 'sslverify' => false,
1284
+ 'timeout' => 0.01,
1285
+ 'headers' => $header,
1286
+ 'user-agent' => BackWPup::get_plugin_data( 'User-Agent' )
1287
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1288
  );
1289
 
1290
+ if ( ! empty( $cookies ) ) {
1291
+ foreach ( $cookies as $name => $value ) {
1292
+ $cron_request['args']['cookies'][] = new WP_Http_Cookie( array( 'name' => $name, 'value' => $value ) );
 
 
 
 
1293
  }
1294
  }
1295
 
1296
+ $cron_request = apply_filters( 'cron_request', $cron_request );
 
 
 
 
 
 
 
1297
 
1298
+ if ( $starttype === 'test' ) {
1299
+ $cron_request['args']['timeout'] = 15;
1300
+ $cron_request['args']['blocking'] = true;
1301
  }
1302
 
1303
+ if ( ! in_array( $starttype, array( 'runnowlink', 'runext', 'restartalt' ), true ) ) {
1304
+ delete_transient( 'doing_cron' );
1305
+
1306
+ return wp_remote_post( $cron_request['url'], $cron_request['args'] );
1307
  }
1308
 
1309
+ return $cron_request;
1310
  }
1311
 
1312
  /**
1313
+ * Run baby run
 
1314
  */
1315
+ public function run() {
1316
+ global $wpdb;
1317
+ /* @var wpdb $wpdb */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1318
 
1319
+ //disable output buffering
1320
+ if ( $level = ob_get_level() ) {
1321
+ for ( $i = 0; $i < $level; $i ++ ) {
1322
+ ob_end_clean();
 
1323
  }
1324
  }
1325
 
1326
+ // Job can't run it is not created
1327
+ if ( empty( $this->steps_todo ) || empty( $this->logfile ) ) {
1328
+ $running_file = BackWPup::get_plugin_data( 'running_file' );
1329
+ if ( file_exists( $running_file ) ) {
1330
+ unlink( $running_file );
1331
+ }
 
 
 
 
 
1332
 
1333
+ return;
 
 
 
1334
  }
1335
 
1336
+ //Check double running and inactivity
1337
+ $last_update = microtime( true ) - $this->timestamp_last_update;
1338
+ if ( ! empty( $this->pid ) && $last_update > 300 ) {
1339
+ $this->log( __( 'Job restarts due to inactivity for more than 5 minutes.', 'backwpup' ), E_USER_WARNING );
1340
+ } elseif ( ! empty( $this->pid ) ) {
1341
+ return;
1342
+ }
1343
+ // set timestamp of script start
1344
+ $this->timestamp_script_start = microtime( true );
1345
+ //set Pid
1346
+ $this->pid = self::get_pid();
1347
+ $this->uniqid = uniqid( '', true );
1348
+ //Early write new working file
1349
+ $this->write_running_file();
1350
+ if ( $this->is_debug() ) {
1351
+ @ini_set( 'error_log', $this->logfile );
1352
+ error_reporting( - 1 );
1353
+ }
1354
+ @ini_set( 'display_errors', '0' );
1355
+ @ini_set( 'log_errors', '1' );
1356
+ @ini_set( 'html_errors', '0' );
1357
+ @ini_set( 'report_memleaks', '1' );
1358
+ @ini_set( 'zlib.output_compression', '0' );
1359
+ @ini_set( 'implicit_flush', '0' );
1360
+ @putenv( 'TMPDIR=' . BackWPup::get_plugin_data( 'TEMP' ) );
1361
+ //Write Wordpress DB errors to log
1362
+ $wpdb->suppress_errors( false );
1363
+ $wpdb->hide_errors();
1364
+ //set wp max memory limit
1365
+ @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
1366
+ //set error handler
1367
+ if ( ! empty( $this->logfile ) ) {
1368
+ if ( $this->is_debug() ) {
1369
+ set_error_handler( array( $this, 'log' ) );
1370
+ } else {
1371
+ set_error_handler( array( $this, 'log' ), E_ALL ^ E_NOTICE );
1372
+ }
1373
+ }
1374
+ set_exception_handler( array( $this, 'exception_handler' ) );
1375
+ // execute function on job shutdown register_shutdown_function( array( $this, 'shutdown' ) );
1376
+ add_action( 'shutdown', array( $this, 'shutdown' ) );
1377
+
1378
+ if ( function_exists( 'pcntl_signal' ) ) {
1379
+ $signals = array(
1380
+ 'SIGHUP', //Term
1381
+ 'SIGINT', //Term
1382
+ 'SIGQUIT', //Core
1383
+ 'SIGILL', //Core
1384
+ //'SIGTRAP', //Core
1385
+ 'SIGABRT', //Core
1386
+ 'SIGBUS', //Core
1387
+ 'SIGFPE', //Core
1388
+ //'SIGKILL', //Term
1389
+ 'SIGSEGV', //Core
1390
+ //'SIGPIPE', Term
1391
+ //'SIGALRM', Term
1392
+ 'SIGTERM', //Term
1393
+ 'SIGSTKFLT', //Term
1394
+ 'SIGUSR1',//Term
1395
+ 'SIGUSR2', //Term
1396
+ //'SIGCHLD', //Ign
1397
+ //'SIGCONT', //Cont
1398
+ //'SIGSTOP', //Stop
1399
+ //'SIGTSTP', //Stop
1400
+ //'SIGTTIN', //Stop
1401
+ //'SIGTTOU', //Stop
1402
+ //'SIGURG', //Ign
1403
+ 'SIGXCPU', //Core
1404
+ 'SIGXFSZ', //Core
1405
+ //'SIGVTALRM', //Term
1406
+ //'SIGPROF', //Term
1407
+ //'SIGWINCH', //Ign
1408
+ //'SIGIO', //Term
1409
+ 'SIGPWR', //Term
1410
+ 'SIGSYS' //Core
1411
+ );
1412
+ $signals = apply_filters( 'backwpup_job_signals_to_handel', $signals );
1413
+ declare( ticks = 1 );
1414
+ $this->signal = 0;
1415
+ foreach ( $signals as $signal ) {
1416
+ if ( defined( $signal ) ) {
1417
+ pcntl_signal( constant( $signal ), array( $this, 'signal_handler' ), false );
1418
  }
1419
  }
1420
+ }
1421
+ $job_types = BackWPup::get_job_types();
1422
+ //go step by step
1423
+ foreach ( $this->steps_todo as $this->step_working ) {
1424
+ //Check if step already done
1425
+ if ( in_array( $this->step_working, $this->steps_done, true ) ) {
1426
+ continue;
1427
+ }
1428
+ //calc step percent
1429
+ if ( count( $this->steps_done ) > 0 ) {
1430
+ $this->step_percent = round( count( $this->steps_done ) / count( $this->steps_todo ) * 100 );
1431
+ } else {
1432
+ $this->step_percent = 1;
1433
+ }
1434
+ // do step tries
1435
+ while ( true ) {
1436
+ if ( $this->steps_data[ $this->step_working ]['STEP_TRY'] >= get_site_option( 'backwpup_cfg_jobstepretry' ) ) {
1437
+ $this->log( __( 'Step aborted: too many attempts!', 'backwpup' ), E_USER_ERROR );
1438
+ $this->temp = array();
1439
+ $this->steps_done[] = $this->step_working;
1440
+ $this->substeps_done = 0;
1441
+ $this->substeps_todo = 0;
1442
+ $this->do_restart();
1443
+ break;
1444
+ }
1445
+
1446
+ $this->steps_data[ $this->step_working ]['STEP_TRY'] ++;
1447
+ $done = false;
1448
+
1449
+ //executes the methods of job process
1450
+ if ( $this->step_working == 'CREATE_ARCHIVE' ) {
1451
+ $done = $this->create_archive();
1452
+ } elseif ( $this->step_working == 'CREATE_MANIFEST' ) {
1453
+ $done = $this->create_manifest();
1454
+ } elseif ( $this->step_working == 'END' ) {
1455
+ $this->end();
1456
+ break 2;
1457
+ } elseif ( strstr( $this->step_working, 'JOB_' ) ) {
1458
+ $done = $job_types[ str_replace( 'JOB_', '', $this->step_working ) ]->job_run( $this );
1459
+ } elseif ( strstr( $this->step_working, 'DEST_SYNC_' ) ) {
1460
+ $done = BackWPup::get_destination( str_replace( 'DEST_SYNC_', '', $this->step_working ) )->job_run_sync( $this );
1461
+ } elseif ( strstr( $this->step_working, 'DEST_' ) ) {
1462
+ $done = BackWPup::get_destination( str_replace( 'DEST_', '', $this->step_working ) )->job_run_archive( $this );
1463
+ } elseif ( ! empty( $this->steps_data[ $this->step_working ]['CALLBACK'] ) ) {
1464
+ $done = $this->steps_data[ $this->step_working ]['CALLBACK']( $this );
1465
+ }
1466
+
1467
+ // set step as done
1468
+ if ( $done === true ) {
1469
+ $this->temp = array();
1470
+ $this->steps_done[] = $this->step_working;
1471
+ $this->substeps_done = 0;
1472
+ $this->substeps_todo = 0;
1473
+ $this->update_working_data( true );
1474
+ }
1475
+ if ( count( $this->steps_done ) < count( $this->steps_todo ) - 1 ) {
1476
+ $this->do_restart();
1477
+ }
1478
+ if ( $done === true ) {
1479
+ break;
1480
+ }
1481
  }
1482
  }
1483
+ }
1484
+
1485
+ /**
1486
+ * Creates the backup archive
1487
+ */
1488
+ private function create_archive() {
1489
+
1490
+ //load folders to backup
1491
+ $folders_to_backup = $this->get_folders_to_backup();
1492
+
1493
+ $this->substeps_todo = $this->count_folder + 1;
1494
+
1495
+ //initial settings for restarts in archiving
1496
+ if ( ! isset( $this->steps_data[ $this->step_working ]['on_file'] ) ) {
1497
+ $this->steps_data[ $this->step_working ]['on_file'] = '';
1498
+ }
1499
+ if ( ! isset( $this->steps_data[ $this->step_working ]['on_folder'] ) ) {
1500
+ $this->steps_data[ $this->step_working ]['on_folder'] = '';
1501
+ }
1502
+
1503
+ if ( $this->steps_data[ $this->step_working ]['on_folder'] == '' && $this->steps_data[ $this->step_working ]['on_file'] == '' && is_file( $this->backup_folder . $this->backup_file ) ) {
1504
+ unlink( $this->backup_folder . $this->backup_file );
1505
+ }
1506
+
1507
+ if ( $this->steps_data[ $this->step_working ]['SAVE_STEP_TRY'] != $this->steps_data[ $this->step_working ]['STEP_TRY'] ) {
1508
+ $this->log( sprintf( __( '%d. Trying to create backup archive &hellip;', 'backwpup' ), $this->steps_data[ $this->step_working ]['STEP_TRY'] ), E_USER_NOTICE );
1509
+ }
1510
+
1511
+ try {
1512
+ $backup_archive = new BackWPup_Create_Archive( $this->backup_folder . $this->backup_file );
1513
+
1514
+ //show method for creation
1515
+ if ( $this->substeps_done == 0 ) {
1516
+ $this->log( sprintf( _x( 'Compressing files as %s. Please be patient, this may take a moment.', 'Archive compression method', 'backwpup' ), $backup_archive->get_method() ) );
1517
+ }
1518
+
1519
+ //add extra files
1520
+ if ( $this->substeps_done == 0 ) {
1521
+ if ( ! empty( $this->additional_files_to_backup ) && $this->substeps_done == 0 ) {
1522
+ if ( $this->is_debug() ) {
1523
+ $this->log( __( 'Adding Extra files to Archive', 'backwpup' ) );
1524
+ }
1525
+ foreach ( $this->additional_files_to_backup as $file ) {
1526
+ if ( $backup_archive->add_file( $file, basename( $file ) ) ) {
1527
+ ;
1528
+ $this->count_files ++;
1529
+ $this->count_files_size = $this->count_files_size + filesize( $file );
1530
+ $this->update_working_data();
1531
+ } else {
1532
+ $backup_archive->close();
1533
+ $this->steps_data[ $this->step_working ]['on_file'] = '';
1534
+ $this->steps_data[ $this->step_working ]['on_folder'] = '';
1535
+ $this->log( __( 'Cannot create backup archive correctly. Aborting creation.', 'backwpup' ), E_USER_ERROR );
1536
+
1537
+ return false;
1538
+ }
1539
+ }
1540
+ }
1541
+ $this->substeps_done ++;
1542
+ }
1543
+
1544
+ //add normal files
1545
+ while ( $folder = array_shift( $folders_to_backup ) ) {
1546
+ //jump over already done folders
1547
+ if ( in_array( $this->steps_data[ $this->step_working ]['on_folder'], $folders_to_backup, true ) ) {
1548
+ continue;
1549
+ }
1550
+ if ( $this->is_debug() ) {
1551
+ $this->log( sprintf( __( 'Archiving Folder: %s', 'backwpup' ), $folder ) );
1552
+ }
1553
+ $this->steps_data[ $this->step_working ]['on_folder'] = $folder;
1554
+ $files_in_folder = $this->get_files_in_folder( $folder );
1555
+ //add empty folders
1556
+ if ( empty( $files_in_folder ) ) {
1557
+ $folder_name_in_archive = trim( ltrim( $this->get_destination_path_replacement( $folder ), '/' ) );
1558
+ if ( ! empty ( $folder_name_in_archive ) ) {
1559
+ $backup_archive->add_empty_folder( $folder, $folder_name_in_archive );
1560
+ }
1561
+ continue;
1562
+ }
1563
+ //add files
1564
+ while ( $file = array_shift( $files_in_folder ) ) {
1565
+ //jump over already done files
1566
+ if ( in_array( $this->steps_data[ $this->step_working ]['on_file'], $files_in_folder, true ) ) {
1567
+ continue;
1568
+ }
1569
+ $this->steps_data[ $this->step_working ]['on_file'] = $file;
1570
+ //restart if needed
1571
+ $restart_time = $this->get_restart_time();
1572
+ if ( $restart_time <= 0 ) {
1573
+ unset( $backup_archive );
1574
+ $this->do_restart_time( true );
1575
+
1576
+ return false;
1577
+ }
1578
+ //generate filename in archive
1579
+ $in_archive_filename = ltrim( $this->get_destination_path_replacement( $file ), '/' );
1580
+ //add file to archive
1581
+ if ( $backup_archive->add_file( $file, $in_archive_filename ) ) {
1582
+ $this->count_files ++;
1583
+ $this->count_files_size = $this->count_files_size + filesize( $file );
1584
+ $this->update_working_data();
1585
+ } else {
1586
+ $backup_archive->close();
1587
+ unset( $backup_archive );
1588
+ $this->steps_data[ $this->step_working ]['on_file'] = '';
1589
+ $this->steps_data[ $this->step_working ]['on_folder'] = '';
1590
+ $this->substeps_done = 0;
1591
+ $this->backup_filesize = filesize( $this->backup_folder . $this->backup_file );
1592
+ if ( $this->backup_filesize === false ) {
1593
+ $this->backup_filesize = PHP_INT_MAX;
1594
+ }
1595
+ $this->log( __( 'Cannot create backup archive correctly. Aborting creation.', 'backwpup' ), E_USER_ERROR );
1596
+
1597
+ return false;
1598
+ }
1599
+ }
1600
+ $this->steps_data[ $this->step_working ]['on_file'] = '';
1601
+ $this->substeps_done ++;
1602
+ }
1603
+ $backup_archive->close();
1604
+ unset( $backup_archive );
1605
+ $this->log( __( 'Backup archive created.', 'backwpup' ), E_USER_NOTICE );
1606
+ } catch ( Exception $e ) {
1607
+ $this->log( $e->getMessage(), E_USER_ERROR, $e->getFile(), $e->getLine() );
1608
+ unset( $backup_archive );
1609
+
1610
+ return false;
1611
+ }
1612
+
1613
+ $this->backup_filesize = filesize( $this->backup_folder . $this->backup_file );
1614
+ if ( $this->backup_filesize === false ) {
1615
+ $this->backup_filesize = PHP_INT_MAX;
1616
+ }
1617
+
1618
+ if ( $this->backup_filesize >= PHP_INT_MAX ) {
1619
+ $this->log( __( 'The Backup archive will be too large for file operations with this PHP Version. You might want to consider splitting the backup job in multiple jobs with less files each.', 'backwpup' ), E_USER_ERROR );
1620
+ $this->end();
1621
+ } else {
1622
+ $this->log( sprintf( __( 'Archive size is %s.', 'backwpup' ), size_format( $this->backup_filesize, 2 ) ), E_USER_NOTICE );
1623
+ }
1624
+
1625
+ $this->log( sprintf( __( '%1$d Files with %2$s in Archive.', 'backwpup' ), $this->count_files, size_format( $this->count_files_size, 2 ) ), E_USER_NOTICE );
1626
+
1627
+ return true;
1628
+ }
1629
+
1630
+ /**
1631
+ * Get list of Folder for backup
1632
+ *
1633
+ * @return array folder list
1634
+ */
1635
+ public function get_folders_to_backup() {
1636
+
1637
+ $file = BackWPup::get_plugin_data( 'temp' ) . 'backwpup-' . BackWPup::get_plugin_data( 'hash' ) . '-folder.php';
1638
+
1639
+ if ( ! file_exists( $file ) ) {
1640
+ return array();
1641
+ }
1642
+
1643
+ $folders = array();
1644
+
1645
+ $file_data = file( $file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
1646
+
1647
+ foreach ( $file_data as $folder ) {
1648
+ $folder = trim( str_replace( array( '<?php', '//' ), '', $folder ) );
1649
+ if ( ! empty( $folder ) && is_dir( $folder ) ) {
1650
+ $folders[] = $folder;
1651
+ }
1652
+ }
1653
+ $folders = array_unique( $folders );
1654
+ sort( $folders );
1655
+ $this->count_folder = count( $folders );
1656
+
1657
+ return $folders;
1658
+ }
1659
+
1660
+ /**
1661
+ *
1662
+ * Get back a array of files to backup in the selected folder
1663
+ *
1664
+ * @param string $folder the folder to get the files from
1665
+ *
1666
+ * @return array files to backup
1667
+ */
1668
+ public function get_files_in_folder( $folder ) {
1669
+
1670
+ $files = array();
1671
+ $folder = trailingslashit( $folder );
1672
+
1673
+ if ( ! is_dir( $folder ) ) {
1674
+ $this->log( sprintf( _x( 'Folder %s not exists', 'Folder name', 'backwpup' ), $folder ), E_USER_WARNING );
1675
+
1676
+ return $files;
1677
+ }
1678
 
1679
+ if ( ! is_readable( $folder ) ) {
1680
+ $this->log( sprintf( _x( 'Folder %s not readable', 'Folder name', 'backwpup' ), $folder ), E_USER_WARNING );
1681
+
1682
+ return $files;
1683
+ }
1684
+
1685
+ if ( $dir = opendir( $folder ) ) {
1686
+ while ( false !== ( $file = readdir( $dir ) ) ) {
1687
+ if ( in_array( $file, array( '.', '..' ), true ) || is_dir( $folder . $file ) ) {
1688
+ continue;
1689
+ }
1690
+ foreach ( $this->exclude_from_backup as $exclusion ) { //exclude files
1691
+ $exclusion = trim( $exclusion );
1692
+ if ( false !== stripos( $folder . $file, trim( $exclusion ) ) && ! empty( $exclusion ) ) {
1693
+ continue 2;
1694
+ }
1695
+ }
1696
+ if ( $this->job['backupexcludethumbs'] && strpos( $folder, BackWPup_File::get_upload_dir() ) !== false && preg_match( "/\-[0-9]{1,4}x[0-9]{1,4}.+\.(jpg|png|gif)$/i", $file ) ) {
1697
+ continue;
1698
+ }
1699
+ if ( is_link( $folder . $file ) ) {
1700
+ $this->log( sprintf( __( 'Link "%s" not following.', 'backwpup' ), $folder . $file ), E_USER_WARNING );
1701
+ } elseif ( ! is_readable( $folder . $file ) ) {
1702
+ $this->log( sprintf( __( 'File "%s" is not readable!', 'backwpup' ), $folder . $file ), E_USER_WARNING );
1703
+ } else {
1704
+ $file_size = filesize( $folder . $file );
1705
+ if ( ! is_int( $file_size ) || $file_size < 0 || $file_size > 2147483647 ) {
1706
+ $this->log( sprintf( __( 'File size of “%s” cannot be retrieved. File might be too large and will not be added to queue.', 'backwpup' ), $folder . $file . ' ' . $file_size ), E_USER_WARNING );
1707
+ continue;
1708
+ }
1709
+ $files[] = $folder . $file;
1710
  }
1711
  }
1712
+ closedir( $dir );
 
 
1713
  }
1714
 
1715
+ return $files;
1716
  }
1717
 
1718
  /**
1719
+ * Get job restart time
1720
  *
1721
+ * @return int remaining time
 
 
1722
  */
1723
+ public function get_restart_time() {
1724
 
1725
+ if ( php_sapi_name() == 'cli' ) {
1726
+ return 300;
1727
+ }
1728
+
1729
+ $job_max_execution_time = get_site_option( 'backwpup_cfg_jobmaxexecutiontime' );
1730
+
1731
+ if ( empty( $job_max_execution_time ) ) {
1732
+ return 300;
1733
+ }
1734
+
1735
+ $execution_time = microtime( true ) - $this->timestamp_script_start;
1736
+
1737
+ return $job_max_execution_time - $execution_time - 3;
1738
  }
1739
 
1740
  /**
1741
+ * Do a job restart
1742
  *
1743
+ * @param bool $do_restart_now should time restart now be done
 
 
 
1744
  *
1745
+ * @return int remaining time
1746
  */
1747
+ public function do_restart_time( $do_restart_now = false ) {
1748
 
1749
+ if ( php_sapi_name() == 'cli' ) {
1750
+ return 300;
 
1751
  }
1752
 
1753
+ //do restart after signal is send
1754
+ if ( $this->signal !== 0 ) {
1755
+ $this->steps_data[ $this->step_working ]['SAVE_STEP_TRY'] = $this->steps_data[ $this->step_working ]['STEP_TRY'];
1756
+ $this->steps_data[ $this->step_working ]['STEP_TRY'] -= 1;
1757
+ $this->do_restart( true );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1758
  }
1759
 
1760
+ $job_max_execution_time = get_site_option( 'backwpup_cfg_jobmaxexecutiontime' );
 
 
 
1761
 
1762
+ if ( empty( $job_max_execution_time ) ) {
1763
+ return 300;
 
 
 
 
 
1764
  }
1765
 
1766
+ $execution_time = microtime( true ) - $this->timestamp_script_start;
 
1767
 
1768
+ // do restart 3 sec. before max. execution time
1769
+ if ( $do_restart_now || $execution_time >= ( $job_max_execution_time - 3 ) ) {
1770
+ $this->steps_data[ $this->step_working ]['SAVE_STEP_TRY'] = $this->steps_data[ $this->step_working ]['STEP_TRY'];
1771
+ $this->steps_data[ $this->step_working ]['STEP_TRY'] -= 1;
1772
+ $this->do_restart( true );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1773
  }
1774
 
1775
+ return $job_max_execution_time - $execution_time;
1776
+ }
1777
 
1778
+ /**
1779
+ * create manifest file
1780
+ * @return bool
1781
+ */
1782
+ public function create_manifest() {
 
 
 
 
 
 
 
 
 
 
 
 
1783
 
1784
+ $this->substeps_todo = 3;
 
 
 
 
 
1785
 
1786
+ $this->log( sprintf( __( '%d. Trying to generate a manifest file&#160;&hellip;', 'backwpup' ), $this->steps_data[ $this->step_working ]['STEP_TRY'] ) );
1787
+
1788
+ //build manifest
1789
+ $manifest = array();
1790
+ // add blog information
1791
+ $manifest['blog_info']['url'] = home_url();
1792
+ $manifest['blog_info']['wpurl'] = site_url();
1793
+ $manifest['blog_info']['prefix'] = $GLOBALS['wpdb']->prefix;
1794
+ $manifest['blog_info']['description'] = get_option( 'blogdescription' );
1795
+ $manifest['blog_info']['stylesheet_directory'] = get_template_directory_uri();
1796
+ $manifest['blog_info']['activate_plugins'] = wp_get_active_and_valid_plugins();
1797
+ $manifest['blog_info']['activate_theme'] = wp_get_theme()->get( 'Name' );
1798
+ $manifest['blog_info']['admin_email'] = get_option( 'admin_email' );
1799
+ $manifest['blog_info']['charset'] = get_bloginfo( 'charset' );
1800
+ $manifest['blog_info']['version'] = BackWPup::get_plugin_data( 'wp_version' );
1801
+ $manifest['blog_info']['backwpup_version'] = BackWPup::get_plugin_data( 'version' );
1802
+ $manifest['blog_info']['language'] = get_bloginfo( 'language' );
1803
+ $manifest['blog_info']['name'] = get_bloginfo( 'name' );
1804
+ $manifest['blog_info']['abspath'] = ABSPATH;
1805
+ $manifest['blog_info']['uploads'] = wp_upload_dir( null, false, true );
1806
+ $manifest['blog_info']['contents']['basedir'] = WP_CONTENT_DIR;
1807
+ $manifest['blog_info']['contents']['baseurl'] = WP_CONTENT_URL;
1808
+ $manifest['blog_info']['plugins']['basedir'] = WP_PLUGIN_DIR;
1809
+ $manifest['blog_info']['plugins']['baseurl'] = WP_PLUGIN_URL;
1810
+ $manifest['blog_info']['themes']['basedir'] = get_theme_root();
1811
+ $manifest['blog_info']['themes']['baseurl'] = get_theme_root_uri();
1812
+ // add job settings
1813
+ $manifest['job_settings'] = $this->job;
1814
+ // add archive info
1815
+ foreach ( $this->additional_files_to_backup as $file ) {
1816
+ $manifest['archive']['extra_files'][] = basename( $file );
1817
  }
1818
+ if ( isset( $this->steps_data['JOB_FILE'] ) ) {
1819
+ if ( $this->job['backuproot'] ) {
1820
+ $manifest['archive']['abspath'] = trailingslashit( $this->get_destination_path_replacement( ABSPATH ) );
1821
+ }
1822
+ if ( $this->job['backupuploads'] ) {
1823
+ $manifest['archive']['uploads'] = trailingslashit( $this->get_destination_path_replacement( BackWPup_File::get_upload_dir() ) );
1824
+ }
1825
+ if ( $this->job['backupcontent'] ) {
1826
+ $manifest['archive']['contents'] = trailingslashit( $this->get_destination_path_replacement( WP_CONTENT_DIR ) );
1827
+ }
1828
+ if ( $this->job['backupplugins'] ) {
1829
+ $manifest['archive']['plugins'] = trailingslashit( $this->get_destination_path_replacement( WP_PLUGIN_DIR ) );
1830
+ }
1831
+ if ( $this->job['backupthemes'] ) {
1832
+ $manifest['archive']['themes'] = trailingslashit( $this->get_destination_path_replacement( get_theme_root() ) );
1833
+ }
1834
  }
1835
+
1836
+ if ( ! file_put_contents( BackWPup::get_plugin_data( 'TEMP' ) . 'manifest.json', json_encode( $manifest ) ) ) {
1837
+ return false;
1838
  }
1839
+ $this->substeps_done = 1;
 
 
 
 
 
 
1840
 
1841
+ //Create backwpup_readme.txt
1842
+ $readme_text = __( 'You may have noticed the manifest.json file in this archive.', 'backwpup' ) . PHP_EOL;
1843
+ $readme_text .= __( 'manifest.json might be needed for later restoring a backup from this archive.', 'backwpup' ) . PHP_EOL;
1844
+ $readme_text .= __( 'Please leave manifest.json untouched and in place. Otherwise it is safe to be ignored.', 'backwpup' ) . PHP_EOL;
1845
+ if ( ! file_put_contents( BackWPup::get_plugin_data( 'TEMP' ) . 'backwpup_readme.txt', $readme_text ) ) {
1846
+ return false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1847
  }
1848
+ $this->substeps_done = 2;
1849
 
1850
+ //add file to backup files
1851
+ if ( is_readable( BackWPup::get_plugin_data( 'TEMP' ) . 'manifest.json' ) ) {
1852
+ $this->additional_files_to_backup[] = BackWPup::get_plugin_data( 'TEMP' ) . 'manifest.json';
1853
+ $this->additional_files_to_backup[] = BackWPup::get_plugin_data( 'TEMP' ) . 'backwpup_readme.txt';
1854
+ $this->log( sprintf( __( 'Added manifest.json file with %1$s to backup file list.', 'backwpup' ), size_format( filesize( BackWPup::get_plugin_data( 'TEMP' ) . 'manifest.json' ), 2 ) ) );
1855
+ }
1856
+ $this->substeps_done = 3;
1857
 
 
1858
  return true;
1859
  }
1860
 
1861
  /**
1862
+ * @param $jobid
 
 
 
 
1863
  */
1864
+ public static function start_cli( $jobid ) {
 
 
 
 
 
 
 
 
 
1865
 
1866
+ if ( php_sapi_name() != 'cli' ) {
 
 
1867
  return;
1868
  }
1869
 
1870
+ //define DOING_CRON to prevent caching
1871
+ if ( ! defined( 'DOING_CRON' ) ) {
1872
+ define( 'DOING_CRON', true );
 
 
 
 
 
 
 
 
 
 
 
1873
  }
1874
 
1875
+ //load text domain
1876
+ $log_level = get_site_option( 'backwpup_cfg_loglevel', 'normal_translated' );
1877
+ if ( strstr( $log_level, 'translated' ) ) {
1878
+ BackWPup::load_text_domain();
 
1879
  } else {
1880
+ add_filter( 'override_load_textdomain', '__return_true' );
1881
+ $GLOBALS['l10n'] = array();
 
 
 
 
1882
  }
 
1883
 
1884
+ $jobid = absint( $jobid );
1885
 
1886
+ //Logs Folder
1887
+ $log_folder = get_site_option( 'backwpup_cfg_logfolder' );
1888
+ $log_folder = BackWPup_File::get_absolute_path( $log_folder );
1889
 
1890
+ //check job id exists
1891
+ $jobids = BackWPup_Option::get_job_ids();
1892
+ if ( ! in_array( $jobid, $jobids, true ) ) {
1893
+ die( __( 'Wrong BackWPup JobID', 'backwpup' ) );
1894
+ }
1895
+ //check folders
1896
+ $log_folder_message = BackWPup_File::check_folder( $log_folder );
1897
+ if ( ! empty( $log_folder_message ) ) {
1898
+ die( $log_folder_message );
1899
+ }
1900
+ $log_folder_message = BackWPup_File::check_folder( BackWPup::get_plugin_data( 'TEMP' ), true );
1901
+ if ( ! empty( $log_folder_message ) ) {
1902
+ die( $log_folder_message );
1903
+ }
1904
+ //check running job
1905
+ if ( file_exists( BackWPup::get_plugin_data( 'running_file' ) ) ) {
1906
+ die( __( 'A BackWPup job is already running', 'backwpup' ) );
1907
  }
1908
+
1909
+ //start class
1910
+ $backwpup_job_object = new self();
1911
+ $backwpup_job_object->create( 'runcli', (int) $jobid );
1912
+ $backwpup_job_object->run();
1913
  }
1914
 
1915
  /**
1916
+ * disable caches
 
 
1917
  */
1918
+ public static function disable_caches() {
 
 
 
1919
 
1920
+ //Special settings
1921
+ @putenv( 'nokeepalive=1' );
1922
+ @ini_set( 'zlib.output_compression', 'Off' );
1923
 
1924
+ // deactivate caches
1925
+ if ( ! defined( 'DONOTCACHEDB' ) ) {
1926
+ define( 'DONOTCACHEDB', true );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1927
  }
1928
+ if ( ! defined( 'DONOTCACHEPAGE' ) ) {
1929
+ define( 'DONOTCACHEPAGE', true );
 
 
 
 
 
 
1930
  }
1931
+ }
1932
 
1933
+ /**
1934
+ *
1935
+ * Reads a BackWPup logfile header and gives back a array of information
1936
+ *
1937
+ * @param string $logfile full logfile path
1938
+ *
1939
+ * @return array|bool
1940
+ */
1941
+ public static function read_logheader( $logfile ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1942
 
1943
+ $usedmetas = array(
1944
+ "date" => "logtime",
1945
+ "backwpup_logtime" => "logtime", //old value of date
1946
+ "backwpup_errors" => "errors",
1947
+ "backwpup_warnings" => "warnings",
1948
+ "backwpup_jobid" => "jobid",
1949
+ "backwpup_jobname" => "name",
1950
+ "backwpup_jobtype" => "type",
1951
+ "backwpup_jobruntime" => "runtime",
1952
+ "backwpup_backupfilesize" => "backupfilesize"
1953
+ );
1954
 
1955
+ //get metadata of logfile
1956
+ $metas = array();
1957
+ if ( is_readable( $logfile ) ) {
1958
+ if ( '.gz' == substr( $logfile, - 3 ) ) {
1959
+ $metas = (array) get_meta_tags( 'compress.zlib://' . $logfile );
1960
+ } else {
1961
+ $metas = (array) get_meta_tags( $logfile );
1962
  }
1963
  }
1964
 
1965
+ //only output needed data
1966
+ foreach ( $usedmetas as $keyword => $field ) {
1967
+ if ( isset( $metas[ $keyword ] ) ) {
1968
+ $joddata[ $field ] = $metas[ $keyword ];
1969
+ } else {
1970
+ $joddata[ $field ] = '';
1971
+ }
1972
+ }
 
 
 
1973
 
1974
+ //convert date
1975
+ if ( isset( $metas['date'] ) ) {
1976
+ $joddata['logtime'] = strtotime( $metas['date'] ) + ( get_option( 'gmt_offset' ) * 3600 );
1977
+ }
1978
 
1979
+ //use file create date if none
1980
+ if ( empty( $joddata['logtime'] ) ) {
1981
+ $joddata['logtime'] = filectime( $logfile );
1982
+ }
1983
 
1984
+ return $joddata;
1985
  }
1986
 
 
1987
  public static function user_abort() {
1988
 
1989
  /* @var $job_object BackWPup_Job */
2004
 
2005
  }
2006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2007
  /**
2008
  *
2009
  * Get the mime type of a file
2224
  return 'application/octet-stream';
2225
  }
2226
 
 
2227
  /**
2228
+ * Check whether exec has been disabled.
2229
  *
2230
+ * @access public
2231
+ * @static
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2232
  * @return bool
2233
  */
2234
+ public static function is_exec() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2235
 
2236
+ // Is function avail
2237
+ if ( ! function_exists( 'exec' ) ) {
2238
  return false;
2239
  }
 
2240
 
2241
+ // Is shell_exec disabled?
2242
+ if ( in_array( 'exec', array_map( 'trim', explode( ',', @ini_get( 'disable_functions' ) ) ), true ) ) {
 
 
 
2243
  return false;
2244
  }
 
2245
 
2246
+ // Can we issue a simple echo command?
2247
+ $output = exec( 'echo backwpupechotest' );
2248
+ if ( $output != 'backwpupechotest' ) {
2249
+ return false;
 
2250
  }
 
2251
 
2252
  return true;
2253
+
2254
  }
2255
 
2256
  /**
2257
+ * Delete some data on cloned objects
2258
  */
2259
+ public function __clone() {
 
 
 
2260
 
2261
+ $this->temp = array();
2262
+ $this->run = array();
2263
+ }
2264
 
2265
+ /**
2266
+ * Signal handler
2267
+ * @param $signal_send
2268
+ */
2269
+ public function signal_handler( $signal_send ) {
 
 
2270
 
2271
+ //known signals
2272
+ $signals = array(
2273
+ 'SIGHUP' => array(
2274
+ 'description' => _x( 'Hangup detected on controlling terminal or death of controlling process', 'SIGHUP: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2275
+ 'error' => E_USER_ERROR
2276
+ ),
2277
+ 'SIGINT' => array(
2278
+ 'description' => _x( 'Interrupt from keyboard', 'SIGINT: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2279
+ 'error' => E_USER_ERROR
2280
+ ),
2281
+ 'SIGQUIT' => array(
2282
+ 'description' => _x( 'Quit from keyboard', 'SIGQUIT: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2283
+ 'error' => E_USER_ERROR
2284
+ ),
2285
+ 'SIGILL' => array(
2286
+ 'description' => _x( 'Illegal Instruction', 'SIGILL: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2287
+ 'error' => E_USER_ERROR
2288
+ ),
2289
+ 'SIGABRT' => array(
2290
+ 'description' => _x( 'Abort signal from abort(3)', 'SIGABRT: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2291
+ 'error' => E_USER_NOTICE
2292
+ ),
2293
+ 'SIGBUS' => array(
2294
+ 'description' => _x( 'Bus error (bad memory access)', 'SIGBUS: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2295
+ 'error' => E_USER_ERROR
2296
+ ),
2297
+ 'SIGFPE' => array(
2298
+ 'description' => _x( 'Floating point exception', 'SIGFPE: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2299
+ 'error' => E_USER_ERROR
2300
+ ),
2301
+ 'SIGSEGV' => array(
2302
+ 'description' => _x( 'Invalid memory reference', 'SIGSEGV: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2303
+ 'error' => E_USER_ERROR
2304
+ ),
2305
+ 'SIGTERM' => array(
2306
+ 'description' => _x( 'Termination signal', 'SIGTERM: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2307
+ 'error' => E_USER_WARNING
2308
+ ),
2309
+ 'SIGSTKFLT' => array(
2310
+ 'description' => _x( 'Stack fault on coprocessor', 'SIGSTKFLT: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2311
+ 'error' => E_USER_ERROR
2312
+ ),
2313
+ 'SIGUSR1' => array(
2314
+ 'description' => _x( 'User-defined signal 1', 'SIGUSR1: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2315
+ 'error' => E_USER_NOTICE
2316
+ ),
2317
+ 'SIGUSR2' => array(
2318
+ 'description' => _x( 'User-defined signal 2', 'SIGUSR2: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2319
+ 'error' => E_USER_NOTICE
2320
+ ),
2321
+ 'SIGURG' => array(
2322
+ 'description' => _x( 'Urgent condition on socket', 'SIGURG: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2323
+ 'error' => E_USER_NOTICE
2324
+ ),
2325
+ 'SIGXCPU' => array(
2326
+ 'description' => _x( 'CPU time limit exceeded', 'SIGXCPU: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2327
+ 'error' => E_USER_ERROR
2328
+ ),
2329
+ 'SIGXFSZ' => array(
2330
+ 'description' => _x( 'File size limit exceeded', 'SIGXFSZ: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2331
+ 'error' => E_USER_ERROR
2332
+ ),
2333
+ 'SIGPWR' => array(
2334
+ 'description' => _x( 'Power failure', 'SIGPWR: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2335
+ 'error' => E_USER_ERROR
2336
+ ),
2337
+ 'SIGSYS' => array(
2338
+ 'description' => _x( 'Bad argument to routine', 'SIGSYS: Please see http://man7.org/linux/man-pages/man7/signal.7.html for details', 'backwpup' ),
2339
+ 'error' => E_USER_ERROR
2340
+ ),
2341
+ );
2342
 
2343
+ foreach ( $signals as $signal => $config ) {
2344
+ if ( defined( $signal ) && $signal_send === constant( $signal ) ) {
2345
+ $this->log( sprintf( __( 'Signal "%1$s" (%2$s) is sent to script!', 'backwpup' ), $signal, $config['description'] ), $config['error'] );
2346
+ $this->signal = $signal_send;
2347
+ break;
2348
+ }
2349
  }
2350
 
2351
+ }
 
2352
 
2353
+ /**
2354
+ *
2355
+ * Shutdown function is call if script terminates try to make a restart if needed
2356
+ *
2357
+ * Prepare the job for start
2358
+ *
2359
+ * @internal param int the signal that terminates the job
2360
+ */
2361
+ public function shutdown() {
2362
 
2363
+ //Put last error to log if one
2364
+ $lasterror = error_get_last();
2365
+ if ( $lasterror['type'] === E_ERROR || $lasterror['type'] === E_PARSE || $lasterror['type'] === E_CORE_ERROR || $lasterror['type'] === E_CORE_WARNING || $lasterror['type'] === E_COMPILE_ERROR || $lasterror['type'] === E_COMPILE_WARNING ) {
2366
+ $this->log( $lasterror['type'], $lasterror['message'], $lasterror['file'], $lasterror['line'] );
2367
+ }
 
 
 
 
 
 
 
 
 
 
 
 
2368
 
2369
+ $error = false;
2370
+ if ( function_exists( 'pcntl_get_last_error' ) ) {
2371
+ $error = pcntl_get_last_error();
2372
+ if ( ! empty( $error ) ) {
2373
+ $error_msg = pcntl_strerror( $error );
2374
+ if ( ! empty( $error_msg ) ) {
2375
+ $error = '(' . $error . ') ' . $error_msg;
2376
  }
 
2377
  }
2378
+ if ( ! empty( $error ) ) {
2379
+ $this->log( sprintf( __( 'System: %s', 'backwpup' ), $error ), E_USER_ERROR );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2380
  }
 
 
 
 
 
 
 
 
2381
  }
2382
 
2383
+ if ( function_exists( 'posix_get_last_error' ) && ! $error ) {
2384
+ $error = posix_get_last_error();
2385
+ if ( ! empty( $error ) ) {
2386
+ $error_msg = posix_strerror( $error );
2387
+ if ( ! empty( $error_msg ) ) {
2388
+ $error = '(' . $error . ') ' . $error_msg;
2389
+ }
2390
+ }
2391
+ if ( ! empty( $error ) ) {
2392
+ $this->log( sprintf( __( 'System: %s', 'backwpup' ), $error ), E_USER_ERROR );
2393
+ }
2394
  }
2395
 
2396
+ $this->do_restart( true );
2397
+ }
 
 
 
 
2398
 
2399
+ /**
2400
+ *
2401
+ * The uncouth exception handler
2402
+ *
2403
+ * @param object $exception
2404
+ */
2405
+ public function exception_handler( $exception ) {
2406
 
2407
+ $this->log( sprintf( __( 'Exception caught in %1$s: %2$s', 'backwpup' ), get_class( $exception ), $exception->getMessage() ), E_USER_ERROR, $exception->getFile(), $exception->getLine() );
2408
  }
2409
 
2410
  /**
2411
+ *
2412
+ * Callback for the CURLOPT_READFUNCTION that submit the transferred bytes
2413
+ * to build the process bar
2414
+ *
2415
+ * @param $curl_handle
2416
+ * @param $file_handle
2417
+ * @param $read_count
2418
  *
2419
  * @return string
2420
+ * @internal param $out
2421
  */
2422
+ public function curl_read_callback( $curl_handle, $file_handle, $read_count ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2423
 
2424
+ $data = null;
2425
+ if ( ! empty( $file_handle ) && is_numeric( $read_count ) ) {
2426
+ $data = fread( $file_handle, $read_count );
2427
  }
2428
 
2429
+ if ( $this->job['backuptype'] == 'sync' ) {
2430
+ return $data;
 
 
2431
  }
2432
 
2433
+ $length = ( is_numeric( $read_count ) ) ? $read_count : strlen( $read_count );
2434
+ $this->substeps_done = $this->substeps_done + $length;
2435
+ $this->update_working_data();
2436
+
2437
+ return $data;
2438
  }
2439
 
2440
  /**
2493
  return false;
2494
  }
2495
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2496
  /**
2497
  * For storing and getting data in/from a extra temp file
2498
  *
2521
  return $data;
2522
  }
2523
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2524
  /**
2525
  * Add a Folders to Folder list that should be backup
2526
  *
2549
  }
2550
  }
2551
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2552
  }
inc/class-page-about.php CHANGED
@@ -391,25 +391,6 @@ _e( 'Use your backup archives to save your entire WordPress installation includi
391
  <?php } ?>
392
  <div>
393
  </div>
394
- <?php
395
- if ( class_exists( 'BackWPup_Pro', FALSE ) ) :
396
- $autoupdate = BackWPup_Pro_MarketPress_Autoupdate::get_instance();
397
- if ( $autoupdate->license_check() == 'false' ) :
398
- $plugins = get_plugins();
399
- $localplugin = FALSE;
400
- foreach ( $plugins as $plugin ) {
401
- if ( BackWPup::get_plugin_data( 'Name' ) == $plugin[ 'Name' ] )
402
- $localplugin = TRUE;
403
- }
404
- ?>
405
- <div class="welcometxt">
406
- <div class="backwpup-welcome">
407
- <h3><?php esc_html_e( 'Please activate your license', 'backwpup' ); ?></h3>
408
- <p><a href="<?php echo $localplugin ? esc_attr(admin_url( 'plugins.php' )) : esc_attr(network_admin_url( 'plugins.php' )); ?>"><?php esc_html_e( 'Please go to your plugin page and active the license to have the autoupdates enabled.', 'backwpup' ); ?></a></p>
409
- </div>
410
- </div>
411
- <?php endif; ?>
412
- <?php endif; ?>
413
  <div class="features">
414
 
415
  <div class="feature-box <?php self::feature_class(); ?>">
391
  <?php } ?>
392
  <div>
393
  </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
  <div class="features">
395
 
396
  <div class="feature-box <?php self::feature_class(); ?>">
inc/class-page-editjob.php CHANGED
@@ -303,92 +303,6 @@ class BackWPup_Page_Editjob {
303
  }
304
  }
305
 
306
- /**
307
- * @static
308
- *
309
- * @param string $args
310
- *
311
- * @return mixed
312
- */
313
- public static function ajax_cron_text( $args = '' ) {
314
-
315
- if ( is_array( $args ) ) {
316
- extract( $args );
317
- $ajax = FALSE;
318
- } else {
319
- if ( ! current_user_can( 'backwpup_jobs_edit' ) )
320
- wp_die( -1 );
321
- check_ajax_referer( 'backwpup_ajax_nonce' );
322
- if ( empty( $_POST[ 'cronminutes' ] ) || $_POST[ 'cronminutes' ][ 0 ] == '*' ) {
323
- if ( ! empty( $_POST[ 'cronminutes' ][ 1 ] ) )
324
- $_POST[ 'cronminutes' ] = array( '*/' . $_POST[ 'cronminutes' ][ 1 ] );
325
- else
326
- $_POST[ 'cronminutes' ] = array( '*' );
327
- }
328
- if ( empty( $_POST[ 'cronhours' ] ) || $_POST[ 'cronhours' ][ 0 ] == '*' ) {
329
- if ( ! empty( $_POST[ 'cronhours' ][ 1 ] ) )
330
- $_POST[ 'cronhours' ] = array( '*/' . $_POST[ 'cronhours' ][ 1 ] );
331
- else
332
- $_POST[ 'cronhours' ] = array( '*' );
333
- }
334
- if ( empty( $_POST[ 'cronmday' ] ) || $_POST[ 'cronmday' ][ 0 ] == '*' ) {
335
- if ( ! empty( $_POST[ 'cronmday' ][ 1 ] ) )
336
- $_POST[ 'cronmday' ] = array( '*/' . $_POST[ 'cronmday' ][ 1 ] );
337
- else
338
- $_POST[ 'cronmday' ] = array( '*' );
339
- }
340
- if ( empty( $_POST[ 'cronmon' ] ) || $_POST[ 'cronmon' ][ 0 ] == '*' ) {
341
- if ( ! empty( $_POST[ 'cronmon' ][ 1 ] ) )
342
- $_POST[ 'cronmon' ] = array( '*/' . $_POST[ 'cronmon' ][ 1 ] );
343
- else
344
- $_POST[ 'cronmon' ] = array( '*' );
345
- }
346
- if ( empty( $_POST[ 'cronwday' ] ) || $_POST[ 'cronwday' ][ 0 ] == '*' ) {
347
- if ( ! empty( $_POST[ 'cronwday' ][ 1 ] ) )
348
- $_POST[ 'cronwday' ] = array( '*/' . $_POST[ 'cronwday' ][ 1 ] );
349
- else
350
- $_POST[ 'cronwday' ] = array( '*' );
351
- }
352
- $crontype = $_POST[ 'crontype' ];
353
- $cronstamp = implode( ",", $_POST[ 'cronminutes' ] ) . ' ' . implode( ",", $_POST[ 'cronhours' ] ) . ' ' . implode( ",", $_POST[ 'cronmday' ] ) . ' ' . implode( ",", $_POST[ 'cronmon' ] ) . ' ' . implode( ",", $_POST[ 'cronwday' ] );
354
- $ajax = TRUE;
355
- }
356
- echo '<p class="wpcron" id="schedulecron">';
357
-
358
- if ( $crontype == 'advanced' ) {
359
- echo str_replace( '\"','"', __( 'Working as <a href="http://wikipedia.org/wiki/Cron">Cron</a> schedule:', 'backwpup' ) );
360
- echo ' <i><b>' . esc_attr( $cronstamp ). '</b></i><br />';
361
- }
362
-
363
- list( $cronstr[ 'minutes' ], $cronstr[ 'hours' ], $cronstr[ 'mday' ], $cronstr[ 'mon' ], $cronstr[ 'wday' ] ) = explode( ' ', $cronstamp, 5 );
364
- if ( FALSE !== strpos( $cronstr[ 'minutes' ], '*/' ) || $cronstr[ 'minutes' ] == '*' ) {
365
- $repeatmins = str_replace( '*/', '', $cronstr[ 'minutes' ] );
366
- if ( $repeatmins == '*' || empty( $repeatmins ) )
367
- $repeatmins = 5;
368
- echo '<span style="color:red;">' . sprintf( __( 'ATTENTION: Job runs every %d minutes!', 'backwpup' ), $repeatmins ) . '</span><br />';
369
- }
370
- if ( FALSE !== strpos( $cronstr[ 'hours' ], '*/' ) || $cronstr[ 'hours' ] == '*' ) {
371
- $repeathouer = str_replace( '*/', '', $cronstr[ 'hours' ] );
372
- if ( $repeathouer == '*' || empty( $repeathouer ) )
373
- $repeathouer = 1;
374
- echo '<span style="color:red;">' . sprintf( __( 'ATTENTION: Job runs every %d hours!', 'backwpup' ), $repeathouer ) . '</span><br />';
375
- }
376
- $cron_next = BackWPup_Cron::cron_next( $cronstamp ) + ( get_option( 'gmt_offset' ) * 3600 );
377
- if ( PHP_INT_MAX === $cron_next ) {
378
- echo '<span style="color:red;">' . __( 'ATTENTION: Can\'t calculate cron!', 'backwpup' ) . '</span><br />';
379
- }
380
- else {
381
- _e( 'Next runtime:', 'backwpup' );
382
- echo ' <b>' . date_i18n( 'D, j M Y, H:i', $cron_next, TRUE ) . '</b>';
383
- }
384
- echo "</p>";
385
-
386
- if ( $ajax )
387
- die();
388
- else
389
- return;
390
- }
391
-
392
  /**
393
  *
394
  */
@@ -935,5 +849,85 @@ class BackWPup_Page_Editjob {
935
  }
936
 
937
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
938
  }
939
 
303
  }
304
  }
305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
  /**
307
  *
308
  */
849
  }
850
 
851
  }
852
+
853
+ /**
854
+ * @static
855
+ *
856
+ * @param string $args
857
+ *
858
+ * @return mixed
859
+ */
860
+ public static function ajax_cron_text( $args = '' ) {
861
+
862
+ if ( is_array( $args ) ) {
863
+ extract( $args );
864
+ $ajax = FALSE;
865
+ } else {
866
+ if ( ! current_user_can( 'backwpup_jobs_edit' ) )
867
+ wp_die( -1 );
868
+ check_ajax_referer( 'backwpup_ajax_nonce' );
869
+ if ( empty( $_POST[ 'cronminutes' ] ) || $_POST[ 'cronminutes' ][ 0 ] == '*' ) {
870
+ if ( ! empty( $_POST[ 'cronminutes' ][ 1 ] ) )
871
+ $_POST[ 'cronminutes' ] = array( '*/' . $_POST[ 'cronminutes' ][ 1 ] );
872
+ else
873
+ $_POST[ 'cronminutes' ] = array( '*' );
874
+ }
875
+ if ( empty( $_POST[ 'cronhours' ] ) || $_POST[ 'cronhours' ][ 0 ] == '*' ) {
876
+ if ( ! empty( $_POST[ 'cronhours' ][ 1 ] ) )
877
+ $_POST[ 'cronhours' ] = array( '*/' . $_POST[ 'cronhours' ][ 1 ] );
878
+ else
879
+ $_POST[ 'cronhours' ] = array( '*' );
880
+ }
881
+ if ( empty( $_POST[ 'cronmday' ] ) || $_POST[ 'cronmday' ][ 0 ] == '*' ) {
882
+ if ( ! empty( $_POST[ 'cronmday' ][ 1 ] ) )
883
+ $_POST[ 'cronmday' ] = array( '*/' . $_POST[ 'cronmday' ][ 1 ] );
884
+ else
885
+ $_POST[ 'cronmday' ] = array( '*' );
886
+ }
887
+ if ( empty( $_POST[ 'cronmon' ] ) || $_POST[ 'cronmon' ][ 0 ] == '*' ) {
888
+ if ( ! empty( $_POST[ 'cronmon' ][ 1 ] ) )
889
+ $_POST[ 'cronmon' ] = array( '*/' . $_POST[ 'cronmon' ][ 1 ] );
890
+ else
891
+ $_POST[ 'cronmon' ] = array( '*' );
892
+ }
893
+ if ( empty( $_POST[ 'cronwday' ] ) || $_POST[ 'cronwday' ][ 0 ] == '*' ) {
894
+ if ( ! empty( $_POST[ 'cronwday' ][ 1 ] ) )
895
+ $_POST[ 'cronwday' ] = array( '*/' . $_POST[ 'cronwday' ][ 1 ] );
896
+ else
897
+ $_POST[ 'cronwday' ] = array( '*' );
898
+ }
899
+ $crontype = $_POST[ 'crontype' ];
900
+ $cronstamp = implode( ",", $_POST[ 'cronminutes' ] ) . ' ' . implode( ",", $_POST[ 'cronhours' ] ) . ' ' . implode( ",", $_POST[ 'cronmday' ] ) . ' ' . implode( ",", $_POST[ 'cronmon' ] ) . ' ' . implode( ",", $_POST[ 'cronwday' ] );
901
+ $ajax = TRUE;
902
+ }
903
+ echo '<p class="wpcron" id="schedulecron">';
904
+
905
+ if ( $crontype == 'advanced' ) {
906
+ echo str_replace( '\"','"', __( 'Working as <a href="http://wikipedia.org/wiki/Cron">Cron</a> schedule:', 'backwpup' ) );
907
+ echo ' <i><b>' . esc_attr( $cronstamp ). '</b></i><br />';
908
+ }
909
+
910
+ list( $cronstr[ 'minutes' ], $cronstr[ 'hours' ], $cronstr[ 'mday' ], $cronstr[ 'mon' ], $cronstr[ 'wday' ] ) = explode( ' ', $cronstamp, 5 );
911
+ if ( FALSE !== strpos( $cronstr[ 'minutes' ], '*/' ) || $cronstr[ 'minutes' ] == '*' ) {
912
+ $repeatmins = str_replace( '*/', '', $cronstr[ 'minutes' ] );
913
+ if ( $repeatmins == '*' || empty( $repeatmins ) )
914
+ $repeatmins = 5;
915
+ echo '<span style="color:red;">' . sprintf( __( 'ATTENTION: Job runs every %d minutes!', 'backwpup' ), $repeatmins ) . '</span><br />';
916
+ }
917
+ $cron_next = BackWPup_Cron::cron_next( $cronstamp ) + ( get_option( 'gmt_offset' ) * 3600 );
918
+ if ( PHP_INT_MAX === $cron_next ) {
919
+ echo '<span style="color:red;">' . __( 'ATTENTION: Can\'t calculate cron!', 'backwpup' ) . '</span><br />';
920
+ }
921
+ else {
922
+ _e( 'Next runtime:', 'backwpup' );
923
+ echo ' <b>' . date_i18n( 'D, j M Y, H:i', $cron_next, TRUE ) . '</b>';
924
+ }
925
+ echo "</p>";
926
+
927
+ if ( $ajax )
928
+ die();
929
+ else
930
+ return;
931
+ }
932
  }
933
 
inc/class-page-settings.php CHANGED
@@ -448,8 +448,6 @@ class BackWPup_Page_Settings {
448
  $php_user = get_current_user();
449
  }
450
  echo '<tr title=""><td>' . __( 'Current PHP user', 'backwpup' ) . '</td><td>' . esc_html( $php_user ) . '</td></tr>';
451
- $text = (bool) ini_get( 'safe_mode' ) ? __( 'On', 'backwpup' ) : __( 'Off', 'backwpup' );
452
- echo '<tr title=""><td>' . __( 'Safe Mode', 'backwpup' ) . '</td><td>' . $text . '</td></tr>';
453
  echo '<tr title="&gt;=30"><td>' . __( 'Maximum execution time', 'backwpup' ) . '</td><td>' . esc_html( ini_get( 'max_execution_time' ) ) . ' ' . __( 'seconds', 'backwpup' ) . '</td></tr>';
454
  if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON )
455
  echo '<tr title="ALTERNATE_WP_CRON"><td>' . __( 'Alternative WP Cron', 'backwpup' ) . '</td><td>' . __( 'On', 'backwpup' ) . '</td></tr>';
448
  $php_user = get_current_user();
449
  }
450
  echo '<tr title=""><td>' . __( 'Current PHP user', 'backwpup' ) . '</td><td>' . esc_html( $php_user ) . '</td></tr>';
 
 
451
  echo '<tr title="&gt;=30"><td>' . __( 'Maximum execution time', 'backwpup' ) . '</td><td>' . esc_html( ini_get( 'max_execution_time' ) ) . ' ' . __( 'seconds', 'backwpup' ) . '</td></tr>';
452
  if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON )
453
  echo '<tr title="ALTERNATE_WP_CRON"><td>' . __( 'Alternative WP Cron', 'backwpup' ) . '</td><td>' . __( 'On', 'backwpup' ) . '</td></tr>';
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
  === BackWPup - WordPress Backup Plugin ===
2
  Contributors: inpsyde, danielhuesken, Bueltge, nullbyte
3
  Tags: Amazon, Amazon S3, back up, backup, chinese, cloud, cloud files, database, db backup, dropbox, dump, file, french, ftp, ftps, german, migrate, multisite, russian, schedule, sftp, storage, S3, time, upload, xml
4
- Requires at least: 3.8
5
- Tested up to: 4.5.2
6
- Stable tag: 3.3.3
7
  License: GPLv3
8
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
9
 
@@ -35,7 +35,7 @@ The **backup plugin** **[BackWPup](http://backwpup.com/)** can be used to save y
35
  * Pro version and support available - [BackWPup Pro](http://backwpup.com)
36
 
37
  = Requirements =
38
- * WordPress 3.8 and PHP 5.2.7 required!
39
  * To use the Plugin with full functionality PHP 5.3.3 with mysqli, FTP,gz, bz2, ZipArchive and curl is needed.
40
  * Plugin functions that don't work because of your server settings, will not be displayed in admin area.
41
 
@@ -157,6 +157,13 @@ Yes. You need to have writing access to the wp-config.php file (usually residing
157
  [You can find a detailed tutorial in the BackWPup documentation.](http://docs.backwpup.com/article/118-install-backwpup)
158
 
159
  == Changelog ==
 
 
 
 
 
 
 
160
  = Version 3.3.3 =
161
  * Removed admin notices
162
 
1
  === BackWPup - WordPress Backup Plugin ===
2
  Contributors: inpsyde, danielhuesken, Bueltge, nullbyte
3
  Tags: Amazon, Amazon S3, back up, backup, chinese, cloud, cloud files, database, db backup, dropbox, dump, file, french, ftp, ftps, german, migrate, multisite, russian, schedule, sftp, storage, S3, time, upload, xml
4
+ Requires at least: 3.9
5
+ Tested up to: 4.7.1
6
+ Stable tag: 3.3.4
7
  License: GPLv3
8
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
9
 
35
  * Pro version and support available - [BackWPup Pro](http://backwpup.com)
36
 
37
  = Requirements =
38
+ * WordPress 3.9 and PHP 5.2.7 required!
39
  * To use the Plugin with full functionality PHP 5.3.3 with mysqli, FTP,gz, bz2, ZipArchive and curl is needed.
40
  * Plugin functions that don't work because of your server settings, will not be displayed in admin area.
41
 
157
  [You can find a detailed tutorial in the BackWPup documentation.](http://docs.backwpup.com/article/118-install-backwpup)
158
 
159
  == Changelog ==
160
+ = Version 3.3.4 =
161
+ * Fixed: Database gone away messages
162
+ * Fixed: restarts in cli mode
163
+ * Added: AWS S3 Region Asia Pacific (Mumbai)
164
+ * PRO: fix false email sender address in job creation wizard
165
+ * PRO: fix gdrive ssl problem on uploads
166
+
167
  = Version 3.3.3 =
168
  * Removed admin notices
169