public function connect($host = '127.0.0.1', $username = '', $passwd = '', $dbname = '', $port = 3306, $socket = '') { if (!parent::real_connect($this->cfg['host'], $this->cfg['username'], $this->cfg['password'], null, $this->cfg['port'], $this->cfg['socket'], $this->cfg['flags'])) { throw new Poodle_SQL_Exception($this->connect_error, $this->connect_errno, Poodle_SQL_Exception::NO_CONNECTION); } $v41 = version_compare($this->server_version, '4.1', '>='); if (!$v41) { $this->cfg['charset'] = 'latin1'; } $this->select_db(); $this->set_charset(); # dev.mysql.com/doc/refman/5.1/en/time-zone-support.html parent::query('SET time_zone = \'+0:00\''); parent::query('SET SESSION sql_mode = \'TRADITIONAL,ONLY_FULL_GROUP_BY,NO_AUTO_VALUE_ON_ZERO,PIPES_AS_CONCAT\''); if ($this->cfg['storage_engine']) { parent::query('SET ' . ($v41 ? 'storage_engine' : 'table_type') . '=' . $this->cfg['storage_engine']); } }
/** * Database connection * * @param bool $persistent * @return object */ public function db_connect($persistent = FALSE) { // Do we have a socket path? if ($this->hostname[0] === '/') { $hostname = NULL; $port = NULL; $socket = $this->hostname; } else { // Persistent connection support was added in PHP 5.3.0 $hostname = $persistent === TRUE && is_php('5.3') ? 'p:' . $this->hostname : $this->hostname; $port = empty($this->port) ? NULL : $this->port; $socket = NULL; } $client_flags = $this->compress === TRUE ? MYSQLI_CLIENT_COMPRESS : 0; $this->_mysqli = mysqli_init(); $this->_mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10); if (isset($this->stricton)) { if ($this->stricton) { $this->_mysqli->options(MYSQLI_INIT_COMMAND, 'SET SESSION sql_mode = CONCAT(@@sql_mode, ",", "STRICT_ALL_TABLES")'); } else { $this->_mysqli->options(MYSQLI_INIT_COMMAND, 'SET SESSION sql_mode = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( @@sql_mode, "STRICT_ALL_TABLES,", ""), ",STRICT_ALL_TABLES", ""), "STRICT_ALL_TABLES", ""), "STRICT_TRANS_TABLES,", ""), ",STRICT_TRANS_TABLES", ""), "STRICT_TRANS_TABLES", "")'); } } if (is_array($this->encrypt)) { $ssl = array(); empty($this->encrypt['ssl_key']) or $ssl['key'] = $this->encrypt['ssl_key']; empty($this->encrypt['ssl_cert']) or $ssl['cert'] = $this->encrypt['ssl_cert']; empty($this->encrypt['ssl_ca']) or $ssl['ca'] = $this->encrypt['ssl_ca']; empty($this->encrypt['ssl_capath']) or $ssl['capath'] = $this->encrypt['ssl_capath']; empty($this->encrypt['ssl_cipher']) or $ssl['cipher'] = $this->encrypt['ssl_cipher']; if (!empty($ssl)) { if (isset($this->encrypt['ssl_verify'])) { if ($this->encrypt['ssl_verify']) { defined('MYSQLI_OPT_SSL_VERIFY_SERVER_CERT') && $this->_mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, TRUE); } elseif (defined('MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT')) { $this->_mysqli->options(MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT, TRUE); } } $client_flags |= MYSQLI_CLIENT_SSL; $this->_mysqli->ssl_set(isset($ssl['key']) ? $ssl['key'] : NULL, isset($ssl['cert']) ? $ssl['cert'] : NULL, isset($ssl['ca']) ? $ssl['ca'] : NULL, isset($ssl['capath']) ? $ssl['capath'] : NULL, isset($ssl['cipher']) ? $ssl['cipher'] : NULL); } } if (@$this->_mysqli->real_connect($hostname, $this->username, $this->password, $this->database, $port, $socket, $client_flags)) { // Prior to version 5.7.3, MySQL silently downgrades to an unencrypted connection if SSL setup fails if ($client_flags & MYSQLI_CLIENT_SSL && version_compare($this->_mysqli->client_info, '5.7.3', '<=') && empty($this->_mysqli->query("SHOW STATUS LIKE 'ssl_cipher'")->fetch_object()->Value)) { $this->_mysqli->close(); $message = 'MySQLi was configured for an SSL connection, but got an unencrypted connection instead!'; log_message('error', $message); return $this->db->db_debug ? $this->db->display_error($message, '', TRUE) : FALSE; } return $this->_mysqli; } return FALSE; }
/** * Connect to the database. * * @param bool $persistent * @return mixed */ public function connect($persistent = false) { // Do we have a socket path? if ($this->hostname[0] === '/') { $hostname = null; $port = null; $socket = $this->hostname; } else { $hostname = $persistent === true ? 'p:' . $this->hostname : $this->hostname; $port = empty($this->port) ? null : $this->port; $socket = null; } $client_flags = $this->compress === true ? MYSQLI_CLIENT_COMPRESS : 0; $this->mysqli = mysqli_init(); $this->mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10); if (isset($this->strictOn)) { if ($this->strictOn) { $this->mysqli->options(MYSQLI_INIT_COMMAND, 'SET SESSION sql_mode = CONCAT(@@sql_mode, ",", "STRICT_ALL_TABLES")'); } else { $this->mysqli->options(MYSQLI_INIT_COMMAND, 'SET SESSION sql_mode = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( @@sql_mode, "STRICT_ALL_TABLES,", ""), ",STRICT_ALL_TABLES", ""), "STRICT_ALL_TABLES", ""), "STRICT_TRANS_TABLES,", ""), ",STRICT_TRANS_TABLES", ""), "STRICT_TRANS_TABLES", "")'); } } if (is_array($this->encrypt)) { $ssl = []; empty($this->encrypt['ssl_key']) or $ssl['key'] = $this->encrypt['ssl_key']; empty($this->encrypt['ssl_cert']) or $ssl['cert'] = $this->encrypt['ssl_cert']; empty($this->encrypt['ssl_ca']) or $ssl['ca'] = $this->encrypt['ssl_ca']; empty($this->encrypt['ssl_capath']) or $ssl['capath'] = $this->encrypt['ssl_capath']; empty($this->encrypt['ssl_cipher']) or $ssl['cipher'] = $this->encrypt['ssl_cipher']; if (!empty($ssl)) { if (isset($this->encrypt['ssl_verify'])) { if ($this->encrypt['ssl_verify']) { defined('MYSQLI_OPT_SSL_VERIFY_SERVER_CERT') && $this->mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true); } elseif (defined('MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT')) { $this->mysqli->options(MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT, true); } } $client_flags |= MYSQLI_CLIENT_SSL; $this->mysqli->ssl_set(isset($ssl['key']) ? $ssl['key'] : null, isset($ssl['cert']) ? $ssl['cert'] : null, isset($ssl['ca']) ? $ssl['ca'] : null, isset($ssl['capath']) ? $ssl['capath'] : null, isset($ssl['cipher']) ? $ssl['cipher'] : null); } } if ($this->mysqli->real_connect($hostname, $this->username, $this->password, $this->database, $port, $socket, $client_flags)) { // Prior to version 5.7.3, MySQL silently downgrades to an unencrypted connection if SSL setup fails if ($client_flags & MYSQLI_CLIENT_SSL && version_compare($this->mysqli->client_info, '5.7.3', '<=') && empty($this->mysqli->query("SHOW STATUS LIKE 'ssl_cipher'")->fetch_object()->Value)) { $this->mysqli->close(); $message = 'MySQLi was configured for an SSL connection, but got an unencrypted connection instead!'; log_message('error', $message); if ($this->db->db_debug) { throw new DatabaseException($message); } return false; } if (!$this->mysqli->set_charset($this->charset)) { log_message('error', "Database: Unable to set the configured connection charset ('{$this->charset}')."); $this->mysqli->close(); if ($this->db->debug) { throw new DatabaseException('Unable to set client connection character set: ' . $this->charset); } return false; } return $this->mysqli; } return false; }
private function init() { $handle = new MySQLi(); $handle->init(); $handle->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5); if (@$handle->real_connect(_DBMS_HOST, _DBMS_LOGIN, _DBMS_PASS, _DBMS_DB) === FALSE) { throw new DatabaseException($handle->connect_error); } if (!$handle->select_db(_DBMS_DB)) { throw new DatabaseException('Can\'t connect to Database !'); } return $handle; }