PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.5.1
Defender Security – Malware Scanner, Login Security & Firewall v5.5.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / framework / component / class-request.php

class-request.php in Defender Security – Malware Scanner, Login Security & Firewall 5.5.1, at framework/component/class-request.php

109 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Request class.
4 *
5 * @package Calotes\Component
6 */
7
8 namespace Calotes\Component;
9
10 use Calotes\Base\Model;
11 use Calotes\Helper\HTTP;
12
13 /**
14 * Parse and validate request data.
15 */
16 class Request {
17
18 /**
19 * Store the data from request.
20 *
21 * @var array
22 */
23 protected $data = array();
24
25 /**
26 * Constructor to initialize the object with data from the POST or GET request.
27 */
28 public function __construct() {
29 if ( 'POST' === defender_get_data_from_request( 'REQUEST_METHOD', 's' ) ) {
30 $this->data = HTTP::post( 'data', '' );
31 } else {
32 $this->data = HTTP::get( 'data', '' );
33 }
34 }
35
36 /**
37 * Retrieve the data that will be in use, it's recommended that $filters should be provided for data validation and
38 * cast.
39 *
40 * @param array $filters The filters to apply.
41 *
42 * @return array
43 */
44 public function get_data( $filters = array() ) {
45 if ( empty( $filters ) ) {
46 return $this->data;
47 }
48 $data = array();
49 foreach ( $filters as $key => $rule ) {
50 if ( ! isset( $this->data[ $key ] ) ) {
51 continue; // Moving on.
52 }
53 // Mandatory.
54 $type = $rule['type'];
55 $sanitize = $rule['sanitize'] ?? null;
56
57 $value = $this->data[ $key ];
58 // Cast.
59 settype( $value, $type );
60 if ( ! is_array( $sanitize ) ) {
61 $sanitize = array( $sanitize );
62 }
63 foreach ( $sanitize as $function ) {
64 if ( null !== $function && function_exists( $function ) ) {
65 if ( is_array( $value ) ) {
66 $value = $this->sanitize_array( $value, $function );
67 } else {
68 $value = $function( $value );
69 }
70 }
71 }
72 $data[ $key ] = $value;
73 }
74
75 return $data;
76 }
77
78 /**
79 * Sanitize an array recursively.
80 *
81 * @param array $arr The array to sanitize.
82 * @param mixed $sanitize The sanitization function to apply.
83 *
84 * @return array The sanitized array.
85 */
86 protected function sanitize_array( $arr, $sanitize ) {
87 foreach ( $arr as &$value ) {
88 if ( is_array( $value ) ) {
89 $value = $this->sanitize_array( $value, $sanitize );
90 } else {
91 $value = $sanitize( $value );
92 }
93 }
94
95 return $arr;
96 }
97
98 /**
99 * Get the data from _REQUEST.
100 *
101 * @param Model $model The model to get data from.
102 *
103 * @return array
104 */
105 public function get_data_by_model( Model $model ) {
106 return $this->get_data( $model->annotations );
107 }
108 }
109