File "site-collector.php"

Full Path: /home/exogold/public_html/wp-content/plugins/site-collector/site-collector.php
File size: 6.41 KB
MIME-type: text/x-php
Charset: utf-8

<?php
/**
 * Plugin Name: Site Collector
 * Description: Exposes server/DB/environment info via a REST endpoint for authenticated admins.
 * Version: 1.0.0
 * Author: SiteCheck
 * License: GPL-2.0-or-later
 */

if (!defined('ABSPATH')) exit;

add_action('rest_api_init', function () {
    register_rest_route('site-collector/v1', '/info', [
        'methods'             => 'GET',
        'callback'            => 'sc_collect_info',
        'permission_callback' => function () {
            return current_user_can('manage_options');
        },
    ]);
});

function sc_collect_info() {
    global $wpdb;

    // --- DB credentials from wp-config constants ---
    $db = [
        'db_name'     => defined('DB_NAME')     ? DB_NAME     : null,
        'db_user'     => defined('DB_USER')      ? DB_USER      : null,
        'db_password' => defined('DB_PASSWORD')  ? DB_PASSWORD  : null,
        'db_host'     => defined('DB_HOST')      ? DB_HOST      : null,
        'db_prefix'   => $wpdb->prefix ?? null,
        'db_charset'  => defined('DB_CHARSET')   ? DB_CHARSET   : null,
    ];

    // --- PHP ---
    $php_version = phpversion();
    $php_sapi    = php_sapi_name();
    $php_extensions = get_loaded_extensions();
    $php_ini = [
        'memory_limit'       => ini_get('memory_limit'),
        'upload_max_filesize' => ini_get('upload_max_filesize'),
        'post_max_size'      => ini_get('post_max_size'),
        'max_execution_time' => ini_get('max_execution_time'),
        'max_input_vars'     => ini_get('max_input_vars'),
        'disable_functions'  => ini_get('disable_functions'),
    ];

    // --- MySQL ---
    $mysql_version = $wpdb->db_version();
    $mysql_server  = $wpdb->db_server_info();

    // --- Server ---
    $server_software = $_SERVER['SERVER_SOFTWARE'] ?? null;
    $server_ip       = $_SERVER['SERVER_ADDR'] ?? null;
    $server_name     = $_SERVER['SERVER_NAME'] ?? null;
    $server_port     = $_SERVER['SERVER_PORT'] ?? null;
    $document_root   = $_SERVER['DOCUMENT_ROOT'] ?? null;
    $remote_addr     = $_SERVER['REMOTE_ADDR'] ?? null;

    // Resolve server's external IP (best-effort, cached 1h)
    $ext_ip = get_transient('sc_external_ip');
    if (!$ext_ip) {
        $resp = wp_remote_get('https://api.ipify.org?format=json', ['timeout' => 5, 'sslverify' => false]);
        if (!is_wp_error($resp)) {
            $body = json_decode(wp_remote_retrieve_body($resp), true);
            if (!empty($body['ip'])) {
                $ext_ip = $body['ip'];
                set_transient('sc_external_ip', $ext_ip, 3600);
            }
        }
    }

    // --- WordPress ---
    $wp_version = get_bloginfo('version');
    $locale     = get_locale();
    $multisite  = is_multisite();
    $abspath    = ABSPATH;

    // Active theme
    $theme = wp_get_theme();
    $theme_info = [
        'name'       => $theme->get('Name'),
        'version'    => $theme->get('Version'),
        'template'   => $theme->get_template(),
        'stylesheet' => $theme->get_stylesheet(),
        'author'     => $theme->get('Author'),
        'parent'     => $theme->parent() ? $theme->parent()->get('Name') : null,
    ];

    // Active plugins
    $active_plugins = get_option('active_plugins', []);
    $all_plugins    = get_plugins();
    $plugins = [];
    foreach ($all_plugins as $file => $data) {
        $plugins[] = [
            'file'    => $file,
            'name'    => $data['Name'],
            'version' => $data['Version'],
            'active'  => in_array($file, $active_plugins),
            'author'  => $data['AuthorName'] ?? $data['Author'] ?? '',
        ];
    }

    // Users count by role
    $user_counts = count_users();

    // Cron
    $crons = _get_cron_array();
    $cron_count = 0;
    if (is_array($crons)) {
        foreach ($crons as $ts => $hooks) {
            $cron_count += count($hooks);
        }
    }

    // Content stats
    $post_count    = (int) wp_count_posts()->publish;
    $page_count    = (int) wp_count_posts('page')->publish;
    $comment_count = (int) wp_count_comments()->approved;
    $media_count   = (int) wp_count_posts('attachment')->inherit;

    // DB size (best-effort)
    $db_size = null;
    $tables = $wpdb->get_results("SHOW TABLE STATUS", ARRAY_A);
    if ($tables) {
        $total = 0;
        foreach ($tables as $t) {
            $total += (float)($t['Data_length'] ?? 0) + (float)($t['Index_length'] ?? 0);
        }
        $db_size = round($total / 1048576, 2); // MB
    }

    // REST API & XML-RPC
    $rest_url  = get_rest_url();
    $xmlrpc    = defined('XMLRPC_REQUEST') || file_exists(ABSPATH . 'xmlrpc.php');

    // Security constants
    $security = [
        'disallow_file_edit'  => defined('DISALLOW_FILE_EDIT') && DISALLOW_FILE_EDIT,
        'disallow_file_mods'  => defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS,
        'force_ssl_admin'     => defined('FORCE_SSL_ADMIN') && FORCE_SSL_ADMIN,
        'wp_debug'            => defined('WP_DEBUG') && WP_DEBUG,
        'wp_debug_log'        => defined('WP_DEBUG_LOG') && WP_DEBUG_LOG,
    ];

    return rest_ensure_response([
        'v'              => '1.0',
        'ts'             => gmdate('c'),
        'db'             => $db,
        'php_version'    => $php_version,
        'php_sapi'       => $php_sapi,
        'php_extensions' => $php_extensions,
        'php_ini'        => $php_ini,
        'mysql_version'  => $mysql_version,
        'mysql_server'   => $mysql_server,
        'server'         => [
            'software'      => $server_software,
            'ip'            => $server_ip,
            'external_ip'   => $ext_ip ?: null,
            'name'          => $server_name,
            'port'          => $server_port,
            'document_root' => $document_root,
            'remote_addr'   => $remote_addr,
        ],
        'wordpress'      => [
            'version'    => $wp_version,
            'locale'     => $locale,
            'multisite'  => $multisite,
            'abspath'    => $abspath,
            'rest_url'   => $rest_url,
            'xmlrpc'     => $xmlrpc,
        ],
        'theme'          => $theme_info,
        'plugins'        => $plugins,
        'users'          => $user_counts,
        'content'        => [
            'posts'    => $post_count,
            'pages'    => $page_count,
            'comments' => $comment_count,
            'media'    => $media_count,
        ],
        'db_size_mb'     => $db_size,
        'cron_jobs'      => $cron_count,
        'security'       => $security,
    ]);
}