PluginProbe
Patchstack – WordPress & Plugins Security / 2.2.4
Patchstack – WordPress & Plugins Security v2.2.4
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
← All changes | includes/api.php +53 -36 2.1.222.2.4 View file →
@@ -23,10 +23,10 @@
23 23 */
24 24 public function __construct( $core ) {
25 25 parent::__construct( $core );
26 26 $this->blog_id = get_current_blog_id();
27 - add_action( 'patchstack_update_license_status', array( $this, 'update_license_status' ) );
28 - add_action( 'patchstack_send_ping', array( $this, 'ping' ) );
27 + add_action( 'patchstack_update_license_status', [ $this, 'update_license_status' ] );
28 + add_action( 'patchstack_send_ping', [ $this, 'ping' ] );
29 29 }
30 30
31 31 /**
32 32 * Get the API token.
@@ -50,12 +50,12 @@
50 50 if ( $response && $response->result == 'success' ) {
51 51 $this->update_blog_option(
52 52 $this->blog_id,
53 53 'patchstack_api_token',
54 - array(
54 + [
55 55 'token' => $response->message,
56 56 'expiresin' => $response->expiresin,
57 - )
57 + ]
58 58 );
59 59 return $response->message;
60 60 }
61 61
@@ -68,21 +68,29 @@
68 68 * Fetch the API Token from API Server.
69 69 *
70 70 * @param string $clientid The API client ID.
71 71 * @param string $secretkey The API secret key.
72 - * @return string|array|object
72 + * @return string|array
73 73 */
74 74 public function fetch_access_token( $clientid = '', $secretkey = '' ) {
75 75 // Skeleton for the response data.
76 - $response_data = (object) array(
76 + $response_data = (object) [
77 77 'result' => '',
78 78 'message' => '',
79 79 'expiresin' => '',
80 - );
80 + ];
81 81
82 82 // Determine if the license id/key is set.
83 - $client_id = $this->get_blog_option( $this->blog_id, 'patchstack_clientid', false ) ? $this->get_blog_option( $this->blog_id, 'patchstack_clientid', false ) : $clientid;
84 - $client_secret = $this->get_blog_option( $this->blog_id, 'patchstack_secretkey', false ) ? $this->get_blog_option( $this->blog_id, 'patchstack_secretkey', false ) : $secretkey;
83 + $client_id = $this->get_blog_option( $this->blog_id, 'patchstack_clientid', $clientid );
84 +
85 + // Decrypt the secret key, if it is encrypted.
86 + $client_secret = $this->get_blog_option( $this->blog_id, 'patchstack_secretkey', $secretkey );
87 + $client_nonce = $this->get_blog_option( $this->blog_id, 'patchstack_secretkey_nonce', false );
88 + if ( $client_nonce ) {
89 + $client_secret = $this->decrypt( $client_secret, $client_nonce );
90 + }
91 +
92 + // Make sure these values are set.
85 93 if ( empty( $client_id ) || empty( $client_secret ) ) {
86 94 $response_data->result = 'failed';
87 95 $response_data->message = __( 'API keys missing! Unable to obtain an access token.', 'patchstack' );
88 96 return $response_data;
@@ -90,22 +98,22 @@
90 98
91 99 // Send a request to our server to obtain the access token.
92 100 $response = wp_remote_post(
93 101 $this->plugin->auth_url . '/oauth/token',
94 - array(
102 + [
95 103 'method' => 'POST',
96 104 'timeout' => 60,
97 105 'redirection' => 5,
98 106 'httpversion' => '1.0',
99 107 'blocking' => true,
100 - 'headers' => array(),
101 - 'body' => array(
108 + 'headers' => [],
109 + 'body' => [
102 110 'client_id' => $client_id,
103 111 'client_secret' => $client_secret,
104 112 'grant_type' => 'client_credentials',
105 - ),
106 - 'cookies' => array(),
107 - )
113 + ],
114 + 'cookies' => [],
115 + ]
108 116 );
109 117
110 118 // Stop if we received an error from the API.
111 119 if ( is_wp_error( $response ) ) {
@@ -176,8 +184,22 @@
176 184 if ( isset( $response['active'] ) && $response['active'] == true ) {
177 185 $this->update_blog_option( $this->blog_id, 'patchstack_license_activated', true );
178 186 }
179 187
188 + if ( isset( $response['class'] ) ) {
189 + $this->update_blog_option( $this->blog_id, 'patchstack_subscription_class', $response['class'] );
190 + $this->update_blog_option( $this->blog_id, 'patchstack_last_license_check', time() );
191 + }
192 +
193 + if ( isset( $response['managed'], $response['managed_string'] ) ) {
194 + $this->update_blog_option( $this->blog_id, 'patchstack_managed', $response['managed'] );
195 + $this->update_blog_option( $this->blog_id, 'patchstack_managed_text', $response['managed_string'] );
196 + }
197 +
198 + if ( isset( $response['site_id'] ) ) {
199 + $this->update_blog_option( $this->blog_id, 'patchstack_site_id', $response['site_id'] );
200 + }
201 +
180 202 return $response;
181 203 }
182 204
183 205 /**
@@ -187,9 +209,9 @@
187 209 * @param string $request
188 210 * @param array $data
189 211 * @return void|array If successful array, otherwise void.
190 212 */
191 - public function send_request( $url, $request, $data = array() ) {
213 + public function send_request( $url, $request, $data = [] ) {
192 214 // Attempt to get the access token.
193 215 $token = $this->get_access_token();
194 216 if ( empty( $token ) ) {
195 217 return;
@@ -197,22 +219,22 @@
197 219
198 220 // Send the remote request using the WordPress built-in method.
199 221 $response = wp_remote_request(
200 222 $this->plugin->api_url . $url,
201 - array(
223 + [
202 224 'method' => $request,
203 225 'timeout' => 60,
204 226 'redirection' => 5,
205 227 'httpversion' => '1.0',
206 228 'blocking' => true,
207 - 'headers' => array(
229 + 'headers' => [
208 230 'Authorization' => 'Bearer ' . $token,
209 231 'LicenseID' => $this->get_blog_option( $this->blog_id, 'patchstack_clientid', 0 ),
210 232 'Source-Host' => get_site_url(),
211 - ),
233 + ],
212 234 'body' => $data,
213 - 'cookies' => array(),
214 - )
235 + 'cookies' => [],
236 + ]
215 237 );
216 238
217 239 // Check error or status code.
218 240 if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) {
@@ -230,9 +252,9 @@
230 252 {
231 253 $header = get_option( 'patchstack_firewall_ip_header', '' );
232 254 $computed = get_option( 'patchstack_ip_header_computed', 0 );
233 255
234 - if ( $header == '' && !$computed ) {
256 + if ( $header == '' && ! $computed ) {
235 257 // Create an OTT token.
236 258 $ott = md5( wp_generate_password( 32, true, true ) );
237 259 update_option( 'patchstack_ott_action', $ott );
238 260
@@ -238,23 +260,23 @@
238 260
239 261 // Tell our API.
240 262 wp_remote_request(
241 263 $this->plugin->api_url . '/api/header',
242 - array(
264 + [
243 265 'method' => 'POST',
244 266 'timeout' => 60,
245 267 'redirection' => 5,
246 268 'httpversion' => '1.0',
247 269 'blocking' => true,
248 - 'headers' => array(
270 + 'headers' => [
249 271 'Source-Host' => get_site_url(),
250 - ),
251 - 'body' => array(
272 + ],
273 + 'body' => [
252 274 'token' => $ott,
253 275 'url' => get_site_url()
254 - ),
255 - 'cookies' => array(),
256 - )
276 + ],
277 + 'cookies' => [],
278 + ]
257 279 );
258 280 }
259 281 }
260 282
@@ -263,14 +285,9 @@
263 285 *
264 286 * @return array The firewall rules.
265 287 */
266 288 public function post_firewall_rule_json() {
267 - // If the request is coming from the API, fetch fresh rules.
268 - if ( isset( $_POST['webarx_refresh_rules'] ) ) {
269 - return $this->send_request( '/api/get-rules/2?bypass=cache', 'POST' );
270 - }
271 -
272 - return $this->send_request( '/api/get-rules/2', 'POST' );
289 + return $this->send_request( '/api/get-rules/3', 'POST' );
273 290 }
274 291
275 292 /**
276 293 * Get the .htaccess rules.
@@ -360,7 +377,7 @@
360 377 *
361 378 * @return void
362 379 */
363 380 public function ping() {
364 - $this->send_request( '/api/ping', 'POST', array( 'firewall' => $this->get_option( 'patchstack_basic_firewall' ) == 1 ? 1 : 0 ) );
381 + $this->send_request( '/api/ping', 'POST', [ 'firewall' => $this->get_option( 'patchstack_basic_firewall' ) == 1 ? 1 : 0 ] );
365 382 }
366 383 }