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