| @@ -5,12 +5,12 @@ | ||
| 5 | 5 | class WPRAccount { |
| 6 | 6 | public $settings; |
| 7 | 7 | public $public; |
| 8 | 8 | public $secret; |
| 9 | + public $sig_match; | |
| 9 | 10 | public static $api_public_key = 'bvApiPublic'; |
| 10 | 11 | public static $accounts_list = 'bvAccountsList'; |
| 11 | - private static $default_credential = array(); | |
| 12 | - | |
| 12 | + | |
| 13 | 13 | public function __construct($settings, $public, $secret) { |
| 14 | 14 | $this->settings = $settings; |
| 15 | 15 | $this->public = $public; |
| 16 | 16 | $this->secret = $secret; |
| @@ -16,24 +16,14 @@ | ||
| 16 | 16 | $this->secret = $secret; |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | public static function find($settings, $public) { |
| 20 | - if (!is_string($public)) { | |
| 21 | - return null; | |
| 22 | - } | |
| 23 | - | |
| 24 | 20 | $accounts = self::allAccounts($settings); |
| 25 | 21 | if (array_key_exists($public, $accounts) && isset($accounts[$public]['secret'])) { |
| 26 | 22 | $secret = $accounts[$public]['secret']; |
| 27 | 23 | } |
| 28 | 24 | 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 | - } | |
| 25 | + return null; | |
| 36 | 26 | } |
| 37 | 27 | return new self($settings, $public, $secret); |
| 38 | 28 | } |
| 39 | 29 | |
| @@ -46,18 +36,14 @@ | ||
| 46 | 36 | |
| 47 | 37 | $str = ""; |
| 48 | 38 | $size = strlen($chars); |
| 49 | 39 | for( $i = 0; $i < $length; $i++ ) { |
| 50 | - $str .= $chars[random_int(0, $size - 1)]; | |
| 40 | + $str .= $chars[rand(0, $size - 1)]; | |
| 51 | 41 | } |
| 52 | 42 | return $str; |
| 53 | 43 | } |
| 54 | 44 | |
| 55 | 45 | public static function sanitizeKey($key) { |
| 56 | - if (!is_string($key)) { | |
| 57 | - return ''; | |
| 58 | - } | |
| 59 | - | |
| 60 | 46 | return preg_replace('/[^a-zA-Z0-9_\-]/', '', $key); |
| 61 | 47 | } |
| 62 | 48 | |
| 63 | 49 | public static function apiPublicAccount($settings) { |
| @@ -68,12 +54,8 @@ | ||
| 68 | 54 | public static function updateApiPublicKey($settings, $pubkey) { |
| 69 | 55 | $settings->updateOption(self::$api_public_key, $pubkey); |
| 70 | 56 | } |
| 71 | 57 | |
| 72 | - public static function getDefaultPublicKey() { | |
| 73 | - return WPRHelper::arrayKeyFirst(self::$default_credential); | |
| 74 | - } | |
| 75 | - | |
| 76 | 58 | public static function getApiPublicKey($settings) { |
| 77 | 59 | return $settings->getOption(self::$api_public_key); |
| 78 | 60 | } |
| 79 | 61 | |
| @@ -127,10 +109,9 @@ | ||
| 127 | 109 | public static function accountsByPattern($settings, $search_key, $search_pattern) { |
| 128 | 110 | $accounts = self::allAccounts($settings); |
| 129 | 111 | $accounts_by_pattern = array(); |
| 130 | 112 | foreach ($accounts as $pubkey => $value) { |
| 131 | - if (array_key_exists($search_key, $value) && | |
| 132 | - WPRHelper::safePregMatch($search_pattern, $value[$search_key]) == 1) { | |
| 113 | + if (array_key_exists($search_key, $value) && preg_match($search_pattern, $value[$search_key]) == 1) { | |
| 133 | 114 | $accounts_by_pattern[$pubkey] = $value; |
| 134 | 115 | } |
| 135 | 116 | } |
| 136 | 117 | return $accounts_by_pattern; |
| @@ -177,12 +158,38 @@ | ||
| 177 | 158 | } |
| 178 | 159 | |
| 179 | 160 | public function info() { |
| 180 | 161 | return array( |
| 181 | - "public" => substr($this->public, 0, 6) | |
| 162 | + "public" => substr($this->public, 0, 6), | |
| 163 | + "sigmatch" => substr($this->sig_match, 0, 6) | |
| 182 | 164 | ); |
| 183 | 165 | } |
| 184 | 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 | + } | |
| 191 | + | |
| 185 | 192 | public function updateInfo($info) { |
| 186 | 193 | $accounts = self::allAccounts($this->settings); |
| 187 | 194 | $account_type = $info["account_type"]; |
| 188 | 195 | $pubkey = $info['pubkey']; |
| @@ -244,5 +251,5 @@ | ||
| 244 | 251 | $accounts = self::allAccounts($settings); |
| 245 | 252 | return array_key_exists($pubkey, $accounts); |
| 246 | 253 | } |
| 247 | 254 | } |
| 248 | -endif; | |
| 255 | +endif; | |