You are here

protected function MySQLiSource::_getConnection in Backup and Migrate 5.0.x

Get the db connection for the specified db.

Return value

\mysqli Connection object.

Throws

\Exception

5 calls to MySQLiSource::_getConnection()
DrupalMySQLiSource::importFromFile in src/Drupal/Source/DrupalMySQLiSource.php
Import to this source from the given backup file.
MySQLiSource::exportToFile in src/Core/Source/MySQLiSource.php
Export this source to the given temp file.
MySQLiSource::importFromFile in src/Core/Source/MySQLiSource.php
Import to this source from the given backup file.
MySQLiSource::query in src/Core/Source/MySQLiSource.php
Run a db query on this destination's db.
MySQLiSource::_dbInfo in src/Core/Source/MySQLiSource.php
Get the version info for the given DB.

File

src/Core/Source/MySQLiSource.php, line 134

Class

MySQLiSource
@package Drupal\backup_migrate\Core\Source

Namespace

Drupal\backup_migrate\Core\Source

Code

protected function _getConnection() {
  if (!$this->connection) {
    if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {
      throw new BackupMigrateException('Cannot connect to the database because the MySQLi extension is missing.');
    }
    $pdo_config = $this
      ->confGet('pdo');
    $ssl_config = [
      'key' => !empty($pdo_config[PDO::MYSQL_ATTR_SSL_KEY]) ? $pdo_config[PDO::MYSQL_ATTR_SSL_KEY] : NULL,
      'cert' => !empty($pdo_config[PDO::MYSQL_ATTR_SSL_CERT]) ? $pdo_config[PDO::MYSQL_ATTR_SSL_CERT] : NULL,
      'ca' => !empty($pdo_config[PDO::MYSQL_ATTR_SSL_CA]) ? $pdo_config[PDO::MYSQL_ATTR_SSL_CA] : NULL,
      'capath' => !empty($pdo_config[PDO::MYSQL_ATTR_SSL_CAPATH]) ? $pdo_config[PDO::MYSQL_ATTR_SSL_CAPATH] : NULL,
      'cypher' => !empty($pdo_config[PDO::MYSQL_ATTR_SSL_CIPHER]) ? $pdo_config[PDO::MYSQL_ATTR_SSL_CIPHER] : NULL,
    ];
    if (defined('PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT')) {
      $ssl_config['verify_server_cert'] = isset($pdo_config[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT]) ? $pdo_config[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] : TRUE;
    }
    else {
      $ssl_config['verify_server_cert'] = TRUE;
    }
    if ($ssl_config['key'] || $ssl_config['cert'] || $ssl_config['ca'] || $ssl_config['capath'] || $ssl_config['cypher']) {

      // Provide a workaround for PHP7 peer certificate verification issues.
      // @see https://bugs.php.net/bug.php?id=68344
      // @see https://bugs.php.net/bug.php?id=71003
      if ($ssl_config['verify_server_cert']) {
        $flags = MYSQLI_CLIENT_SSL;
      }
      else {
        $flags = MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
      }

      // Connect using PDO SSL config.
      $this->connection = new \mysqli();
      $this->connection
        ->ssl_set($ssl_config['key'], $ssl_config['cert'], $ssl_config['ca'], $ssl_config['capath'], $ssl_config['cypher']);
      $this->connection
        ->real_connect($this
        ->confGet('host'), $this
        ->confGet('username'), $this
        ->confGet('password'), $this
        ->confGet('database'), $this
        ->confGet('port'), $this
        ->confGet('socket'), $flags);
    }
    else {
      $this->connection = new \mysqli($this
        ->confGet('host'), $this
        ->confGet('username'), $this
        ->confGet('password'), $this
        ->confGet('database'), $this
        ->confGet('port'), $this
        ->confGet('socket'));
    }

    // Throw an error on fail.
    if ($this->connection->connect_errno || !$this->connection
      ->ping()) {
      throw new BackupMigrateException("Failed to connect to MySQL server.");
    }

    // Ensure, that the character set is utf8mb4.
    if (!$this->connection
      ->set_charset('utf8mb4')) {
      throw new BackupMigrateException('UTF8 is not supported by the MySQL server.');
    }
  }
  return $this->connection;
}