Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
fe482240 | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | ||
34 | require_once 'HTML/QuickForm/Rule/Email.php'; | |
35 | ||
36 | /** | |
fe482240 | 37 | * This class contains string functions. |
6a488035 TO |
38 | */ |
39 | class CRM_Utils_String { | |
7da04cde | 40 | const COMMA = ",", SEMICOLON = ";", SPACE = " ", TAB = "\t", LINEFEED = "\n", CARRIAGELINE = "\r\n", LINECARRIAGE = "\n\r", CARRIAGERETURN = "\r"; |
6a488035 TO |
41 | |
42 | /** | |
43 | * List of all letters and numbers | |
44 | */ | |
45 | const ALPHANUMERIC = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; | |
46 | ||
47 | /** | |
fe482240 | 48 | * Convert a display name into a potential variable name. |
6a488035 | 49 | * |
608e6658 | 50 | * @param $title title of the string |
f4aaa82a EM |
51 | * @param int $maxLength |
52 | * | |
c866eb5f TO |
53 | * @return string |
54 | * An equivalent variable name. | |
6a488035 | 55 | */ |
00be9182 | 56 | public static function titleToVar($title, $maxLength = 31) { |
6a488035 TO |
57 | $variable = self::munge($title, '_', $maxLength); |
58 | ||
59 | if (CRM_Utils_Rule::title($variable, $maxLength)) { | |
60 | return $variable; | |
61 | } | |
62 | ||
63 | // if longer than the maxLength lets just return a substr of the | |
64 | // md5 to prevent errors downstream | |
65 | return substr(md5($title), 0, $maxLength); | |
66 | } | |
67 | ||
68 | /** | |
fe482240 | 69 | * Replace all non alpha numeric characters and spaces with the replacement character. |
6a488035 | 70 | * |
77855840 TO |
71 | * @param string $name |
72 | * The name to be worked on. | |
73 | * @param string $char | |
74 | * The character to use for non-valid chars. | |
75 | * @param int $len | |
76 | * Length of valid variables. | |
6a488035 | 77 | * |
a6c01b45 CW |
78 | * @return string |
79 | * returns the manipulated string | |
6a488035 | 80 | */ |
00be9182 | 81 | public static function munge($name, $char = '_', $len = 63) { |
fe482240 | 82 | // Replace all white space and non-alpha numeric with $char |
6a488035 TO |
83 | // we only use the ascii character set since mysql does not create table names / field names otherwise |
84 | // CRM-11744 | |
85 | $name = preg_replace('/[^a-zA-Z0-9]+/', $char, trim($name)); | |
86 | ||
87 | if ($len) { | |
88 | // lets keep variable names short | |
89 | return substr($name, 0, $len); | |
90 | } | |
91 | else { | |
92 | return $name; | |
93 | } | |
94 | } | |
95 | ||
dc913073 | 96 | /** |
100fef9d | 97 | * Convert possibly underscore separated words to camel case with special handling for 'UF' |
f3258574 CW |
98 | * e.g membership_payment returns MembershipPayment |
99 | * | |
dc913073 EM |
100 | * @param string $string |
101 | * | |
a6c01b45 | 102 | * @return string |
dc913073 | 103 | */ |
00be9182 | 104 | public static function convertStringToCamel($string) { |
6ba0ed7a TO |
105 | $map = array( |
106 | 'acl' => 'Acl', | |
107 | 'ACL' => 'Acl', | |
108 | 'im' => 'Im', | |
109 | 'IM' => 'Im', | |
110 | ); | |
111 | if (isset($map[$string])) { | |
112 | return $map[$string]; | |
113 | } | |
114 | ||
dc913073 EM |
115 | $fragments = explode('_', $string); |
116 | foreach ($fragments as & $fragment) { | |
117 | $fragment = ucfirst($fragment); | |
118 | } | |
119 | // Special case: UFGroup, UFJoin, UFMatch, UFField | |
120 | if ($fragments[0] === 'Uf') { | |
121 | $fragments[0] = 'UF'; | |
122 | } | |
123 | return implode('', $fragments); | |
124 | } | |
125 | ||
6a488035 | 126 | /** |
fe482240 | 127 | * Takes a variable name and munges it randomly into another variable name. |
6a488035 | 128 | * |
77855840 TO |
129 | * @param string $name |
130 | * Initial Variable Name. | |
131 | * @param int $len | |
132 | * Length of valid variables. | |
6a488035 | 133 | * |
a6c01b45 CW |
134 | * @return string |
135 | * Randomized Variable Name | |
6a488035 | 136 | */ |
00be9182 | 137 | public static function rename($name, $len = 4) { |
6a488035 TO |
138 | $rand = substr(uniqid(), 0, $len); |
139 | return substr_replace($name, $rand, -$len, $len); | |
140 | } | |
141 | ||
142 | /** | |
100fef9d | 143 | * Takes a string and returns the last tuple of the string. |
fe482240 EM |
144 | * |
145 | * Useful while converting file names to class names etc | |
6a488035 | 146 | * |
77855840 TO |
147 | * @param string $string |
148 | * The input string. | |
f3258574 CW |
149 | * @param string $char |
150 | * Character used to demarcate the components | |
6a488035 | 151 | * |
a6c01b45 | 152 | * @return string |
f3258574 | 153 | * The last component |
6a488035 | 154 | */ |
00be9182 | 155 | public static function getClassName($string, $char = '_') { |
6a488035 TO |
156 | $names = array(); |
157 | if (!is_array($string)) { | |
158 | $names = explode($char, $string); | |
159 | } | |
160 | if (!empty($names)) { | |
161 | return array_pop($names); | |
162 | } | |
163 | } | |
164 | ||
165 | /** | |
f3258574 | 166 | * Appends a name to a string and separated by delimiter. |
fe482240 EM |
167 | * |
168 | * Does the right thing for an empty string | |
6a488035 | 169 | * |
77855840 TO |
170 | * @param string $str |
171 | * The string to be appended to. | |
172 | * @param string $delim | |
173 | * The delimiter to use. | |
174 | * @param mixed $name | |
175 | * The string (or array of strings) to append. | |
6a488035 | 176 | */ |
00be9182 | 177 | public static function append(&$str, $delim, $name) { |
6a488035 TO |
178 | if (empty($name)) { |
179 | return; | |
180 | } | |
181 | ||
182 | if (is_array($name)) { | |
183 | foreach ($name as $n) { | |
184 | if (empty($n)) { | |
185 | continue; | |
186 | } | |
187 | if (empty($str)) { | |
188 | $str = $n; | |
189 | } | |
190 | else { | |
191 | $str .= $delim . $n; | |
192 | } | |
193 | } | |
194 | } | |
195 | else { | |
196 | if (empty($str)) { | |
197 | $str = $name; | |
198 | } | |
199 | else { | |
200 | $str .= $delim . $name; | |
201 | } | |
202 | } | |
203 | } | |
204 | ||
205 | /** | |
fe482240 | 206 | * Determine if the string is composed only of ascii characters. |
6a488035 | 207 | * |
77855840 TO |
208 | * @param string $str |
209 | * Input string. | |
210 | * @param bool $utf8 | |
211 | * Attempt utf8 match on failure (default yes). | |
6a488035 | 212 | * |
608e6658 | 213 | * @return bool |
a6c01b45 | 214 | * true if string is ascii |
6a488035 | 215 | */ |
00be9182 | 216 | public static function isAscii($str, $utf8 = TRUE) { |
6a488035 TO |
217 | if (!function_exists('mb_detect_encoding')) { |
218 | // eliminate all white space from the string | |
219 | $str = preg_replace('/\s+/', '', $str); | |
220 | // FIXME: This is a pretty brutal hack to make utf8 and 8859-1 work. | |
221 | ||
50bfb460 | 222 | // match low- or high-ascii characters |
6a488035 TO |
223 | if (preg_match('/[\x00-\x20]|[\x7F-\xFF]/', $str)) { |
224 | // || // low ascii characters | |
225 | // high ascii characters | |
226 | // preg_match( '/[\x7F-\xFF]/', $str ) ) { | |
227 | if ($utf8) { | |
50bfb460 | 228 | // if we did match, try for utf-8, or iso8859-1 |
6a488035 TO |
229 | |
230 | return self::isUtf8($str); | |
231 | } | |
232 | else { | |
233 | return FALSE; | |
234 | } | |
235 | } | |
236 | return TRUE; | |
237 | } | |
238 | else { | |
239 | $order = array('ASCII'); | |
240 | if ($utf8) { | |
241 | $order[] = 'UTF-8'; | |
242 | } | |
243 | $enc = mb_detect_encoding($str, $order, TRUE); | |
244 | return ($enc == 'ASCII' || $enc == 'UTF-8'); | |
245 | } | |
246 | } | |
247 | ||
248 | /** | |
fe482240 | 249 | * Determine the string replacements for redaction. |
6a488035 TO |
250 | * on the basis of the regular expressions |
251 | * | |
77855840 TO |
252 | * @param string $str |
253 | * Input string. | |
254 | * @param array $regexRules | |
255 | * Regular expression to be matched w/ replacements. | |
6a488035 | 256 | * |
a6c01b45 CW |
257 | * @return array |
258 | * array of strings w/ corresponding redacted outputs | |
6a488035 | 259 | */ |
00be9182 | 260 | public static function regex($str, $regexRules) { |
50bfb460 | 261 | // redact the regular expressions |
6a488035 TO |
262 | if (!empty($regexRules) && isset($str)) { |
263 | static $matches, $totalMatches, $match = array(); | |
264 | foreach ($regexRules as $pattern => $replacement) { | |
265 | preg_match_all($pattern, $str, $matches); | |
266 | if (!empty($matches[0])) { | |
267 | if (empty($totalMatches)) { | |
268 | $totalMatches = $matches[0]; | |
269 | } | |
270 | else { | |
271 | $totalMatches = array_merge($totalMatches, $matches[0]); | |
272 | } | |
273 | $match = array_flip($totalMatches); | |
274 | } | |
275 | } | |
276 | } | |
277 | ||
278 | if (!empty($match)) { | |
279 | foreach ($match as $matchKey => & $dontCare) { | |
280 | foreach ($regexRules as $pattern => $replacement) { | |
281 | if (preg_match($pattern, $matchKey)) { | |
282 | $dontCare = $replacement . substr(md5($matchKey), 0, 5); | |
283 | break; | |
284 | } | |
285 | } | |
286 | } | |
287 | return $match; | |
288 | } | |
289 | return CRM_Core_DAO::$_nullArray; | |
290 | } | |
291 | ||
5bc392e6 EM |
292 | /** |
293 | * @param $str | |
294 | * @param $stringRules | |
295 | * | |
296 | * @return mixed | |
297 | */ | |
00be9182 | 298 | public static function redaction($str, $stringRules) { |
50bfb460 | 299 | // redact the strings |
6a488035 TO |
300 | if (!empty($stringRules)) { |
301 | foreach ($stringRules as $match => $replace) { | |
302 | $str = str_ireplace($match, $replace, $str); | |
303 | } | |
304 | } | |
305 | ||
50bfb460 | 306 | // return the redacted output |
6a488035 TO |
307 | return $str; |
308 | } | |
309 | ||
310 | /** | |
311 | * Determine if a string is composed only of utf8 characters | |
312 | * | |
77855840 TO |
313 | * @param string $str |
314 | * Input string. | |
6a488035 | 315 | * |
608e6658 | 316 | * @return bool |
6a488035 | 317 | */ |
00be9182 | 318 | public static function isUtf8($str) { |
6a488035 TO |
319 | if (!function_exists(mb_detect_encoding)) { |
320 | // eliminate all white space from the string | |
321 | $str = preg_replace('/\s+/', '', $str); | |
322 | ||
50bfb460 SB |
323 | // pattern stolen from the php.net function documentation for |
324 | // utf8decode(); | |
325 | // comment by JF Sebastian, 30-Mar-2005 | |
6a488035 TO |
326 | 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); |
327 | // || | |
328 | // iconv('ISO-8859-1', 'UTF-8', $str); | |
329 | } | |
330 | else { | |
331 | $enc = mb_detect_encoding($str, array('UTF-8'), TRUE); | |
332 | return ($enc !== FALSE); | |
333 | } | |
334 | } | |
335 | ||
336 | /** | |
50bfb460 | 337 | * Determine if two hrefs are equivalent (fuzzy match) |
6a488035 | 338 | * |
77855840 TO |
339 | * @param string $url1 |
340 | * The first url to be matched. | |
341 | * @param string $url2 | |
342 | * The second url to be matched against. | |
6a488035 | 343 | * |
608e6658 | 344 | * @return bool |
a6c01b45 | 345 | * true if the urls match, else false |
6a488035 | 346 | */ |
00be9182 | 347 | public static function match($url1, $url2) { |
6a488035 TO |
348 | $url1 = strtolower($url1); |
349 | $url2 = strtolower($url2); | |
350 | ||
351 | $url1Str = parse_url($url1); | |
352 | $url2Str = parse_url($url2); | |
353 | ||
354 | if ($url1Str['path'] == $url2Str['path'] && | |
355 | self::extractURLVarValue(CRM_Utils_Array::value('query', $url1Str)) == self::extractURLVarValue(CRM_Utils_Array::value('query', $url2Str)) | |
356 | ) { | |
357 | return TRUE; | |
358 | } | |
359 | return FALSE; | |
360 | } | |
361 | ||
362 | /** | |
fe482240 | 363 | * Extract the civicrm path from the url. |
6a488035 | 364 | * |
f3258574 CW |
365 | * @param string $query |
366 | * A url string. | |
6a488035 | 367 | * |
f3258574 CW |
368 | * @return string|null |
369 | * civicrm url (eg: civicrm/contact/search) | |
6a488035 | 370 | */ |
00be9182 | 371 | public static function extractURLVarValue($query) { |
6a488035 TO |
372 | $config = CRM_Core_Config::singleton(); |
373 | $urlVar = $config->userFrameworkURLVar; | |
374 | ||
375 | $params = explode('&', $query); | |
376 | foreach ($params as $p) { | |
377 | if (strpos($p, '=')) { | |
378 | list($k, $v) = explode('=', $p); | |
379 | if ($k == $urlVar) { | |
380 | return $v; | |
381 | } | |
382 | } | |
383 | } | |
384 | return NULL; | |
385 | } | |
386 | ||
387 | /** | |
100fef9d | 388 | * Translate a true/false/yes/no string to a 0 or 1 value |
6a488035 | 389 | * |
77855840 TO |
390 | * @param string $str |
391 | * The string to be translated. | |
6a488035 | 392 | * |
608e6658 | 393 | * @return bool |
6a488035 | 394 | */ |
00be9182 | 395 | public static function strtobool($str) { |
6a488035 TO |
396 | if (!is_scalar($str)) { |
397 | return FALSE; | |
398 | } | |
399 | ||
400 | if (preg_match('/^(y(es)?|t(rue)?|1)$/i', $str)) { | |
401 | return TRUE; | |
402 | } | |
403 | return FALSE; | |
404 | } | |
405 | ||
406 | /** | |
100fef9d | 407 | * Returns string '1' for a true/yes/1 string, and '0' for no/false/0 else returns false |
6a488035 | 408 | * |
77855840 TO |
409 | * @param string $str |
410 | * The string to be translated. | |
6a488035 | 411 | * |
608e6658 | 412 | * @return bool |
6a488035 | 413 | */ |
00be9182 | 414 | public static function strtoboolstr($str) { |
6a488035 TO |
415 | if (!is_scalar($str)) { |
416 | return FALSE; | |
417 | } | |
418 | ||
419 | if (preg_match('/^(y(es)?|t(rue)?|1)$/i', $str)) { | |
420 | return '1'; | |
421 | } | |
422 | elseif (preg_match('/^(n(o)?|f(alse)?|0)$/i', $str)) { | |
423 | return '0'; | |
424 | } | |
425 | else { | |
426 | return FALSE; | |
427 | } | |
428 | } | |
429 | ||
430 | /** | |
431 | * Convert a HTML string into a text one using html2text | |
432 | * | |
77855840 TO |
433 | * @param string $html |
434 | * The string to be converted. | |
6a488035 | 435 | * |
a6c01b45 CW |
436 | * @return string |
437 | * the converted string | |
6a488035 | 438 | */ |
00be9182 | 439 | public static function htmlToText($html) { |
7a999eae | 440 | require_once 'packages/html2text/rcube_html2text.php'; |
bfef4c7e KW |
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); | |
445 | return $text; | |
6a488035 TO |
446 | } |
447 | ||
5bc392e6 EM |
448 | /** |
449 | * @param $string | |
c490a46a | 450 | * @param array $params |
5bc392e6 | 451 | */ |
00be9182 | 452 | public static function extractName($string, &$params) { |
6a488035 TO |
453 | $name = trim($string); |
454 | if (empty($name)) { | |
455 | return; | |
456 | } | |
457 | ||
458 | // strip out quotes | |
459 | $name = str_replace('"', '', $name); | |
460 | $name = str_replace('\'', '', $name); | |
461 | ||
462 | // check for comma in name | |
463 | if (strpos($name, ',') !== FALSE) { | |
464 | ||
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]); | |
469 | ||
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]); | |
475 | } | |
476 | else { | |
477 | $params['first_name'] = trim($fnames[0]); | |
478 | } | |
479 | } | |
480 | else { | |
481 | $params['first_name'] = trim($names[0]); | |
482 | } | |
483 | } | |
484 | else { | |
485 | // name has no comma - assume fname [mname] fname | |
486 | $names = explode(' ', $name); | |
487 | if (count($names) == 1) { | |
488 | $params['first_name'] = $names[0]; | |
489 | } | |
490 | elseif (count($names) == 2) { | |
491 | $params['first_name'] = $names[0]; | |
492 | $params['last_name'] = $names[1]; | |
493 | } | |
494 | else { | |
495 | $params['first_name'] = $names[0]; | |
496 | $params['middle_name'] = $names[1]; | |
497 | $params['last_name'] = $names[2]; | |
498 | } | |
499 | } | |
500 | } | |
501 | ||
5bc392e6 EM |
502 | /** |
503 | * @param $string | |
504 | * | |
505 | * @return array | |
506 | */ | |
00be9182 | 507 | public static function &makeArray($string) { |
6a488035 TO |
508 | $string = trim($string); |
509 | ||
510 | $values = explode("\n", $string); | |
511 | $result = array(); | |
512 | foreach ($values as $value) { | |
513 | list($n, $v) = CRM_Utils_System::explode('=', $value, 2); | |
514 | if (!empty($v)) { | |
515 | $result[trim($n)] = trim($v); | |
516 | } | |
517 | } | |
518 | return $result; | |
519 | } | |
520 | ||
6a488035 TO |
521 | /** |
522 | * Given an ezComponents-parsed representation of | |
523 | * a text with alternatives return only the first one | |
524 | * | |
77855840 TO |
525 | * @param string $full |
526 | * All alternatives as a long string (or some other text). | |
6a488035 | 527 | * |
a6c01b45 CW |
528 | * @return string |
529 | * only the first alternative found (or the text without alternatives) | |
6a488035 | 530 | */ |
00be9182 | 531 | public static function stripAlternatives($full) { |
6a488035 TO |
532 | $matches = array(); |
533 | preg_match('/-ALTERNATIVE ITEM 0-(.*?)-ALTERNATIVE ITEM 1-.*-ALTERNATIVE END-/s', $full, $matches); | |
534 | ||
535 | if (isset($matches[1]) && | |
536 | trim(strip_tags($matches[1])) != '' | |
537 | ) { | |
538 | return $matches[1]; | |
539 | } | |
540 | else { | |
541 | return $full; | |
542 | } | |
543 | } | |
544 | ||
545 | /** | |
100fef9d | 546 | * Strip leading, trailing, double spaces from string |
6a488035 TO |
547 | * used for postal/greeting/addressee |
548 | * | |
77855840 TO |
549 | * @param string $string |
550 | * Input string to be cleaned. | |
6a488035 | 551 | * |
a6c01b45 CW |
552 | * @return string |
553 | * the cleaned string | |
6a488035 | 554 | */ |
00be9182 | 555 | public static function stripSpaces($string) { |
6a488035 TO |
556 | return (empty($string)) ? $string : preg_replace("/\s{2,}/", " ", trim($string)); |
557 | } | |
558 | ||
559 | /** | |
dc195289 | 560 | * clean the URL 'path' variable that we use |
6a488035 TO |
561 | * to construct CiviCRM urls by removing characters from the path variable |
562 | * | |
77855840 TO |
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. | |
6a488035 | 569 | * |
a6c01b45 CW |
570 | * @return string |
571 | * the sanitized string | |
6a488035 | 572 | */ |
608e6658 | 573 | public static function stripPathChars( |
a3e55d9c | 574 | $string, |
6a488035 TO |
575 | $search = NULL, |
576 | $replace = NULL | |
577 | ) { | |
578 | static $_searchChars = NULL; | |
579 | static $_replaceChar = NULL; | |
580 | ||
581 | if (empty($string)) { | |
582 | return $string; | |
583 | } | |
584 | ||
585 | if ($_searchChars == NULL) { | |
586 | $_searchChars = array( | |
353ffa53 TO |
587 | '&', |
588 | ';', | |
589 | ',', | |
590 | '=', | |
591 | '$', | |
592 | '"', | |
593 | "'", | |
594 | '\\', | |
595 | '<', | |
596 | '>', | |
597 | '(', | |
598 | ')', | |
599 | ' ', | |
600 | "\r", | |
601 | "\r\n", | |
602 | "\n", | |
603 | "\t", | |
6a488035 TO |
604 | ); |
605 | $_replaceChar = '_'; | |
606 | } | |
607 | ||
6a488035 TO |
608 | if ($search == NULL) { |
609 | $search = $_searchChars; | |
610 | } | |
611 | ||
612 | if ($replace == NULL) { | |
613 | $replace = $_replaceChar; | |
614 | } | |
615 | ||
616 | return str_replace($search, $replace, $string); | |
617 | } | |
618 | ||
619 | ||
620 | /** | |
621 | * Use HTMLPurifier to clean up a text string and remove any potential | |
622 | * xss attacks. This is primarily used in public facing pages which | |
623 | * accept html as the input string | |
624 | * | |
77855840 TO |
625 | * @param string $string |
626 | * The input string. | |
6a488035 | 627 | * |
a6c01b45 CW |
628 | * @return string |
629 | * the cleaned up string | |
6a488035 | 630 | */ |
00be9182 | 631 | public static function purifyHTML($string) { |
e7292422 | 632 | static $_filter = NULL; |
6a488035 TO |
633 | if (!$_filter) { |
634 | $config = HTMLPurifier_Config::createDefault(); | |
635 | $config->set('Core.Encoding', 'UTF-8'); | |
636 | ||
637 | // Disable the cache entirely | |
e7292422 | 638 | $config->set('Cache.DefinitionImpl', NULL); |
6a488035 TO |
639 | |
640 | $_filter = new HTMLPurifier($config); | |
641 | } | |
642 | ||
643 | return $_filter->purify($string); | |
644 | } | |
645 | ||
646 | /** | |
647 | * Truncate $string; if $string exceeds $maxLen, place "..." at the end | |
648 | * | |
649 | * @param string $string | |
650 | * @param int $maxLen | |
f4aaa82a EM |
651 | * |
652 | * @return string | |
6a488035 | 653 | */ |
00be9182 | 654 | public static function ellipsify($string, $maxLen) { |
6a488035 TO |
655 | $len = strlen($string); |
656 | if ($len <= $maxLen) { | |
657 | return $string; | |
658 | } | |
659 | else { | |
e7292422 | 660 | return substr($string, 0, $maxLen - 3) . '...'; |
6a488035 TO |
661 | } |
662 | } | |
663 | ||
664 | /** | |
fe482240 | 665 | * Generate a random string. |
6a488035 TO |
666 | * |
667 | * @param $len | |
668 | * @param $alphabet | |
669 | * @return string | |
670 | */ | |
671 | public static function createRandom($len, $alphabet) { | |
672 | $alphabetSize = strlen($alphabet); | |
673 | $result = ''; | |
674 | for ($i = 0; $i < $len; $i++) { | |
675 | $result .= $alphabet{rand(1, $alphabetSize) - 1}; | |
676 | } | |
677 | return $result; | |
678 | } | |
679 | ||
fc7a0aee TO |
680 | /** |
681 | * Examples: | |
682 | * "admin foo" => array(NULL,"admin foo") | |
683 | * "cms:admin foo" => array("cms", "admin foo") | |
684 | * | |
f4aaa82a | 685 | * @param $delim |
77855840 TO |
686 | * @param string $string |
687 | * E.g. "view all contacts". Syntax: "[prefix:]name". | |
f4aaa82a EM |
688 | * @param null $defaultPrefix |
689 | * | |
a6c01b45 CW |
690 | * @return array |
691 | * (0 => string|NULL $prefix, 1 => string $value) | |
fc7a0aee TO |
692 | */ |
693 | public static function parsePrefix($delim, $string, $defaultPrefix = NULL) { | |
694 | $pos = strpos($string, $delim); | |
695 | if ($pos === FALSE) { | |
696 | return array($defaultPrefix, $string); | |
697 | } | |
698 | else { | |
e7292422 | 699 | return array(substr($string, 0, $pos), substr($string, 1 + $pos)); |
fc7a0aee TO |
700 | } |
701 | } | |
702 | ||
87b48098 | 703 | /** |
100fef9d | 704 | * This function will mask part of the the user portion of an Email address (everything before the @) |
87b48098 | 705 | * |
77855840 TO |
706 | * @param string $email |
707 | * The email address to be masked. | |
708 | * @param string $maskChar | |
709 | * The character used for masking. | |
710 | * @param int $percent | |
711 | * The percentage of the user portion to be masked. | |
87b48098 | 712 | * |
a6c01b45 CW |
713 | * @return string |
714 | * returns the masked Email address | |
87b48098 | 715 | */ |
e7292422 | 716 | public static function maskEmail($email, $maskChar = '*', $percent = 50) { |
87b48098 K |
717 | list($user, $domain) = preg_split("/@/", $email); |
718 | $len = strlen($user); | |
e7292422 | 719 | $maskCount = floor($len * $percent / 100); |
87b48098 K |
720 | $offset = floor(($len - $maskCount) / 2); |
721 | ||
722 | $masked = substr($user, 0, $offset) | |
353ffa53 TO |
723 | . str_repeat($maskChar, $maskCount) |
724 | . substr($user, $maskCount + $offset); | |
87b48098 | 725 | |
92fcb95f | 726 | return ($masked . '@' . $domain); |
87b48098 K |
727 | } |
728 | ||
729 | /** | |
fe482240 | 730 | * This function compares two strings. |
87b48098 | 731 | * |
77855840 TO |
732 | * @param string $strOne |
733 | * String one. | |
734 | * @param string $strTwo | |
735 | * String two. | |
736 | * @param bool $case | |
737 | * Boolean indicating whether you want the comparison to be case sensitive or not. | |
87b48098 | 738 | * |
608e6658 | 739 | * @return bool |
a6c01b45 | 740 | * TRUE (string are identical); FALSE (strings are not identical) |
87b48098 K |
741 | */ |
742 | public static function compareStr($strOne, $strTwo, $case) { | |
743 | if ($case == TRUE) { | |
dca9da6e | 744 | // Convert to lowercase and trim white spaces |
87b48098 K |
745 | if (strtolower(trim($strOne)) == strtolower(trim($strTwo))) { |
746 | // yes - they are identical | |
747 | return TRUE; | |
748 | } | |
749 | else { | |
750 | // not identical | |
751 | return FALSE; | |
752 | } | |
753 | } | |
754 | if ($case == FALSE) { | |
dca9da6e | 755 | // Trim white spaces |
87b48098 K |
756 | if (trim($strOne) == trim($strTwo)) { |
757 | // yes - they are identical | |
758 | return TRUE; | |
759 | } | |
760 | else { | |
761 | // not identical | |
762 | return FALSE; | |
763 | } | |
764 | } | |
765 | } | |
fc7a0aee | 766 | |
77d45291 TO |
767 | /** |
768 | * Many parts of the codebase have a convention of internally passing around | |
769 | * HTML-encoded URLs. This effectively means that "&" is replaced by "&" | |
770 | * (because most other odd characters are %-escaped in URLs; and %-escaped | |
771 | * strings don't need any extra escaping in HTML). | |
772 | * | |
608e6658 | 773 | * @param string $htmlUrl |
77855840 | 774 | * URL with HTML entities. |
a6c01b45 CW |
775 | * @return string |
776 | * URL without HTML entities | |
77d45291 TO |
777 | */ |
778 | public static function unstupifyUrl($htmlUrl) { | |
779 | return str_replace('&', '&', $htmlUrl); | |
780 | } | |
96025800 | 781 | |
79a18c21 | 782 | /** |
fe482240 | 783 | * Formats a string of attributes for insertion in an html tag. |
79a18c21 CW |
784 | * |
785 | * @param array $attributes | |
786 | * | |
787 | * @return string | |
788 | */ | |
789 | public static function htmlAttributes($attributes) { | |
790 | $output = ''; | |
791 | foreach ($attributes as $name => $vals) { | |
792 | $output .= " $name=\"" . htmlspecialchars(implode(' ', (array) $vals)) . '"'; | |
793 | } | |
794 | return ltrim($output); | |
795 | } | |
796 | ||
83d511e6 TO |
797 | /** |
798 | * Determine if $string starts with $fragment. | |
799 | * | |
800 | * @param string $string | |
801 | * The long string. | |
802 | * @param string $fragment | |
803 | * The fragment to look for. | |
804 | * @return bool | |
805 | */ | |
806 | public static function startsWith($string, $fragment) { | |
807 | if ($fragment === '') { | |
808 | return TRUE; | |
809 | } | |
810 | $len = strlen($fragment); | |
811 | return substr($string, 0, $len) === $fragment; | |
812 | } | |
813 | ||
814 | /** | |
815 | * Determine if $string ends with $fragment. | |
816 | * | |
817 | * @param string $string | |
818 | * The long string. | |
819 | * @param string $fragment | |
820 | * The fragment to look for. | |
821 | * @return bool | |
822 | */ | |
823 | public static function endsWith($string, $fragment) { | |
824 | if ($fragment === '') { | |
825 | return TRUE; | |
826 | } | |
827 | $len = strlen($fragment); | |
828 | return substr($string, -1 * $len) === $fragment; | |
829 | } | |
830 | ||
831 | /** | |
832 | * @param string|array $patterns | |
833 | * @param array $allStrings | |
834 | * @param bool $allowNew | |
835 | * Whether to return new, unrecognized names. | |
836 | * @return array | |
837 | */ | |
838 | public static function filterByWildcards($patterns, $allStrings, $allowNew = FALSE) { | |
839 | $patterns = (array) $patterns; | |
840 | $result = array(); | |
841 | foreach ($patterns as $pattern) { | |
842 | if (!\CRM_Utils_String::endsWith($pattern, '*')) { | |
843 | if ($allowNew || in_array($pattern, $allStrings)) { | |
844 | $result[] = $pattern; | |
845 | } | |
846 | } | |
847 | else { | |
848 | $prefix = rtrim($pattern, '*'); | |
849 | foreach ($allStrings as $key) { | |
850 | if (\CRM_Utils_String::startsWith($key, $prefix)) { | |
851 | $result[] = $key; | |
852 | } | |
853 | } | |
854 | } | |
855 | } | |
856 | return array_values(array_unique($result)); | |
857 | } | |
858 | ||
6a488035 | 859 | } |