3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 use function xKerman\Restricted\unserialize
;
19 use xKerman\Restricted\UnserializeFailedException
;
21 require_once 'HTML/QuickForm/Rule/Email.php';
24 * This class contains string functions.
26 class CRM_Utils_String
{
27 const COMMA
= ",", SEMICOLON
= ";", SPACE
= " ", TAB
= "\t", LINEFEED
= "\n", CARRIAGELINE
= "\r\n", LINECARRIAGE
= "\n\r", CARRIAGERETURN
= "\r";
30 * List of all letters and numbers
32 const ALPHANUMERIC
= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
35 * Convert a display name into a potential variable name.
37 * @param string $title title of the string
38 * @param int $maxLength
41 * An equivalent variable name.
43 public static function titleToVar($title, $maxLength = 31) {
44 $variable = self
::munge($title, '_', $maxLength);
46 // FIXME: nothing below this line makes sense. The above call to self::munge will always
47 // return a safe string of the correct length, so why are we now checking if it's a safe
48 // string of the correct length?
49 if (CRM_Utils_Rule
::title($variable, $maxLength)) {
53 // FIXME: When would this ever be reachable?
54 return substr(md5($title), 0, $maxLength);
58 * Replace all non alpha numeric characters and spaces with the replacement character.
61 * The name to be worked on.
63 * The character to use for non-valid chars.
65 * Length of valid variables.
68 * returns the manipulated string
70 public static function munge($name, $char = '_', $len = 63) {
71 // Replace all white space and non-alpha numeric with $char
72 // we only use the ascii character set since mysql does not create table names / field names otherwise
74 $name = preg_replace('/[^a-zA-Z0-9]+/', $char, trim($name));
76 //If there are no ascii characters present.
78 $name = self
::createRandom($len, self
::ALPHANUMERIC
);
82 // lets keep variable names short
83 return substr($name, 0, $len);
91 * Convert possibly underscore separated words to camel case.
94 * @param bool $ucFirst
95 * Should the first letter be capitalized like `CamelCase` or lower like `camelCase`
98 public static function convertStringToCamel($str, $ucFirst = TRUE) {
99 $fragments = explode('_', $str);
100 $camel = implode('', array_map('ucfirst', $fragments));
101 return $ucFirst ?
$camel : lcfirst($camel);
105 * Inverse of above function, converts camelCase to snake_case
110 public static function convertStringToSnakeCase(string $str): string {
111 return strtolower(ltrim(preg_replace('/(?=[A-Z])/', '_$0', $str), '_'));
115 * Takes a variable name and munges it randomly into another variable name.
117 * @param string $name
118 * Initial Variable Name.
120 * Length of valid variables.
123 * Randomized Variable Name
125 public static function rename($name, $len = 4) {
126 $rand = substr(uniqid(), 0, $len);
127 return substr_replace($name, $rand, -$len, $len);
131 * Takes a string and returns the last tuple of the string.
133 * Useful while converting file names to class names etc
135 * @param string $string
137 * @param string $char
138 * Character used to demarcate the components
143 public static function getClassName($string, $char = '_') {
145 if (!is_array($string)) {
146 $names = explode($char, $string);
148 if (!empty($names)) {
149 return array_pop($names);
154 * Appends a name to a string and separated by delimiter.
156 * Does the right thing for an empty string
159 * The string to be appended to.
160 * @param string $delim
161 * The delimiter to use.
163 * The string (or array of strings) to append.
165 public static function append(&$str, $delim, $name) {
170 if (is_array($name)) {
171 foreach ($name as $n) {
188 $str .= $delim . $name;
194 * Determine if the string is composed only of ascii characters.
199 * Attempt utf8 match on failure (default yes).
202 * true if string is ascii
204 public static function isAscii($str, $utf8 = TRUE) {
205 if (!function_exists('mb_detect_encoding')) {
206 // eliminate all white space from the string
207 $str = preg_replace('/\s+/', '', $str);
208 // FIXME: This is a pretty brutal hack to make utf8 and 8859-1 work.
210 // match low- or high-ascii characters
211 if (preg_match('/[\x00-\x20]|[\x7F-\xFF]/', $str)) {
212 // || // low ascii characters
213 // high ascii characters
214 // preg_match( '/[\x7F-\xFF]/', $str ) ) {
216 // if we did match, try for utf-8, or iso8859-1
218 return self
::isUtf8($str);
231 $enc = mb_detect_encoding($str, $order, TRUE);
232 return ($enc == 'ASCII' ||
$enc == 'UTF-8');
237 * Encode string using URL-safe Base64.
242 * @see https://tools.ietf.org/html/rfc4648#section-5
244 public static function base64UrlEncode($v) {
245 return rtrim(str_replace(['+', '/'], ['-', '_'], base64_encode($v)), '=');
249 * Decode string using URL-safe Base64.
253 * @return false|string
254 * @see https://tools.ietf.org/html/rfc4648#section-5
256 public static function base64UrlDecode($v) {
257 // PHP base64_decode() is already forgiving about padding ("=").
258 return base64_decode(str_replace(['-', '_'], ['+', '/'], $v));
262 * Determine the string replacements for redaction.
263 * on the basis of the regular expressions
267 * @param array $regexRules
268 * Regular expression to be matched w/ replacements.
271 * array of strings w/ corresponding redacted outputs
273 public static function regex($str, $regexRules) {
274 // redact the regular expressions
275 if (!empty($regexRules) && isset($str)) {
276 static $matches, $totalMatches, $match = [];
277 foreach ($regexRules as $pattern => $replacement) {
278 preg_match_all($pattern, $str, $matches);
279 if (!empty($matches[0])) {
280 if (empty($totalMatches)) {
281 $totalMatches = $matches[0];
284 $totalMatches = array_merge($totalMatches, $matches[0]);
286 $match = array_flip($totalMatches);
291 if (!empty($match)) {
292 foreach ($match as $matchKey => & $dontCare) {
293 foreach ($regexRules as $pattern => $replacement) {
294 if (preg_match($pattern, $matchKey)) {
295 $dontCare = $replacement . substr(md5($matchKey), 0, 5);
307 * @param $stringRules
311 public static function redaction($str, $stringRules) {
312 // redact the strings
313 if (!empty($stringRules)) {
314 foreach ($stringRules as $match => $replace) {
315 $str = str_ireplace($match, $replace, $str);
319 // return the redacted output
324 * Determine if a string is composed only of utf8 characters
331 public static function isUtf8($str) {
332 $enc = mb_detect_encoding($str, ['UTF-8'], TRUE);
333 return ($enc !== FALSE);
337 * Determine if two hrefs are equivalent (fuzzy match)
339 * @param string $url1
340 * The first url to be matched.
341 * @param string $url2
342 * The second url to be matched against.
345 * true if the urls match, else false
347 public static function match($url1, $url2) {
348 $url1 = strtolower($url1);
349 $url2 = strtolower($url2);
351 $url1Str = parse_url($url1);
352 $url2Str = parse_url($url2);
354 if ($url1Str['path'] == $url2Str['path'] &&
355 self
::extractURLVarValue(CRM_Utils_Array
::value('query', $url1Str)) == self
::extractURLVarValue(CRM_Utils_Array
::value('query', $url2Str))
363 * Extract the civicrm path from the url.
365 * @param string $query
368 * @return string|null
369 * civicrm url (eg: civicrm/contact/search)
371 public static function extractURLVarValue($query) {
372 $config = CRM_Core_Config
::singleton();
373 $urlVar = $config->userFrameworkURLVar
;
375 $params = explode('&', $query);
376 foreach ($params as $p) {
377 if (strpos($p, '=')) {
378 list($k, $v) = explode('=', $p);
388 * Translate a true/false/yes/no string to a 0 or 1 value
391 * The string to be translated.
395 public static function strtobool($str) {
396 if (!is_scalar($str)) {
400 if (preg_match('/^(y(es)?|t(rue)?|1)$/i', $str)) {
407 * Returns string '1' for a true/yes/1 string, and '0' for no/false/0 else returns false
410 * The string to be translated.
412 * @return string|false
414 public static function strtoboolstr($str) {
415 if (!is_scalar($str)) {
419 if (preg_match('/^(y(es)?|t(rue)?|1)$/i', $str)) {
422 elseif (preg_match('/^(n(o)?|f(alse)?|0)$/i', $str)) {
431 * Convert a HTML string into a text one using html2text
433 * @param string $html
434 * The string to be converted.
437 * the converted string
439 public static function htmlToText($html) {
440 require_once 'html2text/rcube_html2text.php';
441 $token_html = preg_replace('!\{([a-z_.]+)\}!i', 'token:{$1}', $html);
442 $converter = new rcube_html2text($token_html);
443 $token_text = $converter->get_text();
444 $text = preg_replace('!token\:\{([a-z_.]+)\}!i', '{$1}', $token_text);
450 * @param array $params
452 public static function extractName($string, &$params) {
453 $name = trim($string);
459 $name = str_replace('"', '', $name);
460 $name = str_replace('\'', '', $name);
462 // check for comma in name
463 if (strpos($name, ',') !== FALSE) {
465 // name has a comma - assume lname, fname [mname]
466 $names = explode(',', $name);
467 if (count($names) > 1) {
468 $params['last_name'] = trim($names[0]);
470 // check for space delim
471 $fnames = explode(' ', trim($names[1]));
472 if (count($fnames) > 1) {
473 $params['first_name'] = trim($fnames[0]);
474 $params['middle_name'] = trim($fnames[1]);
477 $params['first_name'] = trim($fnames[0]);
481 $params['first_name'] = trim($names[0]);
485 // name has no comma - assume fname [mname] fname
486 $names = explode(' ', $name);
487 if (count($names) == 1) {
488 $params['first_name'] = $names[0];
490 elseif (count($names) == 2) {
491 $params['first_name'] = $names[0];
492 $params['last_name'] = $names[1];
495 $params['first_name'] = $names[0];
496 $params['middle_name'] = $names[1];
497 $params['last_name'] = $names[2];
507 public static function &makeArray($string) {
508 $string = trim($string);
510 $values = explode("\n", $string);
512 foreach ($values as $value) {
513 list($n, $v) = CRM_Utils_System
::explode('=', $value, 2);
515 $result[trim($n)] = trim($v);
522 * Given an ezComponents-parsed representation of
523 * a text with alternatives return only the first one
525 * @param string $full
526 * All alternatives as a long string (or some other text).
529 * only the first alternative found (or the text without alternatives)
531 public static function stripAlternatives($full) {
533 preg_match('/-ALTERNATIVE ITEM 0-(.*?)-ALTERNATIVE ITEM 1-.*-ALTERNATIVE END-/s', $full, $matches);
535 if (isset($matches[1]) &&
536 trim(strip_tags($matches[1])) != ''
546 * Strip leading, trailing, double spaces from string
547 * used for postal/greeting/addressee
549 * @param string $string
550 * Input string to be cleaned.
555 public static function stripSpaces($string) {
556 return (empty($string)) ?
$string : preg_replace("/\s{2,}/", " ", trim($string));
560 * clean the URL 'path' variable that we use
561 * to construct CiviCRM urls by removing characters from the path variable
563 * @param string $string
564 * The input string to be sanitized.
565 * @param array $search
566 * The characters to be sanitized.
567 * @param string $replace
568 * The character to replace it with.
571 * the sanitized string
573 public static function stripPathChars(
578 static $_searchChars = NULL;
579 static $_replaceChar = NULL;
581 if (empty($string)) {
585 if ($_searchChars == NULL) {
608 if ($search == NULL) {
609 $search = $_searchChars;
612 if ($replace == NULL) {
613 $replace = $_replaceChar;
616 return str_replace($search, $replace, $string);
620 * Use HTMLPurifier to clean up a text string and remove any potential
621 * xss attacks. This is primarily used in public facing pages which
622 * accept html as the input string
624 * @param string $string
628 * the cleaned up string
630 public static function purifyHTML($string) {
631 static $_filter = NULL;
633 $config = HTMLPurifier_Config
::createDefault();
634 $config->set('Core.Encoding', 'UTF-8');
635 $config->set('Attr.AllowedFrameTargets', ['_blank', '_self', '_parent', '_top']);
637 // Disable the cache entirely
638 $config->set('Cache.DefinitionImpl', NULL);
640 $_filter = new HTMLPurifier($config);
643 return $_filter->purify($string);
647 * Truncate $string; if $string exceeds $maxLen, place "..." at the end
649 * @param string $string
654 public static function ellipsify($string, $maxLen) {
655 if (mb_strlen($string, 'UTF-8') <= $maxLen) {
658 return mb_substr($string, 0, $maxLen - 3, 'UTF-8') . '...';
662 * Generate a random string.
668 public static function createRandom($len, $alphabet) {
669 $alphabetSize = strlen($alphabet);
671 for ($i = 0; $i < $len; $i++
) {
672 $result .= $alphabet[rand(1, $alphabetSize) - 1];
679 * "admin foo" => array(NULL,"admin foo")
680 * "cms:admin foo" => array("cms", "admin foo")
682 * @param string $delim
683 * @param string $string
684 * E.g. "view all contacts". Syntax: "[prefix:]name".
685 * @param string|null $defaultPrefix
688 * (0 => string|NULL $prefix, 1 => string $value)
690 public static function parsePrefix($delim, $string, $defaultPrefix = NULL) {
691 $pos = strpos($string, $delim);
692 if ($pos === FALSE) {
693 return [$defaultPrefix, $string];
696 return [substr($string, 0, $pos), substr($string, 1 +
$pos)];
701 * This function will mask part of the the user portion of an Email address (everything before the @)
703 * @param string $email
704 * The email address to be masked.
705 * @param string $maskChar
706 * The character used for masking.
707 * @param int $percent
708 * The percentage of the user portion to be masked.
711 * returns the masked Email address
713 public static function maskEmail($email, $maskChar = '*', $percent = 50) {
714 list($user, $domain) = preg_split("/@/", $email);
715 $len = strlen($user);
716 $maskCount = floor($len * $percent / 100);
717 $offset = floor(($len - $maskCount) / 2);
719 $masked = substr($user, 0, $offset)
720 . str_repeat($maskChar, $maskCount)
721 . substr($user, $maskCount +
$offset);
723 return ($masked . '@' . $domain);
727 * This function compares two strings.
729 * @param string $strOne
731 * @param string $strTwo
734 * Boolean indicating whether you want the comparison to be case sensitive or not.
737 * TRUE (string are identical); FALSE (strings are not identical)
739 public static function compareStr($strOne, $strTwo, $case) {
741 // Convert to lowercase and trim white spaces
742 if (strtolower(trim($strOne)) == strtolower(trim($strTwo))) {
743 // yes - they are identical
751 if ($case == FALSE) {
753 if (trim($strOne) == trim($strTwo)) {
754 // yes - they are identical
765 * Many parts of the codebase have a convention of internally passing around
766 * HTML-encoded URLs. This effectively means that "&" is replaced by "&"
767 * (because most other odd characters are %-escaped in URLs; and %-escaped
768 * strings don't need any extra escaping in HTML).
770 * @param string $htmlUrl
771 * URL with HTML entities.
773 * URL without HTML entities
775 public static function unstupifyUrl($htmlUrl) {
776 return str_replace('&', '&', $htmlUrl);
780 * When a user supplies a URL (e.g. to an image), we'd like to:
781 * - Remove the protocol and domain name if the URL points to the current
783 * - Keep the domain name for remote URLs.
784 * - Optionally, force remote URLs to use https instead of http (which is
788 * The URL to simplify. Examples:
789 * "https://example.org/sites/default/files/coffee-mug.jpg"
790 * "sites/default/files/coffee-mug.jpg"
791 * "http://i.stack.imgur.com/9jb2ial01b.png"
792 * @param bool $forceHttps = FALSE
793 * If TRUE, ensure that remote URLs use https. If a URL with
794 * http is supplied, then we'll change it to https.
795 * This is useful for situations like showing a premium product on a
796 * contribution, because (as reported in CRM-14283) if the user gets a
797 * browser warning like "page contains insecure elements" on a contribution
798 * page, that's a very bad thing. Thus, even if changing http to https
799 * breaks the image, that's better than leaving http content in a
803 * The simplified URL. Examples:
804 * "/sites/default/files/coffee-mug.jpg"
805 * "https://i.stack.imgur.com/9jb2ial01b.png"
807 public static function simplifyURL($url, $forceHttps = FALSE) {
808 $config = CRM_Core_Config
::singleton();
809 $siteURLParts = self
::simpleParseUrl($config->userFrameworkBaseURL
);
810 $urlParts = self
::simpleParseUrl($url);
812 // If the image is locally hosted, then only give the path to the image
814 = ($urlParts['host+port'] == '')
815 |
($urlParts['host+port'] == $siteURLParts['host+port']);
817 // and make sure it begins with one forward slash
818 return preg_replace('_^/*(?=.)_', '/', $urlParts['path+query']);
821 // If the URL is external, then keep the full URL as supplied
823 return $forceHttps ?
preg_replace('_^http://_', 'https://', $url) : $url;
828 * A simplified version of PHP's parse_url() function.
831 * e.g. "https://example.com:8000/foo/bar/?id=1#fragment"
834 * Will always contain keys 'host+port' and 'path+query', even if they're
835 * empty strings. Example:
837 * 'host+port' => "example.com:8000",
838 * 'path+query' => "/foo/bar/?id=1",
841 public static function simpleParseUrl($url) {
842 $parts = parse_url($url);
843 $host = $parts['host'] ??
'';
844 $port = isset($parts['port']) ?
':' . $parts['port'] : '';
845 $path = $parts['path'] ??
'';
846 $query = isset($parts['query']) ?
'?' . $parts['query'] : '';
848 'host+port' => "$host$port",
849 'path+query' => "$path$query",
854 * Formats a string of attributes for insertion in an html tag.
856 * @param array $attributes
860 public static function htmlAttributes($attributes) {
862 foreach ($attributes as $name => $vals) {
863 $output .= " $name=\"" . htmlspecialchars(implode(' ', (array) $vals)) . '"';
865 return ltrim($output);
869 * Determine if $string starts with $fragment.
871 * @param string $string
873 * @param string $fragment
874 * The fragment to look for.
877 public static function startsWith($string, $fragment) {
878 if ($fragment === '') {
881 $len = strlen($fragment);
882 return substr($string, 0, $len) === $fragment;
886 * Determine if $string ends with $fragment.
888 * @param string $string
890 * @param string $fragment
891 * The fragment to look for.
894 public static function endsWith($string, $fragment) {
895 if ($fragment === '') {
898 $len = strlen($fragment);
899 return substr($string, -1 * $len) === $fragment;
903 * @param string|array $patterns
904 * @param array $allStrings
905 * @param bool $allowNew
906 * Whether to return new, unrecognized names.
909 public static function filterByWildcards($patterns, $allStrings, $allowNew = FALSE) {
910 $patterns = (array) $patterns;
912 foreach ($patterns as $pattern) {
913 if (!\CRM_Utils_String
::endsWith($pattern, '*')) {
914 if ($allowNew ||
in_array($pattern, $allStrings)) {
915 $result[] = $pattern;
919 $prefix = rtrim($pattern, '*');
920 foreach ($allStrings as $key) {
921 if (\CRM_Utils_String
::startsWith($key, $prefix)) {
927 return array_values(array_unique($result));
931 * Safely unserialize a string of scalar or array values (but not objects!)
933 * Use `xkerman/restricted-unserialize` to unserialize strings using PHP's
934 * serialization format. `restricted-unserialize` works like PHP's built-in
935 * `unserialize` function except that it does not deserialize object instances,
936 * making it immune to PHP Object Injection {@see https://www.owasp.org/index.php/PHP_Object_Injection}
939 * Note: When dealing with user inputs, it is generally recommended to use
940 * safe, standard data interchange formats such as JSON rather than PHP's
941 * serialization format when dealing with user input.
943 * @param string|null $string
947 public static function unserialize($string) {
948 if (!is_string($string)) {
952 return unserialize($string);
954 catch (UnserializeFailedException
$e) {
960 * Returns the plural form of an English word.
965 public static function pluralize($str) {
966 $lastLetter = substr($str, -1);
967 $lastTwo = substr($str, -2);
968 if ($lastLetter == 's' ||
$lastLetter == 'x' ||
$lastTwo == 'ch') {
971 if ($lastLetter == 'y' && !in_array($lastTwo, ['ay', 'ey', 'iy', 'oy', 'uy'])) {
972 return substr($str, 0, -1) . 'ies';
978 * Generic check as to whether any tokens are in the given string.
980 * It might be a smarty token OR a CiviCRM token. In both cases the
981 * absence of a '{' indicates no token is present.
983 * @param string $string
987 public static function stringContainsTokens(string $string) {
988 return strpos($string, '{') !== FALSE;
992 * Parse a string through smarty without creating a smarty template file per string.
994 * This function is for swapping out any smarty tokens that appear in a string
995 * and are not re-used much if at all. For example parsing a contact's greeting
996 * does not need to be cached are there are some minor security / data privacy benefits
997 * to not caching them per file. We also save disk space, reduce I/O and disk clearing time.
999 * Doing this is cleaning in Smarty3 which we are alas not using
1000 * https://www.smarty.net/docs/en/resources.string.tpl
1002 * However, it highlights that smarty-eval is not evil-eval and still have the security applied.
1004 * In order to replicate that in Smarty2 I'm using {eval} per
1005 * https://www.smarty.net/docsv2/en/language.function.eval.tpl#id2820446
1007 * - Evaluated variables are treated the same as templates. They follow the same escapement and security features just as if they were templates.
1008 * - Evaluated variables are compiled on every invocation, the compiled versions are not saved! However if you have caching enabled, the output
1009 * will be cached with the rest of the template.
1011 * Our set up does not have caching enabled and my testing suggests this still works fine with it
1012 * enabled so turning it off before running this is out of caution based on the above.
1014 * When this function is run only one template file is created (for the eval) tag no matter how
1015 * many times it is run. This compares to it otherwise creating one file for every parsed string.
1017 * @param string $templateString
1021 public static function parseOneOffStringThroughSmarty($templateString) {
1022 if (!CRM_Utils_String
::stringContainsTokens($templateString)) {
1023 // Skip expensive smarty processing.
1024 return $templateString;
1026 $smarty = CRM_Core_Smarty
::singleton();
1027 $cachingValue = $smarty->caching
;
1028 $smarty->caching
= 0;
1029 $smarty->assign('smartySingleUseString', $templateString);
1030 // Do not escape the smartySingleUseString as that is our smarty template
1031 // and is likely to contain html.
1032 $templateString = (string) $smarty->fetch('string:{eval var=$smartySingleUseString|smarty:nodefaults}');
1033 $smarty->caching
= $cachingValue;
1034 $smarty->assign('smartySingleUseString', NULL);
1035 return $templateString;