PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / ThirdParty / MalCare.php
wp-staging / Framework / ThirdParty Last commit date
Aios.php 1 day ago FreemiusScript.php 1 day ago Jetpack.php 1 day ago LiteSpeedCache.php 1 day ago MalCare.php 1 day ago NinjaForms.php 1 day ago ThirdPartyCacheHandler.php 1 day ago WordFence.php 1 day ago
MalCare.php
176 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace WPStaging\Framework\ThirdParty;
6
7 use wpdb;
8 use WPStaging\Framework\Adapter\Database;
9 use WPStaging\Framework\Adapter\WpAdapter;
10 use WPStaging\Framework\Database\ExternalDatabaseConfiguration;
11 use WPStaging\Framework\Filesystem\Filesystem;
12 use WPStaging\Staging\Sites;
13
14
15
16
17 class MalCare
18 {
19
20
21
22 const OPTION_MALCARE_CONFIG = 'mcconfig';
23
24
25
26
27 private $database;
28
29
30
31
32 protected $wpAdapter;
33
34
35
36
37 protected $sites;
38
39
40
41
42 protected $filesystem;
43
44
45 private $externalDatabaseConfiguration;
46
47
48
49
50
51 public function __construct(WpAdapter $wpAdapter, Sites $sites, Filesystem $filesystem)
52 {
53 $this->wpAdapter = $wpAdapter;
54 $this->sites = $sites;
55 $this->filesystem = $filesystem;
56 $this->externalDatabaseConfiguration = new ExternalDatabaseConfiguration();
57 }
58
59
60
61
62
63 public function maybeDisableMalCare($options)
64 {
65 if (empty($options->clone) || empty($options->destinationDir) || empty($options->prefix)) {
66 return;
67 }
68
69 if (!$this->sites->isExistingClone($options->clone)) {
70 return;
71 }
72
73 if (!$this->isMalCareActive()) {
74 return;
75 }
76
77 $this->initializeDatabase($options);
78 if ($this->database === null) {
79 return;
80 }
81
82 $this->cleanMalCareConfig($options->prefix);
83 $this->maybeRemoveMalCareInclude($options->destinationDir . '/wp-config.php');
84 }
85
86
87
88
89
90 private function isMalCareActive(): bool
91 {
92 return $this->wpAdapter->isPluginActive('malcare-security/malcare.php');
93 }
94
95
96
97
98
99
100 private function initializeDatabase($options)
101 {
102 if ($this->database === null) {
103 $this->database = new Database();
104 }
105
106 if (!$this->isExternalDatabase($options)) {
107 return;
108 }
109
110 $wpdb = new wpdb(
111 $options->databaseUser,
112 $options->databasePassword,
113 $options->databaseDatabase,
114 $options->databaseServer
115 );
116
117 $wpdb->prefix = $options->prefix;
118
119 $this->database->setWpDatabase($wpdb);
120 }
121
122
123
124
125
126
127 private function cleanMalCareConfig(string $prefix)
128 {
129 $optionName = self::OPTION_MALCARE_CONFIG;
130 $tableName = $prefix . 'options';
131
132 $result = $this->database->getClient()->query("SELECT option_value FROM `$tableName` WHERE option_name = '$optionName'");
133 if ($this->database->getClient()->numRows($result) === 0) {
134 return;
135 }
136
137 $this->database->getClient()->query("DELETE FROM `$tableName` WHERE option_name = '$optionName'");
138 }
139
140
141
142
143
144
145
146 private function maybeRemoveMalCareInclude(string $filePath): bool
147 {
148 if (!file_exists($filePath)) {
149 return false;
150 }
151
152 $content = file_get_contents($filePath);
153 if ($content === false) {
154 return false;
155 }
156
157 $pattern = '/^\s*(require_once|require|include|@include)\s*\(?\s*[\'"][^\'"]*malcare-waf\.php[\'"]\s*\)?\s*;\s*$/m';
158 $content = preg_replace($pattern, '', $content);
159
160 if ($this->filesystem->create($filePath, $content) === false) {
161 return false;
162 }
163
164 return true;
165 }
166
167
168
169
170
171 private function isExternalDatabase($options): bool
172 {
173 return $this->externalDatabaseConfiguration->isEnabled($options);
174 }
175 }
176