PluginProbe ʕ •ᴥ•ʔ
NinjaFirewall (WP Edition) – Advanced Security Plugin and Firewall / 4.9
NinjaFirewall (WP Edition) – Advanced Security Plugin and Firewall v4.9
4.9 4.8.8 4.8.7 4.8.6 trunk 4.5 4.5.1 4.5.10 4.5.11 4.5.2 4.5.3 4.5.4 4.5.5 4.5.6 4.5.7 4.5.8 4.5.9 4.6 4.6.1 4.7 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.8 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5
ninjafirewall / lib / class-plugin-upgrade.php
ninjafirewall / lib Last commit date
share 9 years ago .htaccess 11 years ago anti_malware.php 5 years ago class-api.php 4 weeks ago class-centralised-logging.php 4 weeks ago class-coupon.php 7 months ago class-email-sodium.php 4 weeks ago class-firewall-log.php 4 weeks ago class-helpers.php 9 months ago class-import-export.php 5 months ago class-ip.php 5 months ago class-nfw-database.php 7 months ago class-plugin-upgrade.php 4 weeks ago class-security-updates.php 4 weeks ago class-session.php 4 weeks ago class_mail.php 4 weeks ago firewall.php 4 weeks ago fw_fileguard.php 5 months ago fw_livelog.php 1 year ago help.php 4 weeks ago helpers.php 4 weeks ago i18n-extra.php 4 weeks ago i18n.php 1 year ago index.html 13 years ago init_update.php 2 years ago install.php 1 year ago install_default.php 4 weeks ago loader.php 7 months ago mail_template_firewall.php 1 year ago mail_template_plugin.php 4 weeks ago scheduled_tasks.php 3 years ago settings_dashboard.php 4 weeks ago settings_dashboard_about.php 4 weeks ago settings_dashboard_statistics.php 2 months ago settings_event_notifications.php 4 weeks ago settings_events.php 2 months ago settings_firewall_options.php 2 months ago settings_firewall_policies.php 4 weeks ago settings_login_protection.php 2 months ago settings_logs.php 4 weeks ago settings_logs_firewall_log.php 4 weeks ago settings_logs_live_log.php 2 months ago settings_monitoring.php 4 weeks ago settings_monitoring_file_check.php 2 months ago settings_monitoring_file_guard.php 2 months ago settings_network.php 2 months ago settings_security_rules.php 2 months ago settings_security_rules_editor.php 4 weeks ago settings_security_rules_update.php 4 weeks ago sign.pub 7 years ago thickbox.php 4 years ago widget.php 3 years ago wpplus.php 5 months ago
class-plugin-upgrade.php
204 lines
1 <?php
2 /*
3 +=====================================================================+
4 | _ _ _ _ _____ _ _ _ |
5 | | \ | (_)_ __ (_) __ _| ___(_)_ __ _____ ____ _| | | |
6 | | \| | | '_ \ | |/ _` | |_ | | '__/ _ \ \ /\ / / _` | | | |
7 | | |\ | | | | || | (_| | _| | | | | __/\ V V / (_| | | | |
8 | |_| \_|_|_| |_|/ |\__,_|_| |_|_| \___| \_/\_/ \__,_|_|_| |
9 | |__/ |
10 | (c) NinTechNet Limited ~ https://nintechnet.com/ |
11 +=====================================================================+
12 */
13
14 if ( class_exists('NinjaFirewall_plugin') ) {
15 return;
16 }
17
18 class NinjaFirewall_plugin {
19
20 private static $url = 'https://downloads.wordpress.org/plugin/%s.%s.zip';
21 private static $slug = '';
22 private static $main = '';
23 private static $network = false;
24 private static $active = false;
25
26 private static $main_site_only = true;
27
28
29 /**
30 * Initialize the filesystem.
31 */
32 private static function init() {
33
34 require_once ABSPATH .'wp-admin/includes/class-wp-filesystem-base.php';
35 require_once ABSPATH .'wp-admin/includes/class-wp-filesystem-direct.php';
36 require_once ABSPATH .'wp-admin/includes/file.php';
37 require_once ABSPATH .'wp-admin/includes/plugin.php';
38
39 $res = WP_Filesystem();
40 if ( $res === true ) {
41 return;
42 }
43 $error = esc_html__('Error initialize the filesystem.', 'ninjafirewall');
44 wp_send_json( ['status' => 'error', 'message' => $error ] );
45 }
46
47
48 /**
49 * Manual upgrade of the corresponding plugin.
50 * `nfw_pluginupgrade` AJAX action.
51 */
52 public static function upgrade() {
53 /**
54 * The superadmin must run the update from the main site only to avoid errors.
55 */
56 if ( self::$main_site_only && ! is_main_site() ) {
57 $error = esc_html__('Connect to the main site and try again.', 'ninjafirewall');
58 wp_send_json( ['status' => 'error', 'message' => $error ] );
59 }
60
61 global $wp_filesystem;
62
63 /**
64 * Verify user capabilities and the security nonce.
65 */
66 nf_not_allowed('block', __LINE__, 'ajax');
67 if (! check_ajax_referer('pluginupgrade', 'nonce', false ) ) {
68 $error = esc_html__('Security nonces do not match. Reload the page and try again.',
69 'ninjafirewall');
70 wp_send_json( ['status' => 'error', 'message' => $error ] );
71 }
72
73 if ( empty( $_POST['plugin'] ) || empty( $_POST['version'] ) ) {
74 $error = esc_html__('Missing parameters.', 'ninjafirewall');
75 wp_send_json( ['status' => 'error', 'message' => $error ] );
76 }
77
78 list( self::$slug, self::$main ) = explode('/', stripslashes( urldecode( $_POST['plugin'] ) ), 2 );
79 self::$slug = sanitize_title( self::$slug );
80 self::$main = sanitize_file_name( self::$main );
81
82 $version = stripslashes( $_POST['version'] );
83
84 /**
85 * Make sure the plugin and its version match our list.
86 */
87 $nfw_checked = nfw_get_option('nfw_checked');
88 $error = esc_html__('Plugin name or version is invalid.', 'ninjafirewall');
89 if ( empty( $nfw_checked['plugins'] ) ||
90 ! isset( $nfw_checked['plugins'][ self::$slug .'/'. self::$main ]['version'] ) ) {
91
92 wp_send_json( ['status' => 'error', 'message' => $error ] );
93 }
94 if ( $nfw_checked['plugins'][ self::$slug .'/'. self::$main ]['version'] != $version ) {
95 wp_send_json( ['status' => 'error', 'message' => $error ] );
96 }
97
98 /**
99 * Initialize the filestem.
100 */
101 self::init();
102
103 /**
104 * Check if the plugin is activated across the entire network,
105 * if that's a multisite environment.
106 */
107 if ( is_multisite() && is_plugin_active_for_network( self::$slug .'/'. self::$main ) ) {
108 self::$network = true;
109 }
110
111 /**
112 * Deactivate the plugin, silently.
113 */
114 if ( is_plugin_active( self::$slug .'/'. self::$main ) ) {
115 deactivate_plugins( self::$slug .'/'. self::$main ,true );
116 self::$active = true;
117 }
118
119 /**
120 * Download it from wordpress.org.
121 */
122 self::$url = sprintf( self::$url, self::$slug, $version );
123 $file = download_url( self::$url );
124 if ( is_wp_error( $file ) ) {
125 $error = sprintf(
126 esc_html__('Error when trying to download the plugin: %s', 'ninjafirewall'),
127 esc_html ( $file->get_error_message() )
128 );
129 /**
130 * Reactivate the plugin and quit.
131 */
132 self::activate_plugin();
133 wp_send_json( ['status' => 'error', 'message' => $error ] );
134 }
135
136 /**
137 * Delete the plugin folder, recursively.
138 */
139 $plugin_dir = trailingslashit( WP_PLUGIN_DIR ) . self::$slug;
140 if ( $wp_filesystem->is_dir( $plugin_dir ) ) {
141 $res = $wp_filesystem->delete( $plugin_dir, true, 'd');
142 if ( $res === false ) {
143 $error = sprintf(
144 esc_html__('Error when trying to delete the plugin folder: %s', 'ninjafirewall'),
145 esc_html( $plugin_dir )
146 );
147 /**
148 * Try to reactivate the plugin, if possible, before quitting.
149 */
150 self::activate_plugin();
151 wp_send_json( ['status' => 'error', 'message' => $error ] );
152 }
153 }
154
155 /**
156 * Unzip the new plugin.
157 */
158 $res = unzip_file( $file, WP_PLUGIN_DIR );
159 if ( is_wp_error( $res ) ) {
160 $error = sprintf(
161 esc_html__('Error when trying to extract the plugin: %s', 'ninjafirewall'),
162 esc_html ( $res->get_error_message() )
163 );
164 wp_send_json( ['status' => 'error', 'message' => $error ] );
165 }
166
167 /**
168 * Delete the downloaded file.
169 */
170 wp_delete_file( $file );
171
172 /**
173 * Reactivate the plugin, if it was activated prior to the upgrade.
174 */
175 $res = self::activate_plugin();
176 if ( is_wp_error( $res ) ) {
177 $error = sprintf(
178 esc_html__('Error when trying to reactivate the plugin: %s', 'ninjafirewall'),
179 esc_html ( $res->get_error_message() )
180 );
181 wp_send_json( ['status' => 'error', 'message' => $error ] );
182 }
183
184 $message = esc_html__('The plugin was successfully updated. The page will now reload.',
185 'ninjafirewall');
186 wp_send_json( ['status' => 'success', 'message' => $message ] );
187 }
188
189
190 /**
191 * Reactivate the plugin.
192 */
193 private static function activate_plugin() {
194
195 if ( self::$active === true ) {
196 return activate_plugin( self::$slug .'/'. self::$main, '', self::$network, true );
197 }
198 }
199
200 }
201
202 // =====================================================================
203 // EOF
204