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