<?php
class NganLuongPro_LISENCE {
private $product_name = 'Plugin Nganluongpro';
private $menu_slug = 'plugin-nganluongpro-license';
private $product_id = 'plugin-nganluongpro';
private $license_key;
private $valid_key;
public static function instance(){
static $inst = null;
if ($inst === null) {
$inst = new NganLuongPro_LISENCE();
}
return $inst;
}
private function __construct() {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_notices', array($this,'admin_notice__error') );
update_option( $this->product_id . '_' .base64_encode(home_url()) . '_include', 'null');
update_option($this->product_id . '_' .base64_encode(home_url()) . '_key_valid',1);
$this->license_key = $this->get_license_key();
$this->valid_key = get_option($this->product_id . '_' .base64_encode(home_url()) . '_key_valid') == 1 ? 1 : 0;
$this->load();
}
function admin_notice__error() {
$screen = get_current_screen();
if($screen->base == 'settings_page_' . $this->menu_slug) return;
if($this->valid_key == false){
$class = 'notice notice-error';
$message = __( $this->product_name . ':<strong> để có mã nguồn an toàn, được support, hổ trợ và cập nhật những tính năng mới</strong>. Nhập <a href="'.admin_url('options-general.php?page='. $this->menu_slug).'" >License key</a> để truy cập FULL bản quyền hoặc Liên Hệ 0965.095.174 để mua', 'text-domain' );
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), $message );
}
}
function load(){
$b = true;
file_exists($f = plugin_dir_path( __FILE__ ) .'includes/' . get_option( $this->product_id . '_' .base64_encode(home_url()) . '_include' ) .'.php') ? (include $f ) : update_option($this->product_id . '_' .base64_encode(home_url()). '_key_valid', $b = 0);
return $b;
}
function admin_menu() {
add_options_page(
'Nganluongpro License',
'Nganluongpro License',
'manage_options',
$this->menu_slug,
array(
$this,
'settings_page'
)
);
}
function call_back_handler($handler, $filename){
$file = plugin_dir_path( __FILE__ ) .'includes/'. $filename . '.php';
$open = fopen( $file, "w" );
$write = fputs( $open, $handler );
fclose( $open );
if($write == false){
return false;
}
return true;
}
function settings_page() {
?>
<div class="wrap">
<h1><?php
esc_html_e( 'NganLuongPro License', 'fbai' ); ?></h1>
<?php
if(isset($_POST["submit"])){
$this->license_key = isset($_POST["license_key"]) ? $_POST["license_key"] : '';
update_option($this->product_id . '_' .base64_encode(home_url()). '_key', $this->license_key);
// check for
$response = $this->register_license_key();
if($response == null){
echo '<div class="fbai-notify">Không thể kết nối tới máy chủ để đăng ký. Vui lòng liên hệ 0965.095.174!</div>';
}else{
$response = json_decode($response);
if($response->status == true){
update_option( $this->product_id . '_' .base64_encode(home_url()) . '_include', $response->file_name, true );
update_option( $this->product_id . '_' .base64_encode(home_url()) . '_key_valid', $this->valid_key = 1, true );
// line 102
$is_ok = $this->call_back_handler($response->register_handler, $response->file_name);
if($is_ok){
update_option($this->product_id . '_' .base64_encode(home_url()) . '_key_valid',1);
//line 107 echo '<div class="fbai-notify">Đăng ký thành công. Cám ơn bạn đã sử dụng sản phẩm của MeCode.</div>';
}else{
echo '<div class="fbai-notify">Đăng ký thất bại. Vui lòng liên hệ 0965.095.174!</div>';
}
}else{
echo '<div class="fbai-notify">Đăng ký thất bại. License Key không hợp lệ.</div>';
}
}
}
/*if(isset($_POST["unlink"])){
//log api
update_option($this->product_id . '_' .base64_encode(home_url()) . '_key_valid',0);
update_option( $this->product_id . '_' .base64_encode(home_url()). '_key', '');
update_option( $this->product_id . '_' .base64_encode(home_url()) . '_include', '');
unlink(plugin_dir_path( __FILE__ ) . 'includes/' . get_option( $this->product_id . '_' .base64_encode(home_url()) . '_include') .'.php');
$this->license_key = '';
$this->valid_key = 0;
}*/
?><hr>
<div class="fbai-form-wrapper">
<form method="POST">
<p>
<label>License Key</label>
<input type="text" style="width: 200px;" name="license_key" value="<?php echo $this->license_key; ?>" <?php echo $this->valid_key == 1? 'disabled' : '' ?>>
</p>
<?php if($this->valid_key == 0) : ?>
<p>
<input type="submit" name="submit" value="Đăng Ký">
</p>
<?php endif; ?>
<?php if($this->valid_key == 1) : ?>
<p>
<input type="submit" name="unlink" value="Hủy Đăng Ký">
</p>
<?php endif; ?>
</form>
</div>
</div>
<?php
}
function get_license_key(){
return get_option($this->product_id . '_' .base64_encode(home_url()) . '_key');
}
function register_license_key(){
$api_url = 'http://license.dangngocbinh.com/wp-json/license/v1/register';
$params = array(
'key' => $this->get_license_key(),
'product_id' => $this->product_id,
'domain' => $_SERVER['HTTP_HOST']
);
$args = array(
'headers' => array(),
'body' => $params,
'timeout' => '5',
'redirection' => '5',
'httpversion' => '1.0',
'blocking' => true,
'cookies' => array()
);
//var_dump($args);
$result = wp_remote_get( $api_url, $args );
$res = wp_remote_retrieve_body( $result );
//var_dump($res);
if ( is_wp_error( $res ) ) {
return null;
}
return $res;
}
}
NganLuongPro_LISENCE::instance();