*/ private $values; /** * Constructor. */ public function __construct() { $this->reload(); } /** * Re-reads the option (used after imports). * * @return void */ public function reload() { $stored = get_option( CCLG_OPTION, array() ); $this->values = array_merge( self::defaults(), is_array( $stored ) ? $stored : array() ); } /** * Field schema: type, default and limits for every setting. * * @return array> */ public static function schema() { return array( // Limits. 'retries' => array( 'type' => 'int', 'default' => 4, 'min' => 1, 'max' => 50, ), 'lockout_minutes' => array( 'type' => 'int', 'default' => 20, 'min' => 1, 'max' => 1440, ), 'lockouts_to_long' => array( 'type' => 'int', 'default' => 4, 'min' => 1, 'max' => 100, ), 'long_hours' => array( 'type' => 'int', 'default' => 24, 'min' => 1, 'max' => 720, ), 'window_hours' => array( 'type' => 'int', 'default' => 12, 'min' => 1, 'max' => 168, ), 'show_remaining' => array( 'type' => 'bool', 'default' => true, ), // IP rules. 'ip_source' => array( 'type' => 'choice', 'default' => 'remote_addr', 'options' => array( 'remote_addr', 'cf', 'xff', 'xri' ), ), 'allowlist' => array( 'type' => 'ips', 'default' => '', ), 'denylist' => array( 'type' => 'ips', 'default' => '', ), // Turnstile. 'ts_site_key' => array( 'type' => 'key', 'default' => '', ), 'ts_secret_key' => array( 'type' => 'key', 'default' => '', ), 'ts_theme' => array( 'type' => 'choice', 'default' => 'auto', 'options' => array( 'auto', 'light', 'dark' ), ), 'ts_login' => array( 'type' => 'bool', 'default' => true, ), 'ts_register' => array( 'type' => 'bool', 'default' => true, ), 'ts_lostpassword' => array( 'type' => 'bool', 'default' => true, ), 'ts_woo' => array( 'type' => 'bool', 'default' => true, ), 'ts_fail_open' => array( 'type' => 'bool', 'default' => true, ), // Recovery and alerts. 'recovery_enabled' => array( 'type' => 'bool', 'default' => true, ), 'notify' => array( 'type' => 'choice', 'default' => 'long', 'options' => array( 'off', 'long', 'all' ), ), 'notify_email' => array( 'type' => 'email', 'default' => '', ), 'log_days' => array( 'type' => 'int', 'default' => 30, 'min' => 1, 'max' => 365, ), ); } /** * Default values. * * @return array */ public static function defaults() { $out = array(); foreach ( self::schema() as $key => $field ) { $out[ $key ] = $field['default']; } return $out; } /** * Returns a value. * * @param string $key Key. * @return mixed */ public function get( $key ) { return array_key_exists( $key, $this->values ) ? $this->values[ $key ] : null; } /** * All values. * * @return array */ public function all() { return $this->values; } /** * Sanitise callback for register_setting(). Builds a complete, typed array from the * schema, so unknown keys are dropped and unticked checkboxes become false. * * @param mixed $input Raw posted value. * @return array */ public function sanitize( $input ) { $input = is_array( $input ) ? wp_unslash( $input ) : array(); $clean = array(); foreach ( self::schema() as $key => $field ) { $raw = isset( $input[ $key ] ) ? $input[ $key ] : null; switch ( $field['type'] ) { case 'int': $n = is_numeric( $raw ) ? (int) $raw : (int) $field['default']; $clean[ $key ] = max( (int) $field['min'], min( (int) $field['max'], $n ) ); break; case 'bool': $clean[ $key ] = ! empty( $raw ); break; case 'choice': $raw = is_string( $raw ) ? sanitize_key( $raw ) : ''; $clean[ $key ] = in_array( $raw, $field['options'], true ) ? $raw : $field['default']; break; case 'ips': $clean[ $key ] = implode( "\n", Ip::parse_list( is_string( $raw ) ? $raw : '' ) ); break; case 'key': $clean[ $key ] = is_string( $raw ) ? preg_replace( '/[^A-Za-z0-9_\-]/', '', $raw ) : ''; break; case 'email': $clean[ $key ] = is_string( $raw ) ? (string) sanitize_email( $raw ) : ''; break; } } // Never let an administrator block their own address from the settings screen. $ip = new Ip( $this ); $me = $ip->client_ip(); if ( '' !== $clean['denylist'] && $me && Ip::in_list( $me, $clean['denylist'] ) ) { $kept = array(); foreach ( explode( "\n", $clean['denylist'] ) as $entry ) { if ( ! Ip::in_list( $me, $entry ) ) { $kept[] = $entry; } } $clean['denylist'] = implode( "\n", $kept ); if ( function_exists( 'add_settings_error' ) ) { add_settings_error( CCLG_OPTION, 'cclg_self_block', sprintf( /* translators: %s: the administrator's IP address. */ __( 'Your own IP address (%s) was removed from the block list so you cannot lock yourself out.', 'codingcush-login-guard' ), $me ), 'warning' ); } } return $clean; } }