| @@ -7,8 +7,9 @@ | ||
| 7 | 7 | public $public; |
| 8 | 8 | public $secret; |
| 9 | 9 | public static $api_public_key = 'bvApiPublic'; |
| 10 | 10 | public static $accounts_list = 'bvAccountsList'; |
| 11 | + private static $default_credential = array(); | |
| 11 | 12 | |
| 12 | 13 | public function __construct($settings, $public, $secret) { |
| 13 | 14 | $this->settings = $settings; |
| 14 | 15 | $this->public = $public; |
| @@ -15,14 +16,24 @@ | ||
| 15 | 16 | $this->secret = $secret; |
| 16 | 17 | } |
| 17 | 18 | |
| 18 | 19 | public static function find($settings, $public) { |
| 20 | + if (!is_string($public)) { | |
| 21 | + return null; | |
| 22 | + } | |
| 23 | + | |
| 19 | 24 | $accounts = self::allAccounts($settings); |
| 20 | 25 | if (array_key_exists($public, $accounts) && isset($accounts[$public]['secret'])) { |
| 21 | 26 | $secret = $accounts[$public]['secret']; |
| 22 | 27 | } |
| 23 | 28 | if (empty($secret) || (strlen($secret) < 32)) { |
| 24 | - return null; | |
| 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 | 36 | } |
| 26 | 37 | return new self($settings, $public, $secret); |
| 27 | 38 | } |
| 28 | 39 | |
| @@ -35,14 +46,18 @@ | ||
| 35 | 46 | |
| 36 | 47 | $str = ""; |
| 37 | 48 | $size = strlen($chars); |
| 38 | 49 | for( $i = 0; $i < $length; $i++ ) { |
| 39 | - $str .= $chars[rand(0, $size - 1)]; | |
| 50 | + $str .= $chars[random_int(0, $size - 1)]; | |
| 40 | 51 | } |
| 41 | 52 | return $str; |
| 42 | 53 | } |
| 43 | 54 | |
| 44 | 55 | public static function sanitizeKey($key) { |
| 56 | + if (!is_string($key)) { | |
| 57 | + return ''; | |
| 58 | + } | |
| 59 | + | |
| 45 | 60 | return preg_replace('/[^a-zA-Z0-9_\-]/', '', $key); |
| 46 | 61 | } |
| 47 | 62 | |
| 48 | 63 | public static function apiPublicAccount($settings) { |
| @@ -53,8 +68,12 @@ | ||
| 53 | 68 | public static function updateApiPublicKey($settings, $pubkey) { |
| 54 | 69 | $settings->updateOption(self::$api_public_key, $pubkey); |
| 55 | 70 | } |
| 56 | 71 | |
| 72 | + public static function getDefaultPublicKey() { | |
| 73 | + return WPRHelper::arrayKeyFirst(self::$default_credential); | |
| 74 | + } | |
| 75 | + | |
| 57 | 76 | public static function getApiPublicKey($settings) { |
| 58 | 77 | return $settings->getOption(self::$api_public_key); |
| 59 | 78 | } |
| 60 | 79 | |
| @@ -225,5 +244,5 @@ | ||
| 225 | 244 | $accounts = self::allAccounts($settings); |
| 226 | 245 | return array_key_exists($pubkey, $accounts); |
| 227 | 246 | } |
| 228 | 247 | } |
| 229 | -endif; | |
| 248 | +endif; | |