| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') or die('Jog on!'); |
| 4 |
|
| 5 |
/** |
| 6 |
* Premium license? |
| 7 |
* |
| 8 |
* @return bool |
| 9 |
*/ |
| 10 |
function sh_cd_license_is_premium() { |
| 11 |
return (bool) get_option( 'sh-cd-license-valid', false ); |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Return a link to the upgrade page |
| 16 |
* |
| 17 |
* @return string |
| 18 |
*/ |
| 19 |
function sh_cd_license_upgrade_link() { |
| 20 |
|
| 21 |
$link = admin_url('admin.php?page=sh-cd-shortcode-variables-license'); |
| 22 |
|
| 23 |
return esc_url( $link ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Check an existing license's hash is still valid |
| 28 |
**/ |
| 29 |
function sh_cd_license_validate( $license ) { |
| 30 |
|
| 31 |
if ( true === empty( $license ) ) { |
| 32 |
return __( 'License missing', SH_CD_SLUG ); |
| 33 |
} |
| 34 |
|
| 35 |
// Decode license |
| 36 |
$license = sh_cd_license_decode( $license ); |
| 37 |
|
| 38 |
if ( true === empty( $license ) ) { |
| 39 |
return 'Could not decode / verify license'; |
| 40 |
} |
| 41 |
|
| 42 |
// Does site hash in license meet this site's actual hash? |
| 43 |
if ( true === empty( $license['site-hash'] ) ) { |
| 44 |
return 'Invalid license hash'; |
| 45 |
} |
| 46 |
|
| 47 |
// Match this site hash? |
| 48 |
if ( sh_cd_generate_site_hash() !== $license['site-hash']) { |
| 49 |
return 'This license doesn\'t appear to be for this site (no match on site hash).'; |
| 50 |
} |
| 51 |
|
| 52 |
// Valid date? |
| 53 |
$today_time = strtotime( date( 'Y-m-d' ) ); |
| 54 |
$expire_time = strtotime( $license['expiry-date'] ); |
| 55 |
|
| 56 |
if ( $expire_time < $today_time ) { |
| 57 |
return 'This license has expired.'; |
| 58 |
} |
| 59 |
|
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Validate and decode a license |
| 65 |
**/ |
| 66 |
function sh_cd_license_decode( $license ) { |
| 67 |
|
| 68 |
if( true === empty( $license ) ) { |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
|
| 72 |
// Base64 and JSON decode |
| 73 |
$license = base64_decode( $license ); |
| 74 |
|
| 75 |
if( false === $license ) { |
| 76 |
return NULL; |
| 77 |
} |
| 78 |
|
| 79 |
$license = json_decode( $license, true ); |
| 80 |
|
| 81 |
if( true === empty( $license ) ) { |
| 82 |
return NULL; |
| 83 |
} |
| 84 |
|
| 85 |
// Validate hash! |
| 86 |
$verify_hash = md5( 'yeken.uk' . $license['type'] . $license['expiry-days'] . $license['site-hash'] . $license['expiry-date'] ); |
| 87 |
|
| 88 |
return ( $license['hash'] == $verify_hash && false === empty( $license ) ) ? $license : NULL; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
/** |
| 93 |
* Validate and apply a license |
| 94 |
**/ |
| 95 |
function sh_cd_license_apply( $license ) { |
| 96 |
|
| 97 |
// Validate license |
| 98 |
$license_result = sh_cd_license_validate($license); |
| 99 |
|
| 100 |
if( true === $license_result ) { |
| 101 |
|
| 102 |
update_option( 'sh-cd-license', $license ); |
| 103 |
update_option( 'sh-cd-license-valid', true ); |
| 104 |
|
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Remove a license |
| 113 |
**/ |
| 114 |
function sh_cd_license_remove() { |
| 115 |
|
| 116 |
delete_option( 'sh-cd-license' ); |
| 117 |
delete_option( 'sh-cd-license-valid' ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Generate a site hash to identify this site. |
| 122 |
**/ |
| 123 |
function sh_cd_generate_site_hash() { |
| 124 |
|
| 125 |
$site_hash = get_option( 'sh-cd-hash' ); |
| 126 |
|
| 127 |
// Generate a basic site key from URL and plugin slug |
| 128 |
if( false == $site_hash ) { |
| 129 |
|
| 130 |
$site_hash = md5( 'yeken-sh-cd-' . site_url() ); |
| 131 |
$site_hash = substr( $site_hash, 0, 6 ); |
| 132 |
|
| 133 |
update_option( 'sh-cd-hash', $site_hash ); |
| 134 |
|
| 135 |
} |
| 136 |
return $site_hash; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Fetch license |
| 141 |
* |
| 142 |
* @return mixed |
| 143 |
*/ |
| 144 |
function sh_cd_license() { |
| 145 |
return get_option( 'sh-cd-license', '' ); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
// function t() { |
| 150 |
// |
| 151 |
// if ( true === is_admin() ) { |
| 152 |
// return; |
| 153 |
// } |
| 154 |
// |
| 155 |
// |
| 156 |
// $t = sh_cd_generate_site_hash(); |
| 157 |
// |
| 158 |
// var_dump( $t ); |
| 159 |
// |
| 160 |
// $t = sh_cd_license_apply('eyJ0eXBlIjoicHJvIiwiZXhwaXJ5LWRheXMiOjM2Niwic2l0ZS1oYXNoIjoiNjhiYjJiIiwiZXhwaXJ5LWRhdGUiOiIyMDIwLTAyLTE5IiwiaGFzaCI6Ijc0NTdhNjg3NjZlYzI3NjZjMmRmOWVjZTk4YjlkNTA5In0'); |
| 161 |
// |
| 162 |
// $t = sh_cd_license_is_premium(); |
| 163 |
// |
| 164 |
// var_dump( $t ); |
| 165 |
// |
| 166 |
// die; |
| 167 |
// |
| 168 |
// } |
| 169 |
// add_action('init', 't'); |