| @@ -2,8 +2,53 @@ | ||
| 2 | 2 | if (!defined('ABSPATH') && !defined('MCDATAPATH') && !defined('PHP_ERR_MONIT_PATH')) exit; |
| 3 | 3 | |
| 4 | 4 | if (!class_exists('WPRHelper')) : |
| 5 | 5 | class WPRHelper { |
| 6 | + const MIN_SALT_LENGTH = 32; | |
| 7 | + const SALT_PLACEHOLDER = 'put your unique phrase here'; | |
| 8 | + const SALT_CONSTANTS = array( | |
| 9 | + 'AUTH_KEY', 'AUTH_SALT', 'SECURE_AUTH_KEY', 'SECURE_AUTH_SALT', | |
| 10 | + 'LOGGED_IN_KEY', 'LOGGED_IN_SALT', 'NONCE_KEY', 'NONCE_SALT' | |
| 11 | + ); | |
| 12 | + | |
| 13 | + public static function configSalt($constant) { | |
| 14 | + if (!defined($constant)) { | |
| 15 | + return null; | |
| 16 | + } | |
| 17 | + | |
| 18 | + $value = constant($constant); | |
| 19 | + if (!is_string($value) || strlen($value) < self::MIN_SALT_LENGTH) { | |
| 20 | + return null; | |
| 21 | + } | |
| 22 | + | |
| 23 | + if (self::isSaltPlaceholder($value) || self::isSharedSalt($constant, $value)) { | |
| 24 | + return null; | |
| 25 | + } | |
| 26 | + | |
| 27 | + return $value; | |
| 28 | + } | |
| 29 | + | |
| 30 | + private static function isSaltPlaceholder($value) { | |
| 31 | + if ($value === self::SALT_PLACEHOLDER) { | |
| 32 | + return true; | |
| 33 | + } | |
| 34 | + | |
| 35 | + #wp-config-sample.php is localized for some locales, so the placeholder is | |
| 36 | + #not always the English string. wp_salt() guards the translated form too. | |
| 37 | + // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 38 | + return function_exists('__') && $value === __('put your unique phrase here'); | |
| 39 | + } | |
| 40 | + | |
| 41 | + private static function isSharedSalt($constant, $value) { | |
| 42 | + foreach (self::SALT_CONSTANTS as $other) { | |
| 43 | + if ($other !== $constant && defined($other) && constant($other) === $value) { | |
| 44 | + return true; | |
| 45 | + } | |
| 46 | + } | |
| 47 | + | |
| 48 | + return false; | |
| 49 | + } | |
| 50 | + | |
| 6 | 51 | public static function safePregMatch($pattern, $subject, &$matches = null, $flags = 0, $offset = 0) { |
| 7 | 52 | if (!is_string($pattern) || !is_string($subject)) { |
| 8 | 53 | return false; |
| 9 | 54 | } |
| @@ -72,8 +117,21 @@ | ||
| 72 | 117 | if ($updated_subject === null) { |
| 73 | 118 | return $subject; |
| 74 | 119 | } |
| 75 | 120 | return $updated_subject; |
| 121 | + } | |
| 122 | + | |
| 123 | + public static function safeStrReplaceFirst($search, $replace, $subject) { | |
| 124 | + if (!is_string($search) || !is_string($replace) || !is_string($subject) || $search === '') { | |
| 125 | + return $subject; | |
| 126 | + } | |
| 127 | + | |
| 128 | + $position = strpos($subject, $search); | |
| 129 | + if ($position === false) { | |
| 130 | + return $subject; | |
| 131 | + } | |
| 132 | + | |
| 133 | + return substr_replace($subject, $replace, $position, strlen($search)); | |
| 76 | 134 | } |
| 77 | 135 | |
| 78 | 136 | public static function preInitWPHook($hook_name, $function_name, $priority, $accepted_args) { |
| 79 | 137 | global $wp_filter; |