Failed to retrieve data from DlerCloud API.

Home » Snippets » Failed to retrieve data from DlerCloud API.
0

Created with:

Visibility: 

public

Creator: Kabucho

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/*
Plugin Name: DlerCloud API Wrapper 1.0.1
Description: WordPress wrapper for DlerCloud API
*/

require_once ABSPATH . 'wp-includes/class-wp-http.php';

class DlerCloudAPI {
    private $access_token;
    private $user_id;
    private $host;
    private $base_url;
    private $http;

    public function __construct($access_token = null, $host = 'dlercloud.co') {
        $this->access_token = $access_token;
        $this->user_id = null;
        $this->host = $host;
        $this->base_url = 'https://' . $this->host . '/api/v1/';
        $this->http = new WP_Http();
    }

    private function _request($path, $data = null, $args = array()) {
        $data = $data ?? array();
        if ($this->access_token && !in_array($path, array('login', 'logout'))) {
            $data['access_token'] = $this->access_token;
        }
        $url = $this->base_url . $path;
        $response = $this->http->post($url, array_merge($args, array('body' => $data)));
        if (is_wp_error($response)) {
            throw new Exception($response->get_error_message());
        }
        $resp_json = json_decode($response['body'], true);
        error_log('API Response: ' . print_r($resp_json, true));

        if (!$resp_json) {
            throw new Exception('Empty API response.');
        } elseif (!isset($resp_json['ret'])) {
            throw new Exception('Missing "ret" field in API response.');
        } elseif (!isset($resp_json['msg'])) {
            throw new Exception('Missing "msg" field in API response.');
        } elseif (!isset($resp_json['data'])) {
            throw new Exception('Missing "data" field in API response.');
        }

        $ret_code = intval($resp_json['ret']);
        if ($ret_code < 200 || $ret_code >= 300) {
            throw new Exception('[' . $ret_code . '] ' . $resp_json['msg']);
        }

        return $resp_json['data'];
    }

    public function login($email, $password) {
        $data = $this->_request('login', array('email' => $email, 'passwd' => $password));
        if (isset($data['token'])) {
            $this->access_token = $data['token'];
        }
        if (isset($data['user_id'])) {
            $this->user_id = $data['user_id'];
        }

        return $this->access_token;
    }

    public function logout($email = null, $password = null) {
        if ($email && $password) {
            $this->_request('logout', array('email' => $email, 'passwd' => $password));
            $this->access_token = null;
        } elseif ($this->access_token) {
            $this->_request('logout', array('access_token' => $this->access_token));
            $this->access_token = null;
        } else {
            throw new Exception('Email and password, or access token (.access_token) is needed at least');
        }
    }

    public function get_nodes() {
        return new Nodes($this);
    }
}

class Nodes {
    private $api;

    public function __construct(DlerCloudAPI $api) {
        $this->api = $api;
    }

    public function ss() {
        return new Shadowsocks($this->api);
    }
}

class Shadowsocks {
    private $api;

    public function __construct(DlerCloudAPI $api) {
        $this->api = $api;
    }

    public function __call($name, $args) {
        if ($name === 'connection_password') {
            return $this->getConnectionPassword();
        } else {
            throw new Exception('Unknown method: ' . $name);
        }
    }

    private function getConnectionPassword() {
        $nodes = $this->api->get_nodes();
        $data = $nodes->_request('ss');
        if (empty($data) || !isset($data['connection_password'])) {
            throw new Exception('Failed to fetch SS connection password');
        }
        return $data['connection_password'];
    }
}

// Plugin Settings Page
function dlercloud_api_settings_page() {
    $access_token = 'S5difVjlGDPUX6z9xcwJ'; // Replace with your DlerCloud access token

    // Retrieve user information
    $user_info = dlercloud_api_request('information', 'POST', $access_token);

    // Retrieve managed Clash data
    $managed_clash = dlercloud_api_request('managed/clash', 'POST', $access_token);

    if ($user_info !== false && $managed_clash !== false) {
        $hosting_address = $managed_clash['ss'] ?? '';

        // Render the settings page
        ?>
        <div class="wrap">
            <h1>DlerCloud API Settings</h1>
            <p>Hosting Address: <?php echo $hosting_address; ?></p>
            <p>Share this hosting address with sub users.</p>
            <p>Each sub user can only use it once and on one gadget.</p>
            <h2>Active Users</h2>
            <ul>
                <?php if (isset($managed_clash['data']) && is_array($managed_clash['data'])):
                    foreach ($managed_clash['data'] as $user): ?>
                        <li><?php echo $user['name']; ?></li>
                    <?php endforeach;
                else: ?>
                    <li>No active users found.</li>
                <?php endif; ?>
            </ul>
            <h2>User Information</h2>
            <ul>
                <li>Plan: <?php echo $user_info['plan'] ?? 'N/A'; ?></li>
                <li>Plan Time: <?php echo $user_info['plan_time'] ?? 'N/A'; ?></li>
                <li>Money: <?php echo $user_info['money'] ?? 'N/A'; ?></li>
                <li>Affiliate Money: <?php echo $user_info['aff_money'] ?? 'N/A'; ?></li>
                <li>Today's Used Traffic: <?php echo $user_info['today_used'] ?? 'N/A'; ?></li>
                <li>Total Used Traffic: <?php echo $user_info['used'] ?? 'N/A'; ?></li>
                <li>Unused Traffic: <?php echo $user_info['unused'] ?? 'N/A'; ?></li>
                <li>Total Traffic: <?php echo $user_info['traffic'] ?? 'N/A'; ?></li>
                <li>Integral: <?php echo $user_info['integral'] ?? 'N/A'; ?></li>
            </ul>
        </div>
        <?php
    } else {
        echo 'Failed to retrieve data from DlerCloud API.';
        echo '<br>';
        echo 'User Info Response: ';
        var_dump($user_info);
        echo '<br>';
        echo 'Managed Clash Response: ';
        var_dump($managed_clash);
    }
}

// Helper function to make API requests
function dlercloud_api_request($endpoint, $method, $access_token, $data = array()) {
    $api_url = 'https://dler.cloud/api/v1/' . $endpoint;

    $headers = array(
        'Content-Type: application/json',
        'Authorization: Bearer ' . $access_token
    );

    $options = array(
        'method'  => $method,
        'headers' => $headers,
        'timeout' => 30,
        'body'    => json_encode($data)
    );

    $response = wp_remote_request($api_url, $options);

    if (is_wp_error($response)) {
        return false;
    }

    $body = wp_remote_retrieve_body($response);
    $decoded_body = json_decode($body, true);

    if ($decoded_body && isset($decoded_body['ret']) && $decoded_body['ret'] === 200) {
        return $decoded_body['data'] ?? array();
    }

    return false;
}

// Add the settings page to WordPress admin menu
function dlercloud_api_add_menu() {
    add_options_page('DlerCloud API Settings', 'DlerCloud API', 'manage_options', 'dlercloud-api-settings', 'dlercloud_api_settings_page');
}

// Hook the menu function to the admin menu
add_action('admin_menu', 'dlercloud_api_add_menu');
				

Register an account to save your snippets or go Pro to get more features.