3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
36 require_once 'HTML/QuickForm/Rule/Email.php';
39 * This class contains string functions
42 class CRM_Utils_String
{
43 const COMMA
= ",", SEMICOLON
= ";", SPACE
= " ", TAB
= "\t", LINEFEED
= "\n", CARRIAGELINE
= "\r\n", LINECARRIAGE
= "\n\r", CARRIAGERETURN
= "\r";
46 * List of all letters and numbers
48 const ALPHANUMERIC
= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
51 * Convert a display name into a potential variable
52 * name that we could use in forms/code
54 * @param name Name of the string
56 * @param int $maxLength
59 * An equivalent variable name.
61 public static function titleToVar($title, $maxLength = 31) {
62 $variable = self
::munge($title, '_', $maxLength);
64 if (CRM_Utils_Rule
::title($variable, $maxLength)) {
68 // if longer than the maxLength lets just return a substr of the
69 // md5 to prevent errors downstream
70 return substr(md5($title), 0, $maxLength);
74 * Given a string, replace all non alpha numeric characters and
75 * spaces with the replacement character
78 * The name to be worked on.
80 * The character to use for non-valid chars.
82 * Length of valid variables.
86 * returns the manipulated string
88 public static function munge($name, $char = '_', $len = 63) {
89 // replace all white space and non-alpha numeric with $char
90 // we only use the ascii character set since mysql does not create table names / field names otherwise
92 $name = preg_replace('/[^a-zA-Z0-9]+/', $char, trim($name));
95 // lets keep variable names short
96 return substr($name, 0, $len);
104 * Convert possibly underscore separated words to camel case with special handling for 'UF'
106 * membership_payment returns MembershipPayment
107 * @param string $string
112 public static function convertStringToCamel($string) {
113 $fragments = explode('_', $string);
114 foreach ($fragments as & $fragment) {
115 $fragment = ucfirst($fragment);
117 // Special case: UFGroup, UFJoin, UFMatch, UFField
118 if ($fragments[0] === 'Uf') {
119 $fragments[0] = 'UF';
121 return implode('', $fragments);
126 * Takes a variable name and munges it randomly into another variable name
128 * @param string $name
129 * Initial Variable Name.
131 * Length of valid variables.
134 * Randomized Variable Name
136 public static function rename($name, $len = 4) {
137 $rand = substr(uniqid(), 0, $len);
138 return substr_replace($name, $rand, -$len, $len);
142 * Takes a string and returns the last tuple of the string.
143 * useful while converting file names to class names etc
145 * @param string $string
147 * @param \char|string $char $char the character used to demarcate the componets
153 public static function getClassName($string, $char = '_') {
155 if (!is_array($string)) {
156 $names = explode($char, $string);
158 if (!empty($names)) {
159 return array_pop($names);
164 * Appends a name to a string and seperated by delimiter.
165 * does the right thing for an empty string
168 * The string to be appended to.
169 * @param string $delim
170 * The delimiter to use.
172 * The string (or array of strings) to append.
176 public static function append(&$str, $delim, $name) {
181 if (is_array($name)) {
182 foreach ($name as $n) {
199 $str .= $delim . $name;
205 * Determine if the string is composed only of ascii characters
210 * Attempt utf8 match on failure (default yes).
213 * true if string is ascii
215 public static function isAscii($str, $utf8 = TRUE) {
216 if (!function_exists('mb_detect_encoding')) {
217 // eliminate all white space from the string
218 $str = preg_replace('/\s+/', '', $str);
219 // FIXME: This is a pretty brutal hack to make utf8 and 8859-1 work.
221 /* match low- or high-ascii characters */
222 if (preg_match('/[\x00-\x20]|[\x7F-\xFF]/', $str)) {
223 // || // low ascii characters
224 // high ascii characters
225 // preg_match( '/[\x7F-\xFF]/', $str ) ) {
227 /* if we did match, try for utf-8, or iso8859-1 */
229 return self
::isUtf8($str);
238 $order = array('ASCII');
242 $enc = mb_detect_encoding($str, $order, TRUE);
243 return ($enc == 'ASCII' ||
$enc == 'UTF-8');
248 * Determine the string replacements for redaction
249 * on the basis of the regular expressions
253 * @param array $regexRules
254 * Regular expression to be matched w/ replacements.
257 * array of strings w/ corresponding redacted outputs
259 public static function regex($str, $regexRules) {
260 //redact the regular expressions
261 if (!empty($regexRules) && isset($str)) {
262 static $matches, $totalMatches, $match = array();
263 foreach ($regexRules as $pattern => $replacement) {
264 preg_match_all($pattern, $str, $matches);
265 if (!empty($matches[0])) {
266 if (empty($totalMatches)) {
267 $totalMatches = $matches[0];
270 $totalMatches = array_merge($totalMatches, $matches[0]);
272 $match = array_flip($totalMatches);
277 if (!empty($match)) {
278 foreach ($match as $matchKey => & $dontCare) {
279 foreach ($regexRules as $pattern => $replacement) {
280 if (preg_match($pattern, $matchKey)) {
281 $dontCare = $replacement . substr(md5($matchKey), 0, 5);
288 return CRM_Core_DAO
::$_nullArray;
293 * @param $stringRules
297 public static function redaction($str, $stringRules) {
299 if (!empty($stringRules)) {
300 foreach ($stringRules as $match => $replace) {
301 $str = str_ireplace($match, $replace, $str);
305 //return the redacted output
310 * Determine if a string is composed only of utf8 characters
317 public static function isUtf8($str) {
318 if (!function_exists(mb_detect_encoding
)) {
319 // eliminate all white space from the string
320 $str = preg_replace('/\s+/', '', $str);
322 /* pattern stolen from the php.net function documentation for
324 * comment by JF Sebastian, 30-Mar-2005
327 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);
329 // iconv('ISO-8859-1', 'UTF-8', $str);
332 $enc = mb_detect_encoding($str, array('UTF-8'), TRUE);
333 return ($enc !== FALSE);
338 * Determine if two href's are equivalent (fuzzy match)
340 * @param string $url1
341 * The first url to be matched.
342 * @param string $url2
343 * The second url to be matched against.
346 * true if the urls match, else false
348 public static function match($url1, $url2) {
349 $url1 = strtolower($url1);
350 $url2 = strtolower($url2);
352 $url1Str = parse_url($url1);
353 $url2Str = parse_url($url2);
355 if ($url1Str['path'] == $url2Str['path'] &&
356 self
::extractURLVarValue(CRM_Utils_Array
::value('query', $url1Str)) == self
::extractURLVarValue(CRM_Utils_Array
::value('query', $url2Str))
364 * Extract variable values
367 * This is basically url.
370 * $v returns civicrm url (eg: civicrm/contact/search/...)
372 public static function extractURLVarValue($query) {
373 $config = CRM_Core_Config
::singleton();
374 $urlVar = $config->userFrameworkURLVar
;
376 $params = explode('&', $query);
377 foreach ($params as $p) {
378 if (strpos($p, '=')) {
379 list($k, $v) = explode('=', $p);
389 * Translate a true/false/yes/no string to a 0 or 1 value
392 * The string to be translated.
396 public static function strtobool($str) {
397 if (!is_scalar($str)) {
401 if (preg_match('/^(y(es)?|t(rue)?|1)$/i', $str)) {
408 * Returns string '1' for a true/yes/1 string, and '0' for no/false/0 else returns false
411 * The string to be translated.
415 public static function strtoboolstr($str) {
416 if (!is_scalar($str)) {
420 if (preg_match('/^(y(es)?|t(rue)?|1)$/i', $str)) {
423 elseif (preg_match('/^(n(o)?|f(alse)?|0)$/i', $str)) {
432 * Convert a HTML string into a text one using html2text
434 * @param string $html
435 * The string to be converted.
438 * the converted string
440 public static function htmlToText($html) {
441 require_once 'packages/html2text/rcube_html2text.php';
442 $token_html = preg_replace('!\{([a-z_.]+)\}!i', 'token:{$1}', $html);
443 $converter = new rcube_html2text($token_html);
444 $token_text = $converter->get_text();
445 $text = preg_replace('!token\:\{([a-z_.]+)\}!i', '{$1}', $token_text);
451 * @param array $params
453 public static function extractName($string, &$params) {
454 $name = trim($string);
460 $name = str_replace('"', '', $name);
461 $name = str_replace('\'', '', $name);
463 // check for comma in name
464 if (strpos($name, ',') !== FALSE) {
466 // name has a comma - assume lname, fname [mname]
467 $names = explode(',', $name);
468 if (count($names) > 1) {
469 $params['last_name'] = trim($names[0]);
471 // check for space delim
472 $fnames = explode(' ', trim($names[1]));
473 if (count($fnames) > 1) {
474 $params['first_name'] = trim($fnames[0]);
475 $params['middle_name'] = trim($fnames[1]);
478 $params['first_name'] = trim($fnames[0]);
482 $params['first_name'] = trim($names[0]);
486 // name has no comma - assume fname [mname] fname
487 $names = explode(' ', $name);
488 if (count($names) == 1) {
489 $params['first_name'] = $names[0];
491 elseif (count($names) == 2) {
492 $params['first_name'] = $names[0];
493 $params['last_name'] = $names[1];
496 $params['first_name'] = $names[0];
497 $params['middle_name'] = $names[1];
498 $params['last_name'] = $names[2];
508 public static function &makeArray($string) {
509 $string = trim($string);
511 $values = explode("\n", $string);
513 foreach ($values as $value) {
514 list($n, $v) = CRM_Utils_System
::explode('=', $value, 2);
516 $result[trim($n)] = trim($v);
523 * Given an ezComponents-parsed representation of
524 * a text with alternatives return only the first one
526 * @param string $full
527 * All alternatives as a long string (or some other text).
530 * only the first alternative found (or the text without alternatives)
532 public static function stripAlternatives($full) {
534 preg_match('/-ALTERNATIVE ITEM 0-(.*?)-ALTERNATIVE ITEM 1-.*-ALTERNATIVE END-/s', $full, $matches);
536 if (isset($matches[1]) &&
537 trim(strip_tags($matches[1])) != ''
547 * Strip leading, trailing, double spaces from string
548 * used for postal/greeting/addressee
550 * @param string $string
551 * Input string to be cleaned.
556 public static function stripSpaces($string) {
557 return (empty($string)) ?
$string : preg_replace("/\s{2,}/", " ", trim($string));
561 * clean the URL 'path' variable that we use
562 * to construct CiviCRM urls by removing characters from the path variable
564 * @param string $string
565 * The input string to be sanitized.
566 * @param array $search
567 * The characters to be sanitized.
568 * @param string $replace
569 * The character to replace it with.
572 * the sanitized string
574 static function stripPathChars(
579 static $_searchChars = NULL;
580 static $_replaceChar = NULL;
582 if (empty($string)) {
586 if ($_searchChars == NULL) {
587 $_searchChars = array(
609 if ($search == NULL) {
610 $search = $_searchChars;
613 if ($replace == NULL) {
614 $replace = $_replaceChar;
617 return str_replace($search, $replace, $string);
622 * Use HTMLPurifier to clean up a text string and remove any potential
623 * xss attacks. This is primarily used in public facing pages which
624 * accept html as the input string
626 * @param string $string
630 * the cleaned up string
632 public static function purifyHTML($string) {
633 static $_filter = NULL;
635 $config = HTMLPurifier_Config
::createDefault();
636 $config->set('Core.Encoding', 'UTF-8');
638 // Disable the cache entirely
639 $config->set('Cache.DefinitionImpl', NULL);
641 $_filter = new HTMLPurifier($config);
644 return $_filter->purify($string);
648 * Truncate $string; if $string exceeds $maxLen, place "..." at the end
650 * @param string $string
655 public static function ellipsify($string, $maxLen) {
656 $len = strlen($string);
657 if ($len <= $maxLen) {
661 return substr($string, 0, $maxLen - 3) . '...';
666 * Generate a random string
672 public static function createRandom($len, $alphabet) {
673 $alphabetSize = strlen($alphabet);
675 for ($i = 0; $i < $len; $i++
) {
676 $result .= $alphabet{rand(1, $alphabetSize) - 1};
683 * "admin foo" => array(NULL,"admin foo")
684 * "cms:admin foo" => array("cms", "admin foo")
687 * @param string $string
688 * E.g. "view all contacts". Syntax: "[prefix:]name".
689 * @param null $defaultPrefix
692 * (0 => string|NULL $prefix, 1 => string $value)
694 public static function parsePrefix($delim, $string, $defaultPrefix = NULL) {
695 $pos = strpos($string, $delim);
696 if ($pos === FALSE) {
697 return array($defaultPrefix, $string);
700 return array(substr($string, 0, $pos), substr($string, 1 +
$pos));
705 * This function will mask part of the the user portion of an Email address (everything before the @)
707 * @param string $email
708 * The email address to be masked.
709 * @param string $maskChar
710 * The character used for masking.
711 * @param int $percent
712 * The percentage of the user portion to be masked.
715 * returns the masked Email address
717 public static function maskEmail($email, $maskChar = '*', $percent = 50) {
718 list($user, $domain) = preg_split("/@/", $email);
719 $len = strlen($user);
720 $maskCount = floor($len * $percent / 100);
721 $offset = floor(($len - $maskCount) / 2);
723 $masked = substr($user, 0, $offset)
724 . str_repeat($maskChar, $maskCount)
725 . substr($user, $maskCount +
$offset);
727 return ($masked . '@' . $domain);
731 * This function compares two strings
733 * @param string $strOne
735 * @param string $strTwo
738 * Boolean indicating whether you want the comparison to be case sensitive or not.
741 * TRUE (string are identical); FALSE (strings are not identical)
743 public static function compareStr($strOne, $strTwo, $case) {
745 // Convert to lowercase and trim white spaces
746 if (strtolower(trim($strOne)) == strtolower(trim($strTwo))) {
747 // yes - they are identical
755 if ($case == FALSE) {
757 if (trim($strOne) == trim($strTwo)) {
758 // yes - they are identical
769 * Many parts of the codebase have a convention of internally passing around
770 * HTML-encoded URLs. This effectively means that "&" is replaced by "&"
771 * (because most other odd characters are %-escaped in URLs; and %-escaped
772 * strings don't need any extra escaping in HTML).
775 * URL with HTML entities.
777 * URL without HTML entities
779 public static function unstupifyUrl($htmlUrl) {
780 return str_replace('&', '&', $htmlUrl);