PluginProbe
WPIDE – File Manager & Code Editor / 3.3
WPIDE – File Manager & Code Editor v3.3
3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 3.4 All 54 releases
wpide / App / Helpers / FileBackup.php

FileBackup.php in WPIDE – File Manager & Code Editor 3.3, at App/Helpers/FileBackup.php

115 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WPIDE\App\Helpers;
3
4 use WPIDE\App\AppConfig;
5 use WPIDE\App\Services\Storage\LocalFileSystem;
6 use WPIDE\App\Utils\Exception;
7 use const WPIDE\Constants\BACKUPS_DIR;
8 use const WPIDE\Constants\BACKUPS_TODAY_DIR;
9 use const WPIDE\Constants\CONTENT_DIR;
10
11 class FileBackup
12 {
13
14 public static function init() {
15
16 EmptyDir::create(BACKUPS_DIR);
17 EmptyDir::create(BACKUPS_TODAY_DIR);
18
19 self::cleanDirs();
20 }
21
22 /**
23 * @throws \Exception
24 */
25 public static function backup($item, $newContent): bool
26 {
27 $dir = $item->dir;
28 $path = $item->path;
29 $name = $item->name;
30
31 $storage = LocalFileSystem::load();
32 $oldStream = $storage->readStream($path);
33
34 if ($oldStream['stream']) {
35
36 $backupStorage = LocalFileSystem::load(BACKUPS_TODAY_DIR);
37
38 $originalFileDir = CONTENT_DIR . AppConfig::get('file.root') . ltrim($dir, '/');
39 $originalFileDir = str_replace(ABSPATH, "", $originalFileDir);
40 $backupFilePath = BACKUPS_TODAY_DIR . "/" .$originalFileDir;
41 $ext = '.'.pathinfo($name, PATHINFO_EXTENSION);
42
43 if (!is_dir($backupFilePath)) {
44 wp_mkdir_p($backupFilePath);
45 }
46
47 if(!$backupStorage->storeStream($originalFileDir, $name, $oldStream['stream'], true)) {
48 return false;
49 }
50 if(!$backupStorage->storeStreamFromContent($originalFileDir, str_replace($ext, "_modified_" . $ext, $name), $newContent, true)) {
51 return false;
52 }
53
54 return true;
55 }
56
57 return false;
58 }
59
60 public static function exists($file): bool
61 {
62
63 $backup_file = WPIDE_Error_Handler::findBackupFile($file);
64 return !empty($backup_file);
65 }
66
67 public static function restore($file) {
68
69 $backup_file = WPIDE_Error_Handler::findBackupFile($file);
70 return WPIDE_Error_Handler::recoverFile($file, $backup_file);
71 }
72
73 protected static function cleanDirs() {
74
75 $backup = LocalFileSystem::load(BACKUPS_DIR);
76
77 try {
78 $collection = $backup->getDirectoryCollection('/');
79
80 $backups_max_days = AppConfig::get('file.backups_max_days', 5);
81
82 $today = date_create_from_format('Y-m-d', date('Y-m-d'));
83
84 $oldBackupFolders = $collection->filter(function($item) use($today, $backups_max_days) {
85
86 if($item['type'] === 'dir') {
87 $folderDate = date_create_from_format('Y-m-d', $item['name']);
88
89 if(!empty($folderDate)) {
90 $days = intval(date_diff($folderDate, $today)->format('%a'));
91 return $days >= $backups_max_days;
92 }
93
94 return true;
95 }
96
97 return false;
98
99 })->map(function($item) {
100
101 return $item['path'];
102
103 })->all();
104
105 foreach($oldBackupFolders as $folder) {
106
107 $backup->deleteDir($folder);
108 }
109
110 } catch (\Exception $e) {}
111
112 }
113
114 }
115