| 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 |
/**/ |
| 20 |
if (!class_exists ("c_ws_plugin__s2member_ssl_in")) |
| 21 |
{ |
| 22 |
/** |
| 23 |
* SSL routines ( inner processing routines ). |
| 24 |
* |
| 25 |
* @package s2Member\SSL |
| 26 |
* @since 3.5 |
| 27 |
*/ |
| 28 |
class c_ws_plugin__s2member_ssl_in |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Forces SSL on specific Posts/Pages, or any page for that matter. |
| 32 |
* |
| 33 |
* Triggered by Custom Field: `s2member_force_ssl = yes|port#` |
| 34 |
* |
| 35 |
* Triggered by: `?s2-ssl` or `?s2-ssl=yes|port#`. |
| 36 |
* |
| 37 |
* @package s2Member\SSL |
| 38 |
* @since 3.5 |
| 39 |
* |
| 40 |
* @attaches-to: ``add_action("init");`` |
| 41 |
* @attaches-to: ``add_action("template_redirect");`` |
| 42 |
* |
| 43 |
* @return null Possibly exiting script execution after redirection to SSL variation. |
| 44 |
*/ |
| 45 |
public static function force_ssl ($vars = array ()) /* Phase 2 of ``c_ws_plugin__s2member_ssl::check_force_ssl()``. */ |
| 46 |
{ |
| 47 |
extract($vars); /* Extract all vars passed in from: ``c_ws_plugin__s2member_ssl::check_force_ssl()``. */ |
| 48 |
/**/ |
| 49 |
$force_ssl = (!is_string ($force_ssl)) ? (string)(int)$force_ssl : $force_ssl; /* Force string. */ |
| 50 |
$force_ssl = (is_numeric ($force_ssl) && $force_ssl > 1) ? $force_ssl : "yes"; /* Use `yes`. */ |
| 51 |
/**/ |
| 52 |
$ssl_host = preg_replace ("/\:[0-9]+$/", "", $_SERVER["HTTP_HOST"]); /* Remove port here. */ |
| 53 |
$ssl_port = (is_numeric ($force_ssl) && $force_ssl > 1) ? $force_ssl : false; /* Port? */ |
| 54 |
$ssl_host_port = $ssl_host . (($ssl_port) ? ":" . $ssl_port : ""); /* Use port # ? */ |
| 55 |
/**/ |
| 56 |
if (!is_ssl () || !isset ($_GET[$s2_ssl_gv])) /* SSL must be enabled. */ |
| 57 |
{ |
| 58 |
$https = "https://" . $ssl_host_port . $_SERVER["REQUEST_URI"]; |
| 59 |
$https_with_s2_ssl_gv = add_query_arg ($s2_ssl_gv, urlencode ($force_ssl), $https); |
| 60 |
/**/ |
| 61 |
wp_redirect($https_with_s2_ssl_gv); /* Redirect to https. */ |
| 62 |
exit (); /* Clean exit. */ |
| 63 |
} |
| 64 |
else /* Otherwise, we buffer all output, and switch all content over to https. */ |
| 65 |
/* Also, we assume here that other links on the site should NOT be converted to https. */ |
| 66 |
{ |
| 67 |
add_filter ("redirect_canonical", "__return_false"); |
| 68 |
/**/ |
| 69 |
define ("_ws_plugin__s2member_force_ssl_host", $ssl_host); |
| 70 |
define ("_ws_plugin__s2member_force_ssl_port", $ssl_port); |
| 71 |
define ("_ws_plugin__s2member_force_ssl_host_port", $ssl_host_port); |
| 72 |
/**/ |
| 73 |
/* Filter these. We do NOT want to create a sitewide https conversion! */ |
| 74 |
add_filter ("home_url", "_ws_plugin__s2member_force_non_ssl_scheme", 10, 3); |
| 75 |
add_filter ("network_home_url", "_ws_plugin__s2member_force_non_ssl_scheme", 10, 3); |
| 76 |
add_filter ("site_url", "_ws_plugin__s2member_force_non_ssl_scheme", 10, 3); |
| 77 |
add_filter ("network_site_url", "_ws_plugin__s2member_force_non_ssl_scheme", 10, 3); |
| 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_plugins_url", false, get_defined_vars ())) |
| 81 |
add_filter ("plugins_url", "_ws_plugin__s2member_force_non_ssl_scheme", 10, 2); |
| 82 |
/* |
| 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 |
/**/ |
| 87 |
if (!function_exists ("_ws_plugin__s2member_force_ssl_buffer")) |
| 88 |
{ |
| 89 |
function _ws_plugin__s2member_force_ssl_buffer ($buffer = FALSE) |
| 90 |
{ |
| 91 |
$o_pcre = @ini_get ("pcre.backtrack_limit"); /* Current configuration. */ |
| 92 |
/**/ |
| 93 |
@ini_set ("pcre.backtrack_limit", 10000000); /* Expands abilities for this routine. */ |
| 94 |
/**/ |
| 95 |
$ssl_tags = array ("script", "style", "link", "img", "input", "iframe", "object", "embed"); |
| 96 |
$ssl_tags = apply_filters ("_ws_plugin__s2member_force_ssl_buffer_tags_array", $ssl_tags, get_defined_vars ()); |
| 97 |
$ssl_tags = array_unique (array_map ("strtolower", $ssl_tags)); /* This array should be lowercase / unique. */ |
| 98 |
/**/ |
| 99 |
$ssl_regex_tags = implode ("|", array_map ("preg_quote", $ssl_tags)); /* Prepare for regex. */ |
| 100 |
/**/ |
| 101 |
$buffer = ($ssl_regex_tags) ? preg_replace_callback ("/\<(" . $ssl_regex_tags . ")(?![a-z_0-9\-])[^\>]+\>/i", "_ws_plugin__s2member_force_ssl_buffer_callback", $buffer) : $buffer; |
| 102 |
$buffer = (in_array ("object", $ssl_tags)) ? preg_replace_callback ("/\<object(?![a-z_0-9\-])[^\>]*\>.*?\<\/object\>/is", "_ws_plugin__s2member_force_ssl_buffer_callback", $buffer) : $buffer; |
| 103 |
$buffer = (in_array ("script", $ssl_tags)) ? preg_replace_callback ("/\<script(?![a-z_0-9\-])[^\>]*\>.*?\<\/script\>/is", "_ws_plugin__s2member_force_ssl_buffer_callback", $buffer) : $buffer; |
| 104 |
$buffer = (in_array ("style", $ssl_tags)) ? preg_replace_callback ("/\<style(?![a-z_0-9\-])[^\>]*\>.*?\<\/style\>/is", "_ws_plugin__s2member_force_ssl_buffer_callback", $buffer) : $buffer; |
| 105 |
/**/ |
| 106 |
$non_ssl_tags = array ("a"); /* Tags that should NOT contain SSL-enabled links in them. Prevents site-wide conversions */ |
| 107 |
$non_ssl_tags = apply_filters ("_ws_plugin__s2member_force_non_ssl_buffer_tags_array", $non_ssl_tags, get_defined_vars ()); |
| 108 |
$non_ssl_tags = array_unique (array_map ("strtolower", $non_ssl_tags)); /* This array should be lowercase / unique. */ |
| 109 |
/**/ |
| 110 |
$non_ssl_regex_tags = implode ("|", array_map ("preg_quote", $non_ssl_tags)); /* Prepare for regex. */ |
| 111 |
/**/ |
| 112 |
$buffer = ($non_ssl_regex_tags) ? preg_replace_callback ("/\<(" . $non_ssl_regex_tags . ")(?![a-z_0-9\-])[^\>]+\>/i", "_ws_plugin__s2member_force_non_ssl_buffer_callback", $buffer) : $buffer; |
| 113 |
/**/ |
| 114 |
@ini_set ("pcre.backtrack_limit", $o_pcre); /* Restores previous configuration value now. */ |
| 115 |
/**/ |
| 116 |
return apply_filters ("_ws_plugin__s2member_force_ssl_buffer", $buffer, get_defined_vars ()); |
| 117 |
} |
| 118 |
} |
| 119 |
/**/ |
| 120 |
if (!function_exists ("_ws_plugin__s2member_force_ssl_buffer_callback")) |
| 121 |
{ |
| 122 |
function _ws_plugin__s2member_force_ssl_buffer_callback ($m = FALSE) |
| 123 |
{ |
| 124 |
$s = preg_replace ("/http\:\/\//i", "https://", $m[0]); /* Conversion. */ |
| 125 |
/**/ |
| 126 |
if (_ws_plugin__s2member_force_ssl_port && _ws_plugin__s2member_force_ssl_host && _ws_plugin__s2member_force_ssl_host_port) /* Need port conversions? */ |
| 127 |
$s = preg_replace ("/\/" . preg_quote (_ws_plugin__s2member_force_ssl_host, "/") . "(\:[0-9]+)?\//i", "/" . _ws_plugin__s2member_force_ssl_host_port . "/", $s); |
| 128 |
/**/ |
| 129 |
$s = (strtolower ($m[1]) === "link" && preg_match ("/['\"]alternate['\"]/i", $m[0])) ? $m[0] : $s; /* Alternates are fine to leave like they are. */ |
| 130 |
/**/ |
| 131 |
return $s; /* Return string with conversions. */ |
| 132 |
} |
| 133 |
} |
| 134 |
/**/ |
| 135 |
if (!function_exists ("_ws_plugin__s2member_force_non_ssl_buffer_callback")) |
| 136 |
{ |
| 137 |
function _ws_plugin__s2member_force_non_ssl_buffer_callback ($m = FALSE) |
| 138 |
{ |
| 139 |
$s = preg_replace ("/https\:\/\/" . preg_quote (_ws_plugin__s2member_force_ssl_host_port, "/") . "/i", "http://" . _ws_plugin__s2member_force_ssl_host, $m[0]); |
| 140 |
/**/ |
| 141 |
$s = preg_replace ("/https\:\/\/" . preg_quote (_ws_plugin__s2member_force_ssl_host, "/") . "/i", "http://" . _ws_plugin__s2member_force_ssl_host, $s); |
| 142 |
/* |
| 143 |
Data gets converted to prevent a site-wide conversion over to SSL links. |
| 144 |
*/ |
| 145 |
return $s; /* Return string with conversions. */ |
| 146 |
} |
| 147 |
} |
| 148 |
/**/ |
| 149 |
if (!function_exists ("_ws_plugin__s2member_force_non_ssl_scheme")) |
| 150 |
{ |
| 151 |
function _ws_plugin__s2member_force_non_ssl_scheme ($url = FALSE, $path = FALSE, $scheme = FALSE) |
| 152 |
{ |
| 153 |
if (!in_array ($scheme, array ("http", "https"))) /* If NOT explicitly passed through. */ |
| 154 |
{ |
| 155 |
/* Allows for special exceptions to the rule of always forcing a non-SSL scheme. */ |
| 156 |
if (($scheme === "login_post" || $scheme === "rpc") && (force_ssl_login () || force_ssl_admin ())) |
| 157 |
$scheme = "https"; |
| 158 |
else if ($scheme === "login" && force_ssl_admin ()) |
| 159 |
$scheme = "https"; |
| 160 |
else if ($scheme === "admin" && force_ssl_admin ()) |
| 161 |
$scheme = "https"; |
| 162 |
else /* Defaults to http. */ |
| 163 |
$scheme = "http"; |
| 164 |
} |
| 165 |
/**/ |
| 166 |
$scheme = apply_filters ("_ws_plugin__s2member_force_non_ssl_scheme", $scheme, get_defined_vars ()); |
| 167 |
/**/ |
| 168 |
return preg_replace ("/^http(s)?\:\/\//i", $scheme . "://", $url); |
| 169 |
} |
| 170 |
} |
| 171 |
/**/ |
| 172 |
ob_start("_ws_plugin__s2member_force_ssl_buffer"); |
| 173 |
} |
| 174 |
/**/ |
| 175 |
return; /* Return for uniformity. */ |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
?> |