Advertisement
Guest User

Link Whisper

a guest
Mar 9th, 2022
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.10 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Work with licenses
  5. */
  6. class Wpil_License
  7. {
  8.     /**
  9.      * Register services
  10.      */
  11.     public function register()
  12.     {
  13.         add_action('wp_ajax_wpil_license_activate', array(__CLASS__, 'ajax_wpil_license_activate'));
  14.     }
  15.  
  16.     public static function init()
  17.     {
  18.         if (!empty($_GET['wpil_deactivate']))
  19.         {
  20.             update_option(WPIL_OPTION_LICENSE_STATUS, 'invalid');
  21.             update_option(WPIL_OPTION_LICENSE_LAST_ERROR, $message='Deactivated manually');
  22.         }
  23.  
  24.         include WP_INTERNAL_LINKING_PLUGIN_DIR . '/templates/wpil_license.php';
  25.     }
  26.  
  27.     /**
  28.      * Check if license is valid
  29.      *
  30.      * @return bool
  31.      */
  32. public static function isValid()
  33. {
  34. if (get_option('wpil_2_license_status') == 'valid') {
  35. $prev = get_option('wpil_2_license_check_time');
  36. $delta = $prev ? time() - strtotime($prev) : 0;
  37.  
  38. if (!$prev || $delta > (60*60*24*3) || !empty($_GET['wpil_check_license'])) {
  39. $license = self::getKey();
  40. self::check($license, $silent = true);
  41. }
  42.  
  43. $status = get_option('wpil_2_license_status');
  44.  
  45. if ($status !== false && $status == 'valid') {
  46. return true;
  47. }
  48. }
  49.  
  50. return true;
  51. }
  52.  
  53.     /**
  54.      * Get license key
  55.      *
  56.      * @param bool $key
  57.      * @return bool|mixed|void
  58.      */
  59.     public static function getKey($key = false)
  60.     {
  61.         if (empty($key)) {
  62.             $key = get_option('wpil_2_license_key');
  63.         }
  64.  
  65.         if (stristr($key, '-')) {
  66.             $ks = explode('-', $key);
  67.             $key = $ks[1];
  68.         }
  69.  
  70.         return $key;
  71.     }
  72.  
  73.     /**
  74.      * Check new license
  75.      *
  76.      * @param $license_key
  77.      * @param bool $silent
  78.      */
  79.     public static function check($license_key, $silent = true)
  80.     {
  81.         $base_url_path = 'admin.php?page=link_whisper_license';
  82.         $item_id = self::getItemId($license_key);
  83.         $license = Wpil_License::getKey($license_key);
  84.         $code = null;
  85.  
  86.         if (function_exists('curl_version')) {
  87.             //CURL is enabled
  88.             $ch = curl_init();
  89.             curl_setopt($ch, CURLOPT_HEADER, 0);
  90.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  91.             curl_setopt($ch, CURLOPT_URL, WPIL_STORE_URL);
  92.             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  93.             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  94.             curl_setopt($ch, CURLOPT_POST, 1);
  95.             curl_setopt($ch, CURLOPT_POSTFIELDS,
  96.                 "edd_action=activate_license&license={$license}&item_id={$item_id}&url=".urlencode(home_url()));
  97.             $data = curl_exec($ch);
  98.             $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  99.  
  100.             // if the curl request failed, try file get contents
  101.             if(empty($code)){
  102.                 $params = [
  103.                     'edd_action' => 'activate_license',
  104.                     'license' => $license,
  105.                     'item_id' => $item_id,
  106.                     'url' => urlencode(home_url()),
  107.                 ];
  108.                 $data = file_get_contents(WPIL_STORE_URL . '/?' . http_build_query($params));
  109.                 if (!empty($data)) {
  110.                     $code = 200;
  111.                 }
  112.             }
  113.  
  114.         } else {
  115.             //CURL is disabled
  116.             $params = [
  117.                 'edd_action' => 'activate_license',
  118.                 'license' => $license,
  119.                 'item_id' => $item_id,
  120.                 'url' => urlencode(home_url()),
  121.             ];
  122.             $data = file_get_contents(WPIL_STORE_URL . '/?' . http_build_query($params));
  123.             if (!empty($data)) {
  124.                 $code = 200;
  125.             }
  126.         }
  127.  
  128.         update_option(WPIL_OPTION_LICENSE_CHECK_TIME, date('c'));
  129.  
  130.         if (empty($data) || $code !== 200) {
  131.             $error_message = !empty($ch) ? curl_error($ch) : '';
  132.  
  133.             if ($error_message) {
  134.                 $message = $error_message;
  135.             } else {
  136.                 $message = "$code response code on activation, please try again or check code";
  137.             }
  138.         } else {
  139.             $license_data = json_decode($data);
  140.  
  141.             if ($license_data->success === false) {
  142.                 $message = self::getMessage($license, $license_data);
  143.             } else {
  144.                 update_option(WPIL_OPTION_LICENSE_STATUS, $license_data->license);
  145.                 update_option(WPIL_OPTION_LICENSE_KEY, $license);
  146.                 update_option(WPIL_OPTION_LICENSE_DATA, var_export($license_data, true));
  147.  
  148.                 if (!$silent) {
  149.                     $base_url = admin_url('admin.php?page=link_whisper_settings&licensing');
  150.                     $message = __("License key `%s` was activated", 'wpil');
  151.                     $message = sprintf($message, $license);
  152.                     $redirect = add_query_arg(array('sl_activation' => 'true', 'message' => urlencode($message)), $base_url);
  153.                     wp_redirect($redirect);
  154.                     exit;
  155.                 } else {
  156.                     return;
  157.                 }
  158.             }
  159.         }
  160.  
  161.         if (!empty($ch)) {
  162.             curl_close($ch);
  163.         }
  164.  
  165.         update_option(WPIL_OPTION_LICENSE_STATUS, 'invalid');
  166.         update_option(WPIL_OPTION_LICENSE_LAST_ERROR, $message);
  167.  
  168.         if (!$silent) {
  169.             $base_url = admin_url($base_url_path);
  170.             $redirect = add_query_arg(array('sl_activation' => 'false', 'msg' => urlencode($message)), $base_url);
  171.             wp_redirect($redirect);
  172.             exit;
  173.         }
  174.     }
  175.  
  176.     /**
  177.      * Check if a given site is licensed in the same plan as this site.
  178.      *
  179.      * @param string $site_url The url of the site we want to check.
  180.      * @return bool
  181.      */
  182.     public static function check_site_license($site_url = '')
  183.     {
  184.         if(empty($site_url)){
  185.             return false;
  186.         }
  187.  
  188.         // if the site has been recently checked and does have a valid license
  189.         if(self::check_cached_site_licenses($site_url)){
  190.             // return true
  191.             return true;
  192.         }
  193.  
  194.         $license_key = self::getKey();
  195.         $item_id = self::getItemId($license_key);
  196.         $license = Wpil_License::getKey($license_key);
  197.         $code = null;
  198.  
  199.         if (function_exists('curl_version')) {
  200.             //CURL is enabled
  201.             $ch = curl_init();
  202.             curl_setopt($ch, CURLOPT_HEADER, 0);
  203.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  204.             curl_setopt($ch, CURLOPT_URL, WPIL_STORE_URL);
  205.             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  206.             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  207.             curl_setopt($ch, CURLOPT_POST, 1);
  208.             curl_setopt($ch, CURLOPT_POSTFIELDS,
  209.                 "edd_action=check_license&license={$license}&item_id={$item_id}&url=".urlencode($site_url));
  210.             $data = curl_exec($ch);
  211.             $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  212.         } else {
  213.             //CURL is disabled
  214.             $params = [
  215.                 'edd_action' => 'check_license',
  216.                 'license' => $license,
  217.                 'item_id' => $item_id,
  218.                 'url' => urlencode($site_url),
  219.             ];
  220.             $data = file_get_contents(WPIL_STORE_URL . '/?' . http_build_query($params));
  221.             if (!empty($data)) {
  222.                 $code = 200;
  223.             }
  224.         }
  225.  
  226.         if (!empty($ch)) {
  227.             curl_close($ch);
  228.         }
  229.  
  230.         if (empty($data) || $code !== 200) {
  231.             return false;
  232.         } else {
  233.             $license_data = json_decode($data);
  234.  
  235.             if(isset($license_data->license) && 'valid' === $license_data->license){
  236.                 self::update_cached_site_list($site_url);
  237.                 return true;
  238.             }
  239.         }
  240.  
  241.         return false;
  242.     }
  243.  
  244.     /**
  245.      * Checks a site url against the cached list of known licensed urls.
  246.      * Returns if the site is licensed and has been checked recently
  247.      *
  248.      * @param string $site_url
  249.      * @return bool
  250.      **/
  251.     public static function check_cached_site_licenses($site_url = ''){
  252.         $site_urls = get_option('wpil_cached_valid_sites', array());
  253.  
  254.         if(empty($site_urls) || empty($site_url)){
  255.             return false;
  256.         }
  257.  
  258.         $time = time();
  259.         foreach($site_urls as $url_data){
  260.             if($site_url === $url_data['site_url'] && $time < $url_data['expiration']){
  261.                 return true;
  262.             }
  263.         }
  264.  
  265.         return false;
  266.     }
  267.  
  268.     /**
  269.      * Updates the cached site list with news of licensed sites.
  270.      *
  271.      **/
  272.     public static function update_cached_site_list($site_url = ''){
  273.         if(empty($site_url)){
  274.             return false;
  275.         }
  276.  
  277.         $site_cache = get_option('wpil_cached_valid_sites', array());
  278.  
  279.         foreach($site_cache as $key => $site_data){
  280.             if($site_data['site_url'] === $site_url){
  281.                 unset($site_cache[$key]);
  282.             }
  283.         }
  284.  
  285.         $site_cache[] = array('site_url' => $site_url, 'expiration' => (time() + (60*60*24*3)) );
  286.  
  287.         update_option('wpil_cached_valid_sites', $site_cache);
  288.     }
  289.  
  290.     /**
  291.      * Get current license ID
  292.      *
  293.      * @param string $license_key
  294.      * @return false|string
  295.      */
  296.     public static function getItemId($license_key = '')
  297.     {
  298.         if ($license_key && stristr($license_key, '-')) {
  299.             $ks = explode('-', $license_key);
  300.             return $ks[0];
  301.         }
  302.  
  303.         $item_id = file_get_contents(dirname(__DIR__) . '/../store-item-id.txt');
  304.  
  305.         return $item_id;
  306.     }
  307.  
  308.     /**
  309.      * Get license message
  310.      *
  311.      * @param $license
  312.      * @param $license_data
  313.      * @return string
  314.      */
  315.     public static function getMessage($license, $license_data)
  316.     {
  317.         switch ($license_data->error) {
  318.             case 'expired' :
  319.                 $d = date_i18n(get_option('date_format'), strtotime($license_data->expires, current_time('timestamp')));
  320.                 $message = sprintf('Your license key %s expired on %s. Please renew your subscription to continue using Link Whisper.', $license, $d);
  321.                 break;
  322.  
  323.             case 'revoked' :
  324.                 $message = 'Your License Key `%s` has been disabled';
  325.                 break;
  326.  
  327.             case 'missing' :
  328.                 $message = 'Missing License `%s`';
  329.                 break;
  330.  
  331.             case 'invalid' :
  332.             case 'site_inactive' :
  333.                 $message = 'The License Key `%s` is not active for this URL.';
  334.                 break;
  335.  
  336.             case 'item_name_mismatch' :
  337.                 $message = 'It appears this License Key (%s) is used for a different product. Please log into your linkwhisper.com user account to find your Link Whisper License Key.';
  338.                 break;
  339.  
  340.             case 'no_activations_left':
  341.                 $message = 'The License Key `%s` has reached its activation limit. Please upgrade your subscription to add more sites.';
  342.                 break;
  343.  
  344.             case 'invalid_item_id':
  345.                 $message = "The License Key `%s` doesn't go to any known products. Fairly often this is caused by a mistake in entering the License Key or after upgrading your Link Whisper subscription. If you've just upgraded your subscription, please delete Link Whisper from your site and download a fresh copy from linkwhisper.com.";
  346.                 break;
  347.    
  348.             default :
  349.                 $message = "Error on activation: " . $license_data->error;
  350.                 break;
  351.         }
  352.  
  353.         if (stristr($message, '%s')) {
  354.             $message = sprintf($message, $license);
  355.         }
  356.  
  357.         return $message;
  358.     }
  359.  
  360.     /**
  361.      * Activate license
  362.      */
  363.     public static function activate()
  364.     {
  365.         if (!isset($_POST['hidden_action']) || $_POST['hidden_action'] != 'activate_license' || !check_admin_referer('wpil_activate_license_nonce', 'wpil_activate_license_nonce')) {
  366.             return;
  367.         }
  368.  
  369.         $license = sanitize_text_field(trim($_POST['wpil_license_key']));
  370.  
  371.         self::check($license, $silent = false);
  372.     }
  373.  
  374.     /**
  375.      * Activate license via ajax call
  376.      **/
  377.     public static function ajax_wpil_license_activate(){
  378.        
  379.     }
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement