| 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 |
* Escapes double quotes. |
| 32 |
* |
| 33 |
* @package s2Member\Utilities |
| 34 |
* @since 3.5 |
| 35 |
* |
| 36 |
* @param str $string Input string. |
| 37 |
* @param int $times Mumber of escapes. Defaults to 1. |
| 38 |
* @return str Output string after double quotes are escaped. |
| 39 |
*/ |
| 40 |
public static function esc_dq ($string = FALSE, $times = FALSE) |
| 41 |
{ |
| 42 |
$times = (is_numeric ($times) && $times >= 0) ? (int)$times : 1; |
| 43 |
return str_replace ('"', str_repeat ("\\", $times) . '"', (string)$string); |
| 44 |
} |
| 45 |
/** |
| 46 |
* Escapes single quotes. |
| 47 |
* |
| 48 |
* @package s2Member\Utilities |
| 49 |
* @since 3.5 |
| 50 |
* |
| 51 |
* @param str $string Input string. |
| 52 |
* @param int $times Mumber of escapes. Defaults to 1. |
| 53 |
* @return str Output string after single quotes are escaped. |
| 54 |
*/ |
| 55 |
public static function esc_sq ($string = FALSE, $times = FALSE) |
| 56 |
{ |
| 57 |
$times = (is_numeric ($times) && $times >= 0) ? (int)$times : 1; |
| 58 |
return str_replace ("'", str_repeat ("\\", $times) . "'", (string)$string); |
| 59 |
} |
| 60 |
/** |
| 61 |
* Escapes JavaScript and single quotes. |
| 62 |
* |
| 63 |
* @package s2Member\Utilities |
| 64 |
* @since 110901 |
| 65 |
* |
| 66 |
* @param str $string Input string. |
| 67 |
* @param int $times Mumber of escapes. Defaults to 1. |
| 68 |
* @return str Output string after JavaScript and single quotes are escaped. |
| 69 |
*/ |
| 70 |
public static function esc_js_sq ($string = FALSE, $times = FALSE) |
| 71 |
{ |
| 72 |
$times = (is_numeric ($times) && $times >= 0) ? (int)$times : 1; |
| 73 |
return str_replace ("'", str_repeat ("\\", $times) . "'", str_replace (array ("\r", "\n"), array ("", '\\n'), str_replace ("\'", "'", (string)$string))); |
| 74 |
} |
| 75 |
/** |
| 76 |
* Escapes dollars signs ( for regex patterns ). |
| 77 |
* |
| 78 |
* @package s2Member\Utilities |
| 79 |
* @since 3.5 |
| 80 |
* |
| 81 |
* @param str $string Input string. |
| 82 |
* @param int $times Mumber of escapes. Defaults to 1. |
| 83 |
* @return str Output string after dollar signs are escaped. |
| 84 |
*/ |
| 85 |
public static function esc_ds ($string = FALSE, $times = FALSE) |
| 86 |
{ |
| 87 |
$times = (is_numeric ($times) && $times >= 0) ? (int)$times : 1; |
| 88 |
return str_replace ('$', str_repeat ("\\", $times) . '$', (string)$string); |
| 89 |
} |
| 90 |
/** |
| 91 |
* Sanitizes a string; by removing non-standard characters. |
| 92 |
* |
| 93 |
* This allows all characters that appears on a standard U.S. keyboard. |
| 94 |
* |
| 95 |
* @package s2Member\Utilities |
| 96 |
* @since 3.5 |
| 97 |
* |
| 98 |
* @param str $string Input string. |
| 99 |
* @return str Output string after non-keyboard chars are removed. |
| 100 |
*/ |
| 101 |
public static function keyboard_chars_only ($string = FALSE) |
| 102 |
{ |
| 103 |
return preg_replace ("/[^0-9A-Z\r\n\t\s`\=\[\]\\\;',\.\/~\!@#\$%\^&\*\(\)_\+\|\}\{\:\"\?\>\<\-]/i", "", remove_accents ((string)$string)); |
| 104 |
} |
| 105 |
/** |
| 106 |
* Trims deeply. |
| 107 |
* |
| 108 |
* @package s2Member\Utilities |
| 109 |
* @since 3.5 |
| 110 |
* |
| 111 |
* @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values. |
| 112 |
* @return str|array Either the input string, or the input array; after all data is trimmed up. |
| 113 |
*/ |
| 114 |
public static function trim_deep ($value = FALSE) |
| 115 |
{ |
| 116 |
return is_array ($value) ? array_map ("c_ws_plugin__s2member_utils_strings::trim_deep", $value) : trim ((string)$value); |
| 117 |
} |
| 118 |
/** |
| 119 |
* Trims " entities deeply. |
| 120 |
* |
| 121 |
* This is useful on Shortcode attributes mangled by a Visual Editor. |
| 122 |
* |
| 123 |
* @package s2Member\Utilities |
| 124 |
* @since 3.5 |
| 125 |
* |
| 126 |
* @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values. |
| 127 |
* @return str|array Either the input string, or the input array; after all data is trimmed up. |
| 128 |
*/ |
| 129 |
public static function trim_quot_deep ($value = FALSE) |
| 130 |
{ |
| 131 |
return is_array ($value) ? array_map ("c_ws_plugin__s2member_utils_strings::trim_quot_deep", $value) : preg_replace ("(^(")+|(")+$)", "", (string)$value); |
| 132 |
} |
| 133 |
/** |
| 134 |
* Trims double quotes deeply. |
| 135 |
* |
| 136 |
* This is useful on CSV data that is encapsulated by double quotes. |
| 137 |
* |
| 138 |
* @package s2Member\Utilities |
| 139 |
* @since 3.5 |
| 140 |
* |
| 141 |
* @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values. |
| 142 |
* @return str|array Either the input string, or the input array; after all data is trimmed up. |
| 143 |
*/ |
| 144 |
public static function trim_dq_deep ($value = FALSE) |
| 145 |
{ |
| 146 |
return is_array ($value) ? array_map ("c_ws_plugin__s2member_utils_strings::trim_dq_deep", $value) : trim ((string)$value, "\" \t\n\r\0\x0B"); |
| 147 |
} |
| 148 |
/** |
| 149 |
* Wraps a string with the characters provided. |
| 150 |
* |
| 151 |
* This is useful when preparing an input array for ``c_ws_plugin__s2member_utils_arrays::in_regex_array()``. |
| 152 |
* |
| 153 |
* @package s2Member\Utilities |
| 154 |
* @since 3.5 |
| 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 $beg A string value to wrap at the beginning of each value. |
| 158 |
* @param str $end A string value to wrap at the ending of each value. |
| 159 |
* @return str|array Either the input string, or the input array; after all data is wrapped up. |
| 160 |
*/ |
| 161 |
public static function wrap_deep ($value = FALSE, $beg = FALSE, $end = FALSE) |
| 162 |
{ |
| 163 |
return (is_array ($value) && !empty ($value)) ? array_map ("c_ws_plugin__s2member_utils_strings::wrap_deep", $value, array_fill (0, count ($value), $beg), array_fill (0, count ($value), $end)) : ((is_string ($value) && strlen ($value)) ? (string)$beg . (string)$value . (string)$end : ((is_array ($value)) ? $value : (string)$value)); |
| 164 |
} |
| 165 |
/** |
| 166 |
* Generates a random string with letters/numbers/symbols. |
| 167 |
* |
| 168 |
* @package s2Member\Utilities |
| 169 |
* @since 3.5 |
| 170 |
* |
| 171 |
* @param int $length Length of the randomly generated string. |
| 172 |
* @param bool $special_chars Defaults to true. If false, special chars are NOT included. |
| 173 |
* @param bool $extra_special_chars Defaults to false. If true, extra special chars are included. |
| 174 |
* @return str A randomly generated string, based on parameter configuration. |
| 175 |
*/ |
| 176 |
public static function random_str_gen ($length = 12, $special_chars = TRUE, $extra_special_chars = FALSE) |
| 177 |
{ |
| 178 |
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 179 |
$chars .= ($extra_special_chars) ? "-_ []{}<>~`+=,.;:/?|" : ""; |
| 180 |
$chars .= ($special_chars) ? "!@#$%^&*()" : ""; |
| 181 |
/**/ |
| 182 |
for ($i = 0, $random_str = ""; $i < $length; $i++) |
| 183 |
$random_str .= substr ($chars, mt_rand (0, strlen ($chars) - 1), 1); |
| 184 |
/**/ |
| 185 |
return $random_str; |
| 186 |
} |
| 187 |
/** |
| 188 |
* Highlights PHP, and also Shortcodes. |
| 189 |
* |
| 190 |
* @package s2Member\Utilities |
| 191 |
* @since 3.5 |
| 192 |
* |
| 193 |
* @param str $str Input string to be highlighted. |
| 194 |
* @return str The highlighted string. |
| 195 |
*/ |
| 196 |
public static function highlight_php ($string = FALSE) |
| 197 |
{ |
| 198 |
$string = highlight_string ((string)$string, true); /* Start with PHP syntax highlighting first. */ |
| 199 |
/**/ |
| 200 |
return preg_replace_callback ("/(\[)(\/?)(_*s2If|s2Get|s2Member-[A-z_0-9\-]+)(.*?)(\])/i", "c_ws_plugin__s2member_utils_strings::_highlight_php", $string); |
| 201 |
} |
| 202 |
/** |
| 203 |
* Highlights Shortcodes. |
| 204 |
* |
| 205 |
* @package s2Member\Utilities |
| 206 |
* @since 3.5 |
| 207 |
* |
| 208 |
* @param array $m Array passed in from `preg_replace_callback()`. |
| 209 |
* @return str The highlighted string. |
| 210 |
*/ |
| 211 |
public static function _highlight_php ($m = FALSE) |
| 212 |
{ |
| 213 |
return '<span style="color:#164A61;">' . $m[0] . '</span>'; |
| 214 |
} |
| 215 |
/** |
| 216 |
* Base64 URL-safe encoding. |
| 217 |
* |
| 218 |
* @package s2Member\Utilities |
| 219 |
* @since 110913 |
| 220 |
* |
| 221 |
* @param str $string Input string to be base64 encoded. |
| 222 |
* @return str The base64 URL-safe encoded string. |
| 223 |
*/ |
| 224 |
public static function base64_url_safe_encode ($string = FALSE) |
| 225 |
{ |
| 226 |
$string = (string)$string; /* Force to a string value. */ |
| 227 |
return rtrim (str_replace (array ("+", "/"), array ("-", "_"), base64_encode ($string)), "="); |
| 228 |
} |
| 229 |
/** |
| 230 |
* Base64 URL-safe decoding. |
| 231 |
* |
| 232 |
* Note, this function is backward compatible with routines supplied by s2Member in the past; |
| 233 |
* where padding characters were replaced with `~` or `.`, instead of being stripped completely. |
| 234 |
* |
| 235 |
* @package s2Member\Utilities |
| 236 |
* @since 110913 |
| 237 |
* |
| 238 |
* @param str $string Input string to be base64 decoded. |
| 239 |
* @return str The decoded string. |
| 240 |
*/ |
| 241 |
public static function base64_url_safe_decode ($string = FALSE) |
| 242 |
{ |
| 243 |
$string = rtrim ((string)$string, "=~."); /* Remove padding chars `=~.`; we'll add `=` padding below. */ |
| 244 |
return base64_decode (str_pad (str_replace (array ("-", "_"), array ("+", "/"), $string), strlen ($string) % 4, "=", STR_PAD_RIGHT)); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
?> |