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.
62 public static function titleToVar($title, $maxLength = 31) {
63 $variable = self
::munge($title, '_', $maxLength);
65 if (CRM_Utils_Rule
::title($variable, $maxLength)) {
69 // if longer than the maxLength lets just return a substr of the
70 // md5 to prevent errors downstream
71 return substr(md5($title), 0, $maxLength);
75 * Given a string, replace all non alpha numeric characters and
76 * spaces with the replacement character
79 * The name to be worked on.
81 * The character to use for non-valid chars.
83 * Length of valid variables.
87 * returns the manipulated string
90 public static function munge($name, $char = '_', $len = 63) {
91 // replace all white space and non-alpha numeric with $char
92 // we only use the ascii character set since mysql does not create table names / field names otherwise
94 $name = preg_replace('/[^a-zA-Z0-9]+/', $char, trim($name));
97 // lets keep variable names short
98 return substr($name, 0, $len);
106 * Convert possibly underscore separated words to camel case with special handling for 'UF'
108 * membership_payment returns MembershipPayment
109 * @param string $string
114 public static function convertStringToCamel($string) {
115 $fragments = explode('_', $string);
116 foreach ($fragments as & $fragment) {
117 $fragment = ucfirst($fragment);
119 // Special case: UFGroup, UFJoin, UFMatch, UFField
120 if ($fragments[0] === 'Uf') {
121 $fragments[0] = 'UF';
123 return implode('', $fragments);
128 * Takes a variable name and munges it randomly into another variable name
130 * @param string $name
131 * Initial Variable Name.
133 * Length of valid variables.
136 * Randomized Variable Name
139 public static function rename($name, $len = 4) {
140 $rand = substr(uniqid(), 0, $len);
141 return substr_replace($name, $rand, -$len, $len);
145 * Takes a string and returns the last tuple of the string.
146 * useful while converting file names to class names etc
148 * @param string $string
150 * @param \char|string $char $char the character used to demarcate the componets
157 public static function getClassName($string, $char = '_') {
159 if (!is_array($string)) {
160 $names = explode($char, $string);
162 if (!empty($names)) {
163 return array_pop($names);
168 * Appends a name to a string and seperated by delimiter.
169 * does the right thing for an empty string
172 * The string to be appended to.
173 * @param string $delim
174 * The delimiter to use.
176 * The string (or array of strings) to append.
181 public static function append(&$str, $delim, $name) {
186 if (is_array($name)) {
187 foreach ($name as $n) {
204 $str .= $delim . $name;
210 * Determine if the string is composed only of ascii characters
215 * Attempt utf8 match on failure (default yes).
218 * true if string is ascii
221 public static function isAscii($str, $utf8 = TRUE) {
222 if (!function_exists('mb_detect_encoding')) {
223 // eliminate all white space from the string
224 $str = preg_replace('/\s+/', '', $str);
225 // FIXME: This is a pretty brutal hack to make utf8 and 8859-1 work.
227 /* match low- or high-ascii characters */
228 if (preg_match('/[\x00-\x20]|[\x7F-\xFF]/', $str)) {
229 // || // low ascii characters
230 // high ascii characters
231 // preg_match( '/[\x7F-\xFF]/', $str ) ) {
233 /* if we did match, try for utf-8, or iso8859-1 */
235 return self
::isUtf8($str);
244 $order = array('ASCII');
248 $enc = mb_detect_encoding($str, $order, TRUE);
249 return ($enc == 'ASCII' ||
$enc == 'UTF-8');
254 * Determine the string replacements for redaction
255 * on the basis of the regular expressions
259 * @param array $regexRules
260 * Regular expression to be matched w/ replacements.
263 * array of strings w/ corresponding redacted outputs
266 public static function regex($str, $regexRules) {
267 //redact the regular expressions
268 if (!empty($regexRules) && isset($str)) {
269 static $matches, $totalMatches, $match = array();
270 foreach ($regexRules as $pattern => $replacement) {
271 preg_match_all($pattern, $str, $matches);
272 if (!empty($matches[0])) {
273 if (empty($totalMatches)) {
274 $totalMatches = $matches[0];
277 $totalMatches = array_merge($totalMatches, $matches[0]);
279 $match = array_flip($totalMatches);
284 if (!empty($match)) {
285 foreach ($match as $matchKey => & $dontCare) {
286 foreach ($regexRules as $pattern => $replacement) {
287 if (preg_match($pattern, $matchKey)) {
288 $dontCare = $replacement . substr(md5($matchKey), 0, 5);
295 return CRM_Core_DAO
::$_nullArray;
300 * @param $stringRules
304 public static function redaction($str, $stringRules) {
306 if (!empty($stringRules)) {
307 foreach ($stringRules as $match => $replace) {
308 $str = str_ireplace($match, $replace, $str);
312 //return the redacted output
317 * Determine if a string is composed only of utf8 characters
325 public static function isUtf8($str) {
326 if (!function_exists(mb_detect_encoding
)) {
327 // eliminate all white space from the string
328 $str = preg_replace('/\s+/', '', $str);
330 /* pattern stolen from the php.net function documentation for
332 * comment by JF Sebastian, 30-Mar-2005
335 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);
337 // iconv('ISO-8859-1', 'UTF-8', $str);
340 $enc = mb_detect_encoding($str, array('UTF-8'), TRUE);
341 return ($enc !== FALSE);
346 * Determine if two href's are equivalent (fuzzy match)
348 * @param string $url1
349 * The first url to be matched.
350 * @param string $url2
351 * The second url to be matched against.
354 * true if the urls match, else false
357 public static function match($url1, $url2) {
358 $url1 = strtolower($url1);
359 $url2 = strtolower($url2);
361 $url1Str = parse_url($url1);
362 $url2Str = parse_url($url2);
364 if ($url1Str['path'] == $url2Str['path'] &&
365 self
::extractURLVarValue(CRM_Utils_Array
::value('query', $url1Str)) == self
::extractURLVarValue(CRM_Utils_Array
::value('query', $url2Str))
373 * Extract variable values
376 * This is basically url.
378 * @return mix $v returns civicrm url (eg: civicrm/contact/search/...)
381 public static function extractURLVarValue($query) {
382 $config = CRM_Core_Config
::singleton();
383 $urlVar = $config->userFrameworkURLVar
;
385 $params = explode('&', $query);
386 foreach ($params as $p) {
387 if (strpos($p, '=')) {
388 list($k, $v) = explode('=', $p);
398 * Translate a true/false/yes/no string to a 0 or 1 value
401 * The string to be translated.
406 public static function strtobool($str) {
407 if (!is_scalar($str)) {
411 if (preg_match('/^(y(es)?|t(rue)?|1)$/i', $str)) {
418 * Returns string '1' for a true/yes/1 string, and '0' for no/false/0 else returns false
421 * The string to be translated.
426 public static function strtoboolstr($str) {
427 if (!is_scalar($str)) {
431 if (preg_match('/^(y(es)?|t(rue)?|1)$/i', $str)) {
434 elseif (preg_match('/^(n(o)?|f(alse)?|0)$/i', $str)) {
443 * Convert a HTML string into a text one using html2text
445 * @param string $html
446 * The string to be converted.
449 * the converted string
452 public static function htmlToText($html) {
453 require_once 'packages/html2text/rcube_html2text.php';
454 $token_html = preg_replace('!\{([a-z_.]+)\}!i', 'token:{$1}', $html);
455 $converter = new rcube_html2text($token_html);
456 $token_text = $converter->get_text();
457 $text = preg_replace('!token\:\{([a-z_.]+)\}!i', '{$1}', $token_text);
463 * @param array $params
465 public static function extractName($string, &$params) {
466 $name = trim($string);
472 $name = str_replace('"', '', $name);
473 $name = str_replace('\'', '', $name);
475 // check for comma in name
476 if (strpos($name, ',') !== FALSE) {
478 // name has a comma - assume lname, fname [mname]
479 $names = explode(',', $name);
480 if (count($names) > 1) {
481 $params['last_name'] = trim($names[0]);
483 // check for space delim
484 $fnames = explode(' ', trim($names[1]));
485 if (count($fnames) > 1) {
486 $params['first_name'] = trim($fnames[0]);
487 $params['middle_name'] = trim($fnames[1]);
490 $params['first_name'] = trim($fnames[0]);
494 $params['first_name'] = trim($names[0]);
498 // name has no comma - assume fname [mname] fname
499 $names = explode(' ', $name);
500 if (count($names) == 1) {
501 $params['first_name'] = $names[0];
503 elseif (count($names) == 2) {
504 $params['first_name'] = $names[0];
505 $params['last_name'] = $names[1];
508 $params['first_name'] = $names[0];
509 $params['middle_name'] = $names[1];
510 $params['last_name'] = $names[2];
520 public static function &makeArray($string) {
521 $string = trim($string);
523 $values = explode("\n", $string);
525 foreach ($values as $value) {
526 list($n, $v) = CRM_Utils_System
::explode('=', $value, 2);
528 $result[trim($n)] = trim($v);
535 * Given an ezComponents-parsed representation of
536 * a text with alternatives return only the first one
538 * @param string $full
539 * All alternatives as a long string (or some other text).
542 * only the first alternative found (or the text without alternatives)
544 public static function stripAlternatives($full) {
546 preg_match('/-ALTERNATIVE ITEM 0-(.*?)-ALTERNATIVE ITEM 1-.*-ALTERNATIVE END-/s', $full, $matches);
548 if (isset($matches[1]) &&
549 trim(strip_tags($matches[1])) != ''
559 * Strip leading, trailing, double spaces from string
560 * used for postal/greeting/addressee
562 * @param string $string
563 * Input string to be cleaned.
569 public static function stripSpaces($string) {
570 return (empty($string)) ?
$string : preg_replace("/\s{2,}/", " ", trim($string));
574 * clean the URL 'path' variable that we use
575 * to construct CiviCRM urls by removing characters from the path variable
577 * @param string $string
578 * The input string to be sanitized.
579 * @param array $search
580 * The characters to be sanitized.
581 * @param string $replace
582 * The character to replace it with.
585 * the sanitized string
588 static function stripPathChars(
593 static $_searchChars = NULL;
594 static $_replaceChar = NULL;
596 if (empty($string)) {
600 if ($_searchChars == NULL) {
601 $_searchChars = array(
602 '&', ';', ',', '=', '$',
605 ' ', "\r", "\r\n", "\n", "\t",
610 if ($search == NULL) {
611 $search = $_searchChars;
614 if ($replace == NULL) {
615 $replace = $_replaceChar;
618 return str_replace($search, $replace, $string);
623 * Use HTMLPurifier to clean up a text string and remove any potential
624 * xss attacks. This is primarily used in public facing pages which
625 * accept html as the input string
627 * @param string $string
631 * the cleaned up string
634 public static function purifyHTML($string) {
635 static $_filter = NULL;
637 $config = HTMLPurifier_Config
::createDefault();
638 $config->set('Core.Encoding', 'UTF-8');
640 // Disable the cache entirely
641 $config->set('Cache.DefinitionImpl', NULL);
643 $_filter = new HTMLPurifier($config);
646 return $_filter->purify($string);
650 * Truncate $string; if $string exceeds $maxLen, place "..." at the end
652 * @param string $string
657 public static function ellipsify($string, $maxLen) {
658 $len = strlen($string);
659 if ($len <= $maxLen) {
663 return substr($string, 0, $maxLen - 3) . '...';
668 * Generate a random string
674 public static function createRandom($len, $alphabet) {
675 $alphabetSize = strlen($alphabet);
677 for ($i = 0; $i < $len; $i++
) {
678 $result .= $alphabet{rand(1, $alphabetSize) - 1};
685 * "admin foo" => array(NULL,"admin foo")
686 * "cms:admin foo" => array("cms", "admin foo")
689 * @param string $string
690 * E.g. "view all contacts". Syntax: "[prefix:]name".
691 * @param null $defaultPrefix
694 * (0 => string|NULL $prefix, 1 => string $value)
696 public static function parsePrefix($delim, $string, $defaultPrefix = NULL) {
697 $pos = strpos($string, $delim);
698 if ($pos === FALSE) {
699 return array($defaultPrefix, $string);
702 return array(substr($string, 0, $pos), substr($string, 1 +
$pos));
707 * This function will mask part of the the user portion of an Email address (everything before the @)
709 * @param string $email
710 * The email address to be masked.
711 * @param string $maskChar
712 * The character used for masking.
713 * @param int $percent
714 * The percentage of the user portion to be masked.
717 * returns the masked Email address
719 public static function maskEmail($email, $maskChar = '*', $percent = 50) {
720 list($user, $domain) = preg_split("/@/", $email);
721 $len = strlen($user);
722 $maskCount = floor($len * $percent / 100);
723 $offset = floor(($len - $maskCount) / 2);
725 $masked = substr($user, 0, $offset)
726 . str_repeat($maskChar, $maskCount)
727 . substr($user, $maskCount +
$offset);
729 return ($masked . '@' . $domain);
733 * This function compares two strings
735 * @param string $strOne
737 * @param string $strTwo
740 * Boolean indicating whether you want the comparison to be case sensitive or not.
743 * TRUE (string are identical); FALSE (strings are not identical)
745 public static function compareStr($strOne, $strTwo, $case) {
747 // Convert to lowercase and trim white spaces
748 if (strtolower(trim($strOne)) == strtolower(trim($strTwo))) {
749 // yes - they are identical
757 if ($case == FALSE) {
759 if (trim($strOne) == trim($strTwo)) {
760 // yes - they are identical
771 * Many parts of the codebase have a convention of internally passing around
772 * HTML-encoded URLs. This effectively means that "&" is replaced by "&"
773 * (because most other odd characters are %-escaped in URLs; and %-escaped
774 * strings don't need any extra escaping in HTML).
777 * URL with HTML entities.
779 * URL without HTML entities
781 public static function unstupifyUrl($htmlUrl) {
782 return str_replace('&', '&', $htmlUrl);