csrf-token-model.php
3 years ago
csrf-token-view.php
3 years ago
csrf-tools.php
3 years ago
wp-nonce-tools.php
3 years ago
csrf-token-model.php
70 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Classes\Security; |
| 5 | |
| 6 | use Jet_Form_Builder\Db_Queries\Base_Db_Model; |
| 7 | |
| 8 | class Csrf_Token_Model extends Base_Db_Model { |
| 9 | |
| 10 | /** |
| 11 | * @return string |
| 12 | */ |
| 13 | public static function table_name(): string { |
| 14 | return 'csrf_tokens'; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * @return array |
| 19 | */ |
| 20 | public static function schema(): array { |
| 21 | return array( |
| 22 | self::PRIMARY_ID => 'bigint(20) NOT NULL AUTO_INCREMENT', |
| 23 | 'client_id' => 'varchar(155)', |
| 24 | 'token' => 'text', |
| 25 | self::CREATED_AT => 'TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP', |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | public function schema_engine() { |
| 30 | return self::MyISAM; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @return array |
| 35 | */ |
| 36 | public static function schema_keys(): array { |
| 37 | return array( |
| 38 | 'id' => 'primary key', |
| 39 | 'client_id' => 'unique', |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | public static function clear() { |
| 44 | global $wpdb; |
| 45 | |
| 46 | /** @var \DateTimeImmutable $datetime_limit */ |
| 47 | $datetime_limit = apply_filters( |
| 48 | 'jet-form-builder/security/csrf-token/datetime-limit', |
| 49 | current_datetime()->modify( '-4 hours' ) |
| 50 | ); |
| 51 | |
| 52 | $self = ( new static() )->create(); |
| 53 | |
| 54 | // phpcs:disable WordPress.DB |
| 55 | $wpdb->query( |
| 56 | $wpdb->prepare( |
| 57 | " |
| 58 | DELETE FROM {$self::table()} |
| 59 | WHERE 1=1 |
| 60 | AND TIMESTAMP(`created_at`) <= TIMESTAMP(%s) |
| 61 | ", |
| 62 | $datetime_limit->format( 'Y-m-d H:i:s' ) |
| 63 | ) |
| 64 | ); |
| 65 | // phpcs:enable WordPress.DB |
| 66 | } |
| 67 | |
| 68 | |
| 69 | } |
| 70 |