| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Licenses Controller |
| 9 |
* |
| 10 |
* Property Hive Licenses Class which handles the storing and checking of licenses |
| 11 |
* |
| 12 |
* @class PH_Licenses |
| 13 |
* @version 1.0.0 |
| 14 |
* @package PropertyHive/Classes/Licenses |
| 15 |
* @category Class |
| 16 |
* @author PropertyHive |
| 17 |
*/ |
| 18 |
class PH_Licenses { |
| 19 |
|
| 20 |
/** @var PH_Licenses The single instance of the class */ |
| 21 |
protected static $_instance = null; |
| 22 |
|
| 23 |
/** |
| 24 |
* Main PH_Licenses Instance. |
| 25 |
* |
| 26 |
* Ensures only one instance of PH_Licenses is loaded or can be loaded. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* @static |
| 30 |
* @return PH_Licenses Main instance |
| 31 |
*/ |
| 32 |
public static function instance() { |
| 33 |
if ( is_null( self::$_instance ) ) { |
| 34 |
self::$_instance = new self(); |
| 35 |
} |
| 36 |
return self::$_instance; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Cloning is forbidden. |
| 41 |
* |
| 42 |
* @since 1.0.0 |
| 43 |
*/ |
| 44 |
public function __clone() { |
| 45 |
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'propertyhive' ), '1.0.0' ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Unserializing instances of this class is forbidden. |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
*/ |
| 53 |
public function __wakeup() { |
| 54 |
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'propertyhive' ), '1.0.0' ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Constructor for the licenses class |
| 59 |
* |
| 60 |
*/ |
| 61 |
public function __construct() { |
| 62 |
add_action( 'propertyhive_check_licenses', array( $this, 'ph_check_licenses' ) ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Automated check for licenses |
| 67 |
* |
| 68 |
*/ |
| 69 |
public function ph_check_licenses( $force_check = false ) |
| 70 |
{ |
| 71 |
// Do a maximum of once per week |
| 72 |
$last_send = get_option( 'propertyhive_last_license_check', '' ); |
| 73 |
if( !$force_check && is_numeric( $last_send ) && $last_send > strtotime( '-1 week' ) ) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
update_option( 'propertyhive_last_license_check', time() ); |
| 78 |
|
| 79 |
// Start be removing what we already know about the license |
| 80 |
update_option( 'propertyhive_license_key_details', '', 'no' ); |
| 81 |
|
| 82 |
$data = array(); |
| 83 |
|
| 84 |
// Retrieve current theme info |
| 85 |
$theme_data = wp_get_theme(); |
| 86 |
$theme = $theme_data->Name . ' ' . $theme_data->Version; |
| 87 |
|
| 88 |
$data['php_version'] = phpversion(); |
| 89 |
$data['ph_version'] = PH_VERSION; |
| 90 |
$data['wp_version'] = get_bloginfo( 'version' ); |
| 91 |
$data['server'] = isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : ''; |
| 92 |
|
| 93 |
$data['install_date'] = get_option('propertyhive_install_timestamp', ''); |
| 94 |
if ( $data['install_date'] != '' && $data['install_date'] != 0 ) |
| 95 |
{ |
| 96 |
$data['install_date'] = date("jS F Y", $data['install_date']); |
| 97 |
} |
| 98 |
|
| 99 |
$data['multisite'] = is_multisite(); |
| 100 |
$data['url'] = home_url(); |
| 101 |
$data['theme'] = $theme; |
| 102 |
$data['email'] = get_bloginfo( 'admin_email' ); |
| 103 |
$data['license_key'] = get_option( 'propertyhive_license_key', '' ); |
| 104 |
|
| 105 |
// Retrieve current plugin information |
| 106 |
if( ! function_exists( 'get_plugins' ) ) { |
| 107 |
include ABSPATH . '/wp-admin/includes/plugin.php'; |
| 108 |
} |
| 109 |
|
| 110 |
$plugins = array_keys( get_plugins() ); |
| 111 |
$active_plugins = get_option( 'active_plugins', array() ); |
| 112 |
|
| 113 |
foreach ( $plugins as $key => $plugin ) { |
| 114 |
if ( in_array( $plugin, $active_plugins ) ) { |
| 115 |
// Remove active plugins from list so we can show active and inactive separately |
| 116 |
unset( $plugins[ $key ] ); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
$data['active_plugins'] = $active_plugins; |
| 121 |
$data['inactive_plugins'] = $plugins; |
| 122 |
$data['locale'] = get_locale(); |
| 123 |
|
| 124 |
$request = wp_remote_post( 'http://license.wp-property-hive.com/check-license.php', array( |
| 125 |
'method' => 'POST', |
| 126 |
'timeout' => 20, |
| 127 |
'redirection' => 5, |
| 128 |
'httpversion' => '1.1', |
| 129 |
'blocking' => true, |
| 130 |
'body' => $data, |
| 131 |
'user-agent' => 'PH/' . PH_VERSION . '; ' . get_bloginfo( 'url' ) |
| 132 |
) ); |
| 133 |
|
| 134 |
if ( is_wp_error( $request ) || ( !is_wp_error( $request ) && isset($request['body']) && $request['body'] == '' ) ) |
| 135 |
{ |
| 136 |
update_option( 'propertyhive_license_key_details', array(), 'no' ); |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
$body = unserialize($request['body']); |
| 141 |
if ( $body !== FALSE && is_array($body) && !empty($body) ) |
| 142 |
{ |
| 143 |
update_option( 'propertyhive_license_key_details', $body, 'no' ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
public function get_current_license() |
| 148 |
{ |
| 149 |
$license = get_option( 'propertyhive_license_key_details', array() ); |
| 150 |
|
| 151 |
if ( !is_array( $license ) ) { $license = array(); } |
| 152 |
|
| 153 |
return $license; |
| 154 |
} |
| 155 |
} |