Events
2 years ago
Purger
2 years ago
AHSC_Check.php
1 year ago
AHSC_Config.php
1 year ago
AHSC_Functions.php
1 year ago
AHSC_Static.php
1 year ago
AHSC_Version.php
2 years ago
AHSC_Warmer.php
2 years ago
index.php
2 years ago
AHSC_Check.php
318 lines
| 1 | <?php |
| 2 | const ACTIVE = 'active'; |
| 3 | const AVAILABLE = 'available'; |
| 4 | const UNAVAILABLE = 'unavailable'; |
| 5 | const NOARUBASERVER = 'no-aruba-server'; |
| 6 | |
| 7 | $target=AHSC_CONSTANT['HOME_URL']; |
| 8 | //var_dump(AHSC_CONSTANT); |
| 9 | $check_error=false; |
| 10 | $check_status_code=false; |
| 11 | $check_parameters=array( |
| 12 | 'is_aruba_server'=>"", |
| 13 | 'service_is_active'=>"", |
| 14 | 'service_is_activabile'=>"", |
| 15 | 'service_status'=>"", |
| 16 | 'esit'=>"" |
| 17 | ); |
| 18 | |
| 19 | /** |
| 20 | * Set_parameters_to_check Set the parameters to perform the service check. |
| 21 | * |
| 22 | * @param boolean $is_aruba_server Set to true if the site is on aruba server. |
| 23 | * @param boolean $service_is_active Set to true if the service is active. |
| 24 | * @param boolean $service_is_activabile Set to true if the service is activabile. |
| 25 | * @param string $service_status The value of x-aruba-cache header def is ''. |
| 26 | * @return void |
| 27 | */ |
| 28 | function AHSC_set_parameters_to_check( |
| 29 | $is_aruba_server, |
| 30 | $service_is_active, |
| 31 | $service_is_activabile, |
| 32 | $service_status = '' |
| 33 | ) { |
| 34 | global $check_parameters; |
| 35 | $check_parameters['is_aruba_server'] = $is_aruba_server; |
| 36 | $check_parameters['service_is_active' ] = $service_is_active; |
| 37 | $check_parameters['service_is_activabile'] = $service_is_activabile; |
| 38 | $check_parameters['service_status'] = $service_status; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Headers_analizer - Analyze the request headers and value the variables. |
| 43 | * |
| 44 | * @since 1.0.1 |
| 45 | * @return void |
| 46 | */ |
| 47 | function AHSC_headers_analizer() { |
| 48 | $headers=AHSC_get_headers(); |
| 49 | /** |
| 50 | * If the request headers are empty or the request |
| 51 | * produced a wp_error then I set everything to true. |
| 52 | */ |
| 53 | if ( empty( $headers ) ) { |
| 54 | AHSC_set_parameters_to_check( false, false, false ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * If the headers contain 'x-aruba-cache' we are on an aruba server. |
| 59 | * If it has value NA we are on servers without cache. |
| 60 | */ |
| 61 | if ( is_array($headers) && array_key_exists( 'x-aruba-cache', $headers ) ) { |
| 62 | $is_active = true; |
| 63 | $is_activabile = true; |
| 64 | if ( 'NA' === $headers['x-aruba-cache'] ) { |
| 65 | $is_activabile = false; |
| 66 | $is_active = false; |
| 67 | } |
| 68 | AHSC_set_parameters_to_check( true, $is_active, $is_activabile, $headers['x-aruba-cache'] ); |
| 69 | |
| 70 | }else{ |
| 71 | AHSC_set_parameters_to_check( false, false, false ); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * If the headers do not contain 'x-aruba-cache' |
| 77 | * we are not on the aruba server. |
| 78 | * |
| 79 | * If the 'server' header contains 'aruba-proxy' |
| 80 | * the service can be activated. |
| 81 | * |
| 82 | * If it is different from 'aruba-proxy' we are |
| 83 | * not on aruba server or behind cdn. |
| 84 | */ |
| 85 | if ( is_array($headers) && array_key_exists( 'server', $headers ) ) { |
| 86 | switch ( $headers['server'] ) { |
| 87 | case 'aruba-proxy': |
| 88 | if ( is_array($headers) && array_key_exists( 'x-aruba-cache', $headers ) ){ |
| 89 | if( 'NA' !== $headers['x-aruba-cache'] ){ |
| 90 | $_status=$headers['x-aruba-cache']; |
| 91 | }else{ |
| 92 | $_status=UNAVAILABLE; |
| 93 | } |
| 94 | } |
| 95 | AHSC_set_parameters_to_check( true, true, true , $_status); |
| 96 | break; |
| 97 | default: |
| 98 | AHSC_set_parameters_to_check( false, false, false ); |
| 99 | |
| 100 | if ( array_key_exists( 'x-servername', $headers ) && str_contains( $headers['x-servername'], 'aruba.it' ) ) { |
| 101 | AHSC_set_parameters_to_check( true, false, true ); |
| 102 | } |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Check - Check the populated variables and issue a control message. |
| 110 | * |
| 111 | * @since 1.0.1 |
| 112 | * @return array |
| 113 | */ |
| 114 | function AHSC_check() { |
| 115 | global $check_parameters; |
| 116 | AHSC_get_headers(); |
| 117 | AHSC_headers_analizer(); |
| 118 | |
| 119 | $check_parameters['esit'] = ACTIVE; |
| 120 | |
| 121 | if ( $check_parameters['is_aruba_server'] && ! $check_parameters['service_is_active'] ) { |
| 122 | $check_parameters['esit'] = ( $check_parameters['service_is_activabile'] ) ? ACTIVE : UNAVAILABLE; |
| 123 | |
| 124 | }else{ |
| 125 | $check_parameters['esit'] = ( $check_parameters['service_is_activabile'] ) ? ACTIVE : NOARUBASERVER; |
| 126 | } |
| 127 | |
| 128 | //var_dump($check_parameters); |
| 129 | |
| 130 | return $check_parameters; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Debugger - It exposes some elements of the control to |
| 135 | * try to resolve any errors. To activate it, just go to the |
| 136 | * dominio.tld/wp-admin/options-general.php?page=aruba-hispeed-cache&debug=1 |
| 137 | * |
| 138 | * @return string |
| 139 | */ |
| 140 | function debugger() { |
| 141 | global $check_error,$check_status_code; |
| 142 | |
| 143 | $headers=AHSC_get_headers(); |
| 144 | |
| 145 | $check_parameters=AHSC_check(); |
| 146 | |
| 147 | $data = array( |
| 148 | 'date' => \wp_date( 'D, d M Y H:i:s', time() ), |
| 149 | 'target' => AHSC_CONSTANT['HOME_URL'], |
| 150 | 'headers' => $headers, |
| 151 | 'status_code' => $check_status_code, |
| 152 | 'check_params' => $check_parameters, |
| 153 | ); |
| 154 | |
| 155 | if ( $check_error ) { |
| 156 | $data['error'] = 'Sorry but the call was answered with an error. please try again later'; |
| 157 | unset( $data['headers'] ); |
| 158 | unset( $data['status_code'] ); |
| 159 | unset( $data['check_params'] ); |
| 160 | } |
| 161 | |
| 162 | return var_export( $data, true ); //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Get_headers - Getter of the headers for the request to perform the check. |
| 167 | * |
| 168 | * @since 1.0.1 |
| 169 | * @return bool |
| 170 | */ |
| 171 | function AHSC_get_headers() { |
| 172 | |
| 173 | global $check_status_code,$check_error; |
| 174 | |
| 175 | |
| 176 | $response = \wp_remote_get( |
| 177 | AHSC_CONSTANT['HOME_URL'], |
| 178 | array( |
| 179 | 'sslverify' => false, |
| 180 | 'user-agent' => 'aruba-ua', |
| 181 | 'httpversion' => '1.1', |
| 182 | 'timeout' =>AHSC_CHECKER['request_timeout'], |
| 183 | ) |
| 184 | ); |
| 185 | |
| 186 | |
| 187 | if ( \is_array( $response ) && ! \is_wp_error( $response ) ) { |
| 188 | $headers = $response['headers']->getAll(); |
| 189 | $check_status_code = $response['response']['code']; |
| 190 | |
| 191 | return $headers; |
| 192 | } |
| 193 | |
| 194 | if ( \is_wp_error( $response ) ) { |
| 195 | $check_error = true; |
| 196 | } |
| 197 | |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Check_hispeed_cache_services - check if aruba hispeed cache service is activable or is a aruba server. |
| 203 | * |
| 204 | * @param string $plugin The plugin fine relative path. |
| 205 | * @return void |
| 206 | * |
| 207 | * @SuppressWarnings(PHPMD.StaticAccess) |
| 208 | */ |
| 209 | function check_hispeed_cache_services( $plugin ) { |
| 210 | if ( 'aruba-hispeed-cache/aruba-hispeed-cache.php' === $plugin ) { |
| 211 | $check = AHSC_check(); |
| 212 | |
| 213 | if ( \is_multisite() ) { |
| 214 | \set_site_transient( |
| 215 | AHSC_CHECKER['transient_name'], |
| 216 | $check, |
| 217 | AHSC_CHECKER['transient_life_time'] |
| 218 | ); |
| 219 | }else{ |
| 220 | \set_transient( |
| 221 | AHSC_CHECKER['transient_name'], |
| 222 | $check, |
| 223 | AHSC_CHECKER['transient_life_time'] |
| 224 | ); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if ( ! \function_exists( 'ahsc_get_check_notice' ) ) { |
| 230 | |
| 231 | /** |
| 232 | * Return null or the admin notice to render. |
| 233 | * |
| 234 | * @param string $notice_type The option key to get. |
| 235 | * @return null|string |
| 236 | * |
| 237 | * @SuppressWarnings(PHPMD.StaticAccess) |
| 238 | */ |
| 239 | function ahsc_get_check_notice( $notice_type ) { |
| 240 | $localize_link = AHSC_LOCALIZE_LINK; // For php 5.6 compatibility. |
| 241 | $notice = null; |
| 242 | $lng=strtolower(substr( get_bloginfo ( 'language' ), 0, 2 )); |
| 243 | |
| 244 | if($notice_type){ |
| 245 | $notice_type=(array) $notice_type; |
| 246 | switch ( $notice_type['esit'] ) { |
| 247 | case AVAILABLE: |
| 248 | $notice['handle'] = 'ahsc-service-warning'; |
| 249 | $notice['type'] = 'warning'; |
| 250 | $notice['message'] = \sprintf( |
| 251 | \wp_kses( |
| 252 | // translators: %1$s: the pca url. |
| 253 | // translators: %2$s: the guide url. |
| 254 | __( '<strong>The HiSpeed Cache service is not enabled.</strong> To activate it, go to your domain <a href="%1$s" rel="nofollow" target="_blank">control panel</a> (verifying the status may take up to 15 minutes). For further details <a href="%2$s" rel="nofollow" target="_blank">see our guide</a>.', 'aruba-hispeed-cache' ), |
| 255 | array( |
| 256 | 'strong' => array(), |
| 257 | 'a' => array( |
| 258 | 'href' => array(), |
| 259 | 'target' => array(), |
| 260 | 'rel' => array(), |
| 261 | ), |
| 262 | ) |
| 263 | ), |
| 264 | esc_html( $localize_link['link_aruba_pca'][$lng] ), |
| 265 | esc_html( $localize_link['link_guide'][$lng] ) |
| 266 | ); |
| 267 | break; |
| 268 | case UNAVAILABLE: |
| 269 | $notice['handle'] = 'ahsc-service-error'; |
| 270 | $notice['type'] = 'error'; |
| 271 | $notice['message'] = \sprintf( |
| 272 | \wp_kses( |
| 273 | // translators: %s: the assistance url. |
| 274 | __( '<strong>The HiSpeed Cache service with which the plugin interfaces is not available on the server that hosts your website.</strong> To use HiSpeed Cache and the plugin, please contact <a href="%s" rel="nofollow" target="_blank">support</a>.', 'aruba-hispeed-cache' ), |
| 275 | array( |
| 276 | 'strong' => array(), |
| 277 | 'a' => array( |
| 278 | 'href' => array(), |
| 279 | 'target' => array(), |
| 280 | 'rel' => array(), |
| 281 | ), |
| 282 | ) |
| 283 | ), |
| 284 | esc_html( $localize_link['link_assistance'][$lng] ) |
| 285 | ); |
| 286 | break; |
| 287 | case NOARUBASERVER: |
| 288 | $notice['handle'] = 'ahsc-service-error'; |
| 289 | $notice['type'] = 'error'; |
| 290 | $notice['message'] = \sprintf( |
| 291 | \wp_kses( |
| 292 | // translators: %s: the hosting truck url. |
| 293 | __( '<strong>The Aruba HiSpeed Cache plugin cannot be used because your WordPress website is not hosted on an Aruba hosting platform.</strong> Buy an <a href="%s" rel="nofollow" target="_blank">Aruba Hosting service</a> and migrate your website to use the plugin.', 'aruba-hispeed-cache' ), |
| 294 | array( |
| 295 | 'strong' => array(), |
| 296 | 'a' => array( |
| 297 | 'href' => array(), |
| 298 | 'target' => array(), |
| 299 | 'rel' => array(), |
| 300 | ), |
| 301 | ) |
| 302 | ), |
| 303 | esc_html( $localize_link[ 'link_hosting_truck' ][$lng] ) |
| 304 | ); |
| 305 | break; |
| 306 | } |
| 307 | |
| 308 | if ( ACTIVE !== $notice_type['esit'] ) { |
| 309 | $notice = AHSC_Notice_Render( $notice['handle'], $notice['type'],$notice['message'], true ); |
| 310 | } |
| 311 | |
| 312 | }else{ |
| 313 | $notice=null; |
| 314 | } |
| 315 | |
| 316 | return $notice; |
| 317 | } |
| 318 | } |