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

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

528 lines 24.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * String 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_strings"))
21 {
22 /**
23 * String utilities.
24 *
25 * @package s2Member\Utilities
26 * @since 3.5
27 */
28 class c_ws_plugin__s2member_utils_strings
29 {
30 /**
31 * Array of all ampersand entities.
32 *
33 * Array keys are actually regex patterns *( very useful )*.
34 *
35 * @package s2Member\Utilities
36 * @since 111106
37 *
38 * @var array
39 */
40 public static /* Array keys are actually regex patterns. */ $ampersand_entities = array ("&amp;" => "&amp;", "&#0*38;" => "&#38;", "&#[xX]0*26;" => "&#x26;");
41 /**
42 * Array of all quote entities *( and entities for quote variations )*.
43 *
44 * Array keys are actually regex patterns *( very useful )*.
45 *
46 * @package s2Member\Utilities
47 * @since 111106
48 *
49 * @var array
50 */
51 public static $quote_entities_w_variations = array ("&apos;" => "&apos;", "&#0*39;" => "&#39;", "&#[xX]0*27;" => "&#x27;", "&lsquo;" => "&lsquo;", "&#0*8216;" => "&#8216;", "&#[xX]0*2018;" => "&#x2018;", "&rsquo;" => "&rsquo;", "&#0*8217;" => "&#8217;", "&#[xX]0*2019;" => "&#x2019;", "&quot;" => "&quot;", "&#0*34;" => "&#34;", "&#[xX]0*22;" => "&#x22;", "&ldquo;" => "&ldquo;", "&#0*8220;" => "&#8220;", "&#[xX]0*201[cC];" => "&#x201C;", "&rdquo;" => "&rdquo;", "&#0*8221;" => "&#8221;", "&#[xX]0*201[dD];" => "&#x201D;");
52 /**
53 * Escapes double quotes.
54 *
55 * @package s2Member\Utilities
56 * @since 3.5
57 *
58 * @param str $string Input string.
59 * @param int $times Mumber of escapes. Defaults to 1.
60 * @return str Output string after double quotes are escaped.
61 */
62 public static function esc_dq ($string = FALSE, $times = FALSE)
63 {
64 $times = (is_numeric ($times) && $times >= 0) ? (int)$times : 1;
65 /**/
66 return str_replace ('"', str_repeat ("\\", $times) . '"', (string)$string);
67 }
68 /**
69 * Escapes single quotes.
70 *
71 * @package s2Member\Utilities
72 * @since 3.5
73 *
74 * @param str $string Input string.
75 * @param int $times Mumber of escapes. Defaults to 1.
76 * @return str Output string after single quotes are escaped.
77 */
78 public static function esc_sq ($string = FALSE, $times = FALSE)
79 {
80 $times = (is_numeric ($times) && $times >= 0) ? (int)$times : 1;
81 /**/
82 return str_replace ("'", str_repeat ("\\", $times) . "'", (string)$string);
83 }
84 /**
85 * Escapes JavaScript and single quotes.
86 *
87 * @package s2Member\Utilities
88 * @since 110901
89 *
90 * @param str $string Input string.
91 * @param int $times Mumber of escapes. Defaults to 1.
92 * @return str Output string after JavaScript and single quotes are escaped.
93 */
94 public static function esc_js_sq ($string = FALSE, $times = FALSE)
95 {
96 $times = (is_numeric ($times) && $times >= 0) ? (int)$times : 1;
97 /**/
98 return str_replace ("'", str_repeat ("\\", $times) . "'", str_replace (array ("\r", "\n"), array ("", '\\n'), str_replace ("\'", "'", (string)$string)));
99 }
100 /**
101 * Escapes dollars signs ( for regex patterns ).
102 *
103 * @package s2Member\Utilities
104 * @since 3.5
105 *
106 * @param str $string Input string.
107 * @param int $times Mumber of escapes. Defaults to 1.
108 * @return str Output string after dollar signs are escaped.
109 */
110 public static function esc_ds ($string = FALSE, $times = FALSE)
111 {
112 $times = (is_numeric ($times) && $times >= 0) ? (int)$times : 1;
113 /**/
114 return str_replace ('$', str_repeat ("\\", $times) . '$', (string)$string);
115 }
116 /**
117 * Sanitizes a string; by stripping characters NOT on a standard U.S. keyboard.
118 *
119 * @package s2Member\Utilities
120 * @since 111106
121 *
122 * @param str $string Input string.
123 * @return str Output string, after characters NOT on a standard U.S. keyboard have been stripped.
124 */
125 public static function strip_2_kb_chars ($string = FALSE)
126 {
127 return preg_replace ("/[^0-9A-Z\r\n\t\s`\=\[\]\\\;',\.\/~\!@#\$%\^&\*\(\)_\+\|\}\{\:\"\?\>\<\-]/i", "", remove_accents ((string)$string));
128 }
129 /**
130 * Trims deeply; alias of ``trim_deep``.
131 *
132 * @package s2Member\Utilities
133 * @since 111106
134 *
135 * @see s2Member\Utilities\c_ws_plugin__s2member_utils_strings::trim_deep()
136 * @see http://php.net/manual/en/function.trim.php
137 *
138 * @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values.
139 * @param str|bool $chars Optional. Defaults to false, indicating the default trim chars ` \t\n\r\0\x0B`. Or, set to a specific string of chars.
140 * @param str|bool $extra_chars Optional. This is NOT possible with PHP alone, but here you can specify extra chars; in addition to ``$chars``.
141 * @return str|array Either the input string, or the input array; after all data is trimmed up according to arguments passed in.
142 */
143 public static function trim ($value = FALSE, $chars = FALSE, $extra_chars = FALSE)
144 {
145 return c_ws_plugin__s2member_utils_strings::trim_deep ($value, $chars, $extra_chars);
146 }
147 /**
148 * Trims deeply; or use {@link s2Member\Utilities\c_ws_plugin__s2member_utils_strings::trim()}.
149 *
150 * @package s2Member\Utilities
151 * @since 3.5
152 *
153 * @see s2Member\Utilities\c_ws_plugin__s2member_utils_strings::trim()
154 * @see http://php.net/manual/en/function.trim.php
155 *
156 * @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values.
157 * @param str|bool $chars Optional. Defaults to false, indicating the default trim chars ` \t\n\r\0\x0B`. Or, set to a specific string of chars.
158 * @param str|bool $extra_chars Optional. This is NOT possible with PHP alone, but here you can specify extra chars; in addition to ``$chars``.
159 * @return str|array Either the input string, or the input array; after all data is trimmed up according to arguments passed in.
160 */
161 public static function trim_deep ($value = FALSE, $chars = FALSE, $extra_chars = FALSE)
162 {
163 $chars = /* List of chars to be trimmed by this routine. */ (is_string ($chars)) ? $chars : " \t\n\r\0\x0B";
164 $chars = (is_string ($extra_chars) /* Adding additional chars? */) ? $chars . $extra_chars : $chars;
165 /**/
166 if (is_array ($value)) /* Handles all types of arrays.
167 Note, we do NOT use ``array_map()`` here, because multiple args to ``array_map()`` causes a loss of string keys.
168 For further details, see: <http://php.net/manual/en/function.array-map.php>. */
169 {
170 foreach ($value as &$r) /* Reference. */
171 $r = c_ws_plugin__s2member_utils_strings::trim_deep ($r, $chars);
172 return $value; /* Return modified array. */
173 }
174 return trim ((string)$value, $chars);
175 }
176 /**
177 * Trims double quotes deeply.
178 *
179 * @package s2Member\Utilities
180 * @since 3.5
181 *
182 * @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values.
183 * @return str|array Either the input string, or the input array; after all data is trimmed up.
184 */
185 public static function trim_dq_deep ($value = FALSE)
186 {
187 return c_ws_plugin__s2member_utils_strings::trim_deep ($value, false, '"');
188 }
189 /**
190 * Trims single quotes deeply.
191 *
192 * @package s2Member\Utilities
193 * @since 111106
194 *
195 * @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values.
196 * @return str|array Either the input string, or the input array; after all data is trimmed up.
197 */
198 public static function trim_sq_deep ($value = FALSE)
199 {
200 return c_ws_plugin__s2member_utils_strings::trim_deep ($value, false, "'");
201 }
202 /**
203 * Trims double and single quotes deeply.
204 *
205 * @package s2Member\Utilities
206 * @since 111106
207 *
208 * @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values.
209 * @return str|array Either the input string, or the input array; after all data is trimmed up.
210 */
211 public static function trim_dsq_deep ($value = FALSE)
212 {
213 return c_ws_plugin__s2member_utils_strings::trim_deep ($value, false, "'" . '"');
214 }
215 /**
216 * Trims all single/double quote entity variations deeply.
217 *
218 * This is useful on Shortcode attributes mangled by a Visual Editor.
219 *
220 * @package s2Member\Utilities
221 * @since 111011
222 *
223 * @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values.
224 * @return str|array Either the input string, or the input array; after all data is trimmed up.
225 */
226 public static function trim_qts_deep ($value = FALSE)
227 {
228 $qts = implode ("|", array_keys /* Keys are regex patterns. */ (c_ws_plugin__s2member_utils_strings::$quote_entities_w_variations));
229 /**/
230 return is_array ($value) ? array_map ("c_ws_plugin__s2member_utils_strings::trim_qts_deep", $value) : preg_replace ("/^(?:" . $qts . ")+|(?:" . $qts . ")+$/", "", (string)$value);
231 }
232 /**
233 * Wraps a string with the characters provided.
234 *
235 * This is useful when preparing an input array for ``c_ws_plugin__s2member_utils_arrays::in_regex_array()``.
236 *
237 * @package s2Member\Utilities
238 * @since 3.5
239 *
240 * @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values.
241 * @param str $beg Optional. A string value to wrap at the beginning of each value.
242 * @param str $end Optional. A string value to wrap at the ending of each value.
243 * @param bool $wrap_e Optional. Defaults to false. Should empty strings be wrapped too?
244 * @return str|array Either the input string, or the input array; after all data is wrapped up.
245 */
246 public static function wrap_deep ($value = FALSE, $beg = FALSE, $end = FALSE, $wrap_e = FALSE)
247 {
248 if (is_array ($value)) /* Handles all types of arrays.
249 Note, we do NOT use ``array_map()`` here, because multiple args to ``array_map()`` causes a loss of string keys.
250 For further details, see: <http://php.net/manual/en/function.array-map.php>. */
251 {
252 foreach ($value as &$r) /* Reference. */
253 $r = c_ws_plugin__s2member_utils_strings::wrap_deep ($r, $beg, $end, $wrap_e);
254 return $value; /* Return modified array. */
255 }
256 return (strlen ((string)$value) || $wrap_e) ? (string)$beg . (string)$value . (string)$end : (string)$value;
257 }
258 /**
259 * Escapes meta characters with ``preg_quote()`` deeply.
260 *
261 * @package s2Member\Utilities
262 * @since 110926
263 *
264 * @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values.
265 * @param str $delimiter Optional. If a delimiting character is specified, it will also be escaped via ``preg_quote()``.
266 * @return str|array Either the input string, or the input array; after all data is escaped with ``preg_quote()``.
267 */
268 public static function preg_quote_deep ($value = FALSE, $delimiter = FALSE)
269 {
270 if (is_array ($value)) /* Handles all types of arrays.
271 Note, we do NOT use ``array_map()`` here, because multiple args to ``array_map()`` causes a loss of string keys.
272 For further details, see: <http://php.net/manual/en/function.array-map.php>. */
273 {
274 foreach ($value as &$r) /* Reference. */
275 $r = c_ws_plugin__s2member_utils_strings::preg_quote_deep ($r, $delimiter);
276 return $value; /* Return modified array. */
277 }
278 return preg_quote ((string)$value, (string)$delimiter);
279 }
280 /**
281 * Generates a random string with letters/numbers/symbols.
282 *
283 * @package s2Member\Utilities
284 * @since 3.5
285 *
286 * @param int $length Optional. Defaults to `12`. Length of the random string.
287 * @param bool $special_chars Defaults to true. If false, special chars are NOT included.
288 * @param bool $extra_special_chars Defaults to false. If true, extra special chars are included.
289 * @return str A randomly generated string, based on parameter configuration.
290 */
291 public static function random_str_gen ($length = FALSE, $special_chars = TRUE, $extra_special_chars = FALSE)
292 {
293 $length = (is_numeric ($length) && $length >= 0) ? (int)$length : 12;
294 /**/
295 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
296 $chars .= ($extra_special_chars) ? "-_ []{}<>~`+=,.;:/?|" : "";
297 $chars .= ($special_chars) ? "!@#$%^&*()" : "";
298 /**/
299 for ($i = 0, $random_str = ""; $i < $length; $i++)
300 $random_str .= substr ($chars, mt_rand (0, strlen ($chars) - 1), 1);
301 /**/
302 return /* Randomly generated string of chars. */ $random_str;
303 }
304 /**
305 * Highlights PHP, and also Shortcodes.
306 *
307 * @package s2Member\Utilities
308 * @since 3.5
309 *
310 * @param str $str Input string to be highlighted.
311 * @return str The highlighted string.
312 */
313 public static function highlight_php ($string = FALSE)
314 {
315 $string = highlight_string ((string)$string, true); /* Start with PHP syntax, then Shortcodes. */
316 /**/
317 return preg_replace ("/\[\/?_*s2[a-z0-9_\-]+.*?\]/i", '<span style="color:#164A61;">$0</span>', $string);
318 }
319 /**
320 * Parses email addresses from a string or array.
321 *
322 * @package s2Member\Utilities
323 * @since 111009
324 *
325 * @param str|array $value Input string or an array is also fine.
326 * @return array Array of parsed email addresses.
327 */
328 public static function parse_emails ($value = FALSE)
329 {
330 if (is_array ($value)) /* Handles all types of arrays.
331 Note, we do NOT use ``array_map()`` here, because multiple args to ``array_map()`` causes a loss of string keys.
332 For further details, see: <http://php.net/manual/en/function.array-map.php>. */
333 {
334 $emails = array (); /* Initialize array. */
335 foreach /* Loop through array. */ ($value as $v)
336 $emails = array_merge ($emails, c_ws_plugin__s2member_utils_strings::parse_emails ($v));
337 return $emails; /* Return array. */
338 }
339 $delimiter = /* Supports semicolons or commas. */ (strpos ((string)$value, ";") !== false) ? ";" : ",";
340 foreach (c_ws_plugin__s2member_utils_strings::trim_deep (preg_split ("/" . preg_quote ($delimiter, "/") . "+/", (string)$value)) as $section)
341 {
342 if (preg_match ("/\<(.+?)\>/", $section, $m) && strpos ($m[1], "@") !== false)
343 $emails[] = $m[1]; /* Email inside <brackets>. */
344 /**/
345 else if (strpos ($section, "@") !== false)
346 $emails[] = $section;
347 }
348 return /* Array. */ (!empty ($emails)) ? $emails : array ();
349 }
350 /**
351 * Base64 URL-safe encoding.
352 *
353 * @package s2Member\Utilities
354 * @since 110913
355 *
356 * @param str $string Input string to be base64 encoded.
357 * @param array $url_unsafe_chars Optional. An array of un-safe characters. Defaults to: ``array("+", "/")``.
358 * @param array $url_safe_chars Optional. An array of safe character replacements. Defaults to: ``array("-", "_")``.
359 * @param str $trim_padding_chars Optional. A string of padding chars to rtrim. Defaults to: `=~.`.
360 * @return str The base64 URL-safe encoded string.
361 */
362 public static function base64_url_safe_encode ($string = FALSE, $url_unsafe_chars = array ("+", "/"), $url_safe_chars = array ("-", "_"), $trim_padding_chars = "=~.")
363 {
364 $string = (string)$string; /* Force string values here. String MUST be a string. */
365 $trim_padding_chars = (string)$trim_padding_chars; /* And force this one too. */
366 /**/
367 $base64_url_safe = str_replace ((array)$url_unsafe_chars, (array)$url_safe_chars, (string)base64_encode ($string));
368 $base64_url_safe = (strlen ($trim_padding_chars)) ? rtrim ($base64_url_safe, $trim_padding_chars) : $base64_url_safe;
369 /**/
370 return $base64_url_safe; /* Base64 encoded, with URL-safe replacements. */
371 }
372 /**
373 * Base64 URL-safe decoding.
374 *
375 * Note, this function is backward compatible with routines supplied by s2Member in the past;
376 * where padding characters were replaced with `~` or `.`, instead of being stripped completely.
377 *
378 * @package s2Member\Utilities
379 * @since 110913
380 *
381 * @param str $base64_url_safe Input string to be base64 decoded.
382 * @param array $url_unsafe_chars Optional. An array of un-safe character replacements. Defaults to: ``array("+", "/")``.
383 * @param array $url_safe_chars Optional. An array of safe characters. Defaults to: ``array("-", "_")``.
384 * @param str $trim_padding_chars Optional. A string of padding chars to rtrim. Defaults to: `=~.`.
385 * @return str The decoded string.
386 */
387 public static function base64_url_safe_decode ($base64_url_safe = FALSE, $url_unsafe_chars = array ("+", "/"), $url_safe_chars = array ("-", "_"), $trim_padding_chars = "=~.")
388 {
389 $base64_url_safe = (string)$base64_url_safe; /* Force string values here. This MUST be a string. */
390 $trim_padding_chars = (string)$trim_padding_chars; /* And force this one too. */
391 /**/
392 $string = (strlen ($trim_padding_chars)) ? rtrim ($base64_url_safe, $trim_padding_chars) : $base64_url_safe;
393 $string = (strlen ($trim_padding_chars)) ? str_pad ($string, strlen ($string) % 4, "=", STR_PAD_RIGHT) : $string;
394 $string = (string)base64_decode (str_replace ((array)$url_safe_chars, (array)$url_unsafe_chars, $string));
395 /**/
396 return $string; /* Base64 decoded, with URL-safe replacements. */
397 }
398 /**
399 * Generates an RSA-SHA1 signature.
400 *
401 * @package s2Member\Utilities
402 * @since 111017
403 *
404 * @param str $string Input string/data, to be signed by this routine.
405 * @param str $key The secret key that will be used in this signature.
406 * @return str|bool An RSA-SHA1 signature string, or false on failure.
407 */
408 public static function rsa_sha1_sign ($string = FALSE, $key = FALSE)
409 {
410 $key = /* Fixes key wrappers. */ c_ws_plugin__s2member_utils_strings::_rsa_sha1_key_fix_wrappers ((string)$key);
411 /**/
412 $signature = /* Command line. */ c_ws_plugin__s2member_utils_strings::_rsa_sha1_shell_sign ((string)$string, (string)$key);
413 /**/
414 if (empty ($signature) && stripos (PHP_OS, "win") === 0 && file_exists (($openssl = "c:\openssl-win32\bin\openssl.exe")))
415 $signature = c_ws_plugin__s2member_utils_strings::_rsa_sha1_shell_sign ((string)$string, (string)$key, /* Specific location. */ $openssl);
416 /**/
417 if (empty ($signature) && stripos (PHP_OS, "win") === 0 && file_exists (($openssl = "c:\openssl-win64\bin\openssl.exe")))
418 $signature = c_ws_plugin__s2member_utils_strings::_rsa_sha1_shell_sign ((string)$string, (string)$key, /* Specific location. */ $openssl);
419 /**/
420 if (empty ($signature) && function_exists ("openssl_get_privatekey") && function_exists ("openssl_sign") && is_resource ($private_key = openssl_get_privatekey ((string)$key)))
421 openssl_sign ((string)$string, $signature, $private_key, OPENSSL_ALGO_SHA1) . openssl_free_key ($private_key);
422 /**/
423 if (empty ($signature)) /* Now, if we're still empty, trigger an error here. */
424 trigger_error ("s2Member was unable to generate an RSA-SHA1 signature." .
425 " Please make sure your installation of PHP is compiled with OpenSSL: `openssl_sign()`." .
426 " See: http://php.net/manual/en/function.openssl-sign.php", E_USER_ERROR);
427 /**/
428 return (!empty ($signature)) ? $signature : false;
429 }
430 /**
431 * Generates an RSA-SHA1 signature from the command line.
432 *
433 * Used by {@link s2Member\Utilities\c_ws_plugin__s2member_utils_strings::rsa_sha1_sign()}.
434 *
435 * @package s2Member\Utilities
436 * @since 111017
437 *
438 * @param str $string Input string/data, to be signed by this routine.
439 * @param str $key The secret key that will be used in this signature.
440 * @param str $openssl Optional. Defaults to `openssl`. Path to OpenSSL executable.
441 * @return str|bool An RSA-SHA1 signature string, or false on failure.
442 */
443 public static function _rsa_sha1_shell_sign ($string = FALSE, $key = FALSE, $openssl = FALSE)
444 {
445 if (function_exists ("shell_exec") && ($esa = "escapeshellarg") && ($openssl = (($openssl && is_string ($openssl)) ? $openssl : "openssl")) && ($temp_dir = c_ws_plugin__s2member_utils_dirs::get_temp_dir ()))
446 {
447 file_put_contents (($string_file = $temp_dir . "/" . md5 (uniqid ("", true) . "rsa-sha1-string") . ".tmp"), (string)$string);
448 file_put_contents (($private_key_file = $temp_dir . "/" . md5 (uniqid ("", true) . "rsa-sha1-private-key") . ".tmp"), (string)$key);
449 file_put_contents (($rsa_sha1_sig_file = $temp_dir . "/" . md5 (uniqid ("", true) . "rsa-sha1-sig") . ".tmp"), "");
450 /**/
451 @shell_exec($esa ($openssl) . " sha1 -sign " . $esa ($private_key_file) . " -out " . $esa ($rsa_sha1_sig_file) . " " . $esa ($string_file));
452 $signature = /* Do NOT trim here. */ file_get_contents ($rsa_sha1_sig_file); /* Was the signature was written? */
453 unlink($rsa_sha1_sig_file) . unlink ($private_key_file) . unlink ($string_file); /* Cleanup. */
454 }
455 return (!empty ($signature)) ? $signature : false;
456 }
457 /**
458 * Fixes incomplete private key wrappers for RSA-SHA1 signing.
459 *
460 * Used by {@link s2Member\Utilities\c_ws_plugin__s2member_utils_strings::rsa_sha1_sign()}.
461 *
462 * @package s2Member\Utilities
463 * @since 111017
464 *
465 * @param str $key The secret key to be used in an RSA-SHA1 signature.
466 * @return str Key with incomplete wrappers corrected, when/if possible.
467 *
468 * @see http://www.faqs.org/qa/qa-14736.html
469 */
470 public static function _rsa_sha1_key_fix_wrappers ($key = FALSE)
471 {
472 if (($key = trim ((string)$key)) && (strpos ($key, "-----BEGIN RSA PRIVATE KEY-----") === false || strpos ($key, "-----END RSA PRIVATE KEY-----") === false))
473 {
474 foreach (($lines = c_ws_plugin__s2member_utils_strings::trim_deep (preg_split ("/[\r\n]+/", $key))) as $line => $value)
475 if (strpos ($value, "-") === 0) /* Begins with a boundary identifying character ( a hyphen `-` )? */
476 {
477 $boundaries = (empty ($boundaries)) ? 1 : $boundaries + 1; /* Counter. */
478 unset($lines[$line]); /* Remove this boundary line. We'll fix these below. */
479 }
480 if (empty ($boundaries) || $boundaries <= 2) /* Do NOT modify keys with more than 2 boundaries. */
481 $key = "-----BEGIN RSA PRIVATE KEY-----\n" . implode ("\n", $lines) . "\n-----END RSA PRIVATE KEY-----";
482 }
483 return $key; /* Always a trimmed string here. */
484 }
485 /**
486 * Generates an HMAC-SHA1 signature.
487 *
488 * @package s2Member\Utilities
489 * @since 111017
490 *
491 * @param str $string Input string/data, to be signed by this routine.
492 * @param str $key The secret key that will be used in this signature.
493 * @return str An HMAC-SHA1 signature string.
494 */
495 public static function hmac_sha1_sign ($string = FALSE, $key = FALSE)
496 {
497 $key_64 = str_pad (((strlen ((string)$key) > 64) ? pack ('H*', sha1 ((string)$key)) : (string)$key), 64, chr (0x00));
498 /**/
499 return pack ('H*', sha1 (($key_64 ^ str_repeat (chr (0x5c), 64)) . pack ('H*', sha1 (($key_64 ^ str_repeat (chr (0x36), 64)) . (string)$string))));
500 }
501 /**
502 * Decodes unreserved chars encoded by PHP's ``urlencode()``, deeply.
503 *
504 * For further details regarding unreserved chars, see: {@link http://www.faqs.org/rfcs/rfc3986.html}.
505 *
506 * @package s2Member\Utilities
507 * @since 111017
508 *
509 * @see http://www.faqs.org/rfcs/rfc3986.html
510 *
511 * @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values.
512 * @return str|array Either the input string, or the input array; after all unreserved chars are decoded properly.
513 */
514 public static function urldecode_ur_chars_deep ($value = array ())
515 {
516 if (is_array ($value)) /* Handles all types of arrays.
517 Note, we do NOT use ``array_map()`` here, because multiple args to ``array_map()`` causes a loss of string keys.
518 For further details, see: <http://php.net/manual/en/function.array-map.php>. */
519 {
520 foreach ($value as &$r) /* Reference. */
521 $r = c_ws_plugin__s2member_utils_strings::urldecode_ur_chars_deep ($r);
522 return $value; /* Return modified array. */
523 }
524 return str_replace (array ("%2D", "%2E", "%5F", "%7E"), array ("-", ".", "_", "~"), (string)$value);
525 }
526 }
527 }
528 ?>