PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.9.5
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.9.5
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 2 years ago FreemiusScript.php 10 months ago Jetpack.php 2 years ago LiteSpeedCache.php 1 year ago MalCare.php 5 days ago NinjaForms.php 1 year ago ThirdPartyCacheHandler.php 2 years ago WordFence.php 6 months 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 * Removes MalCare firewall bootstrapping from classic staging sites.
16 */
17 class MalCare
18 {
19 /**
20 * @var string
21 */
22 const OPTION_MALCARE_CONFIG = 'mcconfig';
23
24 /**
25 * @var Database
26 */
27 private $database;
28
29 /**
30 * @var WpAdapter
31 */
32 protected $wpAdapter;
33
34 /**
35 * @var Sites
36 */
37 protected $sites;
38
39 /**
40 * @var Filesystem
41 */
42 protected $filesystem;
43
44 /** @var ExternalDatabaseConfiguration */
45 private $externalDatabaseConfiguration;
46
47 /**
48 * @param WpAdapter $wpAdapter
49 * @param Sites $sites
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 * @param object $options
61 * @return void
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 * Check if MalCare plugin is active
88 * @return bool
89 */
90 private function isMalCareActive(): bool
91 {
92 return $this->wpAdapter->isPluginActive('malcare-security/malcare.php');
93 }
94
95 /**
96 * Initialize the database connection
97 * @param object $options
98 * @return void
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 * Delete MalCare configuration
124 * @param string $prefix
125 * @return void
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 * Remove the malcare-waf.php include from wp-config.php
142 *
143 * @param string $filePath Path to the wp-config.php file
144 * @return bool
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 * @param $options
169 * @return bool
170 */
171 private function isExternalDatabase($options): bool
172 {
173 return $this->externalDatabaseConfiguration->isEnabled($options);
174 }
175 }
176