PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 111003
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v111003
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-strings.inc.php

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

293 lines 12.1 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 * 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 &quot; 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 ("(^(&quot;)+|(&quot;)+$)", "", (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 Optional. A string value to wrap at the beginning of each value.
158 * @param str $end Optional. 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 if (is_array ($value)) /* Handles all types of arrays.
164 Note, we do NOT use ``array_map()`` here, because multiple args to ``array_map()`` causes a loss of string keys.
165 For further details, see: <http://php.net/manual/en/function.array-map.php>. */
166 {
167 foreach ($value as &$r) /* Reference. */
168 $r = c_ws_plugin__s2member_utils_strings::wrap_deep ($r, $beg, $end);
169 return $value; /* Return modified array. */
170 }
171 return (is_string ($value) && strlen ($value)) ? (string)$beg . $value . (string)$end : (string)$value;
172 }
173 /**
174 * Escapes meta characters with ``preg_quote()`` deeply.
175 *
176 * @package s2Member\Utilities
177 * @since 110926
178 *
179 * @param str|array $value Either a string, an array, or a multi-dimensional array, filled with integer and/or string values.
180 * @param str $delimiter Optional. If a delimiting character is specified, it will also be escaped via ``preg_quote()``.
181 * @return str|array Either the input string, or the input array; after all data is escaped with ``preg_quote()``.
182 */
183 public static function preg_quote_deep ($value = FALSE, $delimiter = FALSE)
184 {
185 if (is_array ($value)) /* Handles all types of arrays.
186 Note, we do NOT use ``array_map()`` here, because multiple args to ``array_map()`` causes a loss of string keys.
187 For further details, see: <http://php.net/manual/en/function.array-map.php>. */
188 {
189 foreach ($value as &$r) /* Reference. */
190 $r = c_ws_plugin__s2member_utils_strings::preg_quote_deep ($r, $delimiter);
191 return $value; /* Return modified array. */
192 }
193 return preg_quote ((string)$value, (string)$delimiter);
194 }
195 /**
196 * Generates a random string with letters/numbers/symbols.
197 *
198 * @package s2Member\Utilities
199 * @since 3.5
200 *
201 * @param int $length Length of the randomly generated string.
202 * @param bool $special_chars Defaults to true. If false, special chars are NOT included.
203 * @param bool $extra_special_chars Defaults to false. If true, extra special chars are included.
204 * @return str A randomly generated string, based on parameter configuration.
205 */
206 public static function random_str_gen ($length = 12, $special_chars = TRUE, $extra_special_chars = FALSE)
207 {
208 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
209 $chars .= ($extra_special_chars) ? "-_ []{}<>~`+=,.;:/?|" : "";
210 $chars .= ($special_chars) ? "!@#$%^&*()" : "";
211 /**/
212 for ($i = 0, $random_str = ""; $i < $length; $i++)
213 $random_str .= substr ($chars, mt_rand (0, strlen ($chars) - 1), 1);
214 /**/
215 return $random_str;
216 }
217 /**
218 * Highlights PHP, and also Shortcodes.
219 *
220 * @package s2Member\Utilities
221 * @since 3.5
222 *
223 * @param str $str Input string to be highlighted.
224 * @return str The highlighted string.
225 */
226 public static function highlight_php ($string = FALSE)
227 {
228 $string = highlight_string ((string)$string, true); /* Start with PHP syntax highlighting first. */
229 /**/
230 return preg_replace_callback ("/(\[)(\/?)(_*s2If|s2Get|s2Member-[A-z_0-9\-]+)(.*?)(\])/i", "c_ws_plugin__s2member_utils_strings::_highlight_php", $string);
231 }
232 /**
233 * Highlights Shortcodes.
234 *
235 * @package s2Member\Utilities
236 * @since 3.5
237 *
238 * @param array $m Array passed in from `preg_replace_callback()`.
239 * @return str The highlighted string.
240 */
241 public static function _highlight_php ($m = FALSE)
242 {
243 return '<span style="color:#164A61;">' . $m[0] . '</span>';
244 }
245 /**
246 * Base64 URL-safe encoding.
247 *
248 * @package s2Member\Utilities
249 * @since 110913
250 *
251 * @param str $string Input string to be base64 encoded.
252 * @param array $url_unsafe_chars Optional. An array of un-safe characters. Defaults to: ``array("+", "/")``.
253 * @param array $url_safe_chars Optional. An array of safe character replacements. Defaults to: ``array("-", "_")``.
254 * @param str $trim_padding_chars Optional. A string of padding chars to rtrim. Defaults to: `=~.`.
255 * @return str The base64 URL-safe encoded string.
256 */
257 public static function base64_url_safe_encode ($string = FALSE, $url_unsafe_chars = array ("+", "/"), $url_safe_chars = array ("-", "_"), $trim_padding_chars = "=~.")
258 {
259 eval ('$string = (string)$string; $trim_padding_chars = (string)$trim_padding_chars;');
260 /**/
261 $base64_url_safe = str_replace ((array)$url_unsafe_chars, (array)$url_safe_chars, base64_encode ($string));
262 $base64_url_safe = (strlen ($trim_padding_chars)) ? rtrim ($base64_url_safe, $trim_padding_chars) : $base64_url_safe;
263 /**/
264 return $base64_url_safe; /* Base64 encoded, with URL-safe replacements. */
265 }
266 /**
267 * Base64 URL-safe decoding.
268 *
269 * Note, this function is backward compatible with routines supplied by s2Member in the past;
270 * where padding characters were replaced with `~` or `.`, instead of being stripped completely.
271 *
272 * @package s2Member\Utilities
273 * @since 110913
274 *
275 * @param str $base64_url_safe Input string to be base64 decoded.
276 * @param array $url_unsafe_chars Optional. An array of un-safe character replacements. Defaults to: ``array("+", "/")``.
277 * @param array $url_safe_chars Optional. An array of safe characters. Defaults to: ``array("-", "_")``.
278 * @param str $trim_padding_chars Optional. A string of padding chars to rtrim. Defaults to: `=~.`.
279 * @return str The decoded string.
280 */
281 public static function base64_url_safe_decode ($base64_url_safe = FALSE, $url_unsafe_chars = array ("+", "/"), $url_safe_chars = array ("-", "_"), $trim_padding_chars = "=~.")
282 {
283 eval ('$base64_url_safe = (string)$base64_url_safe; $trim_padding_chars = (string)$trim_padding_chars;');
284 /**/
285 $string = (strlen ($trim_padding_chars)) ? rtrim ($base64_url_safe, $trim_padding_chars) : $base64_url_safe;
286 $string = (strlen ($trim_padding_chars)) ? str_pad ($string, strlen ($string) % 4, "=", STR_PAD_RIGHT) : $string;
287 $string = base64_decode (str_replace ((array)$url_safe_chars, (array)$url_unsafe_chars, $string));
288 /**/
289 return $string; /* Base64 decoded, with URL-safe replacements. */
290 }
291 }
292 }
293 ?>