PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/s2member/includes/classes/ssl-in.inc.php

https://gitlab.com/Gashler/dp
PHP | 169 lines | 111 code | 9 blank | 49 comment | 14 complexity | 8900c0039508539b5723c814ec8364ba MD5 | raw file
  1. <?php
  2. /**
  3. * SSL routines (inner processing routines).
  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\SSL
  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_ssl_in"))
  20. {
  21. /**
  22. * SSL routines (inner processing routines).
  23. *
  24. * @package s2Member\SSL
  25. * @since 3.5
  26. */
  27. class c_ws_plugin__s2member_ssl_in
  28. {
  29. /**
  30. * Forces SSL on specific Posts/Pages, or any page for that matter.
  31. *
  32. * Triggered by Custom Field: `s2member_force_ssl = yes|port#`
  33. *
  34. * Triggered by: `?s2-ssl` or `?s2-ssl=yes|port#`.
  35. *
  36. * @package s2Member\SSL
  37. * @since 3.5
  38. *
  39. * @attaches-to ``add_action("init");``
  40. * @also-attaches-to ``add_action("wp");``
  41. *
  42. * @return null Possibly exiting script execution after redirection to SSL variation.
  43. *
  44. * @todo Add `form` to the array ``$non_ssl_attr_only_tags``?
  45. * @todo Cleanup this routine and convert callback functions to static class methods?
  46. */
  47. public static function force_ssl($vars = array()) // Phase 2 of ``c_ws_plugin__s2member_ssl::check_force_ssl()``.
  48. {
  49. extract /* Extract all vars passed in from: ``c_ws_plugin__s2member_ssl::check_force_ssl()``. */($vars);
  50. $force_ssl = (!is_string($force_ssl)) ? /* Force string. */ (string)(int)$force_ssl : $force_ssl;
  51. $force_ssl = (is_numeric($force_ssl) && $force_ssl > 1) ? $force_ssl : /* Use `yes`. */ "yes";
  52. $ssl_host = /* Remove port here. */ preg_replace("/\:[0-9]+$/", "", $_SERVER["HTTP_HOST"]);
  53. $ssl_port = /* Port? */ (is_numeric($force_ssl) && $force_ssl > 1) ? $force_ssl : false;
  54. $ssl_host_port = /* Use port #? */ $ssl_host.(($ssl_port) ? ":".$ssl_port : "");
  55. if(!is_ssl() || !isset($_GET[$s2_ssl_gv]) /* SSL must be enabled. */)
  56. {
  57. $https = "https://".$ssl_host_port.$_SERVER["REQUEST_URI"];
  58. $https_with_s2_ssl_gv = add_query_arg($s2_ssl_gv, urlencode($force_ssl), $https);
  59. wp_redirect($https_with_s2_ssl_gv).exit();
  60. }
  61. else // Otherwise, we buffer all output, and switch all content over to `https`.
  62. // Assume here that other links on the site should NOT be converted to `https`.
  63. {
  64. add_filter("redirect_canonical", "__return_false");
  65. define("_ws_plugin__s2member_force_ssl_host", $ssl_host);
  66. define("_ws_plugin__s2member_force_ssl_port", $ssl_port);
  67. define("_ws_plugin__s2member_force_ssl_host_port", $ssl_host_port);
  68. // Filter these. Do NOT create a sitewide conversion to `https`.
  69. add_filter("home_url", "_ws_plugin__s2member_force_non_ssl_scheme", 10, 3);
  70. add_filter("network_home_url", "_ws_plugin__s2member_force_non_ssl_scheme", 10, 3);
  71. // Filter these. Do NOT create a sitewide conversion to `https`.
  72. add_filter("site_url", "_ws_plugin__s2member_force_non_ssl_scheme", 10, 3);
  73. add_filter("network_site_url", "_ws_plugin__s2member_force_non_ssl_scheme", 10, 3);
  74. /*
  75. These additional URLs are NOT Filtered by default; but can be if needed. Use these Filters. */
  76. if(apply_filters("_ws_plugin__s2member_force_non_ssl_scheme_plugins_url", false, get_defined_vars()))
  77. add_filter("plugins_url", "_ws_plugin__s2member_force_non_ssl_scheme", 10, 2);
  78. /*
  79. These additional URLs are NOT Filtered by default; but can be if needed. Use these Filters. */
  80. if(apply_filters("_ws_plugin__s2member_force_non_ssl_scheme_content_url", false, get_defined_vars()))
  81. add_filter("content_url", "_ws_plugin__s2member_force_non_ssl_scheme", 10, 2);
  82. /*
  83. Now we create various callback functions associated with SSL and non-SSL buffering.
  84. */
  85. if(!function_exists("_ws_plugin__s2member_force_ssl_buffer_callback"))
  86. {
  87. function _ws_plugin__s2member_force_ssl_buffer_callback($m = FALSE)
  88. {
  89. $s = /* Conversion to SSL mode via `https`. */ preg_replace("/http\:\/\//i", "https://", $m[0]);
  90. if(_ws_plugin__s2member_force_ssl_host && /* Convert port? */ _ws_plugin__s2member_force_ssl_port && _ws_plugin__s2member_force_ssl_host_port)
  91. $s = preg_replace("/(?:https?\:)?\/\/".preg_quote(_ws_plugin__s2member_force_ssl_host, "/")."(?:\:[0-9]+)?/i", "https://"._ws_plugin__s2member_force_ssl_host_port, $s);
  92. $s = (strtolower($m[1]) === "link" && preg_match /* These are fine to leave like they are. */("/['\"](?:alternate|profile|pingback|EditURI|wlwmanifest|prev|next)['\"]/i", $m[0])) ? $m[0] : $s;
  93. return /* Return string with conversions. */ $s;
  94. }
  95. }
  96. if(!function_exists("_ws_plugin__s2member_force_non_ssl_buffer_callback"))
  97. {
  98. function _ws_plugin__s2member_force_non_ssl_buffer_callback($m = FALSE)
  99. {
  100. $s = preg_replace("/(?:https?\:)?\/\/".preg_quote(_ws_plugin__s2member_force_ssl_host_port, "/")."/i", "http://"._ws_plugin__s2member_force_ssl_host, $m[0]);
  101. $s = preg_replace("/(?:https?\:)?\/\/".preg_quote(_ws_plugin__s2member_force_ssl_host, "/")."/i", "http://"._ws_plugin__s2member_force_ssl_host, $s);
  102. return /* Return string with conversions. */ $s;
  103. }
  104. }
  105. if(!function_exists("_ws_plugin__s2member_force_non_ssl_scheme"))
  106. {
  107. function _ws_plugin__s2member_force_non_ssl_scheme($url = FALSE, $path = FALSE, $scheme = FALSE)
  108. {
  109. if(!in_array /* If NOT explicitly passed through. */($scheme, array("http", "https"), true))
  110. {
  111. if(($scheme === "login_post" || $scheme === "rpc") && (force_ssl_login() || force_ssl_admin()))
  112. $scheme = "https";
  113. else if(($scheme === "login" || $scheme === "admin") && force_ssl_admin())
  114. $scheme = "https";
  115. else // Default to non-SSL: `http`.
  116. $scheme = "http";
  117. }
  118. return preg_replace("/^(?:https?\:)?\/\//i", $scheme."://", $url);
  119. }
  120. }
  121. if(!function_exists("_ws_plugin__s2member_force_ssl_buffer"))
  122. {
  123. function _ws_plugin__s2member_force_ssl_buffer($buffer = FALSE)
  124. {
  125. $o_pcre = /* Record existing PCRE backtrack limit. */ @ini_get("pcre.backtrack_limit");
  126. @ini_set /* Increase PCRE backtrack limit for this routine. */("pcre.backtrack_limit", 10000000);
  127. $ssl_entire_tags = array_unique(array_map("strtolower", apply_filters("_ws_plugin__s2member_force_ssl_buffer_entire_tags", array("script", "style", "iframe", "object", "embed", "video"), get_defined_vars())));
  128. $non_ssl_entire_tags = array_unique(array_map("strtolower", apply_filters("_ws_plugin__s2member_force_non_ssl_buffer_entire_tags", array(), get_defined_vars())));
  129. $ssl_attr_only_tags = array_unique( /* Diff here. No need to re-run entire tags. */array_diff(array_map("strtolower", apply_filters("_ws_plugin__s2member_force_ssl_buffer_attr_only_tags", array("link", "img", "input"), get_defined_vars())), $ssl_entire_tags));
  130. $non_ssl_attr_only_tags = array_unique( /* No need to re-run entire tags. */array_diff(array_map("strtolower", apply_filters("_ws_plugin__s2member_force_non_ssl_buffer_attr_only_tags", array("a"), get_defined_vars())), $non_ssl_entire_tags));
  131. $buffer = ($ssl_entire_tags) ? preg_replace_callback("/\<(".implode("|", c_ws_plugin__s2member_utils_strings::preg_quote_deep($ssl_entire_tags, "/")).")(?![a-z_0-9\-])[^\>]*?\>.*?\<\/\\1\>/is", "_ws_plugin__s2member_force_ssl_buffer_callback", $buffer) : $buffer;
  132. $buffer = ($ssl_attr_only_tags) ? preg_replace_callback("/\<(".implode("|", c_ws_plugin__s2member_utils_strings::preg_quote_deep($ssl_attr_only_tags, "/")).")(?![a-z_0-9\-])[^\>]+?\>/i", "_ws_plugin__s2member_force_ssl_buffer_callback", $buffer) : $buffer;
  133. $buffer = ($non_ssl_entire_tags) ? preg_replace_callback("/\<(".implode("|", c_ws_plugin__s2member_utils_strings::preg_quote_deep($non_ssl_entire_tags, "/")).")(?![a-z_0-9\-])[^\>]*?\>.*?\<\/\\1\>/is", "_ws_plugin__s2member_force_non_ssl_buffer_callback", $buffer) : $buffer;
  134. $buffer = ($non_ssl_attr_only_tags) ? preg_replace_callback("/\<(".implode("|", c_ws_plugin__s2member_utils_strings::preg_quote_deep($non_ssl_attr_only_tags, "/")).")(?![a-z_0-9\-])[^\>]+?\>/i", "_ws_plugin__s2member_force_non_ssl_buffer_callback", $buffer) : $buffer;
  135. @ini_set /* Restore original PCRE backtrack limit. This just keeps things tidy; probably NOT necessary. */("pcre.backtrack_limit", $o_pcre);
  136. return apply_filters("_ws_plugin__s2member_force_ssl_buffer", $buffer, get_defined_vars());
  137. }
  138. }
  139. ob_start("_ws_plugin__s2member_force_ssl_buffer");
  140. }
  141. return /* Return for uniformity. */;
  142. }
  143. }
  144. }
  145. ?>