PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 111011
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v111011
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 111011, at includes/classes/utils-urls.inc.php

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