Added example env
This commit is contained in:
@@ -8,8 +8,9 @@ class File_Server {
|
|||||||
const int FAV_SERVER_NEED_CHANGE = 3600; // In seconds.
|
const int FAV_SERVER_NEED_CHANGE = 3600; // In seconds.
|
||||||
|
|
||||||
const array ENDPOINTS = [
|
const array ENDPOINTS = [
|
||||||
'random-server' => "servers/take-a-random-server.php?return_type=%s",
|
'random-server' => "Getrandomserver",
|
||||||
'server-by-id' => "servers/get-server-by-id.php?server_id=%s",
|
'server-by-id' => "Getserver&server_id=%d",
|
||||||
|
'server-by-url' => "Getserver&server_url=%s"
|
||||||
];
|
];
|
||||||
|
|
||||||
private(set) public string $server_url;
|
private(set) public string $server_url;
|
||||||
@@ -19,9 +20,9 @@ class File_Server {
|
|||||||
|
|
||||||
if( $get_a_server ){
|
if( $get_a_server ){
|
||||||
$resp = self::get_favorite_server( $post_id );
|
$resp = self::get_favorite_server( $post_id );
|
||||||
if( $resp !== null ) {
|
if( !isset( $resp['_wp_error'] ) ) {
|
||||||
$this->server_url = $resp[0];
|
$this->server_url = $resp['server_url'];
|
||||||
$this->server_id = intval($resp[1]);
|
$this->server_id = $resp['server_id'];
|
||||||
} else {
|
} else {
|
||||||
$this->server_url = "";
|
$this->server_url = "";
|
||||||
$this->server_id = -1;
|
$this->server_id = -1;
|
||||||
@@ -44,11 +45,34 @@ class File_Server {
|
|||||||
|
|
||||||
public static function build_url( string $endpoint, ...$args ){
|
public static function build_url( string $endpoint, ...$args ){
|
||||||
|
|
||||||
return sprintf( $_ENV['ROMHACKPLAZA_API_URL'] . $endpoint, ...$args );
|
return sprintf( $_ENV['ROMHACKPLAZA_API_URL'] . "index.php?action=" . $endpoint, ...$args );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function get_favorite_server( int $post_id ){
|
public static function generate_token(
|
||||||
|
int $user_id,
|
||||||
|
string $to,
|
||||||
|
string $action
|
||||||
|
): string {
|
||||||
|
|
||||||
|
$info = [
|
||||||
|
'user_id' => $user_id,
|
||||||
|
'to' => $to,
|
||||||
|
'action' => $action,
|
||||||
|
'generated_at' => time(),
|
||||||
|
'expires_at' => time() + ( 15 * 60 ),
|
||||||
|
'romhackplaza' => \bin2hex( random_bytes( 16 ) )
|
||||||
|
];
|
||||||
|
|
||||||
|
$json = json_encode( $info );
|
||||||
|
$sig = hash_hmac( 'sha256', $json, $_ENV['ROMHACKPLAZAFS_SECRET_KEY'] );
|
||||||
|
|
||||||
|
$end = base64_encode( $json ) . "|" . $sig;
|
||||||
|
return $end;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function get_favorite_server( int $post_id ): array {
|
||||||
|
|
||||||
if( $post_id == 0 )
|
if( $post_id == 0 )
|
||||||
return self::get_a_random_server();
|
return self::get_a_random_server();
|
||||||
@@ -63,30 +87,33 @@ class File_Server {
|
|||||||
if( $time > self::FAV_SERVER_NEED_CHANGE )
|
if( $time > self::FAV_SERVER_NEED_CHANGE )
|
||||||
return self::get_a_random_server();
|
return self::get_a_random_server();
|
||||||
|
|
||||||
$server_url = wp_remote_get( self::build_url( self::ENDPOINTS['server-by-id'], $favorite_server ) )['body'];
|
$request = wp_remote_get( self::build_url( self::ENDPOINTS['server-by-id'], $favorite_server ) );
|
||||||
return [ $server_url, $favorite_server ];
|
if( is_wp_error( $request ) ) {
|
||||||
|
return [ '_wp_error' => 1 ];
|
||||||
|
}
|
||||||
|
|
||||||
|
$body = json_decode( wp_remote_retrieve_body( $request ), ARRAY_A );
|
||||||
|
if( $body['success'] !== true ){
|
||||||
|
return [ '_wp_error' => 2 ];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $body['server'];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function get_a_random_server( $return_type = "PHP" ){
|
public static function get_a_random_server(): array {
|
||||||
|
|
||||||
switch( $return_type ) {
|
$request = wp_remote_get( self::build_url( self::ENDPOINTS['random-server'] ) );
|
||||||
case "PHP":
|
if( is_wp_error( $request ) ) {
|
||||||
case "JS":
|
return [ '_wp_error' => 1 ];
|
||||||
break;
|
|
||||||
default:
|
|
||||||
$return_type = "RAW";
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$resp = wp_remote_get( self::build_url( self::ENDPOINTS['random-server'], $return_type ) );
|
$body = json_decode( wp_remote_retrieve_body( $request ), ARRAY_A );
|
||||||
if( !is_wp_error( $resp ) ) {
|
if( $body['success'] !== true ){
|
||||||
if ($return_type === "PHP")
|
return [ '_wp_error' => 2 ];
|
||||||
return explode("|", $resp['body']);
|
|
||||||
else
|
|
||||||
echo $resp['body'];
|
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
return $body['server'];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
30
src/Extenders/Ajax/Generate_File_Server_Token.php
Normal file
30
src/Extenders/Ajax/Generate_File_Server_Token.php
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace RomhackPlaza\Extenders\Ajax;
|
||||||
|
use RomhackPlaza\API\File_Server;
|
||||||
|
|
||||||
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
|
class Generate_File_Server_Token extends Abstract_Ajax {
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
parent::__construct(
|
||||||
|
'generate_file_server_token',
|
||||||
|
false
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function handle()
|
||||||
|
{
|
||||||
|
|
||||||
|
$to = isset( $_POST['to'] ) ? sanitize_url( $_POST['to'] ) : '';
|
||||||
|
$action = isset( $_POST['action'] ) ? sanitize_text_field( $_POST['action'] ) : '';
|
||||||
|
|
||||||
|
if( $to === "" || $action === "" )
|
||||||
|
wp_send_json_error( ['message' => "Action or To required." ] );
|
||||||
|
|
||||||
|
$token = File_Server::generate_token( get_current_user_id(), $to, $action );
|
||||||
|
wp_send_json_success( ['message' => 'Good', 'zeus' => $token ] );
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user