| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) exit; |
| 4 |
if (!class_exists('WPCOMAccount')) : |
| 5 |
class WPCOMAccount { |
| 6 |
public $settings; |
| 7 |
public $public; |
| 8 |
public $secret; |
| 9 |
public static $api_public_key = 'bvApiPublic'; |
| 10 |
public static $accounts_list = 'bvAccountsList'; |
| 11 |
private static $default_credential = array(); |
| 12 |
|
| 13 |
public function __construct($settings, $public, $secret) { |
| 14 |
$this->settings = $settings; |
| 15 |
$this->public = $public; |
| 16 |
$this->secret = $secret; |
| 17 |
} |
| 18 |
|
| 19 |
public static function find($settings, $public) { |
| 20 |
if (!is_string($public)) { |
| 21 |
return null; |
| 22 |
} |
| 23 |
|
| 24 |
$accounts = self::allAccounts($settings); |
| 25 |
if (array_key_exists($public, $accounts) && isset($accounts[$public]['secret'])) { |
| 26 |
$secret = $accounts[$public]['secret']; |
| 27 |
} |
| 28 |
if (empty($secret) || (strlen($secret) < 32)) { |
| 29 |
if (!empty(self::$default_credential) && array_key_exists($public, self::$default_credential) |
| 30 |
&& strlen($public) >= 32) { |
| 31 |
$secret = self::$default_credential[$public]; |
| 32 |
self::addAccount($settings, $public, $secret); |
| 33 |
} else { |
| 34 |
return null; |
| 35 |
} |
| 36 |
} |
| 37 |
return new self($settings, $public, $secret); |
| 38 |
} |
| 39 |
|
| 40 |
public static function update($settings, $allAccounts) { |
| 41 |
$settings->updateOption(self::$accounts_list, $allAccounts); |
| 42 |
} |
| 43 |
|
| 44 |
public static function randString($length) { |
| 45 |
$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 46 |
|
| 47 |
$str = ""; |
| 48 |
$size = strlen($chars); |
| 49 |
for( $i = 0; $i < $length; $i++ ) { |
| 50 |
$str .= $chars[random_int(0, $size - 1)]; |
| 51 |
} |
| 52 |
return $str; |
| 53 |
} |
| 54 |
|
| 55 |
public static function sanitizeKey($key) { |
| 56 |
if (!is_string($key)) { |
| 57 |
return ''; |
| 58 |
} |
| 59 |
|
| 60 |
return preg_replace('/[^a-zA-Z0-9_\-]/', '', $key); |
| 61 |
} |
| 62 |
|
| 63 |
public static function apiPublicAccount($settings) { |
| 64 |
$pubkey = $settings->getOption(self::$api_public_key); |
| 65 |
return self::find($settings, $pubkey); |
| 66 |
} |
| 67 |
|
| 68 |
public static function updateApiPublicKey($settings, $pubkey) { |
| 69 |
$settings->updateOption(self::$api_public_key, $pubkey); |
| 70 |
} |
| 71 |
|
| 72 |
public static function getDefaultPublicKey() { |
| 73 |
return WPCOMHelper::arrayKeyFirst(self::$default_credential); |
| 74 |
} |
| 75 |
|
| 76 |
public static function getApiPublicKey($settings) { |
| 77 |
return $settings->getOption(self::$api_public_key); |
| 78 |
} |
| 79 |
|
| 80 |
public static function getPlugName($settings) { |
| 81 |
$bvinfo = new WPCOMInfo($settings); |
| 82 |
return $bvinfo->plugname; |
| 83 |
} |
| 84 |
|
| 85 |
public static function allAccounts($settings) { |
| 86 |
$accounts = $settings->getOption(self::$accounts_list); |
| 87 |
if (!is_array($accounts)) { |
| 88 |
$accounts = array(); |
| 89 |
} |
| 90 |
return $accounts; |
| 91 |
} |
| 92 |
|
| 93 |
public static function accountsByPlugname($settings) { |
| 94 |
$accounts = self::allAccounts($settings); |
| 95 |
$accountsByPlugname = array(); |
| 96 |
$plugname = self::getPlugName($settings); |
| 97 |
foreach ($accounts as $pubkey => $value) { |
| 98 |
if (array_key_exists($plugname, $value) && $value[$plugname] == 1) { |
| 99 |
$accountsByPlugname[$pubkey] = $value; |
| 100 |
} |
| 101 |
} |
| 102 |
return $accountsByPlugname; |
| 103 |
} |
| 104 |
|
| 105 |
public static function accountsByType($settings, $account_type) { |
| 106 |
$accounts = self::allAccounts($settings); |
| 107 |
$accounts_by_type = array(); |
| 108 |
foreach ($accounts as $pubkey => $value) { |
| 109 |
if (array_key_exists('account_type', $value) && $value['account_type'] === $account_type) { |
| 110 |
$accounts_by_type[$pubkey] = $value; |
| 111 |
} |
| 112 |
} |
| 113 |
return $accounts_by_type; |
| 114 |
} |
| 115 |
|
| 116 |
public static function accountsByGid($settings, $account_gid) { |
| 117 |
$accounts = self::allAccounts($settings); |
| 118 |
$accounts_by_gid = array(); |
| 119 |
foreach ($accounts as $pubkey => $value) { |
| 120 |
if (array_key_exists('account_gid', $value) && $value['account_gid'] === $account_gid) { |
| 121 |
$accounts_by_gid[$pubkey] = $value; |
| 122 |
} |
| 123 |
} |
| 124 |
return $accounts_by_gid; |
| 125 |
} |
| 126 |
|
| 127 |
public static function accountsByPattern($settings, $search_key, $search_pattern) { |
| 128 |
$accounts = self::allAccounts($settings); |
| 129 |
$accounts_by_pattern = array(); |
| 130 |
foreach ($accounts as $pubkey => $value) { |
| 131 |
if (array_key_exists($search_key, $value) && |
| 132 |
WPCOMHelper::safePregMatch($search_pattern, $value[$search_key]) == 1) { |
| 133 |
$accounts_by_pattern[$pubkey] = $value; |
| 134 |
} |
| 135 |
} |
| 136 |
return $accounts_by_pattern; |
| 137 |
} |
| 138 |
|
| 139 |
public static function isConfigured($settings) { |
| 140 |
$accounts = self::accountsByPlugname($settings); |
| 141 |
return (sizeof($accounts) >= 1); |
| 142 |
} |
| 143 |
|
| 144 |
public static function setup($settings) { |
| 145 |
$bvinfo = new WPCOMInfo($settings); |
| 146 |
$settings->updateOption($bvinfo->plug_redirect, 'yes'); |
| 147 |
$settings->updateOption('bvActivateTime', time()); |
| 148 |
} |
| 149 |
|
| 150 |
public function authenticatedUrl($method) { |
| 151 |
$bvinfo = new WPCOMInfo($this->settings); |
| 152 |
$qstr = http_build_query($this->newAuthParams($bvinfo->version)); |
| 153 |
return $bvinfo->appUrl().$method."?".$qstr; |
| 154 |
} |
| 155 |
|
| 156 |
public function newAuthParams($version) { |
| 157 |
$bvinfo = new WPCOMInfo($this->settings); |
| 158 |
$args = array(); |
| 159 |
$time = time(); |
| 160 |
$sig = sha1($this->public.$this->secret.$time.$version); |
| 161 |
$args['sig'] = $sig; |
| 162 |
$args['bvTime'] = $time; |
| 163 |
$args['bvPublic'] = $this->public; |
| 164 |
$args['bvVersion'] = $version; |
| 165 |
$args['sha1'] = '1'; |
| 166 |
$args['plugname'] = $bvinfo->plugname; |
| 167 |
return $args; |
| 168 |
} |
| 169 |
|
| 170 |
public static function addAccount($settings, $public, $secret) { |
| 171 |
$accounts = self::allAccounts($settings); |
| 172 |
if (!isset($public, $accounts)) { |
| 173 |
$accounts[$public] = array(); |
| 174 |
} |
| 175 |
$accounts[$public]['secret'] = $secret; |
| 176 |
self::update($settings, $accounts); |
| 177 |
} |
| 178 |
|
| 179 |
public function info() { |
| 180 |
return array( |
| 181 |
"public" => substr($this->public, 0, 6) |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
public function updateInfo($info) { |
| 186 |
$accounts = self::allAccounts($this->settings); |
| 187 |
$account_type = $info["account_type"]; |
| 188 |
$pubkey = $info['pubkey']; |
| 189 |
if (!array_key_exists($pubkey, $accounts)) { |
| 190 |
$accounts[$pubkey] = array(); |
| 191 |
} |
| 192 |
if (array_key_exists('secret', $info)) { |
| 193 |
$accounts[$pubkey]['secret'] = $info['secret']; |
| 194 |
} |
| 195 |
$accounts[$pubkey]['account_gid'] = $info['account_gid']; |
| 196 |
$accounts[$pubkey]['lastbackuptime'] = time(); |
| 197 |
if (isset($info["speed_plugname"])) { |
| 198 |
$speed_plugname = $info["speed_plugname"]; |
| 199 |
$accounts[$pubkey][$speed_plugname] = true; |
| 200 |
} |
| 201 |
if (isset($info["plugname"])) { |
| 202 |
$plugname = $info["plugname"]; |
| 203 |
$accounts[$pubkey][$plugname] = true; |
| 204 |
} |
| 205 |
$accounts[$pubkey]['account_type'] = $account_type; |
| 206 |
$accounts[$pubkey]['url'] = $info['url']; |
| 207 |
$accounts[$pubkey]['email'] = $info['email']; |
| 208 |
self::update($this->settings, $accounts); |
| 209 |
} |
| 210 |
|
| 211 |
public static function remove($settings, $pubkey) { |
| 212 |
$accounts = self::allAccounts($settings); |
| 213 |
if (array_key_exists($pubkey, $accounts)) { |
| 214 |
unset($accounts[$pubkey]); |
| 215 |
self::update($settings, $accounts); |
| 216 |
return true; |
| 217 |
} |
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
public static function removeByAccountType($settings, $account_type) { |
| 222 |
$accounts = WPCOMAccount::accountsByType($settings, $account_type); |
| 223 |
if (sizeof($accounts) >= 1) { |
| 224 |
foreach ($accounts as $pubkey => $value) { |
| 225 |
WPCOMAccount::remove($settings, $pubkey); |
| 226 |
} |
| 227 |
return true; |
| 228 |
} |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
public static function removeByAccountGid($settings, $account_gid) { |
| 233 |
$accounts = WPCOMAccount::accountsByGid($settings, $account_gid); |
| 234 |
if (sizeof($accounts) >= 1) { |
| 235 |
foreach ($accounts as $pubkey => $value) { |
| 236 |
WPCOMAccount::remove($settings, $pubkey); |
| 237 |
} |
| 238 |
return true; |
| 239 |
} |
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
public static function exists($settings, $pubkey) { |
| 244 |
$accounts = self::allAccounts($settings); |
| 245 |
return array_key_exists($pubkey, $accounts); |
| 246 |
} |
| 247 |
} |
| 248 |
endif; |
| 249 |
|