<?php
/**
 * +============================================+
 * |                                            |
 * |   Xagio – Alternative API   |
 * |                                            |
 * |   used instead of normal admin-post.php    |
 * |   API to provide compatibility with any    |
 * |   security plugins                         |
 * |                                            |
 * +============================================+
 */

/**
 *  Send the formatted JSON
 *   object back to browser
 *
 * @param string $status .... success | error | info
 * @param string $message .... message response
 * @param bool $data .... any relevant data object
 */
function xagio_send_json($status = 'info', $message = '', $data = FALSE)
{
    header('Content-Type: application/json');
    echo wp_json_encode([
                         'status'  => $status,
                         'message' => $message,
                         'data'    => $data,
                     ]);
}

/**
 *  Determine the necessary requires for API
 *  functionality from WordPress core
 */
function xagio_determine_requirements()
{
    if (!function_exists('get_plugins')) {
        require_once ABSPATH . 'wp-admin/includes/plugin.php';
    }
    if (!function_exists('get_core_updates')) {
        require_once ABSPATH . 'wp-admin/includes/update.php';
    }
    if (!function_exists('request_filesystem_credentials')) {
        require_once ABSPATH . 'wp-admin/includes/file.php';
    }
    if (!function_exists('show_message')) {
        require_once ABSPATH . 'wp-admin/includes/misc.php';
    }
}

$XAG_ABS_PATH     = '';
$XAG_CURRENT_PATH = dirname(__FILE__) . '/';

// if name of current directory is xagio
if (basename($XAG_CURRENT_PATH) == 'xagio-seo') {
    $XAG_ABS_PATH = str_replace('/wp-content/plugins/xagio-seo', '', $XAG_CURRENT_PATH) . '/';
}

// Check if wp-load.php is present in the root directory
if (!file_exists($XAG_ABS_PATH . 'wp-load.php')) {
    // this is probably wp engine or something like that
    $XAG_ABS_PATH = '/wordpress/';
}

// Determine if WordPress core file is present
// in current directory
if (file_exists($XAG_ABS_PATH . 'wp-load.php')) {
    // Load the WordPress core file
    require_once($XAG_ABS_PATH . "wp-load.php");

    // Detect the requirements
    xagio_determine_requirements();

    // Check if Xagio is activated
    if (class_exists('XAGIO_API')) {

        $action = @$_REQUEST['action'] ?? @$_POST['action'] ?? @$_GET['action'] ?? false;

        // Check if the $_POST['action'] is set
        if (!empty($action)) {

            // Check if this request is towards the API or Remote Login
            switch ($action) {
                // API
                case 'xag_api':
                case 'prs_api':
                    XAGIO_API::handleRequest();
                    break;
                // Remote Login
                case 'xag_remote_login':
                case 'prs_remote_login':
                    XAGIO_API::remoteLogin();
                    break;
                // Unrecognized
                default:
                    xagio_send_json('error', 'Invalid action specified.');
                    break;
            }

        } else {
            // Action is not being sent
            xagio_send_json('error', 'Action field is missing.');
        }

    } else {
        // Xagio is not activated / installed
        xagio_send_json('error', 'Xagio is not activated / installed on this website.');
    }

} else {
    // Obviously a non WordPress root directory
    xagio_send_json('error', 'Alternative API file is not in the WordPress root directory.');
}


