All checks were successful
Build & Release Plugin / release (push) Successful in 20s
182 lines
5.3 KiB
PHP
182 lines
5.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Siti License Validator
|
|
*
|
|
* Validates licenses against the Siti Plugin Repo API at plugins.robert.ooo
|
|
*/
|
|
class SitiLicenseValidator {
|
|
|
|
private $api_base_url = 'https://plugins.robert.ooo';
|
|
private $license_key;
|
|
private $hostname;
|
|
private $plugin_version;
|
|
|
|
public function __construct( $license_key = null, $hostname = null, $plugin_version = null ) {
|
|
$this->license_key = $license_key ?: get_option( 'siti_license_key' );
|
|
$this->hostname = $hostname ?: parse_url( home_url(), PHP_URL_HOST );
|
|
$this->plugin_version = $plugin_version;
|
|
}
|
|
|
|
/**
|
|
* Set the license key
|
|
*/
|
|
public function set_license_key( $key ) {
|
|
$this->license_key = $key;
|
|
}
|
|
|
|
/**
|
|
* Set the API base URL
|
|
*/
|
|
public function set_api_base_url( $url ) {
|
|
$this->api_base_url = rtrim( $url, '/' );
|
|
}
|
|
|
|
/**
|
|
* Set the hostname
|
|
*/
|
|
public function set_hostname( $hostname ) {
|
|
$this->hostname = $hostname;
|
|
}
|
|
|
|
/**
|
|
* Set the plugin version for reporting purposes
|
|
*/
|
|
public function set_plugin_version( $version ) {
|
|
$this->plugin_version = $version;
|
|
}
|
|
|
|
/**
|
|
* Verify the license
|
|
*
|
|
* @return array|WP_Error License data on success, WP_Error on failure
|
|
*/
|
|
public function verify() {
|
|
if ( empty( $this->license_key ) ) {
|
|
return new WP_Error( 'missing_license', 'Geen licentiecode ingesteld.' );
|
|
}
|
|
|
|
if ( empty( $this->hostname ) ) {
|
|
return new WP_Error( 'missing_hostname', 'Kon hostname niet bepalen.' );
|
|
}
|
|
|
|
$response = wp_remote_post( $this->api_base_url . '/api/licenses/verify', array(
|
|
'headers' => array(
|
|
'Content-Type' => 'application/json',
|
|
),
|
|
'body' => wp_json_encode( array(
|
|
'key' => $this->license_key,
|
|
'hostname' => $this->hostname,
|
|
'currentVersion' => $this->plugin_version,
|
|
) ),
|
|
'timeout' => 15,
|
|
) );
|
|
|
|
if ( is_wp_error( $response ) ) {
|
|
return $response;
|
|
}
|
|
|
|
$code = wp_remote_retrieve_response_code( $response );
|
|
$body = json_decode( wp_remote_retrieve_body( $response ), true );
|
|
|
|
if ( ! is_array( $body ) ) {
|
|
return new WP_Error( 'verification_failed', 'Onverwacht antwoord van licentieserver.' );
|
|
}
|
|
|
|
$has_payload = isset( $body['license'] ) || isset( $body['pluginVersion'] ) || isset( $body['version'] );
|
|
|
|
if ( 200 !== $code ) {
|
|
if ( $has_payload ) {
|
|
$this->store_license_payload( $body );
|
|
}
|
|
|
|
$error_message = isset( $body['error'] ) ? $body['error'] : 'Licentie verificatie mislukt.';
|
|
return new WP_Error( 'verification_failed', $error_message );
|
|
}
|
|
|
|
if ( empty( $body['valid'] ) ) {
|
|
if ( $has_payload ) {
|
|
$this->store_license_payload( $body );
|
|
}
|
|
|
|
return new WP_Error( 'invalid_license', 'Licentie is ongeldig.' );
|
|
}
|
|
|
|
$this->store_license_payload( $body );
|
|
|
|
return $body;
|
|
}
|
|
|
|
/**
|
|
* Get stored license data
|
|
*/
|
|
public function get_license_data() {
|
|
return get_option( 'siti_license_data', array() );
|
|
}
|
|
|
|
/**
|
|
* Check if license is valid (cached)
|
|
*/
|
|
public function is_valid() {
|
|
$data = $this->get_license_data();
|
|
return ! empty( $data ) && isset( $data['key'] );
|
|
}
|
|
|
|
/**
|
|
* Get the latest version from license data
|
|
*/
|
|
public function get_latest_version() {
|
|
$data = $this->get_license_data();
|
|
return $data['pluginVersion'] ?? null;
|
|
}
|
|
|
|
/**
|
|
* Download a specific version
|
|
*
|
|
* @param string $version Version to download (e.g., 'v1.2.3' or 'latest')
|
|
* @return string|WP_Error Download URL or error
|
|
*/
|
|
public function get_download_url( $version = 'latest' ) {
|
|
if ( empty( $this->license_key ) || empty( $this->hostname ) ) {
|
|
return new WP_Error( 'missing_credentials', 'Licentie of hostname ontbreekt.' );
|
|
}
|
|
|
|
// For now, return the API download endpoint
|
|
// In practice, you might want to proxy this through WordPress
|
|
return $this->api_base_url . '/api/licenses/download?key=' . urlencode( $this->license_key ) . '&hostname=' . urlencode( $this->hostname ) . '&version=' . urlencode( $version );
|
|
}
|
|
|
|
/**
|
|
* Persist payload data locally for UI purposes
|
|
*/
|
|
private function store_license_payload( $body ) {
|
|
$license_data = array();
|
|
|
|
if ( isset( $body['license'] ) && is_array( $body['license'] ) ) {
|
|
$license_data = $body['license'];
|
|
}
|
|
|
|
if ( empty( $license_data['key'] ) ) {
|
|
$license_data['key'] = $this->license_key;
|
|
}
|
|
|
|
$version = null;
|
|
if ( ! empty( $body['pluginVersion'] ) ) {
|
|
$version = $body['pluginVersion'];
|
|
} elseif ( ! empty( $body['version'] ) ) {
|
|
$version = $body['version'];
|
|
}
|
|
|
|
if ( $version ) {
|
|
$license_data['pluginVersion'] = $version;
|
|
}
|
|
|
|
if ( empty( $license_data ) ) {
|
|
return;
|
|
}
|
|
|
|
update_option( 'siti_license_data', $license_data );
|
|
update_option( 'siti_license_last_check', current_time( 'mysql' ) );
|
|
}
|
|
}
|