PluginProbe
WPIDE – File Manager & Code Editor / 3.5.5
WPIDE – File Manager & Code Editor v3.5.5
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.5.5, at App/Helpers/FileBackup.php

117 lines 3.1 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\WP_PATH;
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, $root = false): bool
26 {
27 $dir = $item->dir;
28 $path = $item->path;
29 $name = $item->name;
30
31 $root_dir = $root ? '/' : LocalFileSystem::getRootDir();
32
33 $storage = LocalFileSystem::load($root_dir);
34 $oldStream = $storage->readStream($path);
35
36 if ($oldStream['stream']) {
37
38 $backupStorage = LocalFileSystem::load(BACKUPS_TODAY_DIR);
39
40 $originalFileDir = wp_normalize_path(WP_PATH . $root_dir . '/'. ltrim($dir, '/'));
41 $originalFileDir = str_replace(wp_normalize_path(WP_PATH), "", $originalFileDir);
42 $backupFilePath = BACKUPS_TODAY_DIR . "/" .$originalFileDir;
43 $ext = '.'.pathinfo($name, PATHINFO_EXTENSION);
44
45 if (!is_dir($backupFilePath)) {
46 wp_mkdir_p($backupFilePath);
47 }
48
49 if(!$backupStorage->storeStream($originalFileDir, $name, $oldStream['stream'], true)) {
50 return false;
51 }
52 if(!$backupStorage->storeStreamFromContent($originalFileDir, str_replace($ext, "_modified_" . $ext, $name), $newContent, true)) {
53 return false;
54 }
55
56 return true;
57 }
58
59 return false;
60 }
61
62 public static function exists($file): bool
63 {
64
65 $backup_file = WPIDE_Error_Handler::findBackupFile($file);
66 return !empty($backup_file);
67 }
68
69 public static function restore($file) {
70
71 $backup_file = WPIDE_Error_Handler::findBackupFile($file);
72 return WPIDE_Error_Handler::recoverFile($file, $backup_file);
73 }
74
75 protected static function cleanDirs() {
76
77 $backup = LocalFileSystem::load(BACKUPS_DIR);
78
79 try {
80 $collection = $backup->getDirectoryCollection('/');
81
82 $backups_max_days = AppConfig::get('file.backups_max_days', 5);
83
84 $today = date_create_from_format('Y-m-d', date('Y-m-d'));
85
86 $oldBackupFolders = $collection->filter(function($item) use($today, $backups_max_days) {
87
88 if($item['type'] === 'dir') {
89 $folderDate = date_create_from_format('Y-m-d', $item['name']);
90
91 if(!empty($folderDate)) {
92 $days = intval(date_diff($folderDate, $today)->format('%a'));
93 return $days >= $backups_max_days;
94 }
95
96 return true;
97 }
98
99 return false;
100
101 })->map(function($item) {
102
103 return $item['path'];
104
105 })->all();
106
107 foreach($oldBackupFolders as $folder) {
108
109 $backup->deleteDir($folder);
110 }
111
112 } catch (\Exception $e) {}
113
114 }
115
116 }
117