Source Code Documentation


s2member\includes\classes\utils-strings.inc.php


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

s2member\includes\classes\utils-strings.inc.php

Source Code Documentation