move function to avoid problems
authordemeritcowboy <demeritcowboy@hotmail.com>
Wed, 12 Aug 2020 03:12:41 +0000 (23:12 -0400)
committerdemeritcowboy <demeritcowboy@hotmail.com>
Wed, 12 Aug 2020 11:30:24 +0000 (07:30 -0400)
CRM/Core/DAO.php
CRM/Utils/SQL.php
tests/phpunit/CRM/Utils/SQLTest.php

index a1cd6f7498915e29b1919cb62dd88662533a633a..59d692375cc07a0c940fd0255d032e8d04a54487 100644 (file)
@@ -163,7 +163,7 @@ class CRM_Core_DAO extends DB_DataObject {
     $options = &PEAR::getStaticProperty('DB_DataObject', 'options');
     $options['database'] = $dsn;
     $options['quote_identifiers'] = TRUE;
-    if (self::isSSLDSN($dsn)) {
+    if (CRM_Utils_SQL::isSSLDSN($dsn)) {
       // There are two different options arrays.
       $other_options = &PEAR::getStaticProperty('DB', 'options');
       $other_options['ssl'] = TRUE;
@@ -3111,21 +3111,4 @@ SELECT contact_id
     }
   }
 
-  /**
-   * 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);
-  }
-
 }
index 70e1c5a26ed30a9c71bfd8ee43047114e45d3f14..1cd1dc02b6e70e3c8139fcbf5696da1e5576553b 100644 (file)
@@ -168,4 +168,21 @@ 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);
+  }
+
 }
index 56f2416c4969a0c792670b44fa56e9ff5ef0fe2b..cc5e3679b2db0b5fed35f6db4cb4e8176ce514f8 100644 (file)
@@ -32,4 +32,40 @@ class CRM_Utils_SQLTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * Test isSSLDSN
+   * @dataProvider dsnProvider
+   * @param string $input
+   * @param bool $expected
+   */
+  public function testIsSSLDSN(string $input, bool $expected) {
+    $this->assertSame($expected, CRM_Utils_SQL::isSSLDSN($input));
+  }
+
+  /**
+   * Data provider for testIsSSLDSN
+   * @return array
+   */
+  public function dsnProvider():array {
+    return [
+      ['', FALSE],
+      ['mysqli://user:pass@localhost/drupal', FALSE],
+      ['mysqli://user:pass@localhost:3306/drupal', FALSE],
+      ['mysql://user:pass@localhost:3306/drupal', FALSE],
+      ['mysql://user:pass@localhost:3306/drupal', FALSE],
+      ['mysql://user:pass@localhost:3306/drupal?new_link=true', FALSE],
+      ['mysqli://user:pass@localhost:3306/drupal?ssl', FALSE],
+      ['mysqli://user:pass@localhost:3306/drupal?ssl=1', TRUE],
+      ['mysqli://user:pass@localhost:3306/drupal?new_link=true&ssl=1', TRUE],
+      ['mysql://user:pass@localhost:3306/drupal?ssl=1', TRUE],
+      ['mysqli://user:pass@localhost:3306/drupal?ca=%2Ftmp%2Fcacert.crt', TRUE],
+      ['mysqli://user:pass@localhost/drupal?ca=%2Ftmp%2Fcacert.crt&cert=%2Ftmp%2Fcert.crt&key=%2Ftmp%2F', TRUE],
+      ['mysqli://user:pass@localhost/drupal?ca=%2Fpath%20with%20spaces%2Fcacert.crt', TRUE],
+      ['mysqli://user:pass@localhost:3306/drupal?cipher=aes', TRUE],
+      ['mysqli://user:pass@localhost:3306/drupal?capath=%2Ftmp', TRUE],
+      ['mysqli://user:pass@localhost:3306/drupal?cipher=aes&capath=%2Ftmp&food=banana', TRUE],
+      ['mysqli://user:pass@localhost:3306/drupal?food=banana&cipher=aes', TRUE],
+    ];
+  }
+
 }