| 1 |
<?php |
| 2 |
/** |
| 3 |
* URL 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 |
/**/ |
| 20 |
if (!class_exists ("c_ws_plugin__s2member_utils_urls")) |
| 21 |
{ |
| 22 |
/** |
| 23 |
* URL utilities. |
| 24 |
* |
| 25 |
* @package s2Member\Utilities |
| 26 |
* @since 3.5 |
| 27 |
*/ |
| 28 |
class c_ws_plugin__s2member_utils_urls |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Builds a WordPress® signup URL to `/wp-signup.php`. |
| 32 |
* |
| 33 |
* @package s2Member\Utilities |
| 34 |
* @since 3.5 |
| 35 |
* |
| 36 |
* @return str Full URL to `/wp-signup.php`. |
| 37 |
*/ |
| 38 |
public static function wp_signup_url () /* With Filters. */ |
| 39 |
{ |
| 40 |
return apply_filters ("wp_signup_location", site_url ("/wp-signup.php")); |
| 41 |
} |
| 42 |
/** |
| 43 |
* Builds a WordPress® registration URL to `/wp-login.php?action=register`. |
| 44 |
* |
| 45 |
* @package s2Member\Utilities |
| 46 |
* @since 3.5 |
| 47 |
* |
| 48 |
* @param str $redirect_to Optional. Force a specific redirection after registration. |
| 49 |
* @return str Location of `/wp-login.php?action=register`. |
| 50 |
*/ |
| 51 |
public static function wp_register_url ($redirect_to = FALSE) |
| 52 |
{ |
| 53 |
return apply_filters ("wp_register_location", /* « NOT a core Filter; we're anticipating. */ |
| 54 |
add_query_arg ("action", urlencode ("register"), wp_login_url ($redirect_to)), get_defined_vars ()); |
| 55 |
} |
| 56 |
/** |
| 57 |
* Responsible for all remote communications processed by s2Member. |
| 58 |
* |
| 59 |
* Uses ``wp_remote_request()`` through the `WP_Http` class. |
| 60 |
* This function will try to use cURL first, and then fall back on FOPEN and/or other supported transports. |
| 61 |
* |
| 62 |
* @package s2Member\Utilities |
| 63 |
* @since 3.5 |
| 64 |
* |
| 65 |
* @param str $url Full URL with possible query string parameters. |
| 66 |
* @param str|array $post_vars Optional. Either a string of POST vars, or an array. |
| 67 |
* @param array $args Optional. An array of additional arguments used by ``wp_remote_request()``. |
| 68 |
* @param bool $raw Optional. Return raw data w/ headers too? Defaults to false. |
| 69 |
* @return str|bool The data received in the response from the remote location, else false on failure. |
| 70 |
*/ |
| 71 |
public static function remote ($url = FALSE, $post_vars = FALSE, $args = array (), $raw = FALSE) |
| 72 |
{ |
| 73 |
static $http_response_filtered = false; /* Apply GZ filters only once. */ |
| 74 |
/**/ |
| 75 |
$args = (!is_array ($args)) ? array (): $args; /* Disable SSL verifications. */ |
| 76 |
$args["sslverify"] = (!isset ($args["sslverify"])) ? false : $args["sslverify"]; |
| 77 |
/**/ |
| 78 |
if (!$http_response_filtered && ($http_response_filtered = true)) |
| 79 |
add_filter ("http_response", "c_ws_plugin__s2member_utils_urls::_remote_gz_variations"); |
| 80 |
/**/ |
| 81 |
if ($url) /* Obviously, we must have a valid URL before we do anything at all here. */ |
| 82 |
{ |
| 83 |
if (preg_match ("/^https/i", $url) && strtolower (substr (PHP_OS, 0, 3)) === "win") |
| 84 |
add_filter ("use_curl_transport", "__return_false", ($curl_disabled = 1352)); |
| 85 |
/**/ |
| 86 |
if ((is_array ($post_vars) || is_string ($post_vars)) && !empty ($post_vars)) |
| 87 |
$args = array_merge ($args, array ("method" => "POST", "body" => $post_vars)); |
| 88 |
/**/ |
| 89 |
$response = wp_remote_request ($url, $args); /* Get response array. */ |
| 90 |
/**/ |
| 91 |
if ($raw && !($r = "")) /* Return a raw response w/ all headers too? */ |
| 92 |
{ |
| 93 |
foreach (wp_remote_retrieve_headers ($response) as $header => $header_v) |
| 94 |
$r .= $header . ": " . $header_v . "\r\n"; |
| 95 |
$r = trim ($r) . "\r\n\r\n"; /* Separate headers. */ |
| 96 |
$r .= wp_remote_retrieve_response_message ($response); |
| 97 |
} |
| 98 |
else /* Else we just retrieve the body. */ |
| 99 |
$r = wp_remote_retrieve_body ($response); |
| 100 |
/**/ |
| 101 |
if ($curl_was_disabled_by_this_routine_with_1352_priority = $curl_disabled) |
| 102 |
remove_filter ("use_curl_transport", "__return_false", 1352); |
| 103 |
/**/ |
| 104 |
return $r; /* The return value. */ |
| 105 |
} |
| 106 |
/**/ |
| 107 |
return false; /* Else return false. */ |
| 108 |
} |
| 109 |
/** |
| 110 |
* Filters the `WP_Http` response for additional gzinflate variations. |
| 111 |
* |
| 112 |
* @package s2Member\Utilities |
| 113 |
* @since 3.5 |
| 114 |
* |
| 115 |
* @attaches-to: ``add_filter("http_response");`` |
| 116 |
* |
| 117 |
* @param array $response An array of response details. |
| 118 |
* @return array of ``$response`` details, with possible body modifications. |
| 119 |
*/ |
| 120 |
public static function _remote_gz_variations ($response = array ()) |
| 121 |
{ |
| 122 |
if (!isset ($response["ws__gz_variations"]) && ($response["ws__gz_variations"] = 1)) |
| 123 |
{ |
| 124 |
if (!empty ($response["headers"]["content-encoding"])) |
| 125 |
if (!empty ($response["body"]) && substr ($response["body"], 0, 2) === "\x78\x9c") |
| 126 |
if (($gz = @gzinflate (substr ($response["body"], 2)))) |
| 127 |
$response["body"] = $gz; |
| 128 |
} |
| 129 |
/**/ |
| 130 |
return $response; /* Return response. */ |
| 131 |
} |
| 132 |
/** |
| 133 |
* Filters content redirection status *( uses 302s for browsers )*. |
| 134 |
* |
| 135 |
* @package s2Member\Utilities |
| 136 |
* @since 3.5 |
| 137 |
* |
| 138 |
* @attaches-to: ``add_filter("ws_plugin__s2member_content_redirect_status");`` |
| 139 |
* |
| 140 |
* @param int|str $status A numeric redirection status code. |
| 141 |
* @return int|str A numeric status redirection code, possibly modified to a value of `302`. |
| 142 |
* |
| 143 |
* @see http://en.wikipedia.org/wiki/Web_browser_engine |
| 144 |
*/ |
| 145 |
public static function redirect_browsers_using_302_status ($status = 301) |
| 146 |
{ |
| 147 |
$engines = "msie|trident|gecko|webkit|presto|konqueror|playstation"; |
| 148 |
/**/ |
| 149 |
if ((int)$status === 301 && !empty ($_SERVER["HTTP_USER_AGENT"])) |
| 150 |
if (($is_browser = preg_match ("/(" . $engines . ")[\/ ]([0-9\.]+)/i", $_SERVER["HTTP_USER_AGENT"]))) |
| 151 |
return 302; |
| 152 |
/**/ |
| 153 |
return $status; /* Else keep existing status code. */ |
| 154 |
} |
| 155 |
/** |
| 156 |
* Parses out a full valid URI, from either a full URL, or a partial. |
| 157 |
* |
| 158 |
* @package s2Member\Utilities |
| 159 |
* @since 3.5 |
| 160 |
* |
| 161 |
* @param str $url_or_uri Either a full URL, or a partial URI. |
| 162 |
* @return str|bool A valid URI, starting with `/` on success, else false. |
| 163 |
*/ |
| 164 |
public static function parse_uri ($url_or_uri = FALSE) |
| 165 |
{ |
| 166 |
if (($parse = @parse_url ($url_or_uri))) /* See: http://php.net/manual/en/function.parse-url.php. */ |
| 167 |
{ |
| 168 |
$parse["path"] = (!empty ($parse["path"])) ? ((strpos ($parse["path"], "/") === 0) ? $parse["path"] : "/" . $parse["path"]) : "/"; |
| 169 |
$parse["path"] = preg_replace ("/\/+/", "/", $parse["path"]); /* Removes multi slashes. */ |
| 170 |
/**/ |
| 171 |
return (!empty ($parse["query"])) ? $parse["path"] . "?" . $parse["query"] : $parse["path"]; |
| 172 |
} |
| 173 |
/**/ |
| 174 |
return false; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
?> |