captaincore-manager/app/Run.php
2026-03-04 17:29:23 -05:00

223 lines
No EOL
7.5 KiB
PHP

<?php
namespace CaptainCore;
class Run {
protected $account_id = "";
public static function CLI( $command = "", $background = false ) {
if ( empty( $command ) ) {
return;
}
// Disable https when debug enabled
if ( defined( 'CAPTAINCORE_DEBUG' ) ) {
add_filter( 'https_ssl_verify', '__return_false' );
}
$data = [
'timeout' => 45,
'headers' => [
'Content-Type' => 'application/json; charset=utf-8',
'token' => captaincore_get_cli_token()
],
'body' => json_encode( [ "command" => $command ]),
'method' => 'POST',
'data_format' => 'body'
];
// Add command to dispatch server
$url = CAPTAINCORE_CLI_ADDRESS . "/run";
if ( $background ) {
$url = CAPTAINCORE_CLI_ADDRESS . "/run/background";
}
$response = wp_remote_post( $url, $data );
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
return [];
}
return $response["body"];
}
/**
* Executes a remote CLI command and streams the raw output directly.
* Ideal for binary data like images.
*/
public static function CLI_Stream( $command = "" ) {
if ( empty( $command ) ) {
return;
}
$url = CAPTAINCORE_CLI_ADDRESS . "/run/stream";
$payload = json_encode([ "command" => $command ]);
$headers = [
'Content-Type: application/json; charset=utf-8',
'token: ' . captaincore_get_cli_token(),
'Content-Length: ' . strlen( $payload )
];
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_TIMEOUT, 60 ); // Increased timeout for larger files
// This is the key: disable the return transfer and set a write function.
// This tells cURL to output data chunks directly as they are received.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
curl_setopt( $ch, CURLOPT_WRITEFUNCTION, function( $curl, $data ) {
echo $data; // Echo the raw data chunk
return strlen( $data ); // Return bytes handled
});
// Disable SSL verification if debug is on
if ( defined( 'CAPTAINCORE_DEBUG' ) ) {
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
}
// Execute the request. The output is streamed directly.
curl_exec( $ch );
curl_close( $ch );
}
public static function background( $command = "" ) {
// Disable https when debug enabled
if ( defined( 'CAPTAINCORE_DEBUG' ) ) {
add_filter( 'https_ssl_verify', '__return_false' );
}
$data = [
'timeout' => 45,
'headers' => [
'Content-Type' => 'application/json; charset=utf-8',
'token' => captaincore_get_cli_token()
],
'body' => json_encode( [ "command" => $command ]),
'method' => 'POST',
'data_format' => 'body'
];
// Add command to dispatch server
$response = wp_remote_post( CAPTAINCORE_CLI_ADDRESS . "/run/background", $data );
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
return "Something went wrong: $error_message";
}
return $response["body"];
}
public static function execute( $command = "" ) {
// Disable https when debug enabled
if ( defined( 'CAPTAINCORE_DEBUG' ) ) {
add_filter( 'https_ssl_verify', '__return_false' );
}
$data = [
'timeout' => 300,
'headers' => [
'Content-Type' => 'application/json; charset=utf-8',
'token' => captaincore_get_cli_token()
],
'body' => json_encode( [ "command" => $command ] ),
'method' => 'POST',
'data_format' => 'body'
];
$response = wp_remote_post( CAPTAINCORE_CLI_ADDRESS . "/run", $data );
if ( is_wp_error( $response ) ) {
return new \WP_Error( 'request_failed', $response->get_error_message(), [ 'status' => 500 ] );
}
return [ "status" => "completed", "response" => $response["body"] ];
}
public static function background_task( $command = "" ) {
// Disable https when debug enabled
if ( defined( 'CAPTAINCORE_DEBUG' ) ) {
add_filter( 'https_ssl_verify', '__return_false' );
}
$data = [
'timeout' => 45,
'headers' => [
'Content-Type' => 'application/json; charset=utf-8',
'token' => captaincore_get_cli_token()
],
'body' => json_encode( [ "command" => $command ] ),
'method' => 'POST',
'data_format' => 'body'
];
$response = wp_remote_post( CAPTAINCORE_CLI_ADDRESS . "/run/background", $data );
if ( is_wp_error( $response ) ) {
return new \WP_Error( 'request_failed', $response->get_error_message(), [ 'status' => 500 ] );
}
$response = json_decode( $response["body"] );
if ( $response && $response->token ) {
( new JobTokens )->insert( [
'token' => $response->token,
'task_id' => $response->task_id,
'user_id' => get_current_user_id(),
'command' => $command,
'created_at' => current_time( 'mysql' ),
] );
return [ "status" => "queued", "token" => $response->token ];
}
return new \WP_Error( 'request_failed', 'No token returned from CLI server.', [ 'status' => 500 ] );
}
public static function task( $command = "" ) {
// Disable https when debug enabled
if ( defined( 'CAPTAINCORE_DEBUG' ) ) {
add_filter( 'https_ssl_verify', '__return_false' );
}
$data = [
'timeout' => 45,
'headers' => [
'Content-Type' => 'application/json; charset=utf-8',
'token' => captaincore_get_cli_token()
],
'body' => json_encode( [ "command" => $command ]),
'method' => 'POST',
'data_format' => 'body'
];
// Add command to dispatch server
$response = wp_remote_post( CAPTAINCORE_CLI_ADDRESS . "/tasks", $data );
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
return "Something went wrong: $error_message";
}
$raw_body = $response["body"];
$response = json_decode( $raw_body );
// Response with task id
if ( $response && isset( $response->token ) ) {
( new JobTokens )->insert( [
'token' => $response->token,
'task_id' => $response->task_id,
'user_id' => get_current_user_id(),
'command' => $command,
'created_at' => current_time( 'mysql' ),
] );
return $response->token;
}
return $raw_body;
}
}