| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) exit; |
| 3 |
if (!class_exists('WPRRecover')) : |
| 4 |
class WPRRecover { |
| 5 |
public static $default_secret_key = 'bv_default_secret_key'; |
| 6 |
|
| 7 |
public static function defaultSecret($settings) { |
| 8 |
$secret = self::getDefaultSecret($settings); |
| 9 |
if (empty($secret)) { |
| 10 |
$secret = WPRRecover::refreshDefaultSecret($settings); |
| 11 |
} |
| 12 |
return $secret; |
| 13 |
} |
| 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 |
|
| 26 |
public static function deleteDefaultSecret($settings) { |
| 27 |
$settings->deleteOption(self::$default_secret_key); |
| 28 |
} |
| 29 |
|
| 30 |
public static function getDefaultSecret($settings) { |
| 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; |
| 38 |
} |
| 39 |
|
| 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; |
| 50 |
} |
| 51 |
|
| 52 |
public static function validate($key) { |
| 53 |
return $key && strlen($key) >= 32; |
| 54 |
} |
| 55 |
|
| 56 |
public static function find($settings, $pubkey) { |
| 57 |
if (!self::validate($pubkey)) { |
| 58 |
return null; |
| 59 |
} |
| 60 |
|
| 61 |
$secret = self::getDefaultSecret($settings); |
| 62 |
if (!self::validate($secret)) { |
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
$account = new WPRAccount($settings, $pubkey, $secret); |
| 67 |
return $account; |
| 68 |
} |
| 69 |
} |
| 70 |
endif; |