<?php
/**
 * Description: Get names/content of soundFontsGS, soundFontsXG from server.
 *
 * @package     midi-Synth
 * @category    module
 * @author      Oleg Klenitsky
 * @copyright   2024 Oleg Klenitsky
 * @license     GPL-2.0-or-later
 */

if ( ! defined( "ABSPATH" ) ) {
	exit();
}

/**
 * Get names of soundFontsGS from server.
 *
 * @return [type] [description]
 */
function midiSynth_getSoundFontsGS(){
	WP_Filesystem();
	global $wp_filesystem;

	// security check.
	if(array_key_exists("nonce", $_POST)){
		$_midiSynth_nonce = filter_input( INPUT_POST, "nonce", FILTER_DEFAULT );
		if ( false === wp_verify_nonce( $_midiSynth_nonce, 'midiSynth' ) ) {
			wp_die( 'Security check failed!' );
    }
	}else{
		wp_die( "midiSynth: nonce missing." );
	}
	if( $_midiSynth_nonce !== wp_create_nonce( "midiSynth" ) ){
		wp_die( "midiSynth: nonce is incorrect." );
	}

	$dirGS = plugin_dir_path( __FILE__ ) . "soundFonts/GS";
	$filelist = json_encode($wp_filesystem->dirlist($dirGS));

	echo esc_html($filelist);

	wp_die();
}

/**
 * Get names of soundFontsXG from server.
 *
 * @return [type] [description]
 */
function midiSynth_getSoundFontsXG(){
	WP_Filesystem();
	global $wp_filesystem;

	// security check.
	if(array_key_exists("nonce", $_POST)){
		$_midiSynth_nonce = filter_input( INPUT_POST, "nonce", FILTER_DEFAULT );
		if ( false === wp_verify_nonce( $_midiSynth_nonce, 'midiSynth' ) ) {
			wp_die( 'Security check failed!' );
    }
	}else{
		wp_die( "midiSynth: nonce missing." );
	}
	if( $_midiSynth_nonce !== wp_create_nonce( "midiSynth" ) ){
		wp_die( "midiSynth: nonce is incorrect." );
	}

	$dirXG = plugin_dir_path( __FILE__ ) . "soundFonts/XG";
	$filelist = json_encode($wp_filesystem->dirlist($dirXG));

	echo esc_html($filelist);

	wp_die();
}
/**
 * Get SoundFonts Content
 *
 * @return [type] [description]
 */
function midiSynth_getSoundFontsContent(){
	WP_Filesystem();
	global $wp_filesystem;

	// security check.
	if(array_key_exists("nonce", $_POST)){
		$_midiSynth_nonce = filter_input( INPUT_POST, "nonce", FILTER_DEFAULT );
		if ( false === wp_verify_nonce( $_midiSynth_nonce, 'midiSynth' ) ) {
			wp_die( 'Security check failed!' );
    }
	}else{
		wp_die( "midiSynth: nonce missing." );
	}
	if( $_midiSynth_nonce !== wp_create_nonce( "midiSynth" ) ){
		wp_die( "midiSynth: nonce is incorrect." );
	}

	$_standard = "";
	if(array_key_exists("standard", $_POST)){
		$_standard = filter_input( INPUT_POST, "standard", FILTER_DEFAULT );
	}

	$_fileName = "";
	if(array_key_exists("fileName", $_POST)){
		$_fileName = filter_input(INPUT_POST, "fileName", FILTER_CALLBACK, array("options"=>"midiSynth_SoundFontsFileName"));
	}

	$dirSoundFonts = "";
	$file_contents = "";

	if("" !== $_fileName && "" !== $_standard){
		if("GS" == $_standard){
			$dirSoundFonts = plugin_dir_path( __FILE__ ) . "soundFonts/GS/";
		}
		if("XG" == $_standard){
			$dirSoundFonts = plugin_dir_path( __FILE__ ) . "soundFonts/XG/";
		}
		if ( !file_exists( $dirSoundFonts.$_fileName ) ) {
			wp_die(esc_html( "not found:" . $dirSoundFonts . $_fileName ));
		}else{
			try {
	    	$file_contents = $wp_filesystem->get_contents( $dirSoundFonts . $_fileName);

	    	// Check: "RIFF" (0-3) и "sfbk" (8-11)
		    if (substr($file_contents, 0, 4) === 'RIFF' && substr($file_contents, 8, 4) === 'sfbk') {
		        header('Content-Type: application/x-soundfont');
			    	header('Accept-Ranges: bytes');

			    	echo $file_contents;
		    }else{
		    	wp_die("This file is not SoundFonts.");
		    }

			}catch(Exception $e){
				unset($file_contents);
				wp_die(esc_html( $e->getMessage() ));
			}
		}
	}

	wp_die();
}
/**
* Validate SoundFonts fileName.
*
* @return string.
*/
function midiSynth_SoundFontsFileName($fileName){
	$fileName = sanitize_file_name($fileName);
	$extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));

	if ($extension === "sf2"){
		return $fileName;
	}else{
		wp_die (esc_html("file extension is incorrect: ".$extension));
	}
	return $fileName;
}
