| @@ -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 | |
| @@ -109,9 +119,10 @@ | ||
| 109 | 119 | public static function accountsByPattern($settings, $search_key, $search_pattern) { |
| 110 | 120 | $accounts = self::allAccounts($settings); |
| 111 | 121 | $accounts_by_pattern = array(); |
| 112 | 122 | foreach ($accounts as $pubkey => $value) { |
| 113 | - if (array_key_exists($search_key, $value) && preg_match($search_pattern, $value[$search_key]) == 1) { | |
| 123 | + if (array_key_exists($search_key, $value) && | |
| 124 | + WPRHelper::safePregMatch($search_pattern, $value[$search_key]) == 1) { | |
| 114 | 125 | $accounts_by_pattern[$pubkey] = $value; |
| 115 | 126 | } |
| 116 | 127 | } |
| 117 | 128 | return $accounts_by_pattern; |
| @@ -158,36 +169,10 @@ | ||
| 158 | 169 | } |
| 159 | 170 | |
| 160 | 171 | public function info() { |
| 161 | 172 | return array( |
| 162 | - "public" => substr($this->public, 0, 6), | |
| 163 | - "sigmatch" => substr($this->sig_match, 0, 6) | |
| 173 | + "public" => substr($this->public, 0, 6) | |
| 164 | 174 | ); |
| 165 | - } | |
| 166 | - | |
| 167 | - public static function getSigMatch($request, $secret) { | |
| 168 | - $method = $request->method; | |
| 169 | - $time = $request->time; | |
| 170 | - $version = $request->version; | |
| 171 | - if ($request->is_sha1) { | |
| 172 | - $sig_match = sha1($method.$secret.$time.$version); | |
| 173 | - } else { | |
| 174 | - $sig_match = md5($method.$secret.$time.$version); | |
| 175 | - } | |
| 176 | - return $sig_match; | |
| 177 | - } | |
| 178 | - | |
| 179 | - public function authenticate($request) { | |
| 180 | - $time = $request->time; | |
| 181 | - if ($time < intval($this->settings->getOption('bvLastRecvTime')) - 300) { | |
| 182 | - return false; | |
| 183 | - } | |
| 184 | - $this->sig_match = self::getSigMatch($request, $this->secret); | |
| 185 | - if ($this->sig_match !== $request->sig) { | |
| 186 | - return false; | |
| 187 | - } | |
| 188 | - $this->settings->updateOption('bvLastRecvTime', $time); | |
| 189 | - return 1; | |
| 190 | 175 | } |
| 191 | 176 | |
| 192 | 177 | public function updateInfo($info) { |
| 193 | 178 | $accounts = self::allAccounts($this->settings); |