@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 2 | - exit('No direct script access allowed'); |
|
| 2 | + exit('No direct script access allowed'); |
|
| 3 | 3 | } |
| 4 | 4 | |
| 5 | 5 | |
@@ -16,1142 +16,1142 @@ discard block |
||
| 16 | 16 | abstract class EE_Form_Input_Base extends EE_Form_Section_Validatable |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * the input's name attribute |
|
| 21 | - * |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 24 | - protected $_html_name; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * id for the html label tag |
|
| 28 | - * |
|
| 29 | - * @var string |
|
| 30 | - */ |
|
| 31 | - protected $_html_label_id; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * class for teh html label tag |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - protected $_html_label_class; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * any additional html attributes that you may want to add |
|
| 42 | - * |
|
| 43 | - * @var string |
|
| 44 | - */ |
|
| 45 | - protected $_html_other_attributes; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * style for teh html label tag |
|
| 49 | - * |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $_html_label_style; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * text to be placed in the html label |
|
| 56 | - * |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - protected $_html_label_text; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * the full html label. If used, all other html_label_* properties are invalid |
|
| 63 | - * |
|
| 64 | - * @var string |
|
| 65 | - */ |
|
| 66 | - protected $_html_label; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * HTML to use for help text (normally placed below form input), in a span which normally |
|
| 70 | - * has a class of 'description' |
|
| 71 | - * |
|
| 72 | - * @var string |
|
| 73 | - */ |
|
| 74 | - protected $_html_help_text; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * CSS classes for displaying the help span |
|
| 78 | - * |
|
| 79 | - * @var string |
|
| 80 | - */ |
|
| 81 | - protected $_html_help_class = 'description'; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * CSS to put in the style attribute on the help span |
|
| 85 | - * |
|
| 86 | - * @var string |
|
| 87 | - */ |
|
| 88 | - protected $_html_help_style; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Stores whether or not this input's response is required. |
|
| 92 | - * Because certain styling elements may also want to know that this |
|
| 93 | - * input is required etc. |
|
| 94 | - * |
|
| 95 | - * @var boolean |
|
| 96 | - */ |
|
| 97 | - protected $_required; |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * css class added to required inputs |
|
| 101 | - * |
|
| 102 | - * @var string |
|
| 103 | - */ |
|
| 104 | - protected $_required_css_class = 'ee-required'; |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * css styles applied to button type inputs |
|
| 108 | - * |
|
| 109 | - * @var string |
|
| 110 | - */ |
|
| 111 | - protected $_button_css_attributes; |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * The raw data submitted for this, like in the $_POST super global. |
|
| 115 | - * Generally unsafe for usage in client code |
|
| 116 | - * |
|
| 117 | - * @var mixed string or array |
|
| 118 | - */ |
|
| 119 | - protected $_raw_value; |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Value normalized according to the input's normalization strategy. |
|
| 123 | - * The normalization strategy dictates whether this is a string, int, float, |
|
| 124 | - * boolean, or array of any of those. |
|
| 125 | - * |
|
| 126 | - * @var mixed |
|
| 127 | - */ |
|
| 128 | - protected $_normalized_value; |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Strategy used for displaying this field. |
|
| 132 | - * Child classes must use _get_display_strategy to access it. |
|
| 133 | - * |
|
| 134 | - * @var EE_Display_Strategy_Base |
|
| 135 | - */ |
|
| 136 | - private $_display_strategy; |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Gets all the validation strategies used on this field |
|
| 140 | - * |
|
| 141 | - * @var EE_Validation_Strategy_Base[] |
|
| 142 | - */ |
|
| 143 | - private $_validation_strategies = array(); |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * The normalization strategy for this field |
|
| 147 | - * |
|
| 148 | - * @var EE_Normalization_Strategy_Base |
|
| 149 | - */ |
|
| 150 | - private $_normalization_strategy; |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Strategy for removing sensitive data after we're done with the form input |
|
| 154 | - * |
|
| 155 | - * @var EE_Sensitive_Data_Removal_Base |
|
| 156 | - */ |
|
| 157 | - protected $_sensitive_data_removal_strategy; |
|
| 158 | - |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @param array $input_args { |
|
| 163 | - * @type string $html_name the html name for the input |
|
| 164 | - * @type string $html_label_id the id attribute to give to the html label tag |
|
| 165 | - * @type string $html_label_class the class attribute to give to the html label tag |
|
| 166 | - * @type string $html_label_style the style attribute to give ot teh label tag |
|
| 167 | - * @type string $html_label_text the text to put in the label tag |
|
| 168 | - * @type string $html_label the full html label. If used, |
|
| 169 | - * all other html_label_* args are invalid |
|
| 170 | - * @type string $html_help_text text to put in help element |
|
| 171 | - * @type string $html_help_style style attribute to give to teh help element |
|
| 172 | - * @type string $html_help_class class attribute to give to the help element |
|
| 173 | - * @type string $default default value NORMALIZED (eg, if providing the default |
|
| 174 | - * for a Yes_No_Input, you should provide TRUE or FALSE, not '1' or '0') |
|
| 175 | - * @type EE_Display_Strategy_Base $display strategy |
|
| 176 | - * @type EE_Normalization_Strategy_Base $normalization_strategy |
|
| 177 | - * @type EE_Validation_Strategy_Base[] $validation_strategies |
|
| 178 | - * } |
|
| 179 | - */ |
|
| 180 | - public function __construct($input_args = array()) |
|
| 181 | - { |
|
| 182 | - $input_args = (array)apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this); |
|
| 183 | - // the following properties must be cast as arrays |
|
| 184 | - if (isset($input_args['validation_strategies'])) { |
|
| 185 | - foreach ((array)$input_args['validation_strategies'] as $validation_strategy) { |
|
| 186 | - if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
| 187 | - $this->_validation_strategies[get_class($validation_strategy)] = $validation_strategy; |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - unset($input_args['validation_strategies']); |
|
| 191 | - } |
|
| 192 | - // loop thru incoming options |
|
| 193 | - foreach ($input_args as $key => $value) { |
|
| 194 | - // add underscore to $key to match property names |
|
| 195 | - $_key = '_' . $key; |
|
| 196 | - if (property_exists($this, $_key)) { |
|
| 197 | - $this->{$_key} = $value; |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - // ensure that "required" is set correctly |
|
| 201 | - $this->set_required( |
|
| 202 | - $this->_required, isset($input_args['required_validation_error_message']) |
|
| 203 | - ? $input_args['required_validation_error_message'] |
|
| 204 | - : null |
|
| 205 | - ); |
|
| 206 | - //$this->_html_name_specified = isset( $input_args['html_name'] ) ? TRUE : FALSE; |
|
| 207 | - $this->_display_strategy->_construct_finalize($this); |
|
| 208 | - foreach ($this->_validation_strategies as $validation_strategy) { |
|
| 209 | - $validation_strategy->_construct_finalize($this); |
|
| 210 | - } |
|
| 211 | - if (! $this->_normalization_strategy) { |
|
| 212 | - $this->_normalization_strategy = new EE_Text_Normalization(); |
|
| 213 | - } |
|
| 214 | - $this->_normalization_strategy->_construct_finalize($this); |
|
| 215 | - //at least we can use the normalization strategy to populate the default |
|
| 216 | - if (isset($input_args['default'])) { |
|
| 217 | - $this->set_default($input_args['default']); |
|
| 218 | - } |
|
| 219 | - if (! $this->_sensitive_data_removal_strategy) { |
|
| 220 | - $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal(); |
|
| 221 | - } |
|
| 222 | - $this->_sensitive_data_removal_strategy->_construct_finalize($this); |
|
| 223 | - parent::__construct($input_args); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Sets the html_name to its default value, if none was specified in teh constructor. |
|
| 230 | - * Calculation involves using the name and the parent's html_name |
|
| 231 | - * |
|
| 232 | - * @throws \EE_Error |
|
| 233 | - */ |
|
| 234 | - protected function _set_default_html_name_if_empty() |
|
| 235 | - { |
|
| 236 | - if (! $this->_html_name) { |
|
| 237 | - $this->_html_name = $this->name(); |
|
| 238 | - if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
| 239 | - $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]"; |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * @param $parent_form_section |
|
| 248 | - * @param $name |
|
| 249 | - * @throws \EE_Error |
|
| 250 | - */ |
|
| 251 | - public function _construct_finalize($parent_form_section, $name) |
|
| 252 | - { |
|
| 253 | - parent::_construct_finalize($parent_form_section, $name); |
|
| 254 | - if ($this->_html_label === null && $this->_html_label_text === null) { |
|
| 255 | - $this->_html_label_text = ucwords(str_replace("_", " ", $name)); |
|
| 256 | - } |
|
| 257 | - do_action('AHEE__EE_Form_Input_Base___construct_finalize__end', $this, $parent_form_section, $name); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Returns the strategy for displaying this form input. If none is set, throws an exception. |
|
| 264 | - * |
|
| 265 | - * @return EE_Display_Strategy_Base |
|
| 266 | - * @throws EE_Error |
|
| 267 | - */ |
|
| 268 | - protected function _get_display_strategy() |
|
| 269 | - { |
|
| 270 | - $this->ensure_construct_finalized_called(); |
|
| 271 | - if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
| 272 | - throw new EE_Error( |
|
| 273 | - sprintf( |
|
| 274 | - __( |
|
| 275 | - "Cannot get display strategy for form input with name %s and id %s, because it has not been set in the constructor", |
|
| 276 | - "event_espresso" |
|
| 277 | - ), |
|
| 278 | - $this->html_name(), |
|
| 279 | - $this->html_id() |
|
| 280 | - ) |
|
| 281 | - ); |
|
| 282 | - } else { |
|
| 283 | - return $this->_display_strategy; |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Sets the display strategy. |
|
| 291 | - * |
|
| 292 | - * @param EE_Display_Strategy_Base $strategy |
|
| 293 | - */ |
|
| 294 | - protected function _set_display_strategy(EE_Display_Strategy_Base $strategy) |
|
| 295 | - { |
|
| 296 | - $this->_display_strategy = $strategy; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Sets the sanitization strategy |
|
| 303 | - * |
|
| 304 | - * @param EE_Normalization_Strategy_Base $strategy |
|
| 305 | - */ |
|
| 306 | - protected function _set_normalization_strategy(EE_Normalization_Strategy_Base $strategy) |
|
| 307 | - { |
|
| 308 | - $this->_normalization_strategy = $strategy; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Gets sensitive_data_removal_strategy |
|
| 315 | - * |
|
| 316 | - * @return EE_Sensitive_Data_Removal_Base |
|
| 317 | - */ |
|
| 318 | - public function get_sensitive_data_removal_strategy() |
|
| 319 | - { |
|
| 320 | - return $this->_sensitive_data_removal_strategy; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * Sets sensitive_data_removal_strategy |
|
| 327 | - * |
|
| 328 | - * @param EE_Sensitive_Data_Removal_Base $sensitive_data_removal_strategy |
|
| 329 | - * @return boolean |
|
| 330 | - */ |
|
| 331 | - public function set_sensitive_data_removal_strategy($sensitive_data_removal_strategy) |
|
| 332 | - { |
|
| 333 | - $this->_sensitive_data_removal_strategy = $sensitive_data_removal_strategy; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Gets the display strategy for this input |
|
| 340 | - * |
|
| 341 | - * @return EE_Display_Strategy_Base |
|
| 342 | - */ |
|
| 343 | - public function get_display_strategy() |
|
| 344 | - { |
|
| 345 | - return $this->_display_strategy; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Overwrites the display strategy |
|
| 352 | - * |
|
| 353 | - * @param EE_Display_Strategy_Base $display_strategy |
|
| 354 | - */ |
|
| 355 | - public function set_display_strategy($display_strategy) |
|
| 356 | - { |
|
| 357 | - $this->_display_strategy = $display_strategy; |
|
| 358 | - $this->_display_strategy->_construct_finalize($this); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * Gets the normalization strategy set on this input |
|
| 365 | - * |
|
| 366 | - * @return EE_Normalization_Strategy_Base |
|
| 367 | - */ |
|
| 368 | - public function get_normalization_strategy() |
|
| 369 | - { |
|
| 370 | - return $this->_normalization_strategy; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Overwrites the normalization strategy |
|
| 377 | - * |
|
| 378 | - * @param EE_Normalization_Strategy_Base $normalization_strategy |
|
| 379 | - */ |
|
| 380 | - public function set_normalization_strategy($normalization_strategy) |
|
| 381 | - { |
|
| 382 | - $this->_normalization_strategy = $normalization_strategy; |
|
| 383 | - $this->_normalization_strategy->_construct_finalize($this); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Returns all teh validation strategies which apply to this field, numerically indexed |
|
| 390 | - * |
|
| 391 | - * @return EE_Validation_Strategy_Base[] |
|
| 392 | - */ |
|
| 393 | - public function get_validation_strategies() |
|
| 394 | - { |
|
| 395 | - return $this->_validation_strategies; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Adds this strategy to the field so it will be used in both JS validation and server-side validation |
|
| 402 | - * |
|
| 403 | - * @param EE_Validation_Strategy_Base $validation_strategy |
|
| 404 | - * @return void |
|
| 405 | - */ |
|
| 406 | - protected function _add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
| 407 | - { |
|
| 408 | - $validation_strategy->_construct_finalize($this); |
|
| 409 | - $this->_validation_strategies[] = $validation_strategy; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Adds a new validation strategy onto the form input |
|
| 416 | - * |
|
| 417 | - * @param EE_Validation_Strategy_Base $validation_strategy |
|
| 418 | - * @return void |
|
| 419 | - */ |
|
| 420 | - public function add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
| 421 | - { |
|
| 422 | - $this->_add_validation_strategy($validation_strategy); |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * The classname of the validation strategy to remove |
|
| 429 | - * |
|
| 430 | - * @param string $validation_strategy_classname |
|
| 431 | - */ |
|
| 432 | - public function remove_validation_strategy($validation_strategy_classname) |
|
| 433 | - { |
|
| 434 | - foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
| 435 | - if ( |
|
| 436 | - $validation_strategy instanceof $validation_strategy_classname |
|
| 437 | - || is_subclass_of($validation_strategy, $validation_strategy_classname) |
|
| 438 | - ) { |
|
| 439 | - unset($this->_validation_strategies[$key]); |
|
| 440 | - } |
|
| 441 | - } |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * returns true if input employs any of the validation strategy defined by the supplied array of classnames |
|
| 448 | - * |
|
| 449 | - * @param array $validation_strategy_classnames |
|
| 450 | - * @return bool |
|
| 451 | - */ |
|
| 452 | - public function has_validation_strategy($validation_strategy_classnames) |
|
| 453 | - { |
|
| 454 | - $validation_strategy_classnames = is_array($validation_strategy_classnames) |
|
| 455 | - ? $validation_strategy_classnames |
|
| 456 | - : array($validation_strategy_classnames); |
|
| 457 | - foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
| 458 | - if (in_array($key, $validation_strategy_classnames)) { |
|
| 459 | - return true; |
|
| 460 | - } |
|
| 461 | - } |
|
| 462 | - return false; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * Gets the HTML |
|
| 469 | - * |
|
| 470 | - * @return string |
|
| 471 | - */ |
|
| 472 | - public function get_html() |
|
| 473 | - { |
|
| 474 | - return $this->_parent_section->get_html_for_input($this); |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * Gets the HTML for the input itself (no label or errors) according to the |
|
| 481 | - * input's display strategy |
|
| 482 | - * Makes sure the JS and CSS are enqueued for it |
|
| 483 | - * |
|
| 484 | - * @return string |
|
| 485 | - * @throws \EE_Error |
|
| 486 | - */ |
|
| 487 | - public function get_html_for_input() |
|
| 488 | - { |
|
| 489 | - return $this->_form_html_filter |
|
| 490 | - ? $this->_form_html_filter->filterHtml( |
|
| 491 | - $this->_get_display_strategy()->display(), |
|
| 492 | - $this |
|
| 493 | - ) |
|
| 494 | - : $this->_get_display_strategy()->display(); |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - |
|
| 498 | - |
|
| 499 | - /** |
|
| 500 | - * @return string |
|
| 501 | - */ |
|
| 502 | - public function html_other_attributes() |
|
| 503 | - { |
|
| 504 | - return ! empty($this->_html_other_attributes) ? ' ' . $this->_html_other_attributes : ''; |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - |
|
| 508 | - |
|
| 509 | - /** |
|
| 510 | - * @param string $html_other_attributes |
|
| 511 | - */ |
|
| 512 | - public function set_html_other_attributes($html_other_attributes) |
|
| 513 | - { |
|
| 514 | - $this->_html_other_attributes = $html_other_attributes; |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - |
|
| 518 | - |
|
| 519 | - /** |
|
| 520 | - * Gets the HTML for displaying the label for this form input |
|
| 521 | - * according to the form section's layout strategy |
|
| 522 | - * |
|
| 523 | - * @return string |
|
| 524 | - */ |
|
| 525 | - public function get_html_for_label() |
|
| 526 | - { |
|
| 527 | - return $this->_parent_section->get_layout_strategy()->display_label($this); |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - |
|
| 531 | - |
|
| 532 | - /** |
|
| 533 | - * Gets the HTML for displaying the errors section for this form input |
|
| 534 | - * according to the form section's layout strategy |
|
| 535 | - * |
|
| 536 | - * @return string |
|
| 537 | - */ |
|
| 538 | - public function get_html_for_errors() |
|
| 539 | - { |
|
| 540 | - return $this->_parent_section->get_layout_strategy()->display_errors($this); |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * Gets the HTML for displaying the help text for this form input |
|
| 547 | - * according to the form section's layout strategy |
|
| 548 | - * |
|
| 549 | - * @return string |
|
| 550 | - */ |
|
| 551 | - public function get_html_for_help() |
|
| 552 | - { |
|
| 553 | - return $this->_parent_section->get_layout_strategy()->display_help_text($this); |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - |
|
| 557 | - |
|
| 558 | - /** |
|
| 559 | - * Validates the input's sanitized value (assumes _sanitize() has already been called) |
|
| 560 | - * and returns whether or not the form input's submitted value is value |
|
| 561 | - * |
|
| 562 | - * @return boolean |
|
| 563 | - */ |
|
| 564 | - protected function _validate() |
|
| 565 | - { |
|
| 566 | - foreach ($this->_validation_strategies as $validation_strategy) { |
|
| 567 | - if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
| 568 | - try { |
|
| 569 | - $validation_strategy->validate($this->normalized_value()); |
|
| 570 | - } catch (EE_Validation_Error $e) { |
|
| 571 | - $this->add_validation_error($e); |
|
| 572 | - } |
|
| 573 | - } |
|
| 574 | - } |
|
| 575 | - if ($this->get_validation_errors()) { |
|
| 576 | - return false; |
|
| 577 | - } else { |
|
| 578 | - return true; |
|
| 579 | - } |
|
| 580 | - } |
|
| 581 | - |
|
| 582 | - |
|
| 583 | - |
|
| 584 | - /** |
|
| 585 | - * Performs basic sanitization on this value. But what sanitization can be performed anyways? |
|
| 586 | - * This value MIGHT be allowed to have tags, so we can't really remove them. |
|
| 587 | - * |
|
| 588 | - * @param string $value |
|
| 589 | - * @return null|string |
|
| 590 | - */ |
|
| 591 | - private function _sanitize($value) |
|
| 592 | - { |
|
| 593 | - return $value !== null ? trim($value) : null; |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * Picks out the form value that relates to this form input, |
|
| 600 | - * and stores it as the sanitized value on the form input, and sets the normalized value. |
|
| 601 | - * Returns whether or not any validation errors occurred |
|
| 602 | - * |
|
| 603 | - * @param array $req_data like $_POST |
|
| 604 | - * @return boolean whether or not there was an error |
|
| 605 | - * @throws \EE_Error |
|
| 606 | - */ |
|
| 607 | - protected function _normalize($req_data) |
|
| 608 | - { |
|
| 609 | - //any existing validation errors don't apply so clear them |
|
| 610 | - $this->_validation_errors = array(); |
|
| 611 | - try { |
|
| 612 | - $raw_input = $this->find_form_data_for_this_section($req_data); |
|
| 613 | - //super simple sanitization for now |
|
| 614 | - if (is_array($raw_input)) { |
|
| 615 | - $raw_value = array(); |
|
| 616 | - foreach ($raw_input as $key => $value) { |
|
| 617 | - $raw_value[$key] = $this->_sanitize($value); |
|
| 618 | - } |
|
| 619 | - $this->_set_raw_value($raw_value); |
|
| 620 | - } else { |
|
| 621 | - $this->_set_raw_value($this->_sanitize($raw_input)); |
|
| 622 | - } |
|
| 623 | - //we want to mostly leave the input alone in case we need to re-display it to the user |
|
| 624 | - $this->_set_normalized_value($this->_normalization_strategy->normalize($this->raw_value())); |
|
| 625 | - } catch (EE_Validation_Error $e) { |
|
| 626 | - $this->add_validation_error($e); |
|
| 627 | - } |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - |
|
| 631 | - |
|
| 632 | - /** |
|
| 633 | - * @return string |
|
| 634 | - */ |
|
| 635 | - public function html_name() |
|
| 636 | - { |
|
| 637 | - $this->_set_default_html_name_if_empty(); |
|
| 638 | - return $this->_html_name; |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - |
|
| 642 | - |
|
| 643 | - /** |
|
| 644 | - * @return string |
|
| 645 | - */ |
|
| 646 | - public function html_label_id() |
|
| 647 | - { |
|
| 648 | - return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id() . '-lbl'; |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - |
|
| 652 | - |
|
| 653 | - /** |
|
| 654 | - * @return string |
|
| 655 | - */ |
|
| 656 | - public function html_label_class() |
|
| 657 | - { |
|
| 658 | - return $this->_html_label_class; |
|
| 659 | - } |
|
| 660 | - |
|
| 661 | - |
|
| 662 | - |
|
| 663 | - /** |
|
| 664 | - * @return string |
|
| 665 | - */ |
|
| 666 | - public function html_label_style() |
|
| 667 | - { |
|
| 668 | - return $this->_html_label_style; |
|
| 669 | - } |
|
| 670 | - |
|
| 671 | - |
|
| 672 | - |
|
| 673 | - /** |
|
| 674 | - * @return string |
|
| 675 | - */ |
|
| 676 | - public function html_label_text() |
|
| 677 | - { |
|
| 678 | - return $this->_html_label_text; |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - |
|
| 682 | - |
|
| 683 | - /** |
|
| 684 | - * @return string |
|
| 685 | - */ |
|
| 686 | - public function html_help_text() |
|
| 687 | - { |
|
| 688 | - return $this->_html_help_text; |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - |
|
| 692 | - |
|
| 693 | - /** |
|
| 694 | - * @return string |
|
| 695 | - */ |
|
| 696 | - public function html_help_class() |
|
| 697 | - { |
|
| 698 | - return $this->_html_help_class; |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - |
|
| 702 | - |
|
| 703 | - /** |
|
| 704 | - * @return string |
|
| 705 | - */ |
|
| 706 | - public function html_help_style() |
|
| 707 | - { |
|
| 708 | - return $this->_html_style; |
|
| 709 | - } |
|
| 710 | - |
|
| 711 | - |
|
| 712 | - |
|
| 713 | - /** |
|
| 714 | - * returns the raw, UNSAFE, input, almost exactly as the user submitted it. |
|
| 715 | - * Please note that almost all client code should instead use the normalized_value; |
|
| 716 | - * or possibly raw_value_in_form (which prepares the string for displaying in an HTML attribute on a tag, |
|
| 717 | - * mostly by escaping quotes) |
|
| 718 | - * Note, we do not store the exact original value sent in the user's request because |
|
| 719 | - * it may have malicious content, and we MIGHT want to store the form input in a transient or something... |
|
| 720 | - * in which case, we would have stored the malicious content to our database. |
|
| 721 | - * |
|
| 722 | - * @return string |
|
| 723 | - */ |
|
| 724 | - public function raw_value() |
|
| 725 | - { |
|
| 726 | - return $this->_raw_value; |
|
| 727 | - } |
|
| 728 | - |
|
| 729 | - |
|
| 730 | - |
|
| 731 | - /** |
|
| 732 | - * Returns a string safe to usage in form inputs when displaying, because |
|
| 733 | - * it escapes all html entities |
|
| 734 | - * |
|
| 735 | - * @return string |
|
| 736 | - */ |
|
| 737 | - public function raw_value_in_form() |
|
| 738 | - { |
|
| 739 | - return htmlentities($this->raw_value(), ENT_QUOTES, 'UTF-8'); |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - |
|
| 743 | - |
|
| 744 | - /** |
|
| 745 | - * returns the value after it's been sanitized, and then converted into it's proper type |
|
| 746 | - * in PHP. Eg, a string, an int, an array, |
|
| 747 | - * |
|
| 748 | - * @return mixed |
|
| 749 | - */ |
|
| 750 | - public function normalized_value() |
|
| 751 | - { |
|
| 752 | - return $this->_normalized_value; |
|
| 753 | - } |
|
| 754 | - |
|
| 755 | - |
|
| 756 | - |
|
| 757 | - /** |
|
| 758 | - * Returns the normalized value is a presentable way. By default this is just |
|
| 759 | - * the normalized value by itself, but it can be overridden for when that's not |
|
| 760 | - * the best thing to display |
|
| 761 | - * |
|
| 762 | - * @return string |
|
| 763 | - */ |
|
| 764 | - public function pretty_value() |
|
| 765 | - { |
|
| 766 | - return $this->_normalized_value; |
|
| 767 | - } |
|
| 768 | - |
|
| 769 | - |
|
| 770 | - |
|
| 771 | - /** |
|
| 772 | - * When generating the JS for the jquery validation rules like<br> |
|
| 773 | - * <code>$( "#myform" ).validate({ |
|
| 774 | - * rules: { |
|
| 775 | - * password: "required", |
|
| 776 | - * password_again: { |
|
| 777 | - * equalTo: "#password" |
|
| 778 | - * } |
|
| 779 | - * } |
|
| 780 | - * });</code> |
|
| 781 | - * if this field had the name 'password_again', it should return |
|
| 782 | - * <br><code>password_again: { |
|
| 783 | - * equalTo: "#password" |
|
| 784 | - * }</code> |
|
| 785 | - * |
|
| 786 | - * @return array |
|
| 787 | - */ |
|
| 788 | - public function get_jquery_validation_rules() |
|
| 789 | - { |
|
| 790 | - $jquery_validation_js = array(); |
|
| 791 | - $jquery_validation_rules = array(); |
|
| 792 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
| 793 | - $jquery_validation_rules = array_replace_recursive( |
|
| 794 | - $jquery_validation_rules, |
|
| 795 | - $validation_strategy->get_jquery_validation_rule_array() |
|
| 796 | - ); |
|
| 797 | - } |
|
| 798 | - if (! empty($jquery_validation_rules)) { |
|
| 799 | - foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) { |
|
| 800 | - $jquery_validation_js[$html_id_with_pound_sign] = $jquery_validation_rules; |
|
| 801 | - } |
|
| 802 | - } |
|
| 803 | - return $jquery_validation_js; |
|
| 804 | - } |
|
| 805 | - |
|
| 806 | - |
|
| 807 | - |
|
| 808 | - /** |
|
| 809 | - * Sets the input's default value for use in displaying in the form. Note: value should be |
|
| 810 | - * normalized (Eg, if providing a default of ra Yes_NO_Input you would provide TRUE or FALSE, not '1' or '0') |
|
| 811 | - * |
|
| 812 | - * @param mixed $value |
|
| 813 | - * @return void |
|
| 814 | - */ |
|
| 815 | - public function set_default($value) |
|
| 816 | - { |
|
| 817 | - $this->_set_normalized_value($value); |
|
| 818 | - $this->_set_raw_value($value); |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - |
|
| 822 | - |
|
| 823 | - /** |
|
| 824 | - * Sets the normalized value on this input |
|
| 825 | - * |
|
| 826 | - * @param mixed $value |
|
| 827 | - */ |
|
| 828 | - protected function _set_normalized_value($value) |
|
| 829 | - { |
|
| 830 | - $this->_normalized_value = $value; |
|
| 831 | - } |
|
| 832 | - |
|
| 833 | - |
|
| 834 | - |
|
| 835 | - /** |
|
| 836 | - * Sets the raw value on this input (ie, exactly as the user submitted it) |
|
| 837 | - * |
|
| 838 | - * @param mixed $value |
|
| 839 | - */ |
|
| 840 | - protected function _set_raw_value($value) |
|
| 841 | - { |
|
| 842 | - $this->_raw_value = $this->_normalization_strategy->unnormalize($value); |
|
| 843 | - } |
|
| 844 | - |
|
| 845 | - |
|
| 846 | - |
|
| 847 | - /** |
|
| 848 | - * Sets the HTML label text after it has already been defined |
|
| 849 | - * |
|
| 850 | - * @param string $label |
|
| 851 | - * @return void |
|
| 852 | - */ |
|
| 853 | - public function set_html_label_text($label) |
|
| 854 | - { |
|
| 855 | - $this->_html_label_text = $label; |
|
| 856 | - } |
|
| 857 | - |
|
| 858 | - |
|
| 859 | - |
|
| 860 | - /** |
|
| 861 | - * Sets whether or not this field is required, and adjusts the validation strategy. |
|
| 862 | - * If you want to use the EE_Conditionally_Required_Validation_Strategy, |
|
| 863 | - * please add it as a validation strategy using add_validation_strategy as normal |
|
| 864 | - * |
|
| 865 | - * @param boolean $required boolean |
|
| 866 | - * @param null $required_text |
|
| 867 | - */ |
|
| 868 | - public function set_required($required = true, $required_text = null) |
|
| 869 | - { |
|
| 870 | - $required = filter_var($required, FILTER_VALIDATE_BOOLEAN); |
|
| 871 | - //whether $required is a string or a boolean, we want to add a required validation strategy |
|
| 872 | - if ($required) { |
|
| 873 | - $this->_add_validation_strategy(new EE_Required_Validation_Strategy($required_text)); |
|
| 874 | - } else { |
|
| 875 | - $this->remove_validation_strategy('EE_Required_Validation_Strategy'); |
|
| 876 | - } |
|
| 877 | - $this->_required = $required; |
|
| 878 | - } |
|
| 879 | - |
|
| 880 | - |
|
| 881 | - |
|
| 882 | - /** |
|
| 883 | - * Returns whether or not this field is required |
|
| 884 | - * |
|
| 885 | - * @return boolean |
|
| 886 | - */ |
|
| 887 | - public function required() |
|
| 888 | - { |
|
| 889 | - return $this->_required; |
|
| 890 | - } |
|
| 891 | - |
|
| 892 | - |
|
| 893 | - |
|
| 894 | - /** |
|
| 895 | - * @param string $required_css_class |
|
| 896 | - */ |
|
| 897 | - public function set_required_css_class($required_css_class) |
|
| 898 | - { |
|
| 899 | - $this->_required_css_class = $required_css_class; |
|
| 900 | - } |
|
| 901 | - |
|
| 902 | - |
|
| 903 | - |
|
| 904 | - /** |
|
| 905 | - * @return string |
|
| 906 | - */ |
|
| 907 | - public function required_css_class() |
|
| 908 | - { |
|
| 909 | - return $this->_required_css_class; |
|
| 910 | - } |
|
| 911 | - |
|
| 912 | - |
|
| 913 | - |
|
| 914 | - /** |
|
| 915 | - * @param bool $add_required |
|
| 916 | - * @return string |
|
| 917 | - */ |
|
| 918 | - public function html_class($add_required = false) |
|
| 919 | - { |
|
| 920 | - return $add_required && $this->required() |
|
| 921 | - ? $this->required_css_class() . ' ' . $this->_html_class |
|
| 922 | - : $this->_html_class; |
|
| 923 | - } |
|
| 924 | - |
|
| 925 | - |
|
| 926 | - /** |
|
| 927 | - * Sets the help text, in case |
|
| 928 | - * |
|
| 929 | - * @param string $text |
|
| 930 | - */ |
|
| 931 | - public function set_html_help_text($text) |
|
| 932 | - { |
|
| 933 | - $this->_html_help_text = $text; |
|
| 934 | - } |
|
| 935 | - |
|
| 936 | - |
|
| 937 | - |
|
| 938 | - /** |
|
| 939 | - * Uses the sensitive data removal strategy to remove the sensitive data from this |
|
| 940 | - * input. If there is any kind of sensitive data removal on this input, we clear |
|
| 941 | - * out the raw value completely |
|
| 942 | - * |
|
| 943 | - * @return void |
|
| 944 | - */ |
|
| 945 | - public function clean_sensitive_data() |
|
| 946 | - { |
|
| 947 | - //if we do ANY kind of sensitive data removal on this, then just clear out the raw value |
|
| 948 | - //if we need more logic than this we'll make a strategy for it |
|
| 949 | - if ($this->_sensitive_data_removal_strategy |
|
| 950 | - && ! $this->_sensitive_data_removal_strategy instanceof EE_No_Sensitive_Data_Removal |
|
| 951 | - ) { |
|
| 952 | - $this->_set_raw_value(null); |
|
| 953 | - } |
|
| 954 | - //and clean the normalized value according to the appropriate strategy |
|
| 955 | - $this->_set_normalized_value( |
|
| 956 | - $this->get_sensitive_data_removal_strategy()->remove_sensitive_data( |
|
| 957 | - $this->_normalized_value |
|
| 958 | - ) |
|
| 959 | - ); |
|
| 960 | - } |
|
| 961 | - |
|
| 962 | - |
|
| 963 | - |
|
| 964 | - /** |
|
| 965 | - * @param bool $primary |
|
| 966 | - * @param string $button_size |
|
| 967 | - * @param string $other_attributes |
|
| 968 | - */ |
|
| 969 | - public function set_button_css_attributes($primary = true, $button_size = '', $other_attributes = '') |
|
| 970 | - { |
|
| 971 | - $button_css_attributes = 'button'; |
|
| 972 | - $button_css_attributes .= $primary === true ? ' button-primary' : ' button-secondary'; |
|
| 973 | - switch ($button_size) { |
|
| 974 | - case 'xs' : |
|
| 975 | - case 'extra-small' : |
|
| 976 | - $button_css_attributes .= ' button-xs'; |
|
| 977 | - break; |
|
| 978 | - case 'sm' : |
|
| 979 | - case 'small' : |
|
| 980 | - $button_css_attributes .= ' button-sm'; |
|
| 981 | - break; |
|
| 982 | - case 'lg' : |
|
| 983 | - case 'large' : |
|
| 984 | - $button_css_attributes .= ' button-lg'; |
|
| 985 | - break; |
|
| 986 | - case 'block' : |
|
| 987 | - $button_css_attributes .= ' button-block'; |
|
| 988 | - break; |
|
| 989 | - case 'md' : |
|
| 990 | - case 'medium' : |
|
| 991 | - default : |
|
| 992 | - $button_css_attributes .= ''; |
|
| 993 | - } |
|
| 994 | - $this->_button_css_attributes .= ! empty($other_attributes) |
|
| 995 | - ? $button_css_attributes . ' ' . $other_attributes |
|
| 996 | - : $button_css_attributes; |
|
| 997 | - } |
|
| 998 | - |
|
| 999 | - |
|
| 1000 | - |
|
| 1001 | - /** |
|
| 1002 | - * @return string |
|
| 1003 | - */ |
|
| 1004 | - public function button_css_attributes() |
|
| 1005 | - { |
|
| 1006 | - if (empty($this->_button_css_attributes)) { |
|
| 1007 | - $this->set_button_css_attributes(); |
|
| 1008 | - } |
|
| 1009 | - return $this->_button_css_attributes; |
|
| 1010 | - } |
|
| 1011 | - |
|
| 1012 | - |
|
| 1013 | - |
|
| 1014 | - /** |
|
| 1015 | - * find_form_data_for_this_section |
|
| 1016 | - * using this section's name and its parents, finds the value of the form data that corresponds to it. |
|
| 1017 | - * For example, if this form section's HTML name is my_form[subform][form_input_1], |
|
| 1018 | - * then it's value should be in $_REQUEST at $_REQUEST['my_form']['subform']['form_input_1']. |
|
| 1019 | - * (If that doesn't exist, we also check for this subsection's name |
|
| 1020 | - * at the TOP LEVEL of the request data. Eg $_REQUEST['form_input_1'].) |
|
| 1021 | - * This function finds its value in the form. |
|
| 1022 | - * |
|
| 1023 | - * @param array $req_data |
|
| 1024 | - * @return mixed whatever the raw value of this form section is in the request data |
|
| 1025 | - * @throws \EE_Error |
|
| 1026 | - */ |
|
| 1027 | - public function find_form_data_for_this_section($req_data) |
|
| 1028 | - { |
|
| 1029 | - // break up the html name by "[]" |
|
| 1030 | - if (strpos($this->html_name(), '[') !== false) { |
|
| 1031 | - $before_any_brackets = substr($this->html_name(), 0, strpos($this->html_name(), '[')); |
|
| 1032 | - } else { |
|
| 1033 | - $before_any_brackets = $this->html_name(); |
|
| 1034 | - } |
|
| 1035 | - // grab all of the segments |
|
| 1036 | - preg_match_all('~\[([^]]*)\]~', $this->html_name(), $matches); |
|
| 1037 | - if (isset($matches[1]) && is_array($matches[1])) { |
|
| 1038 | - $name_parts = $matches[1]; |
|
| 1039 | - array_unshift($name_parts, $before_any_brackets); |
|
| 1040 | - } else { |
|
| 1041 | - $name_parts = array($before_any_brackets); |
|
| 1042 | - } |
|
| 1043 | - // now get the value for the input |
|
| 1044 | - $value = $this->_find_form_data_for_this_section_using_name_parts($name_parts, $req_data); |
|
| 1045 | - // check if this thing's name is at the TOP level of the request data |
|
| 1046 | - if ($value === null && isset($req_data[$this->name()])) { |
|
| 1047 | - $value = $req_data[$this->name()]; |
|
| 1048 | - } |
|
| 1049 | - return $value; |
|
| 1050 | - } |
|
| 1051 | - |
|
| 1052 | - |
|
| 1053 | - |
|
| 1054 | - /** |
|
| 1055 | - * @param array $html_name_parts |
|
| 1056 | - * @param array $req_data |
|
| 1057 | - * @return array | NULL |
|
| 1058 | - */ |
|
| 1059 | - public function _find_form_data_for_this_section_using_name_parts($html_name_parts, $req_data) |
|
| 1060 | - { |
|
| 1061 | - $first_part_to_consider = array_shift($html_name_parts); |
|
| 1062 | - if (isset($req_data[$first_part_to_consider])) { |
|
| 1063 | - if (empty($html_name_parts)) { |
|
| 1064 | - return $req_data[$first_part_to_consider]; |
|
| 1065 | - } else { |
|
| 1066 | - return $this->_find_form_data_for_this_section_using_name_parts( |
|
| 1067 | - $html_name_parts, |
|
| 1068 | - $req_data[$first_part_to_consider] |
|
| 1069 | - ); |
|
| 1070 | - } |
|
| 1071 | - } else { |
|
| 1072 | - return null; |
|
| 1073 | - } |
|
| 1074 | - } |
|
| 1075 | - |
|
| 1076 | - |
|
| 1077 | - |
|
| 1078 | - /** |
|
| 1079 | - * Checks if this form input's data is in the request data |
|
| 1080 | - * |
|
| 1081 | - * @param array $req_data like $_POST |
|
| 1082 | - * @return boolean |
|
| 1083 | - * @throws \EE_Error |
|
| 1084 | - */ |
|
| 1085 | - public function form_data_present_in($req_data = null) |
|
| 1086 | - { |
|
| 1087 | - if ($req_data === null) { |
|
| 1088 | - $req_data = $_POST; |
|
| 1089 | - } |
|
| 1090 | - $checked_value = $this->find_form_data_for_this_section($req_data); |
|
| 1091 | - if ($checked_value !== null) { |
|
| 1092 | - return true; |
|
| 1093 | - } else { |
|
| 1094 | - return false; |
|
| 1095 | - } |
|
| 1096 | - } |
|
| 1097 | - |
|
| 1098 | - |
|
| 1099 | - |
|
| 1100 | - /** |
|
| 1101 | - * Overrides parent to add js data from validation and display strategies |
|
| 1102 | - * |
|
| 1103 | - * @param array $form_other_js_data |
|
| 1104 | - * @return array |
|
| 1105 | - */ |
|
| 1106 | - public function get_other_js_data($form_other_js_data = array()) |
|
| 1107 | - { |
|
| 1108 | - $form_other_js_data = $this->get_other_js_data_from_strategies($form_other_js_data); |
|
| 1109 | - return $form_other_js_data; |
|
| 1110 | - } |
|
| 1111 | - |
|
| 1112 | - |
|
| 1113 | - |
|
| 1114 | - /** |
|
| 1115 | - * Gets other JS data for localization from this input's strategies, like |
|
| 1116 | - * the validation strategies and the display strategy |
|
| 1117 | - * |
|
| 1118 | - * @param array $form_other_js_data |
|
| 1119 | - * @return array |
|
| 1120 | - */ |
|
| 1121 | - public function get_other_js_data_from_strategies($form_other_js_data = array()) |
|
| 1122 | - { |
|
| 1123 | - $form_other_js_data = $this->get_display_strategy()->get_other_js_data($form_other_js_data); |
|
| 1124 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
| 1125 | - $form_other_js_data = $validation_strategy->get_other_js_data($form_other_js_data); |
|
| 1126 | - } |
|
| 1127 | - return $form_other_js_data; |
|
| 1128 | - } |
|
| 1129 | - |
|
| 1130 | - |
|
| 1131 | - |
|
| 1132 | - /** |
|
| 1133 | - * Override parent because we want to give our strategies an opportunity to enqueue some js and css |
|
| 1134 | - * |
|
| 1135 | - * @return void |
|
| 1136 | - */ |
|
| 1137 | - public function enqueue_js() |
|
| 1138 | - { |
|
| 1139 | - //ask our display strategy and validation strategies if they have js to enqueue |
|
| 1140 | - $this->enqueue_js_from_strategies(); |
|
| 1141 | - } |
|
| 1142 | - |
|
| 1143 | - |
|
| 1144 | - |
|
| 1145 | - /** |
|
| 1146 | - * Tells strategies when its ok to enqueue their js and css |
|
| 1147 | - * |
|
| 1148 | - * @return void |
|
| 1149 | - */ |
|
| 1150 | - public function enqueue_js_from_strategies() |
|
| 1151 | - { |
|
| 1152 | - $this->get_display_strategy()->enqueue_js(); |
|
| 1153 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
| 1154 | - $validation_strategy->enqueue_js(); |
|
| 1155 | - } |
|
| 1156 | - } |
|
| 19 | + /** |
|
| 20 | + * the input's name attribute |
|
| 21 | + * |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | + protected $_html_name; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * id for the html label tag |
|
| 28 | + * |
|
| 29 | + * @var string |
|
| 30 | + */ |
|
| 31 | + protected $_html_label_id; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * class for teh html label tag |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + protected $_html_label_class; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * any additional html attributes that you may want to add |
|
| 42 | + * |
|
| 43 | + * @var string |
|
| 44 | + */ |
|
| 45 | + protected $_html_other_attributes; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * style for teh html label tag |
|
| 49 | + * |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $_html_label_style; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * text to be placed in the html label |
|
| 56 | + * |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + protected $_html_label_text; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * the full html label. If used, all other html_label_* properties are invalid |
|
| 63 | + * |
|
| 64 | + * @var string |
|
| 65 | + */ |
|
| 66 | + protected $_html_label; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * HTML to use for help text (normally placed below form input), in a span which normally |
|
| 70 | + * has a class of 'description' |
|
| 71 | + * |
|
| 72 | + * @var string |
|
| 73 | + */ |
|
| 74 | + protected $_html_help_text; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * CSS classes for displaying the help span |
|
| 78 | + * |
|
| 79 | + * @var string |
|
| 80 | + */ |
|
| 81 | + protected $_html_help_class = 'description'; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * CSS to put in the style attribute on the help span |
|
| 85 | + * |
|
| 86 | + * @var string |
|
| 87 | + */ |
|
| 88 | + protected $_html_help_style; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Stores whether or not this input's response is required. |
|
| 92 | + * Because certain styling elements may also want to know that this |
|
| 93 | + * input is required etc. |
|
| 94 | + * |
|
| 95 | + * @var boolean |
|
| 96 | + */ |
|
| 97 | + protected $_required; |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * css class added to required inputs |
|
| 101 | + * |
|
| 102 | + * @var string |
|
| 103 | + */ |
|
| 104 | + protected $_required_css_class = 'ee-required'; |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * css styles applied to button type inputs |
|
| 108 | + * |
|
| 109 | + * @var string |
|
| 110 | + */ |
|
| 111 | + protected $_button_css_attributes; |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * The raw data submitted for this, like in the $_POST super global. |
|
| 115 | + * Generally unsafe for usage in client code |
|
| 116 | + * |
|
| 117 | + * @var mixed string or array |
|
| 118 | + */ |
|
| 119 | + protected $_raw_value; |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Value normalized according to the input's normalization strategy. |
|
| 123 | + * The normalization strategy dictates whether this is a string, int, float, |
|
| 124 | + * boolean, or array of any of those. |
|
| 125 | + * |
|
| 126 | + * @var mixed |
|
| 127 | + */ |
|
| 128 | + protected $_normalized_value; |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Strategy used for displaying this field. |
|
| 132 | + * Child classes must use _get_display_strategy to access it. |
|
| 133 | + * |
|
| 134 | + * @var EE_Display_Strategy_Base |
|
| 135 | + */ |
|
| 136 | + private $_display_strategy; |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Gets all the validation strategies used on this field |
|
| 140 | + * |
|
| 141 | + * @var EE_Validation_Strategy_Base[] |
|
| 142 | + */ |
|
| 143 | + private $_validation_strategies = array(); |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * The normalization strategy for this field |
|
| 147 | + * |
|
| 148 | + * @var EE_Normalization_Strategy_Base |
|
| 149 | + */ |
|
| 150 | + private $_normalization_strategy; |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Strategy for removing sensitive data after we're done with the form input |
|
| 154 | + * |
|
| 155 | + * @var EE_Sensitive_Data_Removal_Base |
|
| 156 | + */ |
|
| 157 | + protected $_sensitive_data_removal_strategy; |
|
| 158 | + |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @param array $input_args { |
|
| 163 | + * @type string $html_name the html name for the input |
|
| 164 | + * @type string $html_label_id the id attribute to give to the html label tag |
|
| 165 | + * @type string $html_label_class the class attribute to give to the html label tag |
|
| 166 | + * @type string $html_label_style the style attribute to give ot teh label tag |
|
| 167 | + * @type string $html_label_text the text to put in the label tag |
|
| 168 | + * @type string $html_label the full html label. If used, |
|
| 169 | + * all other html_label_* args are invalid |
|
| 170 | + * @type string $html_help_text text to put in help element |
|
| 171 | + * @type string $html_help_style style attribute to give to teh help element |
|
| 172 | + * @type string $html_help_class class attribute to give to the help element |
|
| 173 | + * @type string $default default value NORMALIZED (eg, if providing the default |
|
| 174 | + * for a Yes_No_Input, you should provide TRUE or FALSE, not '1' or '0') |
|
| 175 | + * @type EE_Display_Strategy_Base $display strategy |
|
| 176 | + * @type EE_Normalization_Strategy_Base $normalization_strategy |
|
| 177 | + * @type EE_Validation_Strategy_Base[] $validation_strategies |
|
| 178 | + * } |
|
| 179 | + */ |
|
| 180 | + public function __construct($input_args = array()) |
|
| 181 | + { |
|
| 182 | + $input_args = (array)apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this); |
|
| 183 | + // the following properties must be cast as arrays |
|
| 184 | + if (isset($input_args['validation_strategies'])) { |
|
| 185 | + foreach ((array)$input_args['validation_strategies'] as $validation_strategy) { |
|
| 186 | + if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
| 187 | + $this->_validation_strategies[get_class($validation_strategy)] = $validation_strategy; |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + unset($input_args['validation_strategies']); |
|
| 191 | + } |
|
| 192 | + // loop thru incoming options |
|
| 193 | + foreach ($input_args as $key => $value) { |
|
| 194 | + // add underscore to $key to match property names |
|
| 195 | + $_key = '_' . $key; |
|
| 196 | + if (property_exists($this, $_key)) { |
|
| 197 | + $this->{$_key} = $value; |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + // ensure that "required" is set correctly |
|
| 201 | + $this->set_required( |
|
| 202 | + $this->_required, isset($input_args['required_validation_error_message']) |
|
| 203 | + ? $input_args['required_validation_error_message'] |
|
| 204 | + : null |
|
| 205 | + ); |
|
| 206 | + //$this->_html_name_specified = isset( $input_args['html_name'] ) ? TRUE : FALSE; |
|
| 207 | + $this->_display_strategy->_construct_finalize($this); |
|
| 208 | + foreach ($this->_validation_strategies as $validation_strategy) { |
|
| 209 | + $validation_strategy->_construct_finalize($this); |
|
| 210 | + } |
|
| 211 | + if (! $this->_normalization_strategy) { |
|
| 212 | + $this->_normalization_strategy = new EE_Text_Normalization(); |
|
| 213 | + } |
|
| 214 | + $this->_normalization_strategy->_construct_finalize($this); |
|
| 215 | + //at least we can use the normalization strategy to populate the default |
|
| 216 | + if (isset($input_args['default'])) { |
|
| 217 | + $this->set_default($input_args['default']); |
|
| 218 | + } |
|
| 219 | + if (! $this->_sensitive_data_removal_strategy) { |
|
| 220 | + $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal(); |
|
| 221 | + } |
|
| 222 | + $this->_sensitive_data_removal_strategy->_construct_finalize($this); |
|
| 223 | + parent::__construct($input_args); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Sets the html_name to its default value, if none was specified in teh constructor. |
|
| 230 | + * Calculation involves using the name and the parent's html_name |
|
| 231 | + * |
|
| 232 | + * @throws \EE_Error |
|
| 233 | + */ |
|
| 234 | + protected function _set_default_html_name_if_empty() |
|
| 235 | + { |
|
| 236 | + if (! $this->_html_name) { |
|
| 237 | + $this->_html_name = $this->name(); |
|
| 238 | + if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
| 239 | + $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]"; |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * @param $parent_form_section |
|
| 248 | + * @param $name |
|
| 249 | + * @throws \EE_Error |
|
| 250 | + */ |
|
| 251 | + public function _construct_finalize($parent_form_section, $name) |
|
| 252 | + { |
|
| 253 | + parent::_construct_finalize($parent_form_section, $name); |
|
| 254 | + if ($this->_html_label === null && $this->_html_label_text === null) { |
|
| 255 | + $this->_html_label_text = ucwords(str_replace("_", " ", $name)); |
|
| 256 | + } |
|
| 257 | + do_action('AHEE__EE_Form_Input_Base___construct_finalize__end', $this, $parent_form_section, $name); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Returns the strategy for displaying this form input. If none is set, throws an exception. |
|
| 264 | + * |
|
| 265 | + * @return EE_Display_Strategy_Base |
|
| 266 | + * @throws EE_Error |
|
| 267 | + */ |
|
| 268 | + protected function _get_display_strategy() |
|
| 269 | + { |
|
| 270 | + $this->ensure_construct_finalized_called(); |
|
| 271 | + if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
| 272 | + throw new EE_Error( |
|
| 273 | + sprintf( |
|
| 274 | + __( |
|
| 275 | + "Cannot get display strategy for form input with name %s and id %s, because it has not been set in the constructor", |
|
| 276 | + "event_espresso" |
|
| 277 | + ), |
|
| 278 | + $this->html_name(), |
|
| 279 | + $this->html_id() |
|
| 280 | + ) |
|
| 281 | + ); |
|
| 282 | + } else { |
|
| 283 | + return $this->_display_strategy; |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Sets the display strategy. |
|
| 291 | + * |
|
| 292 | + * @param EE_Display_Strategy_Base $strategy |
|
| 293 | + */ |
|
| 294 | + protected function _set_display_strategy(EE_Display_Strategy_Base $strategy) |
|
| 295 | + { |
|
| 296 | + $this->_display_strategy = $strategy; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Sets the sanitization strategy |
|
| 303 | + * |
|
| 304 | + * @param EE_Normalization_Strategy_Base $strategy |
|
| 305 | + */ |
|
| 306 | + protected function _set_normalization_strategy(EE_Normalization_Strategy_Base $strategy) |
|
| 307 | + { |
|
| 308 | + $this->_normalization_strategy = $strategy; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Gets sensitive_data_removal_strategy |
|
| 315 | + * |
|
| 316 | + * @return EE_Sensitive_Data_Removal_Base |
|
| 317 | + */ |
|
| 318 | + public function get_sensitive_data_removal_strategy() |
|
| 319 | + { |
|
| 320 | + return $this->_sensitive_data_removal_strategy; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * Sets sensitive_data_removal_strategy |
|
| 327 | + * |
|
| 328 | + * @param EE_Sensitive_Data_Removal_Base $sensitive_data_removal_strategy |
|
| 329 | + * @return boolean |
|
| 330 | + */ |
|
| 331 | + public function set_sensitive_data_removal_strategy($sensitive_data_removal_strategy) |
|
| 332 | + { |
|
| 333 | + $this->_sensitive_data_removal_strategy = $sensitive_data_removal_strategy; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Gets the display strategy for this input |
|
| 340 | + * |
|
| 341 | + * @return EE_Display_Strategy_Base |
|
| 342 | + */ |
|
| 343 | + public function get_display_strategy() |
|
| 344 | + { |
|
| 345 | + return $this->_display_strategy; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Overwrites the display strategy |
|
| 352 | + * |
|
| 353 | + * @param EE_Display_Strategy_Base $display_strategy |
|
| 354 | + */ |
|
| 355 | + public function set_display_strategy($display_strategy) |
|
| 356 | + { |
|
| 357 | + $this->_display_strategy = $display_strategy; |
|
| 358 | + $this->_display_strategy->_construct_finalize($this); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * Gets the normalization strategy set on this input |
|
| 365 | + * |
|
| 366 | + * @return EE_Normalization_Strategy_Base |
|
| 367 | + */ |
|
| 368 | + public function get_normalization_strategy() |
|
| 369 | + { |
|
| 370 | + return $this->_normalization_strategy; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Overwrites the normalization strategy |
|
| 377 | + * |
|
| 378 | + * @param EE_Normalization_Strategy_Base $normalization_strategy |
|
| 379 | + */ |
|
| 380 | + public function set_normalization_strategy($normalization_strategy) |
|
| 381 | + { |
|
| 382 | + $this->_normalization_strategy = $normalization_strategy; |
|
| 383 | + $this->_normalization_strategy->_construct_finalize($this); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Returns all teh validation strategies which apply to this field, numerically indexed |
|
| 390 | + * |
|
| 391 | + * @return EE_Validation_Strategy_Base[] |
|
| 392 | + */ |
|
| 393 | + public function get_validation_strategies() |
|
| 394 | + { |
|
| 395 | + return $this->_validation_strategies; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Adds this strategy to the field so it will be used in both JS validation and server-side validation |
|
| 402 | + * |
|
| 403 | + * @param EE_Validation_Strategy_Base $validation_strategy |
|
| 404 | + * @return void |
|
| 405 | + */ |
|
| 406 | + protected function _add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
| 407 | + { |
|
| 408 | + $validation_strategy->_construct_finalize($this); |
|
| 409 | + $this->_validation_strategies[] = $validation_strategy; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Adds a new validation strategy onto the form input |
|
| 416 | + * |
|
| 417 | + * @param EE_Validation_Strategy_Base $validation_strategy |
|
| 418 | + * @return void |
|
| 419 | + */ |
|
| 420 | + public function add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
| 421 | + { |
|
| 422 | + $this->_add_validation_strategy($validation_strategy); |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * The classname of the validation strategy to remove |
|
| 429 | + * |
|
| 430 | + * @param string $validation_strategy_classname |
|
| 431 | + */ |
|
| 432 | + public function remove_validation_strategy($validation_strategy_classname) |
|
| 433 | + { |
|
| 434 | + foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
| 435 | + if ( |
|
| 436 | + $validation_strategy instanceof $validation_strategy_classname |
|
| 437 | + || is_subclass_of($validation_strategy, $validation_strategy_classname) |
|
| 438 | + ) { |
|
| 439 | + unset($this->_validation_strategies[$key]); |
|
| 440 | + } |
|
| 441 | + } |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * returns true if input employs any of the validation strategy defined by the supplied array of classnames |
|
| 448 | + * |
|
| 449 | + * @param array $validation_strategy_classnames |
|
| 450 | + * @return bool |
|
| 451 | + */ |
|
| 452 | + public function has_validation_strategy($validation_strategy_classnames) |
|
| 453 | + { |
|
| 454 | + $validation_strategy_classnames = is_array($validation_strategy_classnames) |
|
| 455 | + ? $validation_strategy_classnames |
|
| 456 | + : array($validation_strategy_classnames); |
|
| 457 | + foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
| 458 | + if (in_array($key, $validation_strategy_classnames)) { |
|
| 459 | + return true; |
|
| 460 | + } |
|
| 461 | + } |
|
| 462 | + return false; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Gets the HTML |
|
| 469 | + * |
|
| 470 | + * @return string |
|
| 471 | + */ |
|
| 472 | + public function get_html() |
|
| 473 | + { |
|
| 474 | + return $this->_parent_section->get_html_for_input($this); |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * Gets the HTML for the input itself (no label or errors) according to the |
|
| 481 | + * input's display strategy |
|
| 482 | + * Makes sure the JS and CSS are enqueued for it |
|
| 483 | + * |
|
| 484 | + * @return string |
|
| 485 | + * @throws \EE_Error |
|
| 486 | + */ |
|
| 487 | + public function get_html_for_input() |
|
| 488 | + { |
|
| 489 | + return $this->_form_html_filter |
|
| 490 | + ? $this->_form_html_filter->filterHtml( |
|
| 491 | + $this->_get_display_strategy()->display(), |
|
| 492 | + $this |
|
| 493 | + ) |
|
| 494 | + : $this->_get_display_strategy()->display(); |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + |
|
| 498 | + |
|
| 499 | + /** |
|
| 500 | + * @return string |
|
| 501 | + */ |
|
| 502 | + public function html_other_attributes() |
|
| 503 | + { |
|
| 504 | + return ! empty($this->_html_other_attributes) ? ' ' . $this->_html_other_attributes : ''; |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + |
|
| 508 | + |
|
| 509 | + /** |
|
| 510 | + * @param string $html_other_attributes |
|
| 511 | + */ |
|
| 512 | + public function set_html_other_attributes($html_other_attributes) |
|
| 513 | + { |
|
| 514 | + $this->_html_other_attributes = $html_other_attributes; |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + |
|
| 518 | + |
|
| 519 | + /** |
|
| 520 | + * Gets the HTML for displaying the label for this form input |
|
| 521 | + * according to the form section's layout strategy |
|
| 522 | + * |
|
| 523 | + * @return string |
|
| 524 | + */ |
|
| 525 | + public function get_html_for_label() |
|
| 526 | + { |
|
| 527 | + return $this->_parent_section->get_layout_strategy()->display_label($this); |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + |
|
| 531 | + |
|
| 532 | + /** |
|
| 533 | + * Gets the HTML for displaying the errors section for this form input |
|
| 534 | + * according to the form section's layout strategy |
|
| 535 | + * |
|
| 536 | + * @return string |
|
| 537 | + */ |
|
| 538 | + public function get_html_for_errors() |
|
| 539 | + { |
|
| 540 | + return $this->_parent_section->get_layout_strategy()->display_errors($this); |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * Gets the HTML for displaying the help text for this form input |
|
| 547 | + * according to the form section's layout strategy |
|
| 548 | + * |
|
| 549 | + * @return string |
|
| 550 | + */ |
|
| 551 | + public function get_html_for_help() |
|
| 552 | + { |
|
| 553 | + return $this->_parent_section->get_layout_strategy()->display_help_text($this); |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + |
|
| 557 | + |
|
| 558 | + /** |
|
| 559 | + * Validates the input's sanitized value (assumes _sanitize() has already been called) |
|
| 560 | + * and returns whether or not the form input's submitted value is value |
|
| 561 | + * |
|
| 562 | + * @return boolean |
|
| 563 | + */ |
|
| 564 | + protected function _validate() |
|
| 565 | + { |
|
| 566 | + foreach ($this->_validation_strategies as $validation_strategy) { |
|
| 567 | + if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
| 568 | + try { |
|
| 569 | + $validation_strategy->validate($this->normalized_value()); |
|
| 570 | + } catch (EE_Validation_Error $e) { |
|
| 571 | + $this->add_validation_error($e); |
|
| 572 | + } |
|
| 573 | + } |
|
| 574 | + } |
|
| 575 | + if ($this->get_validation_errors()) { |
|
| 576 | + return false; |
|
| 577 | + } else { |
|
| 578 | + return true; |
|
| 579 | + } |
|
| 580 | + } |
|
| 581 | + |
|
| 582 | + |
|
| 583 | + |
|
| 584 | + /** |
|
| 585 | + * Performs basic sanitization on this value. But what sanitization can be performed anyways? |
|
| 586 | + * This value MIGHT be allowed to have tags, so we can't really remove them. |
|
| 587 | + * |
|
| 588 | + * @param string $value |
|
| 589 | + * @return null|string |
|
| 590 | + */ |
|
| 591 | + private function _sanitize($value) |
|
| 592 | + { |
|
| 593 | + return $value !== null ? trim($value) : null; |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * Picks out the form value that relates to this form input, |
|
| 600 | + * and stores it as the sanitized value on the form input, and sets the normalized value. |
|
| 601 | + * Returns whether or not any validation errors occurred |
|
| 602 | + * |
|
| 603 | + * @param array $req_data like $_POST |
|
| 604 | + * @return boolean whether or not there was an error |
|
| 605 | + * @throws \EE_Error |
|
| 606 | + */ |
|
| 607 | + protected function _normalize($req_data) |
|
| 608 | + { |
|
| 609 | + //any existing validation errors don't apply so clear them |
|
| 610 | + $this->_validation_errors = array(); |
|
| 611 | + try { |
|
| 612 | + $raw_input = $this->find_form_data_for_this_section($req_data); |
|
| 613 | + //super simple sanitization for now |
|
| 614 | + if (is_array($raw_input)) { |
|
| 615 | + $raw_value = array(); |
|
| 616 | + foreach ($raw_input as $key => $value) { |
|
| 617 | + $raw_value[$key] = $this->_sanitize($value); |
|
| 618 | + } |
|
| 619 | + $this->_set_raw_value($raw_value); |
|
| 620 | + } else { |
|
| 621 | + $this->_set_raw_value($this->_sanitize($raw_input)); |
|
| 622 | + } |
|
| 623 | + //we want to mostly leave the input alone in case we need to re-display it to the user |
|
| 624 | + $this->_set_normalized_value($this->_normalization_strategy->normalize($this->raw_value())); |
|
| 625 | + } catch (EE_Validation_Error $e) { |
|
| 626 | + $this->add_validation_error($e); |
|
| 627 | + } |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + |
|
| 631 | + |
|
| 632 | + /** |
|
| 633 | + * @return string |
|
| 634 | + */ |
|
| 635 | + public function html_name() |
|
| 636 | + { |
|
| 637 | + $this->_set_default_html_name_if_empty(); |
|
| 638 | + return $this->_html_name; |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + |
|
| 642 | + |
|
| 643 | + /** |
|
| 644 | + * @return string |
|
| 645 | + */ |
|
| 646 | + public function html_label_id() |
|
| 647 | + { |
|
| 648 | + return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id() . '-lbl'; |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + |
|
| 652 | + |
|
| 653 | + /** |
|
| 654 | + * @return string |
|
| 655 | + */ |
|
| 656 | + public function html_label_class() |
|
| 657 | + { |
|
| 658 | + return $this->_html_label_class; |
|
| 659 | + } |
|
| 660 | + |
|
| 661 | + |
|
| 662 | + |
|
| 663 | + /** |
|
| 664 | + * @return string |
|
| 665 | + */ |
|
| 666 | + public function html_label_style() |
|
| 667 | + { |
|
| 668 | + return $this->_html_label_style; |
|
| 669 | + } |
|
| 670 | + |
|
| 671 | + |
|
| 672 | + |
|
| 673 | + /** |
|
| 674 | + * @return string |
|
| 675 | + */ |
|
| 676 | + public function html_label_text() |
|
| 677 | + { |
|
| 678 | + return $this->_html_label_text; |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + |
|
| 682 | + |
|
| 683 | + /** |
|
| 684 | + * @return string |
|
| 685 | + */ |
|
| 686 | + public function html_help_text() |
|
| 687 | + { |
|
| 688 | + return $this->_html_help_text; |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + |
|
| 692 | + |
|
| 693 | + /** |
|
| 694 | + * @return string |
|
| 695 | + */ |
|
| 696 | + public function html_help_class() |
|
| 697 | + { |
|
| 698 | + return $this->_html_help_class; |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + |
|
| 702 | + |
|
| 703 | + /** |
|
| 704 | + * @return string |
|
| 705 | + */ |
|
| 706 | + public function html_help_style() |
|
| 707 | + { |
|
| 708 | + return $this->_html_style; |
|
| 709 | + } |
|
| 710 | + |
|
| 711 | + |
|
| 712 | + |
|
| 713 | + /** |
|
| 714 | + * returns the raw, UNSAFE, input, almost exactly as the user submitted it. |
|
| 715 | + * Please note that almost all client code should instead use the normalized_value; |
|
| 716 | + * or possibly raw_value_in_form (which prepares the string for displaying in an HTML attribute on a tag, |
|
| 717 | + * mostly by escaping quotes) |
|
| 718 | + * Note, we do not store the exact original value sent in the user's request because |
|
| 719 | + * it may have malicious content, and we MIGHT want to store the form input in a transient or something... |
|
| 720 | + * in which case, we would have stored the malicious content to our database. |
|
| 721 | + * |
|
| 722 | + * @return string |
|
| 723 | + */ |
|
| 724 | + public function raw_value() |
|
| 725 | + { |
|
| 726 | + return $this->_raw_value; |
|
| 727 | + } |
|
| 728 | + |
|
| 729 | + |
|
| 730 | + |
|
| 731 | + /** |
|
| 732 | + * Returns a string safe to usage in form inputs when displaying, because |
|
| 733 | + * it escapes all html entities |
|
| 734 | + * |
|
| 735 | + * @return string |
|
| 736 | + */ |
|
| 737 | + public function raw_value_in_form() |
|
| 738 | + { |
|
| 739 | + return htmlentities($this->raw_value(), ENT_QUOTES, 'UTF-8'); |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + |
|
| 743 | + |
|
| 744 | + /** |
|
| 745 | + * returns the value after it's been sanitized, and then converted into it's proper type |
|
| 746 | + * in PHP. Eg, a string, an int, an array, |
|
| 747 | + * |
|
| 748 | + * @return mixed |
|
| 749 | + */ |
|
| 750 | + public function normalized_value() |
|
| 751 | + { |
|
| 752 | + return $this->_normalized_value; |
|
| 753 | + } |
|
| 754 | + |
|
| 755 | + |
|
| 756 | + |
|
| 757 | + /** |
|
| 758 | + * Returns the normalized value is a presentable way. By default this is just |
|
| 759 | + * the normalized value by itself, but it can be overridden for when that's not |
|
| 760 | + * the best thing to display |
|
| 761 | + * |
|
| 762 | + * @return string |
|
| 763 | + */ |
|
| 764 | + public function pretty_value() |
|
| 765 | + { |
|
| 766 | + return $this->_normalized_value; |
|
| 767 | + } |
|
| 768 | + |
|
| 769 | + |
|
| 770 | + |
|
| 771 | + /** |
|
| 772 | + * When generating the JS for the jquery validation rules like<br> |
|
| 773 | + * <code>$( "#myform" ).validate({ |
|
| 774 | + * rules: { |
|
| 775 | + * password: "required", |
|
| 776 | + * password_again: { |
|
| 777 | + * equalTo: "#password" |
|
| 778 | + * } |
|
| 779 | + * } |
|
| 780 | + * });</code> |
|
| 781 | + * if this field had the name 'password_again', it should return |
|
| 782 | + * <br><code>password_again: { |
|
| 783 | + * equalTo: "#password" |
|
| 784 | + * }</code> |
|
| 785 | + * |
|
| 786 | + * @return array |
|
| 787 | + */ |
|
| 788 | + public function get_jquery_validation_rules() |
|
| 789 | + { |
|
| 790 | + $jquery_validation_js = array(); |
|
| 791 | + $jquery_validation_rules = array(); |
|
| 792 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
| 793 | + $jquery_validation_rules = array_replace_recursive( |
|
| 794 | + $jquery_validation_rules, |
|
| 795 | + $validation_strategy->get_jquery_validation_rule_array() |
|
| 796 | + ); |
|
| 797 | + } |
|
| 798 | + if (! empty($jquery_validation_rules)) { |
|
| 799 | + foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) { |
|
| 800 | + $jquery_validation_js[$html_id_with_pound_sign] = $jquery_validation_rules; |
|
| 801 | + } |
|
| 802 | + } |
|
| 803 | + return $jquery_validation_js; |
|
| 804 | + } |
|
| 805 | + |
|
| 806 | + |
|
| 807 | + |
|
| 808 | + /** |
|
| 809 | + * Sets the input's default value for use in displaying in the form. Note: value should be |
|
| 810 | + * normalized (Eg, if providing a default of ra Yes_NO_Input you would provide TRUE or FALSE, not '1' or '0') |
|
| 811 | + * |
|
| 812 | + * @param mixed $value |
|
| 813 | + * @return void |
|
| 814 | + */ |
|
| 815 | + public function set_default($value) |
|
| 816 | + { |
|
| 817 | + $this->_set_normalized_value($value); |
|
| 818 | + $this->_set_raw_value($value); |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + |
|
| 822 | + |
|
| 823 | + /** |
|
| 824 | + * Sets the normalized value on this input |
|
| 825 | + * |
|
| 826 | + * @param mixed $value |
|
| 827 | + */ |
|
| 828 | + protected function _set_normalized_value($value) |
|
| 829 | + { |
|
| 830 | + $this->_normalized_value = $value; |
|
| 831 | + } |
|
| 832 | + |
|
| 833 | + |
|
| 834 | + |
|
| 835 | + /** |
|
| 836 | + * Sets the raw value on this input (ie, exactly as the user submitted it) |
|
| 837 | + * |
|
| 838 | + * @param mixed $value |
|
| 839 | + */ |
|
| 840 | + protected function _set_raw_value($value) |
|
| 841 | + { |
|
| 842 | + $this->_raw_value = $this->_normalization_strategy->unnormalize($value); |
|
| 843 | + } |
|
| 844 | + |
|
| 845 | + |
|
| 846 | + |
|
| 847 | + /** |
|
| 848 | + * Sets the HTML label text after it has already been defined |
|
| 849 | + * |
|
| 850 | + * @param string $label |
|
| 851 | + * @return void |
|
| 852 | + */ |
|
| 853 | + public function set_html_label_text($label) |
|
| 854 | + { |
|
| 855 | + $this->_html_label_text = $label; |
|
| 856 | + } |
|
| 857 | + |
|
| 858 | + |
|
| 859 | + |
|
| 860 | + /** |
|
| 861 | + * Sets whether or not this field is required, and adjusts the validation strategy. |
|
| 862 | + * If you want to use the EE_Conditionally_Required_Validation_Strategy, |
|
| 863 | + * please add it as a validation strategy using add_validation_strategy as normal |
|
| 864 | + * |
|
| 865 | + * @param boolean $required boolean |
|
| 866 | + * @param null $required_text |
|
| 867 | + */ |
|
| 868 | + public function set_required($required = true, $required_text = null) |
|
| 869 | + { |
|
| 870 | + $required = filter_var($required, FILTER_VALIDATE_BOOLEAN); |
|
| 871 | + //whether $required is a string or a boolean, we want to add a required validation strategy |
|
| 872 | + if ($required) { |
|
| 873 | + $this->_add_validation_strategy(new EE_Required_Validation_Strategy($required_text)); |
|
| 874 | + } else { |
|
| 875 | + $this->remove_validation_strategy('EE_Required_Validation_Strategy'); |
|
| 876 | + } |
|
| 877 | + $this->_required = $required; |
|
| 878 | + } |
|
| 879 | + |
|
| 880 | + |
|
| 881 | + |
|
| 882 | + /** |
|
| 883 | + * Returns whether or not this field is required |
|
| 884 | + * |
|
| 885 | + * @return boolean |
|
| 886 | + */ |
|
| 887 | + public function required() |
|
| 888 | + { |
|
| 889 | + return $this->_required; |
|
| 890 | + } |
|
| 891 | + |
|
| 892 | + |
|
| 893 | + |
|
| 894 | + /** |
|
| 895 | + * @param string $required_css_class |
|
| 896 | + */ |
|
| 897 | + public function set_required_css_class($required_css_class) |
|
| 898 | + { |
|
| 899 | + $this->_required_css_class = $required_css_class; |
|
| 900 | + } |
|
| 901 | + |
|
| 902 | + |
|
| 903 | + |
|
| 904 | + /** |
|
| 905 | + * @return string |
|
| 906 | + */ |
|
| 907 | + public function required_css_class() |
|
| 908 | + { |
|
| 909 | + return $this->_required_css_class; |
|
| 910 | + } |
|
| 911 | + |
|
| 912 | + |
|
| 913 | + |
|
| 914 | + /** |
|
| 915 | + * @param bool $add_required |
|
| 916 | + * @return string |
|
| 917 | + */ |
|
| 918 | + public function html_class($add_required = false) |
|
| 919 | + { |
|
| 920 | + return $add_required && $this->required() |
|
| 921 | + ? $this->required_css_class() . ' ' . $this->_html_class |
|
| 922 | + : $this->_html_class; |
|
| 923 | + } |
|
| 924 | + |
|
| 925 | + |
|
| 926 | + /** |
|
| 927 | + * Sets the help text, in case |
|
| 928 | + * |
|
| 929 | + * @param string $text |
|
| 930 | + */ |
|
| 931 | + public function set_html_help_text($text) |
|
| 932 | + { |
|
| 933 | + $this->_html_help_text = $text; |
|
| 934 | + } |
|
| 935 | + |
|
| 936 | + |
|
| 937 | + |
|
| 938 | + /** |
|
| 939 | + * Uses the sensitive data removal strategy to remove the sensitive data from this |
|
| 940 | + * input. If there is any kind of sensitive data removal on this input, we clear |
|
| 941 | + * out the raw value completely |
|
| 942 | + * |
|
| 943 | + * @return void |
|
| 944 | + */ |
|
| 945 | + public function clean_sensitive_data() |
|
| 946 | + { |
|
| 947 | + //if we do ANY kind of sensitive data removal on this, then just clear out the raw value |
|
| 948 | + //if we need more logic than this we'll make a strategy for it |
|
| 949 | + if ($this->_sensitive_data_removal_strategy |
|
| 950 | + && ! $this->_sensitive_data_removal_strategy instanceof EE_No_Sensitive_Data_Removal |
|
| 951 | + ) { |
|
| 952 | + $this->_set_raw_value(null); |
|
| 953 | + } |
|
| 954 | + //and clean the normalized value according to the appropriate strategy |
|
| 955 | + $this->_set_normalized_value( |
|
| 956 | + $this->get_sensitive_data_removal_strategy()->remove_sensitive_data( |
|
| 957 | + $this->_normalized_value |
|
| 958 | + ) |
|
| 959 | + ); |
|
| 960 | + } |
|
| 961 | + |
|
| 962 | + |
|
| 963 | + |
|
| 964 | + /** |
|
| 965 | + * @param bool $primary |
|
| 966 | + * @param string $button_size |
|
| 967 | + * @param string $other_attributes |
|
| 968 | + */ |
|
| 969 | + public function set_button_css_attributes($primary = true, $button_size = '', $other_attributes = '') |
|
| 970 | + { |
|
| 971 | + $button_css_attributes = 'button'; |
|
| 972 | + $button_css_attributes .= $primary === true ? ' button-primary' : ' button-secondary'; |
|
| 973 | + switch ($button_size) { |
|
| 974 | + case 'xs' : |
|
| 975 | + case 'extra-small' : |
|
| 976 | + $button_css_attributes .= ' button-xs'; |
|
| 977 | + break; |
|
| 978 | + case 'sm' : |
|
| 979 | + case 'small' : |
|
| 980 | + $button_css_attributes .= ' button-sm'; |
|
| 981 | + break; |
|
| 982 | + case 'lg' : |
|
| 983 | + case 'large' : |
|
| 984 | + $button_css_attributes .= ' button-lg'; |
|
| 985 | + break; |
|
| 986 | + case 'block' : |
|
| 987 | + $button_css_attributes .= ' button-block'; |
|
| 988 | + break; |
|
| 989 | + case 'md' : |
|
| 990 | + case 'medium' : |
|
| 991 | + default : |
|
| 992 | + $button_css_attributes .= ''; |
|
| 993 | + } |
|
| 994 | + $this->_button_css_attributes .= ! empty($other_attributes) |
|
| 995 | + ? $button_css_attributes . ' ' . $other_attributes |
|
| 996 | + : $button_css_attributes; |
|
| 997 | + } |
|
| 998 | + |
|
| 999 | + |
|
| 1000 | + |
|
| 1001 | + /** |
|
| 1002 | + * @return string |
|
| 1003 | + */ |
|
| 1004 | + public function button_css_attributes() |
|
| 1005 | + { |
|
| 1006 | + if (empty($this->_button_css_attributes)) { |
|
| 1007 | + $this->set_button_css_attributes(); |
|
| 1008 | + } |
|
| 1009 | + return $this->_button_css_attributes; |
|
| 1010 | + } |
|
| 1011 | + |
|
| 1012 | + |
|
| 1013 | + |
|
| 1014 | + /** |
|
| 1015 | + * find_form_data_for_this_section |
|
| 1016 | + * using this section's name and its parents, finds the value of the form data that corresponds to it. |
|
| 1017 | + * For example, if this form section's HTML name is my_form[subform][form_input_1], |
|
| 1018 | + * then it's value should be in $_REQUEST at $_REQUEST['my_form']['subform']['form_input_1']. |
|
| 1019 | + * (If that doesn't exist, we also check for this subsection's name |
|
| 1020 | + * at the TOP LEVEL of the request data. Eg $_REQUEST['form_input_1'].) |
|
| 1021 | + * This function finds its value in the form. |
|
| 1022 | + * |
|
| 1023 | + * @param array $req_data |
|
| 1024 | + * @return mixed whatever the raw value of this form section is in the request data |
|
| 1025 | + * @throws \EE_Error |
|
| 1026 | + */ |
|
| 1027 | + public function find_form_data_for_this_section($req_data) |
|
| 1028 | + { |
|
| 1029 | + // break up the html name by "[]" |
|
| 1030 | + if (strpos($this->html_name(), '[') !== false) { |
|
| 1031 | + $before_any_brackets = substr($this->html_name(), 0, strpos($this->html_name(), '[')); |
|
| 1032 | + } else { |
|
| 1033 | + $before_any_brackets = $this->html_name(); |
|
| 1034 | + } |
|
| 1035 | + // grab all of the segments |
|
| 1036 | + preg_match_all('~\[([^]]*)\]~', $this->html_name(), $matches); |
|
| 1037 | + if (isset($matches[1]) && is_array($matches[1])) { |
|
| 1038 | + $name_parts = $matches[1]; |
|
| 1039 | + array_unshift($name_parts, $before_any_brackets); |
|
| 1040 | + } else { |
|
| 1041 | + $name_parts = array($before_any_brackets); |
|
| 1042 | + } |
|
| 1043 | + // now get the value for the input |
|
| 1044 | + $value = $this->_find_form_data_for_this_section_using_name_parts($name_parts, $req_data); |
|
| 1045 | + // check if this thing's name is at the TOP level of the request data |
|
| 1046 | + if ($value === null && isset($req_data[$this->name()])) { |
|
| 1047 | + $value = $req_data[$this->name()]; |
|
| 1048 | + } |
|
| 1049 | + return $value; |
|
| 1050 | + } |
|
| 1051 | + |
|
| 1052 | + |
|
| 1053 | + |
|
| 1054 | + /** |
|
| 1055 | + * @param array $html_name_parts |
|
| 1056 | + * @param array $req_data |
|
| 1057 | + * @return array | NULL |
|
| 1058 | + */ |
|
| 1059 | + public function _find_form_data_for_this_section_using_name_parts($html_name_parts, $req_data) |
|
| 1060 | + { |
|
| 1061 | + $first_part_to_consider = array_shift($html_name_parts); |
|
| 1062 | + if (isset($req_data[$first_part_to_consider])) { |
|
| 1063 | + if (empty($html_name_parts)) { |
|
| 1064 | + return $req_data[$first_part_to_consider]; |
|
| 1065 | + } else { |
|
| 1066 | + return $this->_find_form_data_for_this_section_using_name_parts( |
|
| 1067 | + $html_name_parts, |
|
| 1068 | + $req_data[$first_part_to_consider] |
|
| 1069 | + ); |
|
| 1070 | + } |
|
| 1071 | + } else { |
|
| 1072 | + return null; |
|
| 1073 | + } |
|
| 1074 | + } |
|
| 1075 | + |
|
| 1076 | + |
|
| 1077 | + |
|
| 1078 | + /** |
|
| 1079 | + * Checks if this form input's data is in the request data |
|
| 1080 | + * |
|
| 1081 | + * @param array $req_data like $_POST |
|
| 1082 | + * @return boolean |
|
| 1083 | + * @throws \EE_Error |
|
| 1084 | + */ |
|
| 1085 | + public function form_data_present_in($req_data = null) |
|
| 1086 | + { |
|
| 1087 | + if ($req_data === null) { |
|
| 1088 | + $req_data = $_POST; |
|
| 1089 | + } |
|
| 1090 | + $checked_value = $this->find_form_data_for_this_section($req_data); |
|
| 1091 | + if ($checked_value !== null) { |
|
| 1092 | + return true; |
|
| 1093 | + } else { |
|
| 1094 | + return false; |
|
| 1095 | + } |
|
| 1096 | + } |
|
| 1097 | + |
|
| 1098 | + |
|
| 1099 | + |
|
| 1100 | + /** |
|
| 1101 | + * Overrides parent to add js data from validation and display strategies |
|
| 1102 | + * |
|
| 1103 | + * @param array $form_other_js_data |
|
| 1104 | + * @return array |
|
| 1105 | + */ |
|
| 1106 | + public function get_other_js_data($form_other_js_data = array()) |
|
| 1107 | + { |
|
| 1108 | + $form_other_js_data = $this->get_other_js_data_from_strategies($form_other_js_data); |
|
| 1109 | + return $form_other_js_data; |
|
| 1110 | + } |
|
| 1111 | + |
|
| 1112 | + |
|
| 1113 | + |
|
| 1114 | + /** |
|
| 1115 | + * Gets other JS data for localization from this input's strategies, like |
|
| 1116 | + * the validation strategies and the display strategy |
|
| 1117 | + * |
|
| 1118 | + * @param array $form_other_js_data |
|
| 1119 | + * @return array |
|
| 1120 | + */ |
|
| 1121 | + public function get_other_js_data_from_strategies($form_other_js_data = array()) |
|
| 1122 | + { |
|
| 1123 | + $form_other_js_data = $this->get_display_strategy()->get_other_js_data($form_other_js_data); |
|
| 1124 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
| 1125 | + $form_other_js_data = $validation_strategy->get_other_js_data($form_other_js_data); |
|
| 1126 | + } |
|
| 1127 | + return $form_other_js_data; |
|
| 1128 | + } |
|
| 1129 | + |
|
| 1130 | + |
|
| 1131 | + |
|
| 1132 | + /** |
|
| 1133 | + * Override parent because we want to give our strategies an opportunity to enqueue some js and css |
|
| 1134 | + * |
|
| 1135 | + * @return void |
|
| 1136 | + */ |
|
| 1137 | + public function enqueue_js() |
|
| 1138 | + { |
|
| 1139 | + //ask our display strategy and validation strategies if they have js to enqueue |
|
| 1140 | + $this->enqueue_js_from_strategies(); |
|
| 1141 | + } |
|
| 1142 | + |
|
| 1143 | + |
|
| 1144 | + |
|
| 1145 | + /** |
|
| 1146 | + * Tells strategies when its ok to enqueue their js and css |
|
| 1147 | + * |
|
| 1148 | + * @return void |
|
| 1149 | + */ |
|
| 1150 | + public function enqueue_js_from_strategies() |
|
| 1151 | + { |
|
| 1152 | + $this->get_display_strategy()->enqueue_js(); |
|
| 1153 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
| 1154 | + $validation_strategy->enqueue_js(); |
|
| 1155 | + } |
|
| 1156 | + } |
|
| 1157 | 1157 | } |
@@ -16,330 +16,330 @@ |
||
| 16 | 16 | class EE_Request implements InterminableInterface |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * $_GET parameters |
|
| 21 | - * |
|
| 22 | - * @var array $_get |
|
| 23 | - */ |
|
| 24 | - private $_get; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * $_POST parameters |
|
| 28 | - * |
|
| 29 | - * @var array $_post |
|
| 30 | - */ |
|
| 31 | - private $_post; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * $_COOKIE parameters |
|
| 35 | - * |
|
| 36 | - * @var array $_cookie |
|
| 37 | - */ |
|
| 38 | - private $_cookie; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * $_REQUEST parameters |
|
| 42 | - * |
|
| 43 | - * @var array $_params |
|
| 44 | - */ |
|
| 45 | - private $_params; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * whether current request is for the admin but NOT via AJAX |
|
| 49 | - * |
|
| 50 | - * @var boolean $admin |
|
| 51 | - */ |
|
| 52 | - public $admin = false; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * whether current request is via AJAX |
|
| 56 | - * |
|
| 57 | - * @var boolean $ajax |
|
| 58 | - */ |
|
| 59 | - public $ajax = false; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * whether current request is via AJAX from the frontend of the site |
|
| 63 | - * |
|
| 64 | - * @var boolean $front_ajax |
|
| 65 | - */ |
|
| 66 | - public $front_ajax = false; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * IP address for request |
|
| 70 | - * |
|
| 71 | - * @var string $_ip_address |
|
| 72 | - */ |
|
| 73 | - private $_ip_address; |
|
| 74 | - |
|
| 75 | - |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * class constructor |
|
| 79 | - * |
|
| 80 | - * @access public |
|
| 81 | - * @param array $get |
|
| 82 | - * @param array $post |
|
| 83 | - * @param array $cookie |
|
| 84 | - */ |
|
| 85 | - public function __construct(array $get, array $post, array $cookie) |
|
| 86 | - { |
|
| 87 | - // grab request vars |
|
| 88 | - $this->_get = $get; |
|
| 89 | - $this->_post = $post; |
|
| 90 | - $this->_cookie = $cookie; |
|
| 91 | - $this->_params = array_merge($this->_get, $this->_post); |
|
| 92 | - // AJAX ??? |
|
| 93 | - $this->ajax = defined('DOING_AJAX') && DOING_AJAX; |
|
| 94 | - $this->front_ajax = $this->ajax |
|
| 95 | - && $this->is_set('ee_front_ajax') |
|
| 96 | - && filter_var($this->get('ee_front_ajax'), FILTER_VALIDATE_BOOLEAN); |
|
| 97 | - $this->admin = is_admin() && ! $this->ajax; |
|
| 98 | - // grab user IP |
|
| 99 | - $this->_ip_address = $this->_visitor_ip(); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @return array |
|
| 106 | - */ |
|
| 107 | - public function get_params() |
|
| 108 | - { |
|
| 109 | - return $this->_get; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Important gotcha: these values usually do NOT have slashes added onto them because slashes are only added onto |
|
| 116 | - * $_POST by WordPress after plugins_loaded, and this object is created before then |
|
| 117 | - * @return array |
|
| 118 | - */ |
|
| 119 | - public function post_params() |
|
| 120 | - { |
|
| 121 | - return $this->_post; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @return array |
|
| 128 | - */ |
|
| 129 | - public function cookie_params() |
|
| 130 | - { |
|
| 131 | - return $this->_cookie; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * returns contents of $_REQUEST |
|
| 138 | - * |
|
| 139 | - * @return array |
|
| 140 | - */ |
|
| 141 | - public function params() |
|
| 142 | - { |
|
| 143 | - return $this->_params; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * @param $key |
|
| 150 | - * @param $value |
|
| 151 | - * @param bool $override_ee |
|
| 152 | - * @return void |
|
| 153 | - */ |
|
| 154 | - public function set($key, $value, $override_ee = false) |
|
| 155 | - { |
|
| 156 | - // don't allow "ee" to be overwritten unless explicitly instructed to do so |
|
| 157 | - if ( |
|
| 158 | - $key !== 'ee' |
|
| 159 | - || ($key === 'ee' && empty($this->_params['ee'])) |
|
| 160 | - || ($key === 'ee' && ! empty($this->_params['ee']) && $override_ee) |
|
| 161 | - ) { |
|
| 162 | - $this->_params[$key] = $value; |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * returns the value for a request param if the given key exists |
|
| 170 | - * |
|
| 171 | - * @param $key |
|
| 172 | - * @param null $default |
|
| 173 | - * @return mixed |
|
| 174 | - */ |
|
| 175 | - public function get($key, $default = null) |
|
| 176 | - { |
|
| 177 | - return $this->request_parameter_drill_down($key, $default, 'get'); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * check if param exists |
|
| 184 | - * |
|
| 185 | - * @param $key |
|
| 186 | - * @return bool |
|
| 187 | - */ |
|
| 188 | - public function is_set($key) |
|
| 189 | - { |
|
| 190 | - return $this->request_parameter_drill_down($key); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * the supplied key can be a simple string to represent a "top-level" request parameter |
|
| 197 | - * or represent a key for a request parameter that is nested deeper within the request parameter array, |
|
| 198 | - * by using square brackets to surround keys for deeper array elements. |
|
| 199 | - * For example : |
|
| 200 | - * if the supplied $key was: "first[second][third]" |
|
| 201 | - * then this will attempt to drill down into the request parameter array to find a value. |
|
| 202 | - * Given the following request parameters: |
|
| 203 | - * array( |
|
| 204 | - * 'first' => array( |
|
| 205 | - * 'second' => array( |
|
| 206 | - * 'third' => 'has a value' |
|
| 207 | - * ) |
|
| 208 | - * ) |
|
| 209 | - * ) |
|
| 210 | - * would return true |
|
| 211 | - * |
|
| 212 | - * @param string $is_set_or_get |
|
| 213 | - * @param $key |
|
| 214 | - * @param null $default |
|
| 215 | - * @param array $request_params |
|
| 216 | - * @return bool|mixed|null |
|
| 217 | - */ |
|
| 218 | - private function request_parameter_drill_down( |
|
| 219 | - $key, |
|
| 220 | - $default = null, |
|
| 221 | - $is_set_or_get = 'is_set', |
|
| 222 | - array $request_params = array() |
|
| 223 | - ) { |
|
| 224 | - $request_params = ! empty($request_params) |
|
| 225 | - ? $request_params |
|
| 226 | - : $this->_params; |
|
| 227 | - // does incoming key represent an array like 'first[second][third]' ? |
|
| 228 | - if (strpos($key, '[') !== false) { |
|
| 229 | - // turn it into an actual array |
|
| 230 | - $key = str_replace(']', '', $key); |
|
| 231 | - $keys = explode('[', $key); |
|
| 232 | - $key = array_shift($keys); |
|
| 233 | - // check if top level key exists |
|
| 234 | - if (isset($request_params[$key])) { |
|
| 235 | - // build a new key to pass along like: 'second[third]' |
|
| 236 | - // or just 'second' depending on depth of keys |
|
| 237 | - $key_string = array_shift($keys); |
|
| 238 | - if (! empty($keys)) { |
|
| 239 | - $key_string .= '[' . implode('][', $keys) . ']'; |
|
| 240 | - } |
|
| 241 | - return $this->request_parameter_drill_down( |
|
| 242 | - $key_string, |
|
| 243 | - $default, |
|
| 244 | - $is_set_or_get, |
|
| 245 | - $request_params[$key] |
|
| 246 | - ); |
|
| 247 | - } |
|
| 248 | - } |
|
| 249 | - if ($is_set_or_get === 'is_set') { |
|
| 250 | - return isset($request_params[$key]); |
|
| 251 | - } |
|
| 252 | - return isset($request_params[$key]) |
|
| 253 | - ? $request_params[$key] |
|
| 254 | - : $default; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * remove param |
|
| 261 | - * |
|
| 262 | - * @param $key |
|
| 263 | - * @param bool $unset_from_global_too |
|
| 264 | - */ |
|
| 265 | - public function un_set($key, $unset_from_global_too = false) |
|
| 266 | - { |
|
| 267 | - unset($this->_params[$key]); |
|
| 268 | - if ($unset_from_global_too) { |
|
| 269 | - unset($_REQUEST[$key]); |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * @return string |
|
| 277 | - */ |
|
| 278 | - public function ip_address() |
|
| 279 | - { |
|
| 280 | - return $this->_ip_address; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * @return bool |
|
| 286 | - */ |
|
| 287 | - public function isAdmin() |
|
| 288 | - { |
|
| 289 | - return $this->admin; |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * @return mixed |
|
| 295 | - */ |
|
| 296 | - public function isAjax() |
|
| 297 | - { |
|
| 298 | - return $this->ajax; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * @return mixed |
|
| 304 | - */ |
|
| 305 | - public function isFrontAjax() |
|
| 306 | - { |
|
| 307 | - return $this->front_ajax; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * _visitor_ip |
|
| 314 | - * attempt to get IP address of current visitor from server |
|
| 315 | - * plz see: http://stackoverflow.com/a/2031935/1475279 |
|
| 316 | - * |
|
| 317 | - * @access public |
|
| 318 | - * @return string |
|
| 319 | - */ |
|
| 320 | - private function _visitor_ip() |
|
| 321 | - { |
|
| 322 | - $visitor_ip = '0.0.0.0'; |
|
| 323 | - $server_keys = array( |
|
| 324 | - 'HTTP_CLIENT_IP', |
|
| 325 | - 'HTTP_X_FORWARDED_FOR', |
|
| 326 | - 'HTTP_X_FORWARDED', |
|
| 327 | - 'HTTP_X_CLUSTER_CLIENT_IP', |
|
| 328 | - 'HTTP_FORWARDED_FOR', |
|
| 329 | - 'HTTP_FORWARDED', |
|
| 330 | - 'REMOTE_ADDR', |
|
| 331 | - ); |
|
| 332 | - foreach ($server_keys as $key) { |
|
| 333 | - if (isset($_SERVER[$key])) { |
|
| 334 | - foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip) { |
|
| 335 | - if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) { |
|
| 336 | - $visitor_ip = $ip; |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - return $visitor_ip; |
|
| 342 | - } |
|
| 19 | + /** |
|
| 20 | + * $_GET parameters |
|
| 21 | + * |
|
| 22 | + * @var array $_get |
|
| 23 | + */ |
|
| 24 | + private $_get; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * $_POST parameters |
|
| 28 | + * |
|
| 29 | + * @var array $_post |
|
| 30 | + */ |
|
| 31 | + private $_post; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * $_COOKIE parameters |
|
| 35 | + * |
|
| 36 | + * @var array $_cookie |
|
| 37 | + */ |
|
| 38 | + private $_cookie; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * $_REQUEST parameters |
|
| 42 | + * |
|
| 43 | + * @var array $_params |
|
| 44 | + */ |
|
| 45 | + private $_params; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * whether current request is for the admin but NOT via AJAX |
|
| 49 | + * |
|
| 50 | + * @var boolean $admin |
|
| 51 | + */ |
|
| 52 | + public $admin = false; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * whether current request is via AJAX |
|
| 56 | + * |
|
| 57 | + * @var boolean $ajax |
|
| 58 | + */ |
|
| 59 | + public $ajax = false; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * whether current request is via AJAX from the frontend of the site |
|
| 63 | + * |
|
| 64 | + * @var boolean $front_ajax |
|
| 65 | + */ |
|
| 66 | + public $front_ajax = false; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * IP address for request |
|
| 70 | + * |
|
| 71 | + * @var string $_ip_address |
|
| 72 | + */ |
|
| 73 | + private $_ip_address; |
|
| 74 | + |
|
| 75 | + |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * class constructor |
|
| 79 | + * |
|
| 80 | + * @access public |
|
| 81 | + * @param array $get |
|
| 82 | + * @param array $post |
|
| 83 | + * @param array $cookie |
|
| 84 | + */ |
|
| 85 | + public function __construct(array $get, array $post, array $cookie) |
|
| 86 | + { |
|
| 87 | + // grab request vars |
|
| 88 | + $this->_get = $get; |
|
| 89 | + $this->_post = $post; |
|
| 90 | + $this->_cookie = $cookie; |
|
| 91 | + $this->_params = array_merge($this->_get, $this->_post); |
|
| 92 | + // AJAX ??? |
|
| 93 | + $this->ajax = defined('DOING_AJAX') && DOING_AJAX; |
|
| 94 | + $this->front_ajax = $this->ajax |
|
| 95 | + && $this->is_set('ee_front_ajax') |
|
| 96 | + && filter_var($this->get('ee_front_ajax'), FILTER_VALIDATE_BOOLEAN); |
|
| 97 | + $this->admin = is_admin() && ! $this->ajax; |
|
| 98 | + // grab user IP |
|
| 99 | + $this->_ip_address = $this->_visitor_ip(); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @return array |
|
| 106 | + */ |
|
| 107 | + public function get_params() |
|
| 108 | + { |
|
| 109 | + return $this->_get; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Important gotcha: these values usually do NOT have slashes added onto them because slashes are only added onto |
|
| 116 | + * $_POST by WordPress after plugins_loaded, and this object is created before then |
|
| 117 | + * @return array |
|
| 118 | + */ |
|
| 119 | + public function post_params() |
|
| 120 | + { |
|
| 121 | + return $this->_post; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @return array |
|
| 128 | + */ |
|
| 129 | + public function cookie_params() |
|
| 130 | + { |
|
| 131 | + return $this->_cookie; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * returns contents of $_REQUEST |
|
| 138 | + * |
|
| 139 | + * @return array |
|
| 140 | + */ |
|
| 141 | + public function params() |
|
| 142 | + { |
|
| 143 | + return $this->_params; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * @param $key |
|
| 150 | + * @param $value |
|
| 151 | + * @param bool $override_ee |
|
| 152 | + * @return void |
|
| 153 | + */ |
|
| 154 | + public function set($key, $value, $override_ee = false) |
|
| 155 | + { |
|
| 156 | + // don't allow "ee" to be overwritten unless explicitly instructed to do so |
|
| 157 | + if ( |
|
| 158 | + $key !== 'ee' |
|
| 159 | + || ($key === 'ee' && empty($this->_params['ee'])) |
|
| 160 | + || ($key === 'ee' && ! empty($this->_params['ee']) && $override_ee) |
|
| 161 | + ) { |
|
| 162 | + $this->_params[$key] = $value; |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * returns the value for a request param if the given key exists |
|
| 170 | + * |
|
| 171 | + * @param $key |
|
| 172 | + * @param null $default |
|
| 173 | + * @return mixed |
|
| 174 | + */ |
|
| 175 | + public function get($key, $default = null) |
|
| 176 | + { |
|
| 177 | + return $this->request_parameter_drill_down($key, $default, 'get'); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * check if param exists |
|
| 184 | + * |
|
| 185 | + * @param $key |
|
| 186 | + * @return bool |
|
| 187 | + */ |
|
| 188 | + public function is_set($key) |
|
| 189 | + { |
|
| 190 | + return $this->request_parameter_drill_down($key); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * the supplied key can be a simple string to represent a "top-level" request parameter |
|
| 197 | + * or represent a key for a request parameter that is nested deeper within the request parameter array, |
|
| 198 | + * by using square brackets to surround keys for deeper array elements. |
|
| 199 | + * For example : |
|
| 200 | + * if the supplied $key was: "first[second][third]" |
|
| 201 | + * then this will attempt to drill down into the request parameter array to find a value. |
|
| 202 | + * Given the following request parameters: |
|
| 203 | + * array( |
|
| 204 | + * 'first' => array( |
|
| 205 | + * 'second' => array( |
|
| 206 | + * 'third' => 'has a value' |
|
| 207 | + * ) |
|
| 208 | + * ) |
|
| 209 | + * ) |
|
| 210 | + * would return true |
|
| 211 | + * |
|
| 212 | + * @param string $is_set_or_get |
|
| 213 | + * @param $key |
|
| 214 | + * @param null $default |
|
| 215 | + * @param array $request_params |
|
| 216 | + * @return bool|mixed|null |
|
| 217 | + */ |
|
| 218 | + private function request_parameter_drill_down( |
|
| 219 | + $key, |
|
| 220 | + $default = null, |
|
| 221 | + $is_set_or_get = 'is_set', |
|
| 222 | + array $request_params = array() |
|
| 223 | + ) { |
|
| 224 | + $request_params = ! empty($request_params) |
|
| 225 | + ? $request_params |
|
| 226 | + : $this->_params; |
|
| 227 | + // does incoming key represent an array like 'first[second][third]' ? |
|
| 228 | + if (strpos($key, '[') !== false) { |
|
| 229 | + // turn it into an actual array |
|
| 230 | + $key = str_replace(']', '', $key); |
|
| 231 | + $keys = explode('[', $key); |
|
| 232 | + $key = array_shift($keys); |
|
| 233 | + // check if top level key exists |
|
| 234 | + if (isset($request_params[$key])) { |
|
| 235 | + // build a new key to pass along like: 'second[third]' |
|
| 236 | + // or just 'second' depending on depth of keys |
|
| 237 | + $key_string = array_shift($keys); |
|
| 238 | + if (! empty($keys)) { |
|
| 239 | + $key_string .= '[' . implode('][', $keys) . ']'; |
|
| 240 | + } |
|
| 241 | + return $this->request_parameter_drill_down( |
|
| 242 | + $key_string, |
|
| 243 | + $default, |
|
| 244 | + $is_set_or_get, |
|
| 245 | + $request_params[$key] |
|
| 246 | + ); |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | + if ($is_set_or_get === 'is_set') { |
|
| 250 | + return isset($request_params[$key]); |
|
| 251 | + } |
|
| 252 | + return isset($request_params[$key]) |
|
| 253 | + ? $request_params[$key] |
|
| 254 | + : $default; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * remove param |
|
| 261 | + * |
|
| 262 | + * @param $key |
|
| 263 | + * @param bool $unset_from_global_too |
|
| 264 | + */ |
|
| 265 | + public function un_set($key, $unset_from_global_too = false) |
|
| 266 | + { |
|
| 267 | + unset($this->_params[$key]); |
|
| 268 | + if ($unset_from_global_too) { |
|
| 269 | + unset($_REQUEST[$key]); |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * @return string |
|
| 277 | + */ |
|
| 278 | + public function ip_address() |
|
| 279 | + { |
|
| 280 | + return $this->_ip_address; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * @return bool |
|
| 286 | + */ |
|
| 287 | + public function isAdmin() |
|
| 288 | + { |
|
| 289 | + return $this->admin; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * @return mixed |
|
| 295 | + */ |
|
| 296 | + public function isAjax() |
|
| 297 | + { |
|
| 298 | + return $this->ajax; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * @return mixed |
|
| 304 | + */ |
|
| 305 | + public function isFrontAjax() |
|
| 306 | + { |
|
| 307 | + return $this->front_ajax; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * _visitor_ip |
|
| 314 | + * attempt to get IP address of current visitor from server |
|
| 315 | + * plz see: http://stackoverflow.com/a/2031935/1475279 |
|
| 316 | + * |
|
| 317 | + * @access public |
|
| 318 | + * @return string |
|
| 319 | + */ |
|
| 320 | + private function _visitor_ip() |
|
| 321 | + { |
|
| 322 | + $visitor_ip = '0.0.0.0'; |
|
| 323 | + $server_keys = array( |
|
| 324 | + 'HTTP_CLIENT_IP', |
|
| 325 | + 'HTTP_X_FORWARDED_FOR', |
|
| 326 | + 'HTTP_X_FORWARDED', |
|
| 327 | + 'HTTP_X_CLUSTER_CLIENT_IP', |
|
| 328 | + 'HTTP_FORWARDED_FOR', |
|
| 329 | + 'HTTP_FORWARDED', |
|
| 330 | + 'REMOTE_ADDR', |
|
| 331 | + ); |
|
| 332 | + foreach ($server_keys as $key) { |
|
| 333 | + if (isset($_SERVER[$key])) { |
|
| 334 | + foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip) { |
|
| 335 | + if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) { |
|
| 336 | + $visitor_ip = $ip; |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + return $visitor_ip; |
|
| 342 | + } |
|
| 343 | 343 | |
| 344 | 344 | |
| 345 | 345 | |