class-wt-security-helper.php
5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Webtoffee Security Library
*
* Includes Data sanitization, Access checking
* @author WebToffee <info@webtoffee.com>
*/
if(!class_exists('Wt_Iew_Sh'))
{
class Wt_Iew_Sh
{
/**
* Data sanitization function.
*
* @param mixed $val value to sanitize
* @param string $key array key in the validation rule
* @param array $validation_rule array of validation rules. Eg: array('field_key' => array('type' => 'textarea'))
* @return mixed sanitized value
*/
public static function sanitize_data($val, $key, $validation_rule = array())
{
if(isset($validation_rule[$key]) && is_array($validation_rule[$key])) /* rule declared/exists */
{
if(isset($validation_rule[$key]['type']))
{
$val = self::sanitize_item($val, $validation_rule[$key]['type']);
}
}else //if no rule is specified then it will be treated as text
{
$val = self::sanitize_item($val, 'text');
}
return $val;
}
/**
* Sanitize individual data item
*
* @param mixed $val value to sanitize
* @param string $type value type
* @return mixed sanitized value
*/
public static function sanitize_item($val, $type='')
{
switch ($type)
{
case 'text':
$val = sanitize_text_field($val);
break;
case 'text_arr':
$val = self::sanitize_arr($val);
break;
case 'url':
$val = esc_url_raw($val);
break;
case 'url_arr':
$val = self::sanitize_arr($val, 'url');
break;
case 'sanitize_title_with_dashes':
$val = sanitize_title_with_dashes($val);
break;
case 'sanitize_title_with_dashes_arr':
$val = self::sanitize_arr($val, 'sanitize_title_with_dashes');
break;
case 'textarea':
$val=sanitize_textarea_field($val);
break;
case 'int':
$val = intval($val);
break;
case 'int_arr':
$val = self::sanitize_arr($val, 'int');
break;
case 'absint':
$val = absint($val);
break;
case 'absint_arr':
$val = self::sanitize_arr($val, 'absint');
break;
case 'float':
$val = floatval($val);
break;
case 'post_content':
$val = wp_kses_post($val);
break;
case 'hex':
$val = sanitize_hex_color($val);
break;
case 'skip': /* skip the validation */
$val = $val;
break;
case 'file_name':
$val = sanitize_file_name($val);
break;
default:
$val = sanitize_text_field($val);
}
return $val;
}
/**
* Recursive array sanitization function
*
* @param mixed $arr value to sanitize
* @param string $type value type
* @return mixed sanitized value
*/
public static function sanitize_arr($arr, $type = 'text')
{
if(is_array($arr))
{
$out = array();
foreach($arr as $k=>$arrv)
{
if(is_array($arrv))
{
$out[$k] = self::sanitize_arr($arrv, $type);
}else
{
$out[$k] = self::sanitize_item($arrv, $type);
}
}
return $out;
}else
{
return self::sanitize_item($arr, $type);
}
}
/**
* User accessibility. Function checks user logged in status, nonce and role access.
*
* @param string $plugin_id unique plugin id. Note: This id is used as an identifier in filter name so please use characters allowed in filters
* @param string $nonce_id Nonce id. If not specified then uses plugin id
* @return boolean if user allowed or not
*/
public static function check_write_access($plugin_id, $nonce_id = '')
{
$er = true;
if(!is_user_logged_in()) //checks user is logged in
{
$er = false;
}
if($er === true) //no error then proceed
{
if(!(self::verify_nonce($plugin_id, $nonce_id))) //verifying nonce
{
$er = false;
}else
{
if(!self::check_role_access($plugin_id)) //Check user role
{
$er = false;
}
}
}
return $er;
}
/**
* Verifying nonce
*
* @param string $plugin_id unique plugin id. Note: This id is used as an identifier in filter name so please use characters allowed in filters
* @param string $nonce_id Nonce id. If not specified then uses plugin id
* @return boolean if user allowed or not
*/
public static function verify_nonce($plugin_id, $nonce_id = '')
{
$nonce = (isset($_REQUEST['_wpnonce']) ? sanitize_text_field($_REQUEST['_wpnonce']) : '');
$nonce = (is_array($nonce) ? $nonce[0] : $nonce); //in some cases multiple nonces are declared
$nonce_id = ($nonce_id == "" ? $plugin_id : $nonce_id); //if nonce id not provided then uses plugin id as nonce id
if(!(wp_verify_nonce($nonce, $nonce_id))) //verifying nonce
{
return false;
}else
{
return true;
}
}
/**
* Checks if user role has access
*
* @param string $plugin_id unique plugin id. Note: This id is used as an identifier in filter name so please use characters allowed in filters
* @return boolean if user allowed or not
*/
public static function check_role_access($plugin_id)
{
$roles = array('manage_options', 'shop_manager');
$roles = apply_filters('wt_'.$plugin_id.'_alter_role_access_basic', $roles); //dynamic filter based on plugin id to alter roles
$roles = (!is_array($roles) ? array() : $roles);
$is_allowed = false;
foreach($roles as $role) //loop through roles
{
if(current_user_can($role))
{
$is_allowed = true;
break;
}
}
return $is_allowed;
}
}
}