| 1 |
<?php |
| 2 |
|
| 3 |
namespace WeDevs\ERP\Framework; |
| 4 |
|
| 5 |
// @since 1.3.4 |
| 6 |
class System_Status { |
| 7 |
|
| 8 |
/** |
| 9 |
* Get array of environment information. Includes thing like software |
| 10 |
* versions, and various server settings. |
| 11 |
* |
| 12 |
* @return array |
| 13 |
*/ |
| 14 |
public function get_environment_info() { |
| 15 |
global $wpdb; |
| 16 |
|
| 17 |
// Figure out cURL version, if installed. |
| 18 |
$curl_version = ''; |
| 19 |
if ( function_exists( 'curl_version' ) ) { |
| 20 |
$curl_version = curl_version(); |
| 21 |
$curl_version = $curl_version['version'] . ', ' . $curl_version['ssl_version']; |
| 22 |
} |
| 23 |
|
| 24 |
// WP memory limit |
| 25 |
$wp_memory_limit = erp_let_to_num( WP_MEMORY_LIMIT ); |
| 26 |
if ( function_exists( 'memory_get_usage' ) ) { |
| 27 |
$wp_memory_limit = max( $wp_memory_limit, erp_let_to_num( @ini_get( 'memory_limit' ) ) ); |
| 28 |
} |
| 29 |
|
| 30 |
// Test POST requests |
| 31 |
$post_response = wp_safe_remote_post( 'https://www.paypal.com/cgi-bin/webscr', array( |
| 32 |
'timeout' => 10, |
| 33 |
'user-agent' => 'ERP/' . wperp()->version, |
| 34 |
'httpversion' => '1.1', |
| 35 |
'body' => array( |
| 36 |
'cmd' => '_notify-validate', |
| 37 |
), |
| 38 |
) ); |
| 39 |
$post_response_successful = false; |
| 40 |
if ( ! is_wp_error( $post_response ) && $post_response['response']['code'] >= 200 && $post_response['response']['code'] < 300 ) { |
| 41 |
$post_response_successful = true; |
| 42 |
} |
| 43 |
|
| 44 |
// Test GET requests |
| 45 |
$get_response = wp_safe_remote_get( 'https://woocommerce.com/wc-api/product-key-api?request=ping&network=' . ( is_multisite() ? '1' : '0' ) ); |
| 46 |
$get_response_successful = false; |
| 47 |
if ( ! is_wp_error( $post_response ) && $post_response['response']['code'] >= 200 && $post_response['response']['code'] < 300 ) { |
| 48 |
$get_response_successful = true; |
| 49 |
} |
| 50 |
|
| 51 |
$upload_dir = wp_upload_dir(); |
| 52 |
$log_directory = $upload_dir['basedir'] . '/erp-logs/'; |
| 53 |
|
| 54 |
// Return all environment info. Described by JSON Schema. |
| 55 |
return array( |
| 56 |
'home_url' => get_option( 'home' ), |
| 57 |
'site_url' => get_option( 'siteurl' ), |
| 58 |
'version' => wperp()->version, |
| 59 |
'log_directory' => $log_directory, |
| 60 |
'log_directory_writable' => ( @fopen( $log_directory . 'test-log.log', 'a' ) ? true : false ), |
| 61 |
'wp_version' => get_bloginfo( 'version' ), |
| 62 |
'wp_multisite' => is_multisite(), |
| 63 |
'wp_memory_limit' => $wp_memory_limit, |
| 64 |
'wp_debug_mode' => ( defined( 'WP_DEBUG' ) && WP_DEBUG ), |
| 65 |
'wp_cron' => ! ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ), |
| 66 |
'language' => get_locale(), |
| 67 |
'server_info' => $_SERVER['SERVER_SOFTWARE'], |
| 68 |
'php_version' => phpversion(), |
| 69 |
'php_post_max_size' => erp_let_to_num( ini_get( 'post_max_size' ) ), |
| 70 |
'php_max_execution_time' => ini_get( 'max_execution_time' ), |
| 71 |
'php_max_input_vars' => ini_get( 'max_input_vars' ), |
| 72 |
'curl_version' => $curl_version, |
| 73 |
'suhosin_installed' => extension_loaded( 'suhosin' ), |
| 74 |
'max_upload_size' => wp_max_upload_size(), |
| 75 |
'mysql_version' => ( ! empty( $wpdb->is_mysql ) ? $wpdb->db_version() : '' ), |
| 76 |
'default_timezone' => date_default_timezone_get(), |
| 77 |
'fsockopen_or_curl_enabled' => ( function_exists( 'fsockopen' ) || function_exists( 'curl_init' ) ), |
| 78 |
'soapclient_enabled' => class_exists( 'SoapClient' ), |
| 79 |
'domdocument_enabled' => class_exists( 'DOMDocument' ), |
| 80 |
'gzip_enabled' => is_callable( 'gzopen' ), |
| 81 |
'mbstring_enabled' => extension_loaded( 'mbstring' ), |
| 82 |
'remote_post_successful' => $post_response_successful, |
| 83 |
'remote_post_response' => ( is_wp_error( $post_response ) ? $post_response->get_error_message() : $post_response['response']['code'] ), |
| 84 |
'remote_get_successful' => $get_response_successful, |
| 85 |
'remote_get_response' => ( is_wp_error( $get_response ) ? $get_response->get_error_message() : $get_response['response']['code'] ), |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Add prefix to table. |
| 91 |
* |
| 92 |
* @param string $table table name |
| 93 |
* @return stromg |
| 94 |
*/ |
| 95 |
protected function add_db_table_prefix( $table ) { |
| 96 |
global $wpdb; |
| 97 |
return $wpdb->prefix . $table; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get array of database information. Version, prefix, and table existence. |
| 102 |
* |
| 103 |
* @return array |
| 104 |
*/ |
| 105 |
public function get_database_info() { |
| 106 |
global $wpdb; |
| 107 |
|
| 108 |
$database_table_sizes = $wpdb->get_results( $wpdb->prepare( " |
| 109 |
SELECT |
| 110 |
table_name AS 'name', |
| 111 |
round( ( data_length / 1024 / 1024 ), 2 ) 'data', |
| 112 |
round( ( index_length / 1024 / 1024 ), 2 ) 'index' |
| 113 |
FROM information_schema.TABLES |
| 114 |
WHERE table_schema = %s |
| 115 |
ORDER BY name ASC; |
| 116 |
", DB_NAME ) ); |
| 117 |
|
| 118 |
// ERP Core tables to check existence of |
| 119 |
$core_tables = apply_filters( 'erp_database_tables', array( |
| 120 |
'erp_hr_depts', |
| 121 |
'erp_hr_dependents' |
| 122 |
) ); |
| 123 |
|
| 124 |
/** |
| 125 |
* Adding the prefix to the tables array, for backwards compatibility. |
| 126 |
* |
| 127 |
* If we changed the tables above to include the prefix, then any filters against that table could break. |
| 128 |
*/ |
| 129 |
$core_tables = array_map( array( $this, 'add_db_table_prefix' ), $core_tables ); |
| 130 |
|
| 131 |
/** |
| 132 |
* Organize WooCommerce and non-WooCommerce tables separately for display purposes later. |
| 133 |
* |
| 134 |
* To ensure we include all ERP tables, even if they do not exist, pre-populate the ERP array with all the tables. |
| 135 |
*/ |
| 136 |
$tables = array( |
| 137 |
'erp' => array_fill_keys( $core_tables, false ), |
| 138 |
'other' => array() |
| 139 |
); |
| 140 |
|
| 141 |
$database_size = array( |
| 142 |
'data' => 0, |
| 143 |
'index' => 0 |
| 144 |
); |
| 145 |
|
| 146 |
foreach ( $database_table_sizes as $table ) { |
| 147 |
$table_type = in_array( $table->name, $core_tables ) ? 'erp' : 'other'; |
| 148 |
|
| 149 |
$tables[ $table_type ][ $table->name ] = array( |
| 150 |
'data' => $table->data, |
| 151 |
'index' => $table->index |
| 152 |
); |
| 153 |
|
| 154 |
$database_size[ 'data' ] += $table->data; |
| 155 |
$database_size[ 'index' ] += $table->index; |
| 156 |
} |
| 157 |
|
| 158 |
// Return all database info. Described by JSON Schema. |
| 159 |
return array( |
| 160 |
'wp_erp_db_version' => get_option( 'wp_erp_db_version' ), |
| 161 |
'database_prefix' => $wpdb->prefix, |
| 162 |
'database_tables' => $tables, |
| 163 |
'database_size' => $database_size, |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Get array of counts of objects. Orders, products, etc. |
| 169 |
* |
| 170 |
* @return array |
| 171 |
*/ |
| 172 |
public function get_post_type_counts() { |
| 173 |
global $wpdb; |
| 174 |
|
| 175 |
$post_type_counts = $wpdb->get_results( "SELECT post_type AS 'type', count(1) AS 'count' FROM {$wpdb->posts} GROUP BY post_type;" ); |
| 176 |
|
| 177 |
return is_array( $post_type_counts ) ? $post_type_counts : array(); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Get a list of plugins active on the site. |
| 182 |
* |
| 183 |
* @return array |
| 184 |
*/ |
| 185 |
public function get_active_plugins() { |
| 186 |
require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 187 |
require_once( ABSPATH . 'wp-admin/includes/update.php' ); |
| 188 |
|
| 189 |
if ( ! function_exists( 'get_plugin_updates' ) ) { |
| 190 |
return array(); |
| 191 |
} |
| 192 |
|
| 193 |
// Get both site plugins and network plugins |
| 194 |
$active_plugins = (array) get_option( 'active_plugins', array() ); |
| 195 |
if ( is_multisite() ) { |
| 196 |
$network_activated_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) ); |
| 197 |
$active_plugins = array_merge( $active_plugins, $network_activated_plugins ); |
| 198 |
} |
| 199 |
|
| 200 |
$active_plugins_data = array(); |
| 201 |
$available_updates = get_plugin_updates(); |
| 202 |
|
| 203 |
foreach ( $active_plugins as $plugin ) { |
| 204 |
$data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); |
| 205 |
$dirname = dirname( $plugin ); |
| 206 |
$version_latest = ''; |
| 207 |
$slug = explode( '/', $plugin ); |
| 208 |
$slug = explode( '.', end( $slug ) ); |
| 209 |
$slug = $slug[0]; |
| 210 |
|
| 211 |
if ( isset( $available_updates[ $plugin ]->update->new_version ) ) { |
| 212 |
$version_latest = $available_updates[ $plugin ]->update->new_version; |
| 213 |
} |
| 214 |
|
| 215 |
// convert plugin data to json response format. |
| 216 |
$active_plugins_data[] = array( |
| 217 |
'plugin' => $plugin, |
| 218 |
'name' => $data['Name'], |
| 219 |
'version' => $data['Version'], |
| 220 |
'version_latest' => $version_latest, |
| 221 |
'url' => $data['PluginURI'], |
| 222 |
'author_name' => $data['AuthorName'], |
| 223 |
'author_url' => esc_url_raw( $data['AuthorURI'] ), |
| 224 |
'network_activated' => $data['Network'], |
| 225 |
); |
| 226 |
} |
| 227 |
|
| 228 |
return $active_plugins_data; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Get info on the current active theme, info on parent theme (if presnet) |
| 233 |
* and a list of template overrides. |
| 234 |
* |
| 235 |
* @return array |
| 236 |
*/ |
| 237 |
public function get_theme_info() { |
| 238 |
$active_theme = wp_get_theme(); |
| 239 |
|
| 240 |
// Get parent theme info if this theme is a child theme, otherwise |
| 241 |
// pass empty info in the response. |
| 242 |
if ( is_child_theme() ) { |
| 243 |
$parent_theme = wp_get_theme( $active_theme->Template ); |
| 244 |
$parent_theme_info = array( |
| 245 |
'parent_name' => $parent_theme->Name, |
| 246 |
'parent_version' => $parent_theme->Version, |
| 247 |
'parent_version_latest' => \WeDevs\ERP\Status::get_latest_theme_version( $parent_theme ), |
| 248 |
'parent_author_url' => $parent_theme->{'Author URI'}, |
| 249 |
); |
| 250 |
} else { |
| 251 |
$parent_theme_info = array( 'parent_name' => '', 'parent_version' => '', 'parent_version_latest' => '', 'parent_author_url' => '' ); |
| 252 |
} |
| 253 |
|
| 254 |
$active_theme_info = array( |
| 255 |
'name' => $active_theme->Name, |
| 256 |
'version' => $active_theme->Version, |
| 257 |
'version_latest' => \WeDevs\ERP\Status::get_latest_theme_version( $active_theme ), |
| 258 |
'author_url' => esc_url_raw( $active_theme->{'Author URI'} ), |
| 259 |
'is_child_theme' => is_child_theme() |
| 260 |
); |
| 261 |
|
| 262 |
return array_merge( $active_theme_info, $parent_theme_info ); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Returns security tips. |
| 267 |
* |
| 268 |
* @return array |
| 269 |
*/ |
| 270 |
public function get_security_info() { |
| 271 |
return array( |
| 272 |
'secure_connection' => 'https' === substr( get_home_url(), 0, 5 ), |
| 273 |
'hide_errors' => ! ( defined( 'WP_DEBUG' ) && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG && WP_DEBUG_DISPLAY ) || 0 === intval( ini_get( 'display_errors' ) ), |
| 274 |
); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Get any query params needed. |
| 279 |
* |
| 280 |
* @return array |
| 281 |
*/ |
| 282 |
public function get_collection_params() { |
| 283 |
return array( |
| 284 |
'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
| 285 |
); |
| 286 |
} |
| 287 |
|
| 288 |
} |
| 289 |
|