Merge pull request #23361 from eileenmcnaughton/import_comment
[civicrm-core.git] / CRM / Utils / SQL.php
index 70e1c5a26ed30a9c71bfd8ee43047114e45d3f14..1ca298e96ad37d881c25c64cde4aeed53709bb33 100644 (file)
@@ -168,4 +168,65 @@ class CRM_Utils_SQL {
     return CRM_Core_DAO::singleValueQuery('SELECT VERSION()');
   }
 
+  /**
+   * Does the DSN indicate the connection should use ssl.
+   *
+   * @param string $dsn
+   *
+   * @return bool
+   */
+  public static function isSSLDSN(string $dsn):bool {
+    // Note that ssl= below is not an official PEAR::DB option. It doesn't know
+    // what to do with it. We made it up because it's not required
+    // to have client-side certificates to use ssl, so here you can specify
+    // you want that by putting ssl=1 in the DSN string.
+    //
+    // Cast to bool in case of error which we interpret as no ssl.
+    return (bool) preg_match('/[\?&](key|cert|ca|capath|cipher|ssl)=/', $dsn);
+  }
+
+  /**
+   * If DB_DSN_MODE is auto then we should replace mysql with mysqli if mysqli is available or the other way around as appropriate
+   * @param string $dsn
+   *
+   * @return string
+   */
+  public static function autoSwitchDSN($dsn) {
+    if (defined('DB_DSN_MODE') && DB_DSN_MODE === 'auto') {
+      if (extension_loaded('mysqli')) {
+        $dsn = preg_replace('/^mysql:/', 'mysqli:', $dsn);
+      }
+      else {
+        $dsn = preg_replace('/^mysqli:/', 'mysql:', $dsn);
+      }
+    }
+    return $dsn;
+  }
+
+  /**
+   * Filter out Emojis in where clause if the database (determined by checking the create table for civicrm_contact)
+   * cannot support emojis
+   * @param mixed $criteria - filter criteria to check
+   *
+   * @return bool|string
+   */
+  public static function handleEmojiInQuery($criteria) {
+    if (!CRM_Core_BAO_SchemaHandler::databaseSupportsUTF8MB4()) {
+      foreach ((array) $criteria as $criterion) {
+        if (!empty($criterion) && !is_numeric($criterion)
+          // The first 2 criteria are redundant but are added as they
+          // seem like they would
+          // be quicker than this 3rd check.
+          && max(array_map('ord', str_split($criterion))) >= 240) {
+          // String contains unsupported emojis.
+          // We return a clause that resolves to false as an emoji string by definition cannot be saved.
+          // note that if we return just 0 for false if gets lost in empty checks.
+          // https://stackoverflow.com/questions/16496554/can-php-detect-4-byte-encoded-utf8-chars
+          return '0 = 1';
+        }
+      }
+      return TRUE;
+    }
+  }
+
 }