a3606c2558ff92f1eb0def141467594a39ccbe75
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 $title title of the string
55 * @param int $maxLength
58 * An equivalent variable name.
60 public static function titleToVar($title, $maxLength = 31) {
61 $variable = self
::munge($title, '_', $maxLength);
63 if (CRM_Utils_Rule
::title($variable, $maxLength)) {
67 // if longer than the maxLength lets just return a substr of the
68 // md5 to prevent errors downstream
69 return substr(md5($title), 0, $maxLength);
73 * Given a string, replace all non alpha numeric characters and
74 * spaces with the replacement character
77 * The name to be worked on.
79 * The character to use for non-valid chars.
81 * Length of valid variables.
85 * returns the manipulated string
87 public static function munge($name, $char = '_', $len = 63) {
88 // replace all white space and non-alpha numeric with $char
89 // we only use the ascii character set since mysql does not create table names / field names otherwise
91 $name = preg_replace('/[^a-zA-Z0-9]+/', $char, trim($name));
94 // lets keep variable names short
95 return substr($name, 0, $len);
103 * Convert possibly underscore separated words to camel case with special handling for 'UF'
104 * e.g membership_payment returns MembershipPayment
106 * @param string $string
110 public static function convertStringToCamel($string) {
111 $fragments = explode('_', $string);
112 foreach ($fragments as & $fragment) {
113 $fragment = ucfirst($fragment);
115 // Special case: UFGroup, UFJoin, UFMatch, UFField
116 if ($fragments[0] === 'Uf') {
117 $fragments[0] = 'UF';
119 return implode('', $fragments);
123 * Takes a variable name and munges it randomly into another variable name
125 * @param string $name
126 * Initial Variable Name.
128 * Length of valid variables.
131 * Randomized Variable Name
133 public static function rename($name, $len = 4) {
134 $rand = substr(uniqid(), 0, $len);
135 return substr_replace($name, $rand, -$len, $len);
139 * Takes a string and returns the last tuple of the string.
140 * useful while converting file names to class names etc
142 * @param string $string
144 * @param string $char
145 * Character used to demarcate the components
150 public static function getClassName($string, $char = '_') {
152 if (!is_array($string)) {
153 $names = explode($char, $string);
155 if (!empty($names)) {
156 return array_pop($names);
161 * Appends a name to a string and separated by delimiter.
162 * does the right thing for an empty string
165 * The string to be appended to.
166 * @param string $delim
167 * The delimiter to use.
169 * The string (or array of strings) to append.
173 public static function append(&$str, $delim, $name) {
178 if (is_array($name)) {
179 foreach ($name as $n) {
196 $str .= $delim . $name;
202 * Determine if the string is composed only of ascii characters
207 * Attempt utf8 match on failure (default yes).
210 * true if string is ascii
212 public static function isAscii($str, $utf8 = TRUE) {
213 if (!function_exists('mb_detect_encoding')) {
214 // eliminate all white space from the string
215 $str = preg_replace('/\s+/', '', $str);
216 // FIXME: This is a pretty brutal hack to make utf8 and 8859-1 work.
218 /* match low- or high-ascii characters */
219 if (preg_match('/[\x00-\x20]|[\x7F-\xFF]/', $str)) {
220 // || // low ascii characters
221 // high ascii characters
222 // preg_match( '/[\x7F-\xFF]/', $str ) ) {
224 /* if we did match, try for utf-8, or iso8859-1 */
226 return self
::isUtf8($str);
235 $order = array('ASCII');
239 $enc = mb_detect_encoding($str, $order, TRUE);
240 return ($enc == 'ASCII' ||
$enc == 'UTF-8');
245 * Determine the string replacements for redaction
246 * on the basis of the regular expressions
250 * @param array $regexRules
251 * Regular expression to be matched w/ replacements.
254 * array of strings w/ corresponding redacted outputs
256 public static function regex($str, $regexRules) {
257 //redact the regular expressions
258 if (!empty($regexRules) && isset($str)) {
259 static $matches, $totalMatches, $match = array();
260 foreach ($regexRules as $pattern => $replacement) {
261 preg_match_all($pattern, $str, $matches);
262 if (!empty($matches[0])) {
263 if (empty($totalMatches)) {
264 $totalMatches = $matches[0];
267 $totalMatches = array_merge($totalMatches, $matches[0]);
269 $match = array_flip($totalMatches);
274 if (!empty($match)) {
275 foreach ($match as $matchKey => & $dontCare) {
276 foreach ($regexRules as $pattern => $replacement) {
277 if (preg_match($pattern, $matchKey)) {
278 $dontCare = $replacement . substr(md5($matchKey), 0, 5);
285 return CRM_Core_DAO
::$_nullArray;
290 * @param $stringRules
294 public static function redaction($str, $stringRules) {
296 if (!empty($stringRules)) {
297 foreach ($stringRules as $match => $replace) {
298 $str = str_ireplace($match, $replace, $str);
302 //return the redacted output
307 * Determine if a string is composed only of utf8 characters
314 public static function isUtf8($str) {
315 if (!function_exists(mb_detect_encoding
)) {
316 // eliminate all white space from the string
317 $str = preg_replace('/\s+/', '', $str);
319 /* pattern stolen from the php.net function documentation for
321 * comment by JF Sebastian, 30-Mar-2005
324 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);
326 // iconv('ISO-8859-1', 'UTF-8', $str);
329 $enc = mb_detect_encoding($str, array('UTF-8'), TRUE);
330 return ($enc !== FALSE);
335 * Determine if two href's are equivalent (fuzzy match)
337 * @param string $url1
338 * The first url to be matched.
339 * @param string $url2
340 * The second url to be matched against.
343 * true if the urls match, else false
345 public static function match($url1, $url2) {
346 $url1 = strtolower($url1);
347 $url2 = strtolower($url2);
349 $url1Str = parse_url($url1);
350 $url2Str = parse_url($url2);
352 if ($url1Str['path'] == $url2Str['path'] &&
353 self
::extractURLVarValue(CRM_Utils_Array
::value('query', $url1Str)) == self
::extractURLVarValue(CRM_Utils_Array
::value('query', $url2Str))
361 * Extract the civicrm path from the url
363 * @param string $query
366 * @return string|null
367 * civicrm url (eg: civicrm/contact/search)
369 public static function extractURLVarValue($query) {
370 $config = CRM_Core_Config
::singleton();
371 $urlVar = $config->userFrameworkURLVar
;
373 $params = explode('&', $query);
374 foreach ($params as $p) {
375 if (strpos($p, '=')) {
376 list($k, $v) = explode('=', $p);
386 * Translate a true/false/yes/no string to a 0 or 1 value
389 * The string to be translated.
393 public static function strtobool($str) {
394 if (!is_scalar($str)) {
398 if (preg_match('/^(y(es)?|t(rue)?|1)$/i', $str)) {
405 * Returns string '1' for a true/yes/1 string, and '0' for no/false/0 else returns false
408 * The string to be translated.
412 public static function strtoboolstr($str) {
413 if (!is_scalar($str)) {
417 if (preg_match('/^(y(es)?|t(rue)?|1)$/i', $str)) {
420 elseif (preg_match('/^(n(o)?|f(alse)?|0)$/i', $str)) {
429 * Convert a HTML string into a text one using html2text
431 * @param string $html
432 * The string to be converted.
435 * the converted string
437 public static function htmlToText($html) {
438 require_once 'packages/html2text/rcube_html2text.php';
439 $token_html = preg_replace('!\{([a-z_.]+)\}!i', 'token:{$1}', $html);
440 $converter = new rcube_html2text($token_html);
441 $token_text = $converter->get_text();
442 $text = preg_replace('!token\:\{([a-z_.]+)\}!i', '{$1}', $token_text);
448 * @param array $params
450 public static function extractName($string, &$params) {
451 $name = trim($string);
457 $name = str_replace('"', '', $name);
458 $name = str_replace('\'', '', $name);
460 // check for comma in name
461 if (strpos($name, ',') !== FALSE) {
463 // name has a comma - assume lname, fname [mname]
464 $names = explode(',', $name);
465 if (count($names) > 1) {
466 $params['last_name'] = trim($names[0]);
468 // check for space delim
469 $fnames = explode(' ', trim($names[1]));
470 if (count($fnames) > 1) {
471 $params['first_name'] = trim($fnames[0]);
472 $params['middle_name'] = trim($fnames[1]);
475 $params['first_name'] = trim($fnames[0]);
479 $params['first_name'] = trim($names[0]);
483 // name has no comma - assume fname [mname] fname
484 $names = explode(' ', $name);
485 if (count($names) == 1) {
486 $params['first_name'] = $names[0];
488 elseif (count($names) == 2) {
489 $params['first_name'] = $names[0];
490 $params['last_name'] = $names[1];
493 $params['first_name'] = $names[0];
494 $params['middle_name'] = $names[1];
495 $params['last_name'] = $names[2];
505 public static function &makeArray($string) {
506 $string = trim($string);
508 $values = explode("\n", $string);
510 foreach ($values as $value) {
511 list($n, $v) = CRM_Utils_System
::explode('=', $value, 2);
513 $result[trim($n)] = trim($v);
520 * Given an ezComponents-parsed representation of
521 * a text with alternatives return only the first one
523 * @param string $full
524 * All alternatives as a long string (or some other text).
527 * only the first alternative found (or the text without alternatives)
529 public static function stripAlternatives($full) {
531 preg_match('/-ALTERNATIVE ITEM 0-(.*?)-ALTERNATIVE ITEM 1-.*-ALTERNATIVE END-/s', $full, $matches);
533 if (isset($matches[1]) &&
534 trim(strip_tags($matches[1])) != ''
544 * Strip leading, trailing, double spaces from string
545 * used for postal/greeting/addressee
547 * @param string $string
548 * Input string to be cleaned.
553 public static function stripSpaces($string) {
554 return (empty($string)) ?
$string : preg_replace("/\s{2,}/", " ", trim($string));
558 * clean the URL 'path' variable that we use
559 * to construct CiviCRM urls by removing characters from the path variable
561 * @param string $string
562 * The input string to be sanitized.
563 * @param array $search
564 * The characters to be sanitized.
565 * @param string $replace
566 * The character to replace it with.
569 * the sanitized string
571 public static function stripPathChars(
576 static $_searchChars = NULL;
577 static $_replaceChar = NULL;
579 if (empty($string)) {
583 if ($_searchChars == NULL) {
584 $_searchChars = array(
606 if ($search == NULL) {
607 $search = $_searchChars;
610 if ($replace == NULL) {
611 $replace = $_replaceChar;
614 return str_replace($search, $replace, $string);
619 * Use HTMLPurifier to clean up a text string and remove any potential
620 * xss attacks. This is primarily used in public facing pages which
621 * accept html as the input string
623 * @param string $string
627 * the cleaned up string
629 public static function purifyHTML($string) {
630 static $_filter = NULL;
632 $config = HTMLPurifier_Config
::createDefault();
633 $config->set('Core.Encoding', 'UTF-8');
635 // Disable the cache entirely
636 $config->set('Cache.DefinitionImpl', NULL);
638 $_filter = new HTMLPurifier($config);
641 return $_filter->purify($string);
645 * Truncate $string; if $string exceeds $maxLen, place "..." at the end
647 * @param string $string
652 public static function ellipsify($string, $maxLen) {
653 $len = strlen($string);
654 if ($len <= $maxLen) {
658 return substr($string, 0, $maxLen - 3) . '...';
663 * Generate a random string
669 public static function createRandom($len, $alphabet) {
670 $alphabetSize = strlen($alphabet);
672 for ($i = 0; $i < $len; $i++
) {
673 $result .= $alphabet{rand(1, $alphabetSize) - 1};
680 * "admin foo" => array(NULL,"admin foo")
681 * "cms:admin foo" => array("cms", "admin foo")
684 * @param string $string
685 * E.g. "view all contacts". Syntax: "[prefix:]name".
686 * @param null $defaultPrefix
689 * (0 => string|NULL $prefix, 1 => string $value)
691 public static function parsePrefix($delim, $string, $defaultPrefix = NULL) {
692 $pos = strpos($string, $delim);
693 if ($pos === FALSE) {
694 return array($defaultPrefix, $string);
697 return array(substr($string, 0, $pos), substr($string, 1 +
$pos));
702 * This function will mask part of the the user portion of an Email address (everything before the @)
704 * @param string $email
705 * The email address to be masked.
706 * @param string $maskChar
707 * The character used for masking.
708 * @param int $percent
709 * The percentage of the user portion to be masked.
712 * returns the masked Email address
714 public static function maskEmail($email, $maskChar = '*', $percent = 50) {
715 list($user, $domain) = preg_split("/@/", $email);
716 $len = strlen($user);
717 $maskCount = floor($len * $percent / 100);
718 $offset = floor(($len - $maskCount) / 2);
720 $masked = substr($user, 0, $offset)
721 . str_repeat($maskChar, $maskCount)
722 . substr($user, $maskCount +
$offset);
724 return ($masked . '@' . $domain);
728 * This function compares two strings
730 * @param string $strOne
732 * @param string $strTwo
735 * Boolean indicating whether you want the comparison to be case sensitive or not.
738 * TRUE (string are identical); FALSE (strings are not identical)
740 public static function compareStr($strOne, $strTwo, $case) {
742 // Convert to lowercase and trim white spaces
743 if (strtolower(trim($strOne)) == strtolower(trim($strTwo))) {
744 // yes - they are identical
752 if ($case == FALSE) {
754 if (trim($strOne) == trim($strTwo)) {
755 // yes - they are identical
766 * Many parts of the codebase have a convention of internally passing around
767 * HTML-encoded URLs. This effectively means that "&" is replaced by "&"
768 * (because most other odd characters are %-escaped in URLs; and %-escaped
769 * strings don't need any extra escaping in HTML).
771 * @param string $htmlUrl
772 * URL with HTML entities.
774 * URL without HTML entities
776 public static function unstupifyUrl($htmlUrl) {
777 return str_replace('&', '&', $htmlUrl);