| @@ -68,9 +68,9 @@ | ||
| 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 | |
| 72 | + * @return string|array|object | |
| 73 | 73 | */ |
| 74 | 74 | public function fetch_access_token( $clientid = '', $secretkey = '' ) { |
| 75 | 75 | // Skeleton for the response data. |
| 76 | 76 | $response_data = (object) array( |
| @@ -79,10 +79,18 @@ | ||
| 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; |
| @@ -128,17 +136,10 @@ | ||
| 128 | 136 | $response_data->message = 'expires_in value must be an integer'; |
| 129 | 137 | return $response_data; |
| 130 | 138 | } |
| 131 | 139 | $response_data->expiresin = $result->expires_in != 0 ? time() + $result->expires_in : 0; |
| 132 | - } elseif ( ! empty( $result->expires_in ) ) { | |
| 133 | - // Some providers supply the seconds until expiration rather than | |
| 134 | - // the exact timestamp. Take a best guess at which we received. | |
| 135 | - $expires = $options['expires']; | |
| 136 | - if ( ! $this->isExpirationTimestamp( $expires ) ) { | |
| 137 | - $expires += time(); | |
| 138 | - } | |
| 139 | - $response_data->expiresin = $expires; | |
| 140 | 140 | } |
| 141 | + | |
| 141 | 142 | return $response_data; |
| 142 | 143 | } elseif ( isset( $result->error ) ) { |
| 143 | 144 | $response_data->result = $result->error; |
| 144 | 145 | $response_data->message = __( 'Unexpected error! Unable to obtain an access token.', 'patchstack' ) . $result->message; |
| @@ -174,8 +175,10 @@ | ||
| 174 | 175 | $this->update_blog_option( $this->blog_id, 'patchstack_license_free', $response['free'] == false ? 0 : 1 ); |
| 175 | 176 | |
| 176 | 177 | if ( $response['free'] == true ) { |
| 177 | 178 | $this->update_blog_option( $this->blog_id, 'patchstack_show_settings', 0 ); |
| 179 | + } else { | |
| 180 | + $this->send_header_request(); | |
| 178 | 181 | } |
| 179 | 182 | } |
| 180 | 183 | |
| 181 | 184 | if ( isset( $response['active'] ) && $response['active'] == true ) { |
| @@ -225,8 +228,43 @@ | ||
| 225 | 228 | return; |
| 226 | 229 | } |
| 227 | 230 | |
| 228 | 231 | return json_decode( wp_remote_retrieve_body( $response ), true ); |
| 232 | + } | |
| 233 | + | |
| 234 | + /** | |
| 235 | + * Send a request to our API for the IP address header. | |
| 236 | + */ | |
| 237 | + public function send_header_request() | |
| 238 | + { | |
| 239 | + $header = get_option( 'patchstack_firewall_ip_header', '' ); | |
| 240 | + $computed = get_option( 'patchstack_ip_header_computed', 0 ); | |
| 241 | + | |
| 242 | + if ( $header == '' && ! $computed ) { | |
| 243 | + // Create an OTT token. | |
| 244 | + $ott = md5( wp_generate_password( 32, true, true ) ); | |
| 245 | + update_option( 'patchstack_ott_action', $ott ); | |
| 246 | + | |
| 247 | + // Tell our API. | |
| 248 | + wp_remote_request( | |
| 249 | + $this->plugin->api_url . '/api/header', | |
| 250 | + array( | |
| 251 | + 'method' => 'POST', | |
| 252 | + 'timeout' => 60, | |
| 253 | + 'redirection' => 5, | |
| 254 | + 'httpversion' => '1.0', | |
| 255 | + 'blocking' => true, | |
| 256 | + 'headers' => array( | |
| 257 | + 'Source-Host' => get_site_url(), | |
| 258 | + ), | |
| 259 | + 'body' => array( | |
| 260 | + 'token' => $ott, | |
| 261 | + 'url' => get_site_url() | |
| 262 | + ), | |
| 263 | + 'cookies' => array(), | |
| 264 | + ) | |
| 265 | + ); | |
| 266 | + } | |
| 229 | 267 | } |
| 230 | 268 | |
| 231 | 269 | /** |
| 232 | 270 | * Get the firewall rules. |