PageRenderTime 45ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Gashler/sg
PHP | 167 lines | 95 code | 21 blank | 51 comment | 28 complexity | 84b16d5f9bc925ced990c3aef5c37018 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(!defined('WPINC')) // MUST have WordPress.
  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. * @param array $vars From: ``c_ws_plugin__s2member_ssl::check_force_ssl()``.
  43. *
  44. * @return null Possibly exiting script execution after redirection to SSL variation.
  45. *
  46. * @todo Add `form` to the array ``$non_ssl_attr_only_tags``?
  47. * @todo Cleanup this routine and convert callback functions to static class methods?
  48. */
  49. public static function force_ssl($vars = array()) // Phase 2 of ``c_ws_plugin__s2member_ssl::check_force_ssl()``.
  50. {
  51. /**
  52. * @var string $s2_ssl_gv Extracted variable.
  53. * @var string|integer|mixed $force_ssl Extracted variable.
  54. */
  55. extract($vars); // From: ``c_ws_plugin__s2member_ssl::check_force_ssl()``.
  56. $force_ssl = !is_string($force_ssl) ? (string)(int)$force_ssl : $force_ssl;
  57. $force_ssl = is_numeric($force_ssl) && $force_ssl > 1 ? $force_ssl : 'yes';
  58. $ssl_host = preg_replace('/\:[0-9]+$/', '', $_SERVER['HTTP_HOST']);
  59. $ssl_port = (is_numeric($force_ssl) && $force_ssl > 1) ? $force_ssl : FALSE;
  60. $ssl_host_port = $ssl_host.(($ssl_port) ? ':'.$ssl_port : '');
  61. if(!is_ssl() || !isset($_GET[$s2_ssl_gv]))
  62. {
  63. $https = 'https://'.$ssl_host_port.$_SERVER['REQUEST_URI'];
  64. $https_with_s2_ssl_gv = add_query_arg($s2_ssl_gv, urlencode($force_ssl), $https);
  65. wp_redirect($https_with_s2_ssl_gv).exit();
  66. }
  67. else // Otherwise, we buffer all output, and switch all content over to `https`.
  68. // Assume here that other links on the site should NOT be converted to `https`.
  69. {
  70. add_filter('redirect_canonical', '__return_false');
  71. define('_ws_plugin__s2member_force_ssl_host', $ssl_host);
  72. define('_ws_plugin__s2member_force_ssl_port', $ssl_port);
  73. define('_ws_plugin__s2member_force_ssl_host_port', $ssl_host_port);
  74. // Filter these. Do NOT create a sitewide conversion to `https`.
  75. add_filter('home_url', '_ws_plugin__s2member_force_non_ssl_scheme', 10, 3);
  76. add_filter('network_home_url', '_ws_plugin__s2member_force_non_ssl_scheme', 10, 3);
  77. // Filter these. Do NOT create a sitewide conversion to `https`.
  78. add_filter('site_url', '_ws_plugin__s2member_force_non_ssl_scheme', 10, 3);
  79. add_filter('network_site_url', '_ws_plugin__s2member_force_non_ssl_scheme', 10, 3);
  80. // These additional URLs are NOT Filtered by default; but can be if needed. Use these Filters.
  81. if(apply_filters('_ws_plugin__s2member_force_non_ssl_scheme_plugins_url', FALSE, get_defined_vars()))
  82. add_filter('plugins_url', '_ws_plugin__s2member_force_non_ssl_scheme', 10, 2);
  83. // These additional URLs are NOT Filtered by default; but can be if needed. Use these Filters.
  84. if(apply_filters('_ws_plugin__s2member_force_non_ssl_scheme_content_url', FALSE, get_defined_vars()))
  85. add_filter('content_url', '_ws_plugin__s2member_force_non_ssl_scheme', 10, 2);
  86. // Now we create various callback functions associated with SSL and non-SSL buffering.
  87. if(!function_exists('_ws_plugin__s2member_force_ssl_buffer_callback'))
  88. {
  89. function _ws_plugin__s2member_force_ssl_buffer_callback($m = FALSE)
  90. {
  91. $s = preg_replace('/http\:\/\//i', 'https://', $m[0]);
  92. if(_ws_plugin__s2member_force_ssl_host && _ws_plugin__s2member_force_ssl_port && _ws_plugin__s2member_force_ssl_host_port)
  93. $s = preg_replace('/(?:https?\:)?\/\/'.preg_quote(_ws_plugin__s2member_force_ssl_host, '/').'(?:\:[0-9]+)?/i', 'https://'._ws_plugin__s2member_force_ssl_host_port, $s);
  94. $s = (strtolower($m[1]) === 'link' && preg_match('/(["\'])(?:alternate|profile|pingback|EditURI|wlwmanifest|prev|next)\\1/i', $m[0])) ? $m[0] : $s;
  95. return $s; // Return string with conversions.
  96. }
  97. }
  98. if(!function_exists('_ws_plugin__s2member_force_non_ssl_buffer_callback'))
  99. {
  100. function _ws_plugin__s2member_force_non_ssl_buffer_callback($m = FALSE)
  101. {
  102. $s = preg_replace('/(?:https?\:)?\/\/'.preg_quote(_ws_plugin__s2member_force_ssl_host_port, '/').'/i', 'http://'._ws_plugin__s2member_force_ssl_host, $m[0]);
  103. $s = preg_replace('/(?:https?\:)?\/\/'.preg_quote(_ws_plugin__s2member_force_ssl_host, '/').'/i', 'http://'._ws_plugin__s2member_force_ssl_host, $s);
  104. return $s; // Return string with conversions.
  105. }
  106. }
  107. if(!function_exists('_ws_plugin__s2member_force_non_ssl_scheme'))
  108. {
  109. function _ws_plugin__s2member_force_non_ssl_scheme($url = FALSE, $path = FALSE, $scheme = FALSE)
  110. {
  111. if($scheme === 'relative')
  112. return $url; // Nothing to do in this case.
  113. if(!in_array($scheme, array('http', 'https'), TRUE)) // If NOT explicitly passed through.
  114. {
  115. if(($scheme === 'login_post' || $scheme === 'rpc') && (force_ssl_login() || force_ssl_admin()))
  116. $scheme = 'https';
  117. else if(($scheme === 'login' || $scheme === 'admin') && force_ssl_admin())
  118. $scheme = 'https';
  119. else $scheme = 'http'; // Default to non-SSL: `http`.
  120. }
  121. return preg_replace('/^(?:https?\:)?\/\//i', $scheme.'://', $url);
  122. }
  123. }
  124. if(!function_exists('_ws_plugin__s2member_force_ssl_buffer'))
  125. {
  126. function _ws_plugin__s2member_force_ssl_buffer($buffer = FALSE)
  127. {
  128. $o_pcre = @ini_get('pcre.backtrack_limit'); // Record existing backtrack limit.
  129. @ini_set('pcre.backtrack_limit', 10000000); // Increase PCRE backtrack limit for this routine.
  130. $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())));
  131. $non_ssl_entire_tags = array_unique(array_map('strtolower', apply_filters('_ws_plugin__s2member_force_non_ssl_buffer_entire_tags', array(), get_defined_vars())));
  132. $ssl_attr_only_tags = array_unique(array_diff(array_map('strtolower', apply_filters('_ws_plugin__s2member_force_ssl_buffer_attr_only_tags', array('link', 'img', 'form', 'input'), get_defined_vars())), $ssl_entire_tags));
  133. $non_ssl_attr_only_tags = array_unique(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));
  134. $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;
  135. $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;
  136. $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;
  137. $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;
  138. @ini_set('pcre.backtrack_limit', $o_pcre); // Restore original PCRE backtrack limit. This just keeps things tidy; probably NOT necessary.
  139. return apply_filters('_ws_plugin__s2member_force_ssl_buffer', $buffer, get_defined_vars());
  140. }
  141. }
  142. ob_start('_ws_plugin__s2member_force_ssl_buffer');
  143. }
  144. }
  145. }
  146. }