base-validation-callback.php
2 years ago
is-user-email-unique.php
2 years ago
is-user-login-unique.php
2 years ago
rest-controller.php
2 years ago
rest-validation-endpoint.php
2 years ago
validation-callbacks.php
2 years ago
validation-callbacks.php
89 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Blocks\Ssr_Validation; |
| 5 | |
| 6 | use JFB_Components\Repository\Repository_Pattern_Trait; |
| 7 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 8 | use JFB_Modules\Block_Parsers\Field_Data_Parser; |
| 9 | |
| 10 | // If this file is called directly, abort. |
| 11 | if ( ! defined( 'WPINC' ) ) { |
| 12 | die; |
| 13 | } |
| 14 | |
| 15 | class Validation_Callbacks { |
| 16 | |
| 17 | use Repository_Pattern_Trait; |
| 18 | |
| 19 | const NOT_ALLOWED = array( |
| 20 | 'var_dump', |
| 21 | 'var_export', |
| 22 | 'print_r', |
| 23 | 'sprintf', |
| 24 | 'printf', |
| 25 | 'shell_exec', |
| 26 | 'system', |
| 27 | 'exec', |
| 28 | 'unserialize', |
| 29 | 'file_get_contents', |
| 30 | 'maybe_unserialize', |
| 31 | ); |
| 32 | |
| 33 | public function __construct() { |
| 34 | $this->rep_install(); |
| 35 | } |
| 36 | |
| 37 | public function rep_instances(): array { |
| 38 | return apply_filters( |
| 39 | 'jet-form-builder/validation-callbacks', |
| 40 | array( |
| 41 | new Is_User_Login_Unique(), |
| 42 | new Is_User_Email_Unique(), |
| 43 | ) |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return Base_Validation_Callback[] |
| 49 | */ |
| 50 | public function get_items(): array { |
| 51 | return $this->rep_get_items(); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | public function validate( Field_Data_Parser $parser, string $function_name ): bool { |
| 56 | try { |
| 57 | /** @var Base_Validation_Callback $callback */ |
| 58 | $callback = $this->rep_get_item( $function_name ); |
| 59 | } catch ( Repository_Exception $exception ) { |
| 60 | return $this->validate_custom( $parser, $function_name ); |
| 61 | } |
| 62 | |
| 63 | return $callback->is_valid( $parser->get_value(), $parser->get_context() ); |
| 64 | } |
| 65 | |
| 66 | protected function validate_custom( Field_Data_Parser $parser, string $function_name ): bool { |
| 67 | $name = $this->validate_callback( $function_name ); |
| 68 | |
| 69 | if ( ! $name ) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | return (bool) call_user_func( $name, $parser->get_value(), $parser->get_context() ); |
| 74 | } |
| 75 | |
| 76 | protected function validate_callback( string $function_name ): string { |
| 77 | $name = preg_replace( '/[^\w]/i', '', $function_name ); |
| 78 | |
| 79 | if ( $name !== $function_name || |
| 80 | // not in the blacklist |
| 81 | in_array( $name, self::NOT_ALLOWED, true ) |
| 82 | ) { |
| 83 | return ''; |
| 84 | } |
| 85 | |
| 86 | return function_exists( $name ) ? $name : ''; |
| 87 | } |
| 88 | } |
| 89 |