PluginProbe
Patchstack – WordPress & Plugins Security / 2.1.25
Patchstack – WordPress & Plugins Security v2.1.25
2.3.7 trunk 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.17 2.1.18 2.1.19 2.1.2 2.1.20 2.1.21 2.1.22 2.1.23 2.1.24 2.1.25 2.1.3 2.1.4 2.1.5 2.1.6 All 49 releases
patchstack / includes / activation.php

activation.php in Patchstack – WordPress & Plugins Security 2.1.25, at includes/activation.php

397 lines 13.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Do not allow the file to be called directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 /**
9 * This class is used to activate and deactivate the plugin.
10 * Additionally, we use it to run migrations.
11 */
12 class P_Activation extends P_Core {
13
14 /**
15 * Add the actions required for the activation.
16 *
17 * @param Patchstack $core
18 * @return void
19 */
20 public function __construct( $core ) {
21 parent::__construct( $core );
22 add_action( 'activated_plugin', array( $this, 'redirect_activation' ), 10, 2 );
23 }
24
25 /**
26 * Redirect the user to our settings page after plugin activation.
27 *
28 * @param string $plugin The plugin that is activated.
29 * @param boolean $network_activation If a network wide activation. (multisite)
30 * @return void
31 */
32 public function redirect_activation( $plugin, $network_activation ) {
33 if ( defined( 'WP_CLI' ) && WP_CLI ) {
34 return;
35 }
36
37 if ( $plugin == $this->plugin->basename ) {
38
39 // In case of multisite, we want to redirect the user to a different page.
40 if ( $network_activation ) {
41 wp_safe_redirect( network_admin_url( 'admin.php?page=patchstack-multisite-settings&tab=multisite&activated=1' ) );
42 } else {
43 wp_safe_redirect( admin_url( 'admin.php?page=' . $this->plugin->name . '&activated=1' ) );
44 }
45 exit;
46 }
47 }
48
49 /**
50 * Check if the plugin meets requirements and disable it if they are not present.
51 *
52 * @return boolean
53 */
54 public function check_requirements() {
55 if ( $this->meets_requirements() ) {
56 return true;
57 }
58
59 // Add a dashboard notice.
60 add_action( 'all_admin_notices', array( $this, 'requirements_not_met_notice' ) );
61 return false;
62 }
63
64 /**
65 * Check that all plugin requirements are met.
66 *
67 * @return boolean
68 */
69 public function meets_requirements() {
70 // Check to see if we can access the API.
71 $response = wp_remote_request(
72 $this->plugin->api_url,
73 array(
74 'method' => 'GET',
75 'timeout' => 10,
76 'redirection' => 5,
77 )
78 );
79
80 // Check if we can access the API.
81 if ( is_wp_error( $response ) ) {
82 $this->activation_errors[] = 'We were unable to contact our API server. Please contact your host and ask them to make sure that outgoing connections to api.webarxsecurity.com and api.patchstack.com are not blocked.<br />Additional error message to give to your host: ' . $response->get_error_message();
83 return false;
84 }
85
86 // Do checks for required classes / functions or similar.
87 // Add detailed messages to $this->activation_errors array.
88 if ( version_compare( phpversion(), '5.3.0', '<' ) ) {
89 $this->activation_errors[] = 'Please update the PHP version on your host to at least 5.3.0. Ask your host if you do not know what this means.';
90 return false;
91 }
92
93 global $wp_version;
94 if ( version_compare( $wp_version, '4.3.0', '<' ) ) {
95 $this->activation_errors[] = 'Please upgrade your WordPress site to at least 4.3.0.';
96 return false;
97 }
98
99 return true;
100 }
101
102 /**
103 * Adds a notice to the dashboard if the plugin requirements are not met.
104 *
105 * @return void
106 */
107 public function requirements_not_met_notice() {
108 // Deactivate the plugin.
109 deactivate_plugins( $this->plugin->basename );
110
111 // Compile default message.
112 $default_message = __( 'Patchstack could not be activated due to a conflict. See below for information regarding the conflict.<br />', 'patchstack' );
113
114 // Print the errors on the screen.
115 echo wp_kses_post( $default_message );
116 echo wp_kses_post( implode( '<br />', $this->activation_errors ) );
117 }
118
119 /**
120 * Activate the plugin.
121 *
122 * @param Patchstack $core
123 * @return void
124 */
125 public function activate( $core ) {
126 // Bail early if requirements are not met.
127 if ( ! $this->check_requirements() ) {
128 $this->requirements_not_met_notice();
129 exit;
130 }
131
132 // Check if the webarx/webarx.php plugin is present, if so, remove it.
133 if ( is_dir( WP_PLUGIN_DIR . '/webarx' ) ) {
134
135 // Migrate all current options to the new prefix.
136 global $wpdb;
137 $exists = $wpdb->get_var( "SELECT COUNT(*) FROM " . $wpdb->prefix . "options WHERE option_name = 'webarx_api_token'" );
138
139 // Move over the options.
140 if ( !is_null( $exists ) && $exists >= 1 ) {
141 $wpdb->query( 'INSERT IGNORE INTO ' . $wpdb->prefix . "options (option_name, option_value, autoload) SELECT REPLACE(option_name, 'webarx_', 'patchstack_') as option_name, option_value, autoload FROM " . $wpdb->prefix . "options WHERE option_name like 'webarx_%'" );
142 $wpdb->query( 'UPDATE ' . $wpdb->prefix . 'options AS a SET option_value = (SELECT option_value FROM ' . $wpdb->prefix . "options WHERE option_name = REPLACE(a.option_name, 'patchstack_', 'webarx_')) WHERE option_name LIKE 'patchstack_%'" );
143 }
144
145 // Deactivate the plugin.
146 include_once ABSPATH . 'wp-admin/includes/plugin.php';
147 deactivate_plugins( array( 'webarx/webarx.php' ) );
148 update_option( 'patchstack_license_free', '0' );
149 }
150
151 // Make sure any rewrite functionality has been loaded.
152 $this->migrate();
153 add_option( 'patchstack_first_activated', '1' );
154
155 // Activate the license.
156 if ( $this->plugin->client_id != 'PATCHSTACK_CLIENT_ID' && $this->plugin->private_key != 'PATCHSTACK_PRIVATE_KEY' ) {
157 $this->alter_license( $this->plugin->client_id, $this->plugin->private_key, 'activate' );
158 } elseif ( get_option( 'patchstack_clientid', false ) != false && get_option( 'patchstack_secretkey', false ) != false ) {
159 $this->alter_license( get_option( 'patchstack_clientid' ), $this->get_secret_key(), 'activate' );
160 } else {
161 update_option( 'patchstack_license_free', '1' );
162 }
163
164 // Update firewall status after activating plugin
165 $api = new P_Api( $core );
166 $token = $api->get_access_token();
167 if ( ! empty( $token ) ) {
168 $api->update_firewall_status( array( 'status' => 1 ) );
169 $api->update_url( array( 'plugin_url' => get_option( 'siteurl' ) ) );
170 }
171
172 // Immediately send software data to our server to set firewall as enabled.
173 // Also immediately download the whitelist file and the firewall rules.
174 do_action( 'patchstack_send_software_data' );
175 if ( get_option( 'patchstack_license_free', 0 ) != 1 ) {
176 do_action( 'patchstack_post_firewall_rules' );
177 do_action( 'patchstack_post_dynamic_firewall_rules' );
178 }
179
180 // One time actions should be placed here.
181 $this->plugin->hardening->delete_readme();
182 }
183
184 /**
185 * Used to activate an individual license on multisite/network.
186 *
187 * @param object $site
188 * @param array $license
189 * @return void
190 */
191 public function activate_multisite_license( $site, $license ) {
192 // Build the Patchstack tables on the site.
193 $this->migrate( null, $site->id );
194
195 // Add the options to given site.
196 foreach ( $this->plugin->admin_options->options as $name => $value ) {
197 add_blog_option( $site->id, $name, $value );
198 }
199
200 // Set the client id and secret key.
201 update_blog_option( $site->id, 'patchstack_clientid', $license['id'] );
202 $enc = $this->get_secret_key( $license['secret'] );
203 update_blog_option( $site->id, 'patchstack_secretkey', $enc['cipher'] );
204 update_blog_option( $site->id, 'patchstack_secretkey_nonce', $enc['nonce'] );
205
206 $this->plugin->api->blog_id = $site->id;
207
208 // Activate the license and update firewall status after activating the plugin.
209 $token = $this->plugin->api->get_access_token( $license['id'], $license['secret'], true );
210 if ( ! empty( $token ) ) {
211 $this->plugin->api->update_firewall_status( array( 'status' => $this->get_option( 'patchstack_basic_firewall' ) == 1 ) );
212 $this->plugin->api->update_url( array( 'plugin_url' => get_blog_option( $site->id, 'siteurl' ) ) );
213
214 // If we have an access token, tell our API that the firewall is activated
215 // and the current URL of the site.
216 update_blog_option( $site->id, 'patchstack_license_activated', '1' );
217 $this->plugin->api->update_license_status();
218
219 // This will trigger the software synchronization action.
220 wp_remote_get( get_site_url( $site->id ), array( 'sslverify' => false ) );
221 }
222
223 // Make sure to switch back to the current blog id.
224 $this->plugin->api->blog_id = get_current_blog_id();
225 }
226
227 /**
228 * Build the required Patchstack tables.
229 *
230 * @param null|string $ver The version to upgrade to.
231 * @param null|integer $site_id The blog id to perform the upgrades on.
232 * @return void
233 */
234 public function migrate( $ver = null, $site_id = null ) {
235 global $wpdb;
236 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
237 $charset_collate = $wpdb->get_charset_collate();
238 $prefix = $site_id != null ? $wpdb->get_blog_prefix( $site_id ) : $wpdb->prefix;
239
240 // The following conditions will only execute if Patchstack is installed because of an update
241 // and if we need to perform migrations.
242 if ( $ver !== null && file_exists( dirname( __FILE__ ) . '/migrations/v' . str_replace( '.', '', $ver ) . '.php' ) ) {
243 require_once dirname( __FILE__ ) . '/migrations/v' . str_replace( '.', '', $ver ) . '.php';
244 return;
245 }
246
247 // Require the base migration.
248 require_once dirname( __FILE__ ) . '/migrations/base.php';
249 }
250
251 /**
252 * Check if the database version of the plugin is running behind.
253 * If so, run the migrations up until the latest version.
254 *
255 * @return void
256 */
257 public function migrate_check() {
258 // Only perform migrations if we have any to execute.
259 $versions = array('3.0.0', '3.0.1', '3.0.2');
260 if ( count( $versions ) == 0 ) {
261 return;
262 }
263
264 // Get current database version and run the migrations.
265 $db_version = get_option( 'patchstack_db_version', false );
266 foreach ( $versions as $version ) {
267 if ( version_compare( $db_version, $version, '<' ) ) {
268 $this->migrate( $version );
269 }
270 }
271 }
272
273 /**
274 * Perform cleanup when the plugin is deactivated.
275 *
276 * @return void
277 */
278 public function deactivate() {
279 // Update firewall status after de-activating plugin
280 $api = new P_Api( $this );
281 $token = $api->get_access_token();
282 if ( ! empty( $token ) ) {
283 $api->update_firewall_status( array( 'status' => 0 ) );
284 }
285
286 // Clear all Patchstack scheduled tasks.
287 $tasks = array( 'patchstack_zip_backup', 'patchstack_send_software_data', 'patchstack_send_hacker_logs', 'patchstack_send_visitor_logs', 'patchstack_send_event_logs', 'patchstack_reset_blocked_attacks', 'patchstack_post_firewall_rules', 'patchstack_post_firewall_htaccess_rules', 'patchstack_post_dynamic_firewall_rules', 'patchstack_update_license_status', 'patchstack_update_plugins', 'patchstack_send_ping', 'puc_cron_check_updates-webarx' );
288 foreach ( $tasks as $task ) {
289 wp_clear_scheduled_hook( $task );
290 }
291
292 // Cleanup the .htaccess file.
293 $this->plugin->htaccess->cleanup_htaccess_file();
294 }
295
296 /**
297 * Activate or deactivate a license on the current site.
298 *
299 * @param integer $id
300 * @param string $secret
301 * @param string $action
302 * @return array
303 */
304 public function alter_license( $id, $secret, $action ) {
305 // Store current keys in tmp variable so in case it fails, we can set it back.
306 $tmp_id = get_option( 'patchstack_clientid' );
307 $tmp_key = $this->get_secret_key();
308
309 // Set the new values.
310 update_option( 'patchstack_clientid', $id );
311 $this->set_secret_key( $secret );
312
313 // Activate the license.
314 if ( $action == 'activate' ) {
315 $api_result = $this->plugin->api->get_access_token( $id, $secret, true );
316
317 // Valid result?
318 if ( ! $api_result ) {
319 update_option( 'patchstack_clientid', $tmp_id );
320 $this->set_secret_key( $tmp_key );
321 return array(
322 'result' => 'error',
323 'message' => 'Cannot activate license!',
324 );
325 }
326
327 // If we have an access token, tell our API that the firewall is activated
328 // and the current URL of the site.
329 update_option( 'patchstack_license_activated', '1' );
330 $this->plugin->api->update_license_status();
331 $token = $this->plugin->api->get_access_token();
332 if ( ! empty( $token ) ) {
333 do_action( 'patchstack_send_software_data' );
334 if ( get_option( 'patchstack_license_free', 0 ) != 1 ) {
335 do_action( 'patchstack_post_firewall_rules' );
336 do_action( 'patchstack_post_dynamic_firewall_rules' );
337 $this->header();
338 }
339
340 $this->plugin->api->update_firewall_status( array( 'status' => $this->get_option( 'patchstack_basic_firewall' ) == 1 ) );
341 $this->plugin->api->update_url( array( 'plugin_url' => get_option( 'siteurl' ) ) );
342 }
343 return array(
344 'result' => 'success',
345 'message' => 'License activated!',
346 );
347 }
348
349 // Deactivate the license.
350 if ( $action == 'deactivate' ) {
351 update_option( 'patchstack_api_token', '' );
352 update_option( 'patchstack_license_activated', '0' );
353 return array(
354 'result' => 'success',
355 'message' => 'License deactivated!',
356 );
357 }
358 }
359
360 /**
361 * Send a request to our API for the IP address header.
362 *
363 * @return void
364 */
365 public function header()
366 {
367 $header = get_option( 'patchstack_firewall_ip_header', '' );
368 $computed = get_option( 'patchstack_ip_header_computed', 0 );
369
370 if ( $header == '' && ! $computed ) {
371 // Create an OTT token.
372 $ott = md5( wp_generate_password( 32, true, true ) );
373 update_option( 'patchstack_ott_action', $ott );
374
375 // Tell our API.
376 wp_remote_request(
377 $this->plugin->api_url . '/api/header',
378 array(
379 'method' => 'POST',
380 'timeout' => 60,
381 'redirection' => 5,
382 'httpversion' => '1.0',
383 'blocking' => true,
384 'headers' => array(
385 'Source-Host' => get_site_url(),
386 ),
387 'body' => array(
388 'token' => $ott,
389 'url' => get_site_url()
390 ),
391 'cookies' => array(),
392 )
393 );
394 }
395 }
396 }
397