GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch dev (7ec9be)
by Liuta
01:54
created
includes/class-xcloner-file-system.php 1 patch
Indentation   +975 added lines, -975 removed lines patch added patch discarded remove patch
@@ -37,857 +37,857 @@  discard block
 block discarded – undo
37 37
 class Xcloner_File_System
38 38
 {
39 39
 
40
-    private $excluded_files = "";
41
-    private $additional_regex_patterns = array();
42
-    private $excluded_files_by_default = array("administrator/backups", "wp-content/backups");
43
-    private $included_files_handler = "backup_files.csv";
44
-    private $temp_dir_handler = ".dir";
45
-    public $filesystem;
46
-    public $tmp_filesystem;
47
-    public $storage_filesystem;
48
-    private $xcloner_container;
49
-    private $diff_timestamp_start = "";
50
-
51
-    private $logger;
52
-    private $start_adapter;
53
-    private $tmp_adapter;
54
-    private $storage_adapter;
55
-    private $xcloner_settings;
56
-    private $start_filesystem;
57
-    private $tmp_filesystem_append;
58
-    private $storage_filesystem_append;
59
-
60
-    private $files_counter;
61
-    private $files_size;
62
-    private $last_logged_file;
63
-    private $folders_to_process_per_session = 25;
64
-    private $backup_archive_extensions = array("tar", "tgz", "tar.gz", "gz", "csv", "encrypted", "decrypted");
65
-    private $backup_name_tags = array('[time]', '[hostname]', '[domain]');
66
-
67
-    /**
68
-     * Xcloner_File_System constructor.
69
-     * @param Xcloner $xcloner_container
70
-     * @param string $hash
71
-     */
72
-    public function __construct(Xcloner $xcloner_container, $hash = "")
73
-    {
74
-        $this->xcloner_container = $xcloner_container;
75
-
76
-        $this->logger = $xcloner_container->get_xcloner_logger()->withName("xcloner_file_system");
77
-        $this->xcloner_settings = $xcloner_container->get_xcloner_settings();
78
-
79
-        try {
80
-
81
-            $this->start_adapter = new Local($this->xcloner_settings->get_xcloner_start_path(), LOCK_EX, 'SKIP_LINKS');
82
-            $this->start_filesystem = new Filesystem($this->start_adapter, new Config([
83
-                'disable_asserts' => true,
84
-            ]));
85
-
86
-            $this->tmp_adapter = new Local($this->xcloner_settings->get_xcloner_tmp_path(), LOCK_EX, 'SKIP_LINKS');
87
-            $this->tmp_filesystem = new Filesystem($this->tmp_adapter, new Config([
88
-                'disable_asserts' => true,
89
-            ]));
90
-            $adapter = new Local($this->xcloner_settings->get_xcloner_tmp_path(), LOCK_EX | FILE_APPEND, 'SKIP_LINKS');
91
-            $this->tmp_filesystem_append = new Filesystem($adapter, new Config([
92
-                'disable_asserts' => true,
93
-            ]));
94
-
95
-            $adapter = new Local($this->xcloner_settings->get_xcloner_store_path(), LOCK_EX, 'SKIP_LINKS');
96
-            $this->storage_filesystem = new Filesystem($adapter, new Config([
97
-                'disable_asserts' => true,
98
-            ]));
99
-
100
-            $this->storage_adapter = new Local($this->xcloner_settings->get_xcloner_store_path(), FILE_APPEND,
101
-                'SKIP_LINKS');
102
-            $this->storage_filesystem_append = new Filesystem($this->storage_adapter, new Config([
103
-                'disable_asserts' => true,
104
-            ]));
105
-        }catch (Exception $e) {
106
-            $this->logger->error("Filesystem Initialization Error: ".$e->getMessage());
107
-        }
108
-
109
-
110
-        if ($value = get_option('xcloner_directories_to_scan_per_request')) {
111
-            $this->folders_to_process_per_session = $value;
112
-        }
113
-
114
-    }
115
-
116
-    /**
117
-     * Set differential timestamp date
118
-     * @param string $timestamp
119
-     */
120
-    public function set_diff_timestamp_start($timestamp = "")
121
-    {
122
-        if ($timestamp) {
123
-            $this->logger->info(sprintf("Setting Differential Timestamp To %s", date("Y-m-d", $timestamp)), array(
124
-                "FILESYSTEM",
125
-                "DIFF"
126
-            ));
127
-            $this->diff_timestamp_start = $timestamp;
128
-        }
129
-    }
130
-
131
-    /**
132
-     * Gets the differential timestamp date
133
-     * @return string
134
-     */
135
-    public function get_diff_timestamp_start()
136
-    {
137
-        return $this->diff_timestamp_start;
138
-    }
139
-
140
-    private function get_xcloner_container()
141
-    {
142
-        return $this->xcloner_container;
143
-    }
144
-
145
-    public function set_hash($hash)
146
-    {
147
-        $this->xcloner_settings->set_hash($hash);
148
-    }
149
-
150
-    public function get_hash($hash)
151
-    {
152
-        $this->xcloner_settings->get_hash();
153
-    }
154
-
155
-    public function get_tmp_filesystem()
156
-    {
157
-        return $this->tmp_filesystem;
158
-    }
159
-
160
-    public function get_storage_filesystem($remote_storage_selection = "")
161
-    {
162
-        if ($remote_storage_selection != "") {
163
-            $remote_storage = $this->get_xcloner_container()->get_xcloner_remote_storage();
164
-            $method = "get_".$remote_storage_selection."_filesystem";
165
-
166
-            if (!method_exists($remote_storage, $method)) {
167
-                return false;
168
-            }
169
-
170
-            list($adapter, $filesystem) = $remote_storage->$method();
171
-
172
-            return $filesystem;
173
-        }
174
-
175
-        return $this->storage_filesystem;
176
-    }
177
-
178
-    public function get_tmp_filesystem_adapter()
179
-    {
180
-        return $this->tmp_adapter;
181
-    }
182
-
183
-    public function get_tmp_filesystem_append()
184
-    {
185
-        return $this->tmp_filesystem_append;
186
-    }
187
-
188
-    public function get_start_adapter()
189
-    {
190
-        return $this->start_adapter;
191
-    }
192
-
193
-    public function get_start_filesystem()
194
-    {
195
-        return $this->start_filesystem;
196
-    }
197
-
198
-    public function get_logger()
199
-    {
200
-        return $this->logger;
201
-    }
202
-
203
-    public function get_start_path_file_info($file)
204
-    {
205
-        $info = $this->getMetadataFull('start_adapter', $file);
206
-
207
-        return $this->start_filesystem->normalizeFileInfo($info);
208
-    }
209
-
210
-    /**
211
-     * @param string $file
212
-     */
213
-    public function get_storage_path_file_info($file)
214
-    {
215
-        return $this->getMetadataFull('storage_adapter', $file);
216
-    }
217
-
218
-    public function get_included_files_handler($metadata = 0)
219
-    {
220
-        $path = $this->included_files_handler;
221
-        if (!$metadata) {
222
-            return $path;
223
-        }
224
-
225
-        $spl_info = $this->getMetadataFull('tmp_adapter', $path);
226
-
227
-        return $spl_info;
228
-
229
-    }
230
-
231
-    public function get_temp_dir_handler()
232
-    {
233
-        return $this->temp_dir_handler;
234
-    }
235
-
236
-    public function get_latest_backup()
237
-    {
238
-        $files = $this->get_backup_archives_list();
239
-
240
-        if (is_array($files)) {
241
-            $this->sort_by($files, "timestamp", "desc");
242
-        }
243
-
244
-        $new_list = array();
245
-
246
-        foreach ($files as $key => $file) {
247
-            if (!isset($file['parent'])) {
248
-                $new_list[] = ($files[$key]);
249
-            }
250
-        }
251
-
252
-        if (isset($new_list[0])) {
253
-            return $new_list[0];
254
-        }
255
-    }
256
-
257
-    public function get_latest_backups()
258
-    {
259
-        $files = $this->get_backup_archives_list();
260
-
261
-        if (is_array($files)) {
262
-            $this->sort_by($files, "timestamp", "desc");
263
-        }
264
-
265
-        $new_list = array();
266
-
267
-        foreach ($files as $key => $file) {
268
-            if (!isset($file['parent'])) {
269
-                $new_list[] = ($files[$key]);
270
-            }
271
-        }
272
-
273
-        return $new_list;
274
-    }
275
-
276
-    public function get_storage_usage()
277
-    {
278
-        $files = $this->get_backup_archives_list();
279
-        $total = 0;
280
-
281
-        if (is_array($files)) {
282
-            foreach ($files as $file) {
283
-                $total += $file['size'];
284
-            }
285
-        }
286
-
287
-        return $total;
288
-    }
289
-
290
-    public function is_part($backup_name)
291
-    {
292
-        if (stristr($backup_name, "-part")) {
293
-            return true;
294
-        }
295
-
296
-        return false;
297
-    }
298
-
299
-    public function is_multipart($backup_name)
300
-    {
301
-        if (stristr($backup_name, "-multipart")) {
302
-            return true;
303
-        }
304
-
305
-        return false;
306
-    }
307
-
308
-    public function get_backup_size($backup_name)
309
-    {
310
-        $backup_size = $this->get_storage_filesystem()->getSize($backup_name);
311
-        if ($this->is_multipart($backup_name)) {
312
-            $backup_parts = $this->get_multipart_files($backup_name);
313
-            foreach ($backup_parts as $part_file) {
314
-                $backup_size += $this->get_storage_filesystem()->getSize($part_file);
315
-            }
316
-        }
317
-
318
-        return $backup_size;
319
-    }
320
-
321
-    public function get_multipart_files($backup_name, $storage_selection = "")
322
-    {
323
-        $files = array();
324
-
325
-        if ($this->is_multipart($backup_name)) {
326
-            $lines = explode(PHP_EOL, $this->get_storage_filesystem($storage_selection)->read($backup_name));
327
-            foreach ($lines as $line) {
328
-                if ($line) {
329
-                    $data = str_getcsv($line);
330
-                    $files[] = $data[0];
331
-                }
332
-            }
333
-        }
334
-
335
-        return $files;
336
-    }
337
-
338
-    public function delete_backup_by_name($backup_name, $storage_selection = "")
339
-    {
340
-        if ($this->is_multipart($backup_name)) {
341
-            $lines = explode(PHP_EOL, $this->get_storage_filesystem($storage_selection)->read($backup_name));
342
-            foreach ($lines as $line) {
343
-                if ($line) {
344
-                    $data = str_getcsv($line);
345
-                    $this->get_storage_filesystem($storage_selection)->delete($data[0]);
346
-                }
347
-            }
348
-        }
349
-
350
-        if ($this->get_storage_filesystem($storage_selection)->delete($backup_name)) {
351
-            $return = true;
352
-        } else {
353
-            $return = false;
354
-        }
355
-
356
-        return $return;
357
-    }
358
-
359
-    public function getMetadataFull($adapter = "storage_adapter", $path)
360
-    {
361
-        $location = $this->$adapter->applyPathPrefix($path);
362
-        $spl_info = new SplFileInfo($location);
363
-
364
-        return ($spl_info);
365
-    }
366
-
367
-
368
-    public function get_backup_archives_list($storage_selection = "")
369
-    {
370
-        $list = array();
371
-
372
-
373
-        if (method_exists($this->get_storage_filesystem($storage_selection), "listContents")) {
374
-            $list = $this->get_storage_filesystem($storage_selection)->listContents();
375
-        }
376
-
377
-        $backup_files = array();
378
-        $parents = array();
379
-
380
-        foreach ($list as $file_info) {
381
-            if (isset($file_info['extension']) and $file_info['extension'] == "csv") {
382
-                $data = array();
383
-
384
-                $lines = explode(PHP_EOL, $this->get_storage_filesystem($storage_selection)->read($file_info['path']));
385
-                foreach ($lines as $line) {
386
-                    if ($line) {
387
-                        $data = str_getcsv($line);
388
-                        if (is_array($data)) {
389
-                            $parents[$data[0]] = $file_info['basename'];
390
-                            $file_info['childs'][] = $data;
391
-                            $file_info['size'] += $data[2];
392
-                        }
393
-                    }
394
-                }
395
-
396
-            }
397
-
398
-            if ($file_info['type'] == 'file' and isset($file_info['extension']) and in_array($file_info['extension'],
399
-                    $this->backup_archive_extensions)) {
400
-                $backup_files[$file_info['path']] = $file_info;
401
-            }
402
-        }
403
-
404
-        foreach ($backup_files as $key => $file_info) {
405
-            if (!isset($backup_files[$key]['timestamp'])) {
406
-                //$backup_files[$key]['timestamp'] = $this->get_storage_filesystem($storage_selection)->getTimestamp($file_info['path']);
407
-            }
408
-
409
-            if (isset($parents[$file_info['basename']])) {
410
-                $backup_files[$key]['parent'] = $parents[$file_info['basename']];
411
-            }
412
-        }
413
-
414
-        return $backup_files;
415
-    }
416
-
417
-    public function start_file_recursion($init = 0)
418
-    {
419
-        if ($init) {
420
-            $this->logger->info(sprintf(__("Starting the filesystem scanner on root folder %s"),
421
-                $this->xcloner_settings->get_xcloner_start_path()));
422
-            $this->do_system_init();
423
-        }
424
-
425
-        if ($this->tmp_filesystem->has($this->get_temp_dir_handler())) {
426
-            //.dir exists, we presume we have files to iterate
427
-            $content = $this->tmp_filesystem->read($this->get_temp_dir_handler());
428
-            $files = array_filter(explode("\n", $content));
429
-            $this->tmp_filesystem->delete($this->get_temp_dir_handler());
430
-
431
-            $counter = 0;
432
-            foreach ($files as $file) {
433
-                if ($counter < $this->folders_to_process_per_session) {
434
-                    $this->build_files_list($file);
435
-                    $counter++;
436
-                } else {
437
-                    $this->tmp_filesystem_append->write($this->get_temp_dir_handler(), $file."\n");
438
-                }
439
-            }
440
-        } else {
441
-            $this->build_files_list();
442
-        }
443
-
444
-        if ($this->scan_finished()) {
445
-            $metadata_dumpfile = $this->get_tmp_filesystem()->getMetadata("index.html");
446
-            $this->store_file($metadata_dumpfile, 'tmp_filesystem');
447
-            $this->files_counter++;
448
-
449
-            //adding included dump file to the included files list
450
-            if ($this->get_tmp_filesystem()->has($this->get_included_files_handler())) {
451
-                $metadata_dumpfile = $this->get_tmp_filesystem()->getMetadata($this->get_included_files_handler());
452
-                $this->store_file($metadata_dumpfile, 'tmp_filesystem');
453
-                $this->files_counter++;
454
-            }
455
-
456
-            //adding a default index.html to the temp xcloner folder
457
-            if (!$this->get_tmp_filesystem()->has("index.html")) {
458
-                $this->get_tmp_filesystem()->write("index.html", "");
459
-            }
460
-
461
-            //adding the default log file
462
-            if ($this->get_tmp_filesystem()->has($this->xcloner_settings->get_logger_filename(1))) {
463
-                $metadata_dumpfile = $this->get_tmp_filesystem()->getMetadata($this->xcloner_settings->get_logger_filename(1));
464
-                $this->store_file($metadata_dumpfile, 'tmp_filesystem');
465
-                $this->files_counter++;
466
-            }
467
-
468
-            return false;
469
-        }
470
-
471
-        return true;
472
-    }
473
-
474
-    public function get_backup_attachments()
475
-    {
476
-        $return = array();
477
-
478
-        $files_list_file = $this->xcloner_settings->get_xcloner_tmp_path().DS.$this->get_included_files_handler();
479
-        if (file_exists($files_list_file)) {
480
-            $return[] = $files_list_file;
481
-        }
482
-
483
-        if ($this->xcloner_settings->get_xcloner_option('xcloner_enable_log')) {
484
-            $log_file = $this->xcloner_settings->get_xcloner_tmp_path().DS.$this->xcloner_settings->get_logger_filename(1);
485
-            if (!file_exists($log_file)) {
486
-                $log_file = $this->xcloner_settings->get_xcloner_store_path().DS.$this->xcloner_settings->get_logger_filename();
487
-            }
488
-
489
-            if (file_exists($log_file)) {
490
-                $return[] = $log_file;
491
-            }
492
-        }
493
-
494
-        return $return;
495
-    }
496
-
497
-    public function remove_tmp_filesystem()
498
-    {
499
-        //delete the temporary folder
500
-        $this->logger->debug(sprintf("Deleting the temporary storage folder %s",
501
-            $this->xcloner_settings->get_xcloner_tmp_path()));
502
-
503
-        $contents = $this->get_tmp_filesystem()->listContents();
504
-
505
-        if (is_array($contents)) {
506
-            foreach ($contents as $file_info) {
507
-                $this->get_tmp_filesystem()->delete($file_info['path']);
508
-            }
509
-        }
510
-
511
-        @rmdir($this->xcloner_settings->get_xcloner_tmp_path());
512
-
513
-        return;
514
-    }
515
-
516
-    public function cleanup_tmp_directories()
517
-    {
518
-        $adapter = new Local($this->xcloner_settings->get_xcloner_tmp_path(false), LOCK_EX | FILE_APPEND, 'SKIP_LINKS');
519
-        $tmp_filesystem = new Filesystem($adapter, new Config([
520
-            'disable_asserts' => true,
521
-        ]));
522
-
523
-        $contents = $tmp_filesystem->listContents();
524
-
525
-        foreach ($contents as $file) {
526
-
527
-            if (preg_match("/.xcloner-(.*)/", $file['path'])) {
528
-                if ($file['timestamp'] < strtotime("-1days")) {
529
-                    $tmp_filesystem->deleteDir($file['path']);
530
-                    $this->logger->debug(sprintf("Delete temporary directory %s", $file['path']));
531
-                }
532
-            }
533
-        }
534
-
535
-        return true;
536
-    }
537
-
538
-    private function do_system_init()
539
-    {
540
-        $this->files_counter = 0;
541
-
542
-        if (!$this->storage_filesystem->has("index.html")) {
543
-            $this->storage_filesystem->write("index.html", "");
544
-        }
545
-
546
-        if (!$this->tmp_filesystem->has("index.html")) {
547
-            $this->tmp_filesystem->write("index.html", "");
548
-        }
549
-
550
-        if ($this->tmp_filesystem->has($this->get_included_files_handler())) {
551
-            $this->tmp_filesystem->delete($this->get_included_files_handler());
552
-        }
553
-
554
-        if ($this->tmp_filesystem->has($this->get_temp_dir_handler())) {
555
-            $this->tmp_filesystem->delete($this->get_temp_dir_handler());
556
-        }
557
-    }
558
-
559
-    public function get_scanned_files_num()
560
-    {
561
-        return $this->files_counter;
562
-    }
563
-
564
-    public function get_scanned_files_total_size()
565
-    {
566
-        return $this->files_size;
567
-    }
568
-
569
-    public function last_logged_file()
570
-    {
571
-        return $this->last_logged_file;
572
-    }
573
-
574
-    public static function is_regex($regex)
575
-    {
576
-        return preg_match("/^\^(.*)\$$/i", $regex);
577
-    }
40
+	private $excluded_files = "";
41
+	private $additional_regex_patterns = array();
42
+	private $excluded_files_by_default = array("administrator/backups", "wp-content/backups");
43
+	private $included_files_handler = "backup_files.csv";
44
+	private $temp_dir_handler = ".dir";
45
+	public $filesystem;
46
+	public $tmp_filesystem;
47
+	public $storage_filesystem;
48
+	private $xcloner_container;
49
+	private $diff_timestamp_start = "";
50
+
51
+	private $logger;
52
+	private $start_adapter;
53
+	private $tmp_adapter;
54
+	private $storage_adapter;
55
+	private $xcloner_settings;
56
+	private $start_filesystem;
57
+	private $tmp_filesystem_append;
58
+	private $storage_filesystem_append;
59
+
60
+	private $files_counter;
61
+	private $files_size;
62
+	private $last_logged_file;
63
+	private $folders_to_process_per_session = 25;
64
+	private $backup_archive_extensions = array("tar", "tgz", "tar.gz", "gz", "csv", "encrypted", "decrypted");
65
+	private $backup_name_tags = array('[time]', '[hostname]', '[domain]');
66
+
67
+	/**
68
+	 * Xcloner_File_System constructor.
69
+	 * @param Xcloner $xcloner_container
70
+	 * @param string $hash
71
+	 */
72
+	public function __construct(Xcloner $xcloner_container, $hash = "")
73
+	{
74
+		$this->xcloner_container = $xcloner_container;
75
+
76
+		$this->logger = $xcloner_container->get_xcloner_logger()->withName("xcloner_file_system");
77
+		$this->xcloner_settings = $xcloner_container->get_xcloner_settings();
78
+
79
+		try {
80
+
81
+			$this->start_adapter = new Local($this->xcloner_settings->get_xcloner_start_path(), LOCK_EX, 'SKIP_LINKS');
82
+			$this->start_filesystem = new Filesystem($this->start_adapter, new Config([
83
+				'disable_asserts' => true,
84
+			]));
85
+
86
+			$this->tmp_adapter = new Local($this->xcloner_settings->get_xcloner_tmp_path(), LOCK_EX, 'SKIP_LINKS');
87
+			$this->tmp_filesystem = new Filesystem($this->tmp_adapter, new Config([
88
+				'disable_asserts' => true,
89
+			]));
90
+			$adapter = new Local($this->xcloner_settings->get_xcloner_tmp_path(), LOCK_EX | FILE_APPEND, 'SKIP_LINKS');
91
+			$this->tmp_filesystem_append = new Filesystem($adapter, new Config([
92
+				'disable_asserts' => true,
93
+			]));
94
+
95
+			$adapter = new Local($this->xcloner_settings->get_xcloner_store_path(), LOCK_EX, 'SKIP_LINKS');
96
+			$this->storage_filesystem = new Filesystem($adapter, new Config([
97
+				'disable_asserts' => true,
98
+			]));
99
+
100
+			$this->storage_adapter = new Local($this->xcloner_settings->get_xcloner_store_path(), FILE_APPEND,
101
+				'SKIP_LINKS');
102
+			$this->storage_filesystem_append = new Filesystem($this->storage_adapter, new Config([
103
+				'disable_asserts' => true,
104
+			]));
105
+		}catch (Exception $e) {
106
+			$this->logger->error("Filesystem Initialization Error: ".$e->getMessage());
107
+		}
108
+
109
+
110
+		if ($value = get_option('xcloner_directories_to_scan_per_request')) {
111
+			$this->folders_to_process_per_session = $value;
112
+		}
113
+
114
+	}
115
+
116
+	/**
117
+	 * Set differential timestamp date
118
+	 * @param string $timestamp
119
+	 */
120
+	public function set_diff_timestamp_start($timestamp = "")
121
+	{
122
+		if ($timestamp) {
123
+			$this->logger->info(sprintf("Setting Differential Timestamp To %s", date("Y-m-d", $timestamp)), array(
124
+				"FILESYSTEM",
125
+				"DIFF"
126
+			));
127
+			$this->diff_timestamp_start = $timestamp;
128
+		}
129
+	}
130
+
131
+	/**
132
+	 * Gets the differential timestamp date
133
+	 * @return string
134
+	 */
135
+	public function get_diff_timestamp_start()
136
+	{
137
+		return $this->diff_timestamp_start;
138
+	}
139
+
140
+	private function get_xcloner_container()
141
+	{
142
+		return $this->xcloner_container;
143
+	}
144
+
145
+	public function set_hash($hash)
146
+	{
147
+		$this->xcloner_settings->set_hash($hash);
148
+	}
149
+
150
+	public function get_hash($hash)
151
+	{
152
+		$this->xcloner_settings->get_hash();
153
+	}
154
+
155
+	public function get_tmp_filesystem()
156
+	{
157
+		return $this->tmp_filesystem;
158
+	}
159
+
160
+	public function get_storage_filesystem($remote_storage_selection = "")
161
+	{
162
+		if ($remote_storage_selection != "") {
163
+			$remote_storage = $this->get_xcloner_container()->get_xcloner_remote_storage();
164
+			$method = "get_".$remote_storage_selection."_filesystem";
165
+
166
+			if (!method_exists($remote_storage, $method)) {
167
+				return false;
168
+			}
169
+
170
+			list($adapter, $filesystem) = $remote_storage->$method();
171
+
172
+			return $filesystem;
173
+		}
174
+
175
+		return $this->storage_filesystem;
176
+	}
177
+
178
+	public function get_tmp_filesystem_adapter()
179
+	{
180
+		return $this->tmp_adapter;
181
+	}
182
+
183
+	public function get_tmp_filesystem_append()
184
+	{
185
+		return $this->tmp_filesystem_append;
186
+	}
187
+
188
+	public function get_start_adapter()
189
+	{
190
+		return $this->start_adapter;
191
+	}
192
+
193
+	public function get_start_filesystem()
194
+	{
195
+		return $this->start_filesystem;
196
+	}
197
+
198
+	public function get_logger()
199
+	{
200
+		return $this->logger;
201
+	}
202
+
203
+	public function get_start_path_file_info($file)
204
+	{
205
+		$info = $this->getMetadataFull('start_adapter', $file);
206
+
207
+		return $this->start_filesystem->normalizeFileInfo($info);
208
+	}
209
+
210
+	/**
211
+	 * @param string $file
212
+	 */
213
+	public function get_storage_path_file_info($file)
214
+	{
215
+		return $this->getMetadataFull('storage_adapter', $file);
216
+	}
217
+
218
+	public function get_included_files_handler($metadata = 0)
219
+	{
220
+		$path = $this->included_files_handler;
221
+		if (!$metadata) {
222
+			return $path;
223
+		}
224
+
225
+		$spl_info = $this->getMetadataFull('tmp_adapter', $path);
226
+
227
+		return $spl_info;
228
+
229
+	}
230
+
231
+	public function get_temp_dir_handler()
232
+	{
233
+		return $this->temp_dir_handler;
234
+	}
235
+
236
+	public function get_latest_backup()
237
+	{
238
+		$files = $this->get_backup_archives_list();
239
+
240
+		if (is_array($files)) {
241
+			$this->sort_by($files, "timestamp", "desc");
242
+		}
243
+
244
+		$new_list = array();
245
+
246
+		foreach ($files as $key => $file) {
247
+			if (!isset($file['parent'])) {
248
+				$new_list[] = ($files[$key]);
249
+			}
250
+		}
251
+
252
+		if (isset($new_list[0])) {
253
+			return $new_list[0];
254
+		}
255
+	}
256
+
257
+	public function get_latest_backups()
258
+	{
259
+		$files = $this->get_backup_archives_list();
260
+
261
+		if (is_array($files)) {
262
+			$this->sort_by($files, "timestamp", "desc");
263
+		}
264
+
265
+		$new_list = array();
266
+
267
+		foreach ($files as $key => $file) {
268
+			if (!isset($file['parent'])) {
269
+				$new_list[] = ($files[$key]);
270
+			}
271
+		}
272
+
273
+		return $new_list;
274
+	}
275
+
276
+	public function get_storage_usage()
277
+	{
278
+		$files = $this->get_backup_archives_list();
279
+		$total = 0;
280
+
281
+		if (is_array($files)) {
282
+			foreach ($files as $file) {
283
+				$total += $file['size'];
284
+			}
285
+		}
286
+
287
+		return $total;
288
+	}
289
+
290
+	public function is_part($backup_name)
291
+	{
292
+		if (stristr($backup_name, "-part")) {
293
+			return true;
294
+		}
295
+
296
+		return false;
297
+	}
298
+
299
+	public function is_multipart($backup_name)
300
+	{
301
+		if (stristr($backup_name, "-multipart")) {
302
+			return true;
303
+		}
304
+
305
+		return false;
306
+	}
307
+
308
+	public function get_backup_size($backup_name)
309
+	{
310
+		$backup_size = $this->get_storage_filesystem()->getSize($backup_name);
311
+		if ($this->is_multipart($backup_name)) {
312
+			$backup_parts = $this->get_multipart_files($backup_name);
313
+			foreach ($backup_parts as $part_file) {
314
+				$backup_size += $this->get_storage_filesystem()->getSize($part_file);
315
+			}
316
+		}
317
+
318
+		return $backup_size;
319
+	}
320
+
321
+	public function get_multipart_files($backup_name, $storage_selection = "")
322
+	{
323
+		$files = array();
324
+
325
+		if ($this->is_multipart($backup_name)) {
326
+			$lines = explode(PHP_EOL, $this->get_storage_filesystem($storage_selection)->read($backup_name));
327
+			foreach ($lines as $line) {
328
+				if ($line) {
329
+					$data = str_getcsv($line);
330
+					$files[] = $data[0];
331
+				}
332
+			}
333
+		}
334
+
335
+		return $files;
336
+	}
337
+
338
+	public function delete_backup_by_name($backup_name, $storage_selection = "")
339
+	{
340
+		if ($this->is_multipart($backup_name)) {
341
+			$lines = explode(PHP_EOL, $this->get_storage_filesystem($storage_selection)->read($backup_name));
342
+			foreach ($lines as $line) {
343
+				if ($line) {
344
+					$data = str_getcsv($line);
345
+					$this->get_storage_filesystem($storage_selection)->delete($data[0]);
346
+				}
347
+			}
348
+		}
349
+
350
+		if ($this->get_storage_filesystem($storage_selection)->delete($backup_name)) {
351
+			$return = true;
352
+		} else {
353
+			$return = false;
354
+		}
355
+
356
+		return $return;
357
+	}
358
+
359
+	public function getMetadataFull($adapter = "storage_adapter", $path)
360
+	{
361
+		$location = $this->$adapter->applyPathPrefix($path);
362
+		$spl_info = new SplFileInfo($location);
363
+
364
+		return ($spl_info);
365
+	}
366
+
367
+
368
+	public function get_backup_archives_list($storage_selection = "")
369
+	{
370
+		$list = array();
371
+
372
+
373
+		if (method_exists($this->get_storage_filesystem($storage_selection), "listContents")) {
374
+			$list = $this->get_storage_filesystem($storage_selection)->listContents();
375
+		}
376
+
377
+		$backup_files = array();
378
+		$parents = array();
379
+
380
+		foreach ($list as $file_info) {
381
+			if (isset($file_info['extension']) and $file_info['extension'] == "csv") {
382
+				$data = array();
383
+
384
+				$lines = explode(PHP_EOL, $this->get_storage_filesystem($storage_selection)->read($file_info['path']));
385
+				foreach ($lines as $line) {
386
+					if ($line) {
387
+						$data = str_getcsv($line);
388
+						if (is_array($data)) {
389
+							$parents[$data[0]] = $file_info['basename'];
390
+							$file_info['childs'][] = $data;
391
+							$file_info['size'] += $data[2];
392
+						}
393
+					}
394
+				}
395
+
396
+			}
397
+
398
+			if ($file_info['type'] == 'file' and isset($file_info['extension']) and in_array($file_info['extension'],
399
+					$this->backup_archive_extensions)) {
400
+				$backup_files[$file_info['path']] = $file_info;
401
+			}
402
+		}
403
+
404
+		foreach ($backup_files as $key => $file_info) {
405
+			if (!isset($backup_files[$key]['timestamp'])) {
406
+				//$backup_files[$key]['timestamp'] = $this->get_storage_filesystem($storage_selection)->getTimestamp($file_info['path']);
407
+			}
408
+
409
+			if (isset($parents[$file_info['basename']])) {
410
+				$backup_files[$key]['parent'] = $parents[$file_info['basename']];
411
+			}
412
+		}
413
+
414
+		return $backup_files;
415
+	}
416
+
417
+	public function start_file_recursion($init = 0)
418
+	{
419
+		if ($init) {
420
+			$this->logger->info(sprintf(__("Starting the filesystem scanner on root folder %s"),
421
+				$this->xcloner_settings->get_xcloner_start_path()));
422
+			$this->do_system_init();
423
+		}
424
+
425
+		if ($this->tmp_filesystem->has($this->get_temp_dir_handler())) {
426
+			//.dir exists, we presume we have files to iterate
427
+			$content = $this->tmp_filesystem->read($this->get_temp_dir_handler());
428
+			$files = array_filter(explode("\n", $content));
429
+			$this->tmp_filesystem->delete($this->get_temp_dir_handler());
430
+
431
+			$counter = 0;
432
+			foreach ($files as $file) {
433
+				if ($counter < $this->folders_to_process_per_session) {
434
+					$this->build_files_list($file);
435
+					$counter++;
436
+				} else {
437
+					$this->tmp_filesystem_append->write($this->get_temp_dir_handler(), $file."\n");
438
+				}
439
+			}
440
+		} else {
441
+			$this->build_files_list();
442
+		}
443
+
444
+		if ($this->scan_finished()) {
445
+			$metadata_dumpfile = $this->get_tmp_filesystem()->getMetadata("index.html");
446
+			$this->store_file($metadata_dumpfile, 'tmp_filesystem');
447
+			$this->files_counter++;
448
+
449
+			//adding included dump file to the included files list
450
+			if ($this->get_tmp_filesystem()->has($this->get_included_files_handler())) {
451
+				$metadata_dumpfile = $this->get_tmp_filesystem()->getMetadata($this->get_included_files_handler());
452
+				$this->store_file($metadata_dumpfile, 'tmp_filesystem');
453
+				$this->files_counter++;
454
+			}
455
+
456
+			//adding a default index.html to the temp xcloner folder
457
+			if (!$this->get_tmp_filesystem()->has("index.html")) {
458
+				$this->get_tmp_filesystem()->write("index.html", "");
459
+			}
460
+
461
+			//adding the default log file
462
+			if ($this->get_tmp_filesystem()->has($this->xcloner_settings->get_logger_filename(1))) {
463
+				$metadata_dumpfile = $this->get_tmp_filesystem()->getMetadata($this->xcloner_settings->get_logger_filename(1));
464
+				$this->store_file($metadata_dumpfile, 'tmp_filesystem');
465
+				$this->files_counter++;
466
+			}
467
+
468
+			return false;
469
+		}
470
+
471
+		return true;
472
+	}
473
+
474
+	public function get_backup_attachments()
475
+	{
476
+		$return = array();
477
+
478
+		$files_list_file = $this->xcloner_settings->get_xcloner_tmp_path().DS.$this->get_included_files_handler();
479
+		if (file_exists($files_list_file)) {
480
+			$return[] = $files_list_file;
481
+		}
482
+
483
+		if ($this->xcloner_settings->get_xcloner_option('xcloner_enable_log')) {
484
+			$log_file = $this->xcloner_settings->get_xcloner_tmp_path().DS.$this->xcloner_settings->get_logger_filename(1);
485
+			if (!file_exists($log_file)) {
486
+				$log_file = $this->xcloner_settings->get_xcloner_store_path().DS.$this->xcloner_settings->get_logger_filename();
487
+			}
488
+
489
+			if (file_exists($log_file)) {
490
+				$return[] = $log_file;
491
+			}
492
+		}
493
+
494
+		return $return;
495
+	}
496
+
497
+	public function remove_tmp_filesystem()
498
+	{
499
+		//delete the temporary folder
500
+		$this->logger->debug(sprintf("Deleting the temporary storage folder %s",
501
+			$this->xcloner_settings->get_xcloner_tmp_path()));
502
+
503
+		$contents = $this->get_tmp_filesystem()->listContents();
504
+
505
+		if (is_array($contents)) {
506
+			foreach ($contents as $file_info) {
507
+				$this->get_tmp_filesystem()->delete($file_info['path']);
508
+			}
509
+		}
510
+
511
+		@rmdir($this->xcloner_settings->get_xcloner_tmp_path());
512
+
513
+		return;
514
+	}
515
+
516
+	public function cleanup_tmp_directories()
517
+	{
518
+		$adapter = new Local($this->xcloner_settings->get_xcloner_tmp_path(false), LOCK_EX | FILE_APPEND, 'SKIP_LINKS');
519
+		$tmp_filesystem = new Filesystem($adapter, new Config([
520
+			'disable_asserts' => true,
521
+		]));
522
+
523
+		$contents = $tmp_filesystem->listContents();
524
+
525
+		foreach ($contents as $file) {
526
+
527
+			if (preg_match("/.xcloner-(.*)/", $file['path'])) {
528
+				if ($file['timestamp'] < strtotime("-1days")) {
529
+					$tmp_filesystem->deleteDir($file['path']);
530
+					$this->logger->debug(sprintf("Delete temporary directory %s", $file['path']));
531
+				}
532
+			}
533
+		}
534
+
535
+		return true;
536
+	}
537
+
538
+	private function do_system_init()
539
+	{
540
+		$this->files_counter = 0;
541
+
542
+		if (!$this->storage_filesystem->has("index.html")) {
543
+			$this->storage_filesystem->write("index.html", "");
544
+		}
545
+
546
+		if (!$this->tmp_filesystem->has("index.html")) {
547
+			$this->tmp_filesystem->write("index.html", "");
548
+		}
549
+
550
+		if ($this->tmp_filesystem->has($this->get_included_files_handler())) {
551
+			$this->tmp_filesystem->delete($this->get_included_files_handler());
552
+		}
553
+
554
+		if ($this->tmp_filesystem->has($this->get_temp_dir_handler())) {
555
+			$this->tmp_filesystem->delete($this->get_temp_dir_handler());
556
+		}
557
+	}
558
+
559
+	public function get_scanned_files_num()
560
+	{
561
+		return $this->files_counter;
562
+	}
563
+
564
+	public function get_scanned_files_total_size()
565
+	{
566
+		return $this->files_size;
567
+	}
568
+
569
+	public function last_logged_file()
570
+	{
571
+		return $this->last_logged_file;
572
+	}
573
+
574
+	public static function is_regex($regex)
575
+	{
576
+		return preg_match("/^\^(.*)\$$/i", $regex);
577
+	}
578 578
 
579
-    public function set_excluded_files($excluded_files = array())
580
-    {
581
-        if (!is_array($excluded_files)) {
582
-            $excluded_files = array();
583
-        }
579
+	public function set_excluded_files($excluded_files = array())
580
+	{
581
+		if (!is_array($excluded_files)) {
582
+			$excluded_files = array();
583
+		}
584 584
 
585
-        foreach ($excluded_files as $excl) {
585
+		foreach ($excluded_files as $excl) {
586 586
 
587
-            if ($this->is_regex($excl)) {
588
-                $this->additional_regex_patterns[] = $excl;
589
-            }
590
-        }
587
+			if ($this->is_regex($excl)) {
588
+				$this->additional_regex_patterns[] = $excl;
589
+			}
590
+		}
591 591
 
592
-        $this->excluded_files = array_merge($excluded_files, $this->excluded_files_by_default);
592
+		$this->excluded_files = array_merge($excluded_files, $this->excluded_files_by_default);
593 593
 
594
-        return $this->excluded_files;
595
-    }
594
+		return $this->excluded_files;
595
+	}
596 596
 
597
-    public function get_excluded_files()
598
-    {
599
-        return $this->excluded_files_by_default;
600
-    }
597
+	public function get_excluded_files()
598
+	{
599
+		return $this->excluded_files_by_default;
600
+	}
601 601
 
602
-    public function list_directory($path)
603
-    {
604
-        return $this->start_filesystem->listContents($path);
605
-    }
602
+	public function list_directory($path)
603
+	{
604
+		return $this->start_filesystem->listContents($path);
605
+	}
606 606
 
607
-    public function build_files_list($folder = "")
608
-    {
609
-        $this->logger->debug(sprintf(("Building the files system list")));
607
+	public function build_files_list($folder = "")
608
+	{
609
+		$this->logger->debug(sprintf(("Building the files system list")));
610 610
 
611
-        //if we start with the root folder(empty value), we initializa the file system
612
-        if (!$folder) {
611
+		//if we start with the root folder(empty value), we initializa the file system
612
+		if (!$folder) {
613 613
 
614
-        }
614
+		}
615 615
 
616
-        try {
616
+		try {
617 617
 
618
-            $files = $this->start_filesystem->listContents($folder);
619
-            foreach ($files as $file) {
620
-                if (!is_readable($this->xcloner_settings->get_xcloner_start_path().DS.$file['path'])) {
621
-                    $this->logger->info(sprintf(__("Excluding %s from the filesystem list, file not readable"),
622
-                        $file['path']), array(
623
-                        "FILESYSTEM SCAN",
624
-                        "NOT READABLE"
625
-                    ));
626
-                } elseif (!$matching_pattern = $this->is_excluded($file)) {
627
-                    $this->logger->info(sprintf(__("Adding %s to the filesystem list"), $file['path']), array(
628
-                        "FILESYSTEM SCAN",
629
-                        "INCLUDE"
630
-                    ));
631
-                    $file['visibility'] = $this->start_filesystem->getVisibility($file['path']);
632
-                    if ($this->store_file($file)) {
633
-                        $this->files_counter++;
634
-                    }
635
-                    if (isset($file['size'])) {
636
-                        $this->files_size += $file['size'];
637
-                    }
638
-
639
-                } else {
640
-                    $this->logger->info(sprintf(__("Excluding %s from the filesystem list, matching pattern %s"),
641
-                        $file['path'], $matching_pattern), array(
642
-                        "FILESYSTEM SCAN",
643
-                        "EXCLUDE"
644
-                    ));
645
-                }
646
-            }
618
+			$files = $this->start_filesystem->listContents($folder);
619
+			foreach ($files as $file) {
620
+				if (!is_readable($this->xcloner_settings->get_xcloner_start_path().DS.$file['path'])) {
621
+					$this->logger->info(sprintf(__("Excluding %s from the filesystem list, file not readable"),
622
+						$file['path']), array(
623
+						"FILESYSTEM SCAN",
624
+						"NOT READABLE"
625
+					));
626
+				} elseif (!$matching_pattern = $this->is_excluded($file)) {
627
+					$this->logger->info(sprintf(__("Adding %s to the filesystem list"), $file['path']), array(
628
+						"FILESYSTEM SCAN",
629
+						"INCLUDE"
630
+					));
631
+					$file['visibility'] = $this->start_filesystem->getVisibility($file['path']);
632
+					if ($this->store_file($file)) {
633
+						$this->files_counter++;
634
+					}
635
+					if (isset($file['size'])) {
636
+						$this->files_size += $file['size'];
637
+					}
638
+
639
+				} else {
640
+					$this->logger->info(sprintf(__("Excluding %s from the filesystem list, matching pattern %s"),
641
+						$file['path'], $matching_pattern), array(
642
+						"FILESYSTEM SCAN",
643
+						"EXCLUDE"
644
+					));
645
+				}
646
+			}
647 647
 
648
-        }catch (Exception $e) {
648
+		}catch (Exception $e) {
649 649
 
650
-            $this->logger->error($e->getMessage());
651
-
652
-        }
650
+			$this->logger->error($e->getMessage());
651
+
652
+		}
653 653
 
654
-    }
654
+	}
655 655
 
656
-    public function estimate_read_write_time()
657
-    {
658
-        $tmp_file = ".xcloner".substr(md5(time()), 0, 5);
656
+	public function estimate_read_write_time()
657
+	{
658
+		$tmp_file = ".xcloner".substr(md5(time()), 0, 5);
659 659
 
660
-        $start_time = microtime(true);
660
+		$start_time = microtime(true);
661 661
 
662
-        $data = str_repeat(rand(0, 9), 1024 * 1024); //write 1MB data
662
+		$data = str_repeat(rand(0, 9), 1024 * 1024); //write 1MB data
663 663
 
664
-        try {
665
-            $this->tmp_filesystem->write($tmp_file, $data);
664
+		try {
665
+			$this->tmp_filesystem->write($tmp_file, $data);
666 666
 
667
-            $end_time = microtime(true) - $start_time;
667
+			$end_time = microtime(true) - $start_time;
668 668
 
669
-            $return['writing_time'] = $end_time;
669
+			$return['writing_time'] = $end_time;
670 670
 
671
-            $return['reading_time'] = $this->estimate_reading_time($tmp_file);
671
+			$return['reading_time'] = $this->estimate_reading_time($tmp_file);
672 672
 
673
-            $this->tmp_filesystem->delete($tmp_file);
673
+			$this->tmp_filesystem->delete($tmp_file);
674 674
 
675
-        }catch (Exception $e) {
675
+		}catch (Exception $e) {
676 676
 
677
-            $this->logger->error($e->getMessage());
677
+			$this->logger->error($e->getMessage());
678 678
 
679
-        }
679
+		}
680 680
 
681
-        return $return;
682
-    }
681
+		return $return;
682
+	}
683 683
 
684
-    public function backup_storage_cleanup()
685
-    {
686
-        $this->logger->info(sprintf(("Cleaning the backup storage on matching rules")));
684
+	public function backup_storage_cleanup()
685
+	{
686
+		$this->logger->info(sprintf(("Cleaning the backup storage on matching rules")));
687 687
 
688
-        $_storage_size = 0;
689
-        $_backup_files_list = array();
688
+		$_storage_size = 0;
689
+		$_backup_files_list = array();
690 690
 
691
-        //rule date limit
692
-        $current_timestamp = strtotime("-".$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_days')." days");
693
-
694
-        $files = $this->storage_filesystem->listContents();
695
-
696
-        if (is_array($files)) {
697
-            foreach ($files as $file) {
698
-                if (isset($file['extension']) and in_array($file['extension'], $this->backup_archive_extensions)) {
699
-                    $_storage_size += $file['size']; //bytes
700
-                    $_backup_files_list[] = $file;
701
-                }
702
-            }
703
-        }
691
+		//rule date limit
692
+		$current_timestamp = strtotime("-".$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_days')." days");
693
+
694
+		$files = $this->storage_filesystem->listContents();
695
+
696
+		if (is_array($files)) {
697
+			foreach ($files as $file) {
698
+				if (isset($file['extension']) and in_array($file['extension'], $this->backup_archive_extensions)) {
699
+					$_storage_size += $file['size']; //bytes
700
+					$_backup_files_list[] = $file;
701
+				}
702
+			}
703
+		}
704 704
 
705 705
 
706
-        $this->sort_by($_backup_files_list, "timestamp", "asc");
707
-
708
-        $_backups_counter = sizeof($_backup_files_list);
709
-
710
-        foreach ($_backup_files_list as $file) {
711
-            //processing rule folder capacity
712
-            if ($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_capacity_limit') &&
713
-                $_storage_size >= ($set_storage_limit = 1024 * 1024 * $this->xcloner_settings->get_xcloner_option('xcloner_cleanup_capacity_limit')))    //bytes
714
-            {
715
-                $this->storage_filesystem->delete($file['path']);
716
-                $_storage_size -= $file['size'];
717
-                $this->logger->info("Deleting backup ".$file['path']." matching rule", array(
718
-                    "STORAGE SIZE LIMIT",
719
-                    $_storage_size." >= ".$set_storage_limit
720
-                ));
721
-            }
722
-
723
-            //processing rule days limit
724
-            if ($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_days') && $current_timestamp >= $file['timestamp']) {
725
-                $this->storage_filesystem->delete($file['path']);
726
-                $this->logger->info("Deleting backup ".$file['path']." matching rule", array(
727
-                    "RETENTION LIMIT TIMESTAMP",
728
-                    $file['timestamp']." =< ".$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_days')
729
-                ));
730
-            }
731
-
732
-            //processing backup countert limit
733
-            if ($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_archives') && $_backups_counter >= $this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_archives')) {
734
-                $this->storage_filesystem->delete($file['path']);
735
-                $_backups_counter--;
736
-                $this->logger->info("Deleting backup ".$file['path']." matching rule", array(
737
-                    "BACKUP QUANTITY LIMIT",
738
-                    $_backups_counter." >= ".$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_archives')
739
-                ));
740
-            }
741
-
742
-
743
-        }
706
+		$this->sort_by($_backup_files_list, "timestamp", "asc");
707
+
708
+		$_backups_counter = sizeof($_backup_files_list);
709
+
710
+		foreach ($_backup_files_list as $file) {
711
+			//processing rule folder capacity
712
+			if ($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_capacity_limit') &&
713
+				$_storage_size >= ($set_storage_limit = 1024 * 1024 * $this->xcloner_settings->get_xcloner_option('xcloner_cleanup_capacity_limit')))    //bytes
714
+			{
715
+				$this->storage_filesystem->delete($file['path']);
716
+				$_storage_size -= $file['size'];
717
+				$this->logger->info("Deleting backup ".$file['path']." matching rule", array(
718
+					"STORAGE SIZE LIMIT",
719
+					$_storage_size." >= ".$set_storage_limit
720
+				));
721
+			}
722
+
723
+			//processing rule days limit
724
+			if ($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_days') && $current_timestamp >= $file['timestamp']) {
725
+				$this->storage_filesystem->delete($file['path']);
726
+				$this->logger->info("Deleting backup ".$file['path']." matching rule", array(
727
+					"RETENTION LIMIT TIMESTAMP",
728
+					$file['timestamp']." =< ".$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_days')
729
+				));
730
+			}
731
+
732
+			//processing backup countert limit
733
+			if ($this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_archives') && $_backups_counter >= $this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_archives')) {
734
+				$this->storage_filesystem->delete($file['path']);
735
+				$_backups_counter--;
736
+				$this->logger->info("Deleting backup ".$file['path']." matching rule", array(
737
+					"BACKUP QUANTITY LIMIT",
738
+					$_backups_counter." >= ".$this->xcloner_settings->get_xcloner_option('xcloner_cleanup_retention_limit_archives')
739
+				));
740
+			}
741
+
742
+
743
+		}
744 744
 
745
-    }
745
+	}
746 746
 
747
-    /**
748
-     * @param string $tmp_file
749
-     */
750
-    public function estimate_reading_time($tmp_file)
751
-    {
752
-        $this->logger->debug(sprintf(("Estimating file system reading time")));
753
-
754
-        $start_time = microtime(true);
755
-
756
-        if ($this->tmp_filesystem->has($tmp_file)) {
757
-            $this->tmp_filesystem->read($tmp_file);
758
-        }
759
-
760
-        $end_time = microtime(true) - $start_time;
761
-
762
-        return $end_time;
763
-
764
-    }
765
-
766
-    public function process_backup_name($name = "", $max_length = 100)
767
-    {
768
-        if (!$name) {
769
-            $name = $this->xcloner_settings->get_default_backup_name();
770
-        }
771
-
772
-        foreach ($this->backup_name_tags as $tag) {
773
-            if ($tag == '[time]') {
774
-                $name = str_replace($tag, date("Y-m-d_H-i"), $name);
775
-            } elseif ($tag == '[hostname]') {
776
-                $name = str_replace($tag, gethostname(), $name);
777
-            } elseif ($tag == '[domain]') {
778
-                $domain = parse_url(admin_url(), PHP_URL_HOST);
779
-                $name = str_replace($tag, $domain, $name);
780
-            }
781
-        }
782
-
783
-        if ($max_length) {
784
-            $name = substr($name, 0, $max_length);
785
-        }
786
-
787
-        return $name;
788
-    }
789
-
790
-    /**
791
-     * @param string $field
792
-     */
793
-    public function sort_by(&$array, $field, $direction = 'asc')
794
-    {
795
-        if (strtolower($direction) == "desc" || $direction == SORT_DESC) {
796
-            $direction = SORT_DESC;
797
-        } else {
798
-            $direction = SORT_ASC;
799
-        }
800
-
801
-        $array = $this->array_orderby($array, $field, $direction);
802
-
803
-        return true;
804
-    }
805
-
806
-    private function array_orderby()
807
-    {
808
-        $args = func_get_args();
809
-        $data = array_shift($args);
810
-
811
-        foreach ($args as $n => $field) {
812
-            if (is_string($field)) {
813
-                $tmp = array();
814
-                foreach ($data as $key => $row) {
815
-                    if (is_array($row)) {
816
-                        $tmp[$key] = $row[$field];
817
-                    } else {
818
-                        $tmp[$key] = $row->$field;
819
-                    }
820
-                }
821
-                $args[$n] = $tmp;
822
-            }
823
-        }
824
-        $args[] = &$data;
825
-
826
-        call_user_func_array('array_multisort', $args);
827
-
828
-        return array_pop($args);
829
-    }
830
-
831
-    private function check_file_diff_time($file)
832
-    {
833
-        if ($this->get_diff_timestamp_start() != "") {
834
-            $fileMeta = $this->getMetadataFull("start_adapter", $file['path']);
835
-            $timestamp = $fileMeta->getMTime();
836
-            if ($timestamp < $fileMeta->getCTime()) {
837
-                $timestamp = $fileMeta->getCTime();
838
-            }
839
-
840
-            if ($timestamp <= $this->get_diff_timestamp_start()) {
841
-                return " file DIFF timestamp ".$timestamp." < ".$this->diff_timestamp_start;
842
-            }
843
-        }
844
-
845
-        return false;
846
-    }
847
-
848
-    public function is_excluded($file)
849
-    {
850
-        $this->logger->debug(sprintf(("Checking if %s is excluded"), $file['path']));
851
-
852
-        if ($xcloner_exclude_files_larger_than_mb = $this->xcloner_settings->get_xcloner_option('xcloner_exclude_files_larger_than_mb')) {
853
-            if (isset($file['size']) && $file['size'] > $this->calc_to_bytes($xcloner_exclude_files_larger_than_mb)) {
854
-                return "> ".$xcloner_exclude_files_larger_than_mb."MB";
855
-            }
856
-        }
857
-
858
-        if (!is_array($this->excluded_files) || !sizeof($this->excluded_files)) {
859
-            $this->set_excluded_files();
860
-        }
861
-
862
-        if (is_array($this->excluded_files)) {
863
-            foreach ($this->excluded_files as $excluded_file_pattern) {
864
-                if ($excluded_file_pattern == "/") {
865
-                    $needle = "$";
866
-                } else {
867
-                    $needle = "$".$excluded_file_pattern;
868
-                }
869
-
870
-                if (strstr("$".$file['path'], $needle)) {
871
-                    return $excluded_file_pattern;
872
-                }
873
-            }
874
-        }
875
-
876
-        if ($regex = $this->is_excluded_regex($file)) {
877
-            return $regex;
878
-        }
879
-
880
-        if ($file['type'] == "file") {
881
-            $check_file_diff_timestamp = $this->check_file_diff_time($file);
882
-            if ($check_file_diff_timestamp) {
883
-                return $check_file_diff_timestamp;
884
-            }
885
-        }
886
-
887
-        return false;
888
-    }
889
-
890
-    /*REGEX examples
747
+	/**
748
+	 * @param string $tmp_file
749
+	 */
750
+	public function estimate_reading_time($tmp_file)
751
+	{
752
+		$this->logger->debug(sprintf(("Estimating file system reading time")));
753
+
754
+		$start_time = microtime(true);
755
+
756
+		if ($this->tmp_filesystem->has($tmp_file)) {
757
+			$this->tmp_filesystem->read($tmp_file);
758
+		}
759
+
760
+		$end_time = microtime(true) - $start_time;
761
+
762
+		return $end_time;
763
+
764
+	}
765
+
766
+	public function process_backup_name($name = "", $max_length = 100)
767
+	{
768
+		if (!$name) {
769
+			$name = $this->xcloner_settings->get_default_backup_name();
770
+		}
771
+
772
+		foreach ($this->backup_name_tags as $tag) {
773
+			if ($tag == '[time]') {
774
+				$name = str_replace($tag, date("Y-m-d_H-i"), $name);
775
+			} elseif ($tag == '[hostname]') {
776
+				$name = str_replace($tag, gethostname(), $name);
777
+			} elseif ($tag == '[domain]') {
778
+				$domain = parse_url(admin_url(), PHP_URL_HOST);
779
+				$name = str_replace($tag, $domain, $name);
780
+			}
781
+		}
782
+
783
+		if ($max_length) {
784
+			$name = substr($name, 0, $max_length);
785
+		}
786
+
787
+		return $name;
788
+	}
789
+
790
+	/**
791
+	 * @param string $field
792
+	 */
793
+	public function sort_by(&$array, $field, $direction = 'asc')
794
+	{
795
+		if (strtolower($direction) == "desc" || $direction == SORT_DESC) {
796
+			$direction = SORT_DESC;
797
+		} else {
798
+			$direction = SORT_ASC;
799
+		}
800
+
801
+		$array = $this->array_orderby($array, $field, $direction);
802
+
803
+		return true;
804
+	}
805
+
806
+	private function array_orderby()
807
+	{
808
+		$args = func_get_args();
809
+		$data = array_shift($args);
810
+
811
+		foreach ($args as $n => $field) {
812
+			if (is_string($field)) {
813
+				$tmp = array();
814
+				foreach ($data as $key => $row) {
815
+					if (is_array($row)) {
816
+						$tmp[$key] = $row[$field];
817
+					} else {
818
+						$tmp[$key] = $row->$field;
819
+					}
820
+				}
821
+				$args[$n] = $tmp;
822
+			}
823
+		}
824
+		$args[] = &$data;
825
+
826
+		call_user_func_array('array_multisort', $args);
827
+
828
+		return array_pop($args);
829
+	}
830
+
831
+	private function check_file_diff_time($file)
832
+	{
833
+		if ($this->get_diff_timestamp_start() != "") {
834
+			$fileMeta = $this->getMetadataFull("start_adapter", $file['path']);
835
+			$timestamp = $fileMeta->getMTime();
836
+			if ($timestamp < $fileMeta->getCTime()) {
837
+				$timestamp = $fileMeta->getCTime();
838
+			}
839
+
840
+			if ($timestamp <= $this->get_diff_timestamp_start()) {
841
+				return " file DIFF timestamp ".$timestamp." < ".$this->diff_timestamp_start;
842
+			}
843
+		}
844
+
845
+		return false;
846
+	}
847
+
848
+	public function is_excluded($file)
849
+	{
850
+		$this->logger->debug(sprintf(("Checking if %s is excluded"), $file['path']));
851
+
852
+		if ($xcloner_exclude_files_larger_than_mb = $this->xcloner_settings->get_xcloner_option('xcloner_exclude_files_larger_than_mb')) {
853
+			if (isset($file['size']) && $file['size'] > $this->calc_to_bytes($xcloner_exclude_files_larger_than_mb)) {
854
+				return "> ".$xcloner_exclude_files_larger_than_mb."MB";
855
+			}
856
+		}
857
+
858
+		if (!is_array($this->excluded_files) || !sizeof($this->excluded_files)) {
859
+			$this->set_excluded_files();
860
+		}
861
+
862
+		if (is_array($this->excluded_files)) {
863
+			foreach ($this->excluded_files as $excluded_file_pattern) {
864
+				if ($excluded_file_pattern == "/") {
865
+					$needle = "$";
866
+				} else {
867
+					$needle = "$".$excluded_file_pattern;
868
+				}
869
+
870
+				if (strstr("$".$file['path'], $needle)) {
871
+					return $excluded_file_pattern;
872
+				}
873
+			}
874
+		}
875
+
876
+		if ($regex = $this->is_excluded_regex($file)) {
877
+			return $regex;
878
+		}
879
+
880
+		if ($file['type'] == "file") {
881
+			$check_file_diff_timestamp = $this->check_file_diff_time($file);
882
+			if ($check_file_diff_timestamp) {
883
+				return $check_file_diff_timestamp;
884
+			}
885
+		}
886
+
887
+		return false;
888
+	}
889
+
890
+	/*REGEX examples
891 891
      *
892 892
     * exclude all except .php file
893 893
     * PATTERN: ^(.*)\.(.+)$(?<!(php))
@@ -919,163 +919,163 @@  discard block
 block discarded – undo
919 919
     * exclude the backup folders
920 920
     * PATTERN: (^|^\/)(wp-content\/backups|administrator\/backups)(.*)$";
921 921
     */
922
-    private function is_excluded_regex($file)
923
-    {
924
-        //$this->logger->debug(sprintf(("Checking if %s is excluded"), $file['path']));
925
-
926
-        $regex_patterns = explode(PHP_EOL, $this->xcloner_settings->get_xcloner_option('xcloner_regex_exclude'));
927
-
928
-        if (is_array($this->additional_regex_patterns)) {
929
-            $regex_patterns = array_merge($regex_patterns, $this->additional_regex_patterns);
930
-        }
931
-
932
-        //print_r($regex_patterns);exit;
933
-
934
-        if (is_array($regex_patterns)) {
935
-            //$this->excluded_files = array();
936
-            //$this->excluded_files[] ="(.*)\.(git)(.*)$";
937
-            //$this->excluded_files[] ="wp-content\/backups(.*)$";
938
-
939
-            foreach ($regex_patterns as $excluded_file_pattern) {
940
-
941
-                if (substr($excluded_file_pattern, strlen($excluded_file_pattern) - 1,
942
-                        strlen($excluded_file_pattern)) == "\r") {
943
-                    $excluded_file_pattern = substr($excluded_file_pattern, 0, strlen($excluded_file_pattern) - 1);
944
-                }
945
-
946
-                if ($file['path'] == "/") {
947
-                    $needle = "/";
948
-                } else {
949
-                    $needle = "/".$file['path'];
950
-                }
951
-                //echo $needle."---".$excluded_file_pattern."---\n";
952
-
953
-                if (@preg_match("/(^|^\/)".$excluded_file_pattern."/i", $needle)) {
954
-                    return $excluded_file_pattern;
955
-                }
956
-            }
957
-        }
958
-
959
-        return false;
960
-    }
961
-
962
-    public function store_file($file, $storage = 'start_filesystem')
963
-    {
964
-        $this->logger->debug(sprintf("Storing %s in the backup list", $file['path']));
965
-
966
-        if (!isset($file['size'])) {
967
-            $file['size'] = 0;
968
-        }
969
-        if (!isset($file['visibility'])) {
970
-            $file['visibility'] = "private";
971
-        }
972
-
973
-        $csv_filename = str_replace('"', '""', $file['path']);
974
-
975
-        $line = '"'.($csv_filename).'","'.$file['timestamp'].'","'.$file['size'].'","'.$file['visibility'].'","'.$storage.'"'.PHP_EOL;
976
-
977
-        $this->last_logged_file = $file['path'];
978
-
979
-        if ($file['type'] == "dir") {
980
-            try {
981
-                $this->tmp_filesystem_append->write($this->get_temp_dir_handler(), $file['path']."\n");
982
-            }catch (Exception $e) {
983
-                $this->logger->error($e->getMessage());
984
-            }
985
-        }
986
-
987
-        if ($this->get_diff_timestamp_start()) {
988
-            if ($file['type'] != "file" && $response = $this->check_file_diff_time($file)) {
989
-                $this->logger->info(sprintf("Directory %s archiving skipped on differential backup %s", $file['path'],
990
-                    $response), array(
991
-                    "FILESYSTEM SCAN",
992
-                    "DIR DIFF"
993
-                ));
994
-
995
-                return false;
996
-            }
997
-        }
998
-
999
-        try {
1000
-            if (!$this->tmp_filesystem_append->has($this->get_included_files_handler())) {
1001
-                //adding fix for UTF-8 CSV preview
1002
-                $start_line = "\xEF\xBB\xBF".'"Filename","Timestamp","Size","Visibility","Storage"'.PHP_EOL;
1003
-                $this->tmp_filesystem_append->write($this->get_included_files_handler(), $start_line);
1004
-            }
1005
-
1006
-            $this->tmp_filesystem_append->write($this->get_included_files_handler(), $line);
1007
-
1008
-        }catch (Exception $e) {
1009
-
1010
-            $this->logger->error($e->getMessage());
1011
-        }
1012
-
1013
-        return true;
1014
-    }
1015
-
1016
-    public function get_fileystem_handler()
1017
-    {
1018
-        return $this;
1019
-    }
1020
-
1021
-    public function get_filesystem($system = "")
1022
-    {
1023
-        if ($system == "storage_filesystem_append") {
1024
-            return $this->storage_filesystem_append;
1025
-        } elseif ($system == "tmp_filesystem_append") {
1026
-            return $this->tmp_filesystem_append;
1027
-        } elseif ($system == "tmp_filesystem") {
1028
-            return $this->tmp_filesystem;
1029
-        } elseif ($system == "storage_filesystem") {
1030
-            return $this->storage_filesystem;
1031
-        } else {
1032
-            return $this->start_filesystem;
1033
-        }
1034
-    }
1035
-
1036
-    public function get_adapter($system)
1037
-    {
1038
-        if ($system == "tmp_filesystem") {
1039
-            return $this->tmp_adapter;
1040
-        } elseif ($system == "storage_filesystem") {
1041
-            return $this->storage_adapter;
1042
-        } else {
1043
-            return $this->start_adapter;
1044
-        }
1045
-    }
1046
-
1047
-    /**
1048
-     * File scan finished
1049
-     * Method called when file scan is finished
1050
-     *
1051
-     * @return bool
1052
-     */
1053
-    private function scan_finished()
1054
-    {
1055
-        if ($this->tmp_filesystem_append->has($this->get_temp_dir_handler()) &&
1056
-            $this->tmp_filesystem_append->getSize($this->get_temp_dir_handler())) {
1057
-            return false;
1058
-        }
1059
-
1060
-        if ($this->tmp_filesystem->has($this->get_temp_dir_handler())) {
1061
-            $this->tmp_filesystem->delete($this->get_temp_dir_handler());
1062
-        }
1063
-
1064
-        $this->logger->debug(sprintf(("File scan finished")));
1065
-
1066
-        return true;
1067
-    }
1068
-
1069
-    /**
1070
-     * Calculate bytes from MB value
1071
-     *
1072
-     * @param int $mb_size
1073
-     *
1074
-     * @return float|int
1075
-     */
1076
-    private function calc_to_bytes($mb_size)
1077
-    {
1078
-        return $mb_size * (1024 * 1024);
1079
-    }
922
+	private function is_excluded_regex($file)
923
+	{
924
+		//$this->logger->debug(sprintf(("Checking if %s is excluded"), $file['path']));
925
+
926
+		$regex_patterns = explode(PHP_EOL, $this->xcloner_settings->get_xcloner_option('xcloner_regex_exclude'));
927
+
928
+		if (is_array($this->additional_regex_patterns)) {
929
+			$regex_patterns = array_merge($regex_patterns, $this->additional_regex_patterns);
930
+		}
931
+
932
+		//print_r($regex_patterns);exit;
933
+
934
+		if (is_array($regex_patterns)) {
935
+			//$this->excluded_files = array();
936
+			//$this->excluded_files[] ="(.*)\.(git)(.*)$";
937
+			//$this->excluded_files[] ="wp-content\/backups(.*)$";
938
+
939
+			foreach ($regex_patterns as $excluded_file_pattern) {
940
+
941
+				if (substr($excluded_file_pattern, strlen($excluded_file_pattern) - 1,
942
+						strlen($excluded_file_pattern)) == "\r") {
943
+					$excluded_file_pattern = substr($excluded_file_pattern, 0, strlen($excluded_file_pattern) - 1);
944
+				}
945
+
946
+				if ($file['path'] == "/") {
947
+					$needle = "/";
948
+				} else {
949
+					$needle = "/".$file['path'];
950
+				}
951
+				//echo $needle."---".$excluded_file_pattern."---\n";
952
+
953
+				if (@preg_match("/(^|^\/)".$excluded_file_pattern."/i", $needle)) {
954
+					return $excluded_file_pattern;
955
+				}
956
+			}
957
+		}
958
+
959
+		return false;
960
+	}
961
+
962
+	public function store_file($file, $storage = 'start_filesystem')
963
+	{
964
+		$this->logger->debug(sprintf("Storing %s in the backup list", $file['path']));
965
+
966
+		if (!isset($file['size'])) {
967
+			$file['size'] = 0;
968
+		}
969
+		if (!isset($file['visibility'])) {
970
+			$file['visibility'] = "private";
971
+		}
972
+
973
+		$csv_filename = str_replace('"', '""', $file['path']);
974
+
975
+		$line = '"'.($csv_filename).'","'.$file['timestamp'].'","'.$file['size'].'","'.$file['visibility'].'","'.$storage.'"'.PHP_EOL;
976
+
977
+		$this->last_logged_file = $file['path'];
978
+
979
+		if ($file['type'] == "dir") {
980
+			try {
981
+				$this->tmp_filesystem_append->write($this->get_temp_dir_handler(), $file['path']."\n");
982
+			}catch (Exception $e) {
983
+				$this->logger->error($e->getMessage());
984
+			}
985
+		}
986
+
987
+		if ($this->get_diff_timestamp_start()) {
988
+			if ($file['type'] != "file" && $response = $this->check_file_diff_time($file)) {
989
+				$this->logger->info(sprintf("Directory %s archiving skipped on differential backup %s", $file['path'],
990
+					$response), array(
991
+					"FILESYSTEM SCAN",
992
+					"DIR DIFF"
993
+				));
994
+
995
+				return false;
996
+			}
997
+		}
998
+
999
+		try {
1000
+			if (!$this->tmp_filesystem_append->has($this->get_included_files_handler())) {
1001
+				//adding fix for UTF-8 CSV preview
1002
+				$start_line = "\xEF\xBB\xBF".'"Filename","Timestamp","Size","Visibility","Storage"'.PHP_EOL;
1003
+				$this->tmp_filesystem_append->write($this->get_included_files_handler(), $start_line);
1004
+			}
1005
+
1006
+			$this->tmp_filesystem_append->write($this->get_included_files_handler(), $line);
1007
+
1008
+		}catch (Exception $e) {
1009
+
1010
+			$this->logger->error($e->getMessage());
1011
+		}
1012
+
1013
+		return true;
1014
+	}
1015
+
1016
+	public function get_fileystem_handler()
1017
+	{
1018
+		return $this;
1019
+	}
1020
+
1021
+	public function get_filesystem($system = "")
1022
+	{
1023
+		if ($system == "storage_filesystem_append") {
1024
+			return $this->storage_filesystem_append;
1025
+		} elseif ($system == "tmp_filesystem_append") {
1026
+			return $this->tmp_filesystem_append;
1027
+		} elseif ($system == "tmp_filesystem") {
1028
+			return $this->tmp_filesystem;
1029
+		} elseif ($system == "storage_filesystem") {
1030
+			return $this->storage_filesystem;
1031
+		} else {
1032
+			return $this->start_filesystem;
1033
+		}
1034
+	}
1035
+
1036
+	public function get_adapter($system)
1037
+	{
1038
+		if ($system == "tmp_filesystem") {
1039
+			return $this->tmp_adapter;
1040
+		} elseif ($system == "storage_filesystem") {
1041
+			return $this->storage_adapter;
1042
+		} else {
1043
+			return $this->start_adapter;
1044
+		}
1045
+	}
1046
+
1047
+	/**
1048
+	 * File scan finished
1049
+	 * Method called when file scan is finished
1050
+	 *
1051
+	 * @return bool
1052
+	 */
1053
+	private function scan_finished()
1054
+	{
1055
+		if ($this->tmp_filesystem_append->has($this->get_temp_dir_handler()) &&
1056
+			$this->tmp_filesystem_append->getSize($this->get_temp_dir_handler())) {
1057
+			return false;
1058
+		}
1059
+
1060
+		if ($this->tmp_filesystem->has($this->get_temp_dir_handler())) {
1061
+			$this->tmp_filesystem->delete($this->get_temp_dir_handler());
1062
+		}
1063
+
1064
+		$this->logger->debug(sprintf(("File scan finished")));
1065
+
1066
+		return true;
1067
+	}
1068
+
1069
+	/**
1070
+	 * Calculate bytes from MB value
1071
+	 *
1072
+	 * @param int $mb_size
1073
+	 *
1074
+	 * @return float|int
1075
+	 */
1076
+	private function calc_to_bytes($mb_size)
1077
+	{
1078
+		return $mb_size * (1024 * 1024);
1079
+	}
1080 1080
 
1081 1081
 }
Please login to merge, or discard this patch.
includes/class-xcloner-archive.php 1 patch
Indentation   +350 added lines, -350 removed lines patch added patch discarded remove patch
@@ -214,9 +214,9 @@  discard block
 block discarded – undo
214 214
      * @return bool
215 215
      */
216 216
 
217
-    /**
218
-     * @param string $error_message
219
-     */
217
+	/**
218
+	 * @param string $error_message
219
+	 */
220 220
 	public function send_notification_error($to, $from, $subject, $backup_name, $params, $error_message)
221 221
 	{
222 222
 
@@ -255,486 +255,486 @@  discard block
 block discarded – undo
255 255
      *
256 256
      * @return bool
257 257
      */
258
-    public function send_notification(
259
-        $to,
260
-        $from,
261
-        $subject,
262
-        $backup_name,
263
-        $params,
264
-        $error_message = "",
265
-        $additional = array()
266
-    ) {
267
-        if (!$from) {
268
-            $from = "XCloner Backup";
269
-        }
258
+	public function send_notification(
259
+		$to,
260
+		$from,
261
+		$subject,
262
+		$backup_name,
263
+		$params,
264
+		$error_message = "",
265
+		$additional = array()
266
+	) {
267
+		if (!$from) {
268
+			$from = "XCloner Backup";
269
+		}
270 270
 
271
-        if (($error_message)) {
272
-            return $this->send_notification_error($to, $from, $subject, $backup_name, $params, $error_message);
273
-        }
271
+		if (($error_message)) {
272
+			return $this->send_notification_error($to, $from, $subject, $backup_name, $params, $error_message);
273
+		}
274 274
 
275
-        $params = (array)$params;
275
+		$params = (array)$params;
276 276
 
277
-        if (!$subject) {
278
-            $subject = sprintf(__("New backup generated %s"), $backup_name);
279
-        }
277
+		if (!$subject) {
278
+			$subject = sprintf(__("New backup generated %s"), $backup_name);
279
+		}
280 280
 
281
-        $body = sprintf(__("Generated Backup Size: %s"), size_format($this->filesystem->get_backup_size($backup_name)));
282
-        $body .= "<br /><br />";
281
+		$body = sprintf(__("Generated Backup Size: %s"), size_format($this->filesystem->get_backup_size($backup_name)));
282
+		$body .= "<br /><br />";
283 283
 
284
-        if (isset($additional['lines_total'])) {
285
-            $body .= sprintf(__("Total files added: %s"), $additional['lines_total']);
286
-            $body .= "<br /><br />";
287
-        }
284
+		if (isset($additional['lines_total'])) {
285
+			$body .= sprintf(__("Total files added: %s"), $additional['lines_total']);
286
+			$body .= "<br /><br />";
287
+		}
288 288
 
289
-        $backup_parts = $this->filesystem->get_multipart_files($backup_name);
289
+		$backup_parts = $this->filesystem->get_multipart_files($backup_name);
290 290
 
291
-        if (!$backups_counter = sizeof($backup_parts)) {
292
-            $backups_counter = 1;
293
-        }
291
+		if (!$backups_counter = sizeof($backup_parts)) {
292
+			$backups_counter = 1;
293
+		}
294 294
 
295
-        $body .= sprintf(__("Backup Parts: %s"), $backups_counter);
296
-        $body .= "<br />";
295
+		$body .= sprintf(__("Backup Parts: %s"), $backups_counter);
296
+		$body .= "<br />";
297 297
 
298
-        if (sizeof($backup_parts)) {
299
-            $body .= implode("<br />", $backup_parts);
300
-            $body .= "<br />";
301
-        }
298
+		if (sizeof($backup_parts)) {
299
+			$body .= implode("<br />", $backup_parts);
300
+			$body .= "<br />";
301
+		}
302 302
 
303
-        $body .= "<br />";
303
+		$body .= "<br />";
304 304
 
305
-        $body .= sprintf(__("Backup Site Url: %s"), get_site_url());
306
-        $body .= "<br />";
305
+		$body .= sprintf(__("Backup Site Url: %s"), get_site_url());
306
+		$body .= "<br />";
307 307
 
308
-        if (isset($params['backup_params']->backup_comments)) {
309
-            $body .= __("Backup Comments: ").$params['backup_params']->backup_comments;
310
-            $body .= "<br /><br />";
311
-        }
308
+		if (isset($params['backup_params']->backup_comments)) {
309
+			$body .= __("Backup Comments: ").$params['backup_params']->backup_comments;
310
+			$body .= "<br /><br />";
311
+		}
312 312
 
313
-        if ($this->xcloner_settings->get_xcloner_option('xcloner_enable_log')) {
314
-            $body .= __("Latest 50 Log Lines: ")."<br />".implode("<br />\n",
315
-                                                                        $this->logger->getLastDebugLines(50));
316
-        }
313
+		if ($this->xcloner_settings->get_xcloner_option('xcloner_enable_log')) {
314
+			$body .= __("Latest 50 Log Lines: ")."<br />".implode("<br />\n",
315
+																		$this->logger->getLastDebugLines(50));
316
+		}
317 317
 
318
-        $attachments = $this->filesystem->get_backup_attachments();
318
+		$attachments = $this->filesystem->get_backup_attachments();
319 319
 
320
-        $attachments_archive = $this->xcloner_settings->get_xcloner_tmp_path().DS."info.tgz";
320
+		$attachments_archive = $this->xcloner_settings->get_xcloner_tmp_path().DS."info.tgz";
321 321
 
322
-        $tar = new Tar();
323
-        $tar->create($attachments_archive);
322
+		$tar = new Tar();
323
+		$tar->create($attachments_archive);
324 324
 
325
-        foreach ($attachments as $key => $file) {
326
-            $tar->addFile($file, basename($file));
327
-        }
328
-        $tar->close();
325
+		foreach ($attachments as $key => $file) {
326
+			$tar->addFile($file, basename($file));
327
+		}
328
+		$tar->close();
329 329
 
330
-        $this->logger->info(sprintf("Sending backup notification to %s", $to));
330
+		$this->logger->info(sprintf("Sending backup notification to %s", $to));
331 331
 
332
-        $admin_email = get_option("admin_email");
332
+		$admin_email = get_option("admin_email");
333 333
 
334
-        $headers = array('Content-Type: text/html; charset=UTF-8', 'From: '.$from.' <'.$admin_email.'>');
334
+		$headers = array('Content-Type: text/html; charset=UTF-8', 'From: '.$from.' <'.$admin_email.'>');
335 335
 
336
-        $return = wp_mail($to, $subject, $body, $headers, array($attachments_archive));
336
+		$return = wp_mail($to, $subject, $body, $headers, array($attachments_archive));
337 337
 
338
-        return $return;
339
-    }
338
+		return $return;
339
+	}
340 340
 
341
-    /*
341
+	/*
342 342
      *
343 343
      * Incremental Backup method
344 344
      *
345 345
      */
346
-    public function start_incremental_backup($backup_params, $extra_params, $init)
347
-    {
348
-        $return = array();
349
-
350
-        if (!isset($extra_params['backup_part'])) {
351
-            $extra_params['backup_part'] = 0;
352
-        }
353
-
354
-        $return['extra']['backup_part'] = $extra_params['backup_part'];
346
+	public function start_incremental_backup($backup_params, $extra_params, $init)
347
+	{
348
+		$return = array();
355 349
 
356
-        if (isset($extra_params['backup_archive_name'])) {
357
-            $this->set_archive_name($extra_params['backup_archive_name'], $return['extra']['backup_part']);
358
-        } else {
359
-            $this->set_archive_name($backup_params['backup_name']);
360
-        }
350
+		if (!isset($extra_params['backup_part'])) {
351
+			$extra_params['backup_part'] = 0;
352
+		}
361 353
 
362
-        if (!$this->get_archive_name()) {
363
-            $this->set_archive_name();
364
-        }
354
+		$return['extra']['backup_part'] = $extra_params['backup_part'];
365 355
 
366
-        $this->backup_archive = new Tar();
367
-        $this->backup_archive->setCompression($this->compression_level);
356
+		if (isset($extra_params['backup_archive_name'])) {
357
+			$this->set_archive_name($extra_params['backup_archive_name'], $return['extra']['backup_part']);
358
+		} else {
359
+			$this->set_archive_name($backup_params['backup_name']);
360
+		}
368 361
 
369
-        $archive_info = $this->filesystem->get_storage_path_file_info($this->get_archive_name_with_extension());
362
+		if (!$this->get_archive_name()) {
363
+			$this->set_archive_name();
364
+		}
370 365
 
371
-        if ($init) {
372
-            $this->logger->info(sprintf(__("Initializing the backup archive %s"), $this->get_archive_name()));
366
+		$this->backup_archive = new Tar();
367
+		$this->backup_archive->setCompression($this->compression_level);
373 368
 
374
-            $this->backup_archive->create($archive_info->getPath().DS.$archive_info->getFilename());
369
+		$archive_info = $this->filesystem->get_storage_path_file_info($this->get_archive_name_with_extension());
375 370
 
376
-            $return['extra']['backup_init'] = 1;
371
+		if ($init) {
372
+			$this->logger->info(sprintf(__("Initializing the backup archive %s"), $this->get_archive_name()));
377 373
 
378
-        } else {
379
-            $this->logger->info(sprintf(__("Opening for append the backup archive %s"), $this->get_archive_name()));
374
+			$this->backup_archive->create($archive_info->getPath().DS.$archive_info->getFilename());
380 375
 
381
-            $this->backup_archive->openForAppend($archive_info->getPath().DS.$archive_info->getFilename());
376
+			$return['extra']['backup_init'] = 1;
382 377
 
383
-            $return['extra']['backup_init'] = 0;
378
+		} else {
379
+			$this->logger->info(sprintf(__("Opening for append the backup archive %s"), $this->get_archive_name()));
384 380
 
385
-        }
381
+			$this->backup_archive->openForAppend($archive_info->getPath().DS.$archive_info->getFilename());
386 382
 
387
-        $return['extra']['backup_archive_name'] = $this->get_archive_name();
388
-        $return['extra']['backup_archive_name_full'] = $this->get_archive_name_with_extension();
383
+			$return['extra']['backup_init'] = 0;
389 384
 
390
-        if (!isset($extra_params['start_at_line'])) {
391
-            $extra_params['start_at_line'] = 0;
392
-        }
385
+		}
393 386
 
394
-        if (!isset($extra_params['start_at_byte'])) {
395
-            $extra_params['start_at_byte'] = 0;
396
-        }
387
+		$return['extra']['backup_archive_name'] = $this->get_archive_name();
388
+		$return['extra']['backup_archive_name_full'] = $this->get_archive_name_with_extension();
397 389
 
398
-        if (!$this->filesystem->get_tmp_filesystem()->has($this->filesystem->get_included_files_handler())) {
399
-            $this->logger->error(sprintf("Missing the includes file handler %s, aborting...",
400
-                $this->filesystem->get_included_files_handler()));
390
+		if (!isset($extra_params['start_at_line'])) {
391
+			$extra_params['start_at_line'] = 0;
392
+		}
401 393
 
402
-            $return['finished'] = 1;
403
-            return $return;
404
-        }
394
+		if (!isset($extra_params['start_at_byte'])) {
395
+			$extra_params['start_at_byte'] = 0;
396
+		}
405 397
 
406
-        $included_files_handler = $this->filesystem->get_included_files_handler(1);
398
+		if (!$this->filesystem->get_tmp_filesystem()->has($this->filesystem->get_included_files_handler())) {
399
+			$this->logger->error(sprintf("Missing the includes file handler %s, aborting...",
400
+				$this->filesystem->get_included_files_handler()));
407 401
 
408
-        $file = new SplFileObject($included_files_handler);
402
+			$return['finished'] = 1;
403
+			return $return;
404
+		}
409 405
 
410
-        $file->seek(PHP_INT_MAX);
406
+		$included_files_handler = $this->filesystem->get_included_files_handler(1);
411 407
 
412
-        $return['extra']['lines_total'] = ($file->key() - 1);
408
+		$file = new SplFileObject($included_files_handler);
413 409
 
414
-        //we skip the first CSV line with headers
415
-        if (!$extra_params['start_at_line']) {
416
-            $file->seek(1);
417
-        } else {
418
-            $file->seek($extra_params['start_at_line'] + 1);
419
-        }
410
+		$file->seek(PHP_INT_MAX);
420 411
 
421
-        $this->processed_size_bytes = 0;
412
+		$return['extra']['lines_total'] = ($file->key() - 1);
422 413
 
423
-        $counter = 0;
414
+		//we skip the first CSV line with headers
415
+		if (!$extra_params['start_at_line']) {
416
+			$file->seek(1);
417
+		} else {
418
+			$file->seek($extra_params['start_at_line'] + 1);
419
+		}
424 420
 
425
-        $start_byte = $extra_params['start_at_byte'];
421
+		$this->processed_size_bytes = 0;
426 422
 
427
-        $byte_limit = 0;
423
+		$counter = 0;
428 424
 
429
-        while (!$file->eof() and $counter <= $this->files_to_process_per_request) {
430
-            $current_line_str = $file->current();
425
+		$start_byte = $extra_params['start_at_byte'];
431 426
 
432
-            $line = str_getcsv($current_line_str);
427
+		$byte_limit = 0;
433 428
 
434
-            $relative_path = stripslashes($line[0]);
429
+		while (!$file->eof() and $counter <= $this->files_to_process_per_request) {
430
+			$current_line_str = $file->current();
435 431
 
436
-            $start_filesystem = "start_filesystem";
432
+			$line = str_getcsv($current_line_str);
437 433
 
438
-            if (isset($line[4])) {
439
-                $start_filesystem = $line[4];
440
-            }
434
+			$relative_path = stripslashes($line[0]);
441 435
 
442
-            //$adapter = $this->filesystem->get_adapter($start_filesystem);
436
+			$start_filesystem = "start_filesystem";
443 437
 
444
-            if (!$relative_path || !$this->filesystem->get_filesystem($start_filesystem)->has($relative_path)) {
445
-                if ($relative_path != "") {
446
-                    $this->logger->error(sprintf("Could not add file %b to backup archive, file not found",
447
-                        $relative_path));
448
-                }
438
+			if (isset($line[4])) {
439
+				$start_filesystem = $line[4];
440
+			}
449 441
 
450
-                $extra_params['start_at_line']++;
451
-                $file->next();
452
-                continue;
453
-            }
442
+			//$adapter = $this->filesystem->get_adapter($start_filesystem);
454 443
 
455
-            $file_info = $this->filesystem->get_filesystem($start_filesystem)->getMetadata($relative_path);
444
+			if (!$relative_path || !$this->filesystem->get_filesystem($start_filesystem)->has($relative_path)) {
445
+				if ($relative_path != "") {
446
+					$this->logger->error(sprintf("Could not add file %b to backup archive, file not found",
447
+						$relative_path));
448
+				}
456 449
 
457
-            if (!isset($file_info['size'])) {
458
-                $file_info['size'] = 0;
459
-            }
450
+				$extra_params['start_at_line']++;
451
+				$file->next();
452
+				continue;
453
+			}
460 454
 
461
-            if ($start_filesystem == "tmp_filesystem") {
462
-                $file_info['archive_prefix_path'] = $this->xcloner_settings->get_xcloner_tmp_path_suffix();
463
-            }
455
+			$file_info = $this->filesystem->get_filesystem($start_filesystem)->getMetadata($relative_path);
464 456
 
465
-            $byte_limit = (int)$this->file_size_per_request_limit / 512;
457
+			if (!isset($file_info['size'])) {
458
+				$file_info['size'] = 0;
459
+			}
466 460
 
467
-            $append = 0;
461
+			if ($start_filesystem == "tmp_filesystem") {
462
+				$file_info['archive_prefix_path'] = $this->xcloner_settings->get_xcloner_tmp_path_suffix();
463
+			}
468 464
 
469
-            if ($file_info['size'] > $byte_limit * 512 or $start_byte) {
470
-                $append = 1;
471
-            }
465
+			$byte_limit = (int)$this->file_size_per_request_limit / 512;
472 466
 
473
-            if (!isset($return['extra']['backup_size'])) {
474
-                $return['extra']['backup_size'] = 0;
475
-            }
467
+			$append = 0;
476 468
 
477
-            $return['extra']['backup_size'] = $archive_info->getSize();
469
+			if ($file_info['size'] > $byte_limit * 512 or $start_byte) {
470
+				$append = 1;
471
+			}
478 472
 
479
-            $estimated_new_size = $return['extra']['backup_size'] + $file_info['size'];
473
+			if (!isset($return['extra']['backup_size'])) {
474
+				$return['extra']['backup_size'] = 0;
475
+			}
480 476
 
481
-            //we create a new backup part if we reach the Split Achive Limit
482
-            if ($this->xcloner_split_backup_limit and ($estimated_new_size > $this->xcloner_split_backup_limit) and (!$start_byte)) {
483
-                $this->logger->info(sprintf("Backup size limit %s bytes reached, file add estimate %s, attempt to create a new archive ",
484
-                    $this->xcloner_split_backup_limit, $estimated_new_size));
485
-                list($archive_info, $return['extra']['backup_part']) = $this->create_new_backup_part($return['extra']['backup_part']);
477
+			$return['extra']['backup_size'] = $archive_info->getSize();
486 478
 
487
-                if ($file_info['size'] > $this->xcloner_split_backup_limit) {
488
-                    $this->logger->info(sprintf("Excluding %s file as it's size(%s) is bigger than the backup split limit of %s and it won't fit a single backup file",
489
-                        $file_info['path'], $file_info['size'], $this->xcloner_split_backup_limit));
490
-                    $extra_params['start_at_line']++;
491
-                }
479
+			$estimated_new_size = $return['extra']['backup_size'] + $file_info['size'];
492 480
 
493
-                $return['extra']['start_at_line'] = $extra_params['start_at_line'];
494
-                $return['extra']['start_at_byte'] = 0;
481
+			//we create a new backup part if we reach the Split Achive Limit
482
+			if ($this->xcloner_split_backup_limit and ($estimated_new_size > $this->xcloner_split_backup_limit) and (!$start_byte)) {
483
+				$this->logger->info(sprintf("Backup size limit %s bytes reached, file add estimate %s, attempt to create a new archive ",
484
+					$this->xcloner_split_backup_limit, $estimated_new_size));
485
+				list($archive_info, $return['extra']['backup_part']) = $this->create_new_backup_part($return['extra']['backup_part']);
495 486
 
496
-                $return['finished'] = 0;
487
+				if ($file_info['size'] > $this->xcloner_split_backup_limit) {
488
+					$this->logger->info(sprintf("Excluding %s file as it's size(%s) is bigger than the backup split limit of %s and it won't fit a single backup file",
489
+						$file_info['path'], $file_info['size'], $this->xcloner_split_backup_limit));
490
+					$extra_params['start_at_line']++;
491
+				}
497 492
 
498
-                return $return;
499
-            }
493
+				$return['extra']['start_at_line'] = $extra_params['start_at_line'];
494
+				$return['extra']['start_at_byte'] = 0;
500 495
 
501
-            list($bytes_wrote, $last_position) = $this->add_file_to_archive($file_info, $start_byte, $byte_limit,
502
-                $append, $start_filesystem);
503
-            $this->processed_size_bytes += $bytes_wrote;
496
+				$return['finished'] = 0;
504 497
 
505
-            //echo" - processed ".$this->processed_size_bytes." bytes ".$this->file_size_per_request_limit." last_position:".$last_position." \n";
506
-            $return['extra']['processed_file'] = $file_info['path'];
507
-            $return['extra']['processed_file_size'] = $file_info['size'];
508
-            $return['extra']['backup_size'] = $archive_info->getSize();
498
+				return $return;
499
+			}
509 500
 
510
-            if ($last_position > 0) {
511
-                $start_byte = $last_position;
512
-            } else {
513
-                $extra_params['start_at_line']++;
514
-                $file->next();
515
-                $start_byte = 0;
516
-                $counter++;
517
-            }
501
+			list($bytes_wrote, $last_position) = $this->add_file_to_archive($file_info, $start_byte, $byte_limit,
502
+				$append, $start_filesystem);
503
+			$this->processed_size_bytes += $bytes_wrote;
504
+
505
+			//echo" - processed ".$this->processed_size_bytes." bytes ".$this->file_size_per_request_limit." last_position:".$last_position." \n";
506
+			$return['extra']['processed_file'] = $file_info['path'];
507
+			$return['extra']['processed_file_size'] = $file_info['size'];
508
+			$return['extra']['backup_size'] = $archive_info->getSize();
509
+
510
+			if ($last_position > 0) {
511
+				$start_byte = $last_position;
512
+			} else {
513
+				$extra_params['start_at_line']++;
514
+				$file->next();
515
+				$start_byte = 0;
516
+				$counter++;
517
+			}
518 518
 
519
-            if ($this->processed_size_bytes >= $this->file_size_per_request_limit) {
520
-                clearstatcache();
521
-                $return['extra']['backup_size'] = $archive_info->getSize();
519
+			if ($this->processed_size_bytes >= $this->file_size_per_request_limit) {
520
+				clearstatcache();
521
+				$return['extra']['backup_size'] = $archive_info->getSize();
522 522
 
523
-                $return['finished'] = 0;
524
-                $return['extra']['start_at_line'] = $extra_params['start_at_line'];
525
-                $return['extra']['start_at_byte'] = $last_position;
526
-                $this->logger->info(sprintf("Reached the maximum %s request data limit, returning response",
527
-                    $this->file_size_per_request_limit));
528
-                return $return;
529
-            }
530
-        }
523
+				$return['finished'] = 0;
524
+				$return['extra']['start_at_line'] = $extra_params['start_at_line'];
525
+				$return['extra']['start_at_byte'] = $last_position;
526
+				$this->logger->info(sprintf("Reached the maximum %s request data limit, returning response",
527
+					$this->file_size_per_request_limit));
528
+				return $return;
529
+			}
530
+		}
531 531
 
532
-        if (!$file->eof()) {
533
-            clearstatcache();
534
-            $return['extra']['backup_size'] = $archive_info->getSize();
532
+		if (!$file->eof()) {
533
+			clearstatcache();
534
+			$return['extra']['backup_size'] = $archive_info->getSize();
535 535
 
536
-            $return['finished'] = 0;
537
-            $return['extra']['start_at_line'] = $extra_params['start_at_line'];
538
-            $return['extra']['start_at_byte'] = $last_position;
539
-            $this->logger->info(sprintf("We have reached the maximum files to process per request limit of %s, returning response",
540
-                $this->files_to_process_per_request));
536
+			$return['finished'] = 0;
537
+			$return['extra']['start_at_line'] = $extra_params['start_at_line'];
538
+			$return['extra']['start_at_byte'] = $last_position;
539
+			$this->logger->info(sprintf("We have reached the maximum files to process per request limit of %s, returning response",
540
+				$this->files_to_process_per_request));
541 541
 
542
-            return $return;
543
-        }
542
+			return $return;
543
+		}
544 544
 
545
-        //close the backup archive by adding 2*512 blocks of zero bytes
546
-        $this->logger->info(sprintf("Closing the backup archive %s with 2*512 zero bytes blocks.",
547
-            $this->get_archive_name_with_extension()));
548
-        $this->backup_archive->close();
545
+		//close the backup archive by adding 2*512 blocks of zero bytes
546
+		$this->logger->info(sprintf("Closing the backup archive %s with 2*512 zero bytes blocks.",
547
+			$this->get_archive_name_with_extension()));
548
+		$this->backup_archive->close();
549 549
 
550
-        /**
551
-         * XCloner HOOK backup_archive_finished.
552
-         *
553
-         * This will get triggered when a backup archive is finished writing.
554
-         */
555
-        //do_action('backup_archive_finished', $this->backup_archive, $this);
550
+		/**
551
+		 * XCloner HOOK backup_archive_finished.
552
+		 *
553
+		 * This will get triggered when a backup archive is finished writing.
554
+		 */
555
+		//do_action('backup_archive_finished', $this->backup_archive, $this);
556 556
 
557
-        //updating archive_info
558
-        $archive_info = $this->filesystem->get_storage_path_file_info($this->get_archive_name_with_extension());
557
+		//updating archive_info
558
+		$archive_info = $this->filesystem->get_storage_path_file_info($this->get_archive_name_with_extension());
559 559
 
560
-        if ($return['extra']['backup_part']) {
561
-            $this->write_multipart_file($this->get_archive_name_with_extension());
562
-        }
560
+		if ($return['extra']['backup_part']) {
561
+			$this->write_multipart_file($this->get_archive_name_with_extension());
562
+		}
563 563
 
564
-        $return['extra']['start_at_line'] = $extra_params['start_at_line'] - 1;
564
+		$return['extra']['start_at_line'] = $extra_params['start_at_line'] - 1;
565 565
 
566
-        if (isset($file_info)) {
567
-            $return['extra']['processed_file'] = $file_info['path'];
568
-            $return['extra']['processed_file_size'] = $file_info['size'];
569
-        }
566
+		if (isset($file_info)) {
567
+			$return['extra']['processed_file'] = $file_info['path'];
568
+			$return['extra']['processed_file_size'] = $file_info['size'];
569
+		}
570 570
 
571
-        clearstatcache();
572
-        $return['extra']['backup_size'] = $archive_info->getSize();
571
+		clearstatcache();
572
+		$return['extra']['backup_size'] = $archive_info->getSize();
573 573
 
574
-        $return['finished'] = 1;
575
-        return $return;
576
-    }
574
+		$return['finished'] = 1;
575
+		return $return;
576
+	}
577 577
 
578
-    /*
578
+	/*
579 579
      *
580 580
      * Write multipart file components
581 581
      *
582 582
      */
583
-    private function write_multipart_file($path = "")
584
-    {
585
-        if (!$path) {
586
-            $path = $this->get_archive_name_with_extension();
587
-        }
583
+	private function write_multipart_file($path = "")
584
+	{
585
+		if (!$path) {
586
+			$path = $this->get_archive_name_with_extension();
587
+		}
588 588
 
589
-        $file = $this->filesystem->get_filesystem("storage_filesystem_append")->getMetadata($path);
590
-        //print_r($file_info);
591
-        $line = '"'.$file['path'].'","'.$file['timestamp'].'","'.$file['size'].'"'.PHP_EOL;
589
+		$file = $this->filesystem->get_filesystem("storage_filesystem_append")->getMetadata($path);
590
+		//print_r($file_info);
591
+		$line = '"'.$file['path'].'","'.$file['timestamp'].'","'.$file['size'].'"'.PHP_EOL;
592 592
 
593 593
 
594
-        $this->filesystem->get_filesystem("storage_filesystem_append")
595
-            ->write($this->get_archive_name_multipart(), $line);
596
-    }
594
+		$this->filesystem->get_filesystem("storage_filesystem_append")
595
+			->write($this->get_archive_name_multipart(), $line);
596
+	}
597 597
 
598
-    /*
598
+	/*
599 599
      *
600 600
      * Create a new backup part
601 601
      *
602 602
      */
603
-    private function create_new_backup_part($part = 0)
604
-    {
605
-        //close the backup archive by adding 2*512 blocks of zero bytes
606
-        $this->logger->info(sprintf("Closing the backup archive %s with 2*512 zero bytes blocks.",
607
-            $this->get_archive_name_with_extension()));
608
-        $this->backup_archive->close();
609
-
610
-        if (!$part) {
611
-            $old_name = $this->get_archive_name_with_extension();
612
-            $this->set_archive_name($this->get_archive_name(), ++$part);
613
-            $this->rename_archive($old_name, $this->get_archive_name_with_extension());
614
-
615
-            if ($this->filesystem->get_storage_filesystem()->has($this->get_archive_name_multipart())) {
616
-                $this->filesystem->get_storage_filesystem()->delete($this->get_archive_name_multipart());
617
-            }
603
+	private function create_new_backup_part($part = 0)
604
+	{
605
+		//close the backup archive by adding 2*512 blocks of zero bytes
606
+		$this->logger->info(sprintf("Closing the backup archive %s with 2*512 zero bytes blocks.",
607
+			$this->get_archive_name_with_extension()));
608
+		$this->backup_archive->close();
609
+
610
+		if (!$part) {
611
+			$old_name = $this->get_archive_name_with_extension();
612
+			$this->set_archive_name($this->get_archive_name(), ++$part);
613
+			$this->rename_archive($old_name, $this->get_archive_name_with_extension());
614
+
615
+			if ($this->filesystem->get_storage_filesystem()->has($this->get_archive_name_multipart())) {
616
+				$this->filesystem->get_storage_filesystem()->delete($this->get_archive_name_multipart());
617
+			}
618 618
 
619
-            $this->write_multipart_file($this->get_archive_name_with_extension());
619
+			$this->write_multipart_file($this->get_archive_name_with_extension());
620 620
 
621
-        } else {
622
-            $this->logger->info(sprintf("Creating new multipart info file %s",
623
-                $this->get_archive_name_with_extension()));
624
-            $this->write_multipart_file($this->get_archive_name_with_extension());
625
-        }
621
+		} else {
622
+			$this->logger->info(sprintf("Creating new multipart info file %s",
623
+				$this->get_archive_name_with_extension()));
624
+			$this->write_multipart_file($this->get_archive_name_with_extension());
625
+		}
626 626
 
627
-        $this->set_archive_name($this->get_archive_name(), ++$part);
627
+		$this->set_archive_name($this->get_archive_name(), ++$part);
628 628
 
629
-        $this->logger->info(sprintf("Creating new backup archive part %s", $this->get_archive_name_with_extension()));
629
+		$this->logger->info(sprintf("Creating new backup archive part %s", $this->get_archive_name_with_extension()));
630 630
 
631
-        $this->backup_archive = new Tar();
632
-        $this->backup_archive->setCompression($this->compression_level);
633
-        $archive_info = $this->filesystem->get_storage_path_file_info($this->get_archive_name_with_extension());
634
-        $this->backup_archive->create($archive_info->getPath().DS.$archive_info->getFilename());
631
+		$this->backup_archive = new Tar();
632
+		$this->backup_archive->setCompression($this->compression_level);
633
+		$archive_info = $this->filesystem->get_storage_path_file_info($this->get_archive_name_with_extension());
634
+		$this->backup_archive->create($archive_info->getPath().DS.$archive_info->getFilename());
635 635
 
636
-        return array($archive_info, $part);
636
+		return array($archive_info, $part);
637 637
 
638
-    }
638
+	}
639 639
 
640
-    /*
640
+	/*
641 641
      *
642 642
      * Add file to archive
643 643
      *
644 644
      */
645 645
 
646
-    /**
647
-     * @param integer $append
648
-     */
649
-    public function add_file_to_archive($file_info, $start_at_byte, $byte_limit = 0, $append, $filesystem)
650
-    {
646
+	/**
647
+	 * @param integer $append
648
+	 */
649
+	public function add_file_to_archive($file_info, $start_at_byte, $byte_limit = 0, $append, $filesystem)
650
+	{
651 651
 
652
-        $start_adapter = $this->filesystem->get_adapter($filesystem);
653
-        $start_filesystem = $this->filesystem->get_adapter($filesystem);
652
+		$start_adapter = $this->filesystem->get_adapter($filesystem);
653
+		$start_filesystem = $this->filesystem->get_adapter($filesystem);
654 654
 
655
-        if (!$file_info['path']) {
656
-            return;
657
-        }
655
+		if (!$file_info['path']) {
656
+			return;
657
+		}
658 658
 
659
-        if (isset($file_info['archive_prefix_path'])) {
660
-            $file_info['target_path'] = $file_info['archive_prefix_path']."/".$file_info['path'];
661
-        } else {
662
-            $file_info['target_path'] = $file_info['path'];
663
-        }
659
+		if (isset($file_info['archive_prefix_path'])) {
660
+			$file_info['target_path'] = $file_info['archive_prefix_path']."/".$file_info['path'];
661
+		} else {
662
+			$file_info['target_path'] = $file_info['path'];
663
+		}
664 664
 
665
-        $last_position = $start_at_byte;
665
+		$last_position = $start_at_byte;
666 666
 
667
-        //$start_adapter = $this->filesystem->get_start_adapter();
667
+		//$start_adapter = $this->filesystem->get_start_adapter();
668 668
 
669
-        if (!$append) {
670
-            $bytes_wrote = $file_info['size'];
671
-            $this->logger->info(sprintf("Adding %s bytes of file %s to archive %s ", $bytes_wrote,
672
-                $file_info['target_path'], $this->get_archive_name_with_extension()));
673
-            $this->backup_archive->addFile($start_adapter->applyPathPrefix($file_info['path']),
674
-                $file_info['target_path']);
675
-        } else {
676
-            $tmp_file = md5($file_info['path']);
669
+		if (!$append) {
670
+			$bytes_wrote = $file_info['size'];
671
+			$this->logger->info(sprintf("Adding %s bytes of file %s to archive %s ", $bytes_wrote,
672
+				$file_info['target_path'], $this->get_archive_name_with_extension()));
673
+			$this->backup_archive->addFile($start_adapter->applyPathPrefix($file_info['path']),
674
+				$file_info['target_path']);
675
+		} else {
676
+			$tmp_file = md5($file_info['path']);
677 677
 
678
-            //we isolate file to tmp if we are at byte 0, the starting point of file reading
679
-            if (!$start_at_byte) {
680
-                $this->logger->info(sprintf("Copying %s file to tmp filesystem file %s to prevent reading changes",
681
-                    $file_info['path'], $tmp_file));
682
-                $file_stream = $start_filesystem->readStream($file_info['path']);
678
+			//we isolate file to tmp if we are at byte 0, the starting point of file reading
679
+			if (!$start_at_byte) {
680
+				$this->logger->info(sprintf("Copying %s file to tmp filesystem file %s to prevent reading changes",
681
+					$file_info['path'], $tmp_file));
682
+				$file_stream = $start_filesystem->readStream($file_info['path']);
683 683
 
684
-                if (is_resource($file_stream['stream'])) {
685
-                    $this->filesystem->get_tmp_filesystem()->writeStream($tmp_file, $file_stream['stream']);
686
-                }
687
-            }
684
+				if (is_resource($file_stream['stream'])) {
685
+					$this->filesystem->get_tmp_filesystem()->writeStream($tmp_file, $file_stream['stream']);
686
+				}
687
+			}
688 688
 
689
-            if ($this->filesystem->get_tmp_filesystem()->has($tmp_file)) {
690
-                $is_tmp = 1;
691
-                $last_position = $this->backup_archive->appendFileData($this->filesystem->get_tmp_filesystem_adapter()
692
-                                                        ->applyPathPrefix($tmp_file),
693
-                    $file_info['target_path'], $start_at_byte, $byte_limit);
694
-            } else {
695
-                $is_tmp = 0;
696
-                $last_position = $this->backup_archive->appendFileData($start_adapter->applyPathPrefix($file_info['path']),
697
-                    $file_info['target_path'], $start_at_byte, $byte_limit);
698
-            }
689
+			if ($this->filesystem->get_tmp_filesystem()->has($tmp_file)) {
690
+				$is_tmp = 1;
691
+				$last_position = $this->backup_archive->appendFileData($this->filesystem->get_tmp_filesystem_adapter()
692
+														->applyPathPrefix($tmp_file),
693
+					$file_info['target_path'], $start_at_byte, $byte_limit);
694
+			} else {
695
+				$is_tmp = 0;
696
+				$last_position = $this->backup_archive->appendFileData($start_adapter->applyPathPrefix($file_info['path']),
697
+					$file_info['target_path'], $start_at_byte, $byte_limit);
698
+			}
699 699
 
700 700
 
701
-            if ($last_position == -1) {
702
-                $bytes_wrote = $file_info['size'] - $start_at_byte;
703
-            } else {
704
-                $bytes_wrote = $last_position - $start_at_byte;
705
-            }
701
+			if ($last_position == -1) {
702
+				$bytes_wrote = $file_info['size'] - $start_at_byte;
703
+			} else {
704
+				$bytes_wrote = $last_position - $start_at_byte;
705
+			}
706 706
 
707 707
 
708
-            if ($is_tmp) {
709
-                $this->logger->info(sprintf("Appended %s bytes, starting position %s, of tmp file %s (%s) to archive %s ",
710
-                    $bytes_wrote, $start_at_byte, $tmp_file, $file_info['target_path'], $this->get_archive_name()));
711
-            } else {
712
-                $this->logger->info(sprintf("Appended %s bytes, starting position %s, of original file %s to archive %s ",
713
-                    $bytes_wrote, $start_at_byte, $file_info['target_path'], $tmp_file, $this->get_archive_name()));
714
-            }
708
+			if ($is_tmp) {
709
+				$this->logger->info(sprintf("Appended %s bytes, starting position %s, of tmp file %s (%s) to archive %s ",
710
+					$bytes_wrote, $start_at_byte, $tmp_file, $file_info['target_path'], $this->get_archive_name()));
711
+			} else {
712
+				$this->logger->info(sprintf("Appended %s bytes, starting position %s, of original file %s to archive %s ",
713
+					$bytes_wrote, $start_at_byte, $file_info['target_path'], $tmp_file, $this->get_archive_name()));
714
+			}
715 715
 
716
-            //we delete here the isolated tmp file
717
-            if ($last_position == -1) {
718
-                if ($this->filesystem->get_tmp_filesystem_adapter()->has($tmp_file)) {
719
-                    $this->logger->info(sprintf("Deleting %s from the tmp filesystem", $tmp_file));
720
-                    $this->filesystem->get_tmp_filesystem_adapter()->delete($tmp_file);
721
-                }
722
-            }
716
+			//we delete here the isolated tmp file
717
+			if ($last_position == -1) {
718
+				if ($this->filesystem->get_tmp_filesystem_adapter()->has($tmp_file)) {
719
+					$this->logger->info(sprintf("Deleting %s from the tmp filesystem", $tmp_file));
720
+					$this->filesystem->get_tmp_filesystem_adapter()->delete($tmp_file);
721
+				}
722
+			}
723 723
 
724
-        }
724
+		}
725 725
 
726
-        return array($bytes_wrote, $last_position);
727
-    }
726
+		return array($bytes_wrote, $last_position);
727
+	}
728 728
 
729
-    /**
730
-     * Open a TAR archive and put the file cursor at the end for data appending
731
-     *
732
-     * If $file is empty, the tar file will be created in memory
733
-     *
734
-     * @param string $file
735
-     * @throws ArchiveIOException
736
-     */
737
-    /*
729
+	/**
730
+	 * Open a TAR archive and put the file cursor at the end for data appending
731
+	 *
732
+	 * If $file is empty, the tar file will be created in memory
733
+	 *
734
+	 * @param string $file
735
+	 * @throws ArchiveIOException
736
+	 */
737
+	/*
738 738
     public function openForAppend($file = '')
739 739
     {
740 740
         $this->file   = $file;
Please login to merge, or discard this patch.
includes/class-xcloner-file-transfer.php 1 patch
Indentation   +99 added lines, -99 removed lines patch added patch discarded remove patch
@@ -29,137 +29,137 @@
 block discarded – undo
29 29
 class Xcloner_File_Transfer extends Xcloner_File_System
30 30
 {
31 31
 
32
-    /**
33
-     * Target url web address of the restore script
34
-     * @var string
35
-     */
36
-    private $target_url;
37
-    /**
38
-     * Transfer data limit in bytes
39
-     * @var int
40
-     */
41
-    private $transfer_limit = 1048576; //bytes 1MB= 1048576 300KB = 358400
32
+	/**
33
+	 * Target url web address of the restore script
34
+	 * @var string
35
+	 */
36
+	private $target_url;
37
+	/**
38
+	 * Transfer data limit in bytes
39
+	 * @var int
40
+	 */
41
+	private $transfer_limit = 1048576; //bytes 1MB= 1048576 300KB = 358400
42 42
 
43 43
 
44
-    /**
45
-     * @param $target_url
46
-     *
47
-     * @return mixed
48
-     */
49
-    public function set_target($target_url)
50
-    {
51
-        return $this->target_url = $target_url;
52
-    }
44
+	/**
45
+	 * @param $target_url
46
+	 *
47
+	 * @return mixed
48
+	 */
49
+	public function set_target($target_url)
50
+	{
51
+		return $this->target_url = $target_url;
52
+	}
53 53
 
54
-    /**
55
-     * @return string
56
-     */
57
-    public function get_target()
58
-    {
59
-        return $this->target_url;
60
-    }
54
+	/**
55
+	 * @return string
56
+	 */
57
+	public function get_target()
58
+	{
59
+		return $this->target_url;
60
+	}
61 61
 
62 62
 
63
-    /**
64
-     * @param $file
65
-     * @param int $start
66
-     * @param string $hash
67
-     *
68
-     * @return bool|int
69
-     * @throws Exception
70
-     */
71
-    public function transfer_file($file, $start = 0, $hash = "")
72
-    {
73
-        if (!$this->target_url) {
74
-            throw new Exception("Please setup a target url for upload");
75
-        }
63
+	/**
64
+	 * @param $file
65
+	 * @param int $start
66
+	 * @param string $hash
67
+	 *
68
+	 * @return bool|int
69
+	 * @throws Exception
70
+	 */
71
+	public function transfer_file($file, $start = 0, $hash = "")
72
+	{
73
+		if (!$this->target_url) {
74
+			throw new Exception("Please setup a target url for upload");
75
+		}
76 76
 
77 77
 
78
-        $fp = $this->get_storage_filesystem()->readStream($file);
78
+		$fp = $this->get_storage_filesystem()->readStream($file);
79 79
 
80
-        fseek($fp, $start, SEEK_SET);
80
+		fseek($fp, $start, SEEK_SET);
81 81
 
82
-        $binary_data = fread($fp, $this->transfer_limit);
82
+		$binary_data = fread($fp, $this->transfer_limit);
83 83
 
84
-        $tmp_filename = "xcloner_upload_".substr(md5(time()), 0, 5);
84
+		$tmp_filename = "xcloner_upload_".substr(md5(time()), 0, 5);
85 85
 
86
-        $this->get_tmp_filesystem()->write($tmp_filename, $binary_data);
86
+		$this->get_tmp_filesystem()->write($tmp_filename, $binary_data);
87 87
 
88
-        $tmp_file_path = $this->get_tmp_filesystem_adapter()->applyPathPrefix($tmp_filename);
88
+		$tmp_file_path = $this->get_tmp_filesystem_adapter()->applyPathPrefix($tmp_filename);
89 89
 
90
-        $send_array = array();
90
+		$send_array = array();
91 91
 
92
-        $send_array['file'] = $file;
93
-        $send_array['start'] = $start;
94
-        $send_array['xcloner_action'] = "write_file";
95
-        $send_array['hash'] = $hash;
96
-        #$send_array['blob'] 	= $binary_data;
97
-        $send_array['blob'] = $this->curl_file_create($tmp_file_path, 'application/x-binary', $tmp_filename);
92
+		$send_array['file'] = $file;
93
+		$send_array['start'] = $start;
94
+		$send_array['xcloner_action'] = "write_file";
95
+		$send_array['hash'] = $hash;
96
+		#$send_array['blob'] 	= $binary_data;
97
+		$send_array['blob'] = $this->curl_file_create($tmp_file_path, 'application/x-binary', $tmp_filename);
98 98
 
99
-        //$data = http_build_query($send_array);
99
+		//$data = http_build_query($send_array);
100 100
 
101
-        $this->get_logger()->info(sprintf("Sending curl request to %s with %s data of file %s starting position %s using temporary file %s",
102
-            $this->target_url, $this->transfer_limit, $file, $start, $tmp_filename));
101
+		$this->get_logger()->info(sprintf("Sending curl request to %s with %s data of file %s starting position %s using temporary file %s",
102
+			$this->target_url, $this->transfer_limit, $file, $start, $tmp_filename));
103 103
 
104 104
 
105
-        $ch = curl_init();
106
-        curl_setopt($ch, CURLOPT_URL, $this->target_url);
105
+		$ch = curl_init();
106
+		curl_setopt($ch, CURLOPT_URL, $this->target_url);
107 107
 
108
-        curl_setopt($ch, CURLOPT_POST, 1);
109
-        //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
110
-        //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
111
-        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
112
-        curl_setopt($ch, CURLOPT_TIMEOUT, 1200);
113
-        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
108
+		curl_setopt($ch, CURLOPT_POST, 1);
109
+		//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
110
+		//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
111
+		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
112
+		curl_setopt($ch, CURLOPT_TIMEOUT, 1200);
113
+		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
114 114
 
115
-        curl_setopt($ch, CURLOPT_POSTFIELDS, $send_array);
116
-        curl_setopt($ch, CURLOPT_VERBOSE, true);
115
+		curl_setopt($ch, CURLOPT_POSTFIELDS, $send_array);
116
+		curl_setopt($ch, CURLOPT_VERBOSE, true);
117 117
 
118
-        $original_result = curl_exec($ch);
118
+		$original_result = curl_exec($ch);
119 119
 
120 120
 
121
-        $this->get_tmp_filesystem()->delete($tmp_filename);
121
+		$this->get_tmp_filesystem()->delete($tmp_filename);
122 122
 
123
-        $result = json_decode($original_result);
123
+		$result = json_decode($original_result);
124 124
 
125
-        if (!$result) {
126
-            throw new Exception("We have received no valid response from the remote host, original message: ".$original_result);
127
-        }
125
+		if (!$result) {
126
+			throw new Exception("We have received no valid response from the remote host, original message: ".$original_result);
127
+		}
128 128
 
129
-        if ($result->status != 200) {
130
-            throw new Exception($result->response);
131
-        }
129
+		if ($result->status != 200) {
130
+			throw new Exception($result->response);
131
+		}
132 132
 
133
-        if (ftell($fp) >= $this->get_storage_filesystem()->getSize($file)) {
134
-            $this->get_logger()->info(sprintf("Upload done for file %s to target url %s, transferred a total of %s bytes",
135
-                $file, $this->target_url, ftell($fp)));
136
-            $this->remove_tmp_filesystem();
133
+		if (ftell($fp) >= $this->get_storage_filesystem()->getSize($file)) {
134
+			$this->get_logger()->info(sprintf("Upload done for file %s to target url %s, transferred a total of %s bytes",
135
+				$file, $this->target_url, ftell($fp)));
136
+			$this->remove_tmp_filesystem();
137 137
 
138
-            return false;
139
-        }
138
+			return false;
139
+		}
140 140
 
141
-        return ftell($fp);
142
-    }
141
+		return ftell($fp);
142
+	}
143 143
 
144
-    /**
145
-     * @param string $filename
146
-     * @param string $mimetype
147
-     * @param string $postname
148
-     *
149
-     * @return CURLFile|string
150
-     */
151
-    private function curl_file_create($filename, $mimetype = '', $postname = '')
152
-    {
153
-        if (!function_exists('curl_file_create')) {
144
+	/**
145
+	 * @param string $filename
146
+	 * @param string $mimetype
147
+	 * @param string $postname
148
+	 *
149
+	 * @return CURLFile|string
150
+	 */
151
+	private function curl_file_create($filename, $mimetype = '', $postname = '')
152
+	{
153
+		if (!function_exists('curl_file_create')) {
154 154
 
155
-            return "@$filename;filename="
156
-                . ($postname ?: basename($filename))
157
-                . ($mimetype ? ";type=$mimetype" : '');
155
+			return "@$filename;filename="
156
+				. ($postname ?: basename($filename))
157
+				. ($mimetype ? ";type=$mimetype" : '');
158 158
 
159
-        } else {
159
+		} else {
160 160
 
161
-            return curl_file_create($filename, $mimetype, $postname);
161
+			return curl_file_create($filename, $mimetype, $postname);
162 162
 
163
-        }
164
-    }
163
+		}
164
+	}
165 165
 }
Please login to merge, or discard this patch.