| @@ -1,37 +1,57 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | if (!defined('ABSPATH')) exit; |
| 3 | 3 | if (!class_exists('WPRRecover')) : |
| 4 | 4 | class WPRRecover { |
| 5 | - public static $default_secret_key = 'bvSecretKey'; | |
| 5 | + public static $default_secret_key = 'bv_default_secret_key'; | |
| 6 | 6 | |
| 7 | 7 | public static function defaultSecret($settings) { |
| 8 | 8 | $secret = self::getDefaultSecret($settings); |
| 9 | 9 | if (empty($secret)) { |
| 10 | - $secret = WPRAccount::randString(32); | |
| 11 | - self::updateDefaultSecret($settings, $secret); | |
| 10 | + $secret = WPRRecover::refreshDefaultSecret($settings); | |
| 12 | 11 | } |
| 13 | 12 | return $secret; |
| 14 | 13 | } |
| 15 | 14 | |
| 15 | + public static function refreshDefaultSecret($settings) { | |
| 16 | + $key_details = array(); | |
| 17 | + $key_details["key"] = WPRAccount::randString(32); | |
| 18 | + $key_details["expires_at"] = time() + (24 * 60 * 60); | |
| 19 | + | |
| 20 | + $settings->updateOption(self::$default_secret_key, $key_details); | |
| 21 | + | |
| 22 | + return $key_details["key"]; | |
| 23 | + } | |
| 24 | + | |
| 25 | + | |
| 16 | 26 | public static function deleteDefaultSecret($settings) { |
| 17 | 27 | $settings->deleteOption(self::$default_secret_key); |
| 18 | 28 | } |
| 19 | 29 | |
| 20 | 30 | public static function getDefaultSecret($settings) { |
| 21 | - return $settings->getOption(self::$default_secret_key); | |
| 31 | + $key_details = $settings->getOption(self::$default_secret_key); | |
| 32 | + | |
| 33 | + if (is_array($key_details) && $key_details["expires_at"] > time()) { | |
| 34 | + return $key_details["key"]; | |
| 35 | + } | |
| 36 | + | |
| 37 | + return null; | |
| 22 | 38 | } |
| 23 | 39 | |
| 24 | - public static function updateDefaultSecret($settings, $secret) { | |
| 25 | - $settings->updateOption(self::$default_secret_key, $secret); | |
| 40 | + public static function getSecretStatus($settings) { | |
| 41 | + $key_details = $settings->getOption(self::$default_secret_key); | |
| 42 | + $status = 'ACTIVE'; | |
| 43 | + if (!is_array($key_details)) { | |
| 44 | + $status = 'DELETED'; | |
| 45 | + } elseif ($key_details["expires_at"] <= time()) { | |
| 46 | + $status = 'EXPIRED'; | |
| 47 | + } | |
| 48 | + | |
| 49 | + return $status; | |
| 26 | 50 | } |
| 27 | 51 | |
| 28 | - public static function validate($pubkey) { | |
| 29 | - if ($pubkey && strlen($pubkey) >= 32) { | |
| 30 | - return true; | |
| 31 | - } else { | |
| 32 | - return false; | |
| 33 | - } | |
| 52 | + public static function validate($key) { | |
| 53 | + return $key && strlen($key) >= 32; | |
| 34 | 54 | } |
| 35 | 55 | |
| 36 | 56 | public static function find($settings, $pubkey) { |
| 37 | 57 | if (!self::validate($pubkey)) { |
| @@ -36,12 +56,15 @@ | ||
| 36 | 56 | public static function find($settings, $pubkey) { |
| 37 | 57 | if (!self::validate($pubkey)) { |
| 38 | 58 | return null; |
| 39 | 59 | } |
| 60 | + | |
| 40 | 61 | $secret = self::getDefaultSecret($settings); |
| 41 | - if (!empty($secret) && (strlen($secret) >= 32)) { | |
| 42 | - $account = new WPRAccount($settings, $pubkey, $secret); | |
| 62 | + if (!self::validate($secret)) { | |
| 63 | + return null; | |
| 43 | 64 | } |
| 65 | + | |
| 66 | + $account = new WPRAccount($settings, $pubkey, $secret); | |
| 44 | 67 | return $account; |
| 45 | 68 | } |
| 46 | 69 | } |
| 47 | 70 | endif; |