PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.9.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.9.2
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 9 months ago Jetpack.php 2 years ago LiteSpeedCache.php 1 year ago MalCare.php 1 year ago NinjaForms.php 1 year ago ThirdPartyCacheHandler.php 2 years ago WordFence.php 5 months ago
MalCare.php
171 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\Filesystem\Filesystem;
11 use WPStaging\Staging\Sites;
12
13 class MalCare
14 {
15 /**
16 * @var string
17 */
18 const OPTION_MALCARE_CONFIG = 'mcconfig';
19
20 /**
21 * @var Database
22 */
23 private $database;
24
25 /**
26 * @var WpAdapter
27 */
28 protected $wpAdapter;
29
30 /**
31 * @var Sites
32 */
33 protected $sites;
34
35 /**
36 * @var Filesystem
37 */
38 protected $filesystem;
39
40 /**
41 * @param WpAdapter $wpAdapter
42 * @param Sites $sites
43 */
44 public function __construct(WpAdapter $wpAdapter, Sites $sites, Filesystem $filesystem)
45 {
46 $this->wpAdapter = $wpAdapter;
47 $this->sites = $sites;
48 $this->filesystem = $filesystem;
49 }
50
51 /**
52 * @param object $options
53 * @return void
54 */
55 public function maybeDisableMalCare($options)
56 {
57 if (empty($options->clone) || empty($options->destinationDir) || empty($options->prefix)) {
58 return;
59 }
60
61 if (!$this->sites->isExistingClone($options->clone)) {
62 return;
63 }
64
65 if (!$this->isMalCareActive()) {
66 return;
67 }
68
69 $this->initializeDatabase($options);
70 if ($this->database === null) {
71 return;
72 }
73
74 $this->cleanMalCareConfig($options->prefix);
75 $this->maybeRemoveMalCareInclude($options->destinationDir . '/wp-config.php');
76 }
77
78 /**
79 * Check if MalCare plugin is active
80 * @return bool
81 */
82 private function isMalCareActive(): bool
83 {
84 return $this->wpAdapter->isPluginActive('malcare-security/malcare.php');
85 }
86
87 /**
88 * Initialize the database connection
89 * @param object $options
90 * @return void
91 */
92 private function initializeDatabase($options)
93 {
94 if ($this->database === null) {
95 $this->database = new Database();
96 }
97
98 if (!$this->isExternalDatabase($options)) {
99 return;
100 }
101
102 $wpdb = new wpdb(
103 $options->databaseUser,
104 $options->databasePassword,
105 $options->databaseDatabase,
106 $options->databaseServer
107 );
108
109 $wpdb->prefix = $options->prefix;
110
111 $this->database->setWpDatabase($wpdb);
112 }
113
114 /**
115 * Delete MalCare configuration
116 * @param string $prefix
117 * @return void
118 */
119 private function cleanMalCareConfig(string $prefix)
120 {
121 $optionName = self::OPTION_MALCARE_CONFIG;
122 $tableName = $prefix . 'options';
123
124 $result = $this->database->getClient()->query("SELECT option_value FROM `$tableName` WHERE option_name = '$optionName'");
125 if ($this->database->getClient()->numRows($result) === 0) {
126 return;
127 }
128
129 $this->database->getClient()->query("DELETE FROM `$tableName` WHERE option_name = '$optionName'");
130 }
131
132 /**
133 * Remove the malcare-waf.php include from wp-config.php
134 *
135 * @param string $filePath Path to the wp-config.php file
136 * @return bool
137 */
138 private function maybeRemoveMalCareInclude(string $filePath): bool
139 {
140 if (!file_exists($filePath)) {
141 return false;
142 }
143
144 $content = file_get_contents($filePath);
145 if ($content === false) {
146 return false;
147 }
148
149 $pattern = '/^\s*(require_once|require|include|@include)\s*\(?\s*[\'"][^\'"]*malcare-waf\.php[\'"]\s*\)?\s*;\s*$/m';
150 $content = preg_replace($pattern, '', $content);
151
152 if ($this->filesystem->create($filePath, $content) === false) {
153 return false;
154 }
155
156 return true;
157 }
158
159 /**
160 * @param $options
161 * @return bool
162 */
163 private function isExternalDatabase($options): bool
164 {
165 return !(empty($options->databaseUser) ||
166 empty($options->databasePassword) ||
167 empty($options->databaseDatabase) ||
168 empty($options->databaseServer));
169 }
170 }
171