/**
 * Gets all kinds of system installation info. Kudos to WP-Migrate-DB and Send-System-Info plugins for the most of 
 * this.
 * 
 * @author Nevma (info@nevma.gr)
 * 
 * @return void Nothing really!
 */
function adaptive_images_debug_diagnostic_info($echo = true)
{
    global $table_prefix;
    global $wpdb;
    // Collect diagnostic information.
    $debug = array();
    $debug['Web Server'] = esc_html($_SERVER['SERVER_SOFTWARE']);
    $debug['Document Root'] = esc_html($_SERVER['DOCUMENT_ROOT']);
    $debug['PHP'] = esc_html(phpversion());
    $debug['PHP Time Limit'] = esc_html(ini_get('max_execution_time'));
    $debug['PHP Memory Limit'] = esc_html(ini_get('memory_limit'));
    $debug['PHP Post Max Size'] = esc_html(ini_get('post_max_size'));
    $debug['PHP Upload Max Size'] = esc_html(ini_get('upload_max_filesize'));
    $debug['PHP Max Input Vars'] = esc_html(ini_get('max_input_vars'));
    $debug['PHP Display Errors'] = esc_html(ini_get('display_errors') ? 'Yes' : 'No');
    $debug['PHP Error Log'] = esc_html(ini_get('error_log'));
    $debug['MySQL'] = esc_html(empty($wpdb->use_mysqli) ? mysql_get_server_info() : mysqli_get_server_info($wpdb->dbh));
    $debug['MySQL Ext/mysqli'] = empty($wpdb->use_mysqli) ? 'No' : 'Yes';
    $debug['MySQL Table Prefix'] = esc_html($table_prefix);
    $debug['MySQL DB Charset'] = esc_html(DB_CHARSET);
    $debug['WP'] = get_bloginfo('version');
    $debug['WP Multisite'] = is_multisite() ? 'Yes' : 'No';
    $debug['WP Debug Mode'] = esc_html(defined('WP_DEBUG') && WP_DEBUG ? 'Yes' : 'No');
    $debug['WP Site url'] = esc_html(site_url());
    $debug['WP WP Home url'] = esc_html(home_url());
    $debug['WP Permalinks'] = esc_html(get_option('permalink_structure'));
    $debug['WP home path'] = esc_html(get_home_path());
    $debug['WP content dir'] = esc_html(WP_CONTENT_DIR);
    $debug['WP plugin dir'] = esc_html(WP_PLUGIN_DIR);
    $debug['WP content url'] = esc_html(WP_CONTENT_URL);
    $debug['WP plugin url'] = esc_html(WP_PLUGIN_URL);
    $debug['WP Locale'] = esc_html(get_locale());
    $debug['WP Memory Limit'] = esc_html(WP_MEMORY_LIMIT);
    $debug['WP Max Upload Size'] = esc_html(adaptive_images_plugin_file_size_human(wp_max_upload_size()));
    // Active system plugins.
    $active_plugins = (array) get_option('active_plugins', array());
    $active_plugins_output = '';
    foreach ($active_plugins as $plugin) {
        $plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin);
        $active_plugins_output .= $plugin_data['Name'] . ' v.' . $plugin_data['Version'] . ' by ' . $plugin_data['AuthorName'] . '<br />';
    }
    $debug['WP Active plugins'] = $active_plugins_output;
    // Multisite (network) plugins.
    if (is_multisite()) {
        $network_active_plugins = wp_get_active_network_plugins();
        $active_plugins_output = '';
        if (count($network_active_plugins) > 0) {
            foreach ($network_active_plugins as $plugin) {
                $plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin);
                $active_plugins_output .= $plugin_data['Name'] . ' v.' . $plugin_data['Version'] . ' by ' . $plugin_data['AuthorName'] . '<br />';
            }
        }
        $debug['WP Network active plugins'] = $active_plugins_output;
    }
    // Must-use plugins.
    $mu_plugins = wp_get_mu_plugins();
    $active_plugins_output = '';
    if (count($mu_plugins) > 0) {
        foreach ($mu_plugins as $plugin) {
            $plugin_data = get_plugin_data($plugin);
            $active_plugins_output .= $plugin_data['Name'] . ' v.' . $plugin_data['Version'] . ' by ' . $plugin_data['AuthorName'] . '<br />';
        }
    }
    $debug['WP MU plugins'] = $active_plugins_output;
    // Create diagnostic output HTML table.
    $debug_output = '<table><tbody>';
    foreach ($debug as $key => $value) {
        $debug_output .= '<tr>' . '<td style = "vertical-align: top; whitespace: nowrap; padding-right: 5px;">' . $key . '</td>' . '<td><p style = "margin: 0; padding: 0 0 2px 0;">' . $value . '</p></td>' . '</tr>';
    }
    $debug_output .= '</tbody></table>';
    // Echo debug info or return it.
    if ($echo) {
        echo $debug_output;
    } else {
        return $debug_output;
    }
}
/**
 * Takes care of the plugin settings page actions.
 * 
 * @author Nevma (info@nevma.gr)
 * 
 * @return void Nothing really!.
 */
function adaptive_images_admin_settings_actions()
{
    if (!isset($_GET['action'])) {
        return;
    }
    // Cleanup image cache action.
    if ($_GET['action'] == 'cleanup-image-cache' && wp_verify_nonce($_GET['_wpnonce'], 'adaptive-images-cleanup-image-cache')) {
        $cache_path = adaptive_images_plugin_get_cahe_directory_path();
        $result = adaptive_images_actions_rmdir_recursive($cache_path);
        add_settings_error('adaptive-images-settings', 'adaptive-images-settings-error', 'Cleanup image cache <hr />' . '<p>Total files deleted from the adaptive images cache: ' . $result['files'] . '</p>' . '<p>Total directories deleted from the adaptive images cache: ' . $result['dirs'] . '</p>' . '<p>' . 'Total size deleted from the adaptive images cache: ' . adaptive_images_plugin_file_size_human($result['size']) . '</p>', 'updated');
    }
    // Calculate image cache size action.
    if ($_GET['action'] == 'calculate-cache-size' && wp_verify_nonce($_GET['_wpnonce'], 'adaptive-images-calculate-cache-size')) {
        $cache_path = adaptive_images_plugin_get_cahe_directory_path();
        $cache_size = adaptive_images_plugin_dir_size($cache_path);
        add_settings_error('adaptive-images-settings', 'adaptive-images-settings-error', 'Calculate cache size <hr />' . '<p>Total files in the adaptive images cache: ' . $cache_size['files'] . '</p>' . '<p>Total directories in the adaptive images cache: ' . $cache_size['dirs'] . '</p>' . '<p>' . 'Total size of the adaptive images cache: ' . adaptive_images_plugin_file_size_human($cache_size['size']) . '</p>', 'updated');
    }
    // Print plugin info action.
    if ($_GET['action'] == 'print-debug-info' && wp_verify_nonce($_GET['_wpnonce'], 'adaptive-images-print-debug-info')) {
        add_settings_error('adaptive-images-settings', 'adaptive-images-settings-error', 'Debug info <hr />' . adaptive_images_debug_general_info(FALSE), 'updated');
    }
    // Print system info action.
    if ($_GET['action'] == 'print-diagnostic-info' && wp_verify_nonce($_GET['_wpnonce'], 'adaptive-images-print-diagnostic-info')) {
        add_settings_error('adaptive-images-settings', 'adaptive-images-settings-error', 'System information <hr />' . adaptive_images_debug_diagnostic_info(FALSE), 'updated');
    }
}