ArubaHiSpeedCacheAdmin.php
4 years ago
ArubaHiSpeedCacheBootstrap.php
3 years ago
ArubaHiSpeedCacheConfigs.php
3 years ago
ArubaHiSpeedCacheLoader.php
3 years ago
ArubaHiSpeedCachePurger.php
3 years ago
ArubaHiSpeedCacheWpPurger.php
3 years ago
ArubaHiSpeedCachei18n.php
3 years ago
ArubaHispeedCacheLogger.php
3 years ago
HiSpeedCacheServiceChecker.php
3 years ago
index.php
3 years ago
HiSpeedCacheServiceChecker.php
283 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ArubaHiSpeedCacheBootstrap - Control center for everything. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category Component |
| 7 | * @package Aruba-HiSpeed-Cache |
| 8 | * @author Aruba Developer <hispeedcache.developer@aruba.it> |
| 9 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPLv2 or later |
| 10 | * @link null |
| 11 | * @since 1.0.1 |
| 12 | */ |
| 13 | |
| 14 | namespace ArubaHiSpeedCache\includes; |
| 15 | |
| 16 | use ArubaHiSpeedCache\includes\ArubaHiSpeedCacheConfigs; |
| 17 | |
| 18 | use \is_array; |
| 19 | |
| 20 | use \is_wp_error; |
| 21 | use \wp_remote_get; |
| 22 | |
| 23 | if (!class_exists(__NAMESPACE__ . '\HiSpeedCacheServiceChecker')) { |
| 24 | |
| 25 | /** |
| 26 | * ArubaHiSpeedCacheConfigs |
| 27 | * |
| 28 | * @category ArubaHiSpeedCache |
| 29 | * @package ArubaHiSpeedCache |
| 30 | * @author Aruba Developer <hispeedcache.developer@aruba.it> |
| 31 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPLv2 or later |
| 32 | * @link null |
| 33 | * @since 1.0.1 |
| 34 | */ |
| 35 | class HiSpeedCacheServiceChecker |
| 36 | { |
| 37 | /** |
| 38 | * Target The address you want to test default is null |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | // phpcs:ignore |
| 43 | private $target = null; |
| 44 | |
| 45 | /** |
| 46 | * Header Request header parameters. |
| 47 | * |
| 48 | * @var array |
| 49 | */ |
| 50 | // phpcs:ignore |
| 51 | private $headers = array(); |
| 52 | |
| 53 | /** |
| 54 | * $status_code The code of the request. |
| 55 | * It is used to perform some integrity checks |
| 56 | * |
| 57 | * @var int |
| 58 | */ |
| 59 | // phpcs:ignore |
| 60 | private $status_code = null; |
| 61 | |
| 62 | /** |
| 63 | * ServiceIsActivabile |
| 64 | * Valued as true if the site is hosted on OW Aruba servers. Default False |
| 65 | * |
| 66 | * @var boolean |
| 67 | */ |
| 68 | public $serviceIsActivabile = false; |
| 69 | |
| 70 | /** |
| 71 | * ServiceIsActive |
| 72 | * Valued as true if the site is hosted on OW Aruba servers |
| 73 | * and the service is active. |
| 74 | * Default False |
| 75 | * |
| 76 | * @var boolean |
| 77 | */ |
| 78 | public $serviceIsActive = false; |
| 79 | |
| 80 | /** |
| 81 | * ServiceIsActive |
| 82 | * Valued on the basis of the x-aruba-cache value. |
| 83 | * |
| 84 | * @var string |
| 85 | */ |
| 86 | public $serviceStatus = null; |
| 87 | |
| 88 | /** |
| 89 | * IsArubaServer Control variables that is used to determine |
| 90 | * if you are on Aruba server or not. |
| 91 | * |
| 92 | * @var boolean |
| 93 | */ |
| 94 | public $isArubaServer = false; |
| 95 | |
| 96 | /** |
| 97 | * Check_error Control variable to determine |
| 98 | * if an error was encountered during the request. |
| 99 | * |
| 100 | * @var boolean |
| 101 | */ |
| 102 | // phpcs:ignore |
| 103 | private $check_error = false; |
| 104 | |
| 105 | // |
| 106 | // Error_code Control variable to determine |
| 107 | // if an error was encountered during the request. |
| 108 | // |
| 109 | // @var int |
| 110 | // |
| 111 | // phpcs:ignore |
| 112 | //private $error_code = null; |
| 113 | |
| 114 | // |
| 115 | // Error_code Control variable to determine |
| 116 | // if an error was encountered during the request. |
| 117 | // |
| 118 | // @var string |
| 119 | // |
| 120 | // phpcs:ignore |
| 121 | //private $error_message = null; |
| 122 | |
| 123 | /** |
| 124 | * HiSpeedCacheServiceChecker |
| 125 | * |
| 126 | * @param string $target Target The address you want to test default is null |
| 127 | * |
| 128 | * @since 1.0.1 |
| 129 | * @return void |
| 130 | */ |
| 131 | public function __construct($target = null) |
| 132 | { |
| 133 | $this->target = (null === $target) ? |
| 134 | \ArubaHiSpeedCache\includes\ArubaHiSpeedCacheConfigs::getSiteHome() : |
| 135 | $target; |
| 136 | |
| 137 | $this->_getHeaders(); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * _getHeaders - Getter of the headers for the request to perform the check. |
| 142 | * |
| 143 | * @since 1.0.1 |
| 144 | * @return bool |
| 145 | */ |
| 146 | private function _getHeaders() |
| 147 | { |
| 148 | $response = \wp_remote_get( |
| 149 | $this->target, |
| 150 | array( |
| 151 | 'sslverify' => false, |
| 152 | 'user-agent' => 'aruba-ua', |
| 153 | 'httpversion' => '1.1', |
| 154 | 'timeout' => \ArubaHiSpeedCache\includes\ArubaHiSpeedCacheConfigs::ArubaHiSpeedCache_getConfigs('CHECK_TIMEOUT') |
| 155 | ) |
| 156 | ); |
| 157 | |
| 158 | if (\is_array($response) && ! \is_wp_error($response)) { |
| 159 | $this->headers = $response['headers']->getAll(); |
| 160 | $this->status_code = $response['response']['code']; |
| 161 | |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | if (\is_wp_error($response)) { |
| 166 | $this->check_error = true; |
| 167 | // $this->error_code = $response->get_error_code(); |
| 168 | // $this->error_message = $response->get_error_message(); |
| 169 | } |
| 170 | |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * _headersAnalizer - Analyze the request headers and value the variables. |
| 176 | * |
| 177 | * @since 1.0.1 |
| 178 | * @return void |
| 179 | */ |
| 180 | private function _headersAnalizer() |
| 181 | { |
| 182 | |
| 183 | /** |
| 184 | * If the request headers are empty or the request |
| 185 | * produced a wp_error then I set everything to true. |
| 186 | */ |
| 187 | if (empty($this->headers) or $this->check_error) { |
| 188 | $this->isArubaServer = true; |
| 189 | $this->serviceIsActive = true; |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * If the headers contain 'x-aruba-cache' we are on an aruba server. |
| 195 | * If it has value NA we are on servers without cache. |
| 196 | */ |
| 197 | if (array_key_exists('x-aruba-cache', $this->headers)) { |
| 198 | $this->isArubaServer = true; |
| 199 | $this->serviceStatus = $this->headers['x-aruba-cache']; |
| 200 | |
| 201 | switch ($this->headers['x-aruba-cache']) { |
| 202 | case 'NA': |
| 203 | $this->serviceIsActivabile = false; |
| 204 | $this->serviceIsActive = false; |
| 205 | break; |
| 206 | default: |
| 207 | $this->serviceIsActive = true; |
| 208 | break; |
| 209 | } |
| 210 | |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /** |
| 216 | * If the headers do not contain 'x-aruba-cache' |
| 217 | * we are not on the aruba server. |
| 218 | * |
| 219 | * If the 'server' header contains 'aruba-proxy' |
| 220 | * the service can be activated. |
| 221 | * |
| 222 | * If it is different from 'aruba-proxy' we are |
| 223 | * not on aruba server or behind cdn. |
| 224 | */ |
| 225 | if (array_key_exists('server', $this->headers)) { |
| 226 | switch ($this->headers['server']) { |
| 227 | case 'aruba-proxy': |
| 228 | $this->serviceIsActivabile = true; |
| 229 | break; |
| 230 | default: |
| 231 | $this->serviceIsActivabile = false; |
| 232 | |
| 233 | if (array_key_exists('x-servername', $this->headers) |
| 234 | && str_contains($this->headers['x-servername'], 'aruba.it') |
| 235 | ) { |
| 236 | $this->serviceIsActivabile = true; |
| 237 | } |
| 238 | break; |
| 239 | } |
| 240 | return; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Check - Check the populated variables and issue a control message. |
| 246 | * |
| 247 | * @since 1.0.1 |
| 248 | * @return string |
| 249 | */ |
| 250 | public function check() |
| 251 | { |
| 252 | $this->_headersAnalizer(); |
| 253 | |
| 254 | if ($this->isArubaServer && !$this->serviceIsActive) { |
| 255 | return ($this->serviceIsActivabile) ? 'available' : 'unavailable'; |
| 256 | } |
| 257 | |
| 258 | if (!$this->isArubaServer && !$this->serviceIsActive) { |
| 259 | return ($this->serviceIsActivabile) ? 'available' : 'no-aruba-server'; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Debugger - It exposes some elements of the control to |
| 265 | * try to resolve any errors. To activate it, just go to the |
| 266 | * dominio.tld/wp-admin/options-general.php?page=aruba-hispeed-cache&debug=1 |
| 267 | * |
| 268 | * @return string |
| 269 | */ |
| 270 | public function debugger() |
| 271 | { |
| 272 | $data = array( |
| 273 | 'date' => date('D, d M Y H:i:s', time()), |
| 274 | 'target' => $this->target, |
| 275 | 'headers' => $this->headers, |
| 276 | 'status_code' => $this->status_code |
| 277 | ); |
| 278 | |
| 279 | return var_export($data, true); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 |