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