| @@ -5,12 +5,12 @@ | ||
| 5 | 5 | class WPRAccount { |
| 6 | 6 | public $settings; |
| 7 | 7 | public $public; |
| 8 | 8 | public $secret; |
| 9 | - public $sig_match; | |
| 10 | 9 | public static $api_public_key = 'bvApiPublic'; |
| 11 | 10 | public static $accounts_list = 'bvAccountsList'; |
| 12 | - | |
| 11 | + private static $default_credential = array(); | |
| 12 | + | |
| 13 | 13 | public function __construct($settings, $public, $secret) { |
| 14 | 14 | $this->settings = $settings; |
| 15 | 15 | $this->public = $public; |
| 16 | 16 | $this->secret = $secret; |
| @@ -21,9 +21,15 @@ | ||
| 21 | 21 | if (array_key_exists($public, $accounts) && isset($accounts[$public]['secret'])) { |
| 22 | 22 | $secret = $accounts[$public]['secret']; |
| 23 | 23 | } |
| 24 | 24 | if (empty($secret) || (strlen($secret) < 32)) { |
| 25 | - return null; | |
| 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 | + } | |
| 26 | 32 | } |
| 27 | 33 | return new self($settings, $public, $secret); |
| 28 | 34 | } |
| 29 | 35 | |
| @@ -36,9 +42,9 @@ | ||
| 36 | 42 | |
| 37 | 43 | $str = ""; |
| 38 | 44 | $size = strlen($chars); |
| 39 | 45 | for( $i = 0; $i < $length; $i++ ) { |
| 40 | - $str .= $chars[rand(0, $size - 1)]; | |
| 46 | + $str .= $chars[rand(0, $size - 1)]; // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_rand | |
| 41 | 47 | } |
| 42 | 48 | return $str; |
| 43 | 49 | } |
| 44 | 50 | |
| @@ -54,8 +60,12 @@ | ||
| 54 | 60 | public static function updateApiPublicKey($settings, $pubkey) { |
| 55 | 61 | $settings->updateOption(self::$api_public_key, $pubkey); |
| 56 | 62 | } |
| 57 | 63 | |
| 64 | + public static function getDefaultPublicKey() { | |
| 65 | + return WPRHelper::arrayKeyFirst(self::$default_credential); | |
| 66 | + } | |
| 67 | + | |
| 58 | 68 | public static function getApiPublicKey($settings) { |
| 59 | 69 | return $settings->getOption(self::$api_public_key); |
| 60 | 70 | } |
| 61 | 71 | |
| @@ -159,36 +169,10 @@ | ||
| 159 | 169 | } |
| 160 | 170 | |
| 161 | 171 | public function info() { |
| 162 | 172 | return array( |
| 163 | - "public" => substr($this->public, 0, 6), | |
| 164 | - "sigmatch" => substr($this->sig_match, 0, 6) | |
| 173 | + "public" => substr($this->public, 0, 6) | |
| 165 | 174 | ); |
| 166 | - } | |
| 167 | - | |
| 168 | - public static function getSigMatch($request, $secret) { | |
| 169 | - $method = $request->method; | |
| 170 | - $time = $request->time; | |
| 171 | - $version = $request->version; | |
| 172 | - if ($request->is_sha1) { | |
| 173 | - $sig_match = sha1($method.$secret.$time.$version); | |
| 174 | - } else { | |
| 175 | - $sig_match = md5($method.$secret.$time.$version); | |
| 176 | - } | |
| 177 | - return $sig_match; | |
| 178 | - } | |
| 179 | - | |
| 180 | - public function authenticate($request) { | |
| 181 | - $time = $request->time; | |
| 182 | - if ($time < intval($this->settings->getOption('bvLastRecvTime')) - 300) { | |
| 183 | - return false; | |
| 184 | - } | |
| 185 | - $this->sig_match = self::getSigMatch($request, $this->secret); | |
| 186 | - if ($this->sig_match !== $request->sig) { | |
| 187 | - return false; | |
| 188 | - } | |
| 189 | - $this->settings->updateOption('bvLastRecvTime', $time); | |
| 190 | - return 1; | |
| 191 | 175 | } |
| 192 | 176 | |
| 193 | 177 | public function updateInfo($info) { |
| 194 | 178 | $accounts = self::allAccounts($this->settings); |