1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace GV; |
3
|
|
|
|
4
|
|
|
/** If this file is called directly, abort. */ |
5
|
|
|
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
6
|
|
|
die(); |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Generic utilities. |
11
|
|
|
*/ |
12
|
|
|
class Utils { |
13
|
|
|
/** |
14
|
|
|
* Grab a value from the _GET superglobal or default. |
15
|
|
|
* |
16
|
|
|
* @param string $name The key name (will be prefixed). |
17
|
|
|
* @param mixed $default The default value if not found (Default: null) |
18
|
|
|
* |
19
|
|
|
* @return mixed The value or $default if not found. |
20
|
|
|
*/ |
21
|
38 |
|
public static function _GET( $name, $default = null ) { |
|
|
|
|
22
|
38 |
|
return self::get( $_GET, $name, $default ); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Grab a value from the _POST superglobal or default. |
27
|
|
|
* |
28
|
|
|
* @param string $name The key name (will be prefixed). |
29
|
|
|
* @param mixed $default The default value if not found (Default: null) |
30
|
|
|
* |
31
|
|
|
* @return mixed The value or $default if not found. |
32
|
|
|
*/ |
33
|
8 |
|
public static function _POST( $name, $default = null ) { |
|
|
|
|
34
|
8 |
|
return self::get( $_POST, $name, $default ); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Grab a value from the _REQUEST superglobal or default. |
39
|
|
|
* |
40
|
|
|
* @param string $name The key name (will be prefixed). |
41
|
|
|
* @param mixed $default The default value if not found (Default: null) |
42
|
|
|
* |
43
|
|
|
* @return mixed The value or $default if not found. |
44
|
|
|
*/ |
45
|
4 |
|
public static function _REQUEST( $name, $default = null ) { |
|
|
|
|
46
|
4 |
|
return self::get( $_REQUEST, $name, $default ); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Grab a value from the _SERVER superglobal or default. |
51
|
|
|
* |
52
|
|
|
* @param string $name The key name (will be prefixed). |
53
|
|
|
* @param mixed $default The default value if not found (Default: null) |
54
|
|
|
* |
55
|
|
|
* @return mixed The value or $default if not found. |
56
|
|
|
*/ |
57
|
18 |
|
public static function _SERVER( $name, $default = null ) { |
|
|
|
|
58
|
18 |
|
return self::get( $_SERVER, $name, $default ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Grab a value from an array or an object or default. |
63
|
|
|
* |
64
|
|
|
* Supports nested arrays, objects via / key delimiters. |
65
|
|
|
* |
66
|
|
|
* @param array|object $array The array (or object) |
67
|
|
|
* @param string $key The key. |
68
|
|
|
* @param mixed $default The default value. Default: null |
69
|
|
|
* |
70
|
|
|
* @return mixed The value or $default if not found. |
71
|
|
|
*/ |
72
|
120 |
|
public static function get( $array, $key, $default = null ) { |
73
|
120 |
|
if ( ! is_array( $array ) && ! is_object( $array ) ) { |
74
|
26 |
|
return $default; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Try direct key. |
79
|
|
|
*/ |
80
|
120 |
|
if ( is_array( $array ) || $array instanceof \ArrayAccess ) { |
81
|
120 |
|
if ( isset( $array[ $key ] ) ) { |
82
|
120 |
|
return $array[ $key ]; |
83
|
|
|
} |
84
|
69 |
|
} else if ( is_object( $array ) ) { |
85
|
69 |
|
if ( property_exists( $array, $key ) ) { |
86
|
69 |
|
return $array->$key; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Try subkeys after split. |
92
|
|
|
*/ |
93
|
109 |
|
if ( count( $parts = explode( '/', $key, 2 ) ) > 1 ) { |
94
|
42 |
|
return self::get( self::get( $array, $parts[0] ), $parts[1], $default ); |
95
|
|
|
} |
96
|
|
|
|
97
|
109 |
|
return $default; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Sanitizes Excel formulas inside CSV output |
102
|
|
|
* |
103
|
|
|
* @internal |
104
|
|
|
* @since 2.1 |
105
|
|
|
* |
106
|
|
|
* @param string $value The cell value to strip formulas from. |
107
|
|
|
* |
108
|
|
|
* @return string The sanitized value. |
109
|
|
|
*/ |
110
|
4 |
|
public static function strip_excel_formulas( $value ) { |
111
|
|
|
|
112
|
4 |
|
if ( strpos( $value, '=' ) === 0 ) { |
|
|
|
|
113
|
3 |
|
$value = "'" . $value; |
114
|
|
|
} |
115
|
|
|
|
116
|
4 |
|
return $value; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Return a value by call. |
121
|
|
|
* |
122
|
|
|
* Use for quick hook callback returns and whatnot. |
123
|
|
|
* |
124
|
|
|
* @internal |
125
|
|
|
* @since 2.1 |
126
|
|
|
* |
127
|
|
|
* @param mixed $value The value to return from the closure. |
128
|
|
|
* |
129
|
|
|
* @return Closure The closure with the $value bound. |
130
|
|
|
*/ |
131
|
|
|
public static function _return( $value ) { |
132
|
|
|
return function() use ( $value ) { return $value; }; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Output an associative array represenation of the query parameters. |
137
|
|
|
* |
138
|
|
|
* @internal |
139
|
|
|
* @since 2.1 |
140
|
|
|
* |
141
|
|
|
* @param \GF_Query The query object to dump. |
142
|
|
|
* |
143
|
|
|
* @return array An associative array of parameters. |
144
|
|
|
*/ |
145
|
29 |
|
public static function gf_query_debug( $query ) { |
146
|
29 |
|
$introspect = $query->_introspect(); |
147
|
|
|
return array( |
148
|
29 |
|
'where' => $query->_where_unwrap( $introspect['where'] ) |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.