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

188 lines 7.5 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 * @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 $return Optional. One of: `body|array`. Defaults to `body`. If `array`, an array with the following elements is returned:
69 * `headers` *(an array of headers)*, `body` *(the response body string)*, `code` *(http response code)*, `message` *(http response message)*, `response` *(response array)*.
70 * @return str|array|bool Requested response data from the remote location *(see ``$return`` parameter )*, else false on failure.
71 */
72 public static function remote ($url = FALSE, $post_vars = FALSE, $args = FALSE, $return = FALSE)
73 {
74 static $http_response_filtered = false; /* Apply GZ filters only once. */
75 /**/
76 $args = (!is_array ($args)) ? array (): $args; /* Disable SSL verifications. */
77 $args["sslverify"] = (!isset ($args["sslverify"])) ? false : $args["sslverify"];
78 /**/
79 if (!$http_response_filtered && ($http_response_filtered = true))
80 add_filter ("http_response", "c_ws_plugin__s2member_utils_urls::_remote_gz_variations");
81 /**/
82 if ($url) /* Obviously, we must have a valid URL before we do anything at all here. */
83 {
84 if (preg_match ("/^https/i", $url) && stripos (PHP_OS, "win") === 0)
85 add_filter ("use_curl_transport", "__return_false", ($curl_disabled = 1352));
86 /**/
87 if ((is_array ($post_vars) || is_string ($post_vars)) && !empty ($post_vars))
88 $args = array_merge ($args, array ("method" => "POST", "body" => $post_vars));
89 /**/
90 $response = wp_remote_request ($url, $args); /* Get remote request response. */
91 /**/
92 if ($return === "array" && !is_wp_error ($response) && is_array ($response))
93 {
94 $r["code"] = (int)wp_remote_retrieve_response_code ($response);
95 $r["message"] = wp_remote_retrieve_response_message ($response);
96 /**/
97 $r["headers"] = array (); /* Creates an array of lowercase headers. */
98 foreach (array_keys ($r["o_headers"] = wp_remote_retrieve_headers ($response)) as $header)
99 $r["headers"][strtolower ($header)] = $r["o_headers"][$header];
100 /**/
101 $r["body"] = wp_remote_retrieve_body ($response);
102 $r["response"] = $response;
103 }
104 /**/
105 else if (!is_wp_error ($response) && is_array ($response))
106 $r = wp_remote_retrieve_body ($response);
107 /**/
108 else /* Else the request has failed. */
109 $r = false; /* Request failed. */
110 /**/
111 if (!empty ($curl_disabled) && $curl_disabled === 1352)
112 remove_filter ("use_curl_transport", "__return_false", 1352);
113 /**/
114 return $r; /* The return value. */
115 }
116 /**/
117 return false; /* Else return false. */
118 }
119 /**
120 * Filters the `WP_Http` response for additional gzinflate variations.
121 *
122 * @package s2Member\Utilities
123 * @since 3.5
124 *
125 * @attaches-to: ``add_filter("http_response");``
126 *
127 * @param array $response An array of response details.
128 * @return array of ``$response`` details, with possible body modifications.
129 */
130 public static function _remote_gz_variations ($response = array ())
131 {
132 if (!isset ($response["ws__gz_variations"]) && ($response["ws__gz_variations"] = 1))
133 {
134 if (!empty ($response["headers"]["content-encoding"]))
135 if (!empty ($response["body"]) && substr ($response["body"], 0, 2) === "\x78\x9c")
136 if (($gz = @gzinflate (substr ($response["body"], 2))))
137 $response["body"] = $gz;
138 }
139 /**/
140 return $response; /* Return response. */
141 }
142 /**
143 * Filters content redirection status *( uses 302s for browsers )*.
144 *
145 * @package s2Member\Utilities
146 * @since 3.5
147 *
148 * @attaches-to: ``add_filter("ws_plugin__s2member_content_redirect_status");``
149 *
150 * @param int|str $status A numeric redirection status code.
151 * @return int|str A numeric status redirection code, possibly modified to a value of `302`.
152 *
153 * @see http://en.wikipedia.org/wiki/Web_browser_engine
154 */
155 public static function redirect_browsers_using_302_status ($status = 301)
156 {
157 $engines = "msie|trident|gecko|webkit|presto|konqueror|playstation";
158 /**/
159 if ((int)$status === 301 && !empty ($_SERVER["HTTP_USER_AGENT"]))
160 if (($is_browser = preg_match ("/(" . $engines . ")[\/ ]([0-9\.]+)/i", $_SERVER["HTTP_USER_AGENT"])))
161 return 302;
162 /**/
163 return $status; /* Else keep existing status code. */
164 }
165 /**
166 * Parses out a full valid URI, from either a full URL, or a partial.
167 *
168 * @package s2Member\Utilities
169 * @since 3.5
170 *
171 * @param str $url_or_uri Either a full URL, or a partial URI.
172 * @return str|bool A valid URI, starting with `/` on success, else false.
173 */
174 public static function parse_uri ($url_or_uri = FALSE)
175 {
176 if (($parse = @parse_url ($url_or_uri))) /* See: http://php.net/manual/en/function.parse-url.php. */
177 {
178 $parse["path"] = (!empty ($parse["path"])) ? ((strpos ($parse["path"], "/") === 0) ? $parse["path"] : "/" . $parse["path"]) : "/";
179 $parse["path"] = preg_replace ("/\/+/", "/", $parse["path"]); /* Removes multi slashes. */
180 /**/
181 return (!empty ($parse["query"])) ? $parse["path"] . "?" . $parse["query"] : $parse["path"];
182 }
183 /**/
184 return false;
185 }
186 }
187 }
188 ?>