-
Notifications
You must be signed in to change notification settings - Fork 11
/
ks_server_checker.php
204 lines (186 loc) · 8.66 KB
/
ks_server_checker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
** 安裝方式: wget https://raw.githubusercontent.com/nczz/work_with_wordpress/master/ks_server_checker.php && echo -e "include dirname(__FILE__) . '/ks_server_checker.php';" >> functions.php && chown www:www * -R
** 更新方式: curl https://raw.githubusercontent.com/nczz/work_with_wordpress/master/ks_server_checker.php > ks_server_checker.php
**/
function mxp_wp_diagnostic_info() {
global $table_prefix;
global $wpdb;
$diagnostic_info = array();
/*
* WordPress & Server Environment
*/
$diagnostic_info['site_url'] = site_url();
$diagnostic_info['home_url'] = home_url();
$diagnostic_info['WordPress'] = get_bloginfo('version', 'display');
$diagnostic_info['Web_Server'] = !empty($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : '';
$diagnostic_info['PHP'] = "";
if (function_exists('phpversion')) {
$diagnostic_info['PHP'] = phpversion();
}
$diagnostic_info['MySQL'] = $wpdb->db_version();
$diagnostic_info['ext_mysqli'] = empty($wpdb->use_mysqli) ? 'no' : 'yes';
$diagnostic_info['PHP_Memory_Limit'] = "";
if (function_exists('ini_get')) {
$diagnostic_info['PHP_Memory_Limit'] = ini_get('memory_limit');
}
$diagnostic_info['WP_MEMORY_LIMIT'] = WP_MEMORY_LIMIT;
$diagnostic_info['Memory_Usage'] = size_format(memory_get_usage(true));
$diagnostic_info['WP_HTTP_BLOCK_EXTERNAL'] = "";
if (!defined('WP_HTTP_BLOCK_EXTERNAL') || !WP_HTTP_BLOCK_EXTERNAL) {
$diagnostic_info['WP_MEMORY_LIMIT'] = "none";
} else {
$accessible_hosts = (defined('WP_ACCESSIBLE_HOSTS')) ? WP_ACCESSIBLE_HOSTS : '';
if (empty($accessible_hosts)) {
$diagnostic_info['WP_ACCESSIBLE_HOSTS'] = "all";
} else {
$diagnostic_info['WP_ACCESSIBLE_HOSTS'] = $accessible_hosts;
}
}
$diagnostic_info['WP_Locale'] = get_locale();
$diagnostic_info['WP_UPLOADS_BY_MY'] = get_option('uploads_use_yearmonth_folders') ? 'Enabled' : 'Disabled';
$diagnostic_info['WP_DEBUG'] = (defined('WP_DEBUG') && WP_DEBUG) ? 'Yes' : 'No';
$diagnostic_info['WP_DEBUG_LOG'] = (defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) ? 'Yes' : 'No';
$diagnostic_info['WP_DEBUG_DISPLAY'] = (defined('WP_DEBUG_DISPLAY') && WP_DEBUG_DISPLAY) ? 'Yes' : 'No';
$diagnostic_info['SCRIPT_DEBUG'] = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? 'Yes' : 'No';
$diagnostic_info['WP_MAX_UPLOAD_SIZE'] = size_format(wp_max_upload_size());
$diagnostic_info['PHP_max_execution_time'] = "";
if (function_exists('ini_get')) {
$diagnostic_info['PHP_max_execution_time'] = ini_get('max_execution_time');
}
$diagnostic_info['WP_CRON'] = (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON) ? 'Disabled' : 'Enabled';
$diagnostic_info['allow_url_fopen'] = "";
$allow_url_fopen = ini_get('allow_url_fopen');
if (empty($allow_url_fopen)) {
$diagnostic_info['allow_url_fopen'] = "Disabled";
} else {
$diagnostic_info['allow_url_fopen'] = "Enabled";
}
$diagnostic_info['OpenSSL'] = "";
if (defined('OPENSSL_VERSION_TEXT')) {
$diagnostic_info['OpenSSL'] = OPENSSL_VERSION_TEXT;
} else {
$diagnostic_info['OpenSSL'] = "Disabled";
}
$diagnostic_info['PHP_GD'] = "";
if (extension_loaded('gd') && function_exists('gd_info')) {
$gd_info = gd_info();
$diagnostic_info['PHP_GD'] = isset($gd_info['GD Version']) ? $gd_info['GD Version'] : 'Enabled';
} else {
$diagnostic_info['PHP_GD'] = 'Disabled';
}
$diagnostic_info['Imagick'] = "";
if (extension_loaded('imagick') && class_exists('Imagick') && class_exists('ImagickPixel')) {
$diagnostic_info['Imagick'] = 'Enabled';
} else {
$diagnostic_info['Imagick'] = 'Disabled';
}
/*
* Settings
*/
$theme_info = wp_get_theme();
$diagnostic_info['Active_Theme'] = array();
$diagnostic_info['Parent_Theme'] = array();
if (!empty($theme_info) && is_a($theme_info, 'WP_Theme')) {
if (file_exists($theme_info->get_stylesheet_directory())) {
$diagnostic_info['Active_Theme']['Name'] = $theme_info->get('Name');
$diagnostic_info['Active_Theme']['Version'] = $theme_info->get('Version');
$diagnostic_info['Active_Theme']['Folder'] = $theme_info->get_stylesheet();
}
if (is_child_theme()) {
$parent_info = $theme_info->parent();
if (!empty($parent_info) && is_a($parent_info, 'WP_Theme')) {
$diagnostic_info['Parent_Theme']['Name'] = $parent_info->get('Name');
$diagnostic_info['Parent_Theme']['Version'] = $parent_info->get('Version');
$diagnostic_info['Parent_Theme']['Folder'] = $parent_info->get_stylesheet();
}
} else {
$diagnostic_info['Parent_Theme']['Name'] = "";
$diagnostic_info['Parent_Theme']['Version'] = "";
$diagnostic_info['Parent_Theme']['Folder'] = "";
}
}
$diagnostic_info['Active_Plugins'] = array();
$diagnostic_info['MU_Plugins'] = array();
$active_plugins = (array) get_option('active_plugins', array());
if (is_multisite()) {
$network_active_plugins = wp_get_active_network_plugins();
$active_plugins = array_map(function ($path) {
$plugin_dir = trailingslashit(WP_PLUGIN_DIR);
$plugin = str_replace($plugin_dir, '', $path);
return $plugin;
}, $network_active_plugins);
}
foreach ($active_plugins as $plugin) {
$diagnostic_info['Active_Plugins'][] = mxp_get_plugin_details(WP_PLUGIN_DIR . '/' . $plugin);
}
$mu_plugins = wp_get_mu_plugins();
if ($mu_plugins) {
foreach ($mu_plugins as $mu_plugin) {
$diagnostic_info['MU_Plugins'][] = mxp_get_plugin_details($mu_plugin);
}
}
return $diagnostic_info;
}
function mxp_get_plugin_details($plugin_path, $suffix = '') {
if (!function_exists('get_plugin_data')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugin_data = get_plugin_data($plugin_path);
if (empty($plugin_data['Name'])) {
return basename($plugin_path);
}
return array("Name" => $plugin_data['Name'], "Version" => $plugin_data['Version'], "Author" => strip_tags($plugin_data['AuthorName']));
}
function mxp_add_cron_schedules($schedules) {
$schedules['ks_custom_2h'] = array(
'interval' => 7200, // 兩小時檢查一次變化
'display' => "兩小時一次",
);
return $schedules;
}
add_filter('cron_schedules', 'mxp_add_cron_schedules');
function mxp_cronjob_register() {
if (!wp_next_scheduled('mxp_plugin_update_cron')) {
wp_schedule_event(time(), 'ks_custom_2h', 'mxp_plugin_update_cron');
}
}
add_action('wp', 'mxp_cronjob_register');
function mxp_cronjob_do_action() {
$diagnostic_info = mxp_wp_diagnostic_info();
$req = array(
'domain' => parse_url($diagnostic_info['site_url'], PHP_URL_HOST),
'php' => $diagnostic_info['PHP'],
'mysql' => $diagnostic_info['MySQL'],
'wp' => $diagnostic_info['WordPress'],
'theme' => $diagnostic_info['Active_Theme']['Name'] . "_" . $diagnostic_info['Active_Theme']['Version'],
'parent_theme' => $diagnostic_info['Parent_Theme']['Name'] . "_" . $diagnostic_info['Parent_Theme']['Version'],
'json' => json_encode($diagnostic_info),
'version' => '1.2',
'knockers' => 0, //站點分類
'email' => '[email protected]', //比對異常時的通知人,可改其他通知人。「,」分隔多重聯絡人,總長度不得超過 100 字元
);
$response = wp_remote_post('https://api.undo.im/wp-json/mxp_knockers/v1/app/register', array(
'method' => 'POST',
'timeout' => 10,
'redirection' => 5,
'httpversion' => '1.1',
'blocking' => false,
'headers' => array('Content-Type' => 'application/json'),
'body' => wp_json_encode($req),
'cookies' => array(),
'sslverify' => false,
'data_format' => 'body',
)
);
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
if (defined('WP_DEBUG') && WP_DEBUG == true) {
error_log($req['domain'] . "_CRONJOB_ERROR: $error_message");
}
}
if (defined('WP_DEBUG') && WP_DEBUG == true) {
error_log($req['domain'] . "_CRONJOB_DONE");
}
}
add_action('mxp_plugin_update_cron', 'mxp_cronjob_do_action');