<?php

class Af2AjaxLizenz {

    function __construct() {}

    public function af2_do_license_check() {
        update_option('af2_license_key', $_POST['key']);
        
        $urlparts = parse_url(home_url());
        $domain = $urlparts['host'];
        $password = '';
        $key = get_option('af2_license_key');


        $url = 'https://licenseserver-anfrageformular.com/key_validation/plugin_validation/plu_interface.php?passkey=1-dj!Activate281ThedaieksMenu192!E&event=checkthedomainevent&password=' . $password . '&key=' . $key . '&domain=' . $domain;
        $args = array(
            'headers' => array(
                'Accept: text/plain',
                'Content-Type: text/plain'
            ),
            'timeout' => 30,
        );

        $response = wp_remote_get($url, $args);
        $resp = wp_remote_retrieve_body($response);

        if (strpos($resp, 'ADK2918dSS') !== false) {
            $resp = str_replace('ADK2918dSS', '', $resp);
        }

        update_option('af2_license_stat', $resp);
        update_option('af2_license_last_updated', create_timestamp());

        echo __($resp, 'af2_multilanguage');
        wp_die();
    }

    public function af2_delete_app() {
        update_option('af2_registered_app_code', '');
        update_option('af2_registered_app_device', '');
        update_option('af2_registered_app_fcm', '');

        if(get_option('af2_license_stat') == 'Die Domain ist aktiviert!') {
            $randomNum = substr(str_shuffle("012345678901234567890123456789012345678901234567890123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmopqrstuvwxyzsdjafkleiwopvnjkdvncyfeiawojkldsmvcxy"), 0, 32);
            update_option('app_code', $randomNum);

            $valcode = get_option('app_code');
            $prefix = str_replace("wp-admin/", "", get_admin_url());
            $qrlink = $prefix.'wp-json/af2/app/v2/register_qr?val_code='.$valcode;
    
            update_option('app_qr_code', 'https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl='.$qrlink.'&choe=UTF-8');
        }
        else {
            update_option('app_code', '');
            update_option('app_qr_code', '');
        }
    }
}
