120 lines
3.4 KiB
PHP
120 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace RomhackPlaza\API;
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
class File_Server {
|
|
|
|
const int FAV_SERVER_NEED_CHANGE = 3600; // In seconds.
|
|
|
|
const array ENDPOINTS = [
|
|
'random-server' => "Getrandomserver",
|
|
'server-by-id' => "Getserver&server_id=%d",
|
|
'server-by-url' => "Getserver&server_url=%s"
|
|
];
|
|
|
|
private(set) public string $server_url;
|
|
private(set) public int $server_id;
|
|
|
|
public function __construct( int $post_id = 0, bool $get_a_server = true ) {
|
|
|
|
if( $get_a_server ){
|
|
$resp = self::get_favorite_server( $post_id );
|
|
if( !isset( $resp['_wp_error'] ) ) {
|
|
$this->server_url = $resp['server_url'];
|
|
$this->server_id = $resp['server_id'];
|
|
} else {
|
|
$this->server_url = "";
|
|
$this->server_id = -1;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public function print_js(){
|
|
|
|
echo sprintf( '<script>const chosenDownloadServer="%s";const chosenDownloadServerId="%d";</script>', $this->server_url, $this->server_id );
|
|
|
|
}
|
|
|
|
public function print(){
|
|
|
|
echo $this->server_url . "|" . $this->server_id;
|
|
|
|
}
|
|
|
|
public static function build_url( string $endpoint, ...$args ){
|
|
|
|
return sprintf( $_ENV['ROMHACKPLAZA_API_URL'] . "index.php?action=" . $endpoint, ...$args );
|
|
|
|
}
|
|
|
|
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 )
|
|
return self::get_a_random_server();
|
|
|
|
$favorite_server = get_post_meta( $post_id, 'favorite_server', true ) ?? false;
|
|
$favorite_server_timestamp = get_post_meta( $post_id, 'favorite_server_timestamp', true ) ?? false;
|
|
|
|
if( $favorite_server === false || $favorite_server_timestamp === false )
|
|
return self::get_a_random_server();
|
|
|
|
$time = time() - intval( $favorite_server_timestamp );
|
|
if( $time > self::FAV_SERVER_NEED_CHANGE )
|
|
return self::get_a_random_server();
|
|
|
|
$request = wp_remote_get( self::build_url( self::ENDPOINTS['server-by-id'], $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(): array {
|
|
|
|
$request = wp_remote_get( self::build_url( self::ENDPOINTS['random-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'];
|
|
|
|
}
|
|
|
|
} |