| Current Path : /home/ereika83/public_html/wp-content/plugins/genesis-club-pro/classes/ |
| Current File : /home/ereika83/public_html/wp-content/plugins/genesis-club-pro/classes/class-api.php |
<?php
class Genesis_Club_API extends Genesis_Club_Module {
const OPTION_NAME = 'licence';
private $plugin_name=GENESIS_CLUB_PLUGIN_NAME;
private $local_version=GENESIS_CLUB_VERSION;
private $default_action='updates';
private $transient_expiry = 86400;
private $initialized=false;
private $key='-';
private $upgrader;
private $version;
private $package;
private $expiry;
private $tested;
private $notice;
private $updates;
private $valid;
function get_options_name() { return false; }
function get_defaults() { return false; }
function init() {
if (!$this->initialized) $this->update();
}
function update($cache = true, $action = '') {
if (empty($action)) $action = $this->default_action;
return ($cache && $this->initialized && ($action==$this->default_action)) ? $this->updates : $this->get_updates($action,$cache);
}
function set_key($new_key, $md5) {
$this->key = empty($new_key) ? '' : ($md5 ? md5($new_key) : $new_key);
}
function get_key($cache=true) {
if (!$cache || ('-'== $this->key)) $this->key = get_option($this->add_plugin_prefix(self::OPTION_NAME));
return $this->key;
}
function has_key($cache=true) {
$key = $this->get_key($cache);
return !empty($key) && (strlen($key) == 32); //has a key worth checking
}
function empty_key($cache=true) {
$key = $this->get_key($cache);
return empty($key);
}
function save_key($new_key, $md5 = true, $save_if_unchanged = false) {
$updated = false;
$old_key = $this->get_key(false); //fetch old key from database
if ($save_if_unchanged || ($new_key != $old_key)) {
$this->initialized = false;
$this->set_key($new_key,$md5);
$updated = update_option($this->add_plugin_prefix(self::OPTION_NAME),$this->key) ;
if ($save_if_unchanged || $updated) $this->update(false); //get updates for new key
}
return $updated;
}
function check_validity(){
if ($this->get_key()) {
$this->init();
return $this->valid;
}
else
return false;
}
function get_version(){
$this->init();
return $this->version;
}
function get_package(){
$this->init();
return $this->package;
}
function get_notice(){
$this->init();
return $this->notice;
}
function get_tested(){
$this->init();
return $this->tested;
}
function get_expiry(){
$this->init();
return $this->expiry;
}
function get_transient($transient) {
if ($value = get_transient($transient)) return $value;
/**transients may not be working so use homespun alternative ***/
$last_update = get_option($transient.'_date');
if ($last_update && (abs(time() - $last_update) < $this->transient_expiry) )
return get_option($transient);
else
return false;
}
function set_transient($transient, $value) {
if (set_transient($transient, $value, $this->transient_expiry)
&& ( $value == get_transient($transient))) return true;
/**transients not working so use alternative ***/
update_option( $transient, $value);
update_option( $transient.'_date', time());
return true;
}
function is_old_version() {
$currarray = explode('.', $this->local_version);
$currint = $currarray[0] * 10000 + $currarray[1] * 100 + (count($currarray)>=3 ? $currarray[2] : 0);
$verarray = explode('.', $this->version);
$verint = $verarray[0] * 10000 + $verarray[1] * 100 + (count($verarray)>=3 ? $verarray[2] : 0);
return $currint < $verint;
}
private function add_plugin_prefix($action) {
return str_replace('-','_', $this->plugin_name).'_'.strtolower($action);
}
private function get_updates($action,$cache) {
$result = $this->has_key($cache) ?
$this->parse_updates($this->fetch_remote_or_cache($action,$cache)) :
$this->set_defaults($this->empty_key($cache) ? '' : 'Invalid License Key' );
if ($action==$this->default_action) $this->updates = $result;
return $result;
}
private function parse_updates($response) {
$this->initialized = true;
if (is_array($response) && (count($response) >= 6)) {
$this->valid = $response['valid_key'];
$this->version = $response['version'];
$this->package = $response['package'];
$this->notice = $response['notice'];
$this->tested = $response['tested'];
$this->expiry = $response['expiry'];
return $response['updates'];
} else {
return $this->set_defaults('Unable to check for updates. Please try again.');
}
}
private function set_defaults($notice = '') {
$this->valid = false;
$this->version = $this->local_version;
$this->package = '';
$this->notice = empty($notice) ? '' : ('<div class="message">'.__($notice).'</div>');
$this->expiry = '';
$this->updates = '';
return $this->updates;
}
private function fetch_remote_or_cache($action,$cache=true){
$transient = $this->add_plugin_prefix($action);
$values = $cache ? $this->get_transient($transient) : false;
if ((false === $values) || is_array($values) || empty($values)) {
$raw_response = $this->remote_call($action, $cache);
$values = (is_array($raw_response) && array_key_exists('body',$raw_response)) ? $raw_response['body'] : false;
$this->set_transient($transient, $values); //cache for 24 hours
}
return false === $values ? false : @unserialize(@gzinflate(@base64_decode($values)));
}
private function remote_call($action, $cache=true, $backup = false){
$options = array('method' => 'POST', 'timeout' => 20);
$options['headers'] = array(
'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset'),
'User-Agent' => 'WordPress/' . get_bloginfo("version"),
'Referer' => get_bloginfo("url")
);
$raw_response = wp_remote_request($this->get_upgrader($cache, $backup). '&act='.$action , $options);
if ( is_wp_error( $raw_response ) || (200 != $raw_response['response']['code']) || empty($raw_response)){
return $backup ? false : $this->remote_call($action, $cache, true);
} else {
return $raw_response;
}
}
private function get_upgrader($cache = true, $backup=false){
global $wpdb;
if (empty($this->upgrader) || ($cache == false) || $backup)
$this->upgrader = $this->get_remote_updater($backup). sprintf("?of=%s&key=%s&v=%s&wp=%s&php=%s&mysql=%s",
urlencode($this->plugin_name), urlencode($this->get_key()), urlencode($this->local_version), urlencode(get_bloginfo("version")),
urlencode(phpversion()), urlencode($wpdb->db_version()));
return $this->upgrader;
}
private function get_remote_updater($backup = false) {
return sprintf('http://updater%1$s.genesisclubpro.com/',$backup ? '2':'1');
}
}