Merge pull request #5243 from totten/4.5-dompdf
[civicrm-core.git] / CRM / Utils / String.php
index 95856cab6ac9be657e2a38f0b52f56988a7a4072..1643491496d49a1c352e489abb4245c00591ad29 100644 (file)
@@ -53,6 +53,8 @@ class CRM_Utils_String {
    *
    * @param  name    Name of the string
    *
+   * @param int $maxLength
+   *
    * @return string  An equivalent variable name
    *
    * @access public
@@ -121,7 +123,7 @@ class CRM_Utils_String {
    * useful while converting file names to class names etc
    *
    * @param string $string the input string
-   * @param char   $char   the character used to demarcate the componets
+   * @param \char|string $char $char   the character used to demarcate the componets
    *
    * @access public
    *
@@ -263,6 +265,12 @@ class CRM_Utils_String {
     return CRM_Core_DAO::$_nullArray;
   }
 
+  /**
+   * @param $str
+   * @param $stringRules
+   *
+   * @return mixed
+   */
   static function redaction($str, $stringRules) {
     //redact the strings
     if (!empty($stringRules)) {
@@ -417,6 +425,10 @@ class CRM_Utils_String {
     return $text;
   }
 
+  /**
+   * @param $string
+   * @param $params
+   */
   static function extractName($string, &$params) {
     $name = trim($string);
     if (empty($name)) {
@@ -467,6 +479,11 @@ class CRM_Utils_String {
     }
   }
 
+  /**
+   * @param $string
+   *
+   * @return array
+   */
   static function &makeArray($string) {
     $string = trim($string);
 
@@ -594,6 +611,8 @@ class CRM_Utils_String {
    *
    * @param string $string
    * @param int $maxLen
+   *
+   * @return string
    */
   static function ellipsify($string, $maxLen) {
     $len = strlen($string);
@@ -626,7 +645,10 @@ class CRM_Utils_String {
    * "admin foo" => array(NULL,"admin foo")
    * "cms:admin foo" => array("cms", "admin foo")
    *
+   * @param $delim
    * @param string $string e.g. "view all contacts". Syntax: "[prefix:]name"
+   * @param null $defaultPrefix
+   *
    * @return array (0 => string|NULL $prefix, 1 => string $value)
    */
   public static function parsePrefix($delim, $string, $defaultPrefix = NULL) {
@@ -639,6 +661,73 @@ class CRM_Utils_String {
     }
   }
 
+  /**
+   * 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
+   *
+   * @return string returns the masked Email address
+   */
+  public static function maskEmail($email, $maskChar= '*', $percent=50) {
+    list($user, $domain) = preg_split("/@/", $email);
+    $len = strlen($user);
+    $maskCount = floor($len * $percent /100);
+    $offset = floor(($len - $maskCount) / 2);
+
+    $masked = substr($user, 0, $offset)
+      .str_repeat($maskChar, $maskCount)
+      .substr($user, $maskCount + $offset);
+
+    return($masked.'@'.$domain);
+  }
 
+  /**
+   * 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
+   *
+   * @return boolean TRUE (string are identical); FALSE (strings are not identical)
+   */
+  public static function compareStr($strOne, $strTwo, $case) {
+    if ($case == TRUE) {
+      // Convert to lowercase and trim white spaces
+      if (strtolower(trim($strOne)) == strtolower(trim($strTwo))) {
+        // yes - they are identical
+        return TRUE;
+      }
+      else {
+        // not identical
+        return FALSE;
+      }
+    }
+    if ($case == FALSE) {
+      // Trim white spaces
+      if (trim($strOne) == trim($strTwo)) {
+        // yes - they are identical
+        return TRUE;
+      }
+      else {
+        // not identical
+        return FALSE;
+      }
+    }
+  }
+
+  /**
+   * Many parts of the codebase have a convention of internally passing around
+   * HTML-encoded URLs. This effectively means that "&" is replaced by "&"
+   * (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
+   */
+  public static function unstupifyUrl($htmlUrl) {
+    return str_replace('&', '&', $htmlUrl);
+  }
 }