nitropack
Last commit date
classes
3 years ago
nitropack-sdk
3 years ago
view
3 years ago
advanced-cache.php
3 years ago
batcache-compat.php
4 years ago
cf-helper.php
5 years ago
constants.php
3 years ago
diagnostics.php
4 years ago
functions.php
3 years ago
integrations.php
4 years ago
main.php
3 years ago
readme.txt
3 years ago
uninstall.php
4 years ago
wp-cli.php
5 years ago
diagnostics.php
215 lines
| 1 | <?php |
| 2 | |
| 3 | defined( 'ABSPATH' ) or die( 'Someone made a boo boo!' ); |
| 4 | |
| 5 | use \NitroPack\SDK\Api\ResponseStatus; |
| 6 | |
| 7 | if (!function_exists('get_plugins')) { |
| 8 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 9 | } |
| 10 | |
| 11 | $np_diag_functions = array( |
| 12 | 'general-info-status' => 'npdiag_get_general_info', |
| 13 | 'active-plugins-status' => 'npdiag_get_active_plugins', |
| 14 | 'conflicting-plugins-status' => 'npdiag_get_conflicting_plugins', |
| 15 | 'user-config-status' => 'npdiag_get_user_config', |
| 16 | 'dir-info-status' => 'npdiag_get_dir_info', |
| 17 | 'getexternalcache' => 'npdiag_detect_third_party_cache' |
| 18 | |
| 19 | ); |
| 20 | |
| 21 | function npdiag_helper_trailingslashit($string) { |
| 22 | return rtrim( $string, '/\\' ) . '/'; |
| 23 | } |
| 24 | |
| 25 | function npdiag_compare_webhooks($nitro_sdk) { |
| 26 | try { |
| 27 | $siteConfig = nitropack_get_site_config(); |
| 28 | if (!empty($siteConfig['siteId'])) { |
| 29 | $WHToken = nitropack_generate_webhook_token($siteConfig['siteId']); |
| 30 | $constructedWH = new \NitroPack\Url(strtolower(get_home_url())) . '?nitroWebhook=config&token=' . $WHToken; |
| 31 | $storedWH = $nitro_sdk->getApi()->getWebhook("config"); |
| 32 | $matchResult = ($constructedWH == $storedWH) ? 'OK' : 'Warning: Webhooks do not match this site'; |
| 33 | } else { |
| 34 | $debugMsg = empty($_SERVER["HTTP_HOST"]) ? "HTTP_HOST is not defined. " : ""; |
| 35 | $debugMsg .= empty($_SERVER["REQUEST_URI"]) ? "REQUEST_URI is not defined. " : ""; |
| 36 | $debugMsg .= empty($debugMsg) ? 'URL used to match config was: ' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] : ""; |
| 37 | $matchResult = 'Site config cannot be found, because ' . $debugMsg; |
| 38 | } |
| 39 | return $matchResult; |
| 40 | } catch (\Exception $e) { |
| 41 | return $e->getMessage(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | function npdiag_poll_api($nitro_sdk) { |
| 46 | $pollResult = array( |
| 47 | ResponseStatus::OK => 'OK', |
| 48 | ResponseStatus::ACCEPTED => 'OK', |
| 49 | ResponseStatus::BAD_REQUEST => 'Bad request.', |
| 50 | ResponseStatus::PAYMENT_REQUIRED => 'Payment required. Please, contact NP support for details.', |
| 51 | ResponseStatus::FORBIDDEN => 'Site disabled. Please, contact NP support for details.', |
| 52 | ResponseStatus::NOT_FOUND => 'URL used for the API poll request returned 404. Please ignore this.', |
| 53 | ResponseStatus::CONFLICT => 'Conflict. There is another operation, which prevents accepting optimization requests at the moment. Please, contact NP support for details.', |
| 54 | ResponseStatus::RUNTIME_ERROR => 'Runtime error.', |
| 55 | ResponseStatus::SERVICE_UNAVAILABLE => 'Service unavailable.', |
| 56 | ResponseStatus::UNKNOWN => 'Unknown.', |
| 57 | ); |
| 58 | |
| 59 | try { |
| 60 | $apiResponseCode = $nitro_sdk->getApi()->getCache(get_home_url(), 'NitroPack Diagnostic Agent', array(), false, 'default')->getStatus(); |
| 61 | return $pollResult[$apiResponseCode]; |
| 62 | } catch (\Exception $e) { |
| 63 | return 'Error: ' . $e->getMessage(); |
| 64 | } |
| 65 | |
| 66 | } |
| 67 | |
| 68 | function npdiag_backlog_status($nitro_sdk) { |
| 69 | return $nitro_sdk->backlog->exists() ? 'Warning' : 'OK'; |
| 70 | } |
| 71 | |
| 72 | function npdiag_get_general_info() { |
| 73 | global $wp_version; |
| 74 | if (null !== $nitro = get_nitropack_sdk()) { |
| 75 | $probe_result = "OK"; |
| 76 | try { |
| 77 | $nitro->fetchConfig(); |
| 78 | } catch (\Exception $e) { |
| 79 | $probe_result = 'Error: ' . $e->getMessage(); |
| 80 | } |
| 81 | } else { |
| 82 | $probe_result = 'Error: Cannot get an SDK instance'; |
| 83 | } |
| 84 | |
| 85 | $third_party_residual_cache = npdiag_detect_third_party_cache(); |
| 86 | |
| 87 | $info = array( |
| 88 | 'Nitro_WP_version' => !empty($wp_version) ? $wp_version : get_bloginfo('version'), |
| 89 | 'Nitro_Version' => defined('NITROPACK_VERSION') ? NITROPACK_VERSION : 'Undefined', |
| 90 | 'Nitro_SDK_Connection' => $probe_result, |
| 91 | 'Nitro_API_Polling' => $nitro ? npdiag_poll_api($nitro) : 'Error: Cannot get an SDK instance', |
| 92 | 'Nitro_SDK_Version' => defined('NitroPack\SDK\Nitropack::VERSION') ? NitroPack\SDK\Nitropack::VERSION : 'Undefined', |
| 93 | 'Nitro_WP_Cache' => defined('WP_CACHE') ? (WP_CACHE ? 'OK for drop-in' : 'Turned off') : 'Undefined', |
| 94 | 'Advanced_Cache_Version' => defined('NITROPACK_ADVANCED_CACHE_VERSION') ? NITROPACK_ADVANCED_CACHE_VERSION : 'Undefined', |
| 95 | 'Nitro_Absolute_Path' => defined('ABSPATH') ? ABSPATH : 'Undefined', |
| 96 | 'Nitro_Plugin_Direcotry' => defined('NITROPACK_PLUGIN_DIR') ? NITROPACK_PLUGIN_DIR : dirname(__FILE__), |
| 97 | 'Nitro_Data_Directory' => defined('NITROPACK_DATA_DIR') ? NITROPACK_DATA_DIR : 'Undefined', |
| 98 | 'Nitro_Config_File' => defined('NITROPACK_CONFIG_FILE') ? NITROPACK_CONFIG_FILE : 'Undefined', |
| 99 | 'Nitro_Backlog_File_Status' => $nitro ? npdiag_backlog_status($nitro) : 'Error: Cannot get an SDK instance', |
| 100 | 'Nitro_Webhooks' => $nitro ? npdiag_compare_webhooks($nitro) : 'Error: Cannot get an SDK instance', |
| 101 | 'Nitro_Connectivity_Requirements' => nitropack_check_func_availability('stream_socket_client') ? 'OK' : 'Warning: "stream_socket_client" function is disabled.', |
| 102 | 'Residual_Cache_Found_For' => $third_party_residual_cache, |
| 103 | ); |
| 104 | |
| 105 | if (defined("NITROPACK_VERSION") && defined("NITROPACK_ADVANCED_CACHE_VERSION") && NITROPACK_VERSION == NITROPACK_ADVANCED_CACHE_VERSION && nitropack_is_dropin_cache_allowed()) { |
| 106 | $info['Nitro_Cache_Method'] = 'drop-in'; |
| 107 | } elseif ( defined('EZOIC_INTEGRATION_VERSION') ) { |
| 108 | $info['Nitro_Cache_Method'] = 'plugin-ezoic'; |
| 109 | } else { |
| 110 | $info['Nitro_Cache_Method'] = 'plugin'; |
| 111 | } |
| 112 | |
| 113 | return $info; |
| 114 | } |
| 115 | |
| 116 | function npdiag_get_active_plugins() { |
| 117 | if (is_admin()) { |
| 118 | $info = array(); |
| 119 | $raw_installed_list = get_plugins(); |
| 120 | $raw_active_list = get_option('active_plugins'); |
| 121 | foreach ($raw_installed_list as $pkey => $pval) { |
| 122 | if ( in_array($pkey, $raw_active_list) ) { |
| 123 | $info[$pval['Name']] = $pval['Version']; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return $info; |
| 129 | } |
| 130 | |
| 131 | function npdiag_get_user_config() { |
| 132 | if (defined('NITROPACK_CONFIG_FILE')) { |
| 133 | if (file_exists(NITROPACK_CONFIG_FILE)) { |
| 134 | $info = json_decode(file_get_contents(NITROPACK_CONFIG_FILE)); |
| 135 | if (!$info) { |
| 136 | $info = 'Config found, but unable to get contents.'; |
| 137 | } |
| 138 | } else { |
| 139 | $info = 'Config file not found.'; |
| 140 | } |
| 141 | } else { |
| 142 | $info = 'Config file constant is not defined.'; |
| 143 | } |
| 144 | |
| 145 | return $info; |
| 146 | } |
| 147 | |
| 148 | function npdiag_get_dir_info() { |
| 149 | $siteConfig = nitropack_get_site_config(); |
| 150 | $siteID = !empty($siteConfig['siteId']) ? $siteConfig['siteId'] : get_option('nitropack-siteId'); |
| 151 | // DoI = Directories of Interest |
| 152 | $DoI = array( |
| 153 | 'WP_Content_Dir_Writable' => defined('WP_CONTENT_DIR') ? WP_CONTENT_DIR : (defined('ABSPATH') ? ABSPATH . '/wp-content' : 'Undefined'), |
| 154 | 'Nitro_Data_Dir_Writable' => defined('NITROPACK_DATA_DIR') ? NITROPACK_DATA_DIR : npdiag_helper_trailingslashit(WP_CONTENT_DIR) . 'nitropack', |
| 155 | 'Nitro_siteID_Dir_Writable' => defined('NITROPACK_DATA_DIR') ? NITROPACK_DATA_DIR . "/$siteID" : npdiag_helper_trailingslashit(WP_CONTENT_DIR) . "nitropack/$siteID", |
| 156 | 'Nitro_Plugin_Dir_Writable' => defined('NITROPACK_PLUGIN_DIR') ? NITROPACK_PLUGIN_DIR : dirname(__FILE__) |
| 157 | ); |
| 158 | |
| 159 | $info = array(); |
| 160 | foreach ($DoI as $doi_dir => $dpath) { |
| 161 | if (is_dir($dpath)) { |
| 162 | $info[$doi_dir] = is_writeable($dpath) ? true : false; |
| 163 | } else if (is_file($dpath)) { |
| 164 | $info[$doi_dir] = "$dpath is a file not a directory"; |
| 165 | } else { |
| 166 | $info[$doi_dir] = 'Directory not found'; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return $info; |
| 171 | } |
| 172 | |
| 173 | function npdiag_get_conflicting_plugins() { |
| 174 | $info = nitropack_get_conflicting_plugins(); |
| 175 | if ( !empty($info) ) { |
| 176 | return $info; |
| 177 | } else { |
| 178 | return $info = 'None detected'; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | function npdiag_detect_third_party_cache() { |
| 183 | $info = \NitroPack\Integration\Plugin\RC::detectThirdPartyCaches(); |
| 184 | if ( !empty($info) ) { |
| 185 | return $info; |
| 186 | } else { |
| 187 | return $info = 'Not found'; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | function nitropack_generate_report() { |
| 192 | global $np_diag_functions; |
| 193 | try { |
| 194 | $ar = !empty($_POST["toggled"]) ? $_POST["toggled"] : NULL; |
| 195 | if ($ar !== NULL) { |
| 196 | $diag_data = array('report-time-stamp' => date("Y-m-d H:i:s")); |
| 197 | foreach ($ar as $func_name => $func_allowed) { |
| 198 | if ((boolean)$func_allowed) { |
| 199 | $diag_data[$func_name] = call_user_func($np_diag_functions[$func_name]); |
| 200 | } |
| 201 | } |
| 202 | $str = json_encode($diag_data, JSON_PRETTY_PRINT); |
| 203 | $filename = 'nitropack_diag_file.txt'; |
| 204 | nitropack_header('Content-Disposition: attachment; filename="'.$filename.'"'); |
| 205 | nitropack_header("Content-Type: text/plain"); |
| 206 | nitropack_header("Content-Length: " . strlen($str)); |
| 207 | echo $str; |
| 208 | exit; |
| 209 | } |
| 210 | } catch (\Exception $e) { |
| 211 | //exception handling here |
| 212 | } |
| 213 | |
| 214 | } |
| 215 |