PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 111017
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v111017
260917 260913 260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 120213 All 189 releases
s2member / includes / classes / utils-urls.inc.php

utils-urls.inc.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions 111017, at includes/classes/utils-urls.inc.php

274 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @return str Location of `/wp-login.php?action=register`.
49 */
50 public static function wp_register_url () /* With Filters. */
51 {
52 return apply_filters ("wp_register_location", add_query_arg ("action", urlencode ("register"), wp_login_url ()), get_defined_vars ());
53 }
54 /**
55 * Builds a BuddyPress registration URL to `/register`.
56 *
57 * @package s2Member\Utilities
58 * @since 111009
59 *
60 * @return str Location of `/register`.
61 */
62 public static function bp_register_url () /* Only if BuddyPress is installed. */
63 {
64 if (c_ws_plugin__s2member_utils_conds::bp_is_installed ()) /* Only if BuddyPress is installed. */
65 return site_url (((function_exists ("bp_get_signup_slug")) ? bp_get_signup_slug () . "/" : BP_REGISTER_SLUG . "/"));
66 }
67 /**
68 * Filters content redirection status *( uses 302s for browsers )*.
69 *
70 * @package s2Member\Utilities
71 * @since 3.5
72 *
73 * @attaches-to: ``add_filter("ws_plugin__s2member_content_redirect_status");``
74 *
75 * @param int|str $status A numeric redirection status code.
76 * @return int|str A numeric status redirection code, possibly modified to a value of `302`.
77 *
78 * @see http://en.wikipedia.org/wiki/Web_browser_engine
79 */
80 public static function redirect_browsers_using_302_status ($status = 301)
81 {
82 $engines = "msie|trident|gecko|webkit|presto|konqueror|playstation";
83 /**/
84 if ((int)$status === 301 && !empty ($_SERVER["HTTP_USER_AGENT"]))
85 if (($is_browser = preg_match ("/(" . $engines . ")[\/ ]([0-9\.]+)/i", $_SERVER["HTTP_USER_AGENT"])))
86 return 302;
87 /**/
88 return $status; /* Else keep existing status code. */
89 }
90 /**
91 * Parses out a full valid URI, from either a full URL, or a partial URI.
92 *
93 * Uses {@link s2Member\Utilities\c_ws_plugin__s2member_utils_urls::parse_url()}.
94 *
95 * @package s2Member\Utilities
96 * @since 3.5
97 *
98 * @param str $url_or_uri Either a full URL, or a partial URI.
99 * @return str A valid URI, starting with `/` on success, else an empty string.
100 */
101 public static function parse_uri ($url_or_uri = FALSE)
102 {
103 if (is_string ($url_or_uri) && is_array ($parse = c_ws_plugin__s2member_utils_urls::parse_url ($url_or_uri)))
104 {
105 $parse["path"] = (!empty ($parse["path"])) ? ((strpos ($parse["path"], "/") === 0) ? $parse["path"] : "/" . $parse["path"]) : "/";
106 /**/
107 return (!empty ($parse["query"])) ? $parse["path"] . "?" . $parse["query"] : $parse["path"];
108 }
109 else /* Force a string return value here. */
110 return ""; /* Empty string. */
111 }
112 /**
113 * Parses a URL with same args as PHP's ``parse_url()`` function.
114 *
115 * This works around issues with this PHP function in versions prior to 5.3.8.
116 *
117 * @package s2Member\Utilities
118 * @since 111017
119 *
120 * @param str $url_or_uri Either a full URL, or a partial URI.
121 * @param bool|int $component Optional. See PHP documentation on ``parse_url()`` function.
122 * @param bool $clean_path Defaults to true. s2Member will cleanup any return array `path`.
123 * @return str|array|bool The return value from PHP's ``parse_url()`` function.
124 * However, if ``$component`` is passed, s2Member forces a string return.
125 */
126 public static function parse_url ($url_or_uri = FALSE, $component = FALSE, $clean_path = TRUE)
127 {
128 $component = ($component === false || $component === -1) ? -1 : $component;
129 /**/
130 if (is_string ($url_or_uri) && strpos ($url_or_uri, "?") !== "false") /* A query string? */
131 {
132 list ($_, $query) = preg_split ("/\?/", $url_or_uri, 2); /* Split at the query string. */
133 /* Works around bug in many versions of PHP. See: <https://bugs.php.net/bug.php?id=38143>. */
134 $query = str_replace ("://", urlencode ("://"), $query);
135 $url_or_uri = $_ . "?" . $query;
136 }
137 /**/
138 $parse = @parse_url ($url_or_uri, $component); /* Let PHP work its magic now. */
139 /**/
140 if ($clean_path && isset ($parse["path"]) && is_string ($parse["path"]) && !empty ($parse["path"]))
141 $parse["path"] = preg_replace ("/\/+/", "/", $parse["path"]);
142 /**/
143 return ($component !== -1) ? /* Force a string return value here? */ (string)$parse : $parse;
144 }
145 /**
146 * Responsible for all remote communications processed by s2Member.
147 *
148 * Uses ``wp_remote_request()`` through the `WP_Http` class.
149 * This function will try to use cURL first, and then fall back on FOPEN and/or other supported transports.
150 *
151 * @package s2Member\Utilities
152 * @since 3.5
153 *
154 * @param str $url Full URL with possible query string parameters.
155 * @param str|array $post_vars Optional. Either a string of POST vars, or an array.
156 * @param array $args Optional. An array of additional arguments used by ``wp_remote_request()``.
157 * @param bool $return Optional. One of: `body|array`. Defaults to `body`. If `array`, an array with the following elements is returned:
158 * `headers` *(an array of headers)*, `body` *(the response body string)*, `code` *(http response code)*, `message` *(http response message)*, `response` *(response array)*.
159 * @return str|array|bool Requested response data from the remote location *(see ``$return`` parameter )*, else false on failure.
160 */
161 public static function remote ($url = FALSE, $post_vars = FALSE, $args = FALSE, $return = FALSE)
162 {
163 if ($url && is_string ($url)) /* We MUST have a valid full URL (string) before we do anything in this routine. */
164 {
165 $args = (!is_array ($args)) ? array (): $args; /* Force array & disable SSL verification. */
166 $args["sslverify"] = (!isset ($args["sslverify"])) ? /* Off. */ false : $args["sslverify"];
167 /**/
168 if ((is_array ($post_vars) || is_string ($post_vars)) && !empty ($post_vars))
169 $args = array_merge ($args, array ("method" => "POST", "body" => $post_vars));
170 /**/
171 if (preg_match ("/^https/i", $url) && stripos (PHP_OS, "win") === 0)
172 add_filter ("use_curl_transport", "__return_false", ($curl_disabled = 1352));
173 /**/
174 if (!has_filter ("http_response", "c_ws_plugin__s2member_utils_urls::_remote_gz_variations"))
175 add_filter ("http_response", "c_ws_plugin__s2member_utils_urls::_remote_gz_variations");
176 /**/
177 $response = wp_remote_request ($url, $args); /* Try to process the remote request now. */
178 /**/
179 if ($return === "array" /* Return array? */ && !is_wp_error ($response) && is_array ($response))
180 {
181 $r = array ("code" => (int)wp_remote_retrieve_response_code ($response), "message" => wp_remote_retrieve_response_message ($response));
182 /**/
183 $r = array_merge ($r, array ("o_headers" => wp_remote_retrieve_headers ($response), "headers" => array ()));
184 foreach (array_keys ($r["o_headers"]) as $header) /* Array of lowercase headers makes things easier. */
185 $r["headers"][strtolower ($header)] = $r["o_headers"][$header];
186 /**/
187 $r = array_merge ($r, array ("body" => wp_remote_retrieve_body ($response), "response" => $response));
188 }
189 /**/
190 else if (!is_wp_error ($response) && is_array ($response)) /* Else returning ``$response`` body only. */
191 $r = wp_remote_retrieve_body ($response);
192 /**/
193 else /* Else this remote request has failed completely. We must return a `false` value. */
194 $r = false; /* Remote request failed, return false. */
195 /**/
196 if (isset ($curl_disabled) && $curl_disabled === 1352) /* Remove this Filter now? */
197 remove_filter ("use_curl_transport", "__return_false", 1352);
198 /**/
199 return $r; /* The ``$r`` return value. */
200 }
201 /**/
202 else /* Else, return false. */
203 return false;
204 }
205 /**
206 * Filters the `WP_Http` response for additional gzinflate variations.
207 *
208 * @package s2Member\Utilities
209 * @since 3.5
210 *
211 * @attaches-to: ``add_filter("http_response");``
212 *
213 * @param array $response An array of response details.
214 * @return array of ``$response`` details, with possible body modifications.
215 */
216 public static function _remote_gz_variations ($response = array ())
217 {
218 if (!isset ($response["ws__gz_variations"]) && ($response["ws__gz_variations"] = 1))
219 {
220 if (!empty ($response["headers"]["content-encoding"]))
221 if (!empty ($response["body"]) && substr ($response["body"], 0, 2) === "\x78\x9c")
222 if (($gz = @gzinflate (substr ($response["body"], 2))))
223 $response["body"] = $gz;
224 }
225 /**/
226 return $response; /* Return response. */
227 }
228 /**
229 * Shortens a long URL, based on s2Member configuration.
230 *
231 * @package s2Member\Utilities
232 * @since 111002
233 *
234 * @param str $url A full/long URL to be shortened.
235 * @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.
236 * @param bool $try_backups Defaults to true. If a failure occurs with the first API, we'll try others until we have success.
237 * @return str|bool The shortened URL on success, else false on failure.
238 */
239 public static function shorten ($url = FALSE, $api_sp = FALSE, $try_backups = TRUE)
240 {
241 $url = ($url && is_string ($url)) ? $url : false; /* Only accept a string value here. */
242 $api_sp = ($api_sp && is_string ($api_sp)) ? $api_sp : false; /* Only accept a string value. */
243 /**/
244 $default_url_shortener = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["default_url_shortener"];
245 $default_custom_str_url_shortener = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["default_custom_str_url_shortener"];
246 /**/
247 $apis = array ("tiny_url", "goo_gl"); /* The shortening APIs integrated with this routine so far. More will come soon. */
248 /**/
249 if ($url && ($api = ($api_sp) ? strtolower ($api_sp) : $default_url_shortener)) /* If specific, use it. Otherwise, default shortener. */
250 {
251 if (!$api_sp && ($custom_url = apply_filters ("ws_plugin__s2member_url_shorten", false, get_defined_vars ())) && stripos ($custom_url, "http") === 0)
252 return ($shorter_url = $custom_url); /* Using whatever other shortener API you prefer, over the ones available by default with s2Member. */
253 /**/
254 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)
255 return ($shorter_url = $custom_url); /* Using whatever other shortener API that a site owner prefers, over the ones available by default with s2Member. */
256 /**/
257 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)
258 return ($shorter_url = $tiny_url); /* The default tinyURL API: <http://tinyurl.com/api-create.php?url=http://www.example.com/>.
259 /**/
260 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)
261 return ($shorter_url = $goo_gl_url); /* Google® API: <http://code.google.com/apis/urlshortener/v1/getting_started.html>.
262 /**/
263 else if ($try_backups && count ($apis) > 1) /* Try backups? This way we can still try to shorten the URL. */
264 /**/
265 foreach (array_diff ($apis, array ($api)) as $backup)
266 if (($backup = c_ws_plugin__s2member_utils_urls::shorten ($url, $backup, false)))
267 return ($shorter_url = $backup);
268 }
269 /**/
270 return false; /* Default return value. */
271 }
272 }
273 }
274 ?>