Merge pull request #19455 from colemanw/afformMeta
[civicrm-core.git] / CRM / Utils / String.php
index 8ffc390096fd850f21de324560b2655eba96797b..f98d4ec6d5c7ca00a3802ec8808e115b699c9b70 100644 (file)
@@ -221,6 +221,31 @@ class CRM_Utils_String {
     }
   }
 
+  /**
+   * Encode string using URL-safe Base64.
+   *
+   * @param string $v
+   *
+   * @return string
+   * @see https://tools.ietf.org/html/rfc4648#section-5
+   */
+  public static function base64UrlEncode($v) {
+    return rtrim(str_replace(['+', '/'], ['-', '_'], base64_encode($v)), '=');
+  }
+
+  /**
+   * Decode string using URL-safe Base64.
+   *
+   * @param string $v
+   *
+   * @return false|string
+   * @see https://tools.ietf.org/html/rfc4648#section-5
+   */
+  public static function base64UrlDecode($v) {
+    // PHP base64_decode() is already forgiving about padding ("=").
+    return base64_decode(str_replace(['-', '_'], ['+', '/'], $v));
+  }
+
   /**
    * Determine the string replacements for redaction.
    * on the basis of the regular expressions
@@ -645,7 +670,7 @@ class CRM_Utils_String {
     $alphabetSize = strlen($alphabet);
     $result = '';
     for ($i = 0; $i < $len; $i++) {
-      $result .= $alphabet{rand(1, $alphabetSize) - 1};
+      $result .= $alphabet[rand(1, $alphabetSize) - 1];
     }
     return $result;
   }
@@ -944,7 +969,7 @@ class CRM_Utils_String {
     if ($lastLetter == 's' || $lastLetter == 'x' || $lastTwo == 'ch') {
       return $str . 'es';
     }
-    if ($lastLetter == 'y' && $lastTwo != 'ey') {
+    if ($lastLetter == 'y' && !in_array($lastTwo, ['ay', 'ey', 'iy', 'oy', 'uy'])) {
       return substr($str, 0, -1) . 'ies';
     }
     return $str . 's';