| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* SSL routines (inner processing routines). |
| 5 |
* |
| 6 |
* Copyright: © 2009-2011 |
| 7 |
* {@link http://websharks-inc.com/ WebSharks, Inc.} |
| 8 |
* (coded in the USA) |
| 9 |
* |
| 10 |
* Released under the terms of the GNU General Public License. |
| 11 |
* You should have received a copy of the GNU General Public License, |
| 12 |
* along with this software. In the main directory, see: /licensing/ |
| 13 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 14 |
* |
| 15 |
* @package s2Member\SSL |
| 16 |
* @since 3.5 |
| 17 |
*/ |
| 18 |
if(!defined('WPINC')) // MUST have WordPress. |
| 19 |
exit('Do not access this file directly.'); |
| 20 |
|
| 21 |
if(!class_exists('c_ws_plugin__s2member_ssl_in')) |
| 22 |
{ |
| 23 |
/** |
| 24 |
* SSL routines (inner processing routines). |
| 25 |
* |
| 26 |
* @package s2Member\SSL |
| 27 |
* @since 3.5 |
| 28 |
*/ |
| 29 |
class c_ws_plugin__s2member_ssl_in |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Forces SSL on specific Posts/Pages, or any page for that matter. |
| 33 |
* |
| 34 |
* Triggered by Custom Field: `s2member_force_ssl = yes|port#` |
| 35 |
* |
| 36 |
* Triggered by: `?s2-ssl` or `?s2-ssl=yes|port#`. |
| 37 |
* |
| 38 |
* @package s2Member\SSL |
| 39 |
* @since 3.5 |
| 40 |
* |
| 41 |
* @attaches-to ``add_action('init');`` |
| 42 |
* @also-attaches-to ``add_action('wp');`` |
| 43 |
* |
| 44 |
* @param array $vars From: ``c_ws_plugin__s2member_ssl::check_force_ssl()``. |
| 45 |
* |
| 46 |
* @return null Possibly exiting script execution after redirection to SSL variation. |
| 47 |
* |
| 48 |
* @todo Cleanup this routine and convert callback functions to static class methods? |
| 49 |
*/ |
| 50 |
public static function force_ssl($vars = array()) // Phase 2 of ``c_ws_plugin__s2member_ssl::check_force_ssl()``. |
| 51 |
{ |
| 52 |
/** |
| 53 |
* @var string $s2_ssl_gv Extracted variable. |
| 54 |
* @var string|integer|mixed $force_ssl Extracted variable. |
| 55 |
*/ |
| 56 |
extract($vars); // From: ``c_ws_plugin__s2member_ssl::check_force_ssl()``. |
| 57 |
|
| 58 |
$force_ssl = !is_string($force_ssl) ? (string)(int)$force_ssl : $force_ssl; |
| 59 |
$force_ssl = is_numeric($force_ssl) && $force_ssl > 1 ? $force_ssl : 'yes'; |
| 60 |
|
| 61 |
$ssl_host = preg_replace('/\:[0-9]+$/', '', $_SERVER['HTTP_HOST']); |
| 62 |
$ssl_port = (is_numeric($force_ssl) && $force_ssl > 1) ? $force_ssl : FALSE; |
| 63 |
$ssl_host_port = $ssl_host.(($ssl_port) ? ':'.$ssl_port : ''); |
| 64 |
|
| 65 |
if(!is_ssl() || !isset($_GET[$s2_ssl_gv])) |
| 66 |
{ |
| 67 |
$https = 'https://'.$ssl_host_port.$_SERVER['REQUEST_URI']; |
| 68 |
$https_with_s2_ssl_gv = add_query_arg($s2_ssl_gv, urlencode($force_ssl), $https); |
| 69 |
wp_redirect($https_with_s2_ssl_gv).exit(); |
| 70 |
} |
| 71 |
else // Otherwise, we buffer all output, and switch all content over to `https`. |
| 72 |
// Assume here that other links on the site should NOT be converted to `https`. |
| 73 |
{ |
| 74 |
add_filter('redirect_canonical', '__return_false'); |
| 75 |
|
| 76 |
define('_ws_plugin__s2member_force_ssl_host', $ssl_host); |
| 77 |
define('_ws_plugin__s2member_force_ssl_port', $ssl_port); |
| 78 |
define('_ws_plugin__s2member_force_ssl_host_port', $ssl_host_port); |
| 79 |
|
| 80 |
// Filter these. Do NOT create a sitewide conversion to `https`. |
| 81 |
add_filter('home_url', '_ws_plugin__s2member_maybe_force_non_ssl_scheme', 10, 3); |
| 82 |
add_filter('network_home_url', '_ws_plugin__s2member_maybe_force_non_ssl_scheme', 10, 3); |
| 83 |
|
| 84 |
// Filter these. Do NOT create a sitewide conversion to `https`. |
| 85 |
add_filter('site_url', '_ws_plugin__s2member_maybe_force_non_ssl_scheme', 10, 3); |
| 86 |
add_filter('network_site_url', '_ws_plugin__s2member_maybe_force_non_ssl_scheme', 10, 3); |
| 87 |
|
| 88 |
// Filter these. Do NOT create a sitewide conversion to `https`. |
| 89 |
// Note: these are necessary because these underlying functions create URLs in bits and pieces. |
| 90 |
// Thus, in order to properly detect static file extensions we need to look at these values also. |
| 91 |
add_filter('plugins_url', '_ws_plugin__s2member_maybe_force_non_ssl_scheme', 10, 2); |
| 92 |
add_filter('content_url', '_ws_plugin__s2member_maybe_force_non_ssl_scheme', 10, 2); |
| 93 |
add_filter('includes_url', '_ws_plugin__s2member_maybe_force_non_ssl_scheme', 10, 2); |
| 94 |
|
| 95 |
// Now we create various callback functions associated with SSL and non-SSL buffering. |
| 96 |
if(!function_exists('_ws_plugin__s2member_force_ssl_buffer_callback')) |
| 97 |
{ |
| 98 |
function _ws_plugin__s2member_force_ssl_buffer_callback($m = array()) |
| 99 |
{ |
| 100 |
$s = preg_replace('/http\:\/\//i', 'https://', $m[0]); |
| 101 |
|
| 102 |
if(_ws_plugin__s2member_force_ssl_host && _ws_plugin__s2member_force_ssl_port && _ws_plugin__s2member_force_ssl_host_port) |
| 103 |
$s = preg_replace('/(?:https?\:)?\/\/'.preg_quote(_ws_plugin__s2member_force_ssl_host, '/').'(?:\:[0-9]+)?/i', 'https://'._ws_plugin__s2member_force_ssl_host_port, $s); |
| 104 |
|
| 105 |
$s = strtolower($m[1]) === 'link' && preg_match('/(["\'])(?:alternate|profile|pingback|EditURI|wlwmanifest|prev|next)\\1/i', $m[0]) ? $m[0] : $s; |
| 106 |
|
| 107 |
return $s; // Return string with conversions. |
| 108 |
} |
| 109 |
} |
| 110 |
if(!function_exists('_ws_plugin__s2member_force_non_ssl_buffer_callback')) |
| 111 |
{ |
| 112 |
function _ws_plugin__s2member_force_non_ssl_buffer_callback($m = array()) |
| 113 |
{ |
| 114 |
$s = $m[0]; // Initialize the `$s` variable. |
| 115 |
|
| 116 |
if(stripos($s, 's2member_file_download') !== false || stripos($s, 's2member-files') !== false) |
| 117 |
return $s; // See: <https://github.com/websharks/s2member/issues/702> |
| 118 |
|
| 119 |
$s = preg_replace('/(?:https?\:)?\/\/'.preg_quote(_ws_plugin__s2member_force_ssl_host_port, '/').'/i', 'http://'._ws_plugin__s2member_force_ssl_host, $s); |
| 120 |
$s = preg_replace('/(?:https?\:)?\/\/'.preg_quote(_ws_plugin__s2member_force_ssl_host, '/').'/i', 'http://'._ws_plugin__s2member_force_ssl_host, $s); |
| 121 |
|
| 122 |
return $s; // Return string with conversions. |
| 123 |
} |
| 124 |
} |
| 125 |
if(!function_exists('_ws_plugin__s2member_maybe_force_non_ssl_scheme')) |
| 126 |
{ |
| 127 |
function _ws_plugin__s2member_maybe_force_non_ssl_scheme($url = '', $path = '', $scheme = null) |
| 128 |
{ |
| 129 |
static $static_file_extensions; // Cache of static file extensions. |
| 130 |
if(!isset($static_file_extensions)) // Cached this yet? |
| 131 |
{ |
| 132 |
$wp_media_library_extensions = array_keys(wp_get_mime_types()); |
| 133 |
$wp_media_library_extensions = explode('|', strtolower(implode('|', $wp_media_library_extensions))); |
| 134 |
$static_file_extensions = array_unique(array_merge($wp_media_library_extensions, array('eot', 'ttf', 'otf', 'woff'))); |
| 135 |
} |
| 136 |
if($scheme === 'relative') // e.g. `/root/relative/path.ext` |
| 137 |
return $url; // Nothing to do in this case. |
| 138 |
|
| 139 |
if(!in_array($scheme, array('http', 'https'), TRUE)) // If NOT explicit. |
| 140 |
{ |
| 141 |
if(($scheme === 'login_post' || $scheme === 'rpc') && (force_ssl_login() || force_ssl_admin())) |
| 142 |
$scheme = 'https'; // Use an SSL scheme in this case. |
| 143 |
|
| 144 |
else if(($scheme === 'login' || $scheme === 'admin') && force_ssl_admin()) |
| 145 |
$scheme = 'https'; // Use an SSL scheme in this case. |
| 146 |
|
| 147 |
else if($url && ($url_path = @parse_url($url, PHP_URL_PATH)) && $url_path !== '/' |
| 148 |
&& ($url_ext = strtolower(ltrim((string) strrchr(basename($url_path), '.'), '.'))) |
| 149 |
&& in_array($url_ext, $static_file_extensions, true) // Static resource? |
| 150 |
) $scheme = 'https'; // Use an SSL scheme in this case. |
| 151 |
|
| 152 |
else $scheme = 'http'; // Default to non-SSL: `http`. |
| 153 |
} |
| 154 |
return preg_replace('/^(?:https?\:)?\/\//i', $scheme.'://', $url); |
| 155 |
} |
| 156 |
} |
| 157 |
if(!function_exists('_ws_plugin__s2member_force_ssl_buffer')) |
| 158 |
{ |
| 159 |
function _ws_plugin__s2member_force_ssl_buffer($buffer = '') |
| 160 |
{ |
| 161 |
$o_pcre = @ini_get('pcre.backtrack_limit'); // Record existing backtrack limit. |
| 162 |
@ini_set('pcre.backtrack_limit', 10000000); // Increase PCRE backtrack limit for this routine. |
| 163 |
|
| 164 |
$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()))); |
| 165 |
$non_ssl_entire_tags = array_unique(array_map('strtolower', apply_filters('_ws_plugin__s2member_force_non_ssl_buffer_entire_tags', array(), get_defined_vars()))); |
| 166 |
|
| 167 |
$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)); |
| 168 |
$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)); |
| 169 |
|
| 170 |
$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; |
| 171 |
$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; |
| 172 |
|
| 173 |
$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; |
| 174 |
$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; |
| 175 |
|
| 176 |
@ini_set('pcre.backtrack_limit', $o_pcre); // Restore original PCRE backtrack limit. This just keeps things tidy; probably NOT necessary. |
| 177 |
|
| 178 |
return apply_filters('_ws_plugin__s2member_force_ssl_buffer', $buffer, get_defined_vars()); |
| 179 |
} |
| 180 |
} |
| 181 |
ob_start('_ws_plugin__s2member_force_ssl_buffer'); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|