1
|
|
|
<?php
|
2
|
|
|
/*
|
3
|
|
|
Plugin Name: Vendi Shared Utility Class
|
4
|
|
|
Description: Helper class shared across all Vendi-controlled properties.
|
5
|
|
|
Version: 3.0.4
|
6
|
|
|
Author: Vendi
|
7
|
|
|
*/
|
8
|
|
|
|
9
|
|
|
namespace Vendi\Shared;
|
10
|
|
|
|
11
|
|
|
class utils
|
12
|
|
|
{
|
13
|
|
|
public static $CUSTOM_POST = null;
|
14
|
|
|
|
15
|
|
|
public static $CUSTOM_GET = null;
|
16
|
|
|
|
17
|
|
|
public static $CUSTOM_COOKIE = null;
|
18
|
|
|
|
19
|
|
|
public static $CUSTOM_SERVER = null;
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* Reset the internal arrays to default values.
|
23
|
|
|
*/
|
24
|
|
|
public static function reset_all_custom_arrays()
|
25
|
|
|
{
|
26
|
|
|
self::$CUSTOM_POST = null;
|
27
|
|
|
self::$CUSTOM_GET = null;
|
28
|
|
|
self::$CUSTOM_COOKIE = null;
|
29
|
|
|
self::$CUSTOM_SERVER = null;
|
30
|
|
|
}
|
31
|
|
|
|
32
|
|
|
/**
|
33
|
|
|
* Get the value from the HTTP POST return the $default_value.
|
34
|
|
|
* @param string $key The form field's name to search in the $_POST array for.
|
35
|
|
|
* @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
|
36
|
|
|
* @return mixed The value of the HTTP POST for the given $key or the $default.
|
37
|
|
|
*/
|
38
|
|
|
public static function get_post_value( $key, $default_value = '' )
|
39
|
|
|
{
|
40
|
|
|
return self::get_request_value( 'POST', $key, $default_value );
|
41
|
|
|
}
|
42
|
|
|
|
43
|
|
|
/**
|
44
|
|
|
* Get the value from the HTTP GET return the $default_value.
|
45
|
|
|
* @param string $key The form field's name to search in the $_GET array for.
|
46
|
|
|
* @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
|
47
|
|
|
* @return mixed The value of the HTTP GET for the given $key or the $default.
|
48
|
|
|
*/
|
49
|
|
|
public static function get_get_value( $key, $default_value = '' )
|
50
|
|
|
{
|
51
|
|
|
return self::get_request_value( 'GET', $key, $default_value );
|
52
|
|
|
}
|
53
|
|
|
|
54
|
|
|
/**
|
55
|
|
|
* Get the value from the HTTP COOKIE return the $default_value.
|
56
|
|
|
* @param string $key The form field's name to search in the $_COOKIE array for.
|
57
|
|
|
* @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
|
58
|
|
|
* @return mixed The value of the HTTP COOKIE for the given $key or the $default.
|
59
|
|
|
*/
|
60
|
|
|
public static function get_cookie_value( $key, $default_value = '' )
|
61
|
|
|
{
|
62
|
|
|
return self::get_request_value( 'COOKIE', $key, $default_value );
|
63
|
|
|
}
|
64
|
|
|
|
65
|
|
|
/**
|
66
|
|
|
* Get the value from the HTTP SERVER return the $default_value.
|
67
|
|
|
* @param string $key The form field's name to search in the $_SERVER array for.
|
68
|
|
|
* @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
|
69
|
|
|
* @return mixed The value of the HTTP SERVER for the given $key or the $default.
|
70
|
|
|
*/
|
71
|
|
|
public static function get_server_value( $key, $default_value = '' )
|
72
|
|
|
{
|
73
|
|
|
return self::get_request_value( 'SERVER', $key, $default_value );
|
74
|
|
|
}
|
75
|
|
|
|
76
|
|
|
/**
|
77
|
|
|
* Get the first non-null value from the supplied list of sources.
|
78
|
|
|
*
|
79
|
|
|
* @param string $key The form field's name to search in the each source array for.
|
80
|
|
|
* @param array $sources Array of sources (GET, POST, SERVER, or COOKIE) in the order to check.
|
81
|
|
|
* @param mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
|
82
|
|
|
* @return mixed The value of the source for the given $key or the $default.
|
83
|
|
|
*/
|
84
|
|
|
public static function get_value_multiple_sources( $key, array $sources, $default_value = null )
|
85
|
|
|
{
|
86
|
|
|
foreach( $sources as $source )
|
87
|
|
|
{
|
88
|
|
|
$value = self::get_request_value( $source, $key, null );
|
89
|
|
|
if( null !== $value )
|
90
|
|
|
{
|
91
|
|
|
return $value;
|
92
|
|
|
}
|
93
|
|
|
}
|
94
|
|
|
|
95
|
|
|
return $default_value;
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
/**
|
99
|
|
|
* Get the value from the HTTP POST as an integer or return the $default_value.
|
100
|
|
|
* @param string $key The form field's name to search in the $_POST array for.
|
101
|
|
|
* @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
|
102
|
|
|
* @return integer|mixed The value of the HTTP POST for the given $key or the $default.
|
103
|
|
|
*/
|
104
|
|
|
public static function get_post_value_int( $key, $default_value = null )
|
105
|
|
|
{
|
106
|
|
|
return self::get_request_value_int( 'POST', $key, $default_value );
|
107
|
|
|
}
|
108
|
|
|
|
109
|
|
|
/**
|
110
|
|
|
* Get the value from the HTTP GET as an integer or return the $default_value.
|
111
|
|
|
* @param string $key The form field's name to search in the $_GET array for.
|
112
|
|
|
* @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
|
113
|
|
|
* @return integer|mixed The value of the HTTP GET for the given $key or the $default.
|
114
|
|
|
*/
|
115
|
|
|
public static function get_get_value_int( $key, $default_value = null )
|
116
|
|
|
{
|
117
|
|
|
return self::get_request_value_int( 'GET', $key, $default_value );
|
118
|
|
|
}
|
119
|
|
|
|
120
|
|
|
/**
|
121
|
|
|
* Get the value from the HTTP COOKIE as an integer or return the $default_value.
|
122
|
|
|
* @param string $key The form field's name to search in the $_COOKIE array for.
|
123
|
|
|
* @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
|
124
|
|
|
* @return integer|mixed The value of the HTTP COOKIE for the given $key or the $default.
|
125
|
|
|
*/
|
126
|
|
|
public static function get_cookie_value_int( $key, $default_value = null )
|
127
|
|
|
{
|
128
|
|
|
return self::get_request_value_int( 'COOKIE', $key, $default_value );
|
129
|
|
|
}
|
130
|
|
|
|
131
|
|
|
/**
|
132
|
|
|
* Get the value from the HTTP SERVER as an integer or return the $default_value.
|
133
|
|
|
* @param string $key The form field's name to search in the $_SERVER array for.
|
134
|
|
|
* @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
|
135
|
|
|
* @return integer|mixed The value of the HTTP SERVER for the given $key or the $default.
|
136
|
|
|
*/
|
137
|
|
|
public static function get_server_value_int( $key, $default_value = null )
|
138
|
|
|
{
|
139
|
|
|
return self::get_request_value_int( 'SERVER', $key, $default_value );
|
140
|
|
|
}
|
141
|
|
|
|
142
|
|
|
/**
|
143
|
|
|
* Get the value from the supplied request as an integer or return the $default_value.
|
144
|
|
|
* @param string $request_method The server method to use, one of GET, POST, SERVER or COOKIE.
|
145
|
|
|
* @param string $key The form field's name to search in the $_SERVER array for.
|
146
|
|
|
* @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
|
147
|
|
|
* @return integer|mixed The value of the request method for the given $key or the $default.
|
148
|
|
|
*/
|
149
|
|
|
public static function get_request_value_int( $request_method, $key, $default_value = null )
|
150
|
|
|
{
|
151
|
|
|
$value = self::get_request_value( $request_method, $key, null );
|
152
|
|
|
if( self::is_integer_like( $value ) )
|
153
|
|
|
{
|
154
|
|
|
return (int)$value;
|
155
|
|
|
}
|
156
|
|
|
|
157
|
|
|
return $default_value;
|
158
|
|
|
}
|
159
|
|
|
|
160
|
|
|
/**
|
161
|
|
|
* Get the value from the supplied request or return the $default_value.
|
162
|
|
|
* @param string $request_method The server method to use, one of GET, POST, SERVER or COOKIE.
|
163
|
|
|
* @param string $key The form field's name to search in the $_SERVER array for.
|
164
|
|
|
* @param integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
|
165
|
|
|
* @return integer|mixed The value of the request method for the given $key or the $default.
|
166
|
|
|
*/
|
167
|
|
|
public static function get_request_value( $request_method, $key, $default_value = null )
|
168
|
|
|
{
|
169
|
|
|
$request_obj = self::get_request_object( $request_method );
|
170
|
|
|
|
171
|
|
|
if( null === $request_obj || ! is_array( $request_obj ) || ! array_key_exists( $key, $request_obj ) )
|
172
|
|
|
{
|
173
|
|
|
return $default_value;
|
174
|
|
|
}
|
175
|
|
|
|
176
|
|
|
$ret = $request_obj[ $key ];
|
177
|
|
|
|
178
|
|
|
if( is_string( $ret ) )
|
179
|
|
|
{
|
180
|
|
|
$ret = trim( $ret );
|
181
|
|
|
}
|
182
|
|
|
|
183
|
|
|
return $ret;
|
184
|
|
|
}
|
185
|
|
|
|
186
|
|
|
/**
|
187
|
|
|
* Get the current request array or mock array if set.
|
188
|
|
|
*
|
189
|
|
|
* @param string The server method to use, one of GET, POST, SERVER or COOKIE.
|
190
|
|
|
* @return arra|null The requested array or null.
|
|
|
|
|
191
|
|
|
*/
|
192
|
|
|
public static function get_request_object( $request_method )
|
|
|
|
|
193
|
|
|
{
|
194
|
|
|
$obj = null;
|
|
|
|
|
195
|
|
|
switch( $request_method )
|
196
|
|
|
{
|
197
|
|
|
case 'GET':
|
198
|
|
|
if( is_array( self::$CUSTOM_GET ) )
|
199
|
|
|
{
|
200
|
|
|
return self::$CUSTOM_GET;
|
201
|
|
|
}
|
202
|
|
|
return ( isset( $_GET ) && is_array( $_GET ) && count( $_GET ) > 0 ? $_GET : null );
|
203
|
|
|
|
204
|
|
|
case 'POST':
|
205
|
|
|
if( is_array( self::$CUSTOM_POST ) )
|
206
|
|
|
{
|
207
|
|
|
return self::$CUSTOM_POST;
|
208
|
|
|
}
|
209
|
|
|
return ( isset( $_POST ) && is_array( $_POST ) && count( $_POST ) > 0 ? $_POST : null );
|
210
|
|
|
|
211
|
|
|
case 'COOKIE':
|
212
|
|
|
if( is_array( self::$CUSTOM_COOKIE ) )
|
213
|
|
|
{
|
214
|
|
|
return self::$CUSTOM_COOKIE;
|
215
|
|
|
}
|
216
|
|
|
return ( isset( $_COOKIE ) && is_array( $_COOKIE ) && count( $_COOKIE ) > 0 ? $_COOKIE : null );
|
217
|
|
|
|
218
|
|
|
case 'SERVER':
|
219
|
|
|
if( is_array( self::$CUSTOM_SERVER ) )
|
220
|
|
|
{
|
221
|
|
|
return self::$CUSTOM_SERVER;
|
222
|
|
|
}
|
223
|
|
|
return ( isset( $_SERVER ) && is_array( $_SERVER ) && count( $_SERVER ) > 0 ? $_SERVER : null );
|
224
|
|
|
|
225
|
|
|
default:
|
226
|
|
|
return null;
|
227
|
|
|
}
|
228
|
|
|
}
|
229
|
|
|
|
230
|
|
|
/**
|
231
|
|
|
* Test if we're in a certain type of HTTP request.
|
232
|
|
|
*
|
233
|
|
|
* @param string $method The server method to test for. Generally one of GET, POST, HEAD, PUT, DELETE.
|
234
|
|
|
* @return boolean Returns true if the REQUEST_METHOD server variable is set to the supplied $method, otherwise false.
|
235
|
|
|
*/
|
236
|
|
|
public static function is_request_method( $method )
|
237
|
|
|
{
|
238
|
|
|
return $method === self::get_server_value( 'REQUEST_METHOD' );
|
239
|
|
|
}
|
240
|
|
|
|
241
|
|
|
/**
|
242
|
|
|
* Check to see if we're in a post.
|
243
|
|
|
*
|
244
|
|
|
* Unit tests were failing because REQUEST_METHOD wasn't always being set. This should be used
|
245
|
|
|
* for all POST checks.
|
246
|
|
|
*
|
247
|
|
|
* \Vendi\Forms\utils::is_post()
|
248
|
|
|
*
|
249
|
|
|
* @return boolean Returns true if the REQUEST_METHOD server variable is set to POST, otherwise false.
|
250
|
|
|
*/
|
251
|
|
|
public static function is_post( )
|
252
|
|
|
{
|
253
|
|
|
return self::is_request_method( 'POST' );
|
254
|
|
|
}
|
255
|
|
|
|
256
|
|
|
/**
|
257
|
|
|
* Test if the given $input can be converted to an int excluding booleans.
|
258
|
|
|
*
|
259
|
|
|
* \Vendi\Forms\utils::is_integer_like( value )
|
260
|
|
|
*
|
261
|
|
|
* @param mixed $input The value to test.
|
262
|
|
|
* @return boolean True if $input is an integer or a string that contains only digits possibly starting with a dash.
|
263
|
|
|
*/
|
264
|
|
|
public static function is_integer_like( $input )
|
265
|
|
|
{
|
266
|
|
|
return
|
267
|
|
|
is_int( $input )
|
268
|
|
|
||
|
269
|
|
|
(
|
270
|
|
|
is_string( $input )
|
271
|
|
|
&&
|
272
|
|
|
preg_match( '/^-?([0-9])+$/', $input )
|
273
|
|
|
);
|
274
|
|
|
}
|
275
|
|
|
|
276
|
|
|
/**
|
277
|
|
|
* Convert the result of urlpieces back to a URL.
|
278
|
|
|
*
|
279
|
|
|
* @see http://php.net/manual/en/function.parse-url.php#106731
|
280
|
|
|
*
|
281
|
|
|
* @param array $parsed_url An array created by urlpieces.
|
282
|
|
|
* @return string A URL string.
|
283
|
|
|
*/
|
284
|
|
|
public static function unparse_url( $parsed_url ) {
|
285
|
|
|
//I don't know what you gave me so you can just have it back
|
286
|
|
|
if( ! is_array( $parsed_url ) ) {
|
287
|
|
|
return $parsed_url;
|
288
|
|
|
}
|
289
|
|
|
$scheme = isset( $parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
|
290
|
|
|
$host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : '';
|
291
|
|
|
$port = isset( $parsed_url['port'] ) ? ':' . $parsed_url['port'] : '';
|
292
|
|
|
$path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : '';
|
293
|
|
|
$query = isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '';
|
294
|
|
|
$fragment = isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '';
|
295
|
|
|
|
296
|
|
|
//NOTE: user and pass were explicitly removed.
|
297
|
|
|
|
298
|
|
|
return "$scheme$host$port$path$query$fragment";
|
299
|
|
|
}
|
300
|
|
|
|
301
|
|
|
}
|
302
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]
orarray<String>
.