PluginProbe
Patchstack – WordPress & Plugins Security / 2.1.9
Patchstack – WordPress & Plugins Security v2.1.9
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 / patchstack.php

patchstack.php in Patchstack – WordPress & Plugins Security 2.1.9, at patchstack.php

356 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Patchstack Security
4 * Plugin URI: https://patchstack.com
5 * Description: Patchstack identifies security vulnerabilities in WordPress plugins, themes, and core.
6 * Version: 2.1.9
7 * Author: Patchstack
8 * License: GPLv3
9 * Text Domain: patchstack
10 * Domain Path: /languages
11 * Requires at least: 4.4
12 * Requires PHP: 5.6
13 */
14
15 // Do not allow the file to be called directly.
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 if ( ! function_exists( 'patchstack_autoload_classes' ) ) {
21 /**
22 * Autoloads the Patchstack classes when called.
23 *
24 * @param string $class_name The class name to autoload.
25 * @return void
26 */
27 function patchstack_autoload_classes( $class_name ) {
28 // If the requested class doesn't have our prefix, don't load it.
29 if ( strpos( $class_name, 'P_' ) !== 0 ) {
30 return;
31 }
32
33 // Set up our filename.
34 $file_name = strtolower( str_replace( '_', '-', substr( $class_name, strlen( 'P_' ) ) ) );
35 $dir = trailingslashit( dirname( __FILE__ ) ) . 'includes/';
36 $target = array( $dir . $file_name . '.php', $dir . 'admin/' . str_replace( 'admin-', '', $file_name ) . '.php' );
37
38 // Attempt each target and load if it exists.
39 foreach ( $target as $file ) {
40 if ( file_exists( $file ) ) {
41 include_once $file;
42 }
43 }
44 }
45 }
46 spl_autoload_register( 'patchstack_autoload_classes' );
47
48 if ( ! class_exists( 'patchstack' ) ) {
49
50 /**
51 * This is the main Patchstack class used for all Patchstack related features and to launch
52 * the Patchstack plugin.
53 */
54 class Patchstack {
55
56 /**
57 * The plugin version.
58 *
59 * @var string
60 */
61 const VERSION = '2.1.9';
62
63 /**
64 * API URL of Patchstack to communicate with.
65 *
66 * @var string
67 */
68 const API_URL = 'https://api.patchstack.com';
69
70 /**
71 * API Auth URL of Patchstack to communicate with.
72 *
73 * @var string
74 */
75 const AUTH_URL = 'https://auth.patchstack.com';
76
77 /**
78 * Client ID, this is only set when freshly downloaded from the app.
79 *
80 * @var string
81 */
82 const CLIENT_ID = 'PATCHSTACK_CLIENT_ID';
83
84 /**
85 * Client private key, this is only set when freshly downloaded from the app.
86 *
87 * @var string
88 */
89 const PRIVATE_KEY = 'PATCHSTACK_PRIVATE_KEY';
90
91 /**
92 * Known IP addresses.
93 *
94 * @var array
95 */
96 protected $ips = array( '18.221.197.243', '52.15.237.250', '3.19.3.34', '3.18.238.17', '13.58.49.77', '18.222.191.77', '3.131.108.250', '3.23.157.140', '18.220.70.233', '3.140.84.221', '185.212.171.100' );
97
98 /**
99 * URL of the plugin directory.
100 *
101 * @var string
102 */
103 protected $url = '';
104
105 /**
106 * Plugin basename.
107 *
108 * @var string
109 */
110 protected $basename = '';
111
112 /**
113 * Plugin name.
114 *
115 * @var string
116 */
117 protected $name = '';
118
119 /**
120 * Detailed activation error messages.
121 *
122 * @var array
123 */
124 protected $activation_errors = array();
125
126 /**
127 * Singleton instance of plugin.
128 *
129 * @var Patchstack
130 */
131 protected static $single_instance = null;
132
133 /**
134 * Define all the variables that will hold the Patchstack classes.
135 * These must be defined because it allows us to communicate from one class to the other.
136 */
137 protected $firewall;
138 protected $firewall_base;
139 protected $activation;
140 protected $cron;
141 protected $api;
142 protected $login;
143 protected $ban;
144 protected $hardening;
145 protected $htaccess;
146 protected $hacker_log;
147 protected $upload;
148 protected $rules;
149 protected $listener;
150 protected $event_log;
151 protected $multisite;
152 protected $notice;
153 protected $admin_ajax;
154 protected $admin_general;
155 protected $admin_menu;
156 protected $admin_options;
157
158 /**
159 * Setup a few base variables for the plugin.
160 * Also make sure certain constants are defined.
161 *
162 * @return void
163 */
164 protected function __construct() {
165 // Set the permission constants if not already set.
166 if ( ! defined( 'FS_CHMOD_DIR' ) ) {
167 define( 'FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) );
168 }
169
170 if ( ! defined( 'FS_CHMOD_FILE' ) ) {
171 define( 'FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) );
172 }
173
174 // Define local variables.
175 $this->basename = plugin_basename( __FILE__ );
176 $this->url = plugin_dir_url( __FILE__ );
177 $names = explode( '/', $this->basename );
178 $this->name = $names[0];
179 }
180
181 /**
182 * Call the constructor of all the Patchstack related classes.
183 *
184 * @return void
185 */
186 public function plugin_classes() {
187 // Define the array of the classes.
188 foreach ( array(
189 'admin_options' => 'P_Admin_Options',
190 'cron' => 'P_Cron',
191 'api' => 'P_Api',
192 'login' => 'P_Login',
193 'ban' => 'P_Ban',
194 'hardening' => 'P_Hardening',
195 'htaccess' => 'P_Htaccess',
196 'hacker_log' => 'P_Hacker_Log',
197 'upload' => 'P_Upload',
198 'rules' => 'P_Rules',
199 'listener' => 'P_Listener',
200 'event_log' => 'P_Event_Log',
201 'activation' => 'P_Activation',
202 'multisite' => 'P_Multisite',
203 'notice' => 'P_Cookie_Notice',
204 'admin_ajax' => 'P_Admin_Ajax',
205 'admin_general' => 'P_Admin_General',
206 'admin_menu' => 'P_Admin_Menu',
207 ) as $var => $class ) {
208 $this->$var = new $class( $this );
209 }
210
211 $this->firewall_base = new P_Firewall( true, $this, true );
212 }
213
214 /**
215 * Activate the plugin.
216 *
217 * @return void
218 */
219 public function activate() {
220 $this->plugin_classes();
221 $this->activation->activate( $this );
222 }
223
224 /**
225 * Deactivate the plugin.
226 *
227 * @return void
228 */
229 public function deactivate() {
230 $this->plugin_classes();
231 $this->activation->deactivate();
232 }
233
234 /**
235 * Boot Patchstack and its classes.
236 *
237 * @return void
238 */
239 public function hooks() {
240 add_action( 'init', array( $this, 'init' ), ~PHP_INT_MAX );
241 }
242
243 /**
244 * Boot Patchstack
245 *
246 * @return void
247 */
248 public function init() {
249 // Load translated strings for plugin.
250 load_plugin_textdomain( 'patchstack', false, dirname( $this->basename ) . '/languages/' );
251
252 // Initialize plugin classes.
253 $this->plugin_classes();
254
255 // Perform migrations if necessary.
256 $this->activation->migrate_check();
257
258 // If license expiration has not been fetched yet while the plugin is active, update it.
259 if ( get_option( 'patchstack_api_token', '' ) == '' && get_option( 'patchstack_license_expiry', '' ) == '' ) {
260 $this->api->update_license_status();
261 }
262
263 // Determine if the license is activated and not expired.
264 if ( get_option( 'patchstack_license_activated', 0 ) == 1 && get_option( 'patchstack_basic_firewall', 0 ) == 1 && get_option( 'patchstack_license_free', 0 ) == 0 ) {
265 $this->firewall = new P_Firewall( true, $this );
266 }
267 }
268
269 /**
270 * Creates or returns an instance of this class.
271 *
272 * @return Patchstack
273 */
274 public static function get_instance() {
275 if ( null === self::$single_instance ) {
276 self::$single_instance = new self();
277 }
278
279 return self::$single_instance;
280 }
281
282 /**
283 * Magic getter.
284 *
285 * @param string $field The field to magically get.
286 * @return mixed
287 */
288 public function __get( $field ) {
289 switch ( $field ) {
290 case 'version':
291 return self::VERSION;
292 case 'api_url':
293 return self::API_URL;
294 case 'auth_url':
295 return self::AUTH_URL;
296 case 'client_id':
297 return self::CLIENT_ID;
298 case 'private_key':
299 return self::PRIVATE_KEY;
300 default:
301 try {
302 return $this->$field;
303 } catch ( \Exception $e ) {
304 return null;
305 }
306 }
307 }
308 }
309 }
310
311 if ( ! function_exists( 'patchstack_uninstall' ) ) {
312 /**
313 * Called when the plugin is uninstalled/removed from the site.
314 * This is not the same as deactivation, where the plugin still resides on the site.
315 *
316 * @return void
317 */
318 function patchstack_uninstall() {
319 // Delete most of the Patchstack options.
320 $options = array( 'patchstack_eventlog_lastid', 'patchstack_api_token', 'patchstack_dashboardlock', 'patchstack_pluginedit', 'patchstack_move_logs', 'patchstack_userenum', 'patchstack_basicscanblock', 'patchstack_hidewpcontent', 'patchstack_hidewpversionk', 'patchstack_prevent_default_file_access', 'patchstack_basic_firewall', 'patchstack_known_blacklist', 'patchstack_block_debug_log_access', 'patchstack_block_fake_bots', 'patchstack_index_views', 'patchstack_proxy_comment_posting', 'patchstack_bad_query_strings', 'patchstack_advanced_character_string_filter', 'patchstack_advanced_blacklist_firewall', 'patchstack_forbid_rfi', 'patchstack_image_hotlinking', 'patchstack_add_security_headers', 'patchstack_firewall_log_lastid', 'patchstack_user_log_lastid', 'patchstack_captcha_public_key', 'patchstack_captcha_private_key', 'patchstack_scan_interval', 'patchstack_scan_day', 'patchstack_scan_time', 'patchstack_hackers_log', 'patchstack_users_log', 'patchstack_visitors_log', 'external_updates-webarx', 'patchstack_wp_stats', 'patchstack_captcha_login_form', 'patchstack_license_activated', 'patchstack_license_expiry', 'patchstack_software_data_hash', 'patchstack_mv_wp_login', 'patchstack_rename_wp_login', 'patchstack_googledrive_backup_is_running', 'patchstack_googledrive_upload_state', 'patchstack_googledrive_access_token', 'patchstack_googledrive_refresh_token', 'patchstack_cron_offset', 'patchstack_htaccess_rules_hash' );
321 foreach ( $options as $option ) {
322 delete_option( $option );
323
324 if ( is_multisite() ) {
325 delete_site_option( $option );
326 }
327 }
328
329 // Drop all Patchstack tables.
330 global $wpdb;
331 $tables = array( 'patchstack_user_log', 'patchstack_visitor_log', 'patchstack_firewall_log', 'patchstack_file_hashes', 'patchstack_logic', 'patchstack_ip', 'patchstack_event_log' );
332 foreach ( $tables as $table ) {
333 $wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . $table );
334 }
335 }
336 }
337
338 if ( ! function_exists( 'patchstack' ) ) {
339 /**
340 * Grab the Patchstack object and return it.
341 *
342 * @return Patchstack
343 */
344 function patchstack() {
345 return patchstack::get_instance();
346 }
347 }
348
349 // Kick it off.
350 add_action( 'plugins_loaded', array( patchstack(), 'hooks' ) );
351
352 // Activation and deactivation hooks.
353 register_activation_hook( __FILE__, array( patchstack(), 'activate' ) );
354 register_deactivation_hook( __FILE__, array( patchstack(), 'deactivate' ) );
355 register_uninstall_hook( __FILE__, 'patchstack_uninstall' );
356