/** * Connects to the database specified if no connection exists * * @return void */ private function connectToDatabase() { // Don't try to reconnect if we are already connected if ($this->connection) { return; } // Establish a connection to the database if ($this->extension == 'pdo') { $odbc = strtolower(substr($this->database, 0, 4)) == 'dsn:'; if ($this->type == 'mssql') { if ($odbc) { $dsn = 'odbc:' . substr($this->database, 4); } else { $separator = fCore::checkOS('windows') ? ',' : ':'; $port = $this->port ? $separator . $this->port : ''; $driver = fCore::checkOs('windows') ? 'mssql' : 'dblib'; $dsn = $driver . ':host=' . $this->host . $port . ';dbname=' . $this->database; } } elseif ($this->type == 'mysql') { if (substr($this->host, 0, 5) == 'sock:') { $dsn = 'mysql:unix_socket=' . substr($this->host, 5) . ';dbname=' . $this->database; } else { $port = $this->port ? ';port=' . $this->port : ''; $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->database . $port; } } elseif ($this->type == 'oracle') { if ($odbc) { $dsn = 'odbc:' . substr($this->database, 4); } else { $port = $this->port ? ':' . $this->port : ''; $dsn = 'oci:dbname=' . $this->host . $port . '/' . $this->database . ';charset=AL32UTF8'; } } elseif ($this->type == 'postgresql') { $dsn = 'pgsql:dbname=' . $this->database; if ($this->host && $this->host != 'sock:') { $dsn .= ' host=' . $this->host; } if ($this->port) { $dsn .= ' port=' . $this->port; } } elseif ($this->type == 'sqlite') { $dsn = 'sqlite:' . $this->database; } try { $this->connection = new PDO($dsn, $this->username, $this->password); if ($this->type == 'mysql') { $this->connection->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1); } } catch (PDOException $e) { $this->connection = FALSE; } } if ($this->extension == 'sqlite') { $this->connection = sqlite_open($this->database); } if ($this->extension == 'mssql') { $separator = fCore::checkOS('windows') ? ',' : ':'; $this->connection = mssql_connect($this->port ? $this->host . $separator . $this->port : $this->host, $this->username, $this->password); if ($this->connection !== FALSE && mssql_select_db($this->database, $this->connection) === FALSE) { $this->connection = FALSE; } } if ($this->extension == 'mysql') { if (substr($this->host, 0, 5) == 'sock:') { $host = substr($this->host, 4); } elseif ($this->port) { $host = $this->host . ':' . $this->port; } else { $host = $this->host; } $this->connection = mysql_connect($host, $this->username, $this->password); if ($this->connection !== FALSE && mysql_select_db($this->database, $this->connection) === FALSE) { $this->connection = FALSE; } } if ($this->extension == 'mysqli') { if (substr($this->host, 0, 5) == 'sock:') { $this->connection = mysqli_connect('localhost', $this->username, $this->password, $this->database, $this->port, substr($this->host, 5)); } elseif ($this->port) { $this->connection = mysqli_connect($this->host, $this->username, $this->password, $this->database, $this->port); } else { $this->connection = mysqli_connect($this->host, $this->username, $this->password, $this->database); } } if ($this->extension == 'oci8') { $this->connection = oci_connect($this->username, $this->password, $this->host . ($this->port ? ':' . $this->port : '') . '/' . $this->database, 'AL32UTF8'); } if ($this->extension == 'odbc') { $this->connection = odbc_connect(substr($this->database, 4), $this->username, $this->password); } if ($this->extension == 'pgsql') { $connection_string = "dbname='" . addslashes($this->database) . "'"; if ($this->host && $this->host != 'sock:') { $connection_string .= " host='" . addslashes($this->host) . "'"; } if ($this->username) { $connection_string .= " user='******'"; } if ($this->password) { $connection_string .= " password='******'"; } if ($this->port) { $connection_string .= " port='" . $this->port . "'"; } $this->connection = pg_connect($connection_string); } if ($this->extension == 'sqlsrv') { $options = array('Database' => $this->database, 'UID' => $this->username, 'PWD' => $this->password); $this->connection = sqlsrv_connect($this->host, $options); } // Ensure the connection was established if ($this->connection === FALSE) { throw new fConnectivityException('Unable to connect to database'); } // Make MySQL act more strict and use UTF-8 if ($this->type == 'mysql') { $this->query("SET SQL_MODE = 'REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE'"); $this->query("SET NAMES 'utf8'"); $this->query("SET CHARACTER SET utf8"); } // Make SQLite behave like other DBs for assoc arrays if ($this->type == 'sqlite') { $this->query('PRAGMA short_column_names = 1'); } // Fix some issues with mssql if ($this->type == 'mssql') { if (!isset($this->schema_info['character_set'])) { $this->determineCharacterSet(); } $this->query('SET TEXTSIZE 65536'); } // Make PostgreSQL use UTF-8 if ($this->type == 'postgresql') { $this->query("SET NAMES 'UTF8'"); } // Oracle has different date and timestamp defaults if ($this->type == 'oracle') { $this->query("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD'"); $this->query("ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"); $this->query("ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT = 'YYYY-MM-DD HH24:MI:SS TZR'"); $this->query("ALTER SESSION SET NLS_TIME_FORMAT = 'HH24:MI:SS'"); $this->query("ALTER SESSION SET NLS_TIME_TZ_FORMAT = 'HH24:MI:SS TZR'"); } }
/** * Connects to the database specified if no connection exists * * @return void */ private function connectToDatabase() { // Don't try to reconnect if we are already connected if ($this->connection) { return; } // Establish a connection to the database if ($this->extension == 'pdo') { $username = $this->username; $password = $this->password; if ($this->type == 'db2') { if ($this->host === NULL && $this->port === NULL) { $dsn = 'ibm:DSN:' . $this->database; } else { $dsn = 'ibm:DRIVER={IBM DB2 ODBC DRIVER};DATABASE=' . $this->database . ';HOSTNAME=' . $this->host . ';'; $dsn .= 'PORT=' . ($this->port ? $this->port : 60000) . ';'; $dsn .= 'PROTOCOL=TCPIP;UID=' . $username . ';PWD=' . $password . ';'; $username = NULL; $password = NULL; } } elseif ($this->type == 'mssql') { $separator = fCore::checkOS('windows') ? ',' : ':'; $port = $this->port ? $separator . $this->port : ''; $driver = fCore::checkOs('windows') ? 'mssql' : 'dblib'; $dsn = $driver . ':host=' . $this->host . $port . ';dbname=' . $this->database; } elseif ($this->type == 'mysql') { if (substr($this->host, 0, 5) == 'sock:') { $dsn = 'mysql:unix_socket=' . substr($this->host, 5) . ';dbname=' . $this->database; } else { $port = $this->port ? ';port=' . $this->port : ''; $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->database . $port; } } elseif ($this->type == 'oracle') { $port = $this->port ? ':' . $this->port : ''; $dsn = 'oci:dbname=' . $this->host . $port . '/' . $this->database . ';charset=AL32UTF8'; } elseif ($this->type == 'postgresql') { $dsn = 'pgsql:dbname=' . $this->database; if ($this->host && $this->host != 'sock:') { $dsn .= ' host=' . $this->host; } if ($this->port) { $dsn .= ' port=' . $this->port; } } elseif ($this->type == 'sqlite') { $dsn = 'sqlite:' . $this->database; } try { $this->connection = new PDO($dsn, $username, $password); if ($this->type == 'mysql') { $this->connection->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1); } } catch (PDOException $e) { $this->connection = FALSE; } } if ($this->extension == 'sqlite') { $this->connection = sqlite_open($this->database); } if ($this->extension == 'ibm_db2') { $username = $this->username; $password = $this->password; if ($this->host === NULL && $this->port === NULL) { $connection_string = $this->database; } else { $connection_string = 'DATABASE=' . $this->database . ';HOSTNAME=' . $this->host . ';'; $connection_string .= 'PORT=' . ($this->port ? $this->port : 60000) . ';'; $connection_string .= 'PROTOCOL=TCPIP;UID=' . $this->username . ';PWD=' . $this->password . ';'; $username = NULL; $password = NULL; } $options = array('autocommit' => DB2_AUTOCOMMIT_ON, 'DB2_ATTR_CASE' => DB2_CASE_LOWER); $this->connection = db2_connect($connection_string, $username, $password, $options); } if ($this->extension == 'mssql') { $separator = fCore::checkOS('windows') ? ',' : ':'; $this->connection = mssql_connect($this->port ? $this->host . $separator . $this->port : $this->host, $this->username, $this->password, TRUE); if ($this->connection !== FALSE && mssql_select_db($this->database, $this->connection) === FALSE) { $this->connection = FALSE; } } if ($this->extension == 'mysql') { if (substr($this->host, 0, 5) == 'sock:') { $host = substr($this->host, 4); } elseif ($this->port) { $host = $this->host . ':' . $this->port; } else { $host = $this->host; } $this->connection = mysql_connect($host, $this->username, $this->password, TRUE); if ($this->connection !== FALSE && mysql_select_db($this->database, $this->connection) === FALSE) { $this->connection = FALSE; } if ($this->connection && function_exists('mysql_set_charset') && !mysql_set_charset('utf8', $this->connection)) { throw new fConnectivityException('There was an error setting the database connection to use UTF-8'); } } if ($this->extension == 'mysqli') { if (substr($this->host, 0, 5) == 'sock:') { $this->connection = mysqli_connect('localhost', $this->username, $this->password, $this->database, $this->port, substr($this->host, 5)); } elseif ($this->port) { $this->connection = mysqli_connect($this->host, $this->username, $this->password, $this->database, $this->port); } else { $this->connection = mysqli_connect($this->host, $this->username, $this->password, $this->database); } if ($this->connection && !mysqli_set_charset($this->connection, 'utf8')) { throw new fConnectivityException('There was an error setting the database connection to use UTF-8'); } } if ($this->extension == 'oci8') { $this->connection = oci_connect($this->username, $this->password, $this->host . ($this->port ? ':' . $this->port : '') . '/' . $this->database, 'AL32UTF8'); } if ($this->extension == 'pgsql') { $connection_string = "dbname='" . addslashes($this->database) . "'"; if ($this->host && $this->host != 'sock:') { $connection_string .= " host='" . addslashes($this->host) . "'"; } if ($this->username) { $connection_string .= " user='******'"; } if ($this->password) { $connection_string .= " password='******'"; } if ($this->port) { $connection_string .= " port='" . $this->port . "'"; } $this->connection = pg_connect($connection_string, PGSQL_CONNECT_FORCE_NEW); } if ($this->extension == 'sqlsrv') { $options = array('Database' => $this->database, 'UID' => $this->username, 'PWD' => $this->password); $this->connection = sqlsrv_connect($this->host . ',' . $this->port, $options); } // Ensure the connection was established if ($this->connection === FALSE) { throw new fConnectivityException('Unable to connect to database'); } // Make MySQL act more strict and use UTF-8 if ($this->type == 'mysql') { $this->execute("SET SQL_MODE = 'REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE'"); $this->execute("SET NAMES 'utf8'"); $this->execute("SET CHARACTER SET utf8"); } // Make SQLite behave like other DBs for assoc arrays if ($this->type == 'sqlite') { $this->execute('PRAGMA short_column_names = 1'); } // Fix some issues with mssql if ($this->type == 'mssql') { if (!isset($this->schema_info['character_set'])) { $this->determineCharacterSet(); } $this->execute('SET TEXTSIZE 65536'); $this->execute('SET QUOTED_IDENTIFIER ON'); } // Make PostgreSQL use UTF-8 if ($this->type == 'postgresql') { $this->execute("SET NAMES 'UTF8'"); } // Oracle has different date and timestamp defaults if ($this->type == 'oracle') { $this->execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD'"); $this->execute("ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"); $this->execute("ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT = 'YYYY-MM-DD HH24:MI:SS TZR'"); $this->execute("ALTER SESSION SET NLS_TIME_FORMAT = 'HH24:MI:SS'"); $this->execute("ALTER SESSION SET NLS_TIME_TZ_FORMAT = 'HH24:MI:SS TZR'"); } }
/** * Connects to the database specified, if no connection exists * * This method is only intended to force a connection, all operations that * require a database connection will automatically call this method. * * @throws fAuthorizationException When the username and password are not accepted * * @return void */ public function connect() { // Don't try to reconnect if we are already connected if ($this->connection) { return; } $connection_error = FALSE; $authentication_error = FALSE; $database_error = FALSE; $errors = NULL; // Establish a connection to the database if ($this->extension == 'pdo') { $username = $this->username; $password = $this->password; $options = array(); if ($this->timeout !== NULL && $this->type != 'sqlite' && $this->type != 'mssql') { $options[PDO::ATTR_TIMEOUT] = $this->timeout; } if ($this->type == 'db2') { if ($this->host === NULL && $this->port === NULL) { $dsn = 'ibm:DSN:' . $this->database; } else { $dsn = 'ibm:DRIVER={IBM DB2 ODBC DRIVER};DATABASE=' . $this->database . ';HOSTNAME=' . $this->host . ';'; $dsn .= 'PORT=' . ($this->port ? $this->port : 60000) . ';'; $dsn .= 'PROTOCOL=TCPIP;UID=' . $username . ';PWD=' . $password . ';'; if ($this->timeout !== NULL) { $dsn .= 'CONNECTTIMEOUT=' . $this->timeout . ';'; } $username = NULL; $password = NULL; } } elseif ($this->type == 'mssql') { $separator = fCore::checkOS('windows') ? ',' : ':'; $port = $this->port ? $separator . $this->port : ''; $driver = fCore::checkOs('windows') ? 'mssql' : 'dblib'; $dsn = $driver . ':host=' . $this->host . $port . ';dbname=' . $this->database; // This driver does not support timeouts so we fake it here if ($this->timeout !== NULL) { fCore::startErrorCapture(); $resource = fsockopen($this->host, $this->port ? $this->port : 1433, $errno, $errstr, $this->timeout); $errors = fCore::stopErrorCapture(); if ($resource !== FALSE) { fclose($resource); } } } elseif ($this->type == 'mysql') { if (substr($this->host, 0, 5) == 'sock:') { $dsn = 'mysql:unix_socket=' . substr($this->host, 5) . ';dbname=' . $this->database; } else { $port = $this->port ? ';port=' . $this->port : ''; $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->database . $port; } } elseif ($this->type == 'oracle') { $port = $this->port ? ':' . $this->port : ''; $dsn = 'oci:dbname=' . $this->host . $port . '/' . $this->database . ';charset=AL32UTF8'; // This driver does not support timeouts so we fake it here if ($this->timeout !== NULL) { fCore::startErrorCapture(); $resource = fsockopen($this->host, $this->port ? $this->port : 1521, $errno, $errstr, $this->timeout); $errors = fCore::stopErrorCapture(); if ($resource !== FALSE) { fclose($resource); } } } elseif ($this->type == 'postgresql') { $dsn = 'pgsql:dbname=' . $this->database; if ($this->host && $this->host != 'sock:') { $dsn .= ' host=' . $this->host; } if ($this->port) { $dsn .= ' port=' . $this->port; } } elseif ($this->type == 'sqlite') { $dsn = 'sqlite:' . $this->database; } try { if ($errors) { $this->connection = FALSE; } else { $this->connection = new PDO($dsn, $username, $password, $options); if ($this->type == 'mysql') { $this->connection->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1); } } } catch (PDOException $e) { $this->connection = FALSE; $errors = $e->getMessage(); } } if ($this->extension == 'sqlite') { $this->connection = sqlite_open($this->database); } if ($this->extension == 'ibm_db2') { $username = $this->username; $password = $this->password; if ($this->host === NULL && $this->port === NULL && $this->timeout === NULL) { $connection_string = $this->database; } else { $connection_string = 'DATABASE=' . $this->database . ';HOSTNAME=' . $this->host . ';'; $connection_string .= 'PORT=' . ($this->port ? $this->port : 60000) . ';'; $connection_string .= 'PROTOCOL=TCPIP;UID=' . $this->username . ';PWD=' . $this->password . ';'; if ($this->timeout !== NULL) { $connection_string .= 'CONNECTTIMEOUT=' . $this->timeout . ';'; } $username = NULL; $password = NULL; } $options = array('autocommit' => DB2_AUTOCOMMIT_ON, 'DB2_ATTR_CASE' => DB2_CASE_LOWER); $this->connection = db2_connect($connection_string, $username, $password, $options); if ($this->connection === FALSE) { $errors = db2_conn_errormsg(); } } if ($this->extension == 'mssql') { if ($this->timeout !== NULL) { $old_timeout = ini_get('mssql.connect_timeout'); ini_set('mssql.connect_timeout', $this->timeout); } fCore::startErrorCapture(); $separator = fCore::checkOS('windows') ? ',' : ':'; if (!$this->persistent) { $this->connection = mssql_connect($this->port ? $this->host . $separator . $this->port : $this->host, $this->username, $this->password, TRUE); } else { $this->connection = mssql_pconnect($this->port ? $this->host . $separator . $this->port : $this->host, $this->username, $this->password); } if ($this->connection !== FALSE && mssql_select_db($this->database, $this->connection) === FALSE) { $this->connection = FALSE; } $errors = fCore::stopErrorCapture(); if ($this->timeout !== NULL) { ini_set('mssql.connect_timeout', $old_timeout); } } if ($this->extension == 'mysql') { if ($this->timeout !== NULL) { $old_timeout = ini_get('mysql.connect_timeout'); ini_set('mysql.connect_timeout', $this->timeout); } if (substr($this->host, 0, 5) == 'sock:') { $host = substr($this->host, 4); } elseif ($this->port) { $host = $this->host . ':' . $this->port; } else { $host = $this->host; } fCore::startErrorCapture(); $this->connection = mysql_connect($host, $this->username, $this->password, TRUE); $errors = fCore::stopErrorCapture(); if ($this->connection !== FALSE && mysql_select_db($this->database, $this->connection) === FALSE) { $errors = 'Unknown database'; $this->connection = FALSE; } if ($this->connection && function_exists('mysql_set_charset') && !mysql_set_charset('utf8', $this->connection)) { throw new fConnectivityException('There was an error setting the database connection to use UTF-8'); } if ($this->timeout !== NULL) { ini_set('mysql.connect_timeout', $old_timeout); } } if ($this->extension == 'mysqli') { $this->connection = mysqli_init(); if ($this->timeout !== NULL) { mysqli_options($this->connection, MYSQLI_OPT_CONNECT_TIMEOUT, $this->timeout); } fCore::startErrorCapture(); if (substr($this->host, 0, 5) == 'sock:') { $result = mysqli_real_connect($this->connection, 'localhost', $this->username, $this->password, $this->database, $this->port, substr($this->host, 5)); } elseif ($this->port) { $result = mysqli_real_connect($this->connection, $this->host, $this->username, $this->password, $this->database, $this->port); } else { $result = mysqli_real_connect($this->connection, $this->host, $this->username, $this->password, $this->database); } if (!$result) { $this->connection = FALSE; } $errors = fCore::stopErrorCapture(); if ($this->connection && function_exists('mysqli_set_charset') && !mysqli_set_charset($this->connection, 'utf8')) { throw new fConnectivityException('There was an error setting the database connection to use UTF-8'); } } if ($this->extension == 'oci8') { fCore::startErrorCapture(); $resource = TRUE; // This driver does not support timeouts so we fake it here if ($this->timeout !== NULL) { $resource = fsockopen($this->host, $this->port ? $this->port : 1521, $errno, $errstr, $this->timeout); if ($resource !== FALSE) { fclose($resource); $resource = TRUE; } else { $this->connection = FALSE; } } if ($resource) { $this->connection = oci_connect($this->username, $this->password, $this->host . ($this->port ? ':' . $this->port : '') . '/' . $this->database, 'AL32UTF8'); } $errors = fCore::stopErrorCapture(); } if ($this->extension == 'pgsql') { $connection_string = "dbname='" . addslashes($this->database) . "'"; if ($this->host && $this->host != 'sock:') { $connection_string .= " host='" . addslashes($this->host) . "'"; } if ($this->username) { $connection_string .= " user='******'"; } if ($this->password) { $connection_string .= " password='******'"; } if ($this->port) { $connection_string .= " port='" . $this->port . "'"; } if ($this->timeout !== NULL) { $connection_string .= " connect_timeout='" . $this->timeout . "'"; } fCore::startErrorCapture(); $this->connection = pg_connect($connection_string, PGSQL_CONNECT_FORCE_NEW); $errors = fCore::stopErrorCapture(); } if ($this->extension == 'sqlsrv') { $options = array('Database' => $this->database); if ($this->username !== NULL) { $options['UID'] = $this->username; } if ($this->password !== NULL) { $options['PWD'] = $this->password; } if ($this->timeout !== NULL) { $options['LoginTimeout'] = $this->timeout; } $this->connection = sqlsrv_connect($this->host . ',' . $this->port, $options); if ($this->connection === FALSE) { $errors = sqlsrv_errors(); } sqlsrv_configure('WarningsReturnAsErrors', 0); } // Ensure the connection was established if ($this->connection === FALSE) { $this->handleConnectionErrors($errors); } // Make MySQL act more strict and use UTF-8 if ($this->type == 'mysql') { $this->execute("SET SQL_MODE = 'REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE'"); $this->execute("SET NAMES 'utf8'"); $this->execute("SET CHARACTER SET utf8"); } // Make SQLite behave like other DBs for assoc arrays if ($this->type == 'sqlite') { $this->execute('PRAGMA short_column_names = 1'); } // Fix some issues with mssql if ($this->type == 'mssql') { if (!isset($this->schema_info['character_set'])) { $this->determineCharacterSet(); } $this->execute('SET TEXTSIZE 65536'); $this->execute('SET QUOTED_IDENTIFIER ON'); } // Make PostgreSQL use UTF-8 if ($this->type == 'postgresql') { $this->execute("SET NAMES 'UTF8'"); } // Oracle has different date and timestamp defaults if ($this->type == 'oracle') { $this->execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD'"); $this->execute("ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"); $this->execute("ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT = 'YYYY-MM-DD HH24:MI:SS TZR'"); $this->execute("ALTER SESSION SET NLS_TIME_FORMAT = 'HH24:MI:SS'"); $this->execute("ALTER SESSION SET NLS_TIME_TZ_FORMAT = 'HH24:MI:SS TZR'"); } }