| 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 |
* Filters content redirection status *( uses 302s for browsers )*. |
| 58 |
* |
| 59 |
* @package s2Member\Utilities |
| 60 |
* @since 3.5 |
| 61 |
* |
| 62 |
* @attaches-to: ``add_filter("ws_plugin__s2member_content_redirect_status");`` |
| 63 |
* |
| 64 |
* @param int|str $status A numeric redirection status code. |
| 65 |
* @return int|str A numeric status redirection code, possibly modified to a value of `302`. |
| 66 |
* |
| 67 |
* @see http://en.wikipedia.org/wiki/Web_browser_engine |
| 68 |
*/ |
| 69 |
public static function redirect_browsers_using_302_status ($status = 301) |
| 70 |
{ |
| 71 |
$engines = "msie|trident|gecko|webkit|presto|konqueror|playstation"; |
| 72 |
/**/ |
| 73 |
if ((int)$status === 301 && !empty ($_SERVER["HTTP_USER_AGENT"])) |
| 74 |
if (($is_browser = preg_match ("/(" . $engines . ")[\/ ]([0-9\.]+)/i", $_SERVER["HTTP_USER_AGENT"]))) |
| 75 |
return 302; |
| 76 |
/**/ |
| 77 |
return $status; /* Else keep existing status code. */ |
| 78 |
} |
| 79 |
/** |
| 80 |
* Parses out a full valid URI, from either a full URL, or a partial. |
| 81 |
* |
| 82 |
* @package s2Member\Utilities |
| 83 |
* @since 3.5 |
| 84 |
* |
| 85 |
* @param str $url_or_uri Either a full URL, or a partial URI. |
| 86 |
* @return str|bool A valid URI, starting with `/` on success, else false. |
| 87 |
*/ |
| 88 |
public static function parse_uri ($url_or_uri = FALSE) |
| 89 |
{ |
| 90 |
if (($parse = @parse_url ($url_or_uri))) /* See: http://php.net/manual/en/function.parse-url.php. */ |
| 91 |
{ |
| 92 |
$parse["path"] = (!empty ($parse["path"])) ? ((strpos ($parse["path"], "/") === 0) ? $parse["path"] : "/" . $parse["path"]) : "/"; |
| 93 |
$parse["path"] = preg_replace ("/\/+/", "/", $parse["path"]); /* Removes multi slashes. */ |
| 94 |
/**/ |
| 95 |
return (!empty ($parse["query"])) ? $parse["path"] . "?" . $parse["query"] : $parse["path"]; |
| 96 |
} |
| 97 |
/**/ |
| 98 |
return false; |
| 99 |
} |
| 100 |
/** |
| 101 |
* Responsible for all remote communications processed by s2Member. |
| 102 |
* |
| 103 |
* Uses ``wp_remote_request()`` through the `WP_Http` class. |
| 104 |
* This function will try to use cURL first, and then fall back on FOPEN and/or other supported transports. |
| 105 |
* |
| 106 |
* @package s2Member\Utilities |
| 107 |
* @since 3.5 |
| 108 |
* |
| 109 |
* @param str $url Full URL with possible query string parameters. |
| 110 |
* @param str|array $post_vars Optional. Either a string of POST vars, or an array. |
| 111 |
* @param array $args Optional. An array of additional arguments used by ``wp_remote_request()``. |
| 112 |
* @param bool $return Optional. One of: `body|array`. Defaults to `body`. If `array`, an array with the following elements is returned: |
| 113 |
* `headers` *(an array of headers)*, `body` *(the response body string)*, `code` *(http response code)*, `message` *(http response message)*, `response` *(response array)*. |
| 114 |
* @return str|array|bool Requested response data from the remote location *(see ``$return`` parameter )*, else false on failure. |
| 115 |
*/ |
| 116 |
public static function remote ($url = FALSE, $post_vars = FALSE, $args = FALSE, $return = FALSE) |
| 117 |
{ |
| 118 |
if ($url && is_string ($url)) /* We MUST have a valid full URL (string) before we do anything in this routine. */ |
| 119 |
{ |
| 120 |
$args = (!is_array ($args)) ? array (): $args; /* Force array & disable SSL verification. */ |
| 121 |
$args["sslverify"] = (!isset ($args["sslverify"])) ? /* Off. */ false : $args["sslverify"]; |
| 122 |
/**/ |
| 123 |
if ((is_array ($post_vars) || is_string ($post_vars)) && !empty ($post_vars)) |
| 124 |
$args = array_merge ($args, array ("method" => "POST", "body" => $post_vars)); |
| 125 |
/**/ |
| 126 |
if (preg_match ("/^https/i", $url) && stripos (PHP_OS, "win") === 0) |
| 127 |
add_filter ("use_curl_transport", "__return_false", ($curl_disabled = 1352)); |
| 128 |
/**/ |
| 129 |
if (!has_filter ("http_response", "c_ws_plugin__s2member_utils_urls::_remote_gz_variations")) |
| 130 |
add_filter ("http_response", "c_ws_plugin__s2member_utils_urls::_remote_gz_variations"); |
| 131 |
/**/ |
| 132 |
$response = wp_remote_request ($url, $args); /* Try to process the remote request now. */ |
| 133 |
/**/ |
| 134 |
if ($return === "array" /* Return array? */ && !is_wp_error ($response) && is_array ($response)) |
| 135 |
{ |
| 136 |
$r = array ("code" => (int)wp_remote_retrieve_response_code ($response), "message" => wp_remote_retrieve_response_message ($response)); |
| 137 |
/**/ |
| 138 |
$r = array_merge ($r, array ("o_headers" => wp_remote_retrieve_headers ($response), "headers" => array ())); |
| 139 |
foreach (array_keys ($r["o_headers"]) as $header) /* Array of lowercase headers makes things easier. */ |
| 140 |
$r["headers"][strtolower ($header)] = $r["o_headers"][$header]; |
| 141 |
/**/ |
| 142 |
$r = array_merge ($r, array ("body" => wp_remote_retrieve_body ($response), "response" => $response)); |
| 143 |
} |
| 144 |
/**/ |
| 145 |
else if (!is_wp_error ($response) && is_array ($response)) /* Else returning ``$response`` body only. */ |
| 146 |
$r = wp_remote_retrieve_body ($response); |
| 147 |
/**/ |
| 148 |
else /* Else this remote request has failed completely. We must return a `false` value. */ |
| 149 |
$r = false; /* Remote request failed, return false. */ |
| 150 |
/**/ |
| 151 |
if (isset ($curl_disabled) && $curl_disabled === 1352) /* Remove this Filter now? */ |
| 152 |
remove_filter ("use_curl_transport", "__return_false", 1352); |
| 153 |
/**/ |
| 154 |
return $r; /* The ``$r`` return value. */ |
| 155 |
} |
| 156 |
/**/ |
| 157 |
else /* Else, return false. */ |
| 158 |
return false; |
| 159 |
} |
| 160 |
/** |
| 161 |
* Filters the `WP_Http` response for additional gzinflate variations. |
| 162 |
* |
| 163 |
* @package s2Member\Utilities |
| 164 |
* @since 3.5 |
| 165 |
* |
| 166 |
* @attaches-to: ``add_filter("http_response");`` |
| 167 |
* |
| 168 |
* @param array $response An array of response details. |
| 169 |
* @return array of ``$response`` details, with possible body modifications. |
| 170 |
*/ |
| 171 |
public static function _remote_gz_variations ($response = array ()) |
| 172 |
{ |
| 173 |
if (!isset ($response["ws__gz_variations"]) && ($response["ws__gz_variations"] = 1)) |
| 174 |
{ |
| 175 |
if (!empty ($response["headers"]["content-encoding"])) |
| 176 |
if (!empty ($response["body"]) && substr ($response["body"], 0, 2) === "\x78\x9c") |
| 177 |
if (($gz = @gzinflate (substr ($response["body"], 2)))) |
| 178 |
$response["body"] = $gz; |
| 179 |
} |
| 180 |
/**/ |
| 181 |
return $response; /* Return response. */ |
| 182 |
} |
| 183 |
/** |
| 184 |
* Shortens a long URL through tinyURL, or through another backup API used by this routine. |
| 185 |
* |
| 186 |
* @package s2Member\Utilities |
| 187 |
* @since 111002 |
| 188 |
* |
| 189 |
* @param str $url A full/long URL to be shortened. |
| 190 |
* @param str $api_sp Optional. A specific URL shortening API to use. Defaults to that which is configured in the s2Member Dashboard. Normally `tiny_url` by default. |
| 191 |
* @param bool $try_backups Defaults to true. If a failure occurs with the first API, we'll try others until we have success. |
| 192 |
* @return str|bool The shortened URL on success, else false on failure. |
| 193 |
*/ |
| 194 |
public static function shorten ($url = FALSE, $api_sp = FALSE, $try_backups = TRUE) |
| 195 |
{ |
| 196 |
$url = ($url && is_string ($url)) ? $url : false; /* Only accept a string value here. */ |
| 197 |
$api_sp = ($api_sp && is_string ($api_sp)) ? $api_sp : false; /* Only accept a string value. */ |
| 198 |
/**/ |
| 199 |
$default_url_shortener = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["default_url_shortener"]; |
| 200 |
$default_custom_str_url_shortener = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["default_custom_str_url_shortener"]; |
| 201 |
/**/ |
| 202 |
$apis = array ("tiny_url", "goo_gl"); /* The shortening APIs integrated with this routine so far. More will come soon. */ |
| 203 |
/**/ |
| 204 |
if ($url && ($api = ($api_sp) ? strtolower ($api_sp) : $default_url_shortener)) /* If specific, use it. Otherwise, default shortener. */ |
| 205 |
{ |
| 206 |
if (!$api_sp && ($custom_url = apply_filters ("ws_plugin__s2member_url_shorten", false, get_defined_vars ())) && stripos ($custom_url, "http") === 0) |
| 207 |
return ($shorter_url = $custom_url); /* Using whatever other shortener API you prefer, over the ones available by default with s2Member. */ |
| 208 |
/**/ |
| 209 |
else if (!$api_sp && stripos ($default_custom_str_url_shortener, "http") === 0 && ($custom_url = trim (c_ws_plugin__s2member_utils_urls::remote (str_ireplace (array ("%%s2_long_url%%", "%%s2_long_url_md5%%"), array (rawurlencode ($url), urlencode (md5 ($url))), $default_custom_str_url_shortener)))) && stripos ($custom_url, "http") === 0) |
| 210 |
return ($shorter_url = $custom_url); /* Using whatever other shortener API that a site owner prefers, over the ones available by default with s2Member. */ |
| 211 |
/**/ |
| 212 |
else if ($api === "tiny_url" && ($tiny_url = trim (c_ws_plugin__s2member_utils_urls::remote ("http://tinyurl.com/api-create.php?url=" . rawurlencode ($url)))) && stripos ($tiny_url, "http") === 0) |
| 213 |
return ($shorter_url = $tiny_url); /* The default tinyURL API: <http://tinyurl.com/api-create.php?url=http://www.example.com/>. |
| 214 |
/**/ |
| 215 |
else if ($api === "goo_gl" && ($goo_gl = json_decode (trim (c_ws_plugin__s2member_utils_urls::remote ("https://www.googleapis.com/urlshortener/v1/url" . ((($goo_gl_key = apply_filters ("ws_plugin__s2member_url_shorten_api_goo_gl_key", false))) ? "?key=" . urlencode ($goo_gl_key) : ""), json_encode (array ("longUrl" => $url)), array ("headers" => array ("Content-Type" => "application/json")))), true)) && !empty ($goo_gl["id"]) && ($goo_gl_url = $goo_gl["id"]) && stripos ($goo_gl_url, "http") === 0) |
| 216 |
return ($shorter_url = $goo_gl_url); /* Google® API: <http://code.google.com/apis/urlshortener/v1/getting_started.html>. |
| 217 |
/**/ |
| 218 |
else if ($try_backups && count ($apis) > 1) /* Try backups? This way we can still try to shorten the URL. */ |
| 219 |
/**/ |
| 220 |
foreach (array_diff ($apis, array ($api)) as $backup) |
| 221 |
if (($backup = c_ws_plugin__s2member_utils_urls::shorten ($url, $backup, false))) |
| 222 |
return ($shorter_url = $backup); |
| 223 |
} |
| 224 |
/**/ |
| 225 |
return false; /* Default return value. */ |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
?> |