| 1 |
<?php |
| 2 |
|
| 3 |
/************************************************************* |
| 4 |
* stats.class.php |
| 5 |
* Get Site Stats |
| 6 |
* Copyright (c) 2011 Prelovac Media |
| 7 |
* www.prelovac.com |
| 8 |
**************************************************************/ |
| 9 |
class MMB_Stats extends MMB_Core |
| 10 |
{ |
| 11 |
public function get_site_statistics() |
| 12 |
{ |
| 13 |
/** @var wpdb $wpdb */ |
| 14 |
global $wpdb; |
| 15 |
$siteStatistics = array(); |
| 16 |
$prefix = $wpdb->prefix; |
| 17 |
$basePrefix = $wpdb->base_prefix; |
| 18 |
|
| 19 |
if (!$this->mmb_multisite) { |
| 20 |
$siteStatistics['users'] = (int)$wpdb->get_var("SELECT COUNT(*) FROM {$basePrefix}users"); |
| 21 |
} else { |
| 22 |
$siteStatistics['users'] = count(get_users( |
| 23 |
array( |
| 24 |
'blog_id' => $wpdb->blogid, |
| 25 |
) |
| 26 |
)); |
| 27 |
} |
| 28 |
|
| 29 |
$siteStatistics['approvedComments'] = (int)$wpdb->get_var("SELECT COUNT(*) FROM {$prefix}comments c INNER JOIN {$prefix}posts p ON c.comment_post_ID = p.ID WHERE comment_approved = '1' AND p.post_status = 'publish'"); |
| 30 |
$siteStatistics['activePlugins'] = count((array)get_option('active_plugins', array())); |
| 31 |
$siteStatistics['publishedPosts'] = (int)$wpdb->get_var("SELECT COUNT(*) FROM {$prefix}posts WHERE post_type='post' AND post_status='publish'"); |
| 32 |
$siteStatistics['draftPosts'] = (int)$wpdb->get_var("SELECT COUNT(*) FROM {$prefix}posts WHERE post_type='post' AND post_status='draft'"); |
| 33 |
$siteStatistics['publishedPages'] = (int)$wpdb->get_var("SELECT COUNT(*) FROM {$prefix}posts WHERE post_type='page' AND post_status='publish'"); |
| 34 |
$siteStatistics['draftPages'] = (int)$wpdb->get_var("SELECT COUNT(*) FROM {$prefix}posts WHERE post_type='page' AND post_status='draft'"); |
| 35 |
|
| 36 |
return $siteStatistics; |
| 37 |
} |
| 38 |
|
| 39 |
public function get_core_update() |
| 40 |
{ |
| 41 |
global $wp_version; |
| 42 |
$update = null; |
| 43 |
$locale = get_locale(); |
| 44 |
$core = $this->mmb_get_transient('update_core'); |
| 45 |
|
| 46 |
if (empty($core->updates)) { |
| 47 |
return null; |
| 48 |
} |
| 49 |
|
| 50 |
foreach ($core->updates as $availableUpdate) { |
| 51 |
if ($availableUpdate->locale == $locale && strtolower($availableUpdate->response) === "upgrade") { |
| 52 |
$update = $availableUpdate; |
| 53 |
break; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
//fallback to first |
| 58 |
if (!$update) { |
| 59 |
$update = $core->updates[0]; |
| 60 |
} |
| 61 |
// WordPress can actually have an update to the same version and locale if locale has not been updated |
| 62 |
if ($update->response === 'development' || $update->response === 'upgrade' || version_compare($wp_version, $update->current, '<') || $locale !== $update->locale) { |
| 63 |
$update->current_version = $wp_version; |
| 64 |
return $update; |
| 65 |
} else { |
| 66 |
return null; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public function get_comments($type, $limit, $trimlen = 200) |
| 71 |
{ |
| 72 |
$comments = (array)get_comments('status='.$type.'&number='.$limit); |
| 73 |
foreach ($comments as &$comment) { |
| 74 |
$commented_post = get_post($comment->comment_post_ID); |
| 75 |
$comment->post_title = $commented_post->post_title; |
| 76 |
$comment->comment_content = $this->trim_content($comment->comment_content, $trimlen); |
| 77 |
$comment->comment_content = seems_utf8($comment->comment_content) ? $comment->comment_content : utf8_encode($comment->comment_content); |
| 78 |
unset($comment->comment_author_IP); |
| 79 |
unset($comment->comment_karma); |
| 80 |
unset($comment->comment_agent); |
| 81 |
unset($comment->comment_type); |
| 82 |
unset($comment->comment_parent); |
| 83 |
} |
| 84 |
return $comments; |
| 85 |
} |
| 86 |
|
| 87 |
public function get_posts($limit) |
| 88 |
{ |
| 89 |
$userIdMap = $this->getUsersIDs(); |
| 90 |
$list = array(); |
| 91 |
|
| 92 |
|
| 93 |
$posts = (array)get_posts('post_status=publish&numberposts='.$limit.'&orderby=post_date&order=desc'); |
| 94 |
foreach ($posts as $id => $post) { |
| 95 |
$add = new stdClass(); |
| 96 |
$add->post_permalink = get_permalink($post->ID); |
| 97 |
$add->ID = $post->ID; |
| 98 |
$add->post_date = $post->post_date; |
| 99 |
$add->post_title = $post->post_title; |
| 100 |
$add->post_type = $post->post_type; |
| 101 |
$add->comment_count = (int)$post->comment_count; |
| 102 |
|
| 103 |
$author_name = isset($userIdMap[$post->post_author]) ? $userIdMap[$post->post_author] : ''; |
| 104 |
$add->post_author_name = array('author_id' => $post->post_author, 'author_name' => $author_name); |
| 105 |
|
| 106 |
$list[] = $add; |
| 107 |
} |
| 108 |
|
| 109 |
$posts = (array)get_pages('post_status=publish&numberposts='.$limit.'&orderby=post_date&order=desc'); |
| 110 |
foreach ($posts as $id => $post) { |
| 111 |
$add = new stdClass(); |
| 112 |
$add->post_permalink = get_permalink($post->ID); |
| 113 |
$add->post_type = $post->post_type; |
| 114 |
$add->ID = $post->ID; |
| 115 |
$add->post_date = $post->post_date; |
| 116 |
$add->post_title = $post->post_title; |
| 117 |
|
| 118 |
$author_name = isset($userIdMap[$post->post_author]) ? $userIdMap[$post->post_author] : ''; |
| 119 |
$add->post_author = array('author_id' => $post->post_author, 'author_name' => $author_name); |
| 120 |
|
| 121 |
$list[] = $add; |
| 122 |
} |
| 123 |
usort($list, array($this, 'cmp_posts_worker')); |
| 124 |
return array_slice($list, 0, $limit); |
| 125 |
} |
| 126 |
|
| 127 |
public function get_drafts($limit) |
| 128 |
{ |
| 129 |
$list = array(); |
| 130 |
|
| 131 |
$posts = (array)get_posts('post_status=draft&numberposts='.$limit.'&orderby=post_date&order=desc'); |
| 132 |
foreach ($posts as $id => $post) { |
| 133 |
$add = new stdClass(); |
| 134 |
$add->post_permalink = get_permalink($post->ID); |
| 135 |
$add->post_type = $post->post_type; |
| 136 |
$add->ID = $post->ID; |
| 137 |
$add->post_date = $post->post_date; |
| 138 |
$add->post_title = $post->post_title; |
| 139 |
|
| 140 |
$list[] = $add; |
| 141 |
} |
| 142 |
$posts = (array)get_pages('post_status=draft&numberposts='.$limit.'&orderby=post_date&order=desc'); |
| 143 |
foreach ($posts as $id => $post) { |
| 144 |
$add = new stdClass(); |
| 145 |
$add->post_permalink = get_permalink($post->ID); |
| 146 |
$add->ID = $post->ID; |
| 147 |
$add->post_type = $post->post_type; |
| 148 |
$add->post_date = $post->post_date; |
| 149 |
$add->post_title = $post->post_title; |
| 150 |
|
| 151 |
$list[] = $add; |
| 152 |
} |
| 153 |
usort($list, array($this, 'cmp_posts_worker')); |
| 154 |
return array_slice($list, 0, $limit); |
| 155 |
} |
| 156 |
|
| 157 |
public function get_scheduled($numberOfItems) |
| 158 |
{ |
| 159 |
$list = array(); |
| 160 |
|
| 161 |
$posts = (array)get_posts('post_status=future&numberposts='.$numberOfItems.'&orderby=post_date&order=desc'); |
| 162 |
foreach ($posts as $id => $post) { |
| 163 |
$add = new stdClass(); |
| 164 |
$add->post_permalink = get_permalink($post->ID); |
| 165 |
$add->ID = $post->ID; |
| 166 |
$add->post_date = $post->post_date; |
| 167 |
$add->post_type = $post->post_type; |
| 168 |
$add->post_title = $post->post_title; |
| 169 |
|
| 170 |
$list[] = $add; |
| 171 |
} |
| 172 |
$posts = (array)get_pages('post_status=future&numberposts='.$numberOfItems.'&orderby=post_date&order=desc'); |
| 173 |
foreach ((array)$posts as $id => $post) { |
| 174 |
$add = new stdClass(); |
| 175 |
$add->post_permalink = get_permalink($post->ID); |
| 176 |
$add->ID = $post->ID; |
| 177 |
$add->post_type = $post->post_type; |
| 178 |
$add->post_date = $post->post_date; |
| 179 |
$add->post_title = $post->post_title; |
| 180 |
|
| 181 |
$list[] = $add; |
| 182 |
} |
| 183 |
usort($list, array($this, 'cmp_posts_worker')); |
| 184 |
return array_slice($list, 0, $numberOfItems); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* @return array Map of wp_users.ID (string) => wp_users.display_name (string) where wp_users.user_status == 0. |
| 189 |
*/ |
| 190 |
private function getUsersIDs() |
| 191 |
{ |
| 192 |
global $wpdb; |
| 193 |
$users_authors = array(); |
| 194 |
$users = $wpdb->get_results("SELECT ID as user_id, display_name FROM $wpdb->users WHERE user_status=0"); |
| 195 |
|
| 196 |
foreach ($users as $user_key => $user_val) { |
| 197 |
$users_authors[$user_val->user_id] = $user_val->display_name; |
| 198 |
} |
| 199 |
|
| 200 |
return $users_authors; |
| 201 |
} |
| 202 |
|
| 203 |
public function get_updates($stats, $options = array()) |
| 204 |
{ |
| 205 |
if (isset($options['themes']) && $options['themes']) { |
| 206 |
$upgrades = $this->get_installer_instance()->get_upgradable_themes(); |
| 207 |
if (!empty($upgrades)) { |
| 208 |
$stats['upgradable_themes'] = $upgrades; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if (isset($options['plugins']) && $options['plugins']) { |
| 213 |
$upgrades = $this->get_installer_instance()->get_upgradable_plugins(); |
| 214 |
if (!empty($upgrades)) { |
| 215 |
$stats['upgradable_plugins'] = $upgrades; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
if (isset($options['translations']) && $options['translations']) { |
| 220 |
$upgrades = $this->get_installer_instance()->get_upgradable_translations(); |
| 221 |
if (!empty($upgrades)) { |
| 222 |
$stats['upgradable_translations'] = $upgrades; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
return $stats; |
| 227 |
} |
| 228 |
|
| 229 |
public function getUserList() |
| 230 |
{ |
| 231 |
$filter = array( |
| 232 |
'user_roles' => array( |
| 233 |
'administrator', |
| 234 |
), |
| 235 |
'username' => '', |
| 236 |
'username_filter' => '', |
| 237 |
); |
| 238 |
$users = $this->get_user_instance()->get_users($filter); |
| 239 |
|
| 240 |
if (empty($users['users']) || !is_array($users['users'])) { |
| 241 |
return array(); |
| 242 |
} |
| 243 |
|
| 244 |
$userList = array(); |
| 245 |
foreach ($users['users'] as $user) { |
| 246 |
$userList[] = $user['user_login']; |
| 247 |
} |
| 248 |
|
| 249 |
return $userList; |
| 250 |
} |
| 251 |
|
| 252 |
private function stats_arg(array $params, $widget, $arg) |
| 253 |
{ |
| 254 |
// Cleanup plugin (the only one) has the following keys that hold options: overhead, revisions, spam. |
| 255 |
if (isset($params['item_filter']['get_stats']['plugins']['cleanup'][$widget][$arg])) { |
| 256 |
return $params['item_filter']['get_stats']['plugins']['cleanup'][$widget][$arg]; |
| 257 |
} |
| 258 |
|
| 259 |
// Remaining "widgets" are number-indexed. |
| 260 |
foreach ((array)@$params['item_filter']['get_stats'] as $key => $data) { |
| 261 |
if ($key !== $widget) { |
| 262 |
continue; |
| 263 |
} |
| 264 |
if (isset($data[1][$arg])) { |
| 265 |
return $data[1][$arg]; |
| 266 |
} |
| 267 |
break; |
| 268 |
} |
| 269 |
return null; |
| 270 |
} |
| 271 |
|
| 272 |
private function get_expired_transients(array $transientsData) |
| 273 |
{ |
| 274 |
$expiredTransients = 0; |
| 275 |
foreach ($transientsData as $transient) { |
| 276 |
$expiredTransients += $this->get_expired_transients_size($transient['name'], $transient['suffix'], $transient['timeout'], $transient['mask'], $transient['limit']); |
| 277 |
} |
| 278 |
return $expiredTransients; |
| 279 |
} |
| 280 |
|
| 281 |
private function get_expired_transients_size($name, $suffix, $timeout, $mask, $limit) |
| 282 |
{ |
| 283 |
/** @var wpdb $wpdb */ |
| 284 |
global $wpdb; |
| 285 |
|
| 286 |
$prefix = $wpdb->prefix; |
| 287 |
|
| 288 |
$timeoutName = $name.$suffix; |
| 289 |
$subStrLength = strlen($timeoutName) + 1; |
| 290 |
|
| 291 |
$escapedTimeoutName = str_replace('_', '\_', $timeoutName); |
| 292 |
|
| 293 |
$selectTimeOutedTransients = <<<SQL |
| 294 |
SELECT SUBSTR(option_name, {$subStrLength}) AS transient_name FROM {$prefix}options WHERE option_name LIKE '{$escapedTimeoutName}{$mask}' AND option_value < {$timeout} LIMIT {$limit} |
| 295 |
SQL; |
| 296 |
|
| 297 |
$transientsToDelete = $wpdb->get_col($selectTimeOutedTransients); |
| 298 |
$timeoutsToDelete = array(); |
| 299 |
$transientsTotalSize = 0; |
| 300 |
|
| 301 |
if (count($transientsToDelete) === 0) { |
| 302 |
return $transientsTotalSize; |
| 303 |
} |
| 304 |
|
| 305 |
foreach ($transientsToDelete as &$transient) { |
| 306 |
$timeoutsToDelete[] = "'".$timeoutName.$transient."'"; |
| 307 |
$transient = "'".$name.$transient."'"; |
| 308 |
} |
| 309 |
|
| 310 |
$transientSizeClause = implode(',', $transientsToDelete); |
| 311 |
|
| 312 |
$transientsSizeQuery = <<<SQL |
| 313 |
SELECT SUM(LENGTH(option_value)) as valueSize, SUM(LENGTH(option_name)) as nameSize FROM {$prefix}options WHERE option_name IN ({$transientSizeClause}) |
| 314 |
SQL; |
| 315 |
$transientsSize = $wpdb->get_results($transientsSizeQuery); |
| 316 |
$transientsTotalSize += ((int)$transientsSize[0]->nameSize); |
| 317 |
$transientsTotalSize += ((int)$transientsSize[0]->valueSize); |
| 318 |
|
| 319 |
$transientSizeClause = implode(',', $timeoutsToDelete); |
| 320 |
$transientsSizeQuery = <<<SQL |
| 321 |
SELECT SUM(LENGTH(option_value)) as valueSize, SUM(LENGTH(option_name)) as nameSize FROM {$prefix}options WHERE option_name IN ({$transientSizeClause}) |
| 322 |
SQL; |
| 323 |
$timeoutsSize = $wpdb->get_results($transientsSizeQuery); |
| 324 |
$transientsTotalSize += ((int)$timeoutsSize[0]->valueSize); |
| 325 |
$transientsTotalSize += ((int)$timeoutsSize[0]->nameSize); |
| 326 |
|
| 327 |
return $transientsTotalSize; |
| 328 |
} |
| 329 |
|
| 330 |
public function get_stats(array $params) |
| 331 |
{ |
| 332 |
$revisionLimit = (int)str_replace('r_', '', (string)$this->stats_arg($params, 'revisions', 'num_to_keep')); |
| 333 |
if (!$revisionLimit) { |
| 334 |
$revisionLimit = 5; |
| 335 |
} |
| 336 |
$commentLimit = (int)$this->stats_arg($params, 'comments', 'numberposts'); |
| 337 |
if (!$commentLimit) { |
| 338 |
$commentLimit = 5; |
| 339 |
} |
| 340 |
// All post limits are the same on the API side. |
| 341 |
$postLimit = (int)$this->stats_arg($params, 'posts', 'numberposts'); |
| 342 |
if (!$postLimit) { |
| 343 |
$postLimit = 5; |
| 344 |
} |
| 345 |
$transientsParams = (array)$this->stats_arg($params, 'transients', 'expire_data'); |
| 346 |
|
| 347 |
unset($params); |
| 348 |
$save = array('plugins' => array('cleanup' => array('revisions' => array('num_to_keep' => $revisionLimit)))); |
| 349 |
// These options are only used for the revision cleanup action. |
| 350 |
update_option('mmb_stats_filter', $save); |
| 351 |
|
| 352 |
include_once ABSPATH.'wp-includes/update.php'; |
| 353 |
include_once ABSPATH.'wp-admin/includes/update.php'; |
| 354 |
|
| 355 |
mwp_logger()->debug('Started initializing stats'); |
| 356 |
|
| 357 |
$stats = array( |
| 358 |
'upgradable_themes' => $this->get_installer_instance()->get_upgradable_themes(), |
| 359 |
'upgradable_plugins' => $this->get_installer_instance()->get_upgradable_plugins(), |
| 360 |
'upgradable_translations' => $this->get_installer_instance()->get_upgradable_translations(), |
| 361 |
'core_updates' => $this->get_core_update(), |
| 362 |
'posts' => $this->get_posts($postLimit), |
| 363 |
'drafts' => $this->get_drafts($postLimit), |
| 364 |
'scheduled' => $this->get_scheduled($postLimit), |
| 365 |
'hit_counter' => get_option('user_hit_count'), |
| 366 |
'comments' => array( |
| 367 |
'pending' => $this->get_comments('hold', $commentLimit), |
| 368 |
'approved' => $this->get_comments('approve', $commentLimit), |
| 369 |
), |
| 370 |
'site_statistics' => $this->get_site_statistics(), |
| 371 |
'num_revisions' => mmb_num_revisions($revisionLimit), |
| 372 |
'overhead' => mmb_handle_overhead(false), |
| 373 |
'expired_transients' => $this->get_expired_transients($transientsParams), |
| 374 |
'num_spam_comments' => mmb_num_spam_comments(), |
| 375 |
); |
| 376 |
|
| 377 |
mwp_logger()->debug('Finished initializing stats'); |
| 378 |
|
| 379 |
/** @var $wpdb wpdb */ |
| 380 |
global $wpdb, $wp_version, $mmb_plugin_dir; |
| 381 |
|
| 382 |
$stats['worker_version'] = $GLOBALS['MMB_WORKER_VERSION']; |
| 383 |
$stats['worker_revision'] = $GLOBALS['MMB_WORKER_REVISION']; |
| 384 |
$stats['wordpress_version'] = $wp_version; |
| 385 |
$stats['wordpress_locale_pckg'] = get_locale(); |
| 386 |
$stats['php_version'] = phpversion(); |
| 387 |
$stats['mysql_version'] = $wpdb->db_version(); |
| 388 |
$stats['wp_multisite'] = $this->mmb_multisite; |
| 389 |
$stats['network_install'] = $this->network_admin_install; |
| 390 |
|
| 391 |
mwp_logger()->debug('Started encrypting cookies...'); |
| 392 |
|
| 393 |
$stats['cookies'] = $this->get_stat_cookies(); |
| 394 |
|
| 395 |
mwp_logger()->debug('Finished encrypting cookies...'); |
| 396 |
|
| 397 |
$absPath = trailingslashit(ABSPATH); // This will prevent 2 backslash when WP in root |
| 398 |
|
| 399 |
$stats['admin_usernames'] = $this->getUserList(); |
| 400 |
$stats['site_title'] = get_bloginfo('name'); |
| 401 |
$stats['site_tagline'] = get_bloginfo('description'); |
| 402 |
$stats['blog_public'] = get_option('blog_public'); |
| 403 |
$stats['timezone'] = get_option('timezone_string'); |
| 404 |
$stats['timezone_offset'] = get_option('gmt_offset'); |
| 405 |
$stats['server_ip'] = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : null; |
| 406 |
$stats['hostname'] = php_uname('n'); |
| 407 |
$stats['db_name'] = $this->get_active_db(); |
| 408 |
$stats['db_prefix'] = $wpdb->prefix; |
| 409 |
$stats['content_path'] = WP_CONTENT_DIR; |
| 410 |
$stats['absolute_path'] = $absPath; |
| 411 |
$stats['worker_path'] = $mmb_plugin_dir; |
| 412 |
$stats['site_home'] = get_option('home'); |
| 413 |
|
| 414 |
$fs = new Symfony_Filesystem_Filesystem(); |
| 415 |
if (defined('WP_CONTENT_DIR')) { |
| 416 |
$contentDir = WP_CONTENT_DIR; |
| 417 |
// This will prevent 2 backslash when WP in root |
| 418 |
if (substr($contentDir, 0, 2) === '//') { |
| 419 |
$contentDir = substr(WP_CONTENT_DIR, 1); |
| 420 |
$stats['content_path'] = $contentDir; |
| 421 |
} |
| 422 |
|
| 423 |
if (substr($contentDir, 0, 1) != '/' && strpos($contentDir, ABSPATH) === false) { |
| 424 |
$contentDir = ABSPATH.$contentDir; |
| 425 |
} |
| 426 |
$stats['content_relative_path'] = $fs->makePathRelative($contentDir, $absPath); |
| 427 |
} |
| 428 |
|
| 429 |
if (defined('WP_PLUGIN_DIR')) { |
| 430 |
$pluginDir = WP_PLUGIN_DIR; |
| 431 |
// This will prevent 2 backslash when WP in root |
| 432 |
if (substr($pluginDir, 0, 2) === '//') { |
| 433 |
$pluginDir = substr(WP_PLUGIN_DIR, 1); |
| 434 |
} |
| 435 |
|
| 436 |
if (substr($pluginDir, 0, 1) != '/' && strpos($pluginDir, ABSPATH) === false) { |
| 437 |
$pluginDir = ABSPATH.$pluginDir; |
| 438 |
} |
| 439 |
$stats['plugin_relative_path'] = $fs->makePathRelative($pluginDir, $absPath); |
| 440 |
} |
| 441 |
|
| 442 |
if (defined('WPMU_PLUGIN_DIR')) { |
| 443 |
$muPluginDir = WPMU_PLUGIN_DIR; |
| 444 |
// This will prevent 2 backslash when WP in root |
| 445 |
if (substr($muPluginDir, 0, 2) === '//') { |
| 446 |
$muPluginDir = substr(WPMU_PLUGIN_DIR, 1); |
| 447 |
} |
| 448 |
|
| 449 |
if (substr($muPluginDir, 0, 1) != '/' && strpos($muPluginDir, ABSPATH) === false) { |
| 450 |
$muPluginDir = ABSPATH.$muPluginDir; |
| 451 |
} |
| 452 |
$stats['mu_plugin_relative_path'] = $fs->makePathRelative($muPluginDir, $absPath); |
| 453 |
} |
| 454 |
|
| 455 |
$uploadDirArray = wp_upload_dir(); |
| 456 |
if (false === $uploadDir = realpath($uploadDirArray['basedir'])) { |
| 457 |
$uploadDir = $uploadDirArray['basedir']; |
| 458 |
} |
| 459 |
|
| 460 |
$stats['uploads_relative_path'] = $fs->makePathRelative($uploadDir, $absPath); |
| 461 |
|
| 462 |
$stats['writable'] = $this->is_server_writable(); |
| 463 |
$stats['fs_method'] = !$this->check_if_pantheon() ? get_filesystem_method() : ''; |
| 464 |
|
| 465 |
$mmode = get_option('mwp_maintenace_mode'); |
| 466 |
|
| 467 |
if (!empty($mmode) && isset($mmode['active']) && $mmode['active'] == true) { |
| 468 |
$stats['maintenance'] = true; |
| 469 |
} |
| 470 |
|
| 471 |
if ($this->mmb_multisite) { |
| 472 |
$stats = array_merge($stats, $this->get_multisite_stats($stats)); |
| 473 |
} |
| 474 |
|
| 475 |
return $stats; |
| 476 |
} |
| 477 |
|
| 478 |
public function get_multisite_stats() |
| 479 |
{ |
| 480 |
/** @var $wpdb wpdb */ |
| 481 |
global $current_user, $wpdb; |
| 482 |
$user_blogs = get_blogs_of_user($current_user->ID); |
| 483 |
$network_blogs = (array)$wpdb->get_results("select `blog_id`, `site_id` from `{$wpdb->blogs}`"); |
| 484 |
$user_id = !empty($GLOBALS['mwp_user_id']) ? $GLOBALS['mwp_user_id'] : false; |
| 485 |
$mainBlogId = defined('BLOG_ID_CURRENT_SITE') ? BLOG_ID_CURRENT_SITE : false; |
| 486 |
|
| 487 |
if ($this->network_admin_install != '1' || !is_super_admin($user_id) || empty($network_blogs)) { |
| 488 |
return array(); |
| 489 |
} |
| 490 |
|
| 491 |
$stats = array('network_blogs' => array(), 'other_blogs' => array()); |
| 492 |
foreach ($network_blogs as $details) { |
| 493 |
if (($mainBlogId !== false && $details->blog_id == $mainBlogId) || ($mainBlogId === false && $details->site_id == $details->blog_id)) { |
| 494 |
continue; |
| 495 |
} else { |
| 496 |
$data = get_blog_details($details->blog_id); |
| 497 |
if (in_array($details->blog_id, array_keys($user_blogs))) { |
| 498 |
$stats['network_blogs'][] = $data->siteurl; |
| 499 |
} else { |
| 500 |
$user = get_users( |
| 501 |
array( |
| 502 |
'blog_id' => $details->blog_id, |
| 503 |
'number' => 1, |
| 504 |
) |
| 505 |
); |
| 506 |
if (!empty($user)) { |
| 507 |
$stats['other_blogs'][$data->siteurl] = $user[0]->user_login; |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
return $stats; |
| 514 |
} |
| 515 |
|
| 516 |
public function get_auth_cookies($user_id) |
| 517 |
{ |
| 518 |
$cookies = array(); |
| 519 |
$secure = is_ssl(); |
| 520 |
$secure = apply_filters('secure_auth_cookie', $secure, $user_id); |
| 521 |
|
| 522 |
if ($secure) { |
| 523 |
$auth_cookie_name = SECURE_AUTH_COOKIE; |
| 524 |
$scheme = 'secure_auth'; |
| 525 |
} else { |
| 526 |
$auth_cookie_name = AUTH_COOKIE; |
| 527 |
$scheme = 'auth'; |
| 528 |
} |
| 529 |
|
| 530 |
$expiration = time() + 2592000; |
| 531 |
|
| 532 |
$cookies[$auth_cookie_name] = wp_generate_auth_cookie($user_id, $expiration, $scheme); |
| 533 |
$cookies[LOGGED_IN_COOKIE] = wp_generate_auth_cookie($user_id, $expiration, 'logged_in'); |
| 534 |
|
| 535 |
if (defined('WPE_APIKEY')) { |
| 536 |
$cookies['wpe-auth'] = md5('wpe_auth_salty_dog|'.WPE_APIKEY); |
| 537 |
} |
| 538 |
|
| 539 |
return $cookies; |
| 540 |
} |
| 541 |
|
| 542 |
public function get_stat_cookies() |
| 543 |
{ |
| 544 |
if (!defined('WPE_APIKEY')) { |
| 545 |
return array(); |
| 546 |
} |
| 547 |
|
| 548 |
global $current_user; |
| 549 |
|
| 550 |
$cookies = $this->get_auth_cookies($current_user->ID); |
| 551 |
$publicKey = mwp_worker_configuration()->getLivePublicKey('cookie_service', true); |
| 552 |
|
| 553 |
if (empty($publicKey)) { |
| 554 |
$publicKey = $this->get_master_public_key(); |
| 555 |
} |
| 556 |
|
| 557 |
if (empty($cookies) || empty($publicKey)) { |
| 558 |
return $cookies; |
| 559 |
} |
| 560 |
|
| 561 |
if (!class_exists('Crypt_RSA', false)) { |
| 562 |
require_once dirname(__FILE__).'/../../src/PHPSecLib/Crypt/RSA.php'; |
| 563 |
} |
| 564 |
|
| 565 |
$rsa = new Crypt_RSA(); |
| 566 |
$rsa->setEncryptionMode(CRYPT_RSA_SIGNATURE_PKCS1); |
| 567 |
$rsa->loadKey($publicKey, CRYPT_RSA_PUBLIC_FORMAT_PKCS1); |
| 568 |
|
| 569 |
foreach ($cookies as &$cookieValue) { |
| 570 |
$cookieValue = base64_encode($rsa->encrypt($cookieValue)); |
| 571 |
} |
| 572 |
|
| 573 |
return $cookies; |
| 574 |
} |
| 575 |
|
| 576 |
public function get_initial_stats() |
| 577 |
{ |
| 578 |
global $mmb_plugin_dir, $wpdb; |
| 579 |
|
| 580 |
$stats = array( |
| 581 |
'email' => get_option('admin_email'), |
| 582 |
'content_path' => WP_CONTENT_DIR, |
| 583 |
'worker_path' => $mmb_plugin_dir, |
| 584 |
'worker_version' => $GLOBALS['MMB_WORKER_VERSION'], |
| 585 |
'worker_revision' => $GLOBALS['MMB_WORKER_REVISION'], |
| 586 |
'site_title' => get_bloginfo('name'), |
| 587 |
'site_tagline' => get_bloginfo('description'), |
| 588 |
'db_name' => $this->get_active_db(), |
| 589 |
'site_home' => get_option('home'), |
| 590 |
'admin_url' => admin_url(), |
| 591 |
'wp_multisite' => $this->mmb_multisite, |
| 592 |
'network_install' => $this->network_admin_install, |
| 593 |
'cookies' => $this->get_stat_cookies(), |
| 594 |
'timezone' => get_option('timezone_string'), |
| 595 |
'timezone_offset' => get_option('gmt_offset'), |
| 596 |
'db_prefix' => $wpdb->prefix, |
| 597 |
'service_key' => mwp_get_service_key(), |
| 598 |
); |
| 599 |
|
| 600 |
if ($this->mmb_multisite) { |
| 601 |
$details = get_blog_details($this->mmb_multisite); |
| 602 |
if (isset($details->site_id)) { |
| 603 |
$details = get_blog_details($details->site_id); |
| 604 |
if (isset($details->siteurl)) { |
| 605 |
$stats['network_parent'] = $details->siteurl; |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
$stats['writable'] = $this->is_server_writable(); |
| 611 |
$stats['initial_stats'] = $this->get_stats(array()); |
| 612 |
|
| 613 |
return $stats; |
| 614 |
} |
| 615 |
|
| 616 |
public function get_active_db() |
| 617 |
{ |
| 618 |
global $wpdb; |
| 619 |
$sql = 'SELECT DATABASE() as db_name'; |
| 620 |
|
| 621 |
$sqlresult = $wpdb->get_row($sql); |
| 622 |
$active_db = $sqlresult->db_name; |
| 623 |
|
| 624 |
return $active_db; |
| 625 |
} |
| 626 |
|
| 627 |
public function get_hit_count() |
| 628 |
{ |
| 629 |
return get_option('user_hit_count'); |
| 630 |
} |
| 631 |
|
| 632 |
public function cmp_posts_worker($a, $b) |
| 633 |
{ |
| 634 |
if ($a->post_date === $b->post_date) { |
| 635 |
return 0; |
| 636 |
} |
| 637 |
|
| 638 |
return ($a->post_date < $b->post_date) ? 1 : -1; |
| 639 |
} |
| 640 |
|
| 641 |
public function trim_content($content = '', $length = 200) |
| 642 |
{ |
| 643 |
if (function_exists('mb_strlen') && function_exists('mb_substr')) { |
| 644 |
$content = (mb_strlen($content) > ($length + 3)) ? mb_substr($content, 0, $length).'...' : $content; |
| 645 |
} else { |
| 646 |
$content = (strlen($content) > ($length + 3)) ? substr($content, 0, $length).'...' : $content; |
| 647 |
} |
| 648 |
|
| 649 |
return $content; |
| 650 |
} |
| 651 |
} |
| 652 |
|