@@ -38,103 +38,103 @@ |
||
| 38 | 38 | * @since 4.0 |
| 39 | 39 | */ |
| 40 | 40 | if (function_exists('espresso_version')) { |
| 41 | - if (! function_exists('espresso_duplicate_plugin_error')) { |
|
| 42 | - /** |
|
| 43 | - * espresso_duplicate_plugin_error |
|
| 44 | - * displays if more than one version of EE is activated at the same time |
|
| 45 | - */ |
|
| 46 | - function espresso_duplicate_plugin_error() |
|
| 47 | - { |
|
| 48 | - ?> |
|
| 41 | + if (! function_exists('espresso_duplicate_plugin_error')) { |
|
| 42 | + /** |
|
| 43 | + * espresso_duplicate_plugin_error |
|
| 44 | + * displays if more than one version of EE is activated at the same time |
|
| 45 | + */ |
|
| 46 | + function espresso_duplicate_plugin_error() |
|
| 47 | + { |
|
| 48 | + ?> |
|
| 49 | 49 | <div class="error"> |
| 50 | 50 | <p> |
| 51 | 51 | <?php |
| 52 | - echo esc_html__( |
|
| 53 | - 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
| 54 | - 'event_espresso' |
|
| 55 | - ); ?> |
|
| 52 | + echo esc_html__( |
|
| 53 | + 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
| 54 | + 'event_espresso' |
|
| 55 | + ); ?> |
|
| 56 | 56 | </p> |
| 57 | 57 | </div> |
| 58 | 58 | <?php |
| 59 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
| 59 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
| 63 | 63 | } else { |
| 64 | - define('EE_MIN_PHP_VER_REQUIRED', '5.6.2'); |
|
| 65 | - if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
| 66 | - /** |
|
| 67 | - * espresso_minimum_php_version_error |
|
| 68 | - * |
|
| 69 | - * @return void |
|
| 70 | - */ |
|
| 71 | - function espresso_minimum_php_version_error() |
|
| 72 | - { |
|
| 73 | - ?> |
|
| 64 | + define('EE_MIN_PHP_VER_REQUIRED', '5.6.2'); |
|
| 65 | + if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
| 66 | + /** |
|
| 67 | + * espresso_minimum_php_version_error |
|
| 68 | + * |
|
| 69 | + * @return void |
|
| 70 | + */ |
|
| 71 | + function espresso_minimum_php_version_error() |
|
| 72 | + { |
|
| 73 | + ?> |
|
| 74 | 74 | <div class="error"> |
| 75 | 75 | <p> |
| 76 | 76 | <?php |
| 77 | - printf( |
|
| 78 | - esc_html__( |
|
| 79 | - 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
| 80 | - 'event_espresso' |
|
| 81 | - ), |
|
| 82 | - EE_MIN_PHP_VER_REQUIRED, |
|
| 83 | - PHP_VERSION, |
|
| 84 | - '<br/>', |
|
| 85 | - '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
| 86 | - ); |
|
| 87 | - ?> |
|
| 77 | + printf( |
|
| 78 | + esc_html__( |
|
| 79 | + 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
| 80 | + 'event_espresso' |
|
| 81 | + ), |
|
| 82 | + EE_MIN_PHP_VER_REQUIRED, |
|
| 83 | + PHP_VERSION, |
|
| 84 | + '<br/>', |
|
| 85 | + '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
| 86 | + ); |
|
| 87 | + ?> |
|
| 88 | 88 | </p> |
| 89 | 89 | </div> |
| 90 | 90 | <?php |
| 91 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 92 | - } |
|
| 91 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
| 95 | - } else { |
|
| 96 | - define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
| 97 | - /** |
|
| 98 | - * espresso_version |
|
| 99 | - * Returns the plugin version |
|
| 100 | - * |
|
| 101 | - * @return string |
|
| 102 | - */ |
|
| 103 | - function espresso_version() |
|
| 104 | - { |
|
| 105 | - return apply_filters('FHEE__espresso__espresso_version', '4.10.17.rc.000'); |
|
| 106 | - } |
|
| 94 | + add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
| 95 | + } else { |
|
| 96 | + define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
| 97 | + /** |
|
| 98 | + * espresso_version |
|
| 99 | + * Returns the plugin version |
|
| 100 | + * |
|
| 101 | + * @return string |
|
| 102 | + */ |
|
| 103 | + function espresso_version() |
|
| 104 | + { |
|
| 105 | + return apply_filters('FHEE__espresso__espresso_version', '4.10.17.rc.000'); |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * espresso_plugin_activation |
|
| 110 | - * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
| 111 | - */ |
|
| 112 | - function espresso_plugin_activation() |
|
| 113 | - { |
|
| 114 | - update_option('ee_espresso_activation', true); |
|
| 115 | - } |
|
| 108 | + /** |
|
| 109 | + * espresso_plugin_activation |
|
| 110 | + * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
| 111 | + */ |
|
| 112 | + function espresso_plugin_activation() |
|
| 113 | + { |
|
| 114 | + update_option('ee_espresso_activation', true); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
| 117 | + register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
| 118 | 118 | |
| 119 | - require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
| 120 | - bootstrap_espresso(); |
|
| 121 | - } |
|
| 119 | + require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
| 120 | + bootstrap_espresso(); |
|
| 121 | + } |
|
| 122 | 122 | } |
| 123 | 123 | if (! function_exists('espresso_deactivate_plugin')) { |
| 124 | - /** |
|
| 125 | - * deactivate_plugin |
|
| 126 | - * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
| 127 | - * |
|
| 128 | - * @access public |
|
| 129 | - * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
| 130 | - * @return void |
|
| 131 | - */ |
|
| 132 | - function espresso_deactivate_plugin($plugin_basename = '') |
|
| 133 | - { |
|
| 134 | - if (! function_exists('deactivate_plugins')) { |
|
| 135 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 136 | - } |
|
| 137 | - unset($_GET['activate'], $_REQUEST['activate']); |
|
| 138 | - deactivate_plugins($plugin_basename); |
|
| 139 | - } |
|
| 124 | + /** |
|
| 125 | + * deactivate_plugin |
|
| 126 | + * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
| 127 | + * |
|
| 128 | + * @access public |
|
| 129 | + * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
| 130 | + * @return void |
|
| 131 | + */ |
|
| 132 | + function espresso_deactivate_plugin($plugin_basename = '') |
|
| 133 | + { |
|
| 134 | + if (! function_exists('deactivate_plugins')) { |
|
| 135 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 136 | + } |
|
| 137 | + unset($_GET['activate'], $_REQUEST['activate']); |
|
| 138 | + deactivate_plugins($plugin_basename); |
|
| 139 | + } |
|
| 140 | 140 | } |
@@ -14,314 +14,314 @@ |
||
| 14 | 14 | class RequestParams |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * $_GET parameters |
|
| 19 | - * |
|
| 20 | - * @var array |
|
| 21 | - */ |
|
| 22 | - protected $get; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * $_POST parameters |
|
| 26 | - * |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - protected $post; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * $_REQUEST parameters |
|
| 33 | - * |
|
| 34 | - * @var array |
|
| 35 | - */ |
|
| 36 | - protected $request; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var RequestSanitizer |
|
| 40 | - */ |
|
| 41 | - protected $sanitizer; |
|
| 42 | - |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * RequestParams constructor. |
|
| 46 | - * |
|
| 47 | - * @param RequestSanitizer $sanitizer |
|
| 48 | - * @param array $get |
|
| 49 | - * @param array $post |
|
| 50 | - */ |
|
| 51 | - public function __construct(RequestSanitizer $sanitizer, array $get = [], array $post = []) |
|
| 52 | - { |
|
| 53 | - $this->sanitizer = $sanitizer; |
|
| 54 | - $this->get = ! empty($get) ? $get : $_GET; |
|
| 55 | - $this->post = ! empty($post) ? $post : $_POST; |
|
| 56 | - $this->request = array_merge($this->get, $this->post); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @return array |
|
| 62 | - */ |
|
| 63 | - public function getParams() |
|
| 64 | - { |
|
| 65 | - return $this->get; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @return array |
|
| 71 | - */ |
|
| 72 | - public function postParams() |
|
| 73 | - { |
|
| 74 | - return $this->post; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * returns contents of $_REQUEST |
|
| 80 | - * |
|
| 81 | - * @return array |
|
| 82 | - */ |
|
| 83 | - public function requestParams() |
|
| 84 | - { |
|
| 85 | - return $this->request; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @param string $key |
|
| 91 | - * @param mixed|null $value |
|
| 92 | - * @param bool $override_ee |
|
| 93 | - * @return void |
|
| 94 | - */ |
|
| 95 | - public function setRequestParam($key, $value, $override_ee = false) |
|
| 96 | - { |
|
| 97 | - // don't allow "ee" to be overwritten unless explicitly instructed to do so |
|
| 98 | - if ($override_ee || $key !== 'ee' || empty($this->request['ee'])) { |
|
| 99 | - $this->request[ $key ] = $value; |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * returns the value for a request param if the given key exists |
|
| 106 | - * |
|
| 107 | - * @param string $key |
|
| 108 | - * @param mixed|null $default |
|
| 109 | - * @param string $type the expected data type for the parameter's value, ie: string, int, bool, etc |
|
| 110 | - * @param bool $is_array if true, then parameter value will be treated as an array of $type |
|
| 111 | - * @param string $delimiter for CSV type strings that should be returned as an array |
|
| 112 | - * @return array|bool|float|int|string |
|
| 113 | - */ |
|
| 114 | - public function getRequestParam($key, $default = null, $type = 'string', $is_array = false, $delimiter = '') |
|
| 115 | - { |
|
| 116 | - return $this->sanitizer->clean( |
|
| 117 | - $this->parameterDrillDown($key, $default, 'get'), |
|
| 118 | - $type, |
|
| 119 | - $is_array, |
|
| 120 | - $delimiter |
|
| 121 | - ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * check if param exists |
|
| 127 | - * |
|
| 128 | - * @param string $key |
|
| 129 | - * @return bool |
|
| 130 | - */ |
|
| 131 | - public function requestParamIsSet($key) |
|
| 132 | - { |
|
| 133 | - return (bool) $this->parameterDrillDown($key); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * check if a request parameter exists whose key that matches the supplied wildcard pattern |
|
| 139 | - * and return the value for the first match found |
|
| 140 | - * wildcards can be either of the following: |
|
| 141 | - * ? to represent a single character of any type |
|
| 142 | - * * to represent one or more characters of any type |
|
| 143 | - * |
|
| 144 | - * @param string $pattern |
|
| 145 | - * @param mixed|null $default |
|
| 146 | - * @param string $type the expected data type for the parameter's value, ie: string, int, bool, etc |
|
| 147 | - * @param bool $is_array if true, then parameter value will be treated as an array of $type |
|
| 148 | - * @param string $delimiter for CSV type strings that should be returned as an array |
|
| 149 | - * @return array|bool|float|int|string |
|
| 150 | - */ |
|
| 151 | - public function getMatch($pattern, $default = null, $type = 'string', $is_array = false, $delimiter = '') |
|
| 152 | - { |
|
| 153 | - return $this->sanitizer->clean( |
|
| 154 | - $this->parameterDrillDown($pattern, $default, 'match'), |
|
| 155 | - $type, |
|
| 156 | - $is_array, |
|
| 157 | - $delimiter |
|
| 158 | - ); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * check if a request parameter exists whose key matches the supplied wildcard pattern |
|
| 164 | - * wildcards can be either of the following: |
|
| 165 | - * ? to represent a single character of any type |
|
| 166 | - * * to represent one or more characters of any type |
|
| 167 | - * returns true if a match is found or false if not |
|
| 168 | - * |
|
| 169 | - * @param string $pattern |
|
| 170 | - * @return bool |
|
| 171 | - */ |
|
| 172 | - public function matches($pattern) |
|
| 173 | - { |
|
| 174 | - return (bool) $this->parameterDrillDown($pattern, false, 'match', 'bool'); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @see https://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard |
|
| 180 | - * @param string $pattern A string including wildcards to be converted to a regex pattern |
|
| 181 | - * and used to search through the current request's parameter keys |
|
| 182 | - * @param array $request_params The array of request parameters to search through |
|
| 183 | - * @param mixed $default [optional] The value to be returned if no match is found. |
|
| 184 | - * Default is null |
|
| 185 | - * @param string $return [optional] Controls what kind of value is returned. |
|
| 186 | - * Options are: |
|
| 187 | - * 'bool' will return true or false if match is found or not |
|
| 188 | - * 'key' will return the first key found that matches the supplied pattern |
|
| 189 | - * 'value' will return the value for the first request parameter |
|
| 190 | - * whose key matches the supplied pattern |
|
| 191 | - * Default is 'value' |
|
| 192 | - * @return boolean|string |
|
| 193 | - */ |
|
| 194 | - private function match($pattern, array $request_params, $default = null, $return = 'value') |
|
| 195 | - { |
|
| 196 | - $return = in_array($return, ['bool', 'key', 'value'], true) |
|
| 197 | - ? $return |
|
| 198 | - : 'is_set'; |
|
| 199 | - // replace wildcard chars with regex chars |
|
| 200 | - $pattern = str_replace( |
|
| 201 | - ['\*', '\?'], |
|
| 202 | - ['.*', '.'], |
|
| 203 | - preg_quote($pattern, '/') |
|
| 204 | - ); |
|
| 205 | - foreach ($request_params as $key => $request_param) { |
|
| 206 | - if (preg_match('/^' . $pattern . '$/is', $key)) { |
|
| 207 | - // return value for request param |
|
| 208 | - if ($return === 'value') { |
|
| 209 | - return $request_param; |
|
| 210 | - } |
|
| 211 | - // or actual key or true just to indicate it was found |
|
| 212 | - return $return === 'key' ? $key : true; |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - // match not found so return default value or false |
|
| 216 | - return $return === 'value' ? $default : false; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * the supplied key can be a simple string to represent a "top-level" request parameter |
|
| 222 | - * or represent a key for a request parameter that is nested deeper within the request parameter array, |
|
| 223 | - * by using square brackets to surround keys for deeper array elements. |
|
| 224 | - * For example : |
|
| 225 | - * if the supplied $key was: "first[second][third]" |
|
| 226 | - * then this will attempt to drill down into the request parameter array to find a value. |
|
| 227 | - * Given the following request parameters: |
|
| 228 | - * array( |
|
| 229 | - * 'first' => array( |
|
| 230 | - * 'second' => array( |
|
| 231 | - * 'third' => 'has a value' |
|
| 232 | - * ) |
|
| 233 | - * ) |
|
| 234 | - * ) |
|
| 235 | - * would return true if default parameters were set |
|
| 236 | - * |
|
| 237 | - * @param string $callback |
|
| 238 | - * @param $key |
|
| 239 | - * @param null $default |
|
| 240 | - * @param string $return |
|
| 241 | - * @param mixed $request_params |
|
| 242 | - * @return bool|mixed|null |
|
| 243 | - */ |
|
| 244 | - private function parameterDrillDown( |
|
| 245 | - $key, |
|
| 246 | - $default = null, |
|
| 247 | - $callback = 'is_set', |
|
| 248 | - $return = 'value', |
|
| 249 | - $request_params = [] |
|
| 250 | - ) { |
|
| 251 | - $callback = in_array($callback, ['is_set', 'get', 'match'], true) |
|
| 252 | - ? $callback |
|
| 253 | - : 'is_set'; |
|
| 254 | - $request_params = ! empty($request_params) |
|
| 255 | - ? $request_params |
|
| 256 | - : $this->request; |
|
| 257 | - // does incoming key represent an array like 'first[second][third]' ? |
|
| 258 | - if (strpos($key, '[') !== false) { |
|
| 259 | - // turn it into an actual array |
|
| 260 | - $key = str_replace(']', '', $key); |
|
| 261 | - $keys = explode('[', $key); |
|
| 262 | - $key = array_shift($keys); |
|
| 263 | - if ($callback === 'match') { |
|
| 264 | - $real_key = $this->match($key, $request_params, $default, 'key'); |
|
| 265 | - $key = $real_key ?: $key; |
|
| 266 | - } |
|
| 267 | - // check if top level key exists |
|
| 268 | - if (isset($request_params[ $key ])) { |
|
| 269 | - // build a new key to pass along like: 'second[third]' |
|
| 270 | - // or just 'second' depending on depth of keys |
|
| 271 | - $key_string = array_shift($keys); |
|
| 272 | - if (! empty($keys)) { |
|
| 273 | - $key_string .= '[' . implode('][', $keys) . ']'; |
|
| 274 | - } |
|
| 275 | - return $this->parameterDrillDown( |
|
| 276 | - $key_string, |
|
| 277 | - $default, |
|
| 278 | - $callback, |
|
| 279 | - $return, |
|
| 280 | - $request_params[ $key ] |
|
| 281 | - ); |
|
| 282 | - } |
|
| 283 | - } |
|
| 284 | - if ($callback === 'is_set') { |
|
| 285 | - return isset($request_params[ $key ]); |
|
| 286 | - } |
|
| 287 | - if ($callback === 'match') { |
|
| 288 | - return $this->match($key, $request_params, $default, $return); |
|
| 289 | - } |
|
| 290 | - return isset($request_params[ $key ]) |
|
| 291 | - ? $request_params[ $key ] |
|
| 292 | - : $default; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * remove param |
|
| 298 | - * |
|
| 299 | - * @param $key |
|
| 300 | - * @param bool $unset_from_global_too |
|
| 301 | - */ |
|
| 302 | - public function unSetRequestParam($key, $unset_from_global_too = false) |
|
| 303 | - { |
|
| 304 | - // because unset may not actually remove var |
|
| 305 | - $this->get[ $key ] = null; |
|
| 306 | - $this->post[ $key ] = null; |
|
| 307 | - $this->request[ $key ] = null; |
|
| 308 | - unset($this->get[ $key ], $this->post[ $key ], $this->request[ $key ]); |
|
| 309 | - if ($unset_from_global_too) { |
|
| 310 | - unset($_GET[ $key ], $_POST[ $key ], $_REQUEST[ $key ]); |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * remove params |
|
| 317 | - * |
|
| 318 | - * @param array $keys |
|
| 319 | - * @param bool $unset_from_global_too |
|
| 320 | - */ |
|
| 321 | - public function unSetRequestParams(array $keys, $unset_from_global_too = false) |
|
| 322 | - { |
|
| 323 | - foreach ($keys as $key) { |
|
| 324 | - $this->unSetRequestParam($key, $unset_from_global_too); |
|
| 325 | - } |
|
| 326 | - } |
|
| 17 | + /** |
|
| 18 | + * $_GET parameters |
|
| 19 | + * |
|
| 20 | + * @var array |
|
| 21 | + */ |
|
| 22 | + protected $get; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * $_POST parameters |
|
| 26 | + * |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + protected $post; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * $_REQUEST parameters |
|
| 33 | + * |
|
| 34 | + * @var array |
|
| 35 | + */ |
|
| 36 | + protected $request; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var RequestSanitizer |
|
| 40 | + */ |
|
| 41 | + protected $sanitizer; |
|
| 42 | + |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * RequestParams constructor. |
|
| 46 | + * |
|
| 47 | + * @param RequestSanitizer $sanitizer |
|
| 48 | + * @param array $get |
|
| 49 | + * @param array $post |
|
| 50 | + */ |
|
| 51 | + public function __construct(RequestSanitizer $sanitizer, array $get = [], array $post = []) |
|
| 52 | + { |
|
| 53 | + $this->sanitizer = $sanitizer; |
|
| 54 | + $this->get = ! empty($get) ? $get : $_GET; |
|
| 55 | + $this->post = ! empty($post) ? $post : $_POST; |
|
| 56 | + $this->request = array_merge($this->get, $this->post); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @return array |
|
| 62 | + */ |
|
| 63 | + public function getParams() |
|
| 64 | + { |
|
| 65 | + return $this->get; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @return array |
|
| 71 | + */ |
|
| 72 | + public function postParams() |
|
| 73 | + { |
|
| 74 | + return $this->post; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * returns contents of $_REQUEST |
|
| 80 | + * |
|
| 81 | + * @return array |
|
| 82 | + */ |
|
| 83 | + public function requestParams() |
|
| 84 | + { |
|
| 85 | + return $this->request; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @param string $key |
|
| 91 | + * @param mixed|null $value |
|
| 92 | + * @param bool $override_ee |
|
| 93 | + * @return void |
|
| 94 | + */ |
|
| 95 | + public function setRequestParam($key, $value, $override_ee = false) |
|
| 96 | + { |
|
| 97 | + // don't allow "ee" to be overwritten unless explicitly instructed to do so |
|
| 98 | + if ($override_ee || $key !== 'ee' || empty($this->request['ee'])) { |
|
| 99 | + $this->request[ $key ] = $value; |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * returns the value for a request param if the given key exists |
|
| 106 | + * |
|
| 107 | + * @param string $key |
|
| 108 | + * @param mixed|null $default |
|
| 109 | + * @param string $type the expected data type for the parameter's value, ie: string, int, bool, etc |
|
| 110 | + * @param bool $is_array if true, then parameter value will be treated as an array of $type |
|
| 111 | + * @param string $delimiter for CSV type strings that should be returned as an array |
|
| 112 | + * @return array|bool|float|int|string |
|
| 113 | + */ |
|
| 114 | + public function getRequestParam($key, $default = null, $type = 'string', $is_array = false, $delimiter = '') |
|
| 115 | + { |
|
| 116 | + return $this->sanitizer->clean( |
|
| 117 | + $this->parameterDrillDown($key, $default, 'get'), |
|
| 118 | + $type, |
|
| 119 | + $is_array, |
|
| 120 | + $delimiter |
|
| 121 | + ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * check if param exists |
|
| 127 | + * |
|
| 128 | + * @param string $key |
|
| 129 | + * @return bool |
|
| 130 | + */ |
|
| 131 | + public function requestParamIsSet($key) |
|
| 132 | + { |
|
| 133 | + return (bool) $this->parameterDrillDown($key); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * check if a request parameter exists whose key that matches the supplied wildcard pattern |
|
| 139 | + * and return the value for the first match found |
|
| 140 | + * wildcards can be either of the following: |
|
| 141 | + * ? to represent a single character of any type |
|
| 142 | + * * to represent one or more characters of any type |
|
| 143 | + * |
|
| 144 | + * @param string $pattern |
|
| 145 | + * @param mixed|null $default |
|
| 146 | + * @param string $type the expected data type for the parameter's value, ie: string, int, bool, etc |
|
| 147 | + * @param bool $is_array if true, then parameter value will be treated as an array of $type |
|
| 148 | + * @param string $delimiter for CSV type strings that should be returned as an array |
|
| 149 | + * @return array|bool|float|int|string |
|
| 150 | + */ |
|
| 151 | + public function getMatch($pattern, $default = null, $type = 'string', $is_array = false, $delimiter = '') |
|
| 152 | + { |
|
| 153 | + return $this->sanitizer->clean( |
|
| 154 | + $this->parameterDrillDown($pattern, $default, 'match'), |
|
| 155 | + $type, |
|
| 156 | + $is_array, |
|
| 157 | + $delimiter |
|
| 158 | + ); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * check if a request parameter exists whose key matches the supplied wildcard pattern |
|
| 164 | + * wildcards can be either of the following: |
|
| 165 | + * ? to represent a single character of any type |
|
| 166 | + * * to represent one or more characters of any type |
|
| 167 | + * returns true if a match is found or false if not |
|
| 168 | + * |
|
| 169 | + * @param string $pattern |
|
| 170 | + * @return bool |
|
| 171 | + */ |
|
| 172 | + public function matches($pattern) |
|
| 173 | + { |
|
| 174 | + return (bool) $this->parameterDrillDown($pattern, false, 'match', 'bool'); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @see https://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard |
|
| 180 | + * @param string $pattern A string including wildcards to be converted to a regex pattern |
|
| 181 | + * and used to search through the current request's parameter keys |
|
| 182 | + * @param array $request_params The array of request parameters to search through |
|
| 183 | + * @param mixed $default [optional] The value to be returned if no match is found. |
|
| 184 | + * Default is null |
|
| 185 | + * @param string $return [optional] Controls what kind of value is returned. |
|
| 186 | + * Options are: |
|
| 187 | + * 'bool' will return true or false if match is found or not |
|
| 188 | + * 'key' will return the first key found that matches the supplied pattern |
|
| 189 | + * 'value' will return the value for the first request parameter |
|
| 190 | + * whose key matches the supplied pattern |
|
| 191 | + * Default is 'value' |
|
| 192 | + * @return boolean|string |
|
| 193 | + */ |
|
| 194 | + private function match($pattern, array $request_params, $default = null, $return = 'value') |
|
| 195 | + { |
|
| 196 | + $return = in_array($return, ['bool', 'key', 'value'], true) |
|
| 197 | + ? $return |
|
| 198 | + : 'is_set'; |
|
| 199 | + // replace wildcard chars with regex chars |
|
| 200 | + $pattern = str_replace( |
|
| 201 | + ['\*', '\?'], |
|
| 202 | + ['.*', '.'], |
|
| 203 | + preg_quote($pattern, '/') |
|
| 204 | + ); |
|
| 205 | + foreach ($request_params as $key => $request_param) { |
|
| 206 | + if (preg_match('/^' . $pattern . '$/is', $key)) { |
|
| 207 | + // return value for request param |
|
| 208 | + if ($return === 'value') { |
|
| 209 | + return $request_param; |
|
| 210 | + } |
|
| 211 | + // or actual key or true just to indicate it was found |
|
| 212 | + return $return === 'key' ? $key : true; |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + // match not found so return default value or false |
|
| 216 | + return $return === 'value' ? $default : false; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * the supplied key can be a simple string to represent a "top-level" request parameter |
|
| 222 | + * or represent a key for a request parameter that is nested deeper within the request parameter array, |
|
| 223 | + * by using square brackets to surround keys for deeper array elements. |
|
| 224 | + * For example : |
|
| 225 | + * if the supplied $key was: "first[second][third]" |
|
| 226 | + * then this will attempt to drill down into the request parameter array to find a value. |
|
| 227 | + * Given the following request parameters: |
|
| 228 | + * array( |
|
| 229 | + * 'first' => array( |
|
| 230 | + * 'second' => array( |
|
| 231 | + * 'third' => 'has a value' |
|
| 232 | + * ) |
|
| 233 | + * ) |
|
| 234 | + * ) |
|
| 235 | + * would return true if default parameters were set |
|
| 236 | + * |
|
| 237 | + * @param string $callback |
|
| 238 | + * @param $key |
|
| 239 | + * @param null $default |
|
| 240 | + * @param string $return |
|
| 241 | + * @param mixed $request_params |
|
| 242 | + * @return bool|mixed|null |
|
| 243 | + */ |
|
| 244 | + private function parameterDrillDown( |
|
| 245 | + $key, |
|
| 246 | + $default = null, |
|
| 247 | + $callback = 'is_set', |
|
| 248 | + $return = 'value', |
|
| 249 | + $request_params = [] |
|
| 250 | + ) { |
|
| 251 | + $callback = in_array($callback, ['is_set', 'get', 'match'], true) |
|
| 252 | + ? $callback |
|
| 253 | + : 'is_set'; |
|
| 254 | + $request_params = ! empty($request_params) |
|
| 255 | + ? $request_params |
|
| 256 | + : $this->request; |
|
| 257 | + // does incoming key represent an array like 'first[second][third]' ? |
|
| 258 | + if (strpos($key, '[') !== false) { |
|
| 259 | + // turn it into an actual array |
|
| 260 | + $key = str_replace(']', '', $key); |
|
| 261 | + $keys = explode('[', $key); |
|
| 262 | + $key = array_shift($keys); |
|
| 263 | + if ($callback === 'match') { |
|
| 264 | + $real_key = $this->match($key, $request_params, $default, 'key'); |
|
| 265 | + $key = $real_key ?: $key; |
|
| 266 | + } |
|
| 267 | + // check if top level key exists |
|
| 268 | + if (isset($request_params[ $key ])) { |
|
| 269 | + // build a new key to pass along like: 'second[third]' |
|
| 270 | + // or just 'second' depending on depth of keys |
|
| 271 | + $key_string = array_shift($keys); |
|
| 272 | + if (! empty($keys)) { |
|
| 273 | + $key_string .= '[' . implode('][', $keys) . ']'; |
|
| 274 | + } |
|
| 275 | + return $this->parameterDrillDown( |
|
| 276 | + $key_string, |
|
| 277 | + $default, |
|
| 278 | + $callback, |
|
| 279 | + $return, |
|
| 280 | + $request_params[ $key ] |
|
| 281 | + ); |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | + if ($callback === 'is_set') { |
|
| 285 | + return isset($request_params[ $key ]); |
|
| 286 | + } |
|
| 287 | + if ($callback === 'match') { |
|
| 288 | + return $this->match($key, $request_params, $default, $return); |
|
| 289 | + } |
|
| 290 | + return isset($request_params[ $key ]) |
|
| 291 | + ? $request_params[ $key ] |
|
| 292 | + : $default; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * remove param |
|
| 298 | + * |
|
| 299 | + * @param $key |
|
| 300 | + * @param bool $unset_from_global_too |
|
| 301 | + */ |
|
| 302 | + public function unSetRequestParam($key, $unset_from_global_too = false) |
|
| 303 | + { |
|
| 304 | + // because unset may not actually remove var |
|
| 305 | + $this->get[ $key ] = null; |
|
| 306 | + $this->post[ $key ] = null; |
|
| 307 | + $this->request[ $key ] = null; |
|
| 308 | + unset($this->get[ $key ], $this->post[ $key ], $this->request[ $key ]); |
|
| 309 | + if ($unset_from_global_too) { |
|
| 310 | + unset($_GET[ $key ], $_POST[ $key ], $_REQUEST[ $key ]); |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * remove params |
|
| 317 | + * |
|
| 318 | + * @param array $keys |
|
| 319 | + * @param bool $unset_from_global_too |
|
| 320 | + */ |
|
| 321 | + public function unSetRequestParams(array $keys, $unset_from_global_too = false) |
|
| 322 | + { |
|
| 323 | + foreach ($keys as $key) { |
|
| 324 | + $this->unSetRequestParam($key, $unset_from_global_too); |
|
| 325 | + } |
|
| 326 | + } |
|
| 327 | 327 | } |