| 1 |
<?php |
| 2 |
/** |
| 3 |
* Manager |
| 4 |
* |
| 5 |
* @namespace UsabilityDynamics |
| 6 |
* |
| 7 |
*/ |
| 8 |
namespace UsabilityDynamics\UD_API { |
| 9 |
|
| 10 |
if( !class_exists( 'UsabilityDynamics\UD_API\Manager' ) ) { |
| 11 |
|
| 12 |
/** |
| 13 |
* |
| 14 |
* @author: peshkov@UD |
| 15 |
*/ |
| 16 |
class Manager { |
| 17 |
|
| 18 |
private $type; |
| 19 |
private $product_id; |
| 20 |
private $referrer; |
| 21 |
private $name; |
| 22 |
private $boot_file; |
| 23 |
private $instance_key; |
| 24 |
|
| 25 |
public $errors_callback; |
| 26 |
|
| 27 |
/** |
| 28 |
* Constructor |
| 29 |
*/ |
| 30 |
public function __construct( $schema = array() ) { |
| 31 |
$this->type = !empty( $schema[ 'type' ] ) ? $schema[ 'type' ] : false; |
| 32 |
$this->product_id = !empty( $schema[ 'product_id' ] ) ? $schema[ 'product_id' ] : false; |
| 33 |
$this->referrer = !empty( $schema[ 'referrer' ] ) ? $schema[ 'referrer' ] : false; |
| 34 |
$this->name = !empty( $schema[ 'name' ] ) ? $schema[ 'name' ] : false; |
| 35 |
$this->boot_file = !empty( $schema[ 'boot_file' ] ) ? $schema[ 'boot_file' ] : false; |
| 36 |
$this->errors_callback = !empty( $schema[ 'errors_callback' ] ) ? $schema[ 'errors_callback' ] : false; |
| 37 |
$this->queue_updates(); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Add product to global list of products. |
| 42 |
*/ |
| 43 |
public function queue_updates() { |
| 44 |
global $_ud_queued_updates; |
| 45 |
|
| 46 |
if( !$this->product_id || !$this->boot_file || !$this->name ) { |
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
//** Get instance key. If it does not exist: generate it. */ |
| 51 |
$option_key = sanitize_key( $this->name ) . ':instance'; |
| 52 |
$this->instance_key = get_option( $option_key, false ); |
| 53 |
if( empty( $this->instance_key ) ) { |
| 54 |
$this->instance_key = $this->generate_password( 12, false ); |
| 55 |
update_option( $option_key, $this->instance_key ); |
| 56 |
} |
| 57 |
|
| 58 |
$product = new \stdClass(); |
| 59 |
$product->type = $this->type; |
| 60 |
$product->product_id = $this->product_id; |
| 61 |
$product->instance_key = $this->instance_key; |
| 62 |
$product->errors_callback = $this->errors_callback; |
| 63 |
|
| 64 |
$_ud_queued_updates = isset( $_ud_queued_updates ) ? $_ud_queued_updates : array(); |
| 65 |
//** Add theme */ |
| 66 |
if( $product->type === 'theme' ) { |
| 67 |
$product->file = $this->boot_file; |
| 68 |
//** Must be only one theme in the list! */ |
| 69 |
if( !empty( $_ud_queued_updates[ '_theme_' ] ) ) { |
| 70 |
//** WTF? How it could be? */ |
| 71 |
wp_die( 'Are you cheating?' ); |
| 72 |
} else { |
| 73 |
$_ud_queued_updates[ '_theme_' ] = $product; |
| 74 |
} |
| 75 |
} |
| 76 |
//** Add plugin */ |
| 77 |
elseif ( $product->type === 'plugin' ) { |
| 78 |
$product->file = plugin_basename( $this->boot_file ); |
| 79 |
if( !$this->referrer ) { |
| 80 |
//** WTF? How it could be? */ |
| 81 |
wp_die( 'Are you cheating?' ); |
| 82 |
} |
| 83 |
$referrer_key = strtolower( $this->referrer ); |
| 84 |
$referrer_key = preg_replace( '/[^a-z0-9_\-\/]/', '', $referrer_key ); |
| 85 |
$_ud_queued_updates[ $referrer_key ] = isset( $_ud_queued_updates[ $referrer_key ] ) ? $_ud_queued_updates[ $referrer_key ] : array(); |
| 86 |
$_ud_queued_updates[ $referrer_key ][] = $product; |
| 87 |
} |
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Creates a unique instance ID |
| 93 |
*/ |
| 94 |
private function generate_password( $length = 12, $special_chars = true, $extra_special_chars = false ) { |
| 95 |
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; |
| 96 |
if ( $special_chars ) { |
| 97 |
$chars .= '!@#$%^&*()'; |
| 98 |
} |
| 99 |
if ( $extra_special_chars ) { |
| 100 |
$chars .= '-_ []{}<>~`+=,.;:/?|'; |
| 101 |
} |
| 102 |
$password = ''; |
| 103 |
for ( $i = 0; $i < $length; $i++ ) { |
| 104 |
$password .= substr( $chars, $this->rand(0, strlen($chars) - 1), 1); |
| 105 |
} |
| 106 |
return $password; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* |
| 111 |
*/ |
| 112 |
private function rand( $min = 0, $max = 0 ) { |
| 113 |
global $rnd_value; |
| 114 |
//** Reset $rnd_value after 14 uses */ |
| 115 |
//** 32(md5) + 40(sha1) + 40(sha1) / 8 = 14 random numbers from $rnd_value */ |
| 116 |
if ( strlen($rnd_value) < 8 ) { |
| 117 |
if ( defined( 'WP_SETUP_CONFIG' ) ) |
| 118 |
static $seed = ''; |
| 119 |
else |
| 120 |
$seed = get_transient('random_seed'); |
| 121 |
$rnd_value = md5( uniqid(microtime() . mt_rand(), true ) . $seed ); |
| 122 |
$rnd_value .= sha1($rnd_value); |
| 123 |
$rnd_value .= sha1($rnd_value . $seed); |
| 124 |
$seed = md5($seed . $rnd_value); |
| 125 |
if ( ! defined( 'WP_SETUP_CONFIG' ) ) |
| 126 |
set_transient('random_seed', $seed); |
| 127 |
} |
| 128 |
//** Take the first 8 digits for our value */ |
| 129 |
$value = substr($rnd_value, 0, 8); |
| 130 |
//** Strip the first eight, leaving the remainder for the next call to wp_rand(). */ |
| 131 |
$rnd_value = substr($rnd_value, 8); |
| 132 |
$value = abs(hexdec($value)); |
| 133 |
//** Some misconfigured 32bit environments (Entropy PHP, for example) truncate integers larger than PHP_INT_MAX to PHP_INT_MAX rather than overflowing them to floats. */ |
| 134 |
$max_random_number = 3000000000 === 2147483647 ? (float) "4294967295" : 4294967295; // 4294967295 = 0xffffffff |
| 135 |
//** Reduce the value to be within the min - max range */ |
| 136 |
if ( $max != 0 ) { |
| 137 |
$value = $min + ( $max - $min + 1 ) * $value / ( $max_random_number + 1 ); |
| 138 |
} |
| 139 |
return abs(intval($value)); |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
} |