Skip to content

Commit

Permalink
fix: add licensing bypass changes on localhost and update callback URL
Browse files Browse the repository at this point in the history
  • Loading branch information
pranvinit committed Nov 6, 2024
1 parent f35e064 commit 64d82ae
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 66 deletions.
118 changes: 65 additions & 53 deletions admin/rt-transcoder-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RT_Transcoder_Handler {
* @access protected
* @var string $transcoding_api_url The URL of the api.
*/
protected $transcoding_api_url = 'https://api.rtmedia.io/api/v1/';
protected $transcoding_api_url = 'http://frappe-transcoder-api.rt.gw/api/';

/**
* The URL of the EDD store.
Expand All @@ -38,7 +38,7 @@ class RT_Transcoder_Handler {
* @access protected
* @var string $store_url The URL of the transcoder api.
*/
protected $store_url = 'https://rtmedia.io/';
protected $store_url = 'http://frappe-transcoder-api.rt.gw/api/';

/**
* Contain uploaded media information.
Expand Down Expand Up @@ -147,24 +147,24 @@ public function __construct( $no_init = false ) {
$usage_info = get_site_option( 'rt-transcoding-usage' );

if ( isset( $usage_info ) && is_array( $usage_info ) && array_key_exists( $this->api_key, $usage_info ) ) {
if ( isset( $usage_info[ $this->api_key ]->plan->expires )
&& strtotime( $usage_info[ $this->api_key ]->plan->expires ) < time() ) {
$usage_info = $this->update_usage( $this->api_key );
}
// if ( isset( $usage_info[ $this->api_key ]->plan->expires )
// && strtotime( $usage_info[ $this->api_key ]->plan->expires ) < time() ) {
// $usage_info = $this->update_usage( $this->api_key );
// }
if ( array_key_exists( $this->api_key, $usage_info ) && is_object( $usage_info[ $this->api_key ] ) && isset( $usage_info[ $this->api_key ]->status ) && $usage_info[ $this->api_key ]->status ) {
if ( isset( $usage_info[ $this->api_key ]->remaining ) && $usage_info[ $this->api_key ]->remaining > 0 ) {

// Enable re-transcoding.
include_once RT_TRANSCODER_PATH . 'admin/rt-retranscode-admin.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant

if ( $usage_info[ $this->api_key ]->remaining < 524288000 && ! get_site_option( 'rt-transcoding-usage-limit-mail' ) ) {
$this->nearing_usage_limit( $usage_info );
} elseif ( $usage_info[ $this->api_key ]->remaining > 524288000 && get_site_option( 'rt-transcoding-usage-limit-mail' ) ) {
update_site_option( 'rt-transcoding-usage-limit-mail', 0 );
}
if ( strtotime( $usage_info[ $this->api_key ]->plan->expires ) > time() ) {
add_filter( 'wp_generate_attachment_metadata', array( $this, 'wp_media_transcoding' ), 21, 2 );
}
// if ( $usage_info[ $this->api_key ]->remaining < 524288000 && ! get_site_option( 'rt-transcoding-usage-limit-mail' ) ) {
// $this->nearing_usage_limit( $usage_info );
// } elseif ( $usage_info[ $this->api_key ]->remaining > 524288000 && get_site_option( 'rt-transcoding-usage-limit-mail' ) ) {
// update_site_option( 'rt-transcoding-usage-limit-mail', 0 );
// }
// if ( strtotime( $usage_info[ $this->api_key ]->plan->expires ) > time() ) {
// add_filter( 'wp_generate_attachment_metadata', array( $this, 'wp_media_transcoding' ), 21, 2 );
// }

/* Do not let the user to upload non supported media types on localhost */
$blacklist = rtt_get_blacklist_ip_addresses();
Expand All @@ -183,6 +183,10 @@ public function __construct( $no_init = false ) {
add_action( 'wp_ajax_rt_disable_transcoding', array( $this, 'disable_transcoding' ), 1 );
add_action( 'wp_ajax_rt_enable_transcoding', array( $this, 'enable_transcoding' ), 1 );
add_action( 'add_attachment', array( $this, 'after_upload_pdf' ) );
add_filter( 'wp_generate_attachment_metadata', array( $this, 'wp_media_transcoding' ), 21, 2 );
add_filter( 'rtmedia_plupload_files_filter', array( $this, 'allowed_types' ), 10, 1 );
add_filter( 'rtmedia_allowed_types', array( $this, 'allowed_types_admin_settings' ), 10, 1 );
add_filter( 'rtmedia_valid_type_check', array( $this, 'bypass_video_audio' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -256,23 +260,29 @@ public function wp_media_transcoding( $wp_metadata, $attachment_id, $autoformat
}
}

$callback_url = RT_TRANSCODER_CALLBACK_URL;

if ( ! defined( 'RT_TRANSCODER_CALLBACK_URL' ) || empty( RT_TRANSCODER_CALLBACK_URL ) ) {
return;
}

$args = array(
'method' => 'POST',
'sslverify' => false,
'timeout' => 60, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'body' => array(
'api_token' => $this->api_key,
'job_type' => $job_type,
'job_for' => $job_for,
'file_url' => rawurlencode( $url ),
'callback_url' => rawurlencode( trailingslashit( home_url() ) . 'index.php' ),
'force' => 0,
'formats' => ( true === $autoformat ) ? ( ( 'video' === $type_array[0] ) ? 'mp4' : 'mp3' ) : $autoformat,
'thumb_count' => $options_video_thumb,
'api_token' => $this->api_key,
'job_type' => $job_type,
'job_for' => $job_for,
'file_origin' => rawurlencode( $url ),
'callback_url' => rawurlencode( $callback_url ),
'force' => 0,
'formats' => ( true === $autoformat ) ? ( ( 'video' === $type_array[0] ) ? 'mp4' : 'mp3' ) : $autoformat,
'thumbnail_count' => $options_video_thumb,
),
);

$transcoding_url = $this->transcoding_api_url . 'job/';
$transcoding_url = $this->transcoding_api_url . 'resource/Transcoder Job';

$upload_page = wp_remote_post( $transcoding_url, $args );

Expand All @@ -283,8 +293,9 @@ public function wp_media_transcoding( $wp_metadata, $attachment_id, $autoformat
)
) {
$upload_info = json_decode( $upload_page['body'] );
if ( isset( $upload_info->status ) && $upload_info->status && isset( $upload_info->job_id ) && $upload_info->job_id ) {
$job_id = $upload_info->job_id;
error_log( json_encode( $upload_info ) );
if ( isset( $upload_info->data ) && isset( $upload_info->data->name ) ) {
$job_id = $upload_info->data->name;
update_post_meta( $attachment_id, '_rt_transcoding_job_id', $job_id );
}
}
Expand Down Expand Up @@ -350,16 +361,16 @@ public function bypass_video_audio( $flag, $file ) {
* @return boolean $status If true then key is valid else key is not valid.
*/
public function is_valid_key( $key ) {
$validate_url = trailingslashit( $this->store_url ) . 'rt-eddsl-api/?rt-eddsl-license-key=' . $key;
$validate_url = trailingslashit( $this->store_url ) . '/resource/Transcoder License/' . $key;
if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
$validation_page = vip_safe_wp_remote_get( $validate_url, '', 3, 3 );
} else {
$validation_page = wp_safe_remote_get( $validate_url ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
}
if ( ! is_wp_error( $validation_page ) ) {
$validation_info = json_decode( $validation_page['body'] );
if ( isset( $validation_info->status ) ) {
$status = $validation_info->status;
if ( isset( $validation_info->data->status ) && 'Active' === $validation_info->data->status ) {
$status = true;
}
} else {
$status = false;
Expand All @@ -378,14 +389,15 @@ public function is_valid_key( $key ) {
* @return array $usage_info An array containing usage information.
*/
public function update_usage( $key ) {
$usage_url = trailingslashit( $this->transcoding_api_url ) . 'usage/' . $key;
$usage_url = trailingslashit( $this->transcoding_api_url ) . 'resource/Transcoder License/' . $key;
if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
$usage_page = vip_safe_wp_remote_get( $usage_url, '', 3, 3 );
} else {
$usage_page = wp_safe_remote_get( $usage_url ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
}
if ( ! is_wp_error( $usage_page ) ) {
$usage_info = json_decode( $usage_page['body'] );
$usage_info = $usage_info->data;
} else {
$usage_info = null;
}
Expand Down Expand Up @@ -463,7 +475,7 @@ public function usage_quota_over() {
*/
public function save_api_key() {
$is_api_key_updated = transcoder_filter_input( INPUT_GET, 'api-key-updated', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$is_invalid_license_key = transcoder_filter_input( INPUT_GET, 'invalid-license-key', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$is_invalid_license_key = false;
$is_localhost = transcoder_filter_input( INPUT_GET, 'need-public-host', FILTER_SANITIZE_FULL_SPECIAL_CHARS );

if ( $is_api_key_updated ) {
Expand Down Expand Up @@ -514,7 +526,7 @@ public function save_api_key() {
$return_page = add_query_arg(
array(
'page' => 'rt-transcoder',
'api-key-updated' => $usage_info->plan->name ? ucfirst( strtolower( $usage_info->plan->name ) ) : 'Free',
'api-key-updated' => $usage_info->plan ? ucfirst( strtolower( $usage_info->plan ) ) : 'Free',
),
admin_url( 'admin.php' )
);
Expand Down Expand Up @@ -1615,16 +1627,16 @@ public function filter_transcoder_response() {
$post_var = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing

$filter_post_args = array(
'job_id' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'job_type' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'job_for' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'format' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'download_url' => FILTER_SANITIZE_URL,
'file_name' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'thumb_count' => FILTER_SANITIZE_NUMBER_INT,
'status' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'error_msg' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'error_code' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'job_id' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'job_type' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'job_for' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'format' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'download_url' => FILTER_SANITIZE_URL,
'file_name' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'thumbnail_count' => FILTER_SANITIZE_NUMBER_INT,
'status' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'error_msg' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'error_code' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
);

$post_array = filter_input_array( INPUT_POST, $filter_post_args );
Expand Down Expand Up @@ -1654,18 +1666,18 @@ public function filter_transcoder_response() {
*/
public function filter_transcoder_response_json( $request ) {
return array(
'job_id' => sanitize_text_field( wp_unslash( $request->get_param( 'job_id' ) ) ),
'job_type' => sanitize_text_field( wp_unslash( $request->get_param( 'job_type' ) ) ),
'job_for' => sanitize_text_field( wp_unslash( $request->get_param( 'job_for' ) ) ),
'format' => sanitize_text_field( wp_unslash( $request->get_param( 'format' ) ) ),
'download_url' => esc_url_raw( $request->get_param( 'download_url' ) ),
'file_name' => sanitize_text_field( wp_unslash( $request->get_param( 'file_name' ) ) ),
'thumb_count' => absint( $request->get_param( 'thumb_count' ) ),
'status' => sanitize_text_field( wp_unslash( $request->get_param( 'status' ) ) ),
'files' => array_map( 'esc_url_raw', (array) $request->get_param( 'files' ) ),
'file_status' => sanitize_text_field( wp_unslash( $request->get_param( 'file_status' ) ) ),
'thumbnail' => array_map( 'esc_url_raw', (array) $request->get_param( 'thumbnail' ) ),
'error_msg' => sanitize_text_field( wp_unslash( $request->get_param( 'error_msg' ) ) ),
'job_id' => sanitize_text_field( wp_unslash( $request->get_param( 'job_id' ) ) ),
'job_type' => sanitize_text_field( wp_unslash( $request->get_param( 'job_type' ) ) ),
'job_for' => sanitize_text_field( wp_unslash( $request->get_param( 'job_for' ) ) ),
'format' => sanitize_text_field( wp_unslash( $request->get_param( 'format' ) ) ),
'download_url' => esc_url_raw( $request->get_param( 'download_url' ) ),
'file_name' => sanitize_text_field( wp_unslash( $request->get_param( 'file_name' ) ) ),
'thumbnail_count' => absint( $request->get_param( 'thumbnail_count' ) ),
'status' => sanitize_text_field( wp_unslash( $request->get_param( 'status' ) ) ),
'files' => array_map( 'esc_url_raw', (array) $request->get_param( 'files' ) ),
'file_status' => sanitize_text_field( wp_unslash( $request->get_param( 'file_status' ) ) ),
'thumbnail' => array_map( 'esc_url_raw', (array) $request->get_param( 'thumbnail' ) ),
'error_msg' => sanitize_text_field( wp_unslash( $request->get_param( 'error_msg' ) ) ),
);
}
}
69 changes: 56 additions & 13 deletions admin/rt-transcoder-rest-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class Transcoder_Rest_Routes extends WP_REST_Controller {
*/
public function __construct() {
$this->rt_transcoder_handler = new RT_Transcoder_Handler( true );

if ( ! defined( 'RT_TRANSCODER_CALLBACK_URL' ) ) {
define( 'RT_TRANSCODER_CALLBACK_URL', $this->get_callback_url() );
}
}

/**
Expand Down Expand Up @@ -84,28 +88,58 @@ public function register_routes() {
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'file_status' => array(
'job_type' => array(
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'error_msg' => array(
'required' => false,
'job_for' => array(
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'job_for' => array(
'required' => false,
'format' => array(
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'thumbnail' => array(
'required' => false,
'download_url' => array(
'required' => true,
'type' => 'string',
'sanitize_callback' => 'esc_url_raw',
),
'file_name' => array(
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'format' => array(
'required' => false,
'thumbnail_count' => array(
'required' => true,
'type' => 'integer',
'sanitize_callback' => 'absint',
),
'status' => array(
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'files' => array(
'required' => true,
'type' => 'array',
'sanitize_callback' => 'esc_url_raw',
),
'file_status' => array(
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'thumbnail' => array(
'required' => true,
'type' => 'array',
'sanitize_callback' => 'esc_url_raw',
),
'error_msg' => array(
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
Expand All @@ -119,6 +153,15 @@ public function register_routes() {
);
}

/**
* Return the callback URL for the transcoder.
*
* @return string
*/
public function get_callback_url() {
return rest_url( $this->namespace_prefix . $this->version . '/transcoder-callback' );
}

/**
* Return poster url for requested media if exists.
*
Expand Down Expand Up @@ -261,7 +304,7 @@ public function handle_callback( WP_REST_Request $request ) {
}

if ( isset( $format ) && 'thumbnail' === $format ) {
return new WP_REST_Response( $post_array, 200 );
return new WP_REST_Response( esc_html_e( 'Thumbnail created successfully.', 'transcoder' ), 200 );
}

if ( ! empty( $post_array['files'] ) ) {
Expand All @@ -288,7 +331,7 @@ public function handle_callback( WP_REST_Request $request ) {
}
return new WP_Error( 'transcoder_error', $flag, array( 'status' => 500 ) );
} else {
return new WP_REST_Response( $post_array, 200 );
return new WP_REST_Response( esc_html_e( 'Media transcoded successfully.', 'transcoder' ), 200 );
}
}
} else {
Expand Down Expand Up @@ -330,7 +373,7 @@ public function handle_callback( WP_REST_Request $request ) {
}

if ( isset( $format ) && 'thumbnail' === $format ) {
return new WP_REST_Response( $post_array, 200 );
return new WP_REST_Response( esc_html_e( 'Thumbnail created successfully.', 'transcoder' ), 200 );
}

if ( ! empty( $post_array['files'] ) ) {
Expand Down Expand Up @@ -358,7 +401,7 @@ public function handle_callback( WP_REST_Request $request ) {
return new WP_Error( 'transcoder_error', $flag, array( 'status' => 500 ) );

} else {
return new WP_REST_Response( $post_array, 200 );
return new WP_REST_Response( esc_html_e( 'Media transcoded successfully.', 'transcoder' ), 200 );
}
}
}
Expand Down

0 comments on commit 64d82ae

Please sign in to comment.