/wp-content/plugins/s2member/includes/classes/utils-forms.inc.php

https://gitlab.com/Gashler/dp · PHP · 98 lines · 67 code · 1 blank · 30 comment · 5 complexity · 8e606e1ec96274dae9eb1d67ed219767 MD5 · raw file

  1. <?php
  2. /**
  3. * Form utilities.
  4. *
  5. * Copyright: © 2009-2011
  6. * {@link http://www.websharks-inc.com/ WebSharks, Inc.}
  7. * (coded in the USA)
  8. *
  9. * Released under the terms of the GNU General Public License.
  10. * You should have received a copy of the GNU General Public License,
  11. * along with this software. In the main directory, see: /licensing/
  12. * If not, see: {@link http://www.gnu.org/licenses/}.
  13. *
  14. * @package s2Member\Utilities
  15. * @since 3.5
  16. */
  17. if (realpath (__FILE__) === realpath ($_SERVER["SCRIPT_FILENAME"]))
  18. exit ("Do not access this file directly.");
  19. if (!class_exists ("c_ws_plugin__s2member_utils_forms"))
  20. {
  21. /**
  22. * Form utilities.
  23. *
  24. * @package s2Member\Utilities
  25. * @since 3.5
  26. */
  27. class c_ws_plugin__s2member_utils_forms
  28. {
  29. /**
  30. * Converts a form with hidden inputs into a URL w/ query string.
  31. *
  32. * @package s2Member\Utilities
  33. * @since 3.5
  34. *
  35. * @param str $form A form tag with hidden input fields.
  36. * @return str A URL with query string equivalents.
  37. */
  38. public static function form_whips_2_url ($form = FALSE)
  39. {
  40. if (preg_match ("/\<form(.+?)\>/is", $form, $form_attr_m)) // Is this a form?
  41. {
  42. if (preg_match ("/(\s)(action)( ?)(\=)( ?)(['\"])(.+?)(['\"])/is", $form_attr_m[1], $form_action_m))
  43. {
  44. if (($url = trim ($form_action_m[7]))) // Set URL value dynamically. Now we add values.
  45. {
  46. foreach ((array)c_ws_plugin__s2member_utils_forms::form_whips_2_array ($form) as $name => $value)
  47. {
  48. if (strlen ($name) && strlen ($value)) // Check $name -› $value lengths.
  49. if (strlen ($value = (preg_match ("/^http(s)?\:\/\//i", $value)) ? rawurlencode ($value) : urlencode ($value)))
  50. {
  51. $url = add_query_arg ($name, $value, $url);
  52. }
  53. }
  54. return $url;
  55. }
  56. }
  57. }
  58. return false;
  59. }
  60. /**
  61. * Converts a form with hidden inputs into an associative array.
  62. *
  63. * @package s2Member\Utilities
  64. * @since 3.5
  65. *
  66. * @param str $form A form tag with hidden input fields.
  67. * @return array An associative array of all hidden input fields.
  68. */
  69. public static function form_whips_2_array ($form = FALSE)
  70. {
  71. if (preg_match ("/\<form(.+?)\>/is", $form)) // Is this a form?
  72. {
  73. if (preg_match_all ("/(?<!\<\!--)\<input(.+?)\>/is", $form, $input_attr_ms, PREG_SET_ORDER))
  74. {
  75. foreach ($input_attr_ms as $input_attr_m) // Go through each hidden input variable.
  76. {
  77. if (preg_match ("/(\s)(type)( ?)(\=)( ?)(['\"])(hidden)(['\"])/is", $input_attr_m[1]))
  78. {
  79. if (preg_match ("/(\s)(name)( ?)(\=)( ?)(['\"])(.+?)(['\"])/is", $input_attr_m[1], $input_name_m))
  80. {
  81. if (preg_match ("/(\s)(value)( ?)(\=)( ?)(['\"])(.+?)(['\"])/is", $input_attr_m[1], $input_value_m))
  82. {
  83. $array[trim ($input_name_m[7])] = trim (wp_specialchars_decode ($input_value_m[7], ENT_QUOTES));
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. return (array)$array;
  91. }
  92. }
  93. }
  94. ?>