Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
fe482240 | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
f24846d5 SL |
18 | use function xKerman\Restricted\unserialize; |
19 | use xKerman\Restricted\UnserializeFailedException; | |
20 | ||
6a488035 TO |
21 | require_once 'HTML/QuickForm/Rule/Email.php'; |
22 | ||
23 | /** | |
fe482240 | 24 | * This class contains string functions. |
6a488035 TO |
25 | */ |
26 | class CRM_Utils_String { | |
7da04cde | 27 | const COMMA = ",", SEMICOLON = ";", SPACE = " ", TAB = "\t", LINEFEED = "\n", CARRIAGELINE = "\r\n", LINECARRIAGE = "\n\r", CARRIAGERETURN = "\r"; |
6a488035 TO |
28 | |
29 | /** | |
30 | * List of all letters and numbers | |
31 | */ | |
32 | const ALPHANUMERIC = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; | |
33 | ||
34 | /** | |
fe482240 | 35 | * Convert a display name into a potential variable name. |
6a488035 | 36 | * |
5ab8fe4a | 37 | * @param string $title title of the string |
f4aaa82a EM |
38 | * @param int $maxLength |
39 | * | |
c866eb5f TO |
40 | * @return string |
41 | * An equivalent variable name. | |
6a488035 | 42 | */ |
00be9182 | 43 | public static function titleToVar($title, $maxLength = 31) { |
6a488035 TO |
44 | $variable = self::munge($title, '_', $maxLength); |
45 | ||
46 | if (CRM_Utils_Rule::title($variable, $maxLength)) { | |
47 | return $variable; | |
48 | } | |
49 | ||
50 | // if longer than the maxLength lets just return a substr of the | |
51 | // md5 to prevent errors downstream | |
52 | return substr(md5($title), 0, $maxLength); | |
53 | } | |
54 | ||
55 | /** | |
fe482240 | 56 | * Replace all non alpha numeric characters and spaces with the replacement character. |
6a488035 | 57 | * |
77855840 TO |
58 | * @param string $name |
59 | * The name to be worked on. | |
60 | * @param string $char | |
61 | * The character to use for non-valid chars. | |
62 | * @param int $len | |
63 | * Length of valid variables. | |
6a488035 | 64 | * |
a6c01b45 CW |
65 | * @return string |
66 | * returns the manipulated string | |
6a488035 | 67 | */ |
00be9182 | 68 | public static function munge($name, $char = '_', $len = 63) { |
fe482240 | 69 | // Replace all white space and non-alpha numeric with $char |
6a488035 TO |
70 | // we only use the ascii character set since mysql does not create table names / field names otherwise |
71 | // CRM-11744 | |
72 | $name = preg_replace('/[^a-zA-Z0-9]+/', $char, trim($name)); | |
755a3caf AA |
73 | |
74 | //If there are no ascii characters present. | |
75 | if ($name == $char) { | |
76 | $name = self::createRandom($len, self::ALPHANUMERIC); | |
77 | } | |
78 | ||
6a488035 TO |
79 | if ($len) { |
80 | // lets keep variable names short | |
81 | return substr($name, 0, $len); | |
82 | } | |
83 | else { | |
84 | return $name; | |
85 | } | |
86 | } | |
87 | ||
dc913073 | 88 | /** |
74c303ca | 89 | * Convert possibly underscore separated words to camel case. |
dc913073 | 90 | * |
74c303ca | 91 | * @param string $str |
92 | * @param bool $ucFirst | |
93 | * Should the first letter be capitalized like `CamelCase` or lower like `camelCase` | |
a6c01b45 | 94 | * @return string |
dc913073 | 95 | */ |
74c303ca | 96 | public static function convertStringToCamel($str, $ucFirst = TRUE) { |
97 | $fragments = explode('_', $str); | |
98 | $camel = implode('', array_map('ucfirst', $fragments)); | |
99 | return $ucFirst ? $camel : lcfirst($camel); | |
dc913073 EM |
100 | } |
101 | ||
6a488035 | 102 | /** |
fe482240 | 103 | * Takes a variable name and munges it randomly into another variable name. |
6a488035 | 104 | * |
77855840 TO |
105 | * @param string $name |
106 | * Initial Variable Name. | |
107 | * @param int $len | |
108 | * Length of valid variables. | |
6a488035 | 109 | * |
a6c01b45 CW |
110 | * @return string |
111 | * Randomized Variable Name | |
6a488035 | 112 | */ |
00be9182 | 113 | public static function rename($name, $len = 4) { |
6a488035 TO |
114 | $rand = substr(uniqid(), 0, $len); |
115 | return substr_replace($name, $rand, -$len, $len); | |
116 | } | |
117 | ||
118 | /** | |
100fef9d | 119 | * Takes a string and returns the last tuple of the string. |
fe482240 EM |
120 | * |
121 | * Useful while converting file names to class names etc | |
6a488035 | 122 | * |
77855840 TO |
123 | * @param string $string |
124 | * The input string. | |
f3258574 CW |
125 | * @param string $char |
126 | * Character used to demarcate the components | |
6a488035 | 127 | * |
a6c01b45 | 128 | * @return string |
f3258574 | 129 | * The last component |
6a488035 | 130 | */ |
00be9182 | 131 | public static function getClassName($string, $char = '_') { |
be2fb01f | 132 | $names = []; |
6a488035 TO |
133 | if (!is_array($string)) { |
134 | $names = explode($char, $string); | |
135 | } | |
136 | if (!empty($names)) { | |
137 | return array_pop($names); | |
138 | } | |
139 | } | |
140 | ||
141 | /** | |
f3258574 | 142 | * Appends a name to a string and separated by delimiter. |
fe482240 EM |
143 | * |
144 | * Does the right thing for an empty string | |
6a488035 | 145 | * |
77855840 TO |
146 | * @param string $str |
147 | * The string to be appended to. | |
148 | * @param string $delim | |
149 | * The delimiter to use. | |
150 | * @param mixed $name | |
151 | * The string (or array of strings) to append. | |
6a488035 | 152 | */ |
00be9182 | 153 | public static function append(&$str, $delim, $name) { |
6a488035 TO |
154 | if (empty($name)) { |
155 | return; | |
156 | } | |
157 | ||
158 | if (is_array($name)) { | |
159 | foreach ($name as $n) { | |
160 | if (empty($n)) { | |
161 | continue; | |
162 | } | |
163 | if (empty($str)) { | |
164 | $str = $n; | |
165 | } | |
166 | else { | |
167 | $str .= $delim . $n; | |
168 | } | |
169 | } | |
170 | } | |
171 | else { | |
172 | if (empty($str)) { | |
173 | $str = $name; | |
174 | } | |
175 | else { | |
176 | $str .= $delim . $name; | |
177 | } | |
178 | } | |
179 | } | |
180 | ||
181 | /** | |
fe482240 | 182 | * Determine if the string is composed only of ascii characters. |
6a488035 | 183 | * |
77855840 TO |
184 | * @param string $str |
185 | * Input string. | |
186 | * @param bool $utf8 | |
187 | * Attempt utf8 match on failure (default yes). | |
6a488035 | 188 | * |
608e6658 | 189 | * @return bool |
a6c01b45 | 190 | * true if string is ascii |
6a488035 | 191 | */ |
00be9182 | 192 | public static function isAscii($str, $utf8 = TRUE) { |
6a488035 TO |
193 | if (!function_exists('mb_detect_encoding')) { |
194 | // eliminate all white space from the string | |
195 | $str = preg_replace('/\s+/', '', $str); | |
196 | // FIXME: This is a pretty brutal hack to make utf8 and 8859-1 work. | |
197 | ||
50bfb460 | 198 | // match low- or high-ascii characters |
6a488035 TO |
199 | if (preg_match('/[\x00-\x20]|[\x7F-\xFF]/', $str)) { |
200 | // || // low ascii characters | |
201 | // high ascii characters | |
202 | // preg_match( '/[\x7F-\xFF]/', $str ) ) { | |
203 | if ($utf8) { | |
50bfb460 | 204 | // if we did match, try for utf-8, or iso8859-1 |
6a488035 TO |
205 | |
206 | return self::isUtf8($str); | |
207 | } | |
208 | else { | |
209 | return FALSE; | |
210 | } | |
211 | } | |
212 | return TRUE; | |
213 | } | |
214 | else { | |
be2fb01f | 215 | $order = ['ASCII']; |
6a488035 TO |
216 | if ($utf8) { |
217 | $order[] = 'UTF-8'; | |
218 | } | |
219 | $enc = mb_detect_encoding($str, $order, TRUE); | |
220 | return ($enc == 'ASCII' || $enc == 'UTF-8'); | |
221 | } | |
222 | } | |
223 | ||
224 | /** | |
fe482240 | 225 | * Determine the string replacements for redaction. |
6a488035 TO |
226 | * on the basis of the regular expressions |
227 | * | |
77855840 TO |
228 | * @param string $str |
229 | * Input string. | |
230 | * @param array $regexRules | |
231 | * Regular expression to be matched w/ replacements. | |
6a488035 | 232 | * |
a6c01b45 CW |
233 | * @return array |
234 | * array of strings w/ corresponding redacted outputs | |
6a488035 | 235 | */ |
00be9182 | 236 | public static function regex($str, $regexRules) { |
50bfb460 | 237 | // redact the regular expressions |
6a488035 | 238 | if (!empty($regexRules) && isset($str)) { |
be2fb01f | 239 | static $matches, $totalMatches, $match = []; |
6a488035 TO |
240 | foreach ($regexRules as $pattern => $replacement) { |
241 | preg_match_all($pattern, $str, $matches); | |
242 | if (!empty($matches[0])) { | |
243 | if (empty($totalMatches)) { | |
244 | $totalMatches = $matches[0]; | |
245 | } | |
246 | else { | |
247 | $totalMatches = array_merge($totalMatches, $matches[0]); | |
248 | } | |
249 | $match = array_flip($totalMatches); | |
250 | } | |
251 | } | |
252 | } | |
253 | ||
254 | if (!empty($match)) { | |
255 | foreach ($match as $matchKey => & $dontCare) { | |
256 | foreach ($regexRules as $pattern => $replacement) { | |
257 | if (preg_match($pattern, $matchKey)) { | |
258 | $dontCare = $replacement . substr(md5($matchKey), 0, 5); | |
259 | break; | |
260 | } | |
261 | } | |
262 | } | |
263 | return $match; | |
264 | } | |
db62d3a5 | 265 | return []; |
6a488035 TO |
266 | } |
267 | ||
5bc392e6 EM |
268 | /** |
269 | * @param $str | |
270 | * @param $stringRules | |
271 | * | |
272 | * @return mixed | |
273 | */ | |
00be9182 | 274 | public static function redaction($str, $stringRules) { |
50bfb460 | 275 | // redact the strings |
6a488035 TO |
276 | if (!empty($stringRules)) { |
277 | foreach ($stringRules as $match => $replace) { | |
278 | $str = str_ireplace($match, $replace, $str); | |
279 | } | |
280 | } | |
281 | ||
50bfb460 | 282 | // return the redacted output |
6a488035 TO |
283 | return $str; |
284 | } | |
285 | ||
286 | /** | |
287 | * Determine if a string is composed only of utf8 characters | |
288 | * | |
77855840 TO |
289 | * @param string $str |
290 | * Input string. | |
6a488035 | 291 | * |
608e6658 | 292 | * @return bool |
6a488035 | 293 | */ |
00be9182 | 294 | public static function isUtf8($str) { |
6a488035 TO |
295 | if (!function_exists(mb_detect_encoding)) { |
296 | // eliminate all white space from the string | |
297 | $str = preg_replace('/\s+/', '', $str); | |
298 | ||
50bfb460 SB |
299 | // pattern stolen from the php.net function documentation for |
300 | // utf8decode(); | |
301 | // comment by JF Sebastian, 30-Mar-2005 | |
6a488035 TO |
302 | return preg_match('/^([\x00-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xec][\x80-\xbf]{2}|\xed[\x80-\x9f][\x80-\xbf]|[\xee-\xef][\x80-\xbf]{2}|f0[\x90-\xbf][\x80-\xbf]{2}|[\xf1-\xf3][\x80-\xbf]{3}|\xf4[\x80-\x8f][\x80-\xbf]{2})*$/', $str); |
303 | // || | |
304 | // iconv('ISO-8859-1', 'UTF-8', $str); | |
305 | } | |
306 | else { | |
be2fb01f | 307 | $enc = mb_detect_encoding($str, ['UTF-8'], TRUE); |
6a488035 TO |
308 | return ($enc !== FALSE); |
309 | } | |
310 | } | |
311 | ||
312 | /** | |
50bfb460 | 313 | * Determine if two hrefs are equivalent (fuzzy match) |
6a488035 | 314 | * |
77855840 TO |
315 | * @param string $url1 |
316 | * The first url to be matched. | |
317 | * @param string $url2 | |
318 | * The second url to be matched against. | |
6a488035 | 319 | * |
608e6658 | 320 | * @return bool |
a6c01b45 | 321 | * true if the urls match, else false |
6a488035 | 322 | */ |
00be9182 | 323 | public static function match($url1, $url2) { |
6a488035 TO |
324 | $url1 = strtolower($url1); |
325 | $url2 = strtolower($url2); | |
326 | ||
327 | $url1Str = parse_url($url1); | |
328 | $url2Str = parse_url($url2); | |
329 | ||
330 | if ($url1Str['path'] == $url2Str['path'] && | |
331 | self::extractURLVarValue(CRM_Utils_Array::value('query', $url1Str)) == self::extractURLVarValue(CRM_Utils_Array::value('query', $url2Str)) | |
332 | ) { | |
333 | return TRUE; | |
334 | } | |
335 | return FALSE; | |
336 | } | |
337 | ||
338 | /** | |
fe482240 | 339 | * Extract the civicrm path from the url. |
6a488035 | 340 | * |
f3258574 CW |
341 | * @param string $query |
342 | * A url string. | |
6a488035 | 343 | * |
f3258574 CW |
344 | * @return string|null |
345 | * civicrm url (eg: civicrm/contact/search) | |
6a488035 | 346 | */ |
00be9182 | 347 | public static function extractURLVarValue($query) { |
6a488035 TO |
348 | $config = CRM_Core_Config::singleton(); |
349 | $urlVar = $config->userFrameworkURLVar; | |
350 | ||
351 | $params = explode('&', $query); | |
352 | foreach ($params as $p) { | |
353 | if (strpos($p, '=')) { | |
354 | list($k, $v) = explode('=', $p); | |
355 | if ($k == $urlVar) { | |
356 | return $v; | |
357 | } | |
358 | } | |
359 | } | |
360 | return NULL; | |
361 | } | |
362 | ||
363 | /** | |
100fef9d | 364 | * Translate a true/false/yes/no string to a 0 or 1 value |
6a488035 | 365 | * |
77855840 TO |
366 | * @param string $str |
367 | * The string to be translated. | |
6a488035 | 368 | * |
608e6658 | 369 | * @return bool |
6a488035 | 370 | */ |
00be9182 | 371 | public static function strtobool($str) { |
6a488035 TO |
372 | if (!is_scalar($str)) { |
373 | return FALSE; | |
374 | } | |
375 | ||
376 | if (preg_match('/^(y(es)?|t(rue)?|1)$/i', $str)) { | |
377 | return TRUE; | |
378 | } | |
379 | return FALSE; | |
380 | } | |
381 | ||
382 | /** | |
100fef9d | 383 | * Returns string '1' for a true/yes/1 string, and '0' for no/false/0 else returns false |
6a488035 | 384 | * |
77855840 TO |
385 | * @param string $str |
386 | * The string to be translated. | |
6a488035 | 387 | * |
608e6658 | 388 | * @return bool |
6a488035 | 389 | */ |
00be9182 | 390 | public static function strtoboolstr($str) { |
6a488035 TO |
391 | if (!is_scalar($str)) { |
392 | return FALSE; | |
393 | } | |
394 | ||
395 | if (preg_match('/^(y(es)?|t(rue)?|1)$/i', $str)) { | |
396 | return '1'; | |
397 | } | |
398 | elseif (preg_match('/^(n(o)?|f(alse)?|0)$/i', $str)) { | |
399 | return '0'; | |
400 | } | |
401 | else { | |
402 | return FALSE; | |
403 | } | |
404 | } | |
405 | ||
406 | /** | |
407 | * Convert a HTML string into a text one using html2text | |
408 | * | |
77855840 TO |
409 | * @param string $html |
410 | * The string to be converted. | |
6a488035 | 411 | * |
a6c01b45 CW |
412 | * @return string |
413 | * the converted string | |
6a488035 | 414 | */ |
00be9182 | 415 | public static function htmlToText($html) { |
05bfb88f | 416 | require_once 'html2text/rcube_html2text.php'; |
bfef4c7e KW |
417 | $token_html = preg_replace('!\{([a-z_.]+)\}!i', 'token:{$1}', $html); |
418 | $converter = new rcube_html2text($token_html); | |
419 | $token_text = $converter->get_text(); | |
420 | $text = preg_replace('!token\:\{([a-z_.]+)\}!i', '{$1}', $token_text); | |
421 | return $text; | |
6a488035 TO |
422 | } |
423 | ||
5bc392e6 EM |
424 | /** |
425 | * @param $string | |
c490a46a | 426 | * @param array $params |
5bc392e6 | 427 | */ |
00be9182 | 428 | public static function extractName($string, &$params) { |
6a488035 TO |
429 | $name = trim($string); |
430 | if (empty($name)) { | |
431 | return; | |
432 | } | |
433 | ||
434 | // strip out quotes | |
435 | $name = str_replace('"', '', $name); | |
436 | $name = str_replace('\'', '', $name); | |
437 | ||
438 | // check for comma in name | |
439 | if (strpos($name, ',') !== FALSE) { | |
440 | ||
441 | // name has a comma - assume lname, fname [mname] | |
442 | $names = explode(',', $name); | |
443 | if (count($names) > 1) { | |
444 | $params['last_name'] = trim($names[0]); | |
445 | ||
446 | // check for space delim | |
447 | $fnames = explode(' ', trim($names[1])); | |
448 | if (count($fnames) > 1) { | |
449 | $params['first_name'] = trim($fnames[0]); | |
450 | $params['middle_name'] = trim($fnames[1]); | |
451 | } | |
452 | else { | |
453 | $params['first_name'] = trim($fnames[0]); | |
454 | } | |
455 | } | |
456 | else { | |
457 | $params['first_name'] = trim($names[0]); | |
458 | } | |
459 | } | |
460 | else { | |
461 | // name has no comma - assume fname [mname] fname | |
462 | $names = explode(' ', $name); | |
463 | if (count($names) == 1) { | |
464 | $params['first_name'] = $names[0]; | |
465 | } | |
466 | elseif (count($names) == 2) { | |
467 | $params['first_name'] = $names[0]; | |
468 | $params['last_name'] = $names[1]; | |
469 | } | |
470 | else { | |
471 | $params['first_name'] = $names[0]; | |
472 | $params['middle_name'] = $names[1]; | |
473 | $params['last_name'] = $names[2]; | |
474 | } | |
475 | } | |
476 | } | |
477 | ||
5bc392e6 EM |
478 | /** |
479 | * @param $string | |
480 | * | |
481 | * @return array | |
482 | */ | |
00be9182 | 483 | public static function &makeArray($string) { |
6a488035 TO |
484 | $string = trim($string); |
485 | ||
486 | $values = explode("\n", $string); | |
be2fb01f | 487 | $result = []; |
6a488035 TO |
488 | foreach ($values as $value) { |
489 | list($n, $v) = CRM_Utils_System::explode('=', $value, 2); | |
490 | if (!empty($v)) { | |
491 | $result[trim($n)] = trim($v); | |
492 | } | |
493 | } | |
494 | return $result; | |
495 | } | |
496 | ||
6a488035 TO |
497 | /** |
498 | * Given an ezComponents-parsed representation of | |
499 | * a text with alternatives return only the first one | |
500 | * | |
77855840 TO |
501 | * @param string $full |
502 | * All alternatives as a long string (or some other text). | |
6a488035 | 503 | * |
a6c01b45 CW |
504 | * @return string |
505 | * only the first alternative found (or the text without alternatives) | |
6a488035 | 506 | */ |
00be9182 | 507 | public static function stripAlternatives($full) { |
be2fb01f | 508 | $matches = []; |
6a488035 TO |
509 | preg_match('/-ALTERNATIVE ITEM 0-(.*?)-ALTERNATIVE ITEM 1-.*-ALTERNATIVE END-/s', $full, $matches); |
510 | ||
511 | if (isset($matches[1]) && | |
512 | trim(strip_tags($matches[1])) != '' | |
513 | ) { | |
514 | return $matches[1]; | |
515 | } | |
516 | else { | |
517 | return $full; | |
518 | } | |
519 | } | |
520 | ||
521 | /** | |
100fef9d | 522 | * Strip leading, trailing, double spaces from string |
6a488035 TO |
523 | * used for postal/greeting/addressee |
524 | * | |
77855840 TO |
525 | * @param string $string |
526 | * Input string to be cleaned. | |
6a488035 | 527 | * |
a6c01b45 CW |
528 | * @return string |
529 | * the cleaned string | |
6a488035 | 530 | */ |
00be9182 | 531 | public static function stripSpaces($string) { |
6a488035 TO |
532 | return (empty($string)) ? $string : preg_replace("/\s{2,}/", " ", trim($string)); |
533 | } | |
534 | ||
535 | /** | |
dc195289 | 536 | * clean the URL 'path' variable that we use |
6a488035 TO |
537 | * to construct CiviCRM urls by removing characters from the path variable |
538 | * | |
77855840 TO |
539 | * @param string $string |
540 | * The input string to be sanitized. | |
541 | * @param array $search | |
542 | * The characters to be sanitized. | |
543 | * @param string $replace | |
544 | * The character to replace it with. | |
6a488035 | 545 | * |
a6c01b45 CW |
546 | * @return string |
547 | * the sanitized string | |
6a488035 | 548 | */ |
608e6658 | 549 | public static function stripPathChars( |
a3e55d9c | 550 | $string, |
6a488035 TO |
551 | $search = NULL, |
552 | $replace = NULL | |
553 | ) { | |
554 | static $_searchChars = NULL; | |
555 | static $_replaceChar = NULL; | |
556 | ||
557 | if (empty($string)) { | |
558 | return $string; | |
559 | } | |
560 | ||
561 | if ($_searchChars == NULL) { | |
be2fb01f | 562 | $_searchChars = [ |
353ffa53 TO |
563 | '&', |
564 | ';', | |
565 | ',', | |
566 | '=', | |
567 | '$', | |
568 | '"', | |
569 | "'", | |
570 | '\\', | |
571 | '<', | |
572 | '>', | |
573 | '(', | |
574 | ')', | |
575 | ' ', | |
576 | "\r", | |
577 | "\r\n", | |
578 | "\n", | |
579 | "\t", | |
be2fb01f | 580 | ]; |
6a488035 TO |
581 | $_replaceChar = '_'; |
582 | } | |
583 | ||
6a488035 TO |
584 | if ($search == NULL) { |
585 | $search = $_searchChars; | |
586 | } | |
587 | ||
588 | if ($replace == NULL) { | |
589 | $replace = $_replaceChar; | |
590 | } | |
591 | ||
592 | return str_replace($search, $replace, $string); | |
593 | } | |
594 | ||
6a488035 TO |
595 | /** |
596 | * Use HTMLPurifier to clean up a text string and remove any potential | |
597 | * xss attacks. This is primarily used in public facing pages which | |
598 | * accept html as the input string | |
599 | * | |
77855840 TO |
600 | * @param string $string |
601 | * The input string. | |
6a488035 | 602 | * |
a6c01b45 CW |
603 | * @return string |
604 | * the cleaned up string | |
6a488035 | 605 | */ |
00be9182 | 606 | public static function purifyHTML($string) { |
e7292422 | 607 | static $_filter = NULL; |
6a488035 TO |
608 | if (!$_filter) { |
609 | $config = HTMLPurifier_Config::createDefault(); | |
610 | $config->set('Core.Encoding', 'UTF-8'); | |
7652022e | 611 | $config->set('Attr.AllowedFrameTargets', ['_blank', '_self', '_parent', '_top']); |
6a488035 TO |
612 | |
613 | // Disable the cache entirely | |
e7292422 | 614 | $config->set('Cache.DefinitionImpl', NULL); |
6a488035 TO |
615 | |
616 | $_filter = new HTMLPurifier($config); | |
617 | } | |
618 | ||
619 | return $_filter->purify($string); | |
620 | } | |
621 | ||
622 | /** | |
623 | * Truncate $string; if $string exceeds $maxLen, place "..." at the end | |
624 | * | |
625 | * @param string $string | |
626 | * @param int $maxLen | |
f4aaa82a EM |
627 | * |
628 | * @return string | |
6a488035 | 629 | */ |
00be9182 | 630 | public static function ellipsify($string, $maxLen) { |
47f49a22 | 631 | if (mb_strlen($string, 'UTF-8') <= $maxLen) { |
6a488035 TO |
632 | return $string; |
633 | } | |
47f49a22 | 634 | return mb_substr($string, 0, $maxLen - 3, 'UTF-8') . '...'; |
6a488035 TO |
635 | } |
636 | ||
637 | /** | |
fe482240 | 638 | * Generate a random string. |
6a488035 TO |
639 | * |
640 | * @param $len | |
641 | * @param $alphabet | |
642 | * @return string | |
643 | */ | |
644 | public static function createRandom($len, $alphabet) { | |
645 | $alphabetSize = strlen($alphabet); | |
646 | $result = ''; | |
647 | for ($i = 0; $i < $len; $i++) { | |
648 | $result .= $alphabet{rand(1, $alphabetSize) - 1}; | |
649 | } | |
650 | return $result; | |
651 | } | |
652 | ||
fc7a0aee TO |
653 | /** |
654 | * Examples: | |
655 | * "admin foo" => array(NULL,"admin foo") | |
656 | * "cms:admin foo" => array("cms", "admin foo") | |
657 | * | |
f4aaa82a | 658 | * @param $delim |
77855840 TO |
659 | * @param string $string |
660 | * E.g. "view all contacts". Syntax: "[prefix:]name". | |
f4aaa82a EM |
661 | * @param null $defaultPrefix |
662 | * | |
a6c01b45 CW |
663 | * @return array |
664 | * (0 => string|NULL $prefix, 1 => string $value) | |
fc7a0aee TO |
665 | */ |
666 | public static function parsePrefix($delim, $string, $defaultPrefix = NULL) { | |
667 | $pos = strpos($string, $delim); | |
668 | if ($pos === FALSE) { | |
be2fb01f | 669 | return [$defaultPrefix, $string]; |
fc7a0aee TO |
670 | } |
671 | else { | |
be2fb01f | 672 | return [substr($string, 0, $pos), substr($string, 1 + $pos)]; |
fc7a0aee TO |
673 | } |
674 | } | |
675 | ||
87b48098 | 676 | /** |
100fef9d | 677 | * This function will mask part of the the user portion of an Email address (everything before the @) |
87b48098 | 678 | * |
77855840 TO |
679 | * @param string $email |
680 | * The email address to be masked. | |
681 | * @param string $maskChar | |
682 | * The character used for masking. | |
683 | * @param int $percent | |
684 | * The percentage of the user portion to be masked. | |
87b48098 | 685 | * |
a6c01b45 CW |
686 | * @return string |
687 | * returns the masked Email address | |
87b48098 | 688 | */ |
e7292422 | 689 | public static function maskEmail($email, $maskChar = '*', $percent = 50) { |
87b48098 K |
690 | list($user, $domain) = preg_split("/@/", $email); |
691 | $len = strlen($user); | |
e7292422 | 692 | $maskCount = floor($len * $percent / 100); |
87b48098 K |
693 | $offset = floor(($len - $maskCount) / 2); |
694 | ||
695 | $masked = substr($user, 0, $offset) | |
353ffa53 TO |
696 | . str_repeat($maskChar, $maskCount) |
697 | . substr($user, $maskCount + $offset); | |
87b48098 | 698 | |
92fcb95f | 699 | return ($masked . '@' . $domain); |
87b48098 K |
700 | } |
701 | ||
702 | /** | |
fe482240 | 703 | * This function compares two strings. |
87b48098 | 704 | * |
77855840 TO |
705 | * @param string $strOne |
706 | * String one. | |
707 | * @param string $strTwo | |
708 | * String two. | |
709 | * @param bool $case | |
710 | * Boolean indicating whether you want the comparison to be case sensitive or not. | |
87b48098 | 711 | * |
608e6658 | 712 | * @return bool |
a6c01b45 | 713 | * TRUE (string are identical); FALSE (strings are not identical) |
87b48098 K |
714 | */ |
715 | public static function compareStr($strOne, $strTwo, $case) { | |
716 | if ($case == TRUE) { | |
dca9da6e | 717 | // Convert to lowercase and trim white spaces |
87b48098 K |
718 | if (strtolower(trim($strOne)) == strtolower(trim($strTwo))) { |
719 | // yes - they are identical | |
720 | return TRUE; | |
721 | } | |
722 | else { | |
723 | // not identical | |
724 | return FALSE; | |
725 | } | |
726 | } | |
727 | if ($case == FALSE) { | |
dca9da6e | 728 | // Trim white spaces |
87b48098 K |
729 | if (trim($strOne) == trim($strTwo)) { |
730 | // yes - they are identical | |
731 | return TRUE; | |
732 | } | |
733 | else { | |
734 | // not identical | |
735 | return FALSE; | |
736 | } | |
737 | } | |
738 | } | |
fc7a0aee | 739 | |
77d45291 TO |
740 | /** |
741 | * Many parts of the codebase have a convention of internally passing around | |
742 | * HTML-encoded URLs. This effectively means that "&" is replaced by "&" | |
743 | * (because most other odd characters are %-escaped in URLs; and %-escaped | |
744 | * strings don't need any extra escaping in HTML). | |
745 | * | |
608e6658 | 746 | * @param string $htmlUrl |
77855840 | 747 | * URL with HTML entities. |
a6c01b45 CW |
748 | * @return string |
749 | * URL without HTML entities | |
77d45291 TO |
750 | */ |
751 | public static function unstupifyUrl($htmlUrl) { | |
752 | return str_replace('&', '&', $htmlUrl); | |
753 | } | |
96025800 | 754 | |
6c094ca6 SM |
755 | /** |
756 | * When a user supplies a URL (e.g. to an image), we'd like to: | |
757 | * - Remove the protocol and domain name if the URL points to the current | |
758 | * site. | |
759 | * - Keep the domain name for remote URLs. | |
760 | * - Optionally, force remote URLs to use https instead of http (which is | |
761 | * useful for images) | |
762 | * | |
763 | * @param string $url | |
764 | * The URL to simplify. Examples: | |
765 | * "https://example.org/sites/default/files/coffee-mug.jpg" | |
766 | * "sites/default/files/coffee-mug.jpg" | |
767 | * "http://i.stack.imgur.com/9jb2ial01b.png" | |
768 | * @param bool $forceHttps = FALSE | |
769 | * If TRUE, ensure that remote URLs use https. If a URL with | |
770 | * http is supplied, then we'll change it to https. | |
771 | * This is useful for situations like showing a premium product on a | |
772 | * contribution, because (as reported in CRM-14283) if the user gets a | |
773 | * browser warning like "page contains insecure elements" on a contribution | |
774 | * page, that's a very bad thing. Thus, even if changing http to https | |
775 | * breaks the image, that's better than leaving http content in a | |
776 | * contribution page. | |
777 | * | |
778 | * @return string | |
779 | * The simplified URL. Examples: | |
780 | * "/sites/default/files/coffee-mug.jpg" | |
781 | * "https://i.stack.imgur.com/9jb2ial01b.png" | |
782 | */ | |
783 | public static function simplifyURL($url, $forceHttps = FALSE) { | |
784 | $config = CRM_Core_Config::singleton(); | |
785 | $siteURLParts = self::simpleParseUrl($config->userFrameworkBaseURL); | |
786 | $urlParts = self::simpleParseUrl($url); | |
787 | ||
788 | // If the image is locally hosted, then only give the path to the image | |
789 | $urlIsLocal | |
790 | = ($urlParts['host+port'] == '') | |
791 | | ($urlParts['host+port'] == $siteURLParts['host+port']); | |
792 | if ($urlIsLocal) { | |
793 | // and make sure it begins with one forward slash | |
794 | return preg_replace('_^/*(?=.)_', '/', $urlParts['path+query']); | |
795 | } | |
796 | ||
797 | // If the URL is external, then keep the full URL as supplied | |
798 | else { | |
799 | return $forceHttps ? preg_replace('_^http://_', 'https://', $url) : $url; | |
800 | } | |
801 | } | |
802 | ||
803 | /** | |
804 | * A simplified version of PHP's parse_url() function. | |
805 | * | |
806 | * @param string $url | |
807 | * e.g. "https://example.com:8000/foo/bar/?id=1#fragment" | |
808 | * | |
809 | * @return array | |
810 | * Will always contain keys 'host+port' and 'path+query', even if they're | |
811 | * empty strings. Example: | |
812 | * [ | |
813 | * 'host+port' => "example.com:8000", | |
814 | * 'path+query' => "/foo/bar/?id=1", | |
815 | * ] | |
816 | */ | |
817 | public static function simpleParseUrl($url) { | |
818 | $parts = parse_url($url); | |
2e1f50d6 | 819 | $host = $parts['host'] ?? ''; |
6c094ca6 | 820 | $port = isset($parts['port']) ? ':' . $parts['port'] : ''; |
2e1f50d6 | 821 | $path = $parts['path'] ?? ''; |
6c094ca6 | 822 | $query = isset($parts['query']) ? '?' . $parts['query'] : ''; |
be2fb01f | 823 | return [ |
6c094ca6 SM |
824 | 'host+port' => "$host$port", |
825 | 'path+query' => "$path$query", | |
be2fb01f | 826 | ]; |
6c094ca6 SM |
827 | } |
828 | ||
79a18c21 | 829 | /** |
fe482240 | 830 | * Formats a string of attributes for insertion in an html tag. |
79a18c21 CW |
831 | * |
832 | * @param array $attributes | |
833 | * | |
834 | * @return string | |
835 | */ | |
836 | public static function htmlAttributes($attributes) { | |
837 | $output = ''; | |
838 | foreach ($attributes as $name => $vals) { | |
839 | $output .= " $name=\"" . htmlspecialchars(implode(' ', (array) $vals)) . '"'; | |
840 | } | |
841 | return ltrim($output); | |
842 | } | |
843 | ||
83d511e6 TO |
844 | /** |
845 | * Determine if $string starts with $fragment. | |
846 | * | |
847 | * @param string $string | |
848 | * The long string. | |
849 | * @param string $fragment | |
850 | * The fragment to look for. | |
851 | * @return bool | |
852 | */ | |
853 | public static function startsWith($string, $fragment) { | |
854 | if ($fragment === '') { | |
855 | return TRUE; | |
856 | } | |
857 | $len = strlen($fragment); | |
858 | return substr($string, 0, $len) === $fragment; | |
859 | } | |
860 | ||
861 | /** | |
862 | * Determine if $string ends with $fragment. | |
863 | * | |
864 | * @param string $string | |
865 | * The long string. | |
866 | * @param string $fragment | |
867 | * The fragment to look for. | |
868 | * @return bool | |
869 | */ | |
870 | public static function endsWith($string, $fragment) { | |
871 | if ($fragment === '') { | |
872 | return TRUE; | |
873 | } | |
874 | $len = strlen($fragment); | |
875 | return substr($string, -1 * $len) === $fragment; | |
876 | } | |
877 | ||
878 | /** | |
879 | * @param string|array $patterns | |
880 | * @param array $allStrings | |
881 | * @param bool $allowNew | |
882 | * Whether to return new, unrecognized names. | |
883 | * @return array | |
884 | */ | |
885 | public static function filterByWildcards($patterns, $allStrings, $allowNew = FALSE) { | |
886 | $patterns = (array) $patterns; | |
be2fb01f | 887 | $result = []; |
83d511e6 TO |
888 | foreach ($patterns as $pattern) { |
889 | if (!\CRM_Utils_String::endsWith($pattern, '*')) { | |
890 | if ($allowNew || in_array($pattern, $allStrings)) { | |
891 | $result[] = $pattern; | |
892 | } | |
893 | } | |
894 | else { | |
895 | $prefix = rtrim($pattern, '*'); | |
896 | foreach ($allStrings as $key) { | |
897 | if (\CRM_Utils_String::startsWith($key, $prefix)) { | |
898 | $result[] = $key; | |
899 | } | |
900 | } | |
901 | } | |
902 | } | |
903 | return array_values(array_unique($result)); | |
904 | } | |
905 | ||
f24846d5 | 906 | /** |
dc7c2d3e SL |
907 | * Safely unserialize a string of scalar or array values (but not objects!) |
908 | * | |
909 | * Use `xkerman/restricted-unserialize` to unserialize strings using PHP's | |
910 | * serialization format. `restricted-unserialize` works like PHP's built-in | |
911 | * `unserialize` function except that it does not deserialize object instances, | |
912 | * making it immune to PHP Object Injection {@see https://www.owasp.org/index.php/PHP_Object_Injection} | |
913 | * vulnerabilities. | |
914 | * | |
915 | * Note: When dealing with user inputs, it is generally recommended to use | |
916 | * safe, standard data interchange formats such as JSON rather than PHP's | |
917 | * serialization format when dealing with user input. | |
918 | * | |
f24846d5 SL |
919 | * @param string|NULL $string |
920 | * | |
921 | * @return mixed | |
f24846d5 SL |
922 | */ |
923 | public static function unserialize($string) { | |
924 | if (!is_string($string)) { | |
925 | return FALSE; | |
926 | } | |
927 | try { | |
928 | return unserialize($string); | |
929 | } | |
930 | catch (UnserializeFailedException $e) { | |
079546f5 | 931 | return FALSE; |
f24846d5 | 932 | } |
dc7c2d3e | 933 | } |
f24846d5 | 934 | |
e3c6d5ff CW |
935 | /** |
936 | * Returns the plural form of an English word. | |
937 | * | |
938 | * @param string $str | |
939 | * @return string | |
940 | */ | |
941 | public static function pluralize($str) { | |
449c4e6b CW |
942 | $lastLetter = substr($str, -1); |
943 | $lastTwo = substr($str, -2); | |
944 | if ($lastLetter == 's' || $lastTwo == 'ch') { | |
945 | return $str . 'es'; | |
946 | } | |
947 | if ($lastLetter == 'y' && $lastTwo != 'ey') { | |
948 | return substr($str, 0, -1) . 'ies'; | |
e3c6d5ff | 949 | } |
449c4e6b | 950 | return $str . 's'; |
e3c6d5ff CW |
951 | } |
952 | ||
1441a378 | 953 | /** |
954 | * Generic check as to whether any tokens are in the given string. | |
955 | * | |
956 | * It might be a smarty token OR a CiviCRM token. In both cases the | |
957 | * absence of a '{' indicates no token is present. | |
958 | * | |
959 | * @param string $string | |
960 | * | |
961 | * @return bool | |
962 | */ | |
963 | public static function stringContainsTokens(string $string) { | |
964 | return strpos($string, '{') !== FALSE; | |
965 | } | |
966 | ||
ab46cf18 | 967 | /** |
968 | * Parse a string through smarty without creating a smarty template file per string. | |
969 | * | |
970 | * This function is for swapping out any smarty tokens that appear in a string | |
971 | * and are not re-used much if at all. For example parsing a contact's greeting | |
972 | * does not need to be cached are there are some minor security / data privacy benefits | |
973 | * to not caching them per file. We also save disk space, reduce I/O and disk clearing time. | |
974 | * | |
975 | * Doing this is cleaning in Smarty3 which we are alas not using | |
976 | * https://www.smarty.net/docs/en/resources.string.tpl | |
977 | * | |
978 | * However, it highlights that smarty-eval is not evil-eval and still have the security applied. | |
979 | * | |
980 | * In order to replicate that in Smarty2 I'm using {eval} per | |
981 | * https://www.smarty.net/docsv2/en/language.function.eval.tpl#id2820446 | |
982 | * From the above: | |
983 | * - Evaluated variables are treated the same as templates. They follow the same escapement and security features just as if they were templates. | |
984 | * - Evaluated variables are compiled on every invocation, the compiled versions are not saved! However if you have caching enabled, the output | |
985 | * will be cached with the rest of the template. | |
986 | * | |
987 | * Our set up does not have caching enabled and my testing suggests this still works fine with it | |
988 | * enabled so turning it off before running this is out of caution based on the above. | |
989 | * | |
990 | * When this function is run only one template file is created (for the eval) tag no matter how | |
991 | * many times it is run. This compares to it otherwise creating one file for every parsed string. | |
992 | * | |
993 | * @param string $templateString | |
994 | * | |
995 | * @return string | |
996 | */ | |
997 | public static function parseOneOffStringThroughSmarty($templateString) { | |
998 | if (!CRM_Utils_String::stringContainsTokens($templateString)) { | |
999 | // Skip expensive smarty processing. | |
1000 | return $templateString; | |
1001 | } | |
1002 | $smarty = CRM_Core_Smarty::singleton(); | |
1003 | $cachingValue = $smarty->caching; | |
1004 | $smarty->caching = 0; | |
1005 | $smarty->assign('smartySingleUseString', $templateString); | |
1006 | $templateString = $smarty->fetch('string:{eval var=$smartySingleUseString}'); | |
1007 | $smarty->caching = $cachingValue; | |
1008 | $smarty->assign('smartySingleUseString', NULL); | |
1009 | return $templateString; | |
1010 | } | |
1011 | ||
6a488035 | 1012 | } |