interfaces
2 years ago
traits
2 years ago
match-not-regexp-rule.php
2 years ago
match-regexp-rule.php
2 years ago
must-contain-characters-rule.php
2 years ago
must-equal-rule.php
2 years ago
must-not-contain-characters-rule.php
2 years ago
rule.php
2 years ago
server-side-rule.php
1 year ago
server-side-rule.php
111 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Validation\Advanced_Rules; |
| 5 | |
| 6 | use JFB_Modules\Validation\Ssr; |
| 7 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 8 | use JFB_Components\Repository\Repository_Pattern_Trait; |
| 9 | use JFB_Modules\Block_Parsers\Field_Data_Parser; |
| 10 | |
| 11 | // If this file is called directly, abort. |
| 12 | if ( ! defined( 'WPINC' ) ) { |
| 13 | die; |
| 14 | } |
| 15 | |
| 16 | class Server_Side_Rule extends Rule { |
| 17 | |
| 18 | use Repository_Pattern_Trait; |
| 19 | |
| 20 | const NOT_ALLOWED = array( |
| 21 | 'var_dump', |
| 22 | 'var_export', |
| 23 | 'print_r', |
| 24 | 'sprintf', |
| 25 | 'printf', |
| 26 | 'shell_exec', |
| 27 | 'system', |
| 28 | 'exec', |
| 29 | 'unserialize', |
| 30 | 'file_get_contents', |
| 31 | 'maybe_unserialize', |
| 32 | ); |
| 33 | |
| 34 | public function __construct() { |
| 35 | $this->rep_install(); |
| 36 | } |
| 37 | |
| 38 | public function rep_instances(): array { |
| 39 | return apply_filters( |
| 40 | 'jet-form-builder/validation-callbacks', |
| 41 | array( |
| 42 | new Ssr\Is_User_Login_Unique(), |
| 43 | new Ssr\Is_User_Email_Unique(), |
| 44 | new Ssr\Is_Field_Value_Unique(), |
| 45 | new Ssr\Is_User_Password_Valid(), |
| 46 | ) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | public function get_id(): string { |
| 51 | return 'ssr'; |
| 52 | } |
| 53 | |
| 54 | public function get_label(): string { |
| 55 | return __( 'Server-Side callback', 'jet-form-builder' ); |
| 56 | } |
| 57 | |
| 58 | public function validate_field( Field_Data_Parser $parser ) { |
| 59 | $function_name = $this->get_setting( 'value' ); |
| 60 | $is_valid = $this->validate( $parser, $function_name ); |
| 61 | |
| 62 | if ( $is_valid ) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | $parser->collect_error( 'rule:ssr:' . $function_name, $this->get_setting( 'message' ) ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @return Ssr\Base_Validation_Callback[] |
| 71 | */ |
| 72 | public function get_callbacks(): array { |
| 73 | return $this->rep_get_items(); |
| 74 | } |
| 75 | |
| 76 | protected function validate( Field_Data_Parser $parser, string $function_name ): bool { |
| 77 | try { |
| 78 | /** @var Ssr\Base_Validation_Callback $callback */ |
| 79 | $callback = $this->rep_get_item( $function_name ); |
| 80 | } catch ( Repository_Exception $exception ) { |
| 81 | return $this->validate_custom( $parser, $function_name ); |
| 82 | } |
| 83 | |
| 84 | return $callback->is_valid( $parser->get_value(), $parser->get_context() ); |
| 85 | } |
| 86 | |
| 87 | protected function validate_custom( Field_Data_Parser $parser, string $function_name ): bool { |
| 88 | $name = $this->validate_callback( $function_name ); |
| 89 | |
| 90 | if ( ! $name ) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | return (bool) call_user_func( $name, $parser->get_value(), $parser->get_context() ); |
| 95 | } |
| 96 | |
| 97 | protected function validate_callback( string $function_name ): string { |
| 98 | $name = preg_replace( '/[^\w]/i', '', $function_name ); |
| 99 | |
| 100 | if ( $name !== $function_name || |
| 101 | // not in the blacklist |
| 102 | in_array( $name, self::NOT_ALLOWED, true ) |
| 103 | ) { |
| 104 | return ''; |
| 105 | } |
| 106 | |
| 107 | return function_exists( $name ) ? $name : ''; |
| 108 | } |
| 109 | |
| 110 | } |
| 111 |