X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FString.php;h=b561cad091520ce2d7915a0d803cabe34aff5ade;hb=aff16051425a0f5c1f9be65810572bbbefe6f408;hp=1643491496d49a1c352e489abb4245c00591ad29;hpb=e913987d303f2c51e60be23820fc8d1031e7eadd;p=civicrm-core.git diff --git a/CRM/Utils/String.php b/CRM/Utils/String.php index 1643491496..b561cad091 100644 --- a/CRM/Utils/String.php +++ b/CRM/Utils/String.php @@ -1,7 +1,7 @@ $replace) { @@ -286,21 +302,20 @@ class CRM_Utils_String { /** * Determine if a string is composed only of utf8 characters * - * @param string $str input string - * @access public - * @static + * @param string $str + * Input string. * - * @return boolean + * @return bool */ - static function isUtf8($str) { + public static function isUtf8($str) { if (!function_exists(mb_detect_encoding)) { // eliminate all white space from the string $str = preg_replace('/\s+/', '', $str); /* pattern stolen from the php.net function documentation for - * utf8decode(); - * comment by JF Sebastian, 30-Mar-2005 - */ + * utf8decode(); + * comment by JF Sebastian, 30-Mar-2005 + */ 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); // || @@ -313,16 +328,17 @@ class CRM_Utils_String { } /** - * determine if two href's are equivalent (fuzzy match) + * Determine if two href's are equivalent (fuzzy match) * - * @param string $url1 the first url to be matched - * @param string $url2 the second url to be matched against + * @param string $url1 + * The first url to be matched. + * @param string $url2 + * The second url to be matched against. * - * @return boolean true if the urls match, else false - * @access public - * @static + * @return bool + * true if the urls match, else false */ - static function match($url1, $url2) { + public static function match($url1, $url2) { $url1 = strtolower($url1); $url2 = strtolower($url2); @@ -338,15 +354,15 @@ class CRM_Utils_String { } /** - * Function to extract variable values + * Extract the civicrm path from the url. * - * @param mix $query this is basically url + * @param string $query + * A url string. * - * @return mix $v returns civicrm url (eg: civicrm/contact/search/...) - * @access public - * @static + * @return string|null + * civicrm url (eg: civicrm/contact/search) */ - static function extractURLVarValue($query) { + public static function extractURLVarValue($query) { $config = CRM_Core_Config::singleton(); $urlVar = $config->userFrameworkURLVar; @@ -363,15 +379,14 @@ class CRM_Utils_String { } /** - * translate a true/false/yes/no string to a 0 or 1 value + * Translate a true/false/yes/no string to a 0 or 1 value * - * @param string $str the string to be translated + * @param string $str + * The string to be translated. * - * @return boolean - * @access public - * @static + * @return bool */ - static function strtobool($str) { + public static function strtobool($str) { if (!is_scalar($str)) { return FALSE; } @@ -383,15 +398,14 @@ class CRM_Utils_String { } /** - * returns string '1' for a true/yes/1 string, and '0' for no/false/0 else returns false + * Returns string '1' for a true/yes/1 string, and '0' for no/false/0 else returns false * - * @param string $str the string to be translated + * @param string $str + * The string to be translated. * - * @return boolean - * @access public - * @static + * @return bool */ - static function strtoboolstr($str) { + public static function strtoboolstr($str) { if (!is_scalar($str)) { return FALSE; } @@ -410,13 +424,13 @@ class CRM_Utils_String { /** * Convert a HTML string into a text one using html2text * - * @param string $html the string to be converted + * @param string $html + * The string to be converted. * - * @return string the converted string - * @access public - * @static + * @return string + * the converted string */ - static function htmlToText($html) { + public static function htmlToText($html) { require_once 'packages/html2text/rcube_html2text.php'; $token_html = preg_replace('!\{([a-z_.]+)\}!i', 'token:{$1}', $html); $converter = new rcube_html2text($token_html); @@ -427,9 +441,9 @@ class CRM_Utils_String { /** * @param $string - * @param $params + * @param array $params */ - static function extractName($string, &$params) { + public static function extractName($string, &$params) { $name = trim($string); if (empty($name)) { return; @@ -484,7 +498,7 @@ class CRM_Utils_String { * * @return array */ - static function &makeArray($string) { + public static function &makeArray($string) { $string = trim($string); $values = explode("\n", $string); @@ -502,11 +516,13 @@ class CRM_Utils_String { * Given an ezComponents-parsed representation of * a text with alternatives return only the first one * - * @param string $full all alternatives as a long string (or some other text) + * @param string $full + * All alternatives as a long string (or some other text). * - * @return string only the first alternative found (or the text without alternatives) + * @return string + * only the first alternative found (or the text without alternatives) */ - static function stripAlternatives($full) { + public static function stripAlternatives($full) { $matches = array(); preg_match('/-ALTERNATIVE ITEM 0-(.*?)-ALTERNATIVE ITEM 1-.*-ALTERNATIVE END-/s', $full, $matches); @@ -521,32 +537,35 @@ class CRM_Utils_String { } /** - * strip leading, trailing, double spaces from string + * Strip leading, trailing, double spaces from string * used for postal/greeting/addressee * - * @param string $string input string to be cleaned + * @param string $string + * Input string to be cleaned. * - * @return string the cleaned string - * @access public - * @static + * @return string + * the cleaned string */ - static function stripSpaces($string) { + public static function stripSpaces($string) { return (empty($string)) ? $string : preg_replace("/\s{2,}/", " ", trim($string)); } /** - * This function is used to clean the URL 'path' variable that we use + * clean the URL 'path' variable that we use * to construct CiviCRM urls by removing characters from the path variable * - * @param string $string the input string to be sanitized - * @param array $search the characters to be sanitized - * @param string $replace the character to replace it with + * @param string $string + * The input string to be sanitized. + * @param array $search + * The characters to be sanitized. + * @param string $replace + * The character to replace it with. * - * @return string the sanitized string - * @access public - * @static + * @return string + * the sanitized string */ - static function stripPathChars($string, + public static function stripPathChars( + $string, $search = NULL, $replace = NULL ) { @@ -559,15 +578,27 @@ class CRM_Utils_String { if ($_searchChars == NULL) { $_searchChars = array( - '&', ';', ',', '=', '$', - '"', "'", '\\', - '<', '>', '(', ')', - ' ', "\r", "\r\n", "\n", "\t", + '&', + ';', + ',', + '=', + '$', + '"', + "'", + '\\', + '<', + '>', + '(', + ')', + ' ', + "\r", + "\r\n", + "\n", + "\t", ); $_replaceChar = '_'; } - if ($search == NULL) { $search = $_searchChars; } @@ -585,20 +616,20 @@ class CRM_Utils_String { * xss attacks. This is primarily used in public facing pages which * accept html as the input string * - * @param string $string the input string + * @param string $string + * The input string. * - * @return string the cleaned up string - * @public - * @static + * @return string + * the cleaned up string */ - static function purifyHTML($string) { - static $_filter = null; + public static function purifyHTML($string) { + static $_filter = NULL; if (!$_filter) { $config = HTMLPurifier_Config::createDefault(); $config->set('Core.Encoding', 'UTF-8'); // Disable the cache entirely - $config->set('Cache.DefinitionImpl', null); + $config->set('Cache.DefinitionImpl', NULL); $_filter = new HTMLPurifier($config); } @@ -614,18 +645,18 @@ class CRM_Utils_String { * * @return string */ - static function ellipsify($string, $maxLen) { + public static function ellipsify($string, $maxLen) { $len = strlen($string); if ($len <= $maxLen) { return $string; } else { - return substr($string, 0, $maxLen-3) . '...'; + return substr($string, 0, $maxLen - 3) . '...'; } } /** - * Generate a random string + * Generate a random string. * * @param $len * @param $alphabet @@ -646,10 +677,12 @@ class CRM_Utils_String { * "cms:admin foo" => array("cms", "admin foo") * * @param $delim - * @param string $string e.g. "view all contacts". Syntax: "[prefix:]name" + * @param string $string + * E.g. "view all contacts". Syntax: "[prefix:]name". * @param null $defaultPrefix * - * @return array (0 => string|NULL $prefix, 1 => string $value) + * @return array + * (0 => string|NULL $prefix, 1 => string $value) */ public static function parsePrefix($delim, $string, $defaultPrefix = NULL) { $pos = strpos($string, $delim); @@ -657,40 +690,48 @@ class CRM_Utils_String { return array($defaultPrefix, $string); } else { - return array(substr($string, 0, $pos), substr($string, 1+$pos)); + return array(substr($string, 0, $pos), substr($string, 1 + $pos)); } } /** - * this function will mask part of the the user portion of an Email address (everything before the @) + * This function will mask part of the the user portion of an Email address (everything before the @) * - * @param string $email the email address to be masked - * @param string $maskChar the character used for masking - * @param integer $percent the percentage of the user portion to be masked + * @param string $email + * The email address to be masked. + * @param string $maskChar + * The character used for masking. + * @param int $percent + * The percentage of the user portion to be masked. * - * @return string returns the masked Email address + * @return string + * returns the masked Email address */ - public static function maskEmail($email, $maskChar= '*', $percent=50) { + public static function maskEmail($email, $maskChar = '*', $percent = 50) { list($user, $domain) = preg_split("/@/", $email); $len = strlen($user); - $maskCount = floor($len * $percent /100); + $maskCount = floor($len * $percent / 100); $offset = floor(($len - $maskCount) / 2); $masked = substr($user, 0, $offset) - .str_repeat($maskChar, $maskCount) - .substr($user, $maskCount + $offset); + . str_repeat($maskChar, $maskCount) + . substr($user, $maskCount + $offset); - return($masked.'@'.$domain); + return ($masked . '@' . $domain); } /** - * this function compares two strings + * This function compares two strings. * - * @param string $strOne string one - * @param string $strTwo string two - * @param boolean $case boolean indicating whether you want the comparison to be case sensitive or not + * @param string $strOne + * String one. + * @param string $strTwo + * String two. + * @param bool $case + * Boolean indicating whether you want the comparison to be case sensitive or not. * - * @return boolean TRUE (string are identical); FALSE (strings are not identical) + * @return bool + * TRUE (string are identical); FALSE (strings are not identical) */ public static function compareStr($strOne, $strTwo, $case) { if ($case == TRUE) { @@ -723,11 +764,28 @@ class CRM_Utils_String { * (because most other odd characters are %-escaped in URLs; and %-escaped * strings don't need any extra escaping in HTML). * - * @param string $url URL with HTML entities - * @return string URL without HTML entities + * @param string $htmlUrl + * URL with HTML entities. + * @return string + * URL without HTML entities */ public static function unstupifyUrl($htmlUrl) { return str_replace('&', '&', $htmlUrl); } -} + /** + * Formats a string of attributes for insertion in an html tag. + * + * @param array $attributes + * + * @return string + */ + public static function htmlAttributes($attributes) { + $output = ''; + foreach ($attributes as $name => $vals) { + $output .= " $name=\"" . htmlspecialchars(implode(' ', (array) $vals)) . '"'; + } + return ltrim($output); + } + +}